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 <gtest/gtest.h>
17
18 #include "file_path_utils.h"
19
20 using namespace testing;
21 using namespace testing::ext;
22
23 namespace OHOS {
24 namespace AbilityRuntime {
25 class FilePathUtilsTest : public testing::Test {
26 public:
27 static void SetUpTestCase();
28 static void TearDownTestCase();
29 void SetUp() override;
30 void TearDown() override;
31 };
32
SetUpTestCase()33 void FilePathUtilsTest::SetUpTestCase()
34 {}
35
TearDownTestCase()36 void FilePathUtilsTest::TearDownTestCase()
37 {}
38
SetUp()39 void FilePathUtilsTest::SetUp()
40 {}
41
TearDown()42 void FilePathUtilsTest::TearDown()
43 {}
44
45 /**
46 * @tc.name: StringStartWith_0100
47 * @tc.desc: StringStartWith Test
48 * @tc.type: FUNC
49 * @tc.require: issueI581SE
50 */
51 HWTEST_F(FilePathUtilsTest, StringStartWith_0100, TestSize.Level0)
52 {
53 std::string longStr = "abcde";
54 const char *shortStr = "abc";
55 size_t startStrLenInvalid1 = 20;
56 EXPECT_FALSE(StringStartWith(longStr, shortStr, startStrLenInvalid1));
57 size_t startStrLenInvalid2 = 0;
58 EXPECT_FALSE(StringStartWith(longStr, shortStr, startStrLenInvalid2));
59 size_t startStrLen = 3;
60 EXPECT_TRUE(StringStartWith(longStr, shortStr, startStrLen));
61 }
62
63 /**
64 * @tc.name: StringEndWith_0100
65 * @tc.desc: StringEndWith Test
66 * @tc.type: FUNC
67 * @tc.require: issueI581SE
68 */
69 HWTEST_F(FilePathUtilsTest, StringEndWith_0100, TestSize.Level0)
70 {
71 std::string longStr = "abcde";
72 const char *shortStr = "de";
73 size_t endStrLenInvalid1 = 20;
74 EXPECT_FALSE(StringEndWith(longStr, shortStr, endStrLenInvalid1));
75 size_t endStrLenInvalid2 = 0;
76 EXPECT_FALSE(StringEndWith(longStr, shortStr, endStrLenInvalid2));
77 size_t endStrLen = 2;
78 EXPECT_TRUE(StringEndWith(longStr, shortStr, endStrLen));
79 }
80
81 /**
82 * @tc.name: SplitString_0100
83 * @tc.desc: SplitString Test
84 * @tc.type: FUNC
85 * @tc.require: issueI581SE
86 */
87 HWTEST_F(FilePathUtilsTest, SplitString_0100, TestSize.Level0)
88 {
89 std::string longStr = "";
90 std::vector<std::string> strVector;
91 size_t pos = 0;
92 const char* seps = "a";
93 SplitString(longStr, strVector, pos, seps);
94 EXPECT_TRUE(strVector.size() == 0);
95 }
96
97 /**
98 * @tc.name: SplitString_0200
99 * @tc.desc: SplitString Test
100 * @tc.type: FUNC
101 * @tc.require: issueI581SE
102 */
103 HWTEST_F(FilePathUtilsTest, SplitString_0200, TestSize.Level0)
104 {
105 std::string longStr = "a";
106 std::vector<std::string> strVector;
107 size_t pos = 6;
108 const char* seps = "a";
109 SplitString(longStr, strVector, pos, seps);
110 EXPECT_TRUE(strVector.size() == 0);
111 }
112
113 /**
114 * @tc.name: SplitString_0300
115 * @tc.desc: SplitString Test
116 * @tc.type: FUNC
117 * @tc.require: issueI581SE
118 */
119 HWTEST_F(FilePathUtilsTest, SplitString_0300, TestSize.Level0)
120 {
121 std::string longStr = "abc:abc";
122 std::vector<std::string> strVector;
123 size_t pos = 0;
124 const char* seps = "|";
125 SplitString(longStr, strVector, pos, seps);
126 EXPECT_TRUE(strVector.size() == 1);
127 }
128
129 /**
130 * @tc.name: SplitString_0400
131 * @tc.desc: SplitString Test
132 * @tc.type: FUNC
133 * @tc.require: issueI581SE
134 */
135 HWTEST_F(FilePathUtilsTest, SplitString_0400, TestSize.Level0)
136 {
137 std::string longStr = "abc:abc";
138 std::vector<std::string> strVector;
139 size_t pos = 0;
140 const char* seps = ":";
141 SplitString(longStr, strVector, pos, seps);
142 EXPECT_TRUE(strVector.size() == 2);
143 }
144
145 /**
146 * @tc.name: JoinString_0100
147 * @tc.desc: JoinString Test
148 * @tc.type: FUNC
149 * @tc.require: issueI581SE
150 */
151 HWTEST_F(FilePathUtilsTest, JoinString_0100, TestSize.Level0)
152 {
153 std::vector<std::string> strVector{"a", "b", "c", "d", "e"};
154 char sep = ':';
155 size_t startIndex = 0;
156 std::string result = JoinString(strVector, sep, startIndex);
157 EXPECT_TRUE(result == "a:b:c:d:e");
158 }
159
160 /**
161 * @tc.name: JoinString_0200
162 * @tc.desc: JoinString Test
163 * @tc.type: FUNC
164 * @tc.require: issueI581SE
165 */
166 HWTEST_F(FilePathUtilsTest, JoinString_0200, TestSize.Level0)
167 {
168 std::vector<std::string> strVector{"a", "b", "c", "d", ""};
169 char sep = ':';
170 size_t startIndex = 0;
171 std::string result = JoinString(strVector, sep, startIndex);
172 EXPECT_TRUE(result == "a:b:c:d");
173 }
174
175 /**
176 * @tc.name: JoinString_0300
177 * @tc.desc: JoinString Test
178 * @tc.type: FUNC
179 * @tc.require: issueI581SE
180 */
181 HWTEST_F(FilePathUtilsTest, JoinString_0300, TestSize.Level0)
182 {
183 std::vector<std::string> strVector{""};
184 char sep = ':';
185 size_t startIndex = 0;
186 std::string result = JoinString(strVector, sep, startIndex);
187 EXPECT_TRUE(result == "");
188 }
189
190 /**
191 * @tc.name: StripString_0100
192 * @tc.desc: StripString Test
193 * @tc.type: FUNC
194 * @tc.require: issueI581SE
195 */
196 HWTEST_F(FilePathUtilsTest, StripString_0100, TestSize.Level0)
197 {
198 std::string str = "abc";
199 const char *charSet = "123";
200 std::string result = StripString(str, charSet);
201 EXPECT_TRUE(result == str);
202
203 std::string str1 = "123abc";
204 std::string result1 = StripString(str, charSet);
205 EXPECT_TRUE(result1 == str);
206 }
207
208 /**
209 * @tc.name: FixExtName_0100
210 * @tc.desc: FixExtName Test
211 * @tc.type: FUNC
212 * @tc.require: issueI581SE
213 */
214 HWTEST_F(FilePathUtilsTest, FixExtName_0100, TestSize.Level0)
215 {
216 std::string path = "";
217 FixExtName(path);
218 EXPECT_TRUE(path == "");
219
220 std::string path1 = "123.abc";
221 FixExtName(path1);
222 EXPECT_TRUE(path1 == "123.abc");
223
224 std::string path2 = "123.ets";
225 FixExtName(path2);
226 EXPECT_TRUE(path2 == "123.abc");
227
228 std::string path3 = "123.ts";
229 FixExtName(path3);
230 EXPECT_TRUE(path3 == "123.abc");
231
232 std::string path4 = "123.js";
233 FixExtName(path4);
234 EXPECT_TRUE(path4 == "123.abc");
235 }
236
237 /**
238 * @tc.name: GetInstallPath_0100
239 * @tc.desc: GetInstallPath Test
240 * @tc.type: FUNC
241 * @tc.require: issueI581SE
242 */
243 HWTEST_F(FilePathUtilsTest, GetInstallPath_0100, TestSize.Level0)
244 {
245 const std::string& curJsModulePath = "/data/storage/el1/bundle/curJsModulePath";
246 bool module = false;
247 std::string newJsModulePath = GetInstallPath(curJsModulePath, module);
248 EXPECT_EQ(newJsModulePath, "/data/storage/el1/bundle/");
249 }
250
251 /**
252 * @tc.name: GetInstallPath_0200
253 * @tc.desc: GetInstallPath Test
254 * @tc.type: FUNC
255 * @tc.require: issueI581SE
256 */
257 HWTEST_F(FilePathUtilsTest, GetInstallPath_0200, TestSize.Level0)
258 {
259 const std::string& curJsModulePath = "/data/bundle";
260 bool module = false;
261 std::string newJsModulePath = GetInstallPath(curJsModulePath, module);
262 EXPECT_EQ(newJsModulePath, std::string());
263 }
264
265 /**
266 * @tc.name: GetInstallPath_0300
267 * @tc.desc: GetInstallPath Test
268 * @tc.type: FUNC
269 * @tc.require: issueI581SE
270 */
271 HWTEST_F(FilePathUtilsTest, GetInstallPath_0300, TestSize.Level0)
272 {
273 const std::string& curJsModulePath = "/data/bundlescurJsModulePath";
274 bool module = false;
275 std::string newJsModulePath = GetInstallPath(curJsModulePath, module);
276 EXPECT_EQ(newJsModulePath, std::string());
277 }
278
279 /**
280 * @tc.name: GetInstallPath_0400
281 * @tc.desc: GetInstallPath Test
282 * @tc.type: FUNC
283 * @tc.require: issueI581SE
284 */
285 HWTEST_F(FilePathUtilsTest, GetInstallPath_0400, TestSize.Level0)
286 {
287 const std::string& curJsModulePath = "/data/bundles/curJsModulePath/module";
288 bool module = false;
289 std::string newJsModulePath = GetInstallPath(curJsModulePath, module);
290 EXPECT_EQ(newJsModulePath, "/data/bundles/curJsModulePath/");
291 }
292
293 /**
294 * @tc.name: GetInstallPath_0500
295 * @tc.desc: GetInstallPath Test
296 * @tc.type: FUNC
297 * @tc.require: issueI581SE
298 */
299 HWTEST_F(FilePathUtilsTest, GetInstallPath_0500, TestSize.Level0)
300 {
301 const std::string& curJsModulePath = "/data/bundles/curJsModulePath/module";
302 bool module = true;
303 std::string newJsModulePath = GetInstallPath(curJsModulePath, module);
304 EXPECT_EQ(newJsModulePath, std::string());
305 }
306
307 /**
308 * @tc.name: GetInstallPath_0600
309 * @tc.desc: GetInstallPath Test
310 * @tc.type: FUNC
311 * @tc.require: issueI581SE
312 */
313 HWTEST_F(FilePathUtilsTest, GetInstallPath_0600, TestSize.Level0)
314 {
315 const std::string& curJsModulePath = "/data/storage/el1/bundle/module/curJsModulePath";
316 bool module = true;
317 std::string newJsModulePath = GetInstallPath(curJsModulePath, module);
318 EXPECT_EQ(newJsModulePath, "/data/storage/el1/bundle/module/");
319 }
320
321 /**
322 * @tc.name: MakeNewJsModulePath_0100
323 * @tc.desc: MakeNewJsModulePath Test
324 * @tc.type: FUNC
325 * @tc.require: issueI581SE
326 */
327 HWTEST_F(FilePathUtilsTest, MakeNewJsModulePath_0100, TestSize.Level0)
328 {
329 const std::string& curJsModulePath = "/data/bundles/curJsModulePath/module";
330 const std::string& newJsModuleUri = "";
331 std::string newJsModulePath = MakeNewJsModulePath(curJsModulePath, newJsModuleUri);
332 EXPECT_EQ(newJsModulePath, std::string());
333 }
334
335 /**
336 * @tc.name: MakeNewJsModulePath_0200
337 * @tc.desc: MakeNewJsModulePath Test
338 * @tc.type: FUNC
339 * @tc.require: issueI581SE
340 */
341 HWTEST_F(FilePathUtilsTest, MakeNewJsModulePath_0200, TestSize.Level0)
342 {
343 const std::string& curJsModulePath = "/data/storage/el1/bundle/module/";
344 const std::string& newJsModuleUri = "";
345 std::string newJsModulePath = MakeNewJsModulePath(curJsModulePath, newJsModuleUri);
346 EXPECT_EQ(newJsModulePath, std::string());
347 }
348
349 /**
350 * @tc.name: FindNpmPackageInPath_0100
351 * @tc.desc: FindNpmPackageInPath Test
352 * @tc.type: FUNC
353 * @tc.require: issueI581SE
354 */
355 HWTEST_F(FilePathUtilsTest, FindNpmPackageInPath_0100, TestSize.Level0)
356 {
357 std::string lengthPath(PATH_MAX, 'a');
358 const std::string& npmPath = lengthPath;
359 std::string newJsModulePath = FindNpmPackageInPath(npmPath);
360 EXPECT_EQ(newJsModulePath, std::string());
361 }
362
363 /**
364 * @tc.name: FindNpmPackageInPath_0200
365 * @tc.desc: FindNpmPackageInPath Test
366 * @tc.type: FUNC
367 * @tc.require: issueI581SE
368 */
369 HWTEST_F(FilePathUtilsTest, FindNpmPackageInPath_0200, TestSize.Level0)
370 {
371 const std::string& npmPath = "npmPath";
372 std::string newJsModulePath = FindNpmPackageInPath(npmPath);
373 EXPECT_EQ(newJsModulePath, std::string());
374 }
375
376 /**
377 * @tc.name: FindNpmPackageInTopLevel_0100
378 * @tc.desc: FindNpmPackageInTopLevel Test
379 * @tc.type: FUNC
380 * @tc.require: issueI581SE
381 */
382 HWTEST_F(FilePathUtilsTest, FindNpmPackageInTopLevel_0100, TestSize.Level0)
383 {
384 const std::string& moduleInstallPath = "";
385 const std::string& npmPackage = "";
386 size_t start = 2;
387 std::string newJsModulePath = FindNpmPackageInTopLevel(moduleInstallPath, npmPackage, start);
388 EXPECT_EQ(newJsModulePath, std::string());
389 }
390
391 /**
392 * @tc.name: ParseOhmUri_0100
393 * @tc.desc: ParseOhmUri Test
394 * @tc.type: FUNC
395 * @tc.require: issueI581SE
396 */
397 HWTEST_F(FilePathUtilsTest, ParseOhmUri_0100, TestSize.Level0)
398 {
399 const std::string& originBundleName = "";
400 const std::string& curJsModulePath = "";
401 const std::string& newJsModuleUri = "@bundle:originBundleName\bundleName";
402 std::string newJsModulePath = ParseOhmUri(originBundleName, curJsModulePath, newJsModuleUri);
403 EXPECT_EQ(newJsModulePath, std::string());
404 }
405
406 /**
407 * @tc.name: ParseOhmUri_0200
408 * @tc.desc: ParseOhmUri Test
409 * @tc.type: FUNC
410 * @tc.require: issueI581SE
411 */
412 HWTEST_F(FilePathUtilsTest, ParseOhmUri_0200, TestSize.Level0)
413 {
414 const std::string& originBundleName = "bundleName1";
415 const std::string& curJsModulePath = "/data/storage/el1/bundle/curJsModulePath";
416 const std::string& newJsModuleUri = "@bundle:originBundleName/bundleName1/bundleName2/bundleName3/bundleName4";
417 std::string newJsModulePath = ParseOhmUri(originBundleName, curJsModulePath, newJsModuleUri);
418 EXPECT_EQ(newJsModulePath, "/data/bundles/originBundleName/bundleName1/bundleName2/bundleName3/bundleName4");
419 }
420
421 /**
422 * @tc.name: ParseOhmUri_0300
423 * @tc.desc: ParseOhmUri Test
424 * @tc.type: FUNC
425 * @tc.require: issueI581SE
426 */
427 HWTEST_F(FilePathUtilsTest, ParseOhmUri_0300, TestSize.Level0)
428 {
429 const std::string& originBundleName = "";
430 const std::string& curJsModulePath = "";
431 const std::string& newJsModuleUri = "@module:originBundleName\bundleName";
432 std::string newJsModulePath = ParseOhmUri(originBundleName, curJsModulePath, newJsModuleUri);
433 EXPECT_EQ(newJsModulePath, std::string());
434 }
435
436 /**
437 * @tc.name: ParseOhmUri_0400
438 * @tc.desc: ParseOhmUri Test
439 * @tc.type: FUNC
440 * @tc.require: issueI581SE
441 */
442 HWTEST_F(FilePathUtilsTest, ParseOhmUri_0400, TestSize.Level0)
443 {
444 const std::string& originBundleName = "";
445 const std::string& curJsModulePath = "";
446 const std::string& newJsModuleUri = "@module:originBundleName\bundleName1\bundleName2";
447 std::string newJsModulePath = ParseOhmUri(originBundleName, curJsModulePath, newJsModuleUri);
448 EXPECT_EQ(newJsModulePath, std::string());
449 }
450
451 /**
452 * @tc.name: ParseOhmUri_0500
453 * @tc.desc: ParseOhmUri Test
454 * @tc.type: FUNC
455 * @tc.require: issueI581SE
456 */
457 HWTEST_F(FilePathUtilsTest, ParseOhmUri_0500, TestSize.Level0)
458 {
459 const std::string& originBundleName = "";
460 const std::string& curJsModulePath = "/data/storage/el1/bundle/module/curJsModulePath";
461 const std::string& newJsModuleUri = "@module:originBundleName/bundleName1/bundleName2/bundleName3";
462 std::string newJsModulePath = ParseOhmUri(originBundleName, curJsModulePath, newJsModuleUri);
463 EXPECT_EQ(newJsModulePath, "/data/storage/el1/bundle/originBundleName/bundleName1/bundleName2/bundleName3");
464 }
465
466 /**
467 * @tc.name: ParseOhmUri_0600
468 * @tc.desc: ParseOhmUri Test
469 * @tc.type: FUNC
470 * @tc.require: issueI581SE
471 */
472 HWTEST_F(FilePathUtilsTest, ParseOhmUri_0600, TestSize.Level0)
473 {
474 const std::string& originBundleName = "";
475 const std::string& curJsModulePath = "";
476 const std::string& newJsModuleUri = "@local:originBundleName";
477 std::string newJsModulePath = ParseOhmUri(originBundleName, curJsModulePath, newJsModuleUri);
478 EXPECT_EQ(newJsModulePath, std::string());
479 }
480
481 /**
482 * @tc.name: ParseOhmUri_0700
483 * @tc.desc: ParseOhmUri Test
484 * @tc.type: FUNC
485 * @tc.require: issueI581SE
486 */
487 HWTEST_F(FilePathUtilsTest, ParseOhmUri_0700, TestSize.Level0)
488 {
489 const std::string& originBundleName = "";
490 const std::string& curJsModulePath = "/data/bundles/curJsModulePath/module";
491 const std::string& newJsModuleUri = "@local:originBundleName";
492 std::string newJsModulePath = ParseOhmUri(originBundleName, curJsModulePath, newJsModuleUri);
493 EXPECT_EQ(newJsModulePath, std::string());
494 }
495
496 /**
497 * @tc.name: ParseOhmUri_0800
498 * @tc.desc: ParseOhmUri Test
499 * @tc.type: FUNC
500 * @tc.require: issueI581SE
501 */
502 HWTEST_F(FilePathUtilsTest, ParseOhmUri_0800, TestSize.Level0)
503 {
504 const std::string& originBundleName = "";
505 const std::string& curJsModulePath = "";
506 const std::string& newJsModuleUri = "@other:originBundleName\bundleName";
507 std::string newJsModulePath = ParseOhmUri(originBundleName, curJsModulePath, newJsModuleUri);
508 EXPECT_EQ(newJsModulePath, std::string());
509 }
510
511 /**
512 * @tc.name: NormalizeUri_0100
513 * @tc.desc: NormalizeUri Test
514 * @tc.type: FUNC
515 * @tc.require: issueI581SE
516 */
517 HWTEST_F(FilePathUtilsTest, NormalizeUri_0100, TestSize.Level0)
518 {
519 const std::string& bundleName = "";
520 const std::string& curJsModulePath = "";
521 const std::string& newJsModuleUri = "";
522 std::string newJsModulePath = NormalizeUri(bundleName, curJsModulePath, newJsModuleUri);
523 EXPECT_EQ(newJsModulePath, "");
524 }
525
526 /**
527 * @tc.name: NormalizeUri_0200
528 * @tc.desc: NormalizeUri Test
529 * @tc.type: FUNC
530 * @tc.require: issueI581SE
531 */
532 HWTEST_F(FilePathUtilsTest, NormalizeUri_0200, TestSize.Level0)
533 {
534 const std::string& bundleName = "";
535 const std::string& curJsModulePath = "";
536 const std::string& newJsModuleUri = "a";
537 std::string newJsModulePath = NormalizeUri(bundleName, curJsModulePath, newJsModuleUri);
538 EXPECT_EQ(newJsModulePath, "");
539 }
540
541 /**
542 * @tc.name: NormalizeUri_0300
543 * @tc.desc: NormalizeUri Test
544 * @tc.type: FUNC
545 * @tc.require: issueI581SE
546 */
547 HWTEST_F(FilePathUtilsTest, NormalizeUri_0300, TestSize.Level0)
548 {
549 const std::string& bundleName = "";
550 const std::string& curJsModulePath = "a";
551 const std::string& newJsModuleUri = "";
552 std::string newJsModulePath = NormalizeUri(bundleName, curJsModulePath, newJsModuleUri);
553 EXPECT_EQ(newJsModulePath, "");
554 }
555
556 /**
557 * @tc.name: MakeFilePath_0100
558 * @tc.desc: MakeFilePath Test
559 * @tc.type: FUNC
560 * @tc.require: issueI581SE
561 */
562 HWTEST_F(FilePathUtilsTest, MakeFilePath_0100, TestSize.Level0)
563 {
564 std::string bundleName(PATH_MAX, 'a');
565 const std::string& codePath = bundleName;
566 const std::string& modulePath = "";
567 std::string fileName = "";
568 bool newJsModulePath = MakeFilePath(codePath, modulePath, fileName);
569 EXPECT_FALSE(newJsModulePath);
570 }
571
572 /**
573 * @tc.name: MakeFilePath_0200
574 * @tc.desc: MakeFilePath Test
575 * @tc.type: FUNC
576 * @tc.require: issueI581SE
577 */
578 HWTEST_F(FilePathUtilsTest, MakeFilePath_0200, TestSize.Level0)
579 {
580 const std::string& codePath = "codePath";
581 const std::string& modulePath = "";
582 std::string fileName = "";
583 bool newJsModulePath = MakeFilePath(codePath, modulePath, fileName);
584 EXPECT_FALSE(newJsModulePath);
585 }
586
587 /**
588 * @tc.name: MakeFilePath_0300
589 * @tc.desc: MakeFilePath Test
590 * @tc.type: FUNC
591 * @tc.require: issueI581SE
592 */
593 HWTEST_F(FilePathUtilsTest, MakeFilePath_0300, TestSize.Level0)
594 {
595 const std::string& codePath = "../codePath";
596 const std::string& modulePath = "";
597 std::string fileName = "";
598 bool newJsModulePath = MakeFilePath(codePath, modulePath, fileName);
599 EXPECT_FALSE(newJsModulePath);
600 }
601 } // namespace AbilityRuntime
602 } // namespace OHOS
603