• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 <fcntl.h>
17 #include <gtest/gtest.h>
18 #include <memory>
19 #include <sys/ioctl.h>
20 #include "log/log.h"
21 #include "securec.h"
22 #include "updater/updater_const.h"
23 #include "updater/updater.h"
24 #include "fs_manager/mount.h"
25 #include "misc_info/misc_info.h"
26 #include "updater_main.h"
27 #include "utils.h"
28 
29 using namespace Updater;
30 using namespace std;
31 using namespace testing::ext;
32 
33 namespace {
34 constexpr uint32_t MAX_ARG_SIZE = 24;
35 class UpdaterUtilUnitTest : public testing::Test {
36 public:
UpdaterUtilUnitTest()37     UpdaterUtilUnitTest()
38     {
39         InitUpdaterLogger("UPDATER", TMP_LOG, TMP_STAGE_LOG, TMP_ERROR_CODE_PATH);
40     }
~UpdaterUtilUnitTest()41     ~UpdaterUtilUnitTest() {}
42 
SetUpTestCase(void)43     static void SetUpTestCase(void) {}
TearDownTestCase(void)44     static void TearDownTestCase(void) {}
SetUp()45     void SetUp() {}
TearDown()46     void TearDown() {}
TestBody()47     void TestBody() {}
48 };
49 
50 HWTEST_F(UpdaterUtilUnitTest, DeleteUpdaterPath, TestSize.Level1)
51 {
52     std::string path = "/data/test/test/test";
53     bool ret = DeleteUpdaterPath(path);
54     EXPECT_EQ(ret, true);
55 
56     path = "/data/test";
57     ret = DeleteUpdaterPath(path);
58     EXPECT_EQ(ret, true);
59 }
60 
61 HWTEST_F(UpdaterUtilUnitTest, ClearMisc, TestSize.Level1)
62 {
63     bool ret = ClearMisc();
64     EXPECT_EQ(ret, true);
65 }
66 
67 HWTEST_F(UpdaterUtilUnitTest, IsSDCardExist, TestSize.Level1)
68 {
69     std::string sdcardStr = "";
70     bool ret = IsSDCardExist(sdcardStr);
71     EXPECT_EQ(ret, false);
72 }
73 
74 HWTEST_F(UpdaterUtilUnitTest, IsFlashd, TestSize.Level1)
75 {
76     EXPECT_EQ(IsFlashd({"boot_updater", "", "boot_flash"}), true);
77     EXPECT_EQ(IsFlashd({"boot_updater", "", ""}), false);
78 }
79 
80 HWTEST_F(UpdaterUtilUnitTest, IsUpdater, TestSize.Level1)
81 {
82     EXPECT_EQ(IsUpdater({"boot_updater", "", ""}), true);
83     EXPECT_EQ(IsUpdater({"boot_updater", "", "boot_flash"}), false);
84     EXPECT_EQ(IsUpdater({"boot_updater", "", "xxx"}), true);
85 }
86 
87 HWTEST_F(UpdaterUtilUnitTest, SelectMode, TestSize.Level1)
88 {
89     // clear already registered mode
90     GetBootModes().clear();
91 
__anonc027f0fa0202(int argc, char **argv) 92     auto dummyEntry = [] (int argc, char **argv) -> int { return 0; };
93     // register modes
94     RegisterMode({ IsFlashd, "FLASHD", "", dummyEntry });
95     RegisterMode({ IsUpdater, "UPDATER", "", dummyEntry });
96 
97     // test select mode
98     auto mode = SelectMode({"boot_updater", "", ""});
99     ASSERT_NE(mode, std::nullopt);
100     EXPECT_EQ(mode->modeName, "UPDATER");
101 
102     mode = SelectMode({"boot_updater", "", "boot_flash"});
103     ASSERT_NE(mode, std::nullopt);
104     EXPECT_EQ(mode->modeName, "FLASHD");
105 
106     mode = SelectMode({"invalid_command", "", ""});
107     EXPECT_EQ(mode, std::nullopt);
108 }
109 
110 HWTEST_F(UpdaterUtilUnitTest, PostUpdater, TestSize.Level1)
111 {
112     PostUpdater(true);
113     LoadSpecificFstab("/data/updater/updater/etc/fstab.ut.updater");
114     int ret = access(TMP_LOG, 0);
115     EXPECT_EQ(ret, 0);
116     PostUpdater(true);
117     ret = access(UPDATER_LOG, 0);
118     EXPECT_EQ(ret, 0);
119 }
120 
121 HWTEST_F(UpdaterUtilUnitTest, ParseParams, TestSize.Level1)
122 {
123     UpdateMessage boot {};
124     std::string commandMsg = "";
125     std::string updateMsg = "";
126     const std::string commandFile = "/data/updater/command";
127     auto fp = std::unique_ptr<FILE, decltype(&fclose)>(fopen(commandFile.c_str(), "wb"), fclose);
128     EXPECT_NE(fp, nullptr);
129     EXPECT_EQ(strncpy_s(boot.command, sizeof(boot.command) - 1, commandMsg.c_str(), commandMsg.size()), 0);
130     EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update) - 1, updateMsg.c_str(), updateMsg.size()), 0);
131     bool bRet = WriteUpdaterMessage(commandFile, boot);
132     EXPECT_EQ(bRet, true);
133     char **argv = new char*[1];
134     argv[0] = new char[MAX_ARG_SIZE];
135     std::string str = "./UpdaterMain";
136     EXPECT_EQ(strncpy_s(argv[0], MAX_ARG_SIZE, str.c_str(), str.size()), 0);
137     int argc = 1;
138     std::vector<std::string> args = Utils::ParseParams(argc, argv);
139     std::string res = "";
140     for (auto s : args) {
141         res += s;
142     }
143     EXPECT_EQ("./UpdaterMain", res);
144 
145     commandMsg = "boot_updater";
146     updateMsg = "--update_package=updater_full.zip";
147     EXPECT_EQ(strncpy_s(boot.command, sizeof(boot.command) - 1, commandMsg.c_str(), commandMsg.size()), 0);
148     EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update) - 1, updateMsg.c_str(), updateMsg.size()), 0);
149     bRet = WriteUpdaterMessage(commandFile, boot);
150     EXPECT_EQ(bRet, true);
151 
152     args = Utils::ParseParams(argc, argv);
153     res = "";
154     for (auto s : args) {
155         res += s;
156     }
157     EXPECT_EQ("./UpdaterMain--update_package=updater_full.zip", res);
158 }
159 
160 HWTEST_F(UpdaterUtilUnitTest, UpdaterMain, TestSize.Level1)
161 {
162     UpdateMessage boot {};
163     if (access("/data/updater/", 0)) {
164         int ret = mkdir("/data/updater/", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
165         ASSERT_EQ(ret, 0);
166     }
167     const std::string commandFile = "/data/updater/command";
168     auto fp = std::unique_ptr<FILE, decltype(&fclose)>(fopen(commandFile.c_str(), "wb"), fclose);
169     EXPECT_NE(fp, nullptr);
170     const std::string commandMsg = "boot_updater";
171     const std::string updateMsg = "--update_package=/data/updater/updater/updater_full.zip";
172     EXPECT_EQ(strncpy_s(boot.command, sizeof(boot.command) - 1, commandMsg.c_str(), commandMsg.size()), 0);
173     EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update) - 1, updateMsg.c_str(), updateMsg.size()), 0);
174     bool bRet = WriteUpdaterMessage(commandFile, boot);
175     EXPECT_EQ(bRet, true);
176     char **argv = new char*[1];
177     argv[0] = new char[MAX_ARG_SIZE];
178     EXPECT_EQ(strncpy_s(argv[0], MAX_ARG_SIZE, "./UpdaterMain", MAX_ARG_SIZE), 0);
179     int argc = 1;
180 
181     int ret = UpdaterMain(argc, argv);
182     EXPECT_EQ(ret, 0);
183     delete argv[0];
184     delete []argv;
185 }
186 
187 HWTEST_F(UpdaterUtilUnitTest, UpdaterFromSdcardTest, TestSize.Level1)
188 {
189     UpdateMessage boot {};
190     if (access("/data/updater/", 0)) {
191         int ret = mkdir("/data/updater/", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
192         ASSERT_EQ(ret, 0);
193     }
194     const std::string commandFile = "/data/updater/command";
195     auto fp = std::unique_ptr<FILE, decltype(&fclose)>(fopen(commandFile.c_str(), "wb"), fclose);
196     EXPECT_NE(fp, nullptr);
197     const std::string commandMsg = "boot_updater";
198     const std::string updateMsg = "--sdcard_update";
199     EXPECT_EQ(strncpy_s(boot.command, sizeof(boot.command) - 1, commandMsg.c_str(), commandMsg.size()), 0);
200     EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update) - 1, updateMsg.c_str(), updateMsg.size()), 0);
201     bool bRet = WriteUpdaterMessage(commandFile, boot);
202     EXPECT_EQ(bRet, true);
203     char **argv = new char*[1];
204     argv[0] = new char[MAX_ARG_SIZE];
205     EXPECT_EQ(strncpy_s(argv[0], MAX_ARG_SIZE, "./UpdaterMain", MAX_ARG_SIZE), 0);
206     int argc = 1;
207     EXPECT_EQ(UpdaterMain(argc, argv), 0);
208     delete argv[0];
209     delete []argv;
210 }
211 
212 HWTEST_F(UpdaterUtilUnitTest, DoInstallUpdaterPackageTest, TestSize.Level1)
213 {
214     UpdaterParams upParams;
215     upParams.callbackProgress = nullptr;
216     std::vector<std::string> output;
217     EXPECT_EQ(DoInstallUpdaterPackage(nullptr, upParams, HOTA_UPDATE), UPDATE_CORRUPT);
__anonc027f0fa0302(float value) 218     upParams.callbackProgress = [] (float value) {};
219     EXPECT_EQ(DoInstallUpdaterPackage(nullptr, upParams, HOTA_UPDATE), UPDATE_CORRUPT);
220     upParams.retryCount = 0;
221     EXPECT_EQ(DoInstallUpdaterPackage(nullptr, upParams, HOTA_UPDATE), UPDATE_CORRUPT);
222     upParams.retryCount = 1;
223     EXPECT_EQ(DoInstallUpdaterPackage(nullptr, upParams, HOTA_UPDATE), UPDATE_CORRUPT);
224 }
225 
226 HWTEST_F(UpdaterUtilUnitTest, updater_ExtractUpdaterBinary, TestSize.Level1)
227 {
228     Hpackage::PkgManager::PkgManagerPtr pkgManager = Hpackage::PkgManager::CreatePackageInstance();
229     std::string path = "xxx";
230     int32_t ret = ExtractUpdaterBinary(pkgManager, path, UPDATER_BINARY);
231     EXPECT_EQ(ret, 1);
232     path = "/data/updater/updater/updater_full.zip";
233     ret = ExtractUpdaterBinary(pkgManager, path, UPDATER_BINARY);
234     Hpackage::PkgManager::ReleasePackageInstance(pkgManager);
235     EXPECT_EQ(ret, 1);
236 }
237 
238 HWTEST_F(UpdaterUtilUnitTest, updater_IsSpaceCapacitySufficient, TestSize.Level1)
239 {
240     std::vector<std::string> packagePath;
241     UpdaterStatus status = IsSpaceCapacitySufficient(packagePath);
242     EXPECT_EQ(status, UPDATE_ERROR);
243     packagePath.push_back("/data/updater/updater/updater_full.zip");
244     status = IsSpaceCapacitySufficient(packagePath);
245     EXPECT_EQ(status, UPDATE_SUCCESS);
246     packagePath.push_back("xxx");
247     ProgressSmoothHandler(0, 0);
248     status = IsSpaceCapacitySufficient(packagePath);
249     EXPECT_EQ(status, UPDATE_ERROR);
250 }
251 
252 HWTEST_F(UpdaterUtilUnitTest, updater_HandleChildOutput, TestSize.Level1)
253 {
254     std::string buf = "xxx";
255     bool retryUpdate = false;
256     UpdaterParams upParams;
257     HandleChildOutput(buf, 0, retryUpdate, upParams);
258     EXPECT_EQ(retryUpdate, false);
259     HandleChildOutput(buf, buf.size(), retryUpdate, upParams);
260     EXPECT_EQ(retryUpdate, false);
261     buf = "write_log:xxx";
262     HandleChildOutput(buf, buf.size(), retryUpdate, upParams);
263     EXPECT_EQ(retryUpdate, false);
264     buf = "retry_update:xxx";
265     HandleChildOutput(buf, buf.size(), retryUpdate, upParams);
266     EXPECT_EQ(retryUpdate, true);
267     buf = "ui_log:xxx";
268     HandleChildOutput(buf, buf.size(), retryUpdate, upParams);
269     EXPECT_EQ(retryUpdate, true);
270     buf = "show_progress:xxx";
271     HandleChildOutput(buf, buf.size(), retryUpdate, upParams);
272     EXPECT_EQ(retryUpdate, true);
273     buf = "show_progress:xxx:xxx";
274     HandleChildOutput(buf, buf.size(), retryUpdate, upParams);
275     EXPECT_EQ(retryUpdate, true);
276     buf = "set_progress:xxx";
277     HandleChildOutput(buf, buf.size(), retryUpdate, upParams);
278     EXPECT_EQ(retryUpdate, true);
279     buf = "xxx:xxx";
280     HandleChildOutput(buf, buf.size(), retryUpdate, upParams);
281     EXPECT_EQ(retryUpdate, true);
282 }
283 }