• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "update_partitions_unittest.h"
17 #include <cerrno>
18 #include <cstdio>
19 #include <iostream>
20 #include <string>
21 #include <unistd.h>
22 #include <vector>
23 #include "log.h"
24 #include "package/pkg_manager.h"
25 #include "script_instruction.h"
26 #include "script_manager.h"
27 #include "script_utils.h"
28 #include "unittest_comm.h"
29 #include "update_processor.h"
30 #include "utils.h"
31 #include "cJSON.h"
32 #include "fs_manager/partitions.h"
33 #include "update_partitions.h"
34 
35 using namespace Updater;
36 using namespace testing::ext;
37 using namespace Uscript;
38 using namespace std;
39 using namespace Hpackage;
40 
41 namespace UpdaterUt {
SetUp(void)42 void UpdatePartitionsUnitTest::SetUp(void)
43 {
44     cout << "Updater Unit UpdatePartitionsUnitTest Begin!" << endl;
45 }
46 
TearDown(void)47 void UpdatePartitionsUnitTest::TearDown(void)
48 {
49     cout << "Updater Unit UpdatePartitionsUnitTest End!" << endl;
50 }
51 
52 // do something at the each function begining
SetUpTestCase(void)53 void UpdatePartitionsUnitTest::SetUpTestCase(void) {}
54 
55 // do something at the each function end
TearDownTestCase(void)56 void UpdatePartitionsUnitTest::TearDownTestCase(void) {}
57 
58 HWTEST_F(UpdatePartitionsUnitTest, UpdatePartitions_Unitest01, TestSize.Level1)
59 {
60     const string packagePath = "/data/updater/updater/parts/updaterpart01.zip";
61     PkgManager::PkgManagerPtr pkgManager = PkgManager::CreatePackageInstance();
62     std::vector<std::string> components;
63     int partRet = -1;
64     int ret = pkgManager->LoadPackage(packagePath, GetTestCertName(), components);
65     cout << "load package's ret:" << ret << endl;
66     UpdaterEnv* env = new UpdaterEnv(pkgManager, nullptr, false);
67     Hpackage::HashDataVerifier scriptVerifier {pkgManager};
68     ScriptManager* scriptManager = ScriptManager::GetScriptManager(env, &scriptVerifier);
69     for (int32_t i = 0; i < ScriptManager::MAX_PRIORITY; i++) {
70         ret = scriptManager->ExecuteScript(i);
71         cout << " execute ret:" << ret << endl;
72         if (i == 1) { // only run update_partitions script
73             partRet = ret;
74         }
75     }
76     delete env;
77     ScriptManager::ReleaseScriptManager();
78     PkgManager::ReleasePackageInstance(pkgManager);
79     EXPECT_EQ(partRet, USCRIPT_SUCCESS);
80 }
81 
82 HWTEST_F(UpdatePartitionsUnitTest, UpdatePartitions_Unitest02, TestSize.Level1)
83 {
84     const string packagePath = "/data/updater/updater/parts/updaterpart02.zip";
85     PkgManager::PkgManagerPtr pkgManager = PkgManager::CreatePackageInstance();
86     std::vector<std::string> components;
87     int partRet = -1;
88     int ret = pkgManager->LoadPackage(packagePath, GetTestCertName(), components);
89     cout << "load package's ret:" << ret << endl;
90     UpdaterEnv* env = new UpdaterEnv(pkgManager, nullptr, false);
91     Hpackage::HashDataVerifier scriptVerifier {pkgManager};
92     ScriptManager* scriptManager = ScriptManager::GetScriptManager(env, &scriptVerifier);
93     for (int32_t i = 0; i < ScriptManager::MAX_PRIORITY; i++) {
94         ret = scriptManager->ExecuteScript(i);
95         cout << " execute ret:" << ret << endl;
96         if (i == 1) { // only run update_partitions script
97             partRet = ret;
98         }
99     }
100     delete env;
101     ScriptManager::ReleaseScriptManager();
102     PkgManager::ReleasePackageInstance(pkgManager);
103     EXPECT_EQ(partRet, USCRIPT_SUCCESS);
104 }
105 
106 HWTEST_F(UpdatePartitionsUnitTest, UpdatePartitions_Unitest04, TestSize.Level1)
107 {
108     const char *partitionInfo = R"(
109     {
110         "disk": "sda",
111         "partition": [
112             {
113                 "nostart": 512
114             },
115             {
116                 "start": 32
117             },
118             {
119                 "start": 32,
120                 "length": 1
121             },
122             {
123                 "start": 32,
124                 "length": 1,
125                 "partName": "cust"
126             }
127         ],
128         "sector_size": 4096
129     }
130     )";
131     cJSON *root = cJSON_Parse(partitionInfo);
132     ASSERT_NE(root, nullptr);
133     cJSON *partitions = cJSON_GetObjectItem(root, "Partition");
134     ASSERT_NE(partitions, nullptr);
135     struct Partition *myPartition = static_cast<struct Partition*>(calloc(1, sizeof(struct Partition)));
136     ASSERT_NE(myPartition, nullptr);
137 
138     UpdatePartitions partition;
139     EXPECT_TRUE(!partition.SetPartitionInfo(partitions, 10, myPartition)); // 10: error index
140     EXPECT_TRUE(!partition.SetPartitionInfo(partitions, 0, myPartition)); // 0: error start
141     EXPECT_TRUE(!partition.SetPartitionInfo(partitions, 1, myPartition)); // 1: error length
142     EXPECT_TRUE(!partition.SetPartitionInfo(partitions, 2, myPartition)); // 2: error partName
143     EXPECT_TRUE(!partition.SetPartitionInfo(partitions, 3, myPartition)); // 2: error fstype
144     free(myPartition);
145     cJSON_Delete(root);
146 }
147 
148 HWTEST_F(UpdatePartitionsUnitTest, UpdatePartitions_Unitest05, TestSize.Level1)
149 {
150     const char *noPartitionInfo = R"(
151     {
152         "disk": "sda",
153         "partitionx": [
154             {
155                 "nostart": 512
156             }
157         ],
158         "sector_size": 4096
159     }
160     )";
161     const char *partitionInfo = R"(
162     {
163         "disk": "sda",
164         "partition": [
165             {
166                 "nostart": 512
167             }
168         ],
169         "sector_size": 4096
170     }
171     )";
172     const char *partitionInfoNew = R"(
173     {
174         "disk": "sda",
175         "partition": [
176             {
177                 "nostart": 512
178             },
179             {
180                 "start": 512
181             }
182         ],
183         "sector_size": 4096
184     }
185     )";
186 
187     PartitonList newPartList {};
188     UpdatePartitions partition;
189     EXPECT_TRUE(partition.ParsePartitionInfo("test", newPartList) != 1); // error root
190     EXPECT_TRUE(partition.ParsePartitionInfo(std::string(noPartitionInfo), newPartList) != 1); // error partitions
191     EXPECT_TRUE(partition.ParsePartitionInfo(std::string(partitionInfo), newPartList) != 1); // error number
192     EXPECT_TRUE(partition.ParsePartitionInfo(std::string(partitionInfoNew), newPartList) != 1); // error number
193 }
194 } // namespace updater_ut
195