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::CreatePackageInstance();
74     UpdaterStatus status;
75     status = StartUpdaterProc(pkgManager, packagePath, 0);
76     EXPECT_EQ(status, UPDATE_CORRUPT);
77 
78     packagePath = "/data/updater/updater/updater_with_incorrect_binary.zip";
79     status = StartUpdaterProc(pkgManager, packagePath, 0);
80     EXPECT_EQ(status, UPDATE_CORRUPT);
81 
82     packagePath = "/data/updater/updater/updater.zip";
83     std::vector<std::string> components;
84     int32_t ret = pkgManager->LoadPackage(packagePath, GetTestCertName(), components);
85     EXPECT_EQ(ret, 0);
86     status = StartUpdaterProc(pkgManager, packagePath, 0);
87     EXPECT_EQ(status, UPDATE_SUCCESS);
88 
89     // retrycount is greater than 0.
90     status = StartUpdaterProc(pkgManager, packagePath, 1);
91     EXPECT_EQ(status, UPDATE_RETRY);
92 
93     packagePath = "/data/updater/updater/updater_binary_abnormal.zip";
94     status = StartUpdaterProc(pkgManager, packagePath, 1);
95     PkgManager::ReleasePackageInstance(pkgManager);
96     EXPECT_EQ(status, UPDATE_ERROR);
97 }
98 
99 HWTEST_F(UpdaterUnitTest, updater_GetUpdatePackageInfo, TestSize.Level1)
100 {
101     // Non-exist file.
102     PkgManager::PkgManagerPtr pkgManager = PkgManager::CreatePackageInstance();
103     std::string nonExistPackagePath = "/data/non_exist";
104     int ret = GetUpdatePackageInfo(pkgManager, nonExistPackagePath);
105     EXPECT_EQ(ret, static_cast<int>(PKG_INVALID_FILE));
106 
107     // valid  updater package.
108     std::string validUpdaterPackage = "/data/updater/updater/updater.zip";
109 
110     ret = GetUpdatePackageInfo(pkgManager, validUpdaterPackage);
111     PkgManager::ReleasePackageInstance(pkgManager);
112     EXPECT_EQ(ret, static_cast<int>(PKG_SUCCESS));
113 }
114 
115 HWTEST_F(UpdaterUnitTest, updater_UpdateSdcard, TestSize.Level1)
116 {
117     UpdaterStatus status;
118     UpdaterParams upParams;
119     upParams.updateMode = SDCARD_UPDATE;
120     status = UpdaterFromSdcard(upParams);
121     EXPECT_EQ(status, UPDATE_SUCCESS);
122 }
123 } // namespace updater_ut
124