• 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 
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::GetPackageInstance();
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     ScriptManager* scriptManager = ScriptManager::GetScriptManager(env);
65     for (int32_t i = 0; i < ScriptManager::MAX_PRIORITY; i++) {
66         ret = scriptManager->ExecuteScript(i);
67         cout << " execute ret:" << ret << endl;
68         if (i == 1) { // only run update_partitions script
69             partRet = ret;
70         }
71     }
72     delete env;
73     ScriptManager::ReleaseScriptManager();
74     PkgManager::ReleasePackageInstance(pkgManager);
75     EXPECT_EQ(partRet, USCRIPT_SUCCESS);
76 }
77 
78 HWTEST_F(UpdatePartitionsUnitTest, UpdatePartitions_Unitest02, TestSize.Level1)
79 {
80     const string packagePath = "/data/updater/updater/parts/updaterpart02.zip";
81     PkgManager::PkgManagerPtr pkgManager = PkgManager::GetPackageInstance();
82     std::vector<std::string> components;
83     int partRet = -1;
84     int ret = pkgManager->LoadPackage(packagePath, GetTestCertName(), components);
85     cout << "load package's ret:" << ret << endl;
86     UpdaterEnv* env = new UpdaterEnv(pkgManager, nullptr, false);
87     ScriptManager* scriptManager = ScriptManager::GetScriptManager(env);
88     for (int32_t i = 0; i < ScriptManager::MAX_PRIORITY; i++) {
89         ret = scriptManager->ExecuteScript(i);
90         cout << " execute ret:" << ret << endl;
91         if (i == 1) { // only run update_partitions script
92             partRet = ret;
93         }
94     }
95     delete env;
96     ScriptManager::ReleaseScriptManager();
97     PkgManager::ReleasePackageInstance(pkgManager);
98     EXPECT_EQ(partRet, USCRIPT_ERROR_EXECUTE);
99 }
100 } // namespace updater_ut
101