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 fd = open("/dev/null", O_RDWR);
76 dup2(fd, STDOUT_FILENO);
77 int32_t ret = ProcessUpdater(false, STDOUT_FILENO, packagePath, GetTestCertName());
78 close(fd);
79 EXPECT_EQ(ret, 0);
80 }
81
82 /* image diff update, zip has 2k size misc.img, base is zero, dst is urandom */
83 HWTEST_F(UpdateProcessorUnitTest, UpdateProcessor_002, TestSize.Level1)
84 {
85 vector<uint8_t> buffer(UT_MISC_BUFFER_SIZE, 0);
86 int32_t ret = Store::WriteDataToStore("/", GetBlockDeviceByMountPoint(UT_MISC_PARTITION_NAME),
87 buffer, UT_MISC_BUFFER_SIZE);
88 EXPECT_EQ(ret, 0);
89
90 const string packagePath = "/data/updater/updater/updater_write_diff_misc_img.zip";
91 int fd = open("/dev/null", O_RDWR);
92 dup2(fd, STDOUT_FILENO);
93 ret = ProcessUpdater(false, STDOUT_FILENO, packagePath, GetTestCertName());
94 close(fd);
95 EXPECT_EQ(ret, 0);
96 }
97
98 /* image diff update, zip has 2k size misc.img, base is zero, dst is urandom, hash check fail */
99 HWTEST_F(UpdateProcessorUnitTest, UpdateProcessor_003, TestSize.Level1)
100 {
101 vector<uint8_t> buffer(UT_MISC_BUFFER_SIZE, 1);
102 int32_t ret = Store::WriteDataToStore("/", GetBlockDeviceByMountPoint(UT_MISC_PARTITION_NAME),
103 buffer, UT_MISC_BUFFER_SIZE);
104 EXPECT_EQ(ret, 0);
105
106 const string packagePath = "/data/updater/updater/updater_write_diff_misc_img.zip";
107 int fd = open("/dev/null", O_RDWR);
108 dup2(fd, STDOUT_FILENO);
109 ret = ProcessUpdater(false, STDOUT_FILENO, packagePath, GetTestCertName());
110 close(fd);
111 EXPECT_EQ(ret, USCRIPT_INVALID_PARAM);
112 }
113
114 HWTEST_F(UpdateProcessorUnitTest, UpdateProcessor_004, TestSize.Level1)
115 {
116 PkgBuffer buffer(BUFFER_SIZE);
117 RingBuffer ringBuffer;
118 bool ret = ringBuffer.Init(UScriptInstructionUpdateFromBin::STASH_BUFFER_SIZE, 4); // power of 2
119 EXPECT_TRUE(ret);
120 for (uint32_t i = 0; i < BUFFER_PUSH_TIMES; i++) {
121 EXPECT_EQ(UScriptInstructionUpdateFromBin::UnCompressDataProducer(buffer,
122 BUFFER_SIZE, 0, false, &ringBuffer), 0);
123 }
124 PkgBuffer emptyBuffer = {};
125 EXPECT_EQ(UScriptInstructionUpdateFromBin::UnCompressDataProducer(emptyBuffer, 0, 0, true, &ringBuffer), 0);
126 uint8_t recvBuffer[UScriptInstructionUpdateFromBin::STASH_BUFFER_SIZE] {};
127 uint32_t len = 0;
128 ringBuffer.Pop(recvBuffer, UScriptInstructionUpdateFromBin::STASH_BUFFER_SIZE, len);
129 EXPECT_EQ(len, UScriptInstructionUpdateFromBin::STASH_BUFFER_SIZE);
130 ringBuffer.Pop(recvBuffer, UScriptInstructionUpdateFromBin::STASH_BUFFER_SIZE, len);
131 EXPECT_EQ(len, BUFFER_SIZE);
132 }
133 } // namespace updater_ut
134