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 26using namespace testing::ext; 27using namespace std; 28 29namespace OHOS { 30namespace { 31class UtilsFileTest : public testing::Test { 32public: 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 43void UtilsFileTest::SetUpTestCase(void) 44{ 45} 46 47void UtilsFileTest::TearDownTestCase(void) 48{ 49} 50 51void UtilsFileTest::SetUp(void) 52{ 53} 54 55void UtilsFileTest::TearDown(void) 56{ 57} 58 59bool 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 71int 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 */ 80HWTEST_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 */ 102HWTEST_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 */ 114HWTEST_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 */ 129HWTEST_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 */ 144HWTEST_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 */ 159HWTEST_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 */ 174HWTEST_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 */ 185HWTEST_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 */ 202HWTEST_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 */ 219HWTEST_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 */ 236HWTEST_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 */ 253HWTEST_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 */ 270HWTEST_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 */ 289HWTEST_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 an empty string to a file in truncate mode 307 */ 308HWTEST_F(UtilsFileTest, testSaveStringToFile003, TestSize.Level0) 309{ 310 string path = FILE_PATH; 311 string content = "Before truncated!"; 312 CreateTestFile(path, content); 313 314 string newContent; 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(), content.c_str()); 323} 324 325/* 326 * @tc.name: testSaveStringToFile004 327 * @tc.desc: Test writting an empty string to a file in append mode 328 */ 329HWTEST_F(UtilsFileTest, testSaveStringToFile004, TestSize.Level0) 330{ 331 string newContent; 332 string path = FILE_PATH; 333 string content = "Before appended!"; 334 CreateTestFile(path, content); 335 bool ret = SaveStringToFile(path, newContent, false); 336 EXPECT_EQ(ret, true); 337 338 string loadResult; 339 ret = LoadStringFromFile(path, loadResult); 340 RemoveTestFile(path); 341 EXPECT_EQ(ret, true); 342 EXPECT_STREQ(loadResult.c_str(), content.c_str()); 343} 344 345/* 346 * @tc.name: testSaveStringToFile005 347 * @tc.desc: Test writting a string to a file in append mode 348 */ 349HWTEST_F(UtilsFileTest, testSaveStringToFile005, TestSize.Level0) 350{ 351 string path = FILE_PATH; 352 string content = "Before appended!"; 353 CreateTestFile(path, content); 354 355 string newContent = CONTENT_STR; 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_EQ(loadResult, content + newContent); 364} 365 366/* 367 * @tc.name: testSaveStringToFd001 368 * @tc.desc: Test writting an empty string to files with invalid fds 369 */ 370HWTEST_F(UtilsFileTest, testSaveStringToFd001, TestSize.Level0) 371{ 372 string content; 373 bool ret = SaveStringToFd(0, content); 374 EXPECT_EQ(ret, false); 375 ret = SaveStringToFd(-1, content); 376 EXPECT_EQ(ret, false); 377 378 content = CONTENT_STR; 379 ret = SaveStringToFd(0, content); 380 EXPECT_EQ(ret, false); 381 ret = SaveStringToFd(-1, content); 382 EXPECT_EQ(ret, false); 383} 384 385/* 386 * @tc.name: testSaveStringToFd002 387 * @tc.desc: Test writting an empty string to a file specified by its fd 388 */ 389HWTEST_F(UtilsFileTest, testSaveStringToFd002, TestSize.Level0) 390{ 391 string content; 392 string filename = FILE_PATH; 393 int fd = open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 394 bool ret = SaveStringToFd(fd, content); 395 close(fd); 396 EXPECT_EQ(ret, true); 397 398 string loadResult; 399 fd = open(filename.c_str(), O_RDONLY); 400 ret = LoadStringFromFd(fd, loadResult); 401 close(fd); 402 RemoveTestFile(filename); 403 EXPECT_EQ(ret, true); 404 EXPECT_EQ(loadResult, ""); 405} 406 407/* 408 * @tc.name: testSaveStringToFd003 409 * @tc.desc: Test loading a non-empty string to a file specified by its fd 410 */ 411HWTEST_F(UtilsFileTest, testSaveStringToFd003, TestSize.Level0) 412{ 413 string content = CONTENT_STR; 414 string filename = FILE_PATH; 415 int fd = open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 416 bool ret = SaveStringToFd(fd, content); 417 close(fd); 418 EXPECT_EQ(ret, true); 419 420 string loadResult; 421 fd = open(filename.c_str(), O_RDONLY); 422 ret = LoadStringFromFd(fd, loadResult); 423 close(fd); 424 RemoveTestFile(filename); 425 EXPECT_EQ(ret, true); 426 EXPECT_EQ(loadResult, content); 427} 428 429/* 430 * @tc.name: testSaveStringToFd004 431 * @tc.desc: Test loading a non-empty string to a file without write-authority specified by its fd 432 */ 433HWTEST_F(UtilsFileTest, testSaveStringToFd004, TestSize.Level0) 434{ 435 string content = CONTENT_STR; 436 string filename = FILE_PATH; 437 int fd = open(filename.c_str(), O_RDONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 438 bool ret = SaveStringToFd(fd, content); 439 close(fd); 440 EXPECT_EQ(ret, false); 441 442 string loadResult; 443 fd = open(filename.c_str(), O_RDONLY); 444 ret = LoadStringFromFd(fd, loadResult); 445 close(fd); 446 RemoveTestFile(filename); 447 EXPECT_EQ(ret, true); 448 EXPECT_EQ(loadResult, ""); 449} 450 451/* 452 * @tc.name: testLoadBufferFromFile001 453 * @tc.desc: singleton template 454 */ 455HWTEST_F(UtilsFileTest, testLoadBufferFromFile001, TestSize.Level0) 456{ 457 vector<char> buff; 458 string filename = ""; 459 bool ret = LoadBufferFromFile(filename, buff); 460 EXPECT_FALSE(ret); 461 EXPECT_EQ(0, static_cast<int>(buff.size())); 462} 463 464/* 465 * @tc.name: testLoadBufferFromFile002 466 * @tc.desc: singleton template 467 */ 468HWTEST_F(UtilsFileTest, testLoadBufferFromFile002, TestSize.Level0) 469{ 470 vector<char> buff; 471 string filename = FILE_PATH; 472 string content; 473 CreateTestFile(filename, content); 474 bool ret = LoadBufferFromFile(filename, buff); 475 RemoveTestFile(filename); 476 EXPECT_TRUE(ret); 477 EXPECT_EQ(0, static_cast<int>(buff.size())); 478} 479 480/* 481 * @tc.name: testLoadBufferFromFile003 482 * @tc.desc: singleton template 483 */ 484HWTEST_F(UtilsFileTest, testLoadBufferFromFile003, TestSize.Level0) 485{ 486 vector<char> buff; 487 string filename = FILE_PATH; 488 string content = "TXB"; 489 CreateTestFile(filename, content); 490 bool ret = LoadBufferFromFile(filename, buff); 491 RemoveTestFile(filename); 492 EXPECT_TRUE(ret); 493 EXPECT_EQ(3, (int)buff.size()); 494 EXPECT_EQ('T', buff[0]); 495 EXPECT_EQ('X', buff[1]); 496 EXPECT_EQ('B', buff[2]); 497} 498 499/* 500 * @tc.name: testLoadBufferFromFile004 501 * @tc.desc: singleton template 502 */ 503HWTEST_F(UtilsFileTest, testLoadBufferFromFile004, TestSize.Level1) 504{ 505 vector<char> buff; 506 string filename = FILE_PATH; 507 string content(32 * 1024 * 1024 + 1, 't'); 508 CreateTestFile(filename, content); 509 bool ret = LoadBufferFromFile(filename, buff); 510 RemoveTestFile(filename); 511 EXPECT_EQ(ret, false); 512 EXPECT_EQ(0, static_cast<int>(buff.size())); 513} 514 515/* 516 * @tc.name: testSaveBufferToFile001 517 * @tc.desc: singleton template 518 */ 519HWTEST_F(UtilsFileTest, testSaveBufferToFile001, TestSize.Level0) 520{ 521 vector<char> buff; 522 string path = FILE_PATH; 523 string content = "ttxx"; 524 CreateTestFile(path, content); 525 bool ret = SaveBufferToFile(path, buff, false); 526 EXPECT_EQ(ret, true); 527 528 string loadResult; 529 ret = LoadStringFromFile(path, loadResult); 530 RemoveTestFile(path); 531 EXPECT_EQ(ret, true); 532 EXPECT_EQ(loadResult, content); 533} 534 535/* 536 * @tc.name: testSaveBufferToFile002 537 * @tc.desc: singleton template 538 */ 539HWTEST_F(UtilsFileTest, testSaveBufferToFile002, TestSize.Level0) 540{ 541 string path = FILE_PATH; 542 string content = "ttxx"; 543 CreateTestFile(path, content); 544 545 vector<char> newContent = {'x', 'x', 't', 't'}; 546 bool ret = SaveBufferToFile(path, newContent); 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, std::string(newContent.begin(), newContent.end())); 554} 555 556/* 557 * @tc.name: testSaveBufferToFile003 558 * @tc.desc: singleton template 559 */ 560HWTEST_F(UtilsFileTest, testSaveBufferToFile003, 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, false); 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, content + std::string(newContent.begin(), newContent.end())); 575} 576 577/* 578 * @tc.name: testStringExistsInFile001 579 * @tc.desc: singleton template 580 */ 581HWTEST_F(UtilsFileTest, testStringExistsInFile001, TestSize.Level0) 582{ 583 string str = "abc"; 584 string filename = ""; 585 EXPECT_FALSE(StringExistsInFile(filename, str, true)); 586 EXPECT_FALSE(str.empty()); 587} 588 589/* 590 * @tc.name: testStringExistsInFile002 591 * @tc.desc: singleton template 592 */ 593HWTEST_F(UtilsFileTest, testStringExistsInFile002, TestSize.Level0) 594{ 595 string str = NULL_STR; 596 string filename = FILE_PATH; 597 string content = "hello world!"; 598 CreateTestFile(filename, content); 599 EXPECT_FALSE(StringExistsInFile(filename, str, true)); 600 RemoveTestFile(filename); 601} 602 603/* 604 * @tc.name: testStringExistsInFile003 605 * @tc.desc: singleton template 606 */ 607HWTEST_F(UtilsFileTest, testStringExistsInFile003, TestSize.Level0) 608{ 609 string str = "world"; 610 string filename = FILE_PATH; 611 string content = "hello world!"; 612 CreateTestFile(filename, content); 613 EXPECT_TRUE(StringExistsInFile(filename, str, true)); 614 RemoveTestFile(filename); 615} 616 617/* 618 * @tc.name: testStringExistsInFile004 619 * @tc.desc: singleton template 620 */ 621HWTEST_F(UtilsFileTest, testStringExistsInFile004, TestSize.Level1) 622{ 623 string str1(32 * 1024 * 1024 + 1, 't'); 624 string str2(32 * 1024 * 1024, 't'); 625 string filename = FILE_PATH; 626 string content(32 * 1024 * 1024, 't'); 627 CreateTestFile(filename, content); 628 EXPECT_FALSE(StringExistsInFile(filename, str1, true)); 629 EXPECT_TRUE(StringExistsInFile(filename, str2, true)); 630 RemoveTestFile(filename); 631} 632 633/* 634 * @tc.name: testStringExistsInFile005 635 * @tc.desc: singleton template 636 */ 637HWTEST_F(UtilsFileTest, testStringExistsInFile005, TestSize.Level0) 638{ 639 string str = "woRld"; 640 string filename = FILE_PATH; 641 string content = "hello world!"; 642 CreateTestFile(filename, content); 643 EXPECT_TRUE(StringExistsInFile(filename, str, false)); 644 EXPECT_FALSE(StringExistsInFile(filename, str, true)); 645 RemoveTestFile(filename); 646} 647 648/* 649 * @tc.name: testStringExistsInFile006 650 * @tc.desc: singleton template 651 */ 652HWTEST_F(UtilsFileTest, testStringExistsInFile006, TestSize.Level0) 653{ 654 string str1 = "woRld!"; 655 string str2 = "123"; 656 string str3 = "llo "; 657 string str4 = "123 w"; 658 string str5 = "hi"; 659 string filename = FILE_PATH; 660 string content = "Test, hello 123 World!"; 661 CreateTestFile(filename, content); 662 EXPECT_TRUE(StringExistsInFile(filename, str1, false)); 663 EXPECT_FALSE(StringExistsInFile(filename, str1, true)); 664 665 EXPECT_TRUE(StringExistsInFile(filename, str2, false)); 666 EXPECT_TRUE(StringExistsInFile(filename, str2, true)); 667 668 EXPECT_TRUE(StringExistsInFile(filename, str3, false)); 669 EXPECT_TRUE(StringExistsInFile(filename, str3, true)); 670 671 EXPECT_TRUE(StringExistsInFile(filename, str4, false)); 672 EXPECT_FALSE(StringExistsInFile(filename, str4, true)); 673 674 EXPECT_FALSE(StringExistsInFile(filename, str5, false)); 675 EXPECT_FALSE(StringExistsInFile(filename, str5, true)); 676 RemoveTestFile(filename); 677} 678 679/* 680 * @tc.name: testStringExistsInFile007 681 * @tc.desc: singleton template 682 */ 683HWTEST_F(UtilsFileTest, testStringExistsInFile007, TestSize.Level0) 684{ 685 string str1 = "is"; 686 string str2 = "\n\ris"; 687 string filename = FILE_PATH; 688 string content = "Test, special string\n\ris ok"; 689 CreateTestFile(filename, content); 690 EXPECT_TRUE(StringExistsInFile(filename, str1, false)); 691 EXPECT_TRUE(StringExistsInFile(filename, str1, true)); 692 693 EXPECT_TRUE(StringExistsInFile(filename, str2, false)); 694 EXPECT_TRUE(StringExistsInFile(filename, str2, true)); 695 RemoveTestFile(filename); 696} 697 698/* 699 * @tc.name: testFileExist001 700 * @tc.desc: singleton template 701 */ 702HWTEST_F(UtilsFileTest, testFileExist001, TestSize.Level0) 703{ 704 string filepath = "/proc/meminfo"; 705 string filepath1 = "/proc/meminfo1"; 706 707 EXPECT_TRUE(FileExists(filepath)); 708 EXPECT_FALSE(FileExists(filepath1)); 709} 710 711/* 712 * @tc.name: testCountStrInFile001 713 * @tc.desc: singleton template 714 */ 715HWTEST_F(UtilsFileTest, testCountStrInFile001, TestSize.Level0) 716{ 717 string str = "abc"; 718 string filename = ""; 719 EXPECT_EQ(CountStrInFile(filename, str, true), -1); 720 EXPECT_FALSE(str.empty()); 721} 722 723/* 724 * @tc.name: testCountStrInFile002 725 * @tc.desc: singleton template 726 */ 727HWTEST_F(UtilsFileTest, testCountStrInFile002, TestSize.Level0) 728{ 729 string str = NULL_STR; 730 string filename = FILE_PATH; 731 string content = "hello world!"; 732 CreateTestFile(filename, content); 733 EXPECT_EQ(CountStrInFile(filename, str, true), -1); 734 RemoveTestFile(filename); 735} 736 737/* 738 * @tc.name: testCountStrInFile003 739 * @tc.desc: singleton template 740 */ 741HWTEST_F(UtilsFileTest, testCountStrInFile003, TestSize.Level1) 742{ 743 string str1(32 * 1024 * 1024 + 1, 't'); 744 string str2(32 * 1024 * 1024, 't'); 745 string filename = FILE_PATH; 746 string content(32 * 1024 * 1024, 't'); 747 CreateTestFile(filename, content); 748 EXPECT_EQ(CountStrInFile(filename, str1, true), 0); 749 EXPECT_EQ(CountStrInFile(filename, str2, true), 1); 750 RemoveTestFile(filename); 751} 752 753/* 754 * @tc.name: testCountStrInFile004 755 * @tc.desc: singleton template 756 */ 757HWTEST_F(UtilsFileTest, testCountStrInFile004, TestSize.Level0) 758{ 759 string str1 = "very"; 760 string str2 = "VERY"; 761 string str3 = "abc"; 762 string filename = FILE_PATH; 763 string content = "This is very very long string for test.\n Very Good,\r VERY HAPPY."; 764 CreateTestFile(filename, content); 765 EXPECT_EQ(CountStrInFile(filename, str1, true), 2); 766 EXPECT_EQ(CountStrInFile(filename, str1, false), 4); 767 768 EXPECT_EQ(CountStrInFile(filename, str2, true), 1); 769 EXPECT_EQ(CountStrInFile(filename, str2, false), 4); 770 771 EXPECT_EQ(CountStrInFile(filename, str3, true), 0); 772 RemoveTestFile(filename); 773} 774 775/* 776 * @tc.name: testCountStrInFile005 777 * @tc.desc: singleton template 778 */ 779HWTEST_F(UtilsFileTest, testCountStrInFile005, TestSize.Level0) 780{ 781 string str1 = "aba"; 782 string filename = FILE_PATH; 783 string content = "This is abababaBABa."; 784 CreateTestFile(filename, content); 785 EXPECT_EQ(CountStrInFile(filename, str1, true), 2); 786 EXPECT_EQ(CountStrInFile(filename, str1, false), 3); 787 RemoveTestFile(filename); 788} 789} // namespace 790} // namespace OHOS