1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <fs_avb/fs_avb_util.h>
18 
19 #include "fs_avb_test_util.h"
20 
21 namespace fs_avb_host_test {
22 
23 class PublicFsAvbUtilTest : public BaseFsAvbTest {
24   public:
PublicFsAvbUtilTest()25     PublicFsAvbUtilTest(){};
26 
27   protected:
~PublicFsAvbUtilTest()28     ~PublicFsAvbUtilTest(){};
29 };
30 
TEST_F(PublicFsAvbUtilTest,GetHashtreeDescriptor)31 TEST_F(PublicFsAvbUtilTest, GetHashtreeDescriptor) {
32     // Generates a raw system_other.img, use a smaller size to speed-up unit test.
33     const size_t system_image_size = 10 * 1024 * 1024;
34     const size_t system_partition_size = 15 * 1024 * 1024;
35     base::FilePath system_path = GenerateImage("system.img", system_image_size);
36 
37     // Adds AVB Hashtree Footer.
38     AddAvbFooter(system_path, "hashtree", "system", system_partition_size, "SHA512_RSA4096", 20,
39                  data_dir_.Append("testkey_rsa4096.pem"), "d00df00d",
40                  "--internal_release_string \"unit test\"");
41 
42     auto system_vbmeta = ExtractAndLoadVBMetaData(system_path, "system-vbmeta.img");
43 
44     auto hashtree_desc =
45             GetHashtreeDescriptor("system" /* avb_partition_name */, std::move(system_vbmeta));
46     EXPECT_NE(nullptr, hashtree_desc);
47 
48     // Checks the returned hashtree_desc matches the following info returned by avbtool.
49     EXPECT_EQ(
50             "Footer version:           1.0\n"
51             "Image size:               15728640 bytes\n"
52             "Original image size:      10485760 bytes\n"
53             "VBMeta offset:            10661888\n"
54             "VBMeta size:              2112 bytes\n"
55             "--\n"
56             "Minimum libavb version:   1.0\n"
57             "Header Block:             256 bytes\n"
58             "Authentication Block:     576 bytes\n"
59             "Auxiliary Block:          1280 bytes\n"
60             "Algorithm:                SHA512_RSA4096\n"
61             "Rollback Index:           20\n"
62             "Flags:                    0\n"
63             "Release String:           'unit test'\n"
64             "Descriptors:\n"
65             "    Hashtree descriptor:\n"
66             "      Version of dm-verity:  1\n"
67             "      Image Size:            10485760 bytes\n"
68             "      Tree Offset:           10485760\n"
69             "      Tree Size:             86016 bytes\n"
70             "      Data Block Size:       4096 bytes\n"
71             "      Hash Block Size:       4096 bytes\n"
72             "      FEC num roots:         2\n"
73             "      FEC offset:            10571776\n"
74             "      FEC size:              90112 bytes\n"
75             "      Hash Algorithm:        sha1\n"
76             "      Partition Name:        system\n"
77             "      Salt:                  d00df00d\n"
78             "      Root Digest:           a3d5dd307341393d85de356c384ff543ec1ed81b\n"
79             "      Flags:                 0\n",
80             InfoImage(system_path));
81 
82     EXPECT_EQ(1UL, hashtree_desc->dm_verity_version);
83     EXPECT_EQ(10485760UL, hashtree_desc->image_size);
84     EXPECT_EQ(10485760UL, hashtree_desc->tree_offset);
85     EXPECT_EQ(86016UL, hashtree_desc->tree_size);
86     EXPECT_EQ(4096UL, hashtree_desc->data_block_size);
87     EXPECT_EQ(4096UL, hashtree_desc->hash_block_size);
88     EXPECT_EQ(2UL, hashtree_desc->fec_num_roots);
89     EXPECT_EQ(10571776UL, hashtree_desc->fec_offset);
90     EXPECT_EQ(90112UL, hashtree_desc->fec_size);
91     EXPECT_EQ(std::string("sha1"),
92               std::string(reinterpret_cast<const char*>(hashtree_desc->hash_algorithm)));
93     EXPECT_EQ(std::string("system").length(), hashtree_desc->partition_name_len);
94     EXPECT_EQ(hashtree_desc->partition_name, "system");
95     EXPECT_EQ(hashtree_desc->salt, "d00df00d");
96     EXPECT_EQ(hashtree_desc->root_digest, "a3d5dd307341393d85de356c384ff543ec1ed81b");
97 
98     // Checks it's null if partition name doesn't match.
99     EXPECT_EQ(nullptr, GetHashtreeDescriptor("system_not_exist" /* avb_partition_name */,
100                                              std::move(system_vbmeta)));
101 }
102 
TEST_F(PublicFsAvbUtilTest,GetHashtreeDescriptor_NotFound)103 TEST_F(PublicFsAvbUtilTest, GetHashtreeDescriptor_NotFound) {
104     // Generates a raw boot.img
105     const size_t image_size = 5 * 1024 * 1024;
106     const size_t partition_size = 10 * 1024 * 1024;
107     base::FilePath boot_path = GenerateImage("boot.img", image_size);
108     // Appends AVB Hash Footer.
109     AddAvbFooter(boot_path, "hash", "boot", partition_size, "SHA256_RSA4096", 10,
110                  data_dir_.Append("testkey_rsa4096.pem"), "d00df00d",
111                  "--internal_release_string \"unit test\"");
112     // Extracts boot vbmeta from boot.img into boot-vbmeta.img.
113     auto boot_vbmeta = ExtractAndLoadVBMetaData(boot_path, "boot-vbmeta.img");
114 
115     auto hashtree_desc =
116             GetHashtreeDescriptor("boot" /* avb_partition_name */, std::move(boot_vbmeta));
117     EXPECT_EQ(nullptr, hashtree_desc);
118 }
119 
120 }  // namespace fs_avb_host_test
121