1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * 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:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * 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.
23 */
24
25 #include "avb_unittest_util.h"
26
27 #include <android-base/file.h>
28
mem_to_hexstring(const uint8_t * data,size_t len)29 std::string mem_to_hexstring(const uint8_t* data, size_t len) {
30 std::string ret;
31 char digits[17] = "0123456789abcdef";
32 for (size_t n = 0; n < len; n++) {
33 ret.push_back(digits[data[n] >> 4]);
34 ret.push_back(digits[data[n] & 0x0f]);
35 }
36 return ret;
37 }
38
string_trim(const std::string & str)39 std::string string_trim(const std::string& str) {
40 size_t first = str.find_first_not_of(" \t\n");
41 if (first == std::string::npos) {
42 return str;
43 }
44 size_t last = str.find_last_not_of(" \t\n");
45 return str.substr(first, (last - first + 1));
46 }
47
48 namespace avb {
49
SetUp()50 void BaseAvbToolTest::SetUp() {
51 /* Change current directory to test executable directory so that relative path
52 * references to test dependencies don't rely on being manually run from
53 * correct directory */
54 ASSERT_TRUE(chdir(android::base::GetExecutableDirectory().c_str()) == 0);
55
56 /* Create temporary directory to stash images in. */
57 char* buf = strdup("/tmp/libavb-tests.XXXXXX");
58 ASSERT_TRUE(mkdtemp(buf) != nullptr);
59 testdir_ = buf;
60 free(buf);
61
62 /* Reset memory leak tracing */
63 avb::testing_memory_reset();
64 }
65
TearDown()66 void BaseAvbToolTest::TearDown() {
67 /* Nuke temporary directory. */
68 ASSERT_EQ(0U, testdir_.string().find("/tmp/libavb-tests"));
69 ASSERT_TRUE(
70 base::DeleteFile(base::FilePath(testdir_.c_str()), true /* recursive */));
71 /* Ensure all memory has been freed. */
72 EXPECT_TRUE(avb::testing_memory_all_freed());
73 }
74
CalcVBMetaDigest(const std::string & vbmeta_image,const std::string & digest_alg)75 std::string BaseAvbToolTest::CalcVBMetaDigest(const std::string& vbmeta_image,
76 const std::string& digest_alg) {
77 std::filesystem::path vbmeta_path = testdir_ / vbmeta_image;
78 std::filesystem::path vbmeta_digest_path = testdir_ / "vbmeta_digest";
79 EXPECT_COMMAND(
80 0,
81 "./avbtool.py calculate_vbmeta_digest --image %s --hash_algorithm %s"
82 " --output %s",
83 vbmeta_path.c_str(),
84 digest_alg.c_str(),
85 vbmeta_digest_path.c_str());
86 std::string vbmeta_digest_data;
87 EXPECT_TRUE(android::base::ReadFileToString(vbmeta_digest_path.string(),
88 &vbmeta_digest_data));
89 return string_trim(vbmeta_digest_data);
90 }
91
GenerateVBMetaImage(const std::string & image_name,const std::string & algorithm,uint64_t rollback_index,const std::string & key_path,const std::string & additional_options)92 void BaseAvbToolTest::GenerateVBMetaImage(
93 const std::string& image_name,
94 const std::string& algorithm,
95 uint64_t rollback_index,
96 const std::string& key_path,
97 const std::string& additional_options) {
98 std::string signing_options;
99 if (algorithm == "") {
100 signing_options = " --algorithm NONE ";
101 } else {
102 signing_options =
103 std::string(" --algorithm ") + algorithm + " --key " + key_path + " ";
104 }
105 vbmeta_image_path_ = testdir_ / image_name;
106 EXPECT_COMMAND(0,
107 "./avbtool.py make_vbmeta_image"
108 " --rollback_index %" PRIu64
109 " %s %s "
110 " --output %s",
111 rollback_index,
112 additional_options.c_str(),
113 signing_options.c_str(),
114 vbmeta_image_path_.c_str());
115 int64_t file_size;
116 ASSERT_TRUE(base::GetFileSize(base::FilePath(vbmeta_image_path_.c_str()),
117 &file_size));
118 vbmeta_image_.resize(file_size);
119 ASSERT_TRUE(base::ReadFile(base::FilePath(vbmeta_image_path_.c_str()),
120 reinterpret_cast<char*>(vbmeta_image_.data()),
121 vbmeta_image_.size()));
122 }
123
124 /* Generate a file with name |file_name| of size |image_size| with
125 * known content (0x00 0x01 0x02 .. 0xff 0x00 0x01 ..).
126 */
GenerateImage(const std::string file_name,size_t image_size,uint8_t start_byte)127 std::string BaseAvbToolTest::GenerateImage(const std::string file_name,
128 size_t image_size,
129 uint8_t start_byte) {
130 std::filesystem::path image_path = testdir_ / file_name;
131 EXPECT_COMMAND(0,
132 "./avbtool.py generate_test_image "
133 "--image_size %d "
134 "--start_byte %d "
135 "--output %s",
136 image_size,
137 start_byte,
138 image_path.c_str());
139 base::File::Info stats;
140 EXPECT_TRUE(base::GetFileInfo(base::FilePath(image_path.c_str()), &stats));
141 EXPECT_EQ((size_t)stats.size, image_size);
142 return image_path.string();
143 }
144
InfoImage(const std::string & image_path)145 std::string BaseAvbToolTest::InfoImage(const std::string& image_path) {
146 std::filesystem::path tmp_path = testdir_ / "info_output.txt";
147 EXPECT_COMMAND(0,
148 "./avbtool.py info_image --image %s --output %s",
149 image_path.c_str(),
150 tmp_path.c_str());
151 std::string info_data;
152 EXPECT_TRUE(android::base::ReadFileToString(tmp_path.string(), &info_data));
153 return info_data;
154 }
155
PublicKeyAVB(const std::string & key_path)156 std::string BaseAvbToolTest::PublicKeyAVB(const std::string& key_path) {
157 std::filesystem::path tmp_path = testdir_ / "public_key.bin";
158 EXPECT_COMMAND(0,
159 "./avbtool.py extract_public_key --key %s"
160 " --output %s",
161 key_path.c_str(),
162 tmp_path.c_str());
163 std::string key_data;
164 EXPECT_TRUE(android::base::ReadFileToString(tmp_path.string(), &key_data));
165 return key_data;
166 }
167
168 } // namespace avb
169