Home | History | Annotate | Download | only in test
      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:
     61   BaseAvbToolTest() {}
     62 
     63  protected:
     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    */
    104   base::FilePath GenerateImage(const std::string file_name,
    105                                size_t image_size,
    106                                uint8_t start_byte = 0) {
    107     std::vector<uint8_t> image;
    108     image.resize(image_size);
    109     for (size_t n = 0; n < image_size; n++) {
    110       image[n] = uint8_t(n + start_byte);
    111     }
    112     base::FilePath image_path = testdir_.Append(file_name);
    113     EXPECT_EQ(image_size,
    114               static_cast<const size_t>(
    115                   base::WriteFile(image_path,
    116                                   reinterpret_cast<const char*>(image.data()),
    117                                   image.size())));
    118     return image_path;
    119   }
    120 
    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");
    124     EXPECT_COMMAND(0,
    125                    "./avbtool info_image --image %s --output %s",
    126                    image_path.value().c_str(),
    127                    tmp_path.value().c_str());
    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",
    139                    key_path.value().c_str(),
    140                    tmp_path.value().c_str());
    141     std::string key_data;
    142     EXPECT_TRUE(base::ReadFileToString(tmp_path, &key_data));
    143     return key_data;
    144   }
    145 
    146   virtual void SetUp() override {
    147     /* Create temporary directory to stash images in. */
    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);
    153     /* Reset memory leak tracing */
    154     avb::testing_memory_reset();
    155   }
    156 
    157   virtual void TearDown() override {
    158     /* Nuke temporary directory. */
    159     ASSERT_EQ(0U, testdir_.value().find("/tmp/libavb-tests"));
    160     ASSERT_TRUE(base::DeleteFile(testdir_, true /* recursive */));
    161     /* Ensure all memory has been freed. */
    162     EXPECT_TRUE(avb::testing_memory_all_freed());
    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 
    175 }  // namespace avb
    176 
    177 #endif /* AVB_UNITTEST_UTIL_H_ */
    178