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_image_block_test.h"
17 #include <cerrno>
18 #include <cstdio>
19 #include <fcntl.h>
20 #include <iostream>
21 #include <libgen.h>
22 #include <string>
23 #include <sys/mman.h>
24 #include <sys/mount.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <vector>
28 #include "applypatch/block_set.h"
29 #include "applypatch/store.h"
30 #include "fs_manager/mount.h"
31 #include "log.h"
32 #include "package/pkg_manager.h"
33 #include "script_instruction.h"
34 #include "script_manager.h"
35 #include "script_utils.h"
36 #include "unittest_comm.h"
37 #include "update_image_block.h"
38 #include "update_processor.h"
39 #include "utils.h"
40
41 using namespace Updater;
42 using namespace testing::ext;
43 using namespace Uscript;
44 using namespace std;
45 using namespace Hpackage;
46
47 namespace UpdaterUt {
SetUp()48 void UpdateImageBlockTest::SetUp()
49 {
50 cout << "Updater Unit UpdateBlockUnitTest Begin!" << endl;
51
52 LoadSpecificFstab("/data/updater/applypatch/etc/fstab.ut.updater");
53 }
54
TearDown()55 void UpdateImageBlockTest::TearDown()
56 {
57 cout << "Updater Unit UpdateBlockUnitTest End!" << endl;
58 }
59
60 /* ota update, upd to base */
61 HWTEST_F(UpdateImageBlockTest, update_image_block_test_001, TestSize.Level1)
62 {
63 const string packagePath = "/data/updater/updater/updater_write_miscblock_img.zip";
64 int pfd[2]; // 2: pipe read, pipe write
65 int ret = pipe(pfd);
66 EXPECT_GE(ret, 0);
67 ret = ProcessUpdater(false, pfd[1], packagePath, GetTestCertName());
68 close(pfd[0]);
69 EXPECT_EQ(ret, 0);
70 }
71
72 /* block diff update, hash check ok */
73 HWTEST_F(UpdateImageBlockTest, update_image_block_test_002, TestSize.Level1)
74 {
75 const string packagePath = "/data/updater/updater/updater_write_diff_miscblock_img.zip";
76 int pfd[2]; // 2: pipe read, pipe write
77 int ret = pipe(pfd);
78 EXPECT_GE(ret, 0);
79 ret = ProcessUpdater(false, pfd[1], packagePath, GetTestCertName());
80 close(pfd[0]);
81 EXPECT_EQ(ret, 500);
82 }
83
84 /* block diff update, hash check fail */
85 HWTEST_F(UpdateImageBlockTest, update_image_block_test_003, TestSize.Level1)
86 {
87 const string packagePath = "/data/updater/updater/updater_write_diff_miscblock_img.zip";
88 int pfd[2]; // 2: pipe read, pipe write
89 int ret = pipe(pfd);
90 EXPECT_GE(ret, 0);
91 ret = ProcessUpdater(false, pfd[1], packagePath, GetTestCertName());
92 close(pfd[0]);
93 EXPECT_EQ(ret, USCRIPT_INVALID_PARAM);
94 }
95
96 HWTEST_F(UpdateImageBlockTest, update_image_block_test_004, TestSize.Level1)
97 {
98 const string packagePath = "/data/updater/updater/updater_diff_misc_verify_err.zip";
99 int pfd[2]; // 2: pipe read, pipe write
100 int ret = pipe(pfd);
101 EXPECT_GE(ret, 0);
102 ret = ProcessUpdater(false, pfd[1], packagePath, GetTestCertName());
103 close(pfd[0]);
104 EXPECT_NE(ret, 0);
105 }
106 }
107