• 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 <gtest/gtest.h>
17 #include "file_ex.h"
18 #include <algorithm>
19 #include <iostream>
20 #include <fstream>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 
25 using namespace testing::ext;
26 using namespace OHOS;
27 using namespace std;
28 
29 
30 class UtilsFileTest : public testing::Test {
31 public :
32     static void SetUpTestCase(void);
33     static void TearDownTestCase(void);
34     void SetUp();
35     void TearDown();
36 };
37 
SetUpTestCase(void)38 void UtilsFileTest::SetUpTestCase(void)
39 {
40 }
41 
TearDownTestCase(void)42 void UtilsFileTest::TearDownTestCase(void)
43 {
44 }
45 
SetUp(void)46 void UtilsFileTest::SetUp(void)
47 {
48 }
49 
TearDown(void)50 void UtilsFileTest::TearDown(void)
51 {
52 }
53 
CreateTestFile(const std::string & path,const std::string & content)54 bool CreateTestFile(const std::string& path, const std::string& content)
55 {
56     ofstream out(path, ios_base::out | ios_base::trunc);
57     if (out.is_open()) {
58         out << content;
59         return true;
60     }
61 
62     std::cout << "open file failed!" << path << std::endl;
63     return false;
64 }
65 
RemoveTestFile(const std::string & path)66 int RemoveTestFile(const std::string& path)
67 {
68     return unlink(path.c_str());
69 }
70 
71 /*
72  * @tc.name: testLoadStringFromFile001
73  * @tc.desc: singleton template
74  */
75 HWTEST_F(UtilsFileTest, testLoadStringFromFile001, TestSize.Level0)
76 {
77     string str;
78     string filename = "/proc/meminfo";
79     EXPECT_TRUE(LoadStringFromFile(filename, str));
80 
81     string str2;
82     int fd = open(filename.c_str(), O_RDONLY);
83     EXPECT_TRUE(LoadStringFromFd(fd, str2));
84     close(fd);
85     EXPECT_EQ(str.size(), str2.size());
86 
87     vector<char> buff;
88     bool ret = LoadBufferFromFile(filename, buff);
89     EXPECT_TRUE(ret);
90     EXPECT_EQ(str2.size(), buff.size());
91 }
92 
93 /*
94  * @tc.name: testLoadStringFromFile002
95  * @tc.desc: singleton template
96  */
97 HWTEST_F(UtilsFileTest, testLoadStringFromFile002, TestSize.Level0)
98 {
99     string str;
100     string filename = "";
101     EXPECT_FALSE(LoadStringFromFile(filename, str));
102     EXPECT_TRUE(str.empty());
103 }
104 
105 /*
106  * @tc.name: testLoadStringFromFile003
107  * @tc.desc: singleton template
108  */
109 HWTEST_F(UtilsFileTest, testLoadStringFromFile003, TestSize.Level0)
110 {
111     string str;
112     string filename = "./tmp.txt";
113     string content = "";
114     CreateTestFile(filename, content);
115     EXPECT_TRUE(LoadStringFromFile(filename, str));
116     RemoveTestFile(filename);
117     EXPECT_TRUE(str == content);
118 }
119 
120 /*
121  * @tc.name: testLoadStringFromFile004
122  * @tc.desc: singleton template
123  */
124 HWTEST_F(UtilsFileTest, testLoadStringFromFile004, TestSize.Level0)
125 {
126     string str;
127     string filename = "./tmp.txt";
128     string content = "TTTTT";
129     CreateTestFile(filename, content);
130     EXPECT_TRUE(LoadStringFromFile(filename, str));
131     RemoveTestFile(filename);
132     EXPECT_TRUE(str == content);
133 }
134 
135 /*
136  * @tc.name: testLoadStringFromFile005
137  * @tc.desc: singleton template
138  */
139 HWTEST_F(UtilsFileTest, testLoadStringFromFile005, TestSize.Level0)
140 {
141     string str;
142     string filename = "./tmp.txt";
143     string content(32*1024*1024, 't');
144     CreateTestFile(filename, content);
145     EXPECT_TRUE(LoadStringFromFile(filename, str));
146     RemoveTestFile(filename);
147     EXPECT_TRUE(str == content);
148 }
149 
150 /*
151  * @tc.name: testLoadStringFromFile006
152  * @tc.desc: singleton template
153  */
154 HWTEST_F(UtilsFileTest, testLoadStringFromFile006, TestSize.Level0)
155 {
156     string str;
157     string filename = "./tmp.txt";
158     string content(32*1024*1024 + 1, 't');
159     CreateTestFile(filename, content);
160     EXPECT_FALSE(LoadStringFromFile(filename, str));
161     RemoveTestFile(filename);
162     EXPECT_TRUE(str.empty());
163 }
164 
165 /*
166  * @tc.name: testLoadStringFromFd001
167  * @tc.desc: singleton template
168  */
169 HWTEST_F(UtilsFileTest, testLoadStringFromFd001, TestSize.Level0)
170 {
171     string result;
172     string filename = "";
173     EXPECT_FALSE(LoadStringFromFd(-1, result));
174     EXPECT_EQ(result, "");
175 }
176 
177 /*
178  * @tc.name: testLoadStringFromFd002
179  * @tc.desc: singleton template
180  */
181 HWTEST_F(UtilsFileTest, testLoadStringFromFd002, TestSize.Level0)
182 {
183     string result;
184     string filename = "./tmp.txt";
185     string content = "";
186     CreateTestFile(filename, content);
187     int fd = open(filename.c_str(), O_RDONLY);
188     EXPECT_TRUE(LoadStringFromFd(fd, result));
189     close(fd);
190     RemoveTestFile(filename);
191     EXPECT_TRUE(result == content);
192 }
193 
194 /*
195  * @tc.name: testLoadStringFromFd003
196  * @tc.desc: singleton template
197  */
198 HWTEST_F(UtilsFileTest, testLoadStringFromFd003, TestSize.Level0)
199 {
200     string result;
201     string filename = "./tmp.txt";
202     string content = "TTTTT";
203     CreateTestFile(filename, content);
204     int fd = open(filename.c_str(), O_RDONLY);
205     EXPECT_TRUE(LoadStringFromFd(fd, result));
206     close(fd);
207     RemoveTestFile(filename);
208     EXPECT_TRUE(result == content);
209 }
210 
211 /*
212  * @tc.name: testLoadStringFromFd004
213  * @tc.desc: singleton template
214  */
215 HWTEST_F(UtilsFileTest, testLoadStringFromFd004, TestSize.Level0)
216 {
217     string result;
218     string filename = "./tmp.txt";
219     string content(32*1024*1024, 't');
220     CreateTestFile(filename, content);
221     int fd = open(filename.c_str(), O_RDONLY);
222     EXPECT_TRUE(LoadStringFromFd(fd, result));
223     close(fd);
224     RemoveTestFile(filename);
225     EXPECT_TRUE(result == content);
226 }
227 
228 /*
229  * @tc.name: testSaveStringToFile001
230  * @tc.desc: singleton template
231  */
232 HWTEST_F(UtilsFileTest, testSaveStringToFile001, TestSize.Level0)
233 {
234     string content;
235     string path = "./tmp2.txt";
236     string fileContent = "ttxx";
237     CreateTestFile(path, fileContent);
238     bool ret = SaveStringToFile(path, content);
239     EXPECT_EQ(ret, true);
240 
241     string loadResult;
242     EXPECT_TRUE(LoadStringFromFile(path, loadResult));
243     RemoveTestFile(path);
244     EXPECT_EQ(loadResult, fileContent);
245 }
246 
247 /*
248  * @tc.name: testSaveStringToFile002
249  * @tc.desc: singleton template
250  */
251 HWTEST_F(UtilsFileTest, testSaveStringToFile002, TestSize.Level0)
252 {
253     string path = "./tmp2.txt";
254     string fileContent = "ttxx";
255     CreateTestFile(path, fileContent);
256 
257     string content = "xxtt";
258     EXPECT_TRUE(SaveStringToFile(path, content));
259 
260     string loadResult;
261     EXPECT_TRUE(LoadStringFromFile(path, loadResult));
262     RemoveTestFile(path);
263     EXPECT_EQ(loadResult, content);
264 }
265 
266 /*
267  * @tc.name: testSaveStringToFile003
268  * @tc.desc: singleton template
269  */
270 HWTEST_F(UtilsFileTest, testSaveStringToFile003, TestSize.Level0)
271 {
272     string path = "./tmp2.txt";
273     string fileContent = "ttxx";
274     CreateTestFile(path, fileContent);
275 
276     string content = "xxtt";
277     bool ret = SaveStringToFile(path, content, true);
278     EXPECT_EQ(ret, true);
279 
280     string loadResult;
281     ret = LoadStringFromFile(path, loadResult);
282     RemoveTestFile(path);
283     EXPECT_EQ(ret, true);
284     EXPECT_STREQ(loadResult.c_str(), content.c_str());
285 }
286 
287 /*
288  * @tc.name: testSaveStringToFile004
289  * @tc.desc: singleton template
290  */
291 HWTEST_F(UtilsFileTest, testSaveStringToFile004, TestSize.Level0)
292 {
293     string path = "./tmp2.txt";
294     string fileContent = "ttxx";
295     CreateTestFile(path, fileContent);
296 
297     string content = "xxtt";
298     bool ret = SaveStringToFile(path, content, true);
299     EXPECT_EQ(ret, true);
300 
301     string loadResult;
302     ret = LoadStringFromFile(path, loadResult);
303     RemoveTestFile(path);
304     EXPECT_EQ(ret, true);
305     EXPECT_STREQ(loadResult.c_str(), content.c_str());
306 }
307 
308 /*
309  * @tc.name: testSaveStringToFile005
310  * @tc.desc: singleton template
311  */
312 HWTEST_F(UtilsFileTest, testSaveStringToFile005, TestSize.Level0)
313 {
314     string path = "./tmp2.txt";
315     string fileContent = "ttxx";
316     CreateTestFile(path, fileContent);
317 
318     string content;
319     bool ret = SaveStringToFile(path, content, true);
320     EXPECT_EQ(ret, true);
321 
322     string loadResult;
323     ret = LoadStringFromFile(path, loadResult);
324     RemoveTestFile(path);
325     EXPECT_EQ(ret, true);
326     EXPECT_STREQ(loadResult.c_str(), fileContent.c_str());
327 }
328 
329 /*
330  * @tc.name: testSaveStringToFile006
331  * @tc.desc: singleton template
332  */
333 HWTEST_F(UtilsFileTest, testSaveStringToFile006, TestSize.Level0)
334 {
335     string content;
336     string path = "./tmp2.txt";
337     string fileContent = "ttxx";
338     CreateTestFile(path, fileContent);
339     bool ret = SaveStringToFile(path, content, false);
340     EXPECT_EQ(ret, true);
341 
342     string loadResult;
343     ret = LoadStringFromFile(path, loadResult);
344     RemoveTestFile(path);
345     EXPECT_EQ(ret, true);
346     EXPECT_STREQ(loadResult.c_str(), fileContent.c_str());
347 }
348 
349 /*
350  * @tc.name: testSaveStringToFile007
351  * @tc.desc: singleton template
352  */
353 HWTEST_F(UtilsFileTest, testSaveStringToFile007, TestSize.Level0)
354 {
355     string path = "./tmp2.txt";
356     string fileContent = "ttxx";
357     CreateTestFile(path, fileContent);
358 
359     string content = "xxtt";
360     bool ret = SaveStringToFile(path, content, false);
361     EXPECT_EQ(ret, true);
362 
363     string loadResult;
364     ret = LoadStringFromFile(path, loadResult);
365     RemoveTestFile(path);
366     EXPECT_EQ(ret, true);
367     EXPECT_EQ(loadResult, fileContent + content);
368 }
369 
370 /*
371  * @tc.name: testSaveStringToFd001
372  * @tc.desc: singleton template
373  */
374 HWTEST_F(UtilsFileTest, testSaveStringToFd001, TestSize.Level0)
375 {
376     string content;
377     bool ret = SaveStringToFd(-1, content);
378     EXPECT_EQ(ret, false);
379 }
380 
381 /*
382  * @tc.name: testSaveStringToFd002
383  * @tc.desc: singleton template
384  */
385 HWTEST_F(UtilsFileTest, testSaveStringToFd002, TestSize.Level0)
386 {
387     string content;
388     bool ret = SaveStringToFd(0, content);
389     EXPECT_EQ(ret, false);
390 }
391 
392 /*
393  * @tc.name: testSaveStringToFd003
394  * @tc.desc: singleton template
395  */
396 HWTEST_F(UtilsFileTest, testSaveStringToFd003, TestSize.Level0)
397 {
398     string content = "tt";
399     bool ret = SaveStringToFd(-1, content);
400     EXPECT_EQ(ret, false);
401 }
402 
403 /*
404  * @tc.name: testSaveStringToFd004
405  * @tc.desc: singleton template
406  */
407 HWTEST_F(UtilsFileTest, testSaveStringToFd004, TestSize.Level0)
408 {
409     string content;
410     bool ret = SaveStringToFd(-1, content);
411     EXPECT_EQ(ret, false);
412 }
413 
414 /*
415  * @tc.name: testSaveStringToFd005
416  * @tc.desc: singleton template
417  */
418 HWTEST_F(UtilsFileTest, testSaveStringToFd005, TestSize.Level0)
419 {
420     string content;
421     string filename = "./tmp3.txt";
422     int fd = open(filename.c_str(), O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
423     bool ret = SaveStringToFd(fd, content);
424     close(fd);
425     EXPECT_EQ(ret, true);
426 
427     string loadResult;
428     fd = open(filename.c_str(), O_RDONLY);
429     ret = LoadStringFromFd(fd, loadResult);
430     close(fd);
431     RemoveTestFile(filename);
432     EXPECT_EQ(ret, true);
433     EXPECT_EQ(loadResult, "");
434 }
435 
436 /*
437  * @tc.name: testSaveStringToFd006
438  * @tc.desc: singleton template
439  */
440 HWTEST_F(UtilsFileTest, testSaveStringToFd006, TestSize.Level0)
441 {
442     string content = "TTTTTTTT";
443     string filename = "./tmp3.txt";
444     int fd = open(filename.c_str(), O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
445     bool ret = SaveStringToFd(fd, content);
446     close(fd);
447     EXPECT_EQ(ret, true);
448 
449     string loadResult;
450     fd = open(filename.c_str(), O_RDONLY);
451     ret = LoadStringFromFd(fd, loadResult);
452     close(fd);
453     RemoveTestFile(filename);
454     EXPECT_EQ(ret, true);
455     EXPECT_EQ(loadResult, content);
456 }
457 
458 /*
459  * @tc.name: testLoadBufferFromFile001
460  * @tc.desc: singleton template
461  */
462 HWTEST_F(UtilsFileTest, testLoadBufferFromFile001, TestSize.Level0)
463 {
464     vector<char> buff;
465     string filename = "";
466     bool ret = LoadBufferFromFile(filename, buff);
467     EXPECT_FALSE(ret);
468     EXPECT_EQ(0, static_cast<int>(buff.size()));
469 }
470 
471 /*
472  * @tc.name: testLoadBufferFromFile002
473  * @tc.desc: singleton template
474  */
475 HWTEST_F(UtilsFileTest, testLoadBufferFromFile002, TestSize.Level0)
476 {
477     vector<char> buff;
478     string filename = "./tmp.txt";
479     string content = "TXB";
480     CreateTestFile(filename, content);
481     bool ret = LoadBufferFromFile(filename, buff);
482     RemoveTestFile(filename);
483     EXPECT_TRUE(ret);
484     EXPECT_EQ(3, (int)buff.size());
485     EXPECT_EQ('T', buff[0]);
486     EXPECT_EQ('X', buff[1]);
487     EXPECT_EQ('B', buff[2]);
488 }
489 
490 /*
491  * @tc.name: testLoadBufferFromFile003
492  * @tc.desc: singleton template
493  */
494 HWTEST_F(UtilsFileTest, testLoadBufferFromFile003, TestSize.Level0)
495 {
496     vector<char> buff;
497     string filename = "./tmp1.txt";
498     string content(32*1024*1024 + 1, 't');
499     CreateTestFile(filename, content);
500     bool ret = LoadBufferFromFile(filename, buff);
501     RemoveTestFile(filename);
502     EXPECT_EQ(ret, false);
503     EXPECT_EQ(0, static_cast<int>(buff.size()));
504 }
505 
506 /*
507  * @tc.name: testSaveBufferToFile001
508  * @tc.desc: singleton template
509  */
510 HWTEST_F(UtilsFileTest, testSaveBufferToFile001, TestSize.Level0)
511 {
512     vector<char> buff;
513     string path = "./tmp2.txt";
514     string fileContent = "ttxx";
515     CreateTestFile(path, fileContent);
516     bool ret = SaveBufferToFile(path, buff, false);
517     EXPECT_EQ(ret, true);
518 
519     string loadResult;
520     ret = LoadStringFromFile(path, loadResult);
521     RemoveTestFile(path);
522     EXPECT_EQ(ret, true);
523     EXPECT_EQ(loadResult, fileContent);
524 }
525 
526 /*
527  * @tc.name: testSaveBufferToFile002
528  * @tc.desc: singleton template
529  */
530 HWTEST_F(UtilsFileTest, testSaveBufferToFile002, TestSize.Level0)
531 {
532     string path = "./tmp2.txt";
533     string fileContent = "ttxx";
534     CreateTestFile(path, fileContent);
535 
536     vector<char> content = {'x', 'x', 't', 't'};
537     bool ret = SaveBufferToFile(path, content);
538     EXPECT_EQ(ret, true);
539 
540     string loadResult;
541     ret = LoadStringFromFile(path, loadResult);
542     RemoveTestFile(path);
543     EXPECT_EQ(ret, true);
544     EXPECT_EQ(loadResult, std::string(content.begin(), content.end()));
545 }
546 
547 /*
548  * @tc.name: testSaveBufferToFile003
549  * @tc.desc: singleton template
550  */
551 HWTEST_F(UtilsFileTest, testSaveBufferToFile003, TestSize.Level0)
552 {
553     vector<char> buff;
554     string path = "./tmp2.txt";
555     string fileContent = "ttxx";
556     CreateTestFile(path, fileContent);
557     bool ret = SaveBufferToFile(path, buff, false);
558     EXPECT_EQ(ret, true);
559 
560     string loadResult;
561     ret = LoadStringFromFile(path, loadResult);
562     RemoveTestFile(path);
563     EXPECT_EQ(ret, true);
564     EXPECT_EQ(loadResult, fileContent);
565 }
566 
567 /*
568  * @tc.name: testSaveBufferToFile004
569  * @tc.desc: singleton template
570  */
571 HWTEST_F(UtilsFileTest, testSaveBufferToFile004, TestSize.Level0)
572 {
573     string path = "./tmp2.txt";
574     string fileContent = "ttxx";
575     CreateTestFile(path, fileContent);
576 
577     vector<char> content = {'x', 'x', 't', 't'};
578     bool ret = SaveBufferToFile(path, content, false);
579     EXPECT_EQ(ret, true);
580 
581     string loadResult;
582     ret = LoadStringFromFile(path, loadResult);
583     RemoveTestFile(path);
584     EXPECT_EQ(ret, true);
585     EXPECT_EQ(loadResult, fileContent + std::string(content.begin(), content.end()));
586 }
587 
588 /*
589  * @tc.name: testStringExistsInFile001
590  * @tc.desc: singleton template
591  */
592 HWTEST_F(UtilsFileTest, testStringExistsInFile001, TestSize.Level0)
593 {
594     string str = "abc";
595     string filename = "";
596     EXPECT_FALSE(StringExistsInFile(filename, str, true));
597     EXPECT_FALSE(str.empty());
598 }
599 
600 /*
601  * @tc.name: testStringExistsInFile002
602  * @tc.desc: singleton template
603  */
604 HWTEST_F(UtilsFileTest, testStringExistsInFile002, TestSize.Level0)
605 {
606     string str = "";
607     string filename = "./tmp.txt";
608     string content = "hello world!";
609     CreateTestFile(filename, content);
610     EXPECT_FALSE(StringExistsInFile(filename, str, true));
611     RemoveTestFile(filename);
612 }
613 
614 /*
615  * @tc.name: testStringExistsInFile003
616  * @tc.desc: singleton template
617  */
618 HWTEST_F(UtilsFileTest, testStringExistsInFile003, TestSize.Level0)
619 {
620     string str = "world";
621     string filename = "./tmp.txt";
622     string content = "hello world!";
623     CreateTestFile(filename, content);
624     EXPECT_TRUE(StringExistsInFile(filename, str, true));
625     RemoveTestFile(filename);
626 }
627 
628 /*
629  * @tc.name: testStringExistsInFile004
630  * @tc.desc: singleton template
631  */
632 HWTEST_F(UtilsFileTest, testStringExistsInFile004, TestSize.Level0)
633 {
634     string str1(32*1024*1024+1, 't');
635     string str2(32*1024*1024, 't');
636     string filename = "./tmp.txt";
637     string content(32*1024*1024, 't');
638     CreateTestFile(filename, content);
639     EXPECT_FALSE(StringExistsInFile(filename, str1, true));
640     EXPECT_TRUE(StringExistsInFile(filename, str2, true));
641     RemoveTestFile(filename);
642 }
643 
644 /*
645  * @tc.name: testStringExistsInFile005
646  * @tc.desc: singleton template
647  */
648 HWTEST_F(UtilsFileTest, testStringExistsInFile005, TestSize.Level0)
649 {
650     string str = "woRld";
651     string filename = "./tmp.txt";
652     string content = "hello world!";
653     CreateTestFile(filename, content);
654     EXPECT_TRUE(StringExistsInFile(filename, str, false));
655     EXPECT_FALSE(StringExistsInFile(filename, str, true));
656     RemoveTestFile(filename);
657 }
658 
659 /*
660  * @tc.name: testStringExistsInFile006
661  * @tc.desc: singleton template
662  */
663 HWTEST_F(UtilsFileTest, testStringExistsInFile006, TestSize.Level0)
664 {
665     string str1 = "woRld!";
666     string str2 = "123";
667     string str3 = "llo ";
668     string str4 = "123 w";
669     string str5 = "hi";
670     string filename = "./tmp.txt";
671     string content = "Test, hello 123 World!";
672     CreateTestFile(filename, content);
673     EXPECT_TRUE(StringExistsInFile(filename, str1, false));
674     EXPECT_FALSE(StringExistsInFile(filename, str1, true));
675 
676     EXPECT_TRUE(StringExistsInFile(filename, str2, false));
677     EXPECT_TRUE(StringExistsInFile(filename, str2, true));
678 
679     EXPECT_TRUE(StringExistsInFile(filename, str3, false));
680     EXPECT_TRUE(StringExistsInFile(filename, str3, true));
681 
682     EXPECT_TRUE(StringExistsInFile(filename, str4, false));
683     EXPECT_FALSE(StringExistsInFile(filename, str4, true));
684 
685     EXPECT_FALSE(StringExistsInFile(filename, str5, false));
686     EXPECT_FALSE(StringExistsInFile(filename, str5, true));
687     RemoveTestFile(filename);
688 }
689 
690 /*
691  * @tc.name: testStringExistsInFile007
692  * @tc.desc: singleton template
693  */
694 HWTEST_F(UtilsFileTest, testStringExistsInFile007, TestSize.Level0)
695 {
696     string str1 = "is";
697     string str2 = "\n\ris";
698     string filename = "./tmp.txt";
699     string content = "Test, special string\n\ris ok";
700     CreateTestFile(filename, content);
701     EXPECT_TRUE(StringExistsInFile(filename, str1, false));
702     EXPECT_TRUE(StringExistsInFile(filename, str1, true));
703 
704     EXPECT_TRUE(StringExistsInFile(filename, str2, false));
705     EXPECT_TRUE(StringExistsInFile(filename, str2, true));
706     RemoveTestFile(filename);
707 }
708 
709 /*
710  * @tc.name: testFileExist001
711  * @tc.desc: singleton template
712  */
713 HWTEST_F(UtilsFileTest, testFileExist001, TestSize.Level0)
714 {
715     string filepath = "/proc/meminfo";
716     string filepath1 = "/proc/meminfo1";
717 
718     EXPECT_TRUE(FileExists(filepath));
719     EXPECT_FALSE(FileExists(filepath1));
720 }
721 
722 /*
723  * @tc.name: testCountStrInFile001
724  * @tc.desc: singleton template
725  */
726 HWTEST_F(UtilsFileTest, testCountStrInFile001, TestSize.Level0)
727 {
728     string str = "abc";
729     string filename = "";
730     EXPECT_EQ(CountStrInFile(filename, str, true), -1);
731     EXPECT_FALSE(str.empty());
732 }
733 
734 /*
735  * @tc.name: testCountStrInFile002
736  * @tc.desc: singleton template
737  */
738 HWTEST_F(UtilsFileTest, testCountStrInFile002, TestSize.Level0)
739 {
740     string str = "";
741     string filename = "./tmp.txt";
742     string content = "hello world!";
743     CreateTestFile(filename, content);
744     EXPECT_EQ(CountStrInFile(filename, str, true), -1);
745     RemoveTestFile(filename);
746 }
747 
748 /*
749  * @tc.name: testCountStrInFile003
750  * @tc.desc: singleton template
751  */
752 HWTEST_F(UtilsFileTest, testCountStrInFile003, TestSize.Level0)
753 {
754     string str1(32*1024*1024+1, 't');
755     string str2(32*1024*1024, 't');
756     string filename = "./tmp.txt";
757     string content(32*1024*1024, 't');
758     CreateTestFile(filename, content);
759     EXPECT_EQ(CountStrInFile(filename, str1, true), 0);
760     EXPECT_EQ(CountStrInFile(filename, str2, true),1);
761     RemoveTestFile(filename);
762 }
763 
764 /*
765  * @tc.name: testCountStrInFile004
766  * @tc.desc: singleton template
767  */
768 HWTEST_F(UtilsFileTest, testCountStrInFile004, TestSize.Level0)
769 {
770     string str1 = "very";
771     string str2 = "VERY";
772     string str3 = "abc";
773     string filename = "./tmp.txt";
774     string content = "This is very very long string for test.\n Very Good,\r VERY HAPPY.";
775     CreateTestFile(filename, content);
776     EXPECT_EQ(CountStrInFile(filename, str1, true), 2);
777     EXPECT_EQ(CountStrInFile(filename, str1, false), 4);
778 
779     EXPECT_EQ(CountStrInFile(filename, str2, true), 1);
780     EXPECT_EQ(CountStrInFile(filename, str2, false), 4);
781 
782     EXPECT_EQ(CountStrInFile(filename, str3, true), 0);
783     RemoveTestFile(filename);
784 }
785 
786 /*
787  * @tc.name: testCountStrInFile005
788  * @tc.desc: singleton template
789  */
790 HWTEST_F(UtilsFileTest, testCountStrInFile005, TestSize.Level0)
791 {
792     string str1 = "aba";
793     string filename = "./tmp.txt";
794     string content = "This is abababaBABa.";
795     CreateTestFile(filename, content);
796     EXPECT_EQ(CountStrInFile(filename, str1, true), 2);
797     EXPECT_EQ(CountStrInFile(filename, str1, false), 3);
798     RemoveTestFile(filename);
799 }