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 
32 using namespace Updater;
33 using namespace testing::ext;
34 using namespace Uscript;
35 using namespace std;
36 using namespace Hpackage;
37 
38 namespace UpdaterUt {
SetUp(void)39 void UpdatePartitionsUnitTest::SetUp(void)
40 {
41     cout << "Updater Unit UpdatePartitionsUnitTest Begin!" << endl;
42 }
43 
TearDown(void)44 void UpdatePartitionsUnitTest::TearDown(void)
45 {
46     cout << "Updater Unit UpdatePartitionsUnitTest End!" << endl;
47 }
48 
49 // do something at the each function begining
SetUpTestCase(void)50 void UpdatePartitionsUnitTest::SetUpTestCase(void) {}
51 
52 // do something at the each function end
TearDownTestCase(void)53 void UpdatePartitionsUnitTest::TearDownTestCase(void) {}
54 
55 HWTEST_F(UpdatePartitionsUnitTest, UpdatePartitions_Unitest01, TestSize.Level1)
56 {
57     const string packagePath = "/data/updater/updater/parts/updaterpart01.zip";
58     PkgManager::PkgManagerPtr pkgManager = PkgManager::CreatePackageInstance();
59     std::vector<std::string> components;
60     int partRet = -1;
61     int ret = pkgManager->LoadPackage(packagePath, GetTestCertName(), components);
62     cout << "load package's ret:" << ret << endl;
63     UpdaterEnv* env = new UpdaterEnv(pkgManager, nullptr, false);
64     Hpackage::HashDataVerifier scriptVerifier {pkgManager};
65     ScriptManager* scriptManager = ScriptManager::GetScriptManager(env, &scriptVerifier);
66     for (int32_t i = 0; i < ScriptManager::MAX_PRIORITY; i++) {
67         ret = scriptManager->ExecuteScript(i);
68         cout << " execute ret:" << ret << endl;
69         if (i == 1) { // only run update_partitions script
70             partRet = ret;
71         }
72     }
73     delete env;
74     ScriptManager::ReleaseScriptManager();
75     PkgManager::ReleasePackageInstance(pkgManager);
76     EXPECT_EQ(partRet, USCRIPT_SUCCESS);
77 }
78 
79 HWTEST_F(UpdatePartitionsUnitTest, UpdatePartitions_Unitest02, TestSize.Level1)
80 {
81     const string packagePath = "/data/updater/updater/parts/updaterpart02.zip";
82     PkgManager::PkgManagerPtr pkgManager = PkgManager::CreatePackageInstance();
83     std::vector<std::string> components;
84     int partRet = -1;
85     int ret = pkgManager->LoadPackage(packagePath, GetTestCertName(), components);
86     cout << "load package's ret:" << ret << endl;
87     UpdaterEnv* env = new UpdaterEnv(pkgManager, nullptr, false);
88     Hpackage::HashDataVerifier scriptVerifier {pkgManager};
89     ScriptManager* scriptManager = ScriptManager::GetScriptManager(env, &scriptVerifier);
90     for (int32_t i = 0; i < ScriptManager::MAX_PRIORITY; i++) {
91         ret = scriptManager->ExecuteScript(i);
92         cout << " execute ret:" << ret << endl;
93         if (i == 1) { // only run update_partitions script
94             partRet = ret;
95         }
96     }
97     delete env;
98     ScriptManager::ReleaseScriptManager();
99     PkgManager::ReleasePackageInstance(pkgManager);
100     EXPECT_EQ(partRet, USCRIPT_SUCCESS);
101 }
102 } // namespace updater_ut
103