1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <fcntl.h>
17#include <gmock/gmock.h>
18#include <gtest/gtest.h>
19#include <iostream>
20#include <string>
21#include "cmp_partition.h"
22#include "log/log.h"
23#include "mount.h"
24#include "partition_const.h"
25#include "partitions.h"
26#include "securec.h"
27
28using namespace Updater;
29using namespace testing::ext;
30using namespace std;
31
32namespace UpdaterUt {
33constexpr int PARTITION_NUMBER = 9;
34constexpr size_t BOOT_PARTITION_LEN = 2048;
35constexpr size_t KERNEL_PARTITION_LEN = 30720;
36constexpr size_t UPDATER_PARTITION_LEN = 40960;
37constexpr size_t MISC_PARTITION_LEN = 2048;
38constexpr size_t SYSTEM_PARTITION_LEN = 3627008;
39constexpr size_t HOS_PARTITION_LEN = 3133440;
40constexpr size_t VENDOR_PARTITION_LEN = 3133440;
41constexpr size_t DATA_PARTITION_LEN = 3133440;
42constexpr size_t XXX_PARTITION_LEN = 2998272;
43constexpr size_t BUFFER_SIZE = 100;
44
45class DoPartitionUnitTest : public testing::Test {
46public:
47    static void SetUpTestCase(void);
48    static void TearDownTestCase(void) {};
49    void SetUp();
50    void TearDown();
51};
52
53void DoPartitionUnitTest::SetUpTestCase()
54{
55    cout << "Updater Unit allCmdUnitTest Setup!" << endl;
56}
57
58void DoPartitionUnitTest::SetUp()
59{
60    cout << "Updater Unit allCmdUnitTest Begin!" << endl;
61}
62
63void DoPartitionUnitTest::TearDown()
64{
65    cout << "Updater Unit allCmdUnitTest End!" << endl;
66}
67
68static void InitEmmcPartition(struct Partition &part, const std::string &partName, size_t start, size_t length)
69{
70    part.partName = partName;
71    part.start = start;
72    part.length = length;
73    // Paramters below just give a random values, DoPartition will ignore the values.
74    part.devName = "mmcblk0px";
75    part.fsType = "emmc";
76}
77
78HWTEST_F(DoPartitionUnitTest, do_partition_test_001, TestSize.Level1)
79{
80    PartitonList nList;
81    int partitionIndex = 0;
82    struct Partition myPaty[PARTITION_NUMBER];
83    EXPECT_EQ(memset_s(myPaty, sizeof(struct Partition) * PARTITION_NUMBER, 0,
84        sizeof(struct Partition) * PARTITION_NUMBER), 0);
85    size_t bootPartitionStart = 0;
86    InitEmmcPartition(myPaty[partitionIndex++], "boot", bootPartitionStart, BOOT_PARTITION_LEN);
87
88    size_t kernelPartitionStart = bootPartitionStart + BOOT_PARTITION_LEN;
89    InitEmmcPartition(myPaty[partitionIndex++], "kernel", bootPartitionStart, BOOT_PARTITION_LEN);
90
91    size_t updaterPartitionStart = kernelPartitionStart + KERNEL_PARTITION_LEN;
92    InitEmmcPartition(myPaty[partitionIndex++], "updater", updaterPartitionStart, UPDATER_PARTITION_LEN);
93
94    size_t miscPartitionStart = updaterPartitionStart + UPDATER_PARTITION_LEN;
95    InitEmmcPartition(myPaty[partitionIndex++], "misc", miscPartitionStart, MISC_PARTITION_LEN);
96
97    size_t systemPartitionStart = miscPartitionStart + MISC_PARTITION_LEN;
98    InitEmmcPartition(myPaty[partitionIndex++], "system", systemPartitionStart, SYSTEM_PARTITION_LEN);
99
100    size_t hosPartitionStart = systemPartitionStart + SYSTEM_PARTITION_LEN;
101    InitEmmcPartition(myPaty[partitionIndex++], "hos", hosPartitionStart, HOS_PARTITION_LEN);
102
103    size_t vendorPartitionStart = hosPartitionStart + HOS_PARTITION_LEN;
104    InitEmmcPartition(myPaty[partitionIndex++], "vendor", vendorPartitionStart, VENDOR_PARTITION_LEN);
105
106    size_t dataPartitionStart = vendorPartitionStart + VENDOR_PARTITION_LEN;
107    InitEmmcPartition(myPaty[partitionIndex++], "userdata", dataPartitionStart, DATA_PARTITION_LEN);
108
109    for (int i = 0; i < partitionIndex; i++) {
110        nList.push_back(&myPaty[i]);
111    }
112
113    std::string fstabPath = "/data/updater/updater/fstab.updater";
114    LoadSpecificFstab(fstabPath);
115    int ret = DoPartitions(nList);
116    ASSERT_GT(ret, 0);
117
118    PartitonList olist;
119    size_t xxxPartitionStart = dataPartitionStart + XXX_PARTITION_LEN;
120    InitEmmcPartition(myPaty[partitionIndex], "xxxxxx", xxxPartitionStart, XXX_PARTITION_LEN);
121    olist.push_back(&myPaty[partitionIndex]);
122    int ret1 = RegisterUpdaterPartitionList(nList, olist);
123    ASSERT_EQ(ret1, 1);
124
125    char aaa[BUFFER_SIZE] = {0};
126    BlockDevice myDev {};
127    myDev.devPath = "xxxxxx";
128    myDev.specific = (void *)aaa;
129    SetBlockDeviceMode(myDev);
130}
131} // updater_ut
132