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