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