xref: /aosp_15_r20/external/avb/test/avb_unittest_util.cc (revision d289c2ba6de359471b23d594623b906876bc48a0)
1*d289c2baSAndroid Build Coastguard Worker /*
2*d289c2baSAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*d289c2baSAndroid Build Coastguard Worker  *
4*d289c2baSAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person
5*d289c2baSAndroid Build Coastguard Worker  * obtaining a copy of this software and associated documentation
6*d289c2baSAndroid Build Coastguard Worker  * files (the "Software"), to deal in the Software without
7*d289c2baSAndroid Build Coastguard Worker  * restriction, including without limitation the rights to use, copy,
8*d289c2baSAndroid Build Coastguard Worker  * modify, merge, publish, distribute, sublicense, and/or sell copies
9*d289c2baSAndroid Build Coastguard Worker  * of the Software, and to permit persons to whom the Software is
10*d289c2baSAndroid Build Coastguard Worker  * furnished to do so, subject to the following conditions:
11*d289c2baSAndroid Build Coastguard Worker  *
12*d289c2baSAndroid Build Coastguard Worker  * The above copyright notice and this permission notice shall be
13*d289c2baSAndroid Build Coastguard Worker  * included in all copies or substantial portions of the Software.
14*d289c2baSAndroid Build Coastguard Worker  *
15*d289c2baSAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*d289c2baSAndroid Build Coastguard Worker  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*d289c2baSAndroid Build Coastguard Worker  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*d289c2baSAndroid Build Coastguard Worker  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*d289c2baSAndroid Build Coastguard Worker  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*d289c2baSAndroid Build Coastguard Worker  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*d289c2baSAndroid Build Coastguard Worker  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*d289c2baSAndroid Build Coastguard Worker  * SOFTWARE.
23*d289c2baSAndroid Build Coastguard Worker  */
24*d289c2baSAndroid Build Coastguard Worker 
25*d289c2baSAndroid Build Coastguard Worker #include "avb_unittest_util.h"
26*d289c2baSAndroid Build Coastguard Worker 
27*d289c2baSAndroid Build Coastguard Worker #include <android-base/file.h>
28*d289c2baSAndroid Build Coastguard Worker 
mem_to_hexstring(const uint8_t * data,size_t len)29*d289c2baSAndroid Build Coastguard Worker std::string mem_to_hexstring(const uint8_t* data, size_t len) {
30*d289c2baSAndroid Build Coastguard Worker   std::string ret;
31*d289c2baSAndroid Build Coastguard Worker   char digits[17] = "0123456789abcdef";
32*d289c2baSAndroid Build Coastguard Worker   for (size_t n = 0; n < len; n++) {
33*d289c2baSAndroid Build Coastguard Worker     ret.push_back(digits[data[n] >> 4]);
34*d289c2baSAndroid Build Coastguard Worker     ret.push_back(digits[data[n] & 0x0f]);
35*d289c2baSAndroid Build Coastguard Worker   }
36*d289c2baSAndroid Build Coastguard Worker   return ret;
37*d289c2baSAndroid Build Coastguard Worker }
38*d289c2baSAndroid Build Coastguard Worker 
string_trim(const std::string & str)39*d289c2baSAndroid Build Coastguard Worker std::string string_trim(const std::string& str) {
40*d289c2baSAndroid Build Coastguard Worker   size_t first = str.find_first_not_of(" \t\n");
41*d289c2baSAndroid Build Coastguard Worker   if (first == std::string::npos) {
42*d289c2baSAndroid Build Coastguard Worker     return str;
43*d289c2baSAndroid Build Coastguard Worker   }
44*d289c2baSAndroid Build Coastguard Worker   size_t last = str.find_last_not_of(" \t\n");
45*d289c2baSAndroid Build Coastguard Worker   return str.substr(first, (last - first + 1));
46*d289c2baSAndroid Build Coastguard Worker }
47*d289c2baSAndroid Build Coastguard Worker 
48*d289c2baSAndroid Build Coastguard Worker namespace avb {
49*d289c2baSAndroid Build Coastguard Worker 
SetUp()50*d289c2baSAndroid Build Coastguard Worker void BaseAvbToolTest::SetUp() {
51*d289c2baSAndroid Build Coastguard Worker   /* Change current directory to test executable directory so that relative path
52*d289c2baSAndroid Build Coastguard Worker    * references to test dependencies don't rely on being manually run from
53*d289c2baSAndroid Build Coastguard Worker    * correct directory */
54*d289c2baSAndroid Build Coastguard Worker   ASSERT_TRUE(chdir(android::base::GetExecutableDirectory().c_str()) == 0);
55*d289c2baSAndroid Build Coastguard Worker 
56*d289c2baSAndroid Build Coastguard Worker   /* Create temporary directory to stash images in. */
57*d289c2baSAndroid Build Coastguard Worker   char* buf = strdup("/tmp/libavb-tests.XXXXXX");
58*d289c2baSAndroid Build Coastguard Worker   ASSERT_TRUE(mkdtemp(buf) != nullptr);
59*d289c2baSAndroid Build Coastguard Worker   testdir_ = buf;
60*d289c2baSAndroid Build Coastguard Worker   free(buf);
61*d289c2baSAndroid Build Coastguard Worker 
62*d289c2baSAndroid Build Coastguard Worker   /* Reset memory leak tracing */
63*d289c2baSAndroid Build Coastguard Worker   avb::testing_memory_reset();
64*d289c2baSAndroid Build Coastguard Worker }
65*d289c2baSAndroid Build Coastguard Worker 
TearDown()66*d289c2baSAndroid Build Coastguard Worker void BaseAvbToolTest::TearDown() {
67*d289c2baSAndroid Build Coastguard Worker   /* Nuke temporary directory. */
68*d289c2baSAndroid Build Coastguard Worker   ASSERT_EQ(0U, testdir_.string().find("/tmp/libavb-tests"));
69*d289c2baSAndroid Build Coastguard Worker   ASSERT_TRUE(
70*d289c2baSAndroid Build Coastguard Worker       base::DeleteFile(base::FilePath(testdir_.c_str()), true /* recursive */));
71*d289c2baSAndroid Build Coastguard Worker   /* Ensure all memory has been freed. */
72*d289c2baSAndroid Build Coastguard Worker   EXPECT_TRUE(avb::testing_memory_all_freed());
73*d289c2baSAndroid Build Coastguard Worker }
74*d289c2baSAndroid Build Coastguard Worker 
CalcVBMetaDigest(const std::string & vbmeta_image,const std::string & digest_alg)75*d289c2baSAndroid Build Coastguard Worker std::string BaseAvbToolTest::CalcVBMetaDigest(const std::string& vbmeta_image,
76*d289c2baSAndroid Build Coastguard Worker                                               const std::string& digest_alg) {
77*d289c2baSAndroid Build Coastguard Worker   std::filesystem::path vbmeta_path = testdir_ / vbmeta_image;
78*d289c2baSAndroid Build Coastguard Worker   std::filesystem::path vbmeta_digest_path = testdir_ / "vbmeta_digest";
79*d289c2baSAndroid Build Coastguard Worker   EXPECT_COMMAND(
80*d289c2baSAndroid Build Coastguard Worker       0,
81*d289c2baSAndroid Build Coastguard Worker       "./avbtool.py calculate_vbmeta_digest --image %s --hash_algorithm %s"
82*d289c2baSAndroid Build Coastguard Worker       " --output %s",
83*d289c2baSAndroid Build Coastguard Worker       vbmeta_path.c_str(),
84*d289c2baSAndroid Build Coastguard Worker       digest_alg.c_str(),
85*d289c2baSAndroid Build Coastguard Worker       vbmeta_digest_path.c_str());
86*d289c2baSAndroid Build Coastguard Worker   std::string vbmeta_digest_data;
87*d289c2baSAndroid Build Coastguard Worker   EXPECT_TRUE(android::base::ReadFileToString(vbmeta_digest_path.string(),
88*d289c2baSAndroid Build Coastguard Worker                                               &vbmeta_digest_data));
89*d289c2baSAndroid Build Coastguard Worker   return string_trim(vbmeta_digest_data);
90*d289c2baSAndroid Build Coastguard Worker }
91*d289c2baSAndroid Build Coastguard Worker 
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*d289c2baSAndroid Build Coastguard Worker void BaseAvbToolTest::GenerateVBMetaImage(
93*d289c2baSAndroid Build Coastguard Worker     const std::string& image_name,
94*d289c2baSAndroid Build Coastguard Worker     const std::string& algorithm,
95*d289c2baSAndroid Build Coastguard Worker     uint64_t rollback_index,
96*d289c2baSAndroid Build Coastguard Worker     const std::string& key_path,
97*d289c2baSAndroid Build Coastguard Worker     const std::string& additional_options) {
98*d289c2baSAndroid Build Coastguard Worker   std::string signing_options;
99*d289c2baSAndroid Build Coastguard Worker   if (algorithm == "") {
100*d289c2baSAndroid Build Coastguard Worker     signing_options = " --algorithm NONE ";
101*d289c2baSAndroid Build Coastguard Worker   } else {
102*d289c2baSAndroid Build Coastguard Worker     signing_options =
103*d289c2baSAndroid Build Coastguard Worker         std::string(" --algorithm ") + algorithm + " --key " + key_path + " ";
104*d289c2baSAndroid Build Coastguard Worker   }
105*d289c2baSAndroid Build Coastguard Worker   vbmeta_image_path_ = testdir_ / image_name;
106*d289c2baSAndroid Build Coastguard Worker   EXPECT_COMMAND(0,
107*d289c2baSAndroid Build Coastguard Worker                  "./avbtool.py make_vbmeta_image"
108*d289c2baSAndroid Build Coastguard Worker                  " --rollback_index %" PRIu64
109*d289c2baSAndroid Build Coastguard Worker                  " %s %s "
110*d289c2baSAndroid Build Coastguard Worker                  " --output %s",
111*d289c2baSAndroid Build Coastguard Worker                  rollback_index,
112*d289c2baSAndroid Build Coastguard Worker                  additional_options.c_str(),
113*d289c2baSAndroid Build Coastguard Worker                  signing_options.c_str(),
114*d289c2baSAndroid Build Coastguard Worker                  vbmeta_image_path_.c_str());
115*d289c2baSAndroid Build Coastguard Worker   int64_t file_size;
116*d289c2baSAndroid Build Coastguard Worker   ASSERT_TRUE(base::GetFileSize(base::FilePath(vbmeta_image_path_.c_str()),
117*d289c2baSAndroid Build Coastguard Worker                                 &file_size));
118*d289c2baSAndroid Build Coastguard Worker   vbmeta_image_.resize(file_size);
119*d289c2baSAndroid Build Coastguard Worker   ASSERT_TRUE(base::ReadFile(base::FilePath(vbmeta_image_path_.c_str()),
120*d289c2baSAndroid Build Coastguard Worker                              reinterpret_cast<char*>(vbmeta_image_.data()),
121*d289c2baSAndroid Build Coastguard Worker                              vbmeta_image_.size()));
122*d289c2baSAndroid Build Coastguard Worker }
123*d289c2baSAndroid Build Coastguard Worker 
124*d289c2baSAndroid Build Coastguard Worker /* Generate a file with name |file_name| of size |image_size| with
125*d289c2baSAndroid Build Coastguard Worker  * known content (0x00 0x01 0x02 .. 0xff 0x00 0x01 ..).
126*d289c2baSAndroid Build Coastguard Worker  */
GenerateImage(const std::string file_name,size_t image_size,uint8_t start_byte)127*d289c2baSAndroid Build Coastguard Worker std::string BaseAvbToolTest::GenerateImage(const std::string file_name,
128*d289c2baSAndroid Build Coastguard Worker                                            size_t image_size,
129*d289c2baSAndroid Build Coastguard Worker                                            uint8_t start_byte) {
130*d289c2baSAndroid Build Coastguard Worker   std::filesystem::path image_path = testdir_ / file_name;
131*d289c2baSAndroid Build Coastguard Worker   EXPECT_COMMAND(0,
132*d289c2baSAndroid Build Coastguard Worker                  "./avbtool.py generate_test_image "
133*d289c2baSAndroid Build Coastguard Worker                  "--image_size %d "
134*d289c2baSAndroid Build Coastguard Worker                  "--start_byte %d "
135*d289c2baSAndroid Build Coastguard Worker                  "--output %s",
136*d289c2baSAndroid Build Coastguard Worker                  image_size,
137*d289c2baSAndroid Build Coastguard Worker                  start_byte,
138*d289c2baSAndroid Build Coastguard Worker                  image_path.c_str());
139*d289c2baSAndroid Build Coastguard Worker   base::File::Info stats;
140*d289c2baSAndroid Build Coastguard Worker   EXPECT_TRUE(base::GetFileInfo(base::FilePath(image_path.c_str()), &stats));
141*d289c2baSAndroid Build Coastguard Worker   EXPECT_EQ((size_t)stats.size, image_size);
142*d289c2baSAndroid Build Coastguard Worker   return image_path.string();
143*d289c2baSAndroid Build Coastguard Worker }
144*d289c2baSAndroid Build Coastguard Worker 
InfoImage(const std::string & image_path)145*d289c2baSAndroid Build Coastguard Worker std::string BaseAvbToolTest::InfoImage(const std::string& image_path) {
146*d289c2baSAndroid Build Coastguard Worker   std::filesystem::path tmp_path = testdir_ / "info_output.txt";
147*d289c2baSAndroid Build Coastguard Worker   EXPECT_COMMAND(0,
148*d289c2baSAndroid Build Coastguard Worker                  "./avbtool.py info_image --image %s --output %s",
149*d289c2baSAndroid Build Coastguard Worker                  image_path.c_str(),
150*d289c2baSAndroid Build Coastguard Worker                  tmp_path.c_str());
151*d289c2baSAndroid Build Coastguard Worker   std::string info_data;
152*d289c2baSAndroid Build Coastguard Worker   EXPECT_TRUE(android::base::ReadFileToString(tmp_path.string(), &info_data));
153*d289c2baSAndroid Build Coastguard Worker   return info_data;
154*d289c2baSAndroid Build Coastguard Worker }
155*d289c2baSAndroid Build Coastguard Worker 
PublicKeyAVB(const std::string & key_path)156*d289c2baSAndroid Build Coastguard Worker std::string BaseAvbToolTest::PublicKeyAVB(const std::string& key_path) {
157*d289c2baSAndroid Build Coastguard Worker   std::filesystem::path tmp_path = testdir_ / "public_key.bin";
158*d289c2baSAndroid Build Coastguard Worker   EXPECT_COMMAND(0,
159*d289c2baSAndroid Build Coastguard Worker                  "./avbtool.py extract_public_key --key %s"
160*d289c2baSAndroid Build Coastguard Worker                  " --output %s",
161*d289c2baSAndroid Build Coastguard Worker                  key_path.c_str(),
162*d289c2baSAndroid Build Coastguard Worker                  tmp_path.c_str());
163*d289c2baSAndroid Build Coastguard Worker   std::string key_data;
164*d289c2baSAndroid Build Coastguard Worker   EXPECT_TRUE(android::base::ReadFileToString(tmp_path.string(), &key_data));
165*d289c2baSAndroid Build Coastguard Worker   return key_data;
166*d289c2baSAndroid Build Coastguard Worker }
167*d289c2baSAndroid Build Coastguard Worker 
168*d289c2baSAndroid Build Coastguard Worker }  // namespace avb
169