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 #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 36 // Encodes |len| bytes of |data| as a lower-case hex-string. 37 std::string mem_to_hexstring(const uint8_t* data, size_t len); 38 39 /* 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 51 namespace avb { 52 53 // These two functions are in avb_sysdeps_posix_testing.cc and is 54 // used for finding memory leaks. 55 void testing_memory_reset(); 56 size_t testing_memory_all_freed(); 57 58 /* Base-class used for unit test. */ 59 class BaseAvbToolTest : public ::testing::Test { 60 public: BaseAvbToolTest()61 BaseAvbToolTest() {} 62 63 protected: ~BaseAvbToolTest()64 virtual ~BaseAvbToolTest() {} 65 66 /* Generates a vbmeta image, using avbtoool, with file name 67 * |image_name|. The generated vbmeta image will written to disk, 68 * see the |vbmeta_image_path_| variable for its path and 69 * |vbmeta_image_| for the content. 70 */ 71 void GenerateVBMetaImage(const std::string& image_name, 72 const std::string& algorithm, 73 uint64_t rollback_index, 74 const base::FilePath& key_path, 75 const std::string& additional_options = "") { 76 std::string signing_options; 77 if (algorithm == "") { 78 signing_options = " --algorithm NONE "; 79 } else { 80 signing_options = std::string(" --algorithm ") + algorithm + " --key " + 81 key_path.value() + " "; 82 } 83 vbmeta_image_path_ = testdir_.Append(image_name); 84 EXPECT_COMMAND(0, 85 "./avbtool make_vbmeta_image" 86 " --rollback_index %" PRIu64 87 " %s %s " 88 " --output %s", 89 rollback_index, 90 additional_options.c_str(), 91 signing_options.c_str(), 92 vbmeta_image_path_.value().c_str()); 93 int64_t file_size; 94 ASSERT_TRUE(base::GetFileSize(vbmeta_image_path_, &file_size)); 95 vbmeta_image_.resize(file_size); 96 ASSERT_TRUE(base::ReadFile(vbmeta_image_path_, 97 reinterpret_cast<char*>(vbmeta_image_.data()), 98 vbmeta_image_.size())); 99 } 100 101 /* Generate a file with name |file_name| of size |image_size| with 102 * known content (0x00 0x01 0x02 .. 0xff 0x00 0x01 ..). 103 */ GenerateImage(const std::string file_name,size_t image_size)104 base::FilePath GenerateImage(const std::string file_name, size_t image_size) { 105 std::vector<uint8_t> image; 106 image.resize(image_size); 107 for (size_t n = 0; n < image_size; n++) { 108 image[n] = uint8_t(n); 109 } 110 base::FilePath image_path = testdir_.Append(file_name); 111 EXPECT_EQ(image_size, 112 static_cast<const size_t>( 113 base::WriteFile(image_path, 114 reinterpret_cast<const char*>(image.data()), 115 image.size()))); 116 return image_path; 117 } 118 119 /* Returns the output of 'avbtool info_image' for a given image. */ InfoImage(const base::FilePath & image_path)120 std::string InfoImage(const base::FilePath& image_path) { 121 base::FilePath tmp_path = testdir_.Append("info_output.txt"); 122 EXPECT_COMMAND(0, 123 "./avbtool info_image --image %s --output %s", 124 image_path.value().c_str(), 125 tmp_path.value().c_str()); 126 std::string info_data; 127 EXPECT_TRUE(base::ReadFileToString(tmp_path, &info_data)); 128 return info_data; 129 } 130 131 /* Returns public key in AVB format for a .pem key */ PublicKeyAVB(const base::FilePath & key_path)132 std::string PublicKeyAVB(const base::FilePath& key_path) { 133 base::FilePath tmp_path = testdir_.Append("public_key.bin"); 134 EXPECT_COMMAND(0, 135 "./avbtool extract_public_key --key %s" 136 " --output %s", 137 key_path.value().c_str(), 138 tmp_path.value().c_str()); 139 std::string key_data; 140 EXPECT_TRUE(base::ReadFileToString(tmp_path, &key_data)); 141 return key_data; 142 } 143 SetUp()144 virtual void SetUp() override { 145 /* Create temporary directory to stash images in. */ 146 base::FilePath ret; 147 char* buf = strdup("/tmp/libavb-tests.XXXXXX"); 148 ASSERT_TRUE(mkdtemp(buf) != nullptr); 149 testdir_ = base::FilePath(buf); 150 free(buf); 151 /* Reset memory leak tracing */ 152 avb::testing_memory_reset(); 153 } 154 TearDown()155 virtual void TearDown() override { 156 /* Nuke temporary directory. */ 157 ASSERT_EQ(0U, testdir_.value().find("/tmp/libavb-tests")); 158 ASSERT_TRUE(base::DeleteFile(testdir_, true /* recursive */)); 159 /* Ensure all memory has been freed. */ 160 EXPECT_TRUE(avb::testing_memory_all_freed()); 161 } 162 163 /* Temporary directory created in SetUp(). */ 164 base::FilePath testdir_; 165 166 /* Path to vbmeta image generated with GenerateVBMetaImage(). */ 167 base::FilePath vbmeta_image_path_; 168 169 /* Contents of the image generated with GenerateVBMetaImage(). */ 170 std::vector<uint8_t> vbmeta_image_; 171 }; 172 173 } // namespace avb 174 175 #endif /* AVB_UNITTEST_UTIL_H_ */ 176