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