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