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 "updater_unittest.h"
17 #include <cerrno>
18 #include <cstdio>
19 #include <iostream>
20 #include <sys/mount.h>
21 #include <unistd.h>
22 #include "fs_manager/mount.h"
23 #include "log/log.h"
24 #include "package/pkg_manager.h"
25 #include "unittest_comm.h"
26 #include "updater/updater.h"
27 #include "updater_main.h"
28 #include "updater_ui.h"
29 #include "utils.h"
30
31 namespace UpdaterUt {
32 using namespace testing::ext;
33 using namespace UpdaterUt;
34 using namespace Updater;
35 using namespace std;
36 using namespace Hpackage;
37 using namespace testing;
38
SetUp()39 void UpdaterUnitTest::SetUp()
40 {
41 unsigned long mountFlag = MS_REMOUNT;
42 std::string tmpPath = "/tmp";
43 // mount rootfs to read-write.
44 std::string rootSource = "/dev/root";
45 if (mount(rootSource.c_str(), "/", "ext4", mountFlag, nullptr) != 0) {
46 std::cout << "Cannot re-mount rootfs\n";
47 }
48 mode_t mode = (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
49 auto ret = mkdir(tmpPath.c_str(), mode);
50 if (ret != 0 && errno != EEXIST) {
51 std::cout << "Cannot create \"/tmp\" directory: " << errno << "\n";
52 }
53
54 // Load specific fstab for testing.
55 LoadSpecificFstab("/data/updater/updater/etc/fstab.ut.updater");
56 }
57
TearDown()58 void UpdaterUnitTest::TearDown() {}
59
SetUpTestCase()60 void UpdaterUnitTest::SetUpTestCase()
61 {
62 UpdaterUiInit();
63 }
64
TearDownTestCase()65 void UpdaterUnitTest::TearDownTestCase()
66 {
67 DeleteView();
68 }
69
70 HWTEST_F(UpdaterUnitTest, updater_StartUpdaterProc, TestSize.Level1)
71 {
72 std::string packagePath = "/data/updater/updater/updater_without_updater_binary.zip";
73 PkgManager::PkgManagerPtr pkgManager = PkgManager::GetPackageInstance();
74 int maxTemperature;
75 UpdaterStatus status;
76 status = StartUpdaterProc(pkgManager, packagePath, 0, maxTemperature);
77 EXPECT_EQ(status, UPDATE_CORRUPT);
78
79 packagePath = "/data/updater/updater/updater_with_incorrect_binary.zip";
80 status = StartUpdaterProc(pkgManager, packagePath, 0, maxTemperature);
81 EXPECT_EQ(status, UPDATE_CORRUPT);
82
83 packagePath = "/data/updater/updater/updater.zip";
84 std::vector<std::string> components;
85 int32_t ret = pkgManager->LoadPackage(packagePath, GetTestCertName(), components);
86 EXPECT_EQ(ret, 0);
87 status = StartUpdaterProc(pkgManager, packagePath, 0, maxTemperature);
88 EXPECT_EQ(status, UPDATE_SUCCESS);
89
90 // retrycount is greater than 0.
91 status = StartUpdaterProc(pkgManager, packagePath, 1, maxTemperature);
92 EXPECT_EQ(status, UPDATE_RETRY);
93
94 packagePath = "/data/updater/updater/updater_binary_abnormal.zip";
95 status = StartUpdaterProc(pkgManager, packagePath, 1, maxTemperature);
96 PkgManager::ReleasePackageInstance(pkgManager);
97 EXPECT_EQ(status, UPDATE_ERROR);
98 }
99
100 HWTEST_F(UpdaterUnitTest, updater_GetUpdatePackageInfo, TestSize.Level1)
101 {
102 // Non-exist file.
103 PkgManager::PkgManagerPtr pkgManager = PkgManager::GetPackageInstance();
104 std::string nonExistPackagePath = "/data/non_exist";
105 int ret = GetUpdatePackageInfo(pkgManager, nonExistPackagePath);
106 EXPECT_EQ(ret, static_cast<int>(PKG_INVALID_FILE));
107
108 // valid updater package.
109 std::string validUpdaterPackage = "/data/updater/updater/updater.zip";
110
111 ret = GetUpdatePackageInfo(pkgManager, validUpdaterPackage);
112 PkgManager::ReleasePackageInstance(pkgManager);
113 EXPECT_EQ(ret, static_cast<int>(PKG_SUCCESS));
114 }
115
116 HWTEST_F(UpdaterUnitTest, updater_UpdateSdcard, TestSize.Level1)
117 {
118 UpdaterStatus status;
119 status = UpdaterFromSdcard();
120 EXPECT_EQ(status, UPDATE_SUCCESS);
121 }
122 } // namespace updater_ut
123