blob: abddb28a381925175d458caa854e29baa1f8b251 [file] [log] [blame]
David Zeuthen21e95262016-07-27 17:58:40 -04001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
David Zeuthenc612e2e2016-09-16 16:44:08 -04004 * 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 Zeuthen21e95262016-07-27 17:58:40 -040011 *
David Zeuthenc612e2e2016-09-16 16:44:08 -040012 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
David Zeuthen21e95262016-07-27 17:58:40 -040014 *
David Zeuthenc612e2e2016-09-16 16:44:08 -040015 * 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 Zeuthen21e95262016-07-27 17:58:40 -040023 */
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 Zeuthen8681a332016-11-23 13:44:06 -050036// Encodes |len| bytes of |data| as a lower-case hex-string.
37std::string mem_to_hexstring(const uint8_t* data, size_t len);
38
David Zeuthen21e95262016-07-27 17:58:40 -040039/* 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 Krahn72d57902016-12-12 18:34:08 -080051namespace avb {
52
David Zeuthen0f7de942017-03-08 13:23:55 -050053// These two functions are in avb_sysdeps_posix_testing.cc and is
54// used for finding memory leaks.
55void testing_memory_reset();
56size_t testing_memory_all_freed();
57
David Zeuthen21e95262016-07-27 17:58:40 -040058/* Base-class used for unit test. */
59class BaseAvbToolTest : public ::testing::Test {
60 public:
61 BaseAvbToolTest() {}
62
63 protected:
64 virtual ~BaseAvbToolTest() {}
65
David Zeuthen8b6973b2016-09-20 12:39:49 -040066 /* Generates a vbmeta image, using avbtoool, with file name
David Zeuthen21e95262016-07-27 17:58:40 -040067 * |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 Zeuthen4b6a6342017-01-03 15:19:56 -050089 rollback_index,
90 additional_options.c_str(),
91 signing_options.c_str(),
92 vbmeta_image_path_.value().c_str());
David Zeuthen21e95262016-07-27 17:58:40 -040093 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 Zeuthen8b6973b2016-09-20 12:39:49 -0400101 /* Generate a file with name |file_name| of size |image_size| with
102 * known content (0x00 0x01 0x02 .. 0xff 0x00 0x01 ..).
103 */
David Zeuthen27a291f2017-04-27 18:18:33 -0400104 base::FilePath GenerateImage(const std::string file_name,
105 size_t image_size,
106 uint8_t start_byte = 0) {
David Zeuthen8b6973b2016-09-20 12:39:49 -0400107 std::vector<uint8_t> image;
108 image.resize(image_size);
109 for (size_t n = 0; n < image_size; n++) {
David Zeuthen27a291f2017-04-27 18:18:33 -0400110 image[n] = uint8_t(n + start_byte);
David Zeuthen8b6973b2016-09-20 12:39:49 -0400111 }
112 base::FilePath image_path = testdir_.Append(file_name);
113 EXPECT_EQ(image_size,
David Zeuthen4b6a6342017-01-03 15:19:56 -0500114 static_cast<const size_t>(
115 base::WriteFile(image_path,
116 reinterpret_cast<const char*>(image.data()),
117 image.size())));
David Zeuthen8b6973b2016-09-20 12:39:49 -0400118 return image_path;
119 }
120
David Zeuthen21e95262016-07-27 17:58:40 -0400121 /* 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 Zeuthen4b6a6342017-01-03 15:19:56 -0500124 EXPECT_COMMAND(0,
125 "./avbtool info_image --image %s --output %s",
126 image_path.value().c_str(),
127 tmp_path.value().c_str());
David Zeuthen21e95262016-07-27 17:58:40 -0400128 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 Zeuthen4b6a6342017-01-03 15:19:56 -0500139 key_path.value().c_str(),
140 tmp_path.value().c_str());
David Zeuthen21e95262016-07-27 17:58:40 -0400141 std::string key_data;
142 EXPECT_TRUE(base::ReadFileToString(tmp_path, &key_data));
143 return key_data;
144 }
145
David Zeuthen21e95262016-07-27 17:58:40 -0400146 virtual void SetUp() override {
David Zeuthen0f7de942017-03-08 13:23:55 -0500147 /* Create temporary directory to stash images in. */
David Zeuthen21e95262016-07-27 17:58:40 -0400148 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 Zeuthen0f7de942017-03-08 13:23:55 -0500153 /* Reset memory leak tracing */
154 avb::testing_memory_reset();
David Zeuthen21e95262016-07-27 17:58:40 -0400155 }
156
David Zeuthen21e95262016-07-27 17:58:40 -0400157 virtual void TearDown() override {
David Zeuthen0f7de942017-03-08 13:23:55 -0500158 /* Nuke temporary directory. */
David Zeuthen21e95262016-07-27 17:58:40 -0400159 ASSERT_EQ(0U, testdir_.value().find("/tmp/libavb-tests"));
160 ASSERT_TRUE(base::DeleteFile(testdir_, true /* recursive */));
David Zeuthen0f7de942017-03-08 13:23:55 -0500161 /* Ensure all memory has been freed. */
162 EXPECT_TRUE(avb::testing_memory_all_freed());
David Zeuthen21e95262016-07-27 17:58:40 -0400163 }
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 Krahn72d57902016-12-12 18:34:08 -0800175} // namespace avb
176
David Zeuthen21e95262016-07-27 17:58:40 -0400177#endif /* AVB_UNITTEST_UTIL_H_ */