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
28 using namespace Updater;
29 using namespace std;
30 using namespace Hpackage;
31 using namespace testing::ext;
32 using namespace Uscript;
33
34 namespace UpdaterUt {
35 constexpr const char *UT_MISC_PARTITION_NAME = "/misc";
36 constexpr const uint32_t UT_MISC_BUFFER_SIZE = 2048;
37
SetUp(void)38 void UpdateProcessorUnitTest::SetUp(void)
39 {
40 cout << "Updater Unit UpdateProcessorUnitTest Begin!" << endl;
41
42 LoadSpecificFstab("/data/updater/applypatch/etc/fstab.ut.updater");
43
44 /* create 2k size test file */
45 string devPath = GetBlockDeviceByMountPoint(UT_MISC_PARTITION_NAME);
46 vector<uint8_t> buffer(UT_MISC_BUFFER_SIZE, 0);
47 auto ret = Store::WriteDataToStore("/", devPath, buffer, UT_MISC_BUFFER_SIZE);
48 printf("WriteDataToStore ret: %d\n", ret);
49 }
50
TearDown(void)51 void UpdateProcessorUnitTest::TearDown(void)
52 {
53 cout << "Updater Unit UpdateProcessorUnitTest End!" << endl;
54
55 /* delete 2k size test file */
56 string devPath = GetBlockDeviceByMountPoint(UT_MISC_PARTITION_NAME);
57 auto ret = Store::FreeStore("/", devPath);
58 printf("FreeStore ret: %d\n", ret);
59 }
60
61 // do something at the each function begining
SetUpTestCase(void)62 void UpdateProcessorUnitTest::SetUpTestCase(void) {}
63
64 // do something at the each function end
TearDownTestCase(void)65 void UpdateProcessorUnitTest::TearDownTestCase(void) {}
66
67 /* ota update, zip has 2k size misc.img */
68 HWTEST_F(UpdateProcessorUnitTest, UpdateProcessor_001, TestSize.Level1)
69 {
70 const string packagePath = "/data/updater/updater/updater_write_misc_img.zip";
71 int32_t ret = ProcessUpdater(false, STDOUT_FILENO, packagePath, GetTestCertName());
72 EXPECT_EQ(ret, 0);
73 }
74
75 /* diff update, zip has 2k size misc.img, base is zero, dst is urandom */
76 HWTEST_F(UpdateProcessorUnitTest, UpdateProcessor_002, TestSize.Level1)
77 {
78 vector<uint8_t> buffer(UT_MISC_BUFFER_SIZE, 0);
79 int32_t ret = Store::WriteDataToStore("/", GetBlockDeviceByMountPoint(UT_MISC_PARTITION_NAME),
80 buffer, UT_MISC_BUFFER_SIZE);
81 EXPECT_EQ(ret, 0);
82
83 const string packagePath = "/data/updater/updater/updater_write_diff_misc_img.zip";
84 ret = ProcessUpdater(false, STDOUT_FILENO, packagePath, GetTestCertName());
85 EXPECT_EQ(ret, 0);
86 }
87
88 /* diff update, zip has 2k size misc.img, base is zero, dst is urandom, hash check fail */
89 HWTEST_F(UpdateProcessorUnitTest, UpdateProcessor_003, TestSize.Level1)
90 {
91 vector<uint8_t> buffer(UT_MISC_BUFFER_SIZE, 1);
92 int32_t ret = Store::WriteDataToStore("/", GetBlockDeviceByMountPoint(UT_MISC_PARTITION_NAME),
93 buffer, UT_MISC_BUFFER_SIZE);
94 EXPECT_EQ(ret, 0);
95
96 const string packagePath = "/data/updater/updater/updater_write_diff_misc_img.zip";
97 ret = ProcessUpdater(false, STDOUT_FILENO, packagePath, GetTestCertName());
98 EXPECT_EQ(ret, USCRIPT_INVALID_PARAM);
99 }
100 } // namespace updater_ut
101