• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 #include <limits>
19 #include <iostream>
20 #include <fstream>
21 #include <vector>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 #include <unittest_comm.h>
25 #include "utils.h"
26 #include "misc_info/misc_info.h"
27 #include "securec.h"
28 #include "updater_const.h"
29 #include "fs_manager/mount.h"
30 
31 using namespace Updater;
32 using namespace testing::ext;
33 using namespace std;
34 
35 namespace UpdaterUt {
36 class UtilsUnitTest : public testing::Test {
37 public:
SetUpTestCase(void)38     static void SetUpTestCase(void) {};
TearDownTestCase(void)39     static void TearDownTestCase(void) {};
SetUp()40     void SetUp() {};
TearDown()41     void TearDown() {};
42 };
43 
44 HWTEST_F(UtilsUnitTest, updater_utils_test_001, TestSize.Level0)
45 {
46     string emptyStr = Utils::Trim("");
47     EXPECT_STREQ(emptyStr.c_str(), "");
48     emptyStr = Utils::Trim("   ");
49     EXPECT_STREQ(emptyStr.c_str(), "");
50     emptyStr = Utils::Trim("aa   ");
51     EXPECT_STREQ(emptyStr.c_str(), "aa");
52 }
53 
54 HWTEST_F(UtilsUnitTest, updater_utils_test_002, TestSize.Level0)
55 {
56     uint8_t a[1] = {0};
57     a[0] = 1;
58     string newStr = Utils::ConvertSha256Hex(a, 1);
59     EXPECT_STREQ(newStr.c_str(), "01");
60 }
61 
62 HWTEST_F(UtilsUnitTest, updater_utils_test_003, TestSize.Level0)
63 {
64     string str = "aaa\nbbb";
65     vector<string> newStr = Utils::SplitString(str, "\n");
66     EXPECT_EQ(newStr[0], "aaa");
67     EXPECT_EQ(newStr[1], "bbb");
68 }
69 
70 HWTEST_F(UtilsUnitTest, updater_utils_test_004, TestSize.Level0)
71 {
72     string path = string(PATH_MAX, 'a') + "/";
73     EXPECT_EQ(Utils::MkdirRecursive(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH), -1);
74     path = "/data/updater/firstDir/secondDir";
75     EXPECT_EQ(Utils::MkdirRecursive(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH), 0);
76 }
77 
78 HWTEST_F(UtilsUnitTest, updater_utils_test_005, TestSize.Level0)
79 {
80     string input = "";
81     int output = Utils::String2Int<int>(input, 10);
82     EXPECT_EQ(output, 0);
83     input = "0x01";
84     output = Utils::String2Int<int>(input, 10);
85     EXPECT_EQ(output, 1);
86 }
87 
88 HWTEST_F(UtilsUnitTest, updater_utils_test_006, TestSize.Level0)
89 {
90     std::vector<std::string> files;
91     string path = "/no_exist";
92     EXPECT_EQ(Utils::GetFilesFromDirectory(path, files, true), -1);
93     path = "/data";
94     Utils::GetFilesFromDirectory(path, files, true);
95 }
96 
97 HWTEST_F(UtilsUnitTest, WriteFully, TestSize.Level0)
98 {
99     string path = "/data/updater/test_file";
100     uint8_t a[1] = {0};
101     auto fd = open(path.c_str(), O_RDWR | O_CREAT, 0777);
102     EXPECT_EQ(fd != -1, true);
103     EXPECT_EQ(Utils::WriteFully(fd, a, 1), true);
104     close(fd);
105     auto readFd = open(path.c_str(), O_RDONLY);
106     EXPECT_EQ(readFd != -1, true);
107     EXPECT_EQ(Utils::WriteFully(readFd, a, 1), false);
108     close(readFd);
109     EXPECT_EQ(Utils::DeleteFile(path), 0);
110 }
111 
112 HWTEST_F(UtilsUnitTest, ReadFully, TestSize.Level0)
113 {
114     string path = "/data/updater/test_file";
115     uint8_t a[1] = {0};
116     auto fd = open(path.c_str(), O_RDWR | O_CREAT, 0777);
117     EXPECT_EQ(fd != -1, true);
118     EXPECT_EQ(Utils::WriteFully(fd, a, 1), true);
119     EXPECT_EQ(lseek(fd, 0, SEEK_SET) != -1, true);
120     EXPECT_EQ(Utils::ReadFully(fd, a, 1), true);
121     close(fd);
122 
123     auto writeFd = open(path.c_str(),  O_WRONLY);
124     EXPECT_EQ(writeFd != -1, true);
125     EXPECT_EQ(Utils::ReadFully(writeFd, a, 1), false);
126     close(writeFd);
127     EXPECT_EQ(Utils::DeleteFile(path), 0);
128 }
129 
130 HWTEST_F(UtilsUnitTest, ReadFileToString, TestSize.Level0)
131 {
132     string path = "/data/updater/test_file";
133     uint8_t a[1] = {0};
134     auto fd = open(path.c_str(), O_RDWR | O_CREAT, 0777);
135     EXPECT_EQ(fd != -1, true);
136     EXPECT_EQ(Utils::WriteFully(fd, a, 1), true);
137     EXPECT_EQ(lseek(fd, 0, SEEK_SET) != -1, true);
138     string content;
139     EXPECT_EQ(Utils::ReadFileToString(fd, content), true);
140     close(fd);
141 
142     auto writeFd = open(path.c_str(),  O_WRONLY);
143     EXPECT_EQ(writeFd != -1, true);
144     EXPECT_EQ(Utils::ReadFileToString(writeFd, content), false);
145     close(writeFd);
146     EXPECT_EQ(Utils::DeleteFile(path), 0);
147 }
148 
149 HWTEST_F(UtilsUnitTest, WriteStringToFile, TestSize.Level0)
150 {
151     string path = "/data/updater/test_file";
152     uint8_t a[1] = {0};
153     auto fd = open(path.c_str(), O_RDWR | O_CREAT, 0777);
154     EXPECT_EQ(fd != -1, true);
155     EXPECT_EQ(Utils::WriteFully(fd, a, 1), true);
156     string content = "111";
157     EXPECT_EQ(Utils::WriteStringToFile(fd, content), true);
158     close(fd);
159 
160     auto readFd = open(path.c_str(), O_RDONLY);
161     EXPECT_EQ(readFd != -1, true);
162     EXPECT_EQ(Utils::WriteStringToFile(readFd, content), false);
163     close(readFd);
164     EXPECT_EQ(Utils::DeleteFile(path), 0);
165 }
166 
167 HWTEST_F(UtilsUnitTest, CopyFile, TestSize.Level0)
168 {
169     string src = "/data/updater/no_exist";
170     string dest = "/data/updater/dest_file";
171     EXPECT_EQ(Utils::CopyFile(src, dest), false);
172     src = "/data/updater/src_file";
173     auto fd = open(src.c_str(), O_RDWR | O_CREAT, 0777);
174     EXPECT_EQ(fd != -1, true);
175     uint8_t a[1] = {0};
176     EXPECT_EQ(Utils::WriteFully(fd, a, 1), true);
177     close(fd);
178     EXPECT_EQ(Utils::CopyFile(src, dest), true);
179 }
180 
181 HWTEST_F(UtilsUnitTest, CheckDumpResult, TestSize.Level0)
182 {
183     string path = std::string(UPDATER_PATH) + "/" + std::string(UPDATER_RESULT_FILE);
184     auto fd = open(path.c_str(), O_RDWR | O_CREAT, 0777);
185     EXPECT_EQ(fd != -1, true);
186     EXPECT_EQ(Utils::WriteStringToFile(fd, "fail:\n"), true);
187     EXPECT_EQ(lseek(fd, 0, SEEK_SET) != -1, true);
188     close(fd);
189     EXPECT_EQ(Utils::CheckDumpResult(), true);
190 
191     EXPECT_EQ(Utils::DeleteFile(path), 0);
192     EXPECT_EQ(Utils::CheckDumpResult(), false);
193 }
194 
195 HWTEST_F(UtilsUnitTest, WriteDumpResult, TestSize.Level0)
196 {
197     Utils::WriteDumpResult("pass");
198 }
199 
200 HWTEST_F(UtilsUnitTest, RemoveDirTest, TestSize.Level0)
201 {
202     string path = "";
203     EXPECT_EQ(Utils::RemoveDir(path), false);
204     path = "/data/updater/nonExistDir";
205     EXPECT_EQ(Utils::RemoveDir(path), false);
206     path = "/data/updater/rmDir";
207     int ret = mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
208     string secPath = path + "/secRmDir";
209     int secRet = mkdir(secPath.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
210     if (ret == 0 && secRet == 0) {
211         ofstream tmpFile;
212         string filePath = path + "/tmpFile";
213         tmpFile.open(filePath.c_str());
214         if (tmpFile.is_open()) {
215             tmpFile.close();
216             EXPECT_EQ(Utils::RemoveDir(path), true);
217         }
218     }
219 }
220 
221 HWTEST_F(UtilsUnitTest, IsUpdaterMode, TestSize.Level0)
222 {
223     EXPECT_EQ(Utils::IsUpdaterMode(), false);
224 }
225 
226 HWTEST_F(UtilsUnitTest, DeleteFile, TestSize.Level0)
227 {
228     string filePath = "";
229     EXPECT_EQ(Utils::DeleteFile(filePath), -1);
230     filePath = "/data/updater/tmpFile";
231     ofstream tmpFile;
232     tmpFile.open(filePath.c_str());
233     EXPECT_EQ(tmpFile.is_open(), true);
234     tmpFile.close();
235     EXPECT_EQ(Utils::DeleteFile(filePath), 0);
236 }
237 
238 HWTEST_F(UtilsUnitTest, UsSleep, TestSize.Level1)
239 {
240     Utils::UsSleep(1);
241 }
242 
243 HWTEST_F(UtilsUnitTest, PathToRealPath, TestSize.Level1)
244 {
245     string emptyPath = "";
246     string realPath = "";
247     EXPECT_EQ(Utils::PathToRealPath(emptyPath, realPath), false);
248 
249     string path(PATH_MAX + 1, '/');
250     EXPECT_EQ(Utils::PathToRealPath(path, realPath), false);
251 
252     path = "/data/updater/no_exist";
253     EXPECT_EQ(Utils::PathToRealPath(path, realPath), false);
254 
255     path = "/data/../system";
256     EXPECT_EQ(Utils::PathToRealPath(path, realPath), true);
257     EXPECT_EQ(realPath, "/system");
258 }
259 
260 HWTEST_F(UtilsUnitTest, DoReboot, TestSize.Level0)
261 {
262     auto path = GetBlockDeviceByMountPoint(MISC_PATH);
263     string rebootTarget = "";
264     Utils::DoReboot(rebootTarget);
265     struct UpdateMessage msg {};
266     struct UpdateMessage resMsg {};
267     EXPECT_EQ(ReadUpdaterMessage(path, msg), true);
268     EXPECT_EQ(strcmp(msg.command, resMsg.command), 0);
269 
270     rebootTarget = "updater";
271     string command = "not_boot_updater";
272     string resCmd = "boot_updater";
273     EXPECT_EQ(strncpy_s(msg.command, sizeof(msg.command) - 1, command.c_str(), command.size()), 0);
274     EXPECT_EQ(strncpy_s(resMsg.command, sizeof(resMsg.command) - 1, resCmd.c_str(), resCmd.size()), 0);
275     EXPECT_EQ(WriteUpdaterMessage(path, msg), true);
276     Utils::DoReboot(rebootTarget);
277     EXPECT_EQ(ReadUpdaterMessage(path, msg), true);
278     EXPECT_EQ(strcmp(msg.command, resMsg.command), 0);
279 
280     rebootTarget = "flashd";
281     command = "not_flashd";
282     resCmd = "boot_flash";
283     EXPECT_EQ(strncpy_s(msg.command, sizeof(msg.command) - 1, command.c_str(), command.size()), 0);
284     EXPECT_EQ(strncpy_s(resMsg.command, sizeof(resMsg.command) - 1, resCmd.c_str(), resCmd.size()), 0);
285     EXPECT_EQ(WriteUpdaterMessage(path, msg), true);
286     Utils::DoReboot(rebootTarget);
287     EXPECT_EQ(ReadUpdaterMessage(path, msg), true);
288     EXPECT_EQ(strcmp(msg.command, resMsg.command), 0);
289 
290     rebootTarget = "bootloader";
291     command = "not_boot_loader";
292     resCmd = "boot_loader";
293     EXPECT_EQ(strncpy_s(msg.command, sizeof(msg.command) - 1, command.c_str(), command.size()), 0);
294     EXPECT_EQ(strncpy_s(resMsg.command, sizeof(resMsg.command) - 1, resCmd.c_str(), resCmd.size()), 0);
295     EXPECT_EQ(WriteUpdaterMessage(path, msg), true);
296     Utils::DoReboot(rebootTarget);
297     EXPECT_EQ(ReadUpdaterMessage(path, msg), true);
298     EXPECT_EQ(strcmp(msg.command, resMsg.command), 0);
299 
300     rebootTarget = "updater";
301     string extData = "--update_package=/update/ota_package.zip";
302     EXPECT_EQ(strncpy_s(msg.update, sizeof(msg.update) - 1, extData.c_str(), extData.size()), 0);
303     EXPECT_EQ(strncpy_s(resMsg.update, sizeof(resMsg.update) - 1, extData.c_str(), extData.size()), 0);
304     Utils::DoReboot(rebootTarget, extData);
305     EXPECT_EQ(ReadUpdaterMessage(path, msg), true);
306     EXPECT_EQ(strcmp(msg.update, resMsg.update), 0);
307 }
308 } // updater_ut
309