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_processor_unittest.h"
17 #include <cerrno>
18 #include <cstdio>
19 #include <iostream>
20 #include <sys/mount.h>
21 #include <unistd.h>
22 #include "mount.h"
23 #include "store.h"
24 #include "unittest_comm.h"
25 #include "update_processor.h"
26 #include "script_manager.h"
27 #include "pkg_manager.h"
28 #include "ring_buffer/ring_buffer.h"
29
30 using namespace Updater;
31 using namespace std;
32 using namespace Hpackage;
33 using namespace testing::ext;
34 using namespace Uscript;
35
36 namespace UpdaterUt {
37 constexpr const char *UT_MISC_PARTITION_NAME = "/misc";
38 constexpr const uint32_t UT_MISC_BUFFER_SIZE = 2048;
39 constexpr const uint32_t BUFFER_PUSH_TIMES = 3;
40 constexpr const uint32_t BUFFER_SIZE = 1024 * 1024 * 2;
SetUp(void)41 void UpdateProcessorUnitTest::SetUp(void)
42
43 {
44 cout << "Updater Unit UpdateProcessorUnitTest Begin!" << endl;
45
46 LoadSpecificFstab("/data/updater/applypatch/etc/fstab.ut.updater");
47
48 /* create 2k size test file */
49 string devPath = GetBlockDeviceByMountPoint(UT_MISC_PARTITION_NAME);
50 vector<uint8_t> buffer(UT_MISC_BUFFER_SIZE, 0);
51 auto ret = Store::WriteDataToStore("/", devPath, buffer, UT_MISC_BUFFER_SIZE);
52 cout << "WriteDataToStore ret: " << ret << endl;
53 }
54
TearDown(void)55 void UpdateProcessorUnitTest::TearDown(void)
56 {
57 cout << "Updater Unit UpdateProcessorUnitTest End!" << endl;
58
59 /* delete 2k size test file */
60 string devPath = GetBlockDeviceByMountPoint(UT_MISC_PARTITION_NAME);
61 auto ret = Store::FreeStore("/", devPath);
62 cout << "FreeStore ret: " << ret << endl;
63 }
64
65 // do something at the each function begining
SetUpTestCase(void)66 void UpdateProcessorUnitTest::SetUpTestCase(void) {}
67
68 // do something at the each function end
TearDownTestCase(void)69 void UpdateProcessorUnitTest::TearDownTestCase(void) {}
70
71 /* ota update, zip has 2k size misc.img */
72 HWTEST_F(UpdateProcessorUnitTest, UpdateProcessor_001, TestSize.Level1)
73 {
74 const string packagePath = "/data/updater/updater/updater_write_misc_img.zip";
75 int pfd[2]; // 2: pipe read, pipe write
76 int ret = pipe(pfd);
77 EXPECT_GE(ret, 0);
78 ret = ProcessUpdater(false, pfd[1], packagePath, GetTestCertName());
79 close(pfd[0]);
80 EXPECT_EQ(ret, 0);
81 }
82
83 /* image diff update, zip has 2k size misc.img, base is zero, dst is urandom, hash check fail */
84 HWTEST_F(UpdateProcessorUnitTest, UpdateProcessor_003, TestSize.Level1)
85 {
86 vector<uint8_t> buffer(UT_MISC_BUFFER_SIZE, 1);
87 int32_t ret = Store::WriteDataToStore("/", GetBlockDeviceByMountPoint(UT_MISC_PARTITION_NAME),
88 buffer, UT_MISC_BUFFER_SIZE);
89 EXPECT_EQ(ret, 0);
90
91 const string packagePath = "/data/updater/updater/updater_write_diff_misc_img.zip";
92 int pfd[2]; // 2: pipe read, pipe write
93 ret = pipe(pfd);
94 EXPECT_GE(ret, 0);
95 ret = ProcessUpdater(false, pfd[1], packagePath, GetTestCertName());
96 close(pfd[0]);
97 EXPECT_EQ(ret, USCRIPT_INVALID_PARAM);
98 }
99
100 HWTEST_F(UpdateProcessorUnitTest, UpdateProcessor_004, TestSize.Level1)
101 {
102 PkgBuffer buffer(BUFFER_SIZE);
103 RingBuffer ringBuffer;
104 bool ret = ringBuffer.Init(UScriptInstructionUpdateFromBin::STASH_BUFFER_SIZE, 4); // power of 2
105 EXPECT_TRUE(ret);
106 for (uint32_t i = 0; i < BUFFER_PUSH_TIMES; i++) {
107 EXPECT_EQ(UScriptInstructionUpdateFromBin::UnCompressDataProducer(buffer,
108 BUFFER_SIZE, 0, false, &ringBuffer), 0);
109 }
110 PkgBuffer emptyBuffer = {};
111 EXPECT_EQ(UScriptInstructionUpdateFromBin::UnCompressDataProducer(emptyBuffer, 0, 0, true, &ringBuffer), 0);
112 uint8_t recvBuffer[UScriptInstructionUpdateFromBin::STASH_BUFFER_SIZE] {};
113 uint32_t len = 0;
114 ringBuffer.Pop(recvBuffer, UScriptInstructionUpdateFromBin::STASH_BUFFER_SIZE, len);
115 EXPECT_EQ(len, UScriptInstructionUpdateFromBin::STASH_BUFFER_SIZE);
116 ringBuffer.Pop(recvBuffer, UScriptInstructionUpdateFromBin::STASH_BUFFER_SIZE, len);
117 EXPECT_EQ(len, BUFFER_SIZE);
118 }
119
120 HWTEST_F(UpdateProcessorUnitTest, UpdateProcessor_005, TestSize.Level1)
121 {
122 const string packagePath = "/data/updater/updater/updater_write_extract_img.zip";
123 int32_t ret = ProcessUpdater(false, -1, packagePath, GetTestCertName());
124 EXPECT_EQ(ret, 1);
125 }
126
127 HWTEST_F(UpdateProcessorUnitTest, UpdateProcessor_006, TestSize.Level1)
128 {
129 const string packagePath = "/data/updater/updater/updater_write_not_exist.zip";
130 int pfd[2]; // 2: pipe read, pipe write
131 int ret = pipe(pfd);
132 EXPECT_GE(ret, 0);
133 ret = ProcessUpdater(false, pfd[1], packagePath, GetTestCertName());
134 close(pfd[0]);
135 EXPECT_EQ(ret, EXIT_INVALID_ARGS);
136 }
137
138 HWTEST_F(UpdateProcessorUnitTest, UpdaterEnv_Unitest01, TestSize.Level1)
139 {
140 PkgManager::PkgManagerPtr pkgManager = PkgManager::CreatePackageInstance();
141 UpdaterEnv *env = new UpdaterEnv(pkgManager, nullptr, false);
142 EXPECT_NE(env, nullptr);
143 env->PostMessage("set_progress", "0.1");
144 free(env);
145 env = nullptr;
146
__anone8b285410102(const char *cmd, const char *content) 147 env = new UpdaterEnv(pkgManager, [](const char *cmd, const char *content) {}, false);
148 EXPECT_NE(env, nullptr);
149 env->PostMessage("set_progress", "0.1");
150 UScriptInstructionFactoryPtr factory = env->GetInstructionFactory();
151 factory = env->GetInstructionFactory();
152 EXPECT_NE(factory, nullptr);
153 free(env);
154 env = nullptr;
155 PkgManager::ReleasePackageInstance(pkgManager);
156 }
157 } // namespace updater_ut
158