David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
David Zeuthen | c612e2e | 2016-09-16 16:44:08 -0400 | [diff] [blame] | 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: |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 11 | * |
David Zeuthen | c612e2e | 2016-09-16 16:44:08 -0400 | [diff] [blame] | 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 14 | * |
David Zeuthen | c612e2e | 2016-09-16 16:44:08 -0400 | [diff] [blame] | 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. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 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 | |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 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 | |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 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 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 51 | namespace avb { |
| 52 | |
David Zeuthen | 0f7de94 | 2017-03-08 13:23:55 -0500 | [diff] [blame] | 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 | |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 58 | /* Base-class used for unit test. */ |
| 59 | class BaseAvbToolTest : public ::testing::Test { |
| 60 | public: |
| 61 | BaseAvbToolTest() {} |
| 62 | |
| 63 | protected: |
| 64 | virtual ~BaseAvbToolTest() {} |
| 65 | |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 66 | /* Generates a vbmeta image, using avbtoool, with file name |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 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", |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 89 | rollback_index, |
| 90 | additional_options.c_str(), |
| 91 | signing_options.c_str(), |
| 92 | vbmeta_image_path_.value().c_str()); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 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 | |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 101 | /* Generate a file with name |file_name| of size |image_size| with |
| 102 | * known content (0x00 0x01 0x02 .. 0xff 0x00 0x01 ..). |
| 103 | */ |
David Zeuthen | 27a291f | 2017-04-27 18:18:33 -0400 | [diff] [blame] | 104 | base::FilePath GenerateImage(const std::string file_name, |
| 105 | size_t image_size, |
| 106 | uint8_t start_byte = 0) { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 107 | std::vector<uint8_t> image; |
| 108 | image.resize(image_size); |
| 109 | for (size_t n = 0; n < image_size; n++) { |
David Zeuthen | 27a291f | 2017-04-27 18:18:33 -0400 | [diff] [blame] | 110 | image[n] = uint8_t(n + start_byte); |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 111 | } |
| 112 | base::FilePath image_path = testdir_.Append(file_name); |
| 113 | EXPECT_EQ(image_size, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 114 | static_cast<const size_t>( |
| 115 | base::WriteFile(image_path, |
| 116 | reinterpret_cast<const char*>(image.data()), |
| 117 | image.size()))); |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 118 | return image_path; |
| 119 | } |
| 120 | |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 121 | /* Returns the output of 'avbtool info_image' for a given image. */ |
| 122 | std::string InfoImage(const base::FilePath& image_path) { |
| 123 | base::FilePath tmp_path = testdir_.Append("info_output.txt"); |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 124 | EXPECT_COMMAND(0, |
| 125 | "./avbtool info_image --image %s --output %s", |
| 126 | image_path.value().c_str(), |
| 127 | tmp_path.value().c_str()); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 128 | std::string info_data; |
| 129 | EXPECT_TRUE(base::ReadFileToString(tmp_path, &info_data)); |
| 130 | return info_data; |
| 131 | } |
| 132 | |
| 133 | /* Returns public key in AVB format for a .pem key */ |
| 134 | std::string PublicKeyAVB(const base::FilePath& key_path) { |
| 135 | base::FilePath tmp_path = testdir_.Append("public_key.bin"); |
| 136 | EXPECT_COMMAND(0, |
| 137 | "./avbtool extract_public_key --key %s" |
| 138 | " --output %s", |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 139 | key_path.value().c_str(), |
| 140 | tmp_path.value().c_str()); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 141 | std::string key_data; |
| 142 | EXPECT_TRUE(base::ReadFileToString(tmp_path, &key_data)); |
| 143 | return key_data; |
| 144 | } |
| 145 | |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 146 | virtual void SetUp() override { |
David Zeuthen | 0f7de94 | 2017-03-08 13:23:55 -0500 | [diff] [blame] | 147 | /* Create temporary directory to stash images in. */ |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 148 | base::FilePath ret; |
| 149 | char* buf = strdup("/tmp/libavb-tests.XXXXXX"); |
| 150 | ASSERT_TRUE(mkdtemp(buf) != nullptr); |
| 151 | testdir_ = base::FilePath(buf); |
| 152 | free(buf); |
David Zeuthen | 0f7de94 | 2017-03-08 13:23:55 -0500 | [diff] [blame] | 153 | /* Reset memory leak tracing */ |
| 154 | avb::testing_memory_reset(); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 155 | } |
| 156 | |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 157 | virtual void TearDown() override { |
David Zeuthen | 0f7de94 | 2017-03-08 13:23:55 -0500 | [diff] [blame] | 158 | /* Nuke temporary directory. */ |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 159 | ASSERT_EQ(0U, testdir_.value().find("/tmp/libavb-tests")); |
| 160 | ASSERT_TRUE(base::DeleteFile(testdir_, true /* recursive */)); |
David Zeuthen | 0f7de94 | 2017-03-08 13:23:55 -0500 | [diff] [blame] | 161 | /* Ensure all memory has been freed. */ |
| 162 | EXPECT_TRUE(avb::testing_memory_all_freed()); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* Temporary directory created in SetUp(). */ |
| 166 | base::FilePath testdir_; |
| 167 | |
| 168 | /* Path to vbmeta image generated with GenerateVBMetaImage(). */ |
| 169 | base::FilePath vbmeta_image_path_; |
| 170 | |
| 171 | /* Contents of the image generated with GenerateVBMetaImage(). */ |
| 172 | std::vector<uint8_t> vbmeta_image_; |
| 173 | }; |
| 174 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 175 | } // namespace avb |
| 176 | |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 177 | #endif /* AVB_UNITTEST_UTIL_H_ */ |