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