• 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 <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: ParseOhmUri_0100
394  * @tc.desc: ParseOhmUri Test
395  * @tc.type: FUNC
396  * @tc.require: issueI581SE
397  */
398 HWTEST_F(FilePathUtilsTest, ParseOhmUri_0100, TestSize.Level0)
399 {
400     const std::string& originBundleName = "";
401     const std::string& curJsModulePath = "";
402     const std::string& newJsModuleUri = "@bundle:originBundleName\bundleName";
403     std::string newJsModulePath = ParseOhmUri(originBundleName, curJsModulePath, newJsModuleUri);
404     EXPECT_EQ(newJsModulePath, std::string());
405 }
406 
407 /**
408  * @tc.name: ParseOhmUri_0200
409  * @tc.desc: ParseOhmUri Test
410  * @tc.type: FUNC
411  * @tc.require: issueI581SE
412  */
413 HWTEST_F(FilePathUtilsTest, ParseOhmUri_0200, TestSize.Level0)
414 {
415     const std::string& originBundleName = "bundleName1";
416     const std::string& curJsModulePath = "/data/storage/el1/bundle/curJsModulePath";
417     const std::string& newJsModuleUri = "@bundle:originBundleName/bundleName1/bundleName2/bundleName3/bundleName4";
418     std::string newJsModulePath = ParseOhmUri(originBundleName, curJsModulePath, newJsModuleUri);
419     EXPECT_EQ(newJsModulePath, "/data/bundles/originBundleName/bundleName1/bundleName2/bundleName3/bundleName4");
420 }
421 
422 /**
423  * @tc.name: ParseOhmUri_0300
424  * @tc.desc: ParseOhmUri Test
425  * @tc.type: FUNC
426  * @tc.require: issueI581SE
427  */
428 HWTEST_F(FilePathUtilsTest, ParseOhmUri_0300, TestSize.Level0)
429 {
430     const std::string& originBundleName = "";
431     const std::string& curJsModulePath = "";
432     const std::string& newJsModuleUri = "@module:originBundleName\bundleName";
433     std::string newJsModulePath = ParseOhmUri(originBundleName, curJsModulePath, newJsModuleUri);
434     EXPECT_EQ(newJsModulePath, std::string());
435 }
436 
437 /**
438  * @tc.name: ParseOhmUri_0400
439  * @tc.desc: ParseOhmUri Test
440  * @tc.type: FUNC
441  * @tc.require: issueI581SE
442  */
443 HWTEST_F(FilePathUtilsTest, ParseOhmUri_0400, TestSize.Level0)
444 {
445     const std::string& originBundleName = "";
446     const std::string& curJsModulePath = "";
447     const std::string& newJsModuleUri = "@module:originBundleName\bundleName1\bundleName2";
448     std::string newJsModulePath = ParseOhmUri(originBundleName, curJsModulePath, newJsModuleUri);
449     EXPECT_EQ(newJsModulePath, std::string());
450 }
451 
452 /**
453  * @tc.name: ParseOhmUri_0500
454  * @tc.desc: ParseOhmUri Test
455  * @tc.type: FUNC
456  * @tc.require: issueI581SE
457  */
458 HWTEST_F(FilePathUtilsTest, ParseOhmUri_0500, TestSize.Level0)
459 {
460     const std::string& originBundleName = "";
461     const std::string& curJsModulePath = "/data/storage/el1/bundle/module/curJsModulePath";
462     const std::string& newJsModuleUri = "@module:originBundleName/bundleName1/bundleName2/bundleName3";
463     std::string newJsModulePath = ParseOhmUri(originBundleName, curJsModulePath, newJsModuleUri);
464     EXPECT_EQ(newJsModulePath, "/data/storage/el1/bundle/originBundleName/bundleName1/bundleName2/bundleName3");
465 }
466 
467 /**
468  * @tc.name: ParseOhmUri_0600
469  * @tc.desc: ParseOhmUri Test
470  * @tc.type: FUNC
471  * @tc.require: issueI581SE
472  */
473 HWTEST_F(FilePathUtilsTest, ParseOhmUri_0600, TestSize.Level0)
474 {
475     const std::string& originBundleName = "";
476     const std::string& curJsModulePath = "";
477     const std::string& newJsModuleUri = "@local:originBundleName";
478     std::string newJsModulePath = ParseOhmUri(originBundleName, curJsModulePath, newJsModuleUri);
479     EXPECT_EQ(newJsModulePath, std::string());
480 }
481 
482 /**
483  * @tc.name: ParseOhmUri_0700
484  * @tc.desc: ParseOhmUri Test
485  * @tc.type: FUNC
486  * @tc.require: issueI581SE
487  */
488 HWTEST_F(FilePathUtilsTest, ParseOhmUri_0700, TestSize.Level0)
489 {
490     const std::string& originBundleName = "";
491     const std::string& curJsModulePath = "/data/bundles/curJsModulePath/module";
492     const std::string& newJsModuleUri = "@local:originBundleName";
493     std::string newJsModulePath = ParseOhmUri(originBundleName, curJsModulePath, newJsModuleUri);
494     EXPECT_EQ(newJsModulePath, std::string());
495 }
496 
497 /**
498  * @tc.name: ParseOhmUri_0800
499  * @tc.desc: ParseOhmUri Test
500  * @tc.type: FUNC
501  * @tc.require: issueI581SE
502  */
503 HWTEST_F(FilePathUtilsTest, ParseOhmUri_0800, TestSize.Level0)
504 {
505     const std::string& originBundleName = "";
506     const std::string& curJsModulePath = "";
507     const std::string& newJsModuleUri = "@other:originBundleName\bundleName";
508     std::string newJsModulePath = ParseOhmUri(originBundleName, curJsModulePath, newJsModuleUri);
509     EXPECT_EQ(newJsModulePath, std::string());
510 }
511 
512 /**
513  * @tc.name: NormalizeUri_0100
514  * @tc.desc: NormalizeUri Test
515  * @tc.type: FUNC
516  * @tc.require: issueI581SE
517  */
518 HWTEST_F(FilePathUtilsTest, NormalizeUri_0100, TestSize.Level0)
519 {
520     const std::string& bundleName = "";
521     const std::string& curJsModulePath = "";
522     const std::string& newJsModuleUri = "";
523     std::string newJsModulePath = NormalizeUri(bundleName, curJsModulePath, newJsModuleUri);
524     EXPECT_EQ(newJsModulePath, "");
525 }
526 
527 /**
528  * @tc.name: NormalizeUri_0200
529  * @tc.desc: NormalizeUri Test
530  * @tc.type: FUNC
531  * @tc.require: issueI581SE
532  */
533 HWTEST_F(FilePathUtilsTest, NormalizeUri_0200, TestSize.Level0)
534 {
535     const std::string& bundleName = "";
536     const std::string& curJsModulePath = "";
537     const std::string& newJsModuleUri = "a";
538     std::string newJsModulePath = NormalizeUri(bundleName, curJsModulePath, newJsModuleUri);
539     EXPECT_EQ(newJsModulePath, "");
540 }
541 
542 /**
543  * @tc.name: NormalizeUri_0300
544  * @tc.desc: NormalizeUri Test
545  * @tc.type: FUNC
546  * @tc.require: issueI581SE
547  */
548 HWTEST_F(FilePathUtilsTest, NormalizeUri_0300, TestSize.Level0)
549 {
550     const std::string& bundleName = "";
551     const std::string& curJsModulePath = "a";
552     const std::string& newJsModuleUri = "";
553     std::string newJsModulePath = NormalizeUri(bundleName, curJsModulePath, newJsModuleUri);
554     EXPECT_EQ(newJsModulePath, "");
555 }
556 
557 /**
558  * @tc.name: MakeFilePath_0100
559  * @tc.desc: MakeFilePath Test
560  * @tc.type: FUNC
561  * @tc.require: issueI581SE
562  */
563 HWTEST_F(FilePathUtilsTest, MakeFilePath_0100, TestSize.Level0)
564 {
565     std::string bundleName(PATH_MAX, 'a');
566     const std::string& codePath = bundleName;
567     const std::string& modulePath = "";
568     std::string fileName = "";
569     bool newJsModulePath = MakeFilePath(codePath, modulePath, fileName);
570     EXPECT_FALSE(newJsModulePath);
571 }
572 
573 /**
574  * @tc.name: MakeFilePath_0200
575  * @tc.desc: MakeFilePath Test
576  * @tc.type: FUNC
577  * @tc.require: issueI581SE
578  */
579 HWTEST_F(FilePathUtilsTest, MakeFilePath_0200, TestSize.Level0)
580 {
581     const std::string& codePath = "codePath";
582     const std::string& modulePath = "";
583     std::string fileName = "";
584     bool newJsModulePath = MakeFilePath(codePath, modulePath, fileName);
585     EXPECT_FALSE(newJsModulePath);
586 }
587 
588 /**
589  * @tc.name: MakeFilePath_0300
590  * @tc.desc: MakeFilePath Test
591  * @tc.type: FUNC
592  * @tc.require: issueI581SE
593  */
594 HWTEST_F(FilePathUtilsTest, MakeFilePath_0300, TestSize.Level0)
595 {
596     const std::string& codePath = "../codePath";
597     const std::string& modulePath = "";
598     std::string fileName = "";
599     bool newJsModulePath = MakeFilePath(codePath, modulePath, fileName);
600     EXPECT_FALSE(newJsModulePath);
601 }
602 }  // namespace AbilityRuntime
603 }  // namespace OHOS
604