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_image_block_test.h"
17 #include <cerrno>
18 #include <cstdio>
19 #include <fcntl.h>
20 #include <iostream>
21 #include <libgen.h>
22 #include <string>
23 #include <sys/mman.h>
24 #include <sys/mount.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <vector>
28 #include "applypatch/block_set.h"
29 #include "applypatch/store.h"
30 #include "fs_manager/mount.h"
31 #include "log.h"
32 #include "package/pkg_manager.h"
33 #include "script_instruction.h"
34 #include "script_manager.h"
35 #include "script_utils.h"
36 #include "unittest_comm.h"
37 #include "update_image_block.h"
38 #include "update_processor.h"
39 #include "utils.h"
40
41 using namespace Updater;
42 using namespace testing::ext;
43 using namespace Uscript;
44 using namespace std;
45 using namespace Hpackage;
46
47 namespace UpdaterUt {
SetUp()48 void UpdateImageBlockTest::SetUp()
49 {
50 unsigned long mountFlag = MS_REMOUNT;
51 std::string tmpPath = "/tmp";
52 // mount rootfs to read-write.
53 std::string rootSource = "/dev/root";
54 if (mount(rootSource.c_str(), "/", "ext4", mountFlag, nullptr) != 0) {
55 std::cout << "Cannot re-mount rootfs\n";
56 }
57 auto ret = mkdir(tmpPath.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
58 if (ret != 0) {
59 std::cout << "Cannot create \"/tmp\" directory: " << errno << "\n";
60 }
61
62 // Load specific fstab for testing.
63 LoadSpecificFstab("/data/updater/applypatch/etc/fstab.ut.updater");
64 cout << "SetUpTestCase" << endl;
65 }
66
TearDown()67 void UpdateImageBlockTest::TearDown()
68 {
69 cout << "TearDownTestCase" << endl;
70 }
71
72 HWTEST_F(UpdateImageBlockTest, update_image_block_test_001, TestSize.Level1)
73 {
74 LoadSpecificFstab("/data/updater/applypatch/etc/fstab.ut.updater");
75 string devPath = GetBlockDeviceByMountPoint("/vendortest1");
76 size_t bufferSize = 4096;
77 std::vector<uint8_t> buffer(bufferSize, 0);
78 auto ret = Store::WriteDataToStore("/", devPath, buffer, bufferSize);
79 string packagePath = "/data/updater/updater/updater_diff_1.zip";
80 PkgManager::PkgManagerPtr pkgManager = PkgManager::GetPackageInstance();
81 std::vector<std::string> components;
82 ret = pkgManager->LoadPackage(packagePath, GetTestCertName(), components);
83 printf("load package's ret : %d\n", ret);
84 UpdaterEnv* env = new UpdaterEnv(pkgManager, nullptr, false); // retry
85 ScriptManager* scriptManager = ScriptManager::GetScriptManager(env);
86 for (int32_t i = 0; i < ScriptManager::MAX_PRIORITY; i++) {
87 ret = scriptManager->ExecuteScript(i);
88 printf(" execute ret : %d\n", ret);
89 }
90 delete env;
91 ScriptManager::ReleaseScriptManager();
92 PkgManager::ReleasePackageInstance(pkgManager);
93 }
94
95 HWTEST_F(UpdateImageBlockTest, update_image_block_test_002, TestSize.Level1)
96 {
97 LoadSpecificFstab("/data/updater/applypatch/etc/fstab.ut.updater");
98 string devPath = GetBlockDeviceByMountPoint("/vendortest");
99 size_t bufferSize = 4096;
100 printf("dev path is %s\n", devPath.c_str());
101 std::vector<uint8_t> buffer(bufferSize, 0);
102 auto ret = Store::WriteDataToStore("/", devPath, buffer, bufferSize);
103 printf("WriteDataToStore's ret: %d\n", ret);
104
105 string oldName = "/data/updater/updater/retry_flag";
106 string newName = "/data/updater/update_tmp/retry_flag";
107 string newDir = "/data/updater/update_tmp";
108 ret = Store::CreateNewSpace(newDir, false);
109 printf("CreateNewSpace's ret: %d\n", ret);
110 ret = rename(oldName.c_str(), newName.c_str());
111 printf("rename's ret : %d\n", ret);
112
113 string packagePath = "/data/updater/updater/updater_diff_2.zip";
114 PkgManager::PkgManagerPtr pkgManager = PkgManager::GetPackageInstance();
115 std::vector<std::string> components;
116 ret = pkgManager->LoadPackage(packagePath, GetTestCertName(), components);
117 printf("load package's ret : %d\n", ret);
118 UpdaterEnv* env = new UpdaterEnv(pkgManager, nullptr, true);
119 ScriptManager* scriptManager = ScriptManager::GetScriptManager(env);
120 for (int32_t i = 0; i < ScriptManager::MAX_PRIORITY; i++) {
121 ret = scriptManager->ExecuteScript(i);
122 printf(" execute ret : %d\n", ret);
123 }
124 delete env;
125 ScriptManager::ReleaseScriptManager();
126 PkgManager::ReleasePackageInstance(pkgManager);
127 }
128 }
129