• 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_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 */
84 HWTEST_F(UpdateProcessorUnitTest, UpdateProcessor_002, TestSize.Level1)
85 {
86     vector<uint8_t> buffer(UT_MISC_BUFFER_SIZE, 0);
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, 500);
98 }
99 
100 /* image diff update, zip has 2k size misc.img, base is zero, dst is urandom, hash check fail */
101 HWTEST_F(UpdateProcessorUnitTest, UpdateProcessor_003, TestSize.Level1)
102 {
103     vector<uint8_t> buffer(UT_MISC_BUFFER_SIZE, 1);
104     int32_t ret = Store::WriteDataToStore("/", GetBlockDeviceByMountPoint(UT_MISC_PARTITION_NAME),
105         buffer, UT_MISC_BUFFER_SIZE);
106     EXPECT_EQ(ret, 0);
107 
108     const string packagePath = "/data/updater/updater/updater_write_diff_misc_img.zip";
109     int pfd[2]; // 2: pipe read, pipe write
110     ret = pipe(pfd);
111     EXPECT_GE(ret, 0);
112     ret = ProcessUpdater(false, pfd[1], packagePath, GetTestCertName());
113     close(pfd[0]);
114     EXPECT_EQ(ret, USCRIPT_INVALID_PARAM);
115 }
116 
117 HWTEST_F(UpdateProcessorUnitTest, UpdateProcessor_004, TestSize.Level1)
118 {
119     PkgBuffer buffer(BUFFER_SIZE);
120     RingBuffer ringBuffer;
121     bool ret = ringBuffer.Init(UScriptInstructionUpdateFromBin::STASH_BUFFER_SIZE, 4); // power of 2
122     EXPECT_TRUE(ret);
123     for (uint32_t i = 0; i < BUFFER_PUSH_TIMES; i++) {
124         EXPECT_EQ(UScriptInstructionUpdateFromBin::UnCompressDataProducer(buffer,
125             BUFFER_SIZE, 0, false, &ringBuffer), 0);
126     }
127     PkgBuffer emptyBuffer = {};
128     EXPECT_EQ(UScriptInstructionUpdateFromBin::UnCompressDataProducer(emptyBuffer, 0, 0, true, &ringBuffer), 0);
129     uint8_t recvBuffer[UScriptInstructionUpdateFromBin::STASH_BUFFER_SIZE] {};
130     uint32_t len = 0;
131     ringBuffer.Pop(recvBuffer, UScriptInstructionUpdateFromBin::STASH_BUFFER_SIZE, len);
132     EXPECT_EQ(len, UScriptInstructionUpdateFromBin::STASH_BUFFER_SIZE);
133     ringBuffer.Pop(recvBuffer, UScriptInstructionUpdateFromBin::STASH_BUFFER_SIZE, len);
134     EXPECT_EQ(len, BUFFER_SIZE);
135 }
136 
137 HWTEST_F(UpdateProcessorUnitTest, UpdateProcessor_005, TestSize.Level1)
138 {
139     const string packagePath = "/data/updater/updater/updater_write_extract_img.zip";
140     int32_t ret = ProcessUpdater(false, -1, packagePath, GetTestCertName());
141     EXPECT_EQ(ret, 1);
142 }
143 
144 HWTEST_F(UpdateProcessorUnitTest, UpdateProcessor_006, TestSize.Level1)
145 {
146     const string packagePath = "/data/updater/updater/updater_write_not_exist.zip";
147     int pfd[2]; // 2: pipe read, pipe write
148     int ret = pipe(pfd);
149     EXPECT_GE(ret, 0);
150     ret = ProcessUpdater(false, pfd[1], packagePath, GetTestCertName());
151     close(pfd[0]);
152     EXPECT_EQ(ret, EXIT_INVALID_ARGS);
153 }
154 
155 HWTEST_F(UpdateProcessorUnitTest, UpdaterEnv_Unitest01, TestSize.Level1)
156 {
157     PkgManager::PkgManagerPtr pkgManager = PkgManager::CreatePackageInstance();
158     UpdaterEnv *env = new UpdaterEnv(pkgManager, nullptr, false);
159     EXPECT_NE(env, nullptr);
160     env->PostMessage("set_progress", "0.1");
161     free(env);
162     env = nullptr;
163 
__anon30f659a30102(const char *cmd, const char *content) 164     env = new UpdaterEnv(pkgManager, [](const char *cmd, const char *content) {}, false);
165     EXPECT_NE(env, nullptr);
166     env->PostMessage("set_progress", "0.1");
167     UScriptInstructionFactoryPtr factory = env->GetInstructionFactory();
168     factory = env->GetInstructionFactory();
169     EXPECT_NE(factory, nullptr);
170     free(env);
171     env = nullptr;
172     PkgManager::ReleasePackageInstance(pkgManager);
173 }
174 } // namespace updater_ut
175