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/stringprintf.h> 34 35 // Encodes |len| bytes of |data| as a lower-case hex-string. 36 std::string mem_to_hexstring(const uint8_t* data, size_t len); 37 38 // Trims whitespace from start and end of |str|. 39 std::string string_trim(const std::string& str); 40 41 /* Utility macro to run the command expressed by the printf()-style string 42 * |command_format| using the system(3) utility function. Will assert unless 43 * the command exits normally with exit status |expected_exit_status|. 44 */ 45 #define EXPECT_COMMAND(expected_exit_status, command_format, ...) \ 46 do { \ 47 int rc = \ 48 system(base::StringPrintf(command_format, ##__VA_ARGS__).c_str()); \ 49 EXPECT_TRUE(WIFEXITED(rc)); \ 50 EXPECT_EQ(WEXITSTATUS(rc), expected_exit_status); \ 51 } while (0); 52 53 namespace avb { 54 55 // These two functions are in avb_sysdeps_posix_testing.cc and is 56 // used for finding memory leaks. 57 void testing_memory_reset(); 58 size_t testing_memory_all_freed(); 59 60 /* Base-class used for unit test. */ 61 class BaseAvbToolTest : public ::testing::Test { 62 public: BaseAvbToolTest()63 BaseAvbToolTest() {} 64 65 protected: ~BaseAvbToolTest()66 virtual ~BaseAvbToolTest() {} 67 68 /* Calculates the vbmeta digest using 'avbtool calc_vbmeta_digest' command. */ 69 std::string CalcVBMetaDigest(const std::string& vbmeta_image, 70 const std::string& digest_alg); 71 72 /* Generates a vbmeta image, using avbtoool, with file name 73 * |image_name|. The generated vbmeta image will written to disk, 74 * see the |vbmeta_image_path_| variable for its path and 75 * |vbmeta_image_| for the content. 76 */ 77 void GenerateVBMetaImage(const std::string& image_name, 78 const std::string& algorithm, 79 uint64_t rollback_index, 80 const base::FilePath& key_path, 81 const std::string& additional_options = ""); 82 83 /* Generate a file with name |file_name| of size |image_size| with 84 * known content (0x00 0x01 0x02 .. 0xff 0x00 0x01 ..). 85 */ 86 base::FilePath GenerateImage(const std::string file_name, 87 size_t image_size, 88 uint8_t start_byte = 0); 89 90 /* Returns the output of 'avbtool info_image' for a given image. */ 91 std::string InfoImage(const base::FilePath& image_path); 92 93 /* Returns public key in AVB format for a .pem key */ 94 std::string PublicKeyAVB(const base::FilePath& key_path); 95 96 void SetUp() override; 97 void TearDown() override; 98 99 /* Temporary directory created in SetUp(). */ 100 base::FilePath testdir_; 101 102 /* Path to vbmeta image generated with GenerateVBMetaImage(). */ 103 base::FilePath vbmeta_image_path_; 104 105 /* Contents of the image generated with GenerateVBMetaImage(). */ 106 std::vector<uint8_t> vbmeta_image_; 107 }; 108 109 } // namespace avb 110 111 #endif /* AVB_UNITTEST_UTIL_H_ */ 112