• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "adapter_utility_ohos_test.h"
17 
18 #include <fcntl.h>
19 #include <fstream>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 
23 #include "ash_memory_utils.h"
24 #include "common_utils.h"
25 #include "file_util.h"
26 #include "securec.h"
27 #include "socket_util.h"
28 #include "time_util.h"
29 
30 using namespace std;
31 
32 namespace OHOS {
33 namespace HiviewDFX {
34 namespace {
35 const char LOG_FILE_PATH[] = "/data/test/adapter_utility_test/";
36 constexpr int SUFFIX_0 = 0;
37 constexpr int SUFFIX_1 = 1;
38 
GetLogDir(std::string & testCaseName)39 std::string GetLogDir(std::string& testCaseName)
40 {
41     std::string workPath = std::string(LOG_FILE_PATH);
42     if (workPath.back() != '/') {
43         workPath = workPath + "/";
44     }
45     workPath.append(testCaseName);
46     workPath.append("/");
47     std::string logDestDir = workPath;
48     if (!FileUtil::FileExists(logDestDir)) {
49         FileUtil::ForceCreateDirectory(logDestDir, FileUtil::FILE_PERM_770);
50     }
51     return logDestDir;
52 }
53 
GenerateLogFileName(std::string & testCaseName,int index)54 std::string GenerateLogFileName(std::string& testCaseName, int index)
55 {
56     return GetLogDir(testCaseName) + "testFile" + std::to_string(index);
57 }
58 
59 struct AshMemTestStruct {
AshMemTestStructOHOS::HiviewDFX::__anon884b92eb0111::AshMemTestStruct60     explicit AshMemTestStruct(std::string data) : data(data) {};
61 
GetDataOHOS::HiviewDFX::__anon884b92eb0111::AshMemTestStruct62     void* GetData(uint32_t& dataSize) const
63     {
64         auto dataLen = data.length() + 1;
65         char* buff = reinterpret_cast<char *>(malloc(dataLen));
66         if (buff == nullptr) {
67             return nullptr;
68         }
69         auto ret = memset_s(buff, dataLen, 0, dataLen);
70         if (ret != EOK) {
71             return nullptr;
72         }
73 
74         ret = memcpy_s(buff, dataLen, data.c_str(), data.length());
75         if (ret != EOK) {
76             return nullptr;
77         }
78         dataSize = dataLen;
79         return buff;
80     }
81 
ParseDataOHOS::HiviewDFX::__anon884b92eb0111::AshMemTestStruct82     static AshMemTestStruct ParseData(const char* dataInput, const uint32_t dataSize)
83     {
84         if (dataInput == nullptr || dataInput[dataSize - 1] != 0) {
85             return AshMemTestStruct("");
86         }
87         return AshMemTestStruct(dataInput);
88     }
89     std::string data;
90 };
91 }
92 
SetUpTestCase()93 void AdapterUtilityOhosTest::SetUpTestCase() {}
94 
TearDownTestCase()95 void AdapterUtilityOhosTest::TearDownTestCase() {}
96 
SetUp()97 void AdapterUtilityOhosTest::SetUp() {}
98 
TearDown()99 void AdapterUtilityOhosTest::TearDown()
100 {
101     (void)FileUtil::ForceRemoveDirectory(LOG_FILE_PATH);
102 }
103 
104 /**
105  * @tc.name: SocketUtilOhosTest001
106  * @tc.desc: Test GetExistingSocketServer defined in namespace SocketUtil
107  * @tc.type: FUNC
108  * @tc.require: issueI65DUW
109  */
110 HWTEST_F(AdapterUtilityOhosTest, SocketUtilOhosTest001, testing::ext::TestSize.Level3)
111 {
112     int socketFdIndex = 0;
113     auto ret = SocketUtil::GetExistingSocketServer("/dev/socket/unix/hisysevent", socketFdIndex);
114     int expectedRet = -1;
115     ASSERT_EQ(expectedRet, ret);
116 }
117 
118 /**
119  * @tc.name: CommonUtilsOhosTest001
120  * @tc.desc: Test ExecCommand defined in namespace CommonUtils
121  * @tc.type: FUNC
122  * @tc.require: issueI65DUW
123  */
124 HWTEST_F(AdapterUtilityOhosTest, CommonUtilsOhosTest001, testing::ext::TestSize.Level3)
125 {
126     std::vector<std::string> cmdRet;
127     auto ret = CommonUtils::ExecCommand("hisysevent -l -m 10000", cmdRet);
128     int expectRet = 0;
129     ASSERT_EQ(expectRet, ret);
130     ret = CommonUtils::ExecCommand("", cmdRet);
131     ASSERT_EQ(expectRet, ret);
132 }
133 
134 /**
135  * @tc.name: CommonUtilsOhosTest002
136  * @tc.desc: Test GetPidByName defined in namespace CommonUtils
137  * @tc.type: FUNC
138  * @tc.require: issueI65DUW
139  */
140 HWTEST_F(AdapterUtilityOhosTest, CommonUtilsOhosTest002, testing::ext::TestSize.Level3)
141 {
142     std::vector<std::string> cmdRet;
143     auto hiviewProcessId = CommonUtils::GetPidByName("hiview");
144     ASSERT_TRUE(hiviewProcessId > 0);
145 }
146 
147 /**
148  * @tc.name: CommonUtilsOhosTest003
149  * @tc.desc: Test WriteCommandResultToFile defined in namespace CommonUtils
150  * @tc.type: FUNC
151  * @tc.require: issueI65DUW
152  */
153 HWTEST_F(AdapterUtilityOhosTest, CommonUtilsOhosTest003, testing::ext::TestSize.Level3)
154 {
155     std::string caseName("CommonUtilsOhosTest003");
156     std::string cmd = "";
157     auto ret = CommonUtils::WriteCommandResultToFile(0, cmd);
158     ASSERT_EQ(false, ret);
159     std::string cmd2 = "hisysevent -l -m 1 | wc -l";
160     auto fd = FileUtil::Open(GenerateLogFileName(caseName, SUFFIX_0),
161         O_CREAT | O_WRONLY | O_TRUNC, FileUtil::FILE_PERM_770);
162     ret = CommonUtils::WriteCommandResultToFile(fd, cmd2);
163     ASSERT_EQ(true, ret);
164     (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_0), "");
165     fd = FileUtil::Open(GenerateLogFileName(caseName, SUFFIX_0),
166         O_CREAT | O_WRONLY | O_TRUNC, FileUtil::FILE_PERM_770);
167     ret = CommonUtils::WriteCommandResultToFile(fd, cmd2);
168     ASSERT_EQ(true, ret);
169 }
170 
171 /**
172  * @tc.name: TimeUtilOhosTest001
173  * @tc.desc: Test Sleep/GetMillSecOfSec defined in namespace TimeUtil
174  * @tc.type: FUNC
175  * @tc.require: issueI65DUW
176  */
177 HWTEST_F(AdapterUtilityOhosTest, TimeUtilOhosTest001, testing::ext::TestSize.Level3)
178 {
179     auto time = TimeUtil::GetMillSecOfSec();
180     ASSERT_GE(time, 0);
181     int sleepSecs = 1;
182     TimeUtil::Sleep(sleepSecs);
183     ASSERT_TRUE(true);
184 }
185 
186 /**
187  * @tc.name: TimeUtilOhosTest002
188  * @tc.desc: Test Get0ClockStampMs/GetSteadyClockTimeMs defined in namespace TimeUtil
189  * @tc.type: FUNC
190  * @tc.require: issueI65DUW
191  */
192 HWTEST_F(AdapterUtilityOhosTest, TimeUtilOhosTest002, testing::ext::TestSize.Level3)
193 {
194     auto time1 = TimeUtil::Get0ClockStampMs();
195     auto time2 = TimeUtil::GetSteadyClockTimeMs();
196     ASSERT_GE(time1, 0);
197     ASSERT_GE(time2, 0);
198 }
199 
200 /**
201  * @tc.name: FileUtilOhosTest001
202  * @tc.desc: Test LoadBufferFromFile/SaveBufferToFile defined in namespace FileUtil
203  * @tc.type: FUNC
204  * @tc.require: issueI65DUW
205  */
206 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest001, testing::ext::TestSize.Level3)
207 {
208     std::string caseName("FileUtilOhosTest001");
209     std::vector<char> content;
210     (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_0), "123");
211     (void)FileUtil::LoadBufferFromFile(GenerateLogFileName(caseName, SUFFIX_0), content);
212     ASSERT_TRUE(true);
213     (void)FileUtil::SaveBufferToFile(GenerateLogFileName(caseName, SUFFIX_0), content, true);
214     ASSERT_TRUE(true);
215 }
216 
217 /**
218  * @tc.name: FileUtilOhosTest002
219  * @tc.desc: Test ExtractFilePath/ExtractFileName defined in namespace FileUtil
220  * @tc.type: FUNC
221  * @tc.require: issueI65DUW
222  */
223 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest002, testing::ext::TestSize.Level3)
224 {
225     std::string path = "ohos/test/123.txt";
226     auto dir = FileUtil::ExtractFilePath(path);
227     ASSERT_EQ("ohos/test/", dir);
228     auto name = FileUtil::ExtractFileName(path);
229     ASSERT_EQ("123.txt", name);
230 }
231 
232 /**
233  * @tc.name: FileUtilOhosTest003
234  * @tc.desc: Test ExcludeTrailingPathDelimiter/IncludeTrailingPathDelimiter defined in namespace FileUtil
235  * @tc.type: FUNC
236  * @tc.require: issueI65DUW
237  */
238 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest003, testing::ext::TestSize.Level3)
239 {
240     auto excludeRet = FileUtil::ExcludeTrailingPathDelimiter("ohos/test/123.txt");
241     ASSERT_EQ("ohos/test/123.txt", excludeRet);
242     auto name = FileUtil::IncludeTrailingPathDelimiter("ohos/test/123.txt/");
243     ASSERT_EQ("ohos/test/123.txt/", name);
244 }
245 
246 /**
247  * @tc.name: FileUtilOhosTest004
248  * @tc.desc: Test ChangeModeFile defined in namespace FileUtil
249  * @tc.type: FUNC
250  * @tc.require: issueI65DUW
251  */
252 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest004, testing::ext::TestSize.Level3)
253 {
254     std::string caseName("FileUtilOhosTest004");
255     (void)FileUtil::ChangeModeFile(GenerateLogFileName(caseName, SUFFIX_0), FileUtil::FILE_PERM_755);
256     ASSERT_TRUE(true);
257     (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_0), "test content");
258     (void)FileUtil::ChangeModeFile(GenerateLogFileName(caseName, SUFFIX_0), FileUtil::FILE_PERM_660);
259     ASSERT_TRUE(true);
260 }
261 
262 /**
263  * @tc.name: FileUtilOhosTest005
264  * @tc.desc: Test Umask defined in namespace FileUtil
265  * @tc.type: FUNC
266  * @tc.require: issueI65DUW
267  */
268 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest005, testing::ext::TestSize.Level3)
269 {
270     auto umask = FileUtil::Umask(FileUtil::FILE_PERM_755);
271     auto expectedRet = 18;
272     ASSERT_EQ(expectedRet, umask);
273 }
274 
275 /**
276  * @tc.name: FileUtilOhosTest006
277  * @tc.desc: Test FormatPath2UnixStyle/RemoveFolderBeginWith defined in namespace FileUtil
278  * @tc.type: FUNC
279  * @tc.require: issueI65DUW
280  */
281 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest006, testing::ext::TestSize.Level3)
282 {
283     std::string path = std::string(LOG_FILE_PATH);
284     FileUtil::FormatPath2UnixStyle(path);
285     ASSERT_TRUE(true);
286     FileUtil::RemoveFolderBeginWith(path, "data");
287     ASSERT_TRUE(true);
288 }
289 
290 /**
291  * @tc.name: FileUtilOhosTest007
292  * @tc.desc: Test WriteBufferToFd defined in namespace FileUtil
293  * @tc.type: FUNC
294  * @tc.require: issueI65DUW
295  */
296 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest007, testing::ext::TestSize.Level3)
297 {
298     std::string caseName("FileUtilOhosTest007");
299     auto fd = -1;
300     std::string writeContent = "write content";
301     auto ret = FileUtil::WriteBufferToFd(fd, writeContent.c_str(), writeContent.size());
302     ASSERT_TRUE(!ret);
303     (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_0), "test");
304     fd = FileUtil::Open(GenerateLogFileName(caseName, SUFFIX_0),
305         O_CREAT | O_WRONLY | O_TRUNC, FileUtil::FILE_PERM_770);
306     ret = FileUtil::WriteBufferToFd(fd, nullptr, writeContent.size());
307     ASSERT_TRUE(!ret);
308     (void)FileUtil::WriteBufferToFd(fd, "", writeContent.size());
309     ASSERT_TRUE(true);
310     (void)FileUtil::WriteBufferToFd(fd, writeContent.c_str(), writeContent.size());
311     ASSERT_TRUE(true);
312 }
313 
314 /**
315  * @tc.name: FileUtilOhosTest008
316  * @tc.desc: Test CreateFile defined in namespace FileUtil
317  * @tc.type: FUNC
318  * @tc.require: issueI65DUW
319  */
320 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest008, testing::ext::TestSize.Level3)
321 {
322     std::string caseName("FileUtilOhosTest008");
323     int expectedFailedRet = -1;
324     auto ret = FileUtil::CreateFile(GenerateLogFileName(caseName, SUFFIX_0));
325     ASSERT_EQ(expectedFailedRet, ret);
326     (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_0), "1111");
327     (void)FileUtil::CreateFile(GenerateLogFileName(caseName, SUFFIX_0));
328     ASSERT_TRUE(true);
329 }
330 
331 /**
332  * @tc.name: FileUtilOhosTest009
333  * @tc.desc: Test CopyFile defined in namespace FileUtil
334  * @tc.type: FUNC
335  * @tc.require: issueI65DUW
336  */
337 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest009, testing::ext::TestSize.Level3)
338 {
339     std::string caseName("FileUtilOhosTest009");
340     int expectedFailedRet = -1;
341     auto ret = FileUtil::CopyFile("//...../invalid_dest_file", GenerateLogFileName(caseName, SUFFIX_1));
342     ASSERT_EQ(expectedFailedRet, ret);
343     (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_0), "test");
344     ret = FileUtil::CopyFile(GenerateLogFileName(caseName, SUFFIX_0), "//...../invalid_dest_file");
345     ASSERT_EQ(expectedFailedRet, ret);
346     (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_1), "test");
347     (void)FileUtil::CopyFile(GenerateLogFileName(caseName, SUFFIX_0), GenerateLogFileName(caseName, SUFFIX_1));
348     ASSERT_TRUE(true);
349 }
350 
351 /**
352  * @tc.name: FileUtilOhosTest010
353  * @tc.desc: Test GetLastLine defined in namespace FileUtil
354  * @tc.type: FUNC
355  * @tc.require: issueI65DUW
356  */
357 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest010, testing::ext::TestSize.Level3)
358 {
359     std::string caseName("FileUtilOhosTest010");
360     ifstream in(GenerateLogFileName(caseName, SUFFIX_0));
361     std::string line;
362     int invalidMaxLen = 5;
363     auto ret = FileUtil::GetLastLine(in, line, invalidMaxLen);
364     ASSERT_TRUE(!ret);
365     (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_1), "line1");
366     (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_1), "\nline2");
367     (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_1), "\nline3");
368     ifstream in1(GenerateLogFileName(caseName, SUFFIX_1));
369     ret = FileUtil::GetLastLine(in1, line, invalidMaxLen);
370     ASSERT_TRUE(!ret);
371     int validMaxLen = 2;
372     ret = FileUtil::GetLastLine(in1, line, validMaxLen);
373     ASSERT_TRUE(!ret);
374     validMaxLen = 3;
375     ret = FileUtil::GetLastLine(in1, line, validMaxLen);
376     ASSERT_TRUE(!ret);
377 }
378 
379 /**
380  * @tc.name: FileUtilOhosTest011
381  * @tc.desc: Test GetParentDir defined in namespace FileUtil
382  * @tc.type: FUNC
383  * @tc.require: issueI65DUW
384  */
385 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest011, testing::ext::TestSize.Level3)
386 {
387     auto ret = FileUtil::GetParentDir("/");
388     ASSERT_EQ("", ret);
389     ret = FileUtil::GetParentDir("123/345/789");
390     ASSERT_EQ("123/345", ret);
391 }
392 
393 /**
394  * @tc.name: FileUtilOhosTest012
395  * @tc.desc: Test IslegalPath defined in namespace FileUtil
396  * @tc.type: FUNC
397  * @tc.require: issueI65DUW
398  */
399 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest012, testing::ext::TestSize.Level3)
400 {
401     auto ret = FileUtil::IsLegalPath("aa/../bb");
402     ASSERT_TRUE(!ret);
403     ret = FileUtil::IsLegalPath("aa/./bb");
404     ASSERT_TRUE(!ret);
405     ret = FileUtil::IsLegalPath("aa/bb/");
406     ASSERT_TRUE(ret);
407     ret = FileUtil::IsLegalPath("aa/bb/cc");
408     ASSERT_TRUE(ret);
409 }
410 
411 /**
412  * @tc.name: FileUtilOhosTest013
413  * @tc.desc: Test RenameFile defined in namespace FileUtil
414  * @tc.type: FUNC
415  * @tc.require: issueI65DUW
416  */
417 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest013, testing::ext::TestSize.Level3)
418 {
419     std::string caseName("FileUtilOhosTest013");
420     auto ret = FileUtil::RenameFile(GenerateLogFileName(caseName, SUFFIX_0),
421         GenerateLogFileName(caseName, SUFFIX_1));
422     ASSERT_TRUE(!ret);
423     (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_0), "line1");
424     (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_1), "line1");
425     (void)FileUtil::RenameFile(GenerateLogFileName(caseName, SUFFIX_0),
426         GenerateLogFileName(caseName, SUFFIX_1));
427     ASSERT_TRUE(true);
428 }
429 
430 HWTEST_F(AdapterUtilityOhosTest, AshMemoryUtilsOhosTest001, testing::ext::TestSize.Level3)
431 {
432     std::vector<AshMemTestStruct> dataIn = {
433         AshMemTestStruct("testData1"),
434         AshMemTestStruct("testData2"),
435         AshMemTestStruct("testData3"),
436         AshMemTestStruct("testData4"),
437     };
438     const uint32_t memSize = 256;
439     auto ashmem = AshMemoryUtils::GetAshmem("ashMemTest", memSize);
440     if (ashmem == nullptr) {
441         ASSERT_TRUE(false);
442     }
443     std::vector<uint32_t> allSize;
444     bool retIn = AshMemoryUtils::WriteBulkData<AshMemTestStruct>(dataIn, ashmem, memSize, allSize);
445     ASSERT_TRUE(retIn);
446     std::vector<AshMemTestStruct> dataOut;
447     bool retOut = AshMemoryUtils::ReadBulkData<AshMemTestStruct>(ashmem, allSize, dataOut);
448     ASSERT_TRUE(retOut);
449     ASSERT_TRUE(dataIn.size() == dataOut.size());
450     for (size_t i = 0; i < dataIn.size(); i++) {
451         ASSERT_EQ(dataIn[i].data, dataOut[i].data);
452     }
453 }
454 }
455 }