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 <gtest/gtest.h>
18 
19 #include "builder.h"
20 #include "super_vbmeta_format.h"
21 
22 using android::base::Result;
23 using android::fs_mgr::SuperVBMetaBuilder;
24 
TEST(BuilderTest,VBMetaTableBasic)25 TEST(BuilderTest, VBMetaTableBasic) {
26     std::unique_ptr<SuperVBMetaBuilder> builder = std::make_unique<SuperVBMetaBuilder>();
27     ASSERT_NE(builder, nullptr);
28 
29     Result<uint8_t> vbmeta_index = builder->AddVBMetaImage("vbmeta" /* vbmeta_name */);
30     EXPECT_RESULT_OK(vbmeta_index);
31 
32     Result<uint8_t> vbmeta_system_slot = builder->AddVBMetaImage("vbmeta_system" /* vbmeta_name */);
33     EXPECT_RESULT_OK(vbmeta_system_slot);
34 
35     Result<uint8_t> vbmeta_vendor_slot = builder->AddVBMetaImage("vbmeta_vendor" /* vbmeta_name */);
36     EXPECT_RESULT_OK(vbmeta_vendor_slot);
37 
38     builder->DeleteVBMetaImage("vbmeta_system" /* vbmeta_name */);
39 
40     Result<uint8_t> vbmeta_product_slot =
41             builder->AddVBMetaImage("vbmeta_product" /* vbmeta_name */);
42     EXPECT_RESULT_OK(vbmeta_product_slot);
43 
44     std::unique_ptr<VBMetaTable> table = builder->ExportVBMetaTable();
45     ASSERT_NE(table, nullptr);
46 
47     // check for vbmeta table header
48     EXPECT_EQ(table->header.magic, SUPER_VBMETA_MAGIC);
49     EXPECT_EQ(table->header.major_version, SUPER_VBMETA_MAJOR_VERSION);
50     EXPECT_EQ(table->header.minor_version, SUPER_VBMETA_MINOR_VERSION);
51     EXPECT_EQ(table->header.header_size, SUPER_VBMETA_HEADER_SIZE);
52     EXPECT_EQ(table->header.total_size,
53               SUPER_VBMETA_HEADER_SIZE + SUPER_VBMETA_DESCRIPTOR_SIZE * 3 + 33);
54     EXPECT_EQ(table->header.descriptors_size, SUPER_VBMETA_DESCRIPTOR_SIZE * 3 + 33);
55 
56     // Test for vbmeta table descriptors
57     EXPECT_EQ(table->descriptors.size(), 3);
58 
59     EXPECT_EQ(table->descriptors[0].vbmeta_index, 0);
60     EXPECT_EQ(table->descriptors[0].vbmeta_name_length, 6);
61     for (int i = 0; i < sizeof(table->descriptors[0].reserved); i++)
62         EXPECT_EQ(table->descriptors[0].reserved[i], 0);
63     EXPECT_EQ(table->descriptors[0].vbmeta_name, "vbmeta");
64 
65     EXPECT_EQ(table->descriptors[1].vbmeta_index, 2);
66     EXPECT_EQ(table->descriptors[1].vbmeta_name_length, 13);
67     for (int i = 0; i < sizeof(table->descriptors[1].reserved); i++)
68         EXPECT_EQ(table->descriptors[1].reserved[i], 0);
69     EXPECT_EQ(table->descriptors[1].vbmeta_name, "vbmeta_vendor");
70 
71     EXPECT_EQ(table->descriptors[2].vbmeta_index, 1);
72     EXPECT_EQ(table->descriptors[2].vbmeta_name_length, 14);
73     for (int i = 0; i < sizeof(table->descriptors[2].reserved); i++)
74         EXPECT_EQ(table->descriptors[2].reserved[i], 0);
75     EXPECT_EQ(table->descriptors[2].vbmeta_name, "vbmeta_product");
76 }
77