blob: 05390e496c95c6d48502cdf3f142e48c4084b269 [file] [log] [blame]
David Zeuthen21e95262016-07-27 17:58:40 -04001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
David Zeuthenc612e2e2016-09-16 16:44:08 -04004 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
David Zeuthen21e95262016-07-27 17:58:40 -040011 *
David Zeuthenc612e2e2016-09-16 16:44:08 -040012 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
David Zeuthen21e95262016-07-27 17:58:40 -040014 *
David Zeuthenc612e2e2016-09-16 16:44:08 -040015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
David Zeuthen21e95262016-07-27 17:58:40 -040023 */
24
25#ifndef AVB_UNITTEST_UTIL_H_
26#define AVB_UNITTEST_UTIL_H_
27
28#include <inttypes.h>
29
30#include <gtest/gtest.h>
31
32#include <base/files/file_util.h>
33#include <base/strings/string_util.h>
34#include <base/strings/stringprintf.h>
35
David Zeuthen8681a332016-11-23 13:44:06 -050036// Encodes |len| bytes of |data| as a lower-case hex-string.
37std::string mem_to_hexstring(const uint8_t* data, size_t len);
38
David Zeuthen21e95262016-07-27 17:58:40 -040039/* Utility macro to run the command expressed by the printf()-style string
40 * |command_format| using the system(3) utility function. Will assert unless
41 * the command exits normally with exit status |expected_exit_status|.
42 */
43#define EXPECT_COMMAND(expected_exit_status, command_format, ...) \
44 do { \
45 int rc = \
46 system(base::StringPrintf(command_format, ##__VA_ARGS__).c_str()); \
47 EXPECT_TRUE(WIFEXITED(rc)); \
48 EXPECT_EQ(WEXITSTATUS(rc), expected_exit_status); \
49 } while (0);
50
Darren Krahn72d57902016-12-12 18:34:08 -080051namespace avb {
52
David Zeuthen21e95262016-07-27 17:58:40 -040053/* Base-class used for unit test. */
54class BaseAvbToolTest : public ::testing::Test {
55 public:
56 BaseAvbToolTest() {}
57
58 protected:
59 virtual ~BaseAvbToolTest() {}
60
David Zeuthen8b6973b2016-09-20 12:39:49 -040061 /* Generates a vbmeta image, using avbtoool, with file name
David Zeuthen21e95262016-07-27 17:58:40 -040062 * |image_name|. The generated vbmeta image will written to disk,
63 * see the |vbmeta_image_path_| variable for its path and
64 * |vbmeta_image_| for the content.
65 */
66 void GenerateVBMetaImage(const std::string& image_name,
67 const std::string& algorithm,
68 uint64_t rollback_index,
69 const base::FilePath& key_path,
70 const std::string& additional_options = "") {
71 std::string signing_options;
72 if (algorithm == "") {
73 signing_options = " --algorithm NONE ";
74 } else {
75 signing_options = std::string(" --algorithm ") + algorithm + " --key " +
76 key_path.value() + " ";
77 }
78 vbmeta_image_path_ = testdir_.Append(image_name);
79 EXPECT_COMMAND(0,
80 "./avbtool make_vbmeta_image"
81 " --rollback_index %" PRIu64
82 " %s %s "
83 " --output %s",
84 rollback_index, additional_options.c_str(),
85 signing_options.c_str(), vbmeta_image_path_.value().c_str());
86 int64_t file_size;
87 ASSERT_TRUE(base::GetFileSize(vbmeta_image_path_, &file_size));
88 vbmeta_image_.resize(file_size);
89 ASSERT_TRUE(base::ReadFile(vbmeta_image_path_,
90 reinterpret_cast<char*>(vbmeta_image_.data()),
91 vbmeta_image_.size()));
92 }
93
David Zeuthen8b6973b2016-09-20 12:39:49 -040094 /* Generate a file with name |file_name| of size |image_size| with
95 * known content (0x00 0x01 0x02 .. 0xff 0x00 0x01 ..).
96 */
97 base::FilePath GenerateImage(const std::string file_name, size_t image_size) {
98 std::vector<uint8_t> image;
99 image.resize(image_size);
100 for (size_t n = 0; n < image_size; n++) {
101 image[n] = uint8_t(n);
102 }
103 base::FilePath image_path = testdir_.Append(file_name);
104 EXPECT_EQ(image_size,
105 static_cast<const size_t>(base::WriteFile(
106 image_path, reinterpret_cast<const char*>(image.data()),
107 image.size())));
108 return image_path;
109 }
110
David Zeuthen21e95262016-07-27 17:58:40 -0400111 /* Returns the output of 'avbtool info_image' for a given image. */
112 std::string InfoImage(const base::FilePath& image_path) {
113 base::FilePath tmp_path = testdir_.Append("info_output.txt");
114 EXPECT_COMMAND(0, "./avbtool info_image --image %s --output %s",
115 image_path.value().c_str(), tmp_path.value().c_str());
116 std::string info_data;
117 EXPECT_TRUE(base::ReadFileToString(tmp_path, &info_data));
118 return info_data;
119 }
120
121 /* Returns public key in AVB format for a .pem key */
122 std::string PublicKeyAVB(const base::FilePath& key_path) {
123 base::FilePath tmp_path = testdir_.Append("public_key.bin");
124 EXPECT_COMMAND(0,
125 "./avbtool extract_public_key --key %s"
126 " --output %s",
127 key_path.value().c_str(), tmp_path.value().c_str());
128 std::string key_data;
129 EXPECT_TRUE(base::ReadFileToString(tmp_path, &key_data));
130 return key_data;
131 }
132
133 /* Create temporary directory to stash images in. */
134 virtual void SetUp() override {
135 base::FilePath ret;
136 char* buf = strdup("/tmp/libavb-tests.XXXXXX");
137 ASSERT_TRUE(mkdtemp(buf) != nullptr);
138 testdir_ = base::FilePath(buf);
139 free(buf);
140 }
141
142 /* Nuke temporary directory. */
143 virtual void TearDown() override {
144 ASSERT_EQ(0U, testdir_.value().find("/tmp/libavb-tests"));
145 ASSERT_TRUE(base::DeleteFile(testdir_, true /* recursive */));
146 }
147
148 /* Temporary directory created in SetUp(). */
149 base::FilePath testdir_;
150
151 /* Path to vbmeta image generated with GenerateVBMetaImage(). */
152 base::FilePath vbmeta_image_path_;
153
154 /* Contents of the image generated with GenerateVBMetaImage(). */
155 std::vector<uint8_t> vbmeta_image_;
156};
157
Darren Krahn72d57902016-12-12 18:34:08 -0800158} // namespace avb
159
David Zeuthen21e95262016-07-27 17:58:40 -0400160#endif /* AVB_UNITTEST_UTIL_H_ */