13f4cbf05Sopenharmony_ci/* 23f4cbf05Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 33f4cbf05Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43f4cbf05Sopenharmony_ci * you may not use this file except in compliance with the License. 53f4cbf05Sopenharmony_ci * You may obtain a copy of the License at 63f4cbf05Sopenharmony_ci * 73f4cbf05Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83f4cbf05Sopenharmony_ci * 93f4cbf05Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103f4cbf05Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113f4cbf05Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123f4cbf05Sopenharmony_ci * See the License for the specific language governing permissions and 133f4cbf05Sopenharmony_ci * limitations under the License. 143f4cbf05Sopenharmony_ci */ 153f4cbf05Sopenharmony_ci 163f4cbf05Sopenharmony_ci#include <gtest/gtest.h> 173f4cbf05Sopenharmony_ci#include <algorithm> 183f4cbf05Sopenharmony_ci#include <iostream> 193f4cbf05Sopenharmony_ci#include <fstream> 203f4cbf05Sopenharmony_ci#include <sys/types.h> 213f4cbf05Sopenharmony_ci#include <sys/stat.h> 223f4cbf05Sopenharmony_ci#include <fcntl.h> 233f4cbf05Sopenharmony_ci 243f4cbf05Sopenharmony_ci#include "file_ex.h" 253f4cbf05Sopenharmony_ci 263f4cbf05Sopenharmony_ciusing namespace testing::ext; 273f4cbf05Sopenharmony_ciusing namespace std; 283f4cbf05Sopenharmony_ci 293f4cbf05Sopenharmony_cinamespace OHOS { 303f4cbf05Sopenharmony_cinamespace { 313f4cbf05Sopenharmony_ciclass UtilsFileTest : public testing::Test { 323f4cbf05Sopenharmony_cipublic: 333f4cbf05Sopenharmony_ci static constexpr char CONTENT_STR[] = "TTtt@#$%^&*()_+~`"; 343f4cbf05Sopenharmony_ci static constexpr char FILE_PATH[] = "./tmp.txt"; 353f4cbf05Sopenharmony_ci static constexpr char NULL_STR[] = ""; 363f4cbf05Sopenharmony_ci static constexpr int MAX_FILE_LENGTH = 32 * 1024 * 1024; 373f4cbf05Sopenharmony_ci static void SetUpTestCase(void); 383f4cbf05Sopenharmony_ci static void TearDownTestCase(void); 393f4cbf05Sopenharmony_ci void SetUp(); 403f4cbf05Sopenharmony_ci void TearDown(); 413f4cbf05Sopenharmony_ci}; 423f4cbf05Sopenharmony_ci 433f4cbf05Sopenharmony_civoid UtilsFileTest::SetUpTestCase(void) 443f4cbf05Sopenharmony_ci{ 453f4cbf05Sopenharmony_ci} 463f4cbf05Sopenharmony_ci 473f4cbf05Sopenharmony_civoid UtilsFileTest::TearDownTestCase(void) 483f4cbf05Sopenharmony_ci{ 493f4cbf05Sopenharmony_ci} 503f4cbf05Sopenharmony_ci 513f4cbf05Sopenharmony_civoid UtilsFileTest::SetUp(void) 523f4cbf05Sopenharmony_ci{ 533f4cbf05Sopenharmony_ci} 543f4cbf05Sopenharmony_ci 553f4cbf05Sopenharmony_civoid UtilsFileTest::TearDown(void) 563f4cbf05Sopenharmony_ci{ 573f4cbf05Sopenharmony_ci} 583f4cbf05Sopenharmony_ci 593f4cbf05Sopenharmony_cibool CreateTestFile(const std::string& path, const std::string& content) 603f4cbf05Sopenharmony_ci{ 613f4cbf05Sopenharmony_ci ofstream out(path, ios_base::out | ios_base::trunc); 623f4cbf05Sopenharmony_ci if (out.is_open()) { 633f4cbf05Sopenharmony_ci out << content; 643f4cbf05Sopenharmony_ci return true; 653f4cbf05Sopenharmony_ci } 663f4cbf05Sopenharmony_ci 673f4cbf05Sopenharmony_ci std::cout << "open file failed!" << path << std::endl; 683f4cbf05Sopenharmony_ci return false; 693f4cbf05Sopenharmony_ci} 703f4cbf05Sopenharmony_ci 713f4cbf05Sopenharmony_ciint RemoveTestFile(const std::string& path) 723f4cbf05Sopenharmony_ci{ 733f4cbf05Sopenharmony_ci return unlink(path.c_str()); 743f4cbf05Sopenharmony_ci} 753f4cbf05Sopenharmony_ci 763f4cbf05Sopenharmony_ci/* 773f4cbf05Sopenharmony_ci * @tc.name: testLoadStringFromFile001 783f4cbf05Sopenharmony_ci * @tc.desc: Test loading an existed file 'meminfo' 793f4cbf05Sopenharmony_ci */ 803f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testLoadStringFromFile001, TestSize.Level0) 813f4cbf05Sopenharmony_ci{ 823f4cbf05Sopenharmony_ci string str; 833f4cbf05Sopenharmony_ci string filename = "/proc/meminfo"; 843f4cbf05Sopenharmony_ci EXPECT_TRUE(LoadStringFromFile(filename, str)); 853f4cbf05Sopenharmony_ci 863f4cbf05Sopenharmony_ci string str2; 873f4cbf05Sopenharmony_ci int fd = open(filename.c_str(), O_RDONLY); 883f4cbf05Sopenharmony_ci EXPECT_TRUE(LoadStringFromFd(fd, str2)); 893f4cbf05Sopenharmony_ci close(fd); 903f4cbf05Sopenharmony_ci EXPECT_EQ(str.size(), str2.size()); 913f4cbf05Sopenharmony_ci 923f4cbf05Sopenharmony_ci vector<char> buff; 933f4cbf05Sopenharmony_ci bool ret = LoadBufferFromFile(filename, buff); 943f4cbf05Sopenharmony_ci EXPECT_TRUE(ret); 953f4cbf05Sopenharmony_ci EXPECT_EQ(str2.size(), buff.size()); 963f4cbf05Sopenharmony_ci} 973f4cbf05Sopenharmony_ci 983f4cbf05Sopenharmony_ci/* 993f4cbf05Sopenharmony_ci * @tc.name: testLoadStringFromFile002 1003f4cbf05Sopenharmony_ci * @tc.desc: Test loading a non-existed file 1013f4cbf05Sopenharmony_ci */ 1023f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testLoadStringFromFile002, TestSize.Level0) 1033f4cbf05Sopenharmony_ci{ 1043f4cbf05Sopenharmony_ci string str; 1053f4cbf05Sopenharmony_ci string filename = NULL_STR; 1063f4cbf05Sopenharmony_ci EXPECT_FALSE(LoadStringFromFile(filename, str)); 1073f4cbf05Sopenharmony_ci EXPECT_TRUE(str.empty()); 1083f4cbf05Sopenharmony_ci} 1093f4cbf05Sopenharmony_ci 1103f4cbf05Sopenharmony_ci/* 1113f4cbf05Sopenharmony_ci * @tc.name: testLoadStringFromFile003 1123f4cbf05Sopenharmony_ci * @tc.desc: Test loading a newly created file with null contents 1133f4cbf05Sopenharmony_ci */ 1143f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testLoadStringFromFile003, TestSize.Level0) 1153f4cbf05Sopenharmony_ci{ 1163f4cbf05Sopenharmony_ci string str; 1173f4cbf05Sopenharmony_ci string filename = FILE_PATH; 1183f4cbf05Sopenharmony_ci string content = NULL_STR; 1193f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 1203f4cbf05Sopenharmony_ci EXPECT_TRUE(LoadStringFromFile(filename, str)); 1213f4cbf05Sopenharmony_ci RemoveTestFile(filename); 1223f4cbf05Sopenharmony_ci EXPECT_TRUE(str == content); 1233f4cbf05Sopenharmony_ci} 1243f4cbf05Sopenharmony_ci 1253f4cbf05Sopenharmony_ci/* 1263f4cbf05Sopenharmony_ci * @tc.name: testLoadStringFromFile004 1273f4cbf05Sopenharmony_ci * @tc.desc: Test loading a newly created file with contents 1283f4cbf05Sopenharmony_ci */ 1293f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testLoadStringFromFile004, TestSize.Level0) 1303f4cbf05Sopenharmony_ci{ 1313f4cbf05Sopenharmony_ci string str; 1323f4cbf05Sopenharmony_ci string filename = FILE_PATH; 1333f4cbf05Sopenharmony_ci string content = CONTENT_STR; 1343f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 1353f4cbf05Sopenharmony_ci EXPECT_TRUE(LoadStringFromFile(filename, str)); 1363f4cbf05Sopenharmony_ci RemoveTestFile(filename); 1373f4cbf05Sopenharmony_ci EXPECT_TRUE(str == content); 1383f4cbf05Sopenharmony_ci} 1393f4cbf05Sopenharmony_ci 1403f4cbf05Sopenharmony_ci/* 1413f4cbf05Sopenharmony_ci * @tc.name: testLoadStringFromFile005 1423f4cbf05Sopenharmony_ci * @tc.desc: Test loading a newly created file, whose contents are of maximum length 1433f4cbf05Sopenharmony_ci */ 1443f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testLoadStringFromFile005, TestSize.Level1) 1453f4cbf05Sopenharmony_ci{ 1463f4cbf05Sopenharmony_ci string str; 1473f4cbf05Sopenharmony_ci string filename = FILE_PATH; 1483f4cbf05Sopenharmony_ci string content(MAX_FILE_LENGTH, 't'); 1493f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 1503f4cbf05Sopenharmony_ci EXPECT_TRUE(LoadStringFromFile(filename, str)); 1513f4cbf05Sopenharmony_ci RemoveTestFile(filename); 1523f4cbf05Sopenharmony_ci EXPECT_TRUE(str == content); 1533f4cbf05Sopenharmony_ci} 1543f4cbf05Sopenharmony_ci 1553f4cbf05Sopenharmony_ci/* 1563f4cbf05Sopenharmony_ci * @tc.name: testLoadStringFromFile006 1573f4cbf05Sopenharmony_ci * @tc.desc: Test loading a newly created file, whose contents exceeds maximum length 1583f4cbf05Sopenharmony_ci */ 1593f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testLoadStringFromFile006, TestSize.Level1) 1603f4cbf05Sopenharmony_ci{ 1613f4cbf05Sopenharmony_ci string str; 1623f4cbf05Sopenharmony_ci string filename = FILE_PATH; 1633f4cbf05Sopenharmony_ci string content(MAX_FILE_LENGTH + 1, 't'); 1643f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 1653f4cbf05Sopenharmony_ci EXPECT_FALSE(LoadStringFromFile(filename, str)); 1663f4cbf05Sopenharmony_ci RemoveTestFile(filename); 1673f4cbf05Sopenharmony_ci EXPECT_TRUE(str.empty()); 1683f4cbf05Sopenharmony_ci} 1693f4cbf05Sopenharmony_ci 1703f4cbf05Sopenharmony_ci/* 1713f4cbf05Sopenharmony_ci * @tc.name: testLoadStringFromFd001 1723f4cbf05Sopenharmony_ci * @tc.desc: Test loading a file by a invalid fd -1 1733f4cbf05Sopenharmony_ci */ 1743f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testLoadStringFromFd001, TestSize.Level0) 1753f4cbf05Sopenharmony_ci{ 1763f4cbf05Sopenharmony_ci string result; 1773f4cbf05Sopenharmony_ci EXPECT_FALSE(LoadStringFromFd(-1, result)); 1783f4cbf05Sopenharmony_ci EXPECT_EQ(result, ""); 1793f4cbf05Sopenharmony_ci} 1803f4cbf05Sopenharmony_ci 1813f4cbf05Sopenharmony_ci/* 1823f4cbf05Sopenharmony_ci * @tc.name: testLoadStringFromFd002 1833f4cbf05Sopenharmony_ci * @tc.desc: Test loading a newly created file without contents by its fd 1843f4cbf05Sopenharmony_ci */ 1853f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testLoadStringFromFd002, TestSize.Level0) 1863f4cbf05Sopenharmony_ci{ 1873f4cbf05Sopenharmony_ci string result; 1883f4cbf05Sopenharmony_ci string filename = FILE_PATH; 1893f4cbf05Sopenharmony_ci string content = NULL_STR; 1903f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 1913f4cbf05Sopenharmony_ci int fd = open(filename.c_str(), O_RDONLY); 1923f4cbf05Sopenharmony_ci EXPECT_TRUE(LoadStringFromFd(fd, result)); 1933f4cbf05Sopenharmony_ci close(fd); 1943f4cbf05Sopenharmony_ci RemoveTestFile(filename); 1953f4cbf05Sopenharmony_ci EXPECT_TRUE(result == content); 1963f4cbf05Sopenharmony_ci} 1973f4cbf05Sopenharmony_ci 1983f4cbf05Sopenharmony_ci/* 1993f4cbf05Sopenharmony_ci * @tc.name: testLoadStringFromFd003 2003f4cbf05Sopenharmony_ci * @tc.desc: Test loading a newly created file with contents by its fd 2013f4cbf05Sopenharmony_ci */ 2023f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testLoadStringFromFd003, TestSize.Level0) 2033f4cbf05Sopenharmony_ci{ 2043f4cbf05Sopenharmony_ci string result; 2053f4cbf05Sopenharmony_ci string filename = FILE_PATH; 2063f4cbf05Sopenharmony_ci string content = CONTENT_STR; 2073f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 2083f4cbf05Sopenharmony_ci int fd = open(filename.c_str(), O_RDONLY); 2093f4cbf05Sopenharmony_ci EXPECT_TRUE(LoadStringFromFd(fd, result)); 2103f4cbf05Sopenharmony_ci close(fd); 2113f4cbf05Sopenharmony_ci RemoveTestFile(filename); 2123f4cbf05Sopenharmony_ci EXPECT_TRUE(result == content); 2133f4cbf05Sopenharmony_ci} 2143f4cbf05Sopenharmony_ci 2153f4cbf05Sopenharmony_ci/* 2163f4cbf05Sopenharmony_ci * @tc.name: testLoadStringFromFd004 2173f4cbf05Sopenharmony_ci * @tc.desc: Test loading a newly created file by fd, whose contents are of maximum length 2183f4cbf05Sopenharmony_ci */ 2193f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testLoadStringFromFd004, TestSize.Level1) 2203f4cbf05Sopenharmony_ci{ 2213f4cbf05Sopenharmony_ci string result; 2223f4cbf05Sopenharmony_ci string filename = FILE_PATH; 2233f4cbf05Sopenharmony_ci string content(MAX_FILE_LENGTH, 't'); 2243f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 2253f4cbf05Sopenharmony_ci int fd = open(filename.c_str(), O_RDONLY); 2263f4cbf05Sopenharmony_ci EXPECT_TRUE(LoadStringFromFd(fd, result)); 2273f4cbf05Sopenharmony_ci close(fd); 2283f4cbf05Sopenharmony_ci RemoveTestFile(filename); 2293f4cbf05Sopenharmony_ci EXPECT_TRUE(result == content); 2303f4cbf05Sopenharmony_ci} 2313f4cbf05Sopenharmony_ci 2323f4cbf05Sopenharmony_ci/* 2333f4cbf05Sopenharmony_ci * @tc.name: testLoadStringFromFd005 2343f4cbf05Sopenharmony_ci * @tc.desc: Test loading a newly created file by fd, whose contents exceeds maximum length 2353f4cbf05Sopenharmony_ci */ 2363f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testLoadStringFromFd005, TestSize.Level1) 2373f4cbf05Sopenharmony_ci{ 2383f4cbf05Sopenharmony_ci string result; 2393f4cbf05Sopenharmony_ci string filename = FILE_PATH; 2403f4cbf05Sopenharmony_ci string content(MAX_FILE_LENGTH + 1, 't'); 2413f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 2423f4cbf05Sopenharmony_ci int fd = open(filename.c_str(), O_RDONLY); 2433f4cbf05Sopenharmony_ci EXPECT_FALSE(LoadStringFromFd(fd, result)); 2443f4cbf05Sopenharmony_ci close(fd); 2453f4cbf05Sopenharmony_ci RemoveTestFile(filename); 2463f4cbf05Sopenharmony_ci EXPECT_NE(result, content); 2473f4cbf05Sopenharmony_ci} 2483f4cbf05Sopenharmony_ci 2493f4cbf05Sopenharmony_ci/* 2503f4cbf05Sopenharmony_ci * @tc.name: testLoadStringFromFd006 2513f4cbf05Sopenharmony_ci * @tc.desc: Test loading a newly created file by fd, which is closed ahead of loading. 2523f4cbf05Sopenharmony_ci */ 2533f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testLoadStringFromFd006, TestSize.Level0) 2543f4cbf05Sopenharmony_ci{ 2553f4cbf05Sopenharmony_ci string result; 2563f4cbf05Sopenharmony_ci string filename = FILE_PATH; 2573f4cbf05Sopenharmony_ci string content = CONTENT_STR; 2583f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 2593f4cbf05Sopenharmony_ci int fd = open(filename.c_str(), O_RDONLY); 2603f4cbf05Sopenharmony_ci close(fd); 2613f4cbf05Sopenharmony_ci EXPECT_FALSE(LoadStringFromFd(fd, result)); 2623f4cbf05Sopenharmony_ci RemoveTestFile(filename); 2633f4cbf05Sopenharmony_ci EXPECT_EQ(result, ""); 2643f4cbf05Sopenharmony_ci} 2653f4cbf05Sopenharmony_ci 2663f4cbf05Sopenharmony_ci/* 2673f4cbf05Sopenharmony_ci * @tc.name: testSaveStringToFile001 2683f4cbf05Sopenharmony_ci * @tc.desc: singleton template 2693f4cbf05Sopenharmony_ci */ 2703f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testSaveStringToFile001, TestSize.Level0) 2713f4cbf05Sopenharmony_ci{ 2723f4cbf05Sopenharmony_ci string path = FILE_PATH; 2733f4cbf05Sopenharmony_ci string content = CONTENT_STR; 2743f4cbf05Sopenharmony_ci string newContent; 2753f4cbf05Sopenharmony_ci CreateTestFile(path, content); 2763f4cbf05Sopenharmony_ci bool ret = SaveStringToFile(path, newContent); 2773f4cbf05Sopenharmony_ci EXPECT_EQ(ret, true); 2783f4cbf05Sopenharmony_ci 2793f4cbf05Sopenharmony_ci string loadResult; 2803f4cbf05Sopenharmony_ci EXPECT_TRUE(LoadStringFromFile(path, loadResult)); 2813f4cbf05Sopenharmony_ci RemoveTestFile(path); 2823f4cbf05Sopenharmony_ci EXPECT_EQ(loadResult, content); 2833f4cbf05Sopenharmony_ci} 2843f4cbf05Sopenharmony_ci 2853f4cbf05Sopenharmony_ci/* 2863f4cbf05Sopenharmony_ci * @tc.name: testSaveStringToFile002 2873f4cbf05Sopenharmony_ci * @tc.desc: singleton template 2883f4cbf05Sopenharmony_ci */ 2893f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testSaveStringToFile002, TestSize.Level0) 2903f4cbf05Sopenharmony_ci{ 2913f4cbf05Sopenharmony_ci string path = FILE_PATH; 2923f4cbf05Sopenharmony_ci string content = "Before truncated!"; 2933f4cbf05Sopenharmony_ci CreateTestFile(path, content); 2943f4cbf05Sopenharmony_ci 2953f4cbf05Sopenharmony_ci string newContent = CONTENT_STR; 2963f4cbf05Sopenharmony_ci EXPECT_TRUE(SaveStringToFile(path, newContent)); 2973f4cbf05Sopenharmony_ci 2983f4cbf05Sopenharmony_ci string loadResult; 2993f4cbf05Sopenharmony_ci EXPECT_TRUE(LoadStringFromFile(path, loadResult)); 3003f4cbf05Sopenharmony_ci RemoveTestFile(path); 3013f4cbf05Sopenharmony_ci EXPECT_EQ(loadResult, newContent); 3023f4cbf05Sopenharmony_ci} 3033f4cbf05Sopenharmony_ci 3043f4cbf05Sopenharmony_ci/* 3053f4cbf05Sopenharmony_ci * @tc.name: testSaveStringToFile003 3063f4cbf05Sopenharmony_ci * @tc.desc: Test writting an empty string to a file in truncate mode 3073f4cbf05Sopenharmony_ci */ 3083f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testSaveStringToFile003, TestSize.Level0) 3093f4cbf05Sopenharmony_ci{ 3103f4cbf05Sopenharmony_ci string path = FILE_PATH; 3113f4cbf05Sopenharmony_ci string content = "Before truncated!"; 3123f4cbf05Sopenharmony_ci CreateTestFile(path, content); 3133f4cbf05Sopenharmony_ci 3143f4cbf05Sopenharmony_ci string newContent; 3153f4cbf05Sopenharmony_ci bool ret = SaveStringToFile(path, newContent, true); 3163f4cbf05Sopenharmony_ci EXPECT_EQ(ret, true); 3173f4cbf05Sopenharmony_ci 3183f4cbf05Sopenharmony_ci string loadResult; 3193f4cbf05Sopenharmony_ci ret = LoadStringFromFile(path, loadResult); 3203f4cbf05Sopenharmony_ci RemoveTestFile(path); 3213f4cbf05Sopenharmony_ci EXPECT_EQ(ret, true); 3223f4cbf05Sopenharmony_ci EXPECT_STREQ(loadResult.c_str(), content.c_str()); 3233f4cbf05Sopenharmony_ci} 3243f4cbf05Sopenharmony_ci 3253f4cbf05Sopenharmony_ci/* 3263f4cbf05Sopenharmony_ci * @tc.name: testSaveStringToFile004 3273f4cbf05Sopenharmony_ci * @tc.desc: Test writting an empty string to a file in append mode 3283f4cbf05Sopenharmony_ci */ 3293f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testSaveStringToFile004, TestSize.Level0) 3303f4cbf05Sopenharmony_ci{ 3313f4cbf05Sopenharmony_ci string newContent; 3323f4cbf05Sopenharmony_ci string path = FILE_PATH; 3333f4cbf05Sopenharmony_ci string content = "Before appended!"; 3343f4cbf05Sopenharmony_ci CreateTestFile(path, content); 3353f4cbf05Sopenharmony_ci bool ret = SaveStringToFile(path, newContent, false); 3363f4cbf05Sopenharmony_ci EXPECT_EQ(ret, true); 3373f4cbf05Sopenharmony_ci 3383f4cbf05Sopenharmony_ci string loadResult; 3393f4cbf05Sopenharmony_ci ret = LoadStringFromFile(path, loadResult); 3403f4cbf05Sopenharmony_ci RemoveTestFile(path); 3413f4cbf05Sopenharmony_ci EXPECT_EQ(ret, true); 3423f4cbf05Sopenharmony_ci EXPECT_STREQ(loadResult.c_str(), content.c_str()); 3433f4cbf05Sopenharmony_ci} 3443f4cbf05Sopenharmony_ci 3453f4cbf05Sopenharmony_ci/* 3463f4cbf05Sopenharmony_ci * @tc.name: testSaveStringToFile005 3473f4cbf05Sopenharmony_ci * @tc.desc: Test writting a string to a file in append mode 3483f4cbf05Sopenharmony_ci */ 3493f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testSaveStringToFile005, TestSize.Level0) 3503f4cbf05Sopenharmony_ci{ 3513f4cbf05Sopenharmony_ci string path = FILE_PATH; 3523f4cbf05Sopenharmony_ci string content = "Before appended!"; 3533f4cbf05Sopenharmony_ci CreateTestFile(path, content); 3543f4cbf05Sopenharmony_ci 3553f4cbf05Sopenharmony_ci string newContent = CONTENT_STR; 3563f4cbf05Sopenharmony_ci bool ret = SaveStringToFile(path, newContent, false); 3573f4cbf05Sopenharmony_ci EXPECT_EQ(ret, true); 3583f4cbf05Sopenharmony_ci 3593f4cbf05Sopenharmony_ci string loadResult; 3603f4cbf05Sopenharmony_ci ret = LoadStringFromFile(path, loadResult); 3613f4cbf05Sopenharmony_ci RemoveTestFile(path); 3623f4cbf05Sopenharmony_ci EXPECT_EQ(ret, true); 3633f4cbf05Sopenharmony_ci EXPECT_EQ(loadResult, content + newContent); 3643f4cbf05Sopenharmony_ci} 3653f4cbf05Sopenharmony_ci 3663f4cbf05Sopenharmony_ci/* 3673f4cbf05Sopenharmony_ci * @tc.name: testSaveStringToFd001 3683f4cbf05Sopenharmony_ci * @tc.desc: Test writting an empty string to files with invalid fds 3693f4cbf05Sopenharmony_ci */ 3703f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testSaveStringToFd001, TestSize.Level0) 3713f4cbf05Sopenharmony_ci{ 3723f4cbf05Sopenharmony_ci string content; 3733f4cbf05Sopenharmony_ci bool ret = SaveStringToFd(0, content); 3743f4cbf05Sopenharmony_ci EXPECT_EQ(ret, false); 3753f4cbf05Sopenharmony_ci ret = SaveStringToFd(-1, content); 3763f4cbf05Sopenharmony_ci EXPECT_EQ(ret, false); 3773f4cbf05Sopenharmony_ci 3783f4cbf05Sopenharmony_ci content = CONTENT_STR; 3793f4cbf05Sopenharmony_ci ret = SaveStringToFd(0, content); 3803f4cbf05Sopenharmony_ci EXPECT_EQ(ret, false); 3813f4cbf05Sopenharmony_ci ret = SaveStringToFd(-1, content); 3823f4cbf05Sopenharmony_ci EXPECT_EQ(ret, false); 3833f4cbf05Sopenharmony_ci} 3843f4cbf05Sopenharmony_ci 3853f4cbf05Sopenharmony_ci/* 3863f4cbf05Sopenharmony_ci * @tc.name: testSaveStringToFd002 3873f4cbf05Sopenharmony_ci * @tc.desc: Test writting an empty string to a file specified by its fd 3883f4cbf05Sopenharmony_ci */ 3893f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testSaveStringToFd002, TestSize.Level0) 3903f4cbf05Sopenharmony_ci{ 3913f4cbf05Sopenharmony_ci string content; 3923f4cbf05Sopenharmony_ci string filename = FILE_PATH; 3933f4cbf05Sopenharmony_ci int fd = open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 3943f4cbf05Sopenharmony_ci bool ret = SaveStringToFd(fd, content); 3953f4cbf05Sopenharmony_ci close(fd); 3963f4cbf05Sopenharmony_ci EXPECT_EQ(ret, true); 3973f4cbf05Sopenharmony_ci 3983f4cbf05Sopenharmony_ci string loadResult; 3993f4cbf05Sopenharmony_ci fd = open(filename.c_str(), O_RDONLY); 4003f4cbf05Sopenharmony_ci ret = LoadStringFromFd(fd, loadResult); 4013f4cbf05Sopenharmony_ci close(fd); 4023f4cbf05Sopenharmony_ci RemoveTestFile(filename); 4033f4cbf05Sopenharmony_ci EXPECT_EQ(ret, true); 4043f4cbf05Sopenharmony_ci EXPECT_EQ(loadResult, ""); 4053f4cbf05Sopenharmony_ci} 4063f4cbf05Sopenharmony_ci 4073f4cbf05Sopenharmony_ci/* 4083f4cbf05Sopenharmony_ci * @tc.name: testSaveStringToFd003 4093f4cbf05Sopenharmony_ci * @tc.desc: Test loading a non-empty string to a file specified by its fd 4103f4cbf05Sopenharmony_ci */ 4113f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testSaveStringToFd003, TestSize.Level0) 4123f4cbf05Sopenharmony_ci{ 4133f4cbf05Sopenharmony_ci string content = CONTENT_STR; 4143f4cbf05Sopenharmony_ci string filename = FILE_PATH; 4153f4cbf05Sopenharmony_ci int fd = open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 4163f4cbf05Sopenharmony_ci bool ret = SaveStringToFd(fd, content); 4173f4cbf05Sopenharmony_ci close(fd); 4183f4cbf05Sopenharmony_ci EXPECT_EQ(ret, true); 4193f4cbf05Sopenharmony_ci 4203f4cbf05Sopenharmony_ci string loadResult; 4213f4cbf05Sopenharmony_ci fd = open(filename.c_str(), O_RDONLY); 4223f4cbf05Sopenharmony_ci ret = LoadStringFromFd(fd, loadResult); 4233f4cbf05Sopenharmony_ci close(fd); 4243f4cbf05Sopenharmony_ci RemoveTestFile(filename); 4253f4cbf05Sopenharmony_ci EXPECT_EQ(ret, true); 4263f4cbf05Sopenharmony_ci EXPECT_EQ(loadResult, content); 4273f4cbf05Sopenharmony_ci} 4283f4cbf05Sopenharmony_ci 4293f4cbf05Sopenharmony_ci/* 4303f4cbf05Sopenharmony_ci * @tc.name: testSaveStringToFd004 4313f4cbf05Sopenharmony_ci * @tc.desc: Test loading a non-empty string to a file without write-authority specified by its fd 4323f4cbf05Sopenharmony_ci */ 4333f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testSaveStringToFd004, TestSize.Level0) 4343f4cbf05Sopenharmony_ci{ 4353f4cbf05Sopenharmony_ci string content = CONTENT_STR; 4363f4cbf05Sopenharmony_ci string filename = FILE_PATH; 4373f4cbf05Sopenharmony_ci int fd = open(filename.c_str(), O_RDONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 4383f4cbf05Sopenharmony_ci bool ret = SaveStringToFd(fd, content); 4393f4cbf05Sopenharmony_ci close(fd); 4403f4cbf05Sopenharmony_ci EXPECT_EQ(ret, false); 4413f4cbf05Sopenharmony_ci 4423f4cbf05Sopenharmony_ci string loadResult; 4433f4cbf05Sopenharmony_ci fd = open(filename.c_str(), O_RDONLY); 4443f4cbf05Sopenharmony_ci ret = LoadStringFromFd(fd, loadResult); 4453f4cbf05Sopenharmony_ci close(fd); 4463f4cbf05Sopenharmony_ci RemoveTestFile(filename); 4473f4cbf05Sopenharmony_ci EXPECT_EQ(ret, true); 4483f4cbf05Sopenharmony_ci EXPECT_EQ(loadResult, ""); 4493f4cbf05Sopenharmony_ci} 4503f4cbf05Sopenharmony_ci 4513f4cbf05Sopenharmony_ci/* 4523f4cbf05Sopenharmony_ci * @tc.name: testLoadBufferFromFile001 4533f4cbf05Sopenharmony_ci * @tc.desc: singleton template 4543f4cbf05Sopenharmony_ci */ 4553f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testLoadBufferFromFile001, TestSize.Level0) 4563f4cbf05Sopenharmony_ci{ 4573f4cbf05Sopenharmony_ci vector<char> buff; 4583f4cbf05Sopenharmony_ci string filename = ""; 4593f4cbf05Sopenharmony_ci bool ret = LoadBufferFromFile(filename, buff); 4603f4cbf05Sopenharmony_ci EXPECT_FALSE(ret); 4613f4cbf05Sopenharmony_ci EXPECT_EQ(0, static_cast<int>(buff.size())); 4623f4cbf05Sopenharmony_ci} 4633f4cbf05Sopenharmony_ci 4643f4cbf05Sopenharmony_ci/* 4653f4cbf05Sopenharmony_ci * @tc.name: testLoadBufferFromFile002 4663f4cbf05Sopenharmony_ci * @tc.desc: singleton template 4673f4cbf05Sopenharmony_ci */ 4683f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testLoadBufferFromFile002, TestSize.Level0) 4693f4cbf05Sopenharmony_ci{ 4703f4cbf05Sopenharmony_ci vector<char> buff; 4713f4cbf05Sopenharmony_ci string filename = FILE_PATH; 4723f4cbf05Sopenharmony_ci string content; 4733f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 4743f4cbf05Sopenharmony_ci bool ret = LoadBufferFromFile(filename, buff); 4753f4cbf05Sopenharmony_ci RemoveTestFile(filename); 4763f4cbf05Sopenharmony_ci EXPECT_TRUE(ret); 4773f4cbf05Sopenharmony_ci EXPECT_EQ(0, static_cast<int>(buff.size())); 4783f4cbf05Sopenharmony_ci} 4793f4cbf05Sopenharmony_ci 4803f4cbf05Sopenharmony_ci/* 4813f4cbf05Sopenharmony_ci * @tc.name: testLoadBufferFromFile003 4823f4cbf05Sopenharmony_ci * @tc.desc: singleton template 4833f4cbf05Sopenharmony_ci */ 4843f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testLoadBufferFromFile003, TestSize.Level0) 4853f4cbf05Sopenharmony_ci{ 4863f4cbf05Sopenharmony_ci vector<char> buff; 4873f4cbf05Sopenharmony_ci string filename = FILE_PATH; 4883f4cbf05Sopenharmony_ci string content = "TXB"; 4893f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 4903f4cbf05Sopenharmony_ci bool ret = LoadBufferFromFile(filename, buff); 4913f4cbf05Sopenharmony_ci RemoveTestFile(filename); 4923f4cbf05Sopenharmony_ci EXPECT_TRUE(ret); 4933f4cbf05Sopenharmony_ci EXPECT_EQ(3, (int)buff.size()); 4943f4cbf05Sopenharmony_ci EXPECT_EQ('T', buff[0]); 4953f4cbf05Sopenharmony_ci EXPECT_EQ('X', buff[1]); 4963f4cbf05Sopenharmony_ci EXPECT_EQ('B', buff[2]); 4973f4cbf05Sopenharmony_ci} 4983f4cbf05Sopenharmony_ci 4993f4cbf05Sopenharmony_ci/* 5003f4cbf05Sopenharmony_ci * @tc.name: testLoadBufferFromFile004 5013f4cbf05Sopenharmony_ci * @tc.desc: singleton template 5023f4cbf05Sopenharmony_ci */ 5033f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testLoadBufferFromFile004, TestSize.Level1) 5043f4cbf05Sopenharmony_ci{ 5053f4cbf05Sopenharmony_ci vector<char> buff; 5063f4cbf05Sopenharmony_ci string filename = FILE_PATH; 5073f4cbf05Sopenharmony_ci string content(32 * 1024 * 1024 + 1, 't'); 5083f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 5093f4cbf05Sopenharmony_ci bool ret = LoadBufferFromFile(filename, buff); 5103f4cbf05Sopenharmony_ci RemoveTestFile(filename); 5113f4cbf05Sopenharmony_ci EXPECT_EQ(ret, false); 5123f4cbf05Sopenharmony_ci EXPECT_EQ(0, static_cast<int>(buff.size())); 5133f4cbf05Sopenharmony_ci} 5143f4cbf05Sopenharmony_ci 5153f4cbf05Sopenharmony_ci/* 5163f4cbf05Sopenharmony_ci * @tc.name: testSaveBufferToFile001 5173f4cbf05Sopenharmony_ci * @tc.desc: singleton template 5183f4cbf05Sopenharmony_ci */ 5193f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testSaveBufferToFile001, TestSize.Level0) 5203f4cbf05Sopenharmony_ci{ 5213f4cbf05Sopenharmony_ci vector<char> buff; 5223f4cbf05Sopenharmony_ci string path = FILE_PATH; 5233f4cbf05Sopenharmony_ci string content = "ttxx"; 5243f4cbf05Sopenharmony_ci CreateTestFile(path, content); 5253f4cbf05Sopenharmony_ci bool ret = SaveBufferToFile(path, buff, false); 5263f4cbf05Sopenharmony_ci EXPECT_EQ(ret, true); 5273f4cbf05Sopenharmony_ci 5283f4cbf05Sopenharmony_ci string loadResult; 5293f4cbf05Sopenharmony_ci ret = LoadStringFromFile(path, loadResult); 5303f4cbf05Sopenharmony_ci RemoveTestFile(path); 5313f4cbf05Sopenharmony_ci EXPECT_EQ(ret, true); 5323f4cbf05Sopenharmony_ci EXPECT_EQ(loadResult, content); 5333f4cbf05Sopenharmony_ci} 5343f4cbf05Sopenharmony_ci 5353f4cbf05Sopenharmony_ci/* 5363f4cbf05Sopenharmony_ci * @tc.name: testSaveBufferToFile002 5373f4cbf05Sopenharmony_ci * @tc.desc: singleton template 5383f4cbf05Sopenharmony_ci */ 5393f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testSaveBufferToFile002, TestSize.Level0) 5403f4cbf05Sopenharmony_ci{ 5413f4cbf05Sopenharmony_ci string path = FILE_PATH; 5423f4cbf05Sopenharmony_ci string content = "ttxx"; 5433f4cbf05Sopenharmony_ci CreateTestFile(path, content); 5443f4cbf05Sopenharmony_ci 5453f4cbf05Sopenharmony_ci vector<char> newContent = {'x', 'x', 't', 't'}; 5463f4cbf05Sopenharmony_ci bool ret = SaveBufferToFile(path, newContent); 5473f4cbf05Sopenharmony_ci EXPECT_EQ(ret, true); 5483f4cbf05Sopenharmony_ci 5493f4cbf05Sopenharmony_ci string loadResult; 5503f4cbf05Sopenharmony_ci ret = LoadStringFromFile(path, loadResult); 5513f4cbf05Sopenharmony_ci RemoveTestFile(path); 5523f4cbf05Sopenharmony_ci EXPECT_EQ(ret, true); 5533f4cbf05Sopenharmony_ci EXPECT_EQ(loadResult, std::string(newContent.begin(), newContent.end())); 5543f4cbf05Sopenharmony_ci} 5553f4cbf05Sopenharmony_ci 5563f4cbf05Sopenharmony_ci/* 5573f4cbf05Sopenharmony_ci * @tc.name: testSaveBufferToFile003 5583f4cbf05Sopenharmony_ci * @tc.desc: singleton template 5593f4cbf05Sopenharmony_ci */ 5603f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testSaveBufferToFile003, TestSize.Level0) 5613f4cbf05Sopenharmony_ci{ 5623f4cbf05Sopenharmony_ci string path = FILE_PATH; 5633f4cbf05Sopenharmony_ci string content = "ttxx"; 5643f4cbf05Sopenharmony_ci CreateTestFile(path, content); 5653f4cbf05Sopenharmony_ci 5663f4cbf05Sopenharmony_ci vector<char> newContent = {'x', 'x', 't', 't'}; 5673f4cbf05Sopenharmony_ci bool ret = SaveBufferToFile(path, newContent, false); 5683f4cbf05Sopenharmony_ci EXPECT_EQ(ret, true); 5693f4cbf05Sopenharmony_ci 5703f4cbf05Sopenharmony_ci string loadResult; 5713f4cbf05Sopenharmony_ci ret = LoadStringFromFile(path, loadResult); 5723f4cbf05Sopenharmony_ci RemoveTestFile(path); 5733f4cbf05Sopenharmony_ci EXPECT_EQ(ret, true); 5743f4cbf05Sopenharmony_ci EXPECT_EQ(loadResult, content + std::string(newContent.begin(), newContent.end())); 5753f4cbf05Sopenharmony_ci} 5763f4cbf05Sopenharmony_ci 5773f4cbf05Sopenharmony_ci/* 5783f4cbf05Sopenharmony_ci * @tc.name: testStringExistsInFile001 5793f4cbf05Sopenharmony_ci * @tc.desc: singleton template 5803f4cbf05Sopenharmony_ci */ 5813f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testStringExistsInFile001, TestSize.Level0) 5823f4cbf05Sopenharmony_ci{ 5833f4cbf05Sopenharmony_ci string str = "abc"; 5843f4cbf05Sopenharmony_ci string filename = ""; 5853f4cbf05Sopenharmony_ci EXPECT_FALSE(StringExistsInFile(filename, str, true)); 5863f4cbf05Sopenharmony_ci EXPECT_FALSE(str.empty()); 5873f4cbf05Sopenharmony_ci} 5883f4cbf05Sopenharmony_ci 5893f4cbf05Sopenharmony_ci/* 5903f4cbf05Sopenharmony_ci * @tc.name: testStringExistsInFile002 5913f4cbf05Sopenharmony_ci * @tc.desc: singleton template 5923f4cbf05Sopenharmony_ci */ 5933f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testStringExistsInFile002, TestSize.Level0) 5943f4cbf05Sopenharmony_ci{ 5953f4cbf05Sopenharmony_ci string str = NULL_STR; 5963f4cbf05Sopenharmony_ci string filename = FILE_PATH; 5973f4cbf05Sopenharmony_ci string content = "hello world!"; 5983f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 5993f4cbf05Sopenharmony_ci EXPECT_FALSE(StringExistsInFile(filename, str, true)); 6003f4cbf05Sopenharmony_ci RemoveTestFile(filename); 6013f4cbf05Sopenharmony_ci} 6023f4cbf05Sopenharmony_ci 6033f4cbf05Sopenharmony_ci/* 6043f4cbf05Sopenharmony_ci * @tc.name: testStringExistsInFile003 6053f4cbf05Sopenharmony_ci * @tc.desc: singleton template 6063f4cbf05Sopenharmony_ci */ 6073f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testStringExistsInFile003, TestSize.Level0) 6083f4cbf05Sopenharmony_ci{ 6093f4cbf05Sopenharmony_ci string str = "world"; 6103f4cbf05Sopenharmony_ci string filename = FILE_PATH; 6113f4cbf05Sopenharmony_ci string content = "hello world!"; 6123f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 6133f4cbf05Sopenharmony_ci EXPECT_TRUE(StringExistsInFile(filename, str, true)); 6143f4cbf05Sopenharmony_ci RemoveTestFile(filename); 6153f4cbf05Sopenharmony_ci} 6163f4cbf05Sopenharmony_ci 6173f4cbf05Sopenharmony_ci/* 6183f4cbf05Sopenharmony_ci * @tc.name: testStringExistsInFile004 6193f4cbf05Sopenharmony_ci * @tc.desc: singleton template 6203f4cbf05Sopenharmony_ci */ 6213f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testStringExistsInFile004, TestSize.Level1) 6223f4cbf05Sopenharmony_ci{ 6233f4cbf05Sopenharmony_ci string str1(32 * 1024 * 1024 + 1, 't'); 6243f4cbf05Sopenharmony_ci string str2(32 * 1024 * 1024, 't'); 6253f4cbf05Sopenharmony_ci string filename = FILE_PATH; 6263f4cbf05Sopenharmony_ci string content(32 * 1024 * 1024, 't'); 6273f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 6283f4cbf05Sopenharmony_ci EXPECT_FALSE(StringExistsInFile(filename, str1, true)); 6293f4cbf05Sopenharmony_ci EXPECT_TRUE(StringExistsInFile(filename, str2, true)); 6303f4cbf05Sopenharmony_ci RemoveTestFile(filename); 6313f4cbf05Sopenharmony_ci} 6323f4cbf05Sopenharmony_ci 6333f4cbf05Sopenharmony_ci/* 6343f4cbf05Sopenharmony_ci * @tc.name: testStringExistsInFile005 6353f4cbf05Sopenharmony_ci * @tc.desc: singleton template 6363f4cbf05Sopenharmony_ci */ 6373f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testStringExistsInFile005, TestSize.Level0) 6383f4cbf05Sopenharmony_ci{ 6393f4cbf05Sopenharmony_ci string str = "woRld"; 6403f4cbf05Sopenharmony_ci string filename = FILE_PATH; 6413f4cbf05Sopenharmony_ci string content = "hello world!"; 6423f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 6433f4cbf05Sopenharmony_ci EXPECT_TRUE(StringExistsInFile(filename, str, false)); 6443f4cbf05Sopenharmony_ci EXPECT_FALSE(StringExistsInFile(filename, str, true)); 6453f4cbf05Sopenharmony_ci RemoveTestFile(filename); 6463f4cbf05Sopenharmony_ci} 6473f4cbf05Sopenharmony_ci 6483f4cbf05Sopenharmony_ci/* 6493f4cbf05Sopenharmony_ci * @tc.name: testStringExistsInFile006 6503f4cbf05Sopenharmony_ci * @tc.desc: singleton template 6513f4cbf05Sopenharmony_ci */ 6523f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testStringExistsInFile006, TestSize.Level0) 6533f4cbf05Sopenharmony_ci{ 6543f4cbf05Sopenharmony_ci string str1 = "woRld!"; 6553f4cbf05Sopenharmony_ci string str2 = "123"; 6563f4cbf05Sopenharmony_ci string str3 = "llo "; 6573f4cbf05Sopenharmony_ci string str4 = "123 w"; 6583f4cbf05Sopenharmony_ci string str5 = "hi"; 6593f4cbf05Sopenharmony_ci string filename = FILE_PATH; 6603f4cbf05Sopenharmony_ci string content = "Test, hello 123 World!"; 6613f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 6623f4cbf05Sopenharmony_ci EXPECT_TRUE(StringExistsInFile(filename, str1, false)); 6633f4cbf05Sopenharmony_ci EXPECT_FALSE(StringExistsInFile(filename, str1, true)); 6643f4cbf05Sopenharmony_ci 6653f4cbf05Sopenharmony_ci EXPECT_TRUE(StringExistsInFile(filename, str2, false)); 6663f4cbf05Sopenharmony_ci EXPECT_TRUE(StringExistsInFile(filename, str2, true)); 6673f4cbf05Sopenharmony_ci 6683f4cbf05Sopenharmony_ci EXPECT_TRUE(StringExistsInFile(filename, str3, false)); 6693f4cbf05Sopenharmony_ci EXPECT_TRUE(StringExistsInFile(filename, str3, true)); 6703f4cbf05Sopenharmony_ci 6713f4cbf05Sopenharmony_ci EXPECT_TRUE(StringExistsInFile(filename, str4, false)); 6723f4cbf05Sopenharmony_ci EXPECT_FALSE(StringExistsInFile(filename, str4, true)); 6733f4cbf05Sopenharmony_ci 6743f4cbf05Sopenharmony_ci EXPECT_FALSE(StringExistsInFile(filename, str5, false)); 6753f4cbf05Sopenharmony_ci EXPECT_FALSE(StringExistsInFile(filename, str5, true)); 6763f4cbf05Sopenharmony_ci RemoveTestFile(filename); 6773f4cbf05Sopenharmony_ci} 6783f4cbf05Sopenharmony_ci 6793f4cbf05Sopenharmony_ci/* 6803f4cbf05Sopenharmony_ci * @tc.name: testStringExistsInFile007 6813f4cbf05Sopenharmony_ci * @tc.desc: singleton template 6823f4cbf05Sopenharmony_ci */ 6833f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testStringExistsInFile007, TestSize.Level0) 6843f4cbf05Sopenharmony_ci{ 6853f4cbf05Sopenharmony_ci string str1 = "is"; 6863f4cbf05Sopenharmony_ci string str2 = "\n\ris"; 6873f4cbf05Sopenharmony_ci string filename = FILE_PATH; 6883f4cbf05Sopenharmony_ci string content = "Test, special string\n\ris ok"; 6893f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 6903f4cbf05Sopenharmony_ci EXPECT_TRUE(StringExistsInFile(filename, str1, false)); 6913f4cbf05Sopenharmony_ci EXPECT_TRUE(StringExistsInFile(filename, str1, true)); 6923f4cbf05Sopenharmony_ci 6933f4cbf05Sopenharmony_ci EXPECT_TRUE(StringExistsInFile(filename, str2, false)); 6943f4cbf05Sopenharmony_ci EXPECT_TRUE(StringExistsInFile(filename, str2, true)); 6953f4cbf05Sopenharmony_ci RemoveTestFile(filename); 6963f4cbf05Sopenharmony_ci} 6973f4cbf05Sopenharmony_ci 6983f4cbf05Sopenharmony_ci/* 6993f4cbf05Sopenharmony_ci * @tc.name: testFileExist001 7003f4cbf05Sopenharmony_ci * @tc.desc: singleton template 7013f4cbf05Sopenharmony_ci */ 7023f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testFileExist001, TestSize.Level0) 7033f4cbf05Sopenharmony_ci{ 7043f4cbf05Sopenharmony_ci string filepath = "/proc/meminfo"; 7053f4cbf05Sopenharmony_ci string filepath1 = "/proc/meminfo1"; 7063f4cbf05Sopenharmony_ci 7073f4cbf05Sopenharmony_ci EXPECT_TRUE(FileExists(filepath)); 7083f4cbf05Sopenharmony_ci EXPECT_FALSE(FileExists(filepath1)); 7093f4cbf05Sopenharmony_ci} 7103f4cbf05Sopenharmony_ci 7113f4cbf05Sopenharmony_ci/* 7123f4cbf05Sopenharmony_ci * @tc.name: testCountStrInFile001 7133f4cbf05Sopenharmony_ci * @tc.desc: singleton template 7143f4cbf05Sopenharmony_ci */ 7153f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testCountStrInFile001, TestSize.Level0) 7163f4cbf05Sopenharmony_ci{ 7173f4cbf05Sopenharmony_ci string str = "abc"; 7183f4cbf05Sopenharmony_ci string filename = ""; 7193f4cbf05Sopenharmony_ci EXPECT_EQ(CountStrInFile(filename, str, true), -1); 7203f4cbf05Sopenharmony_ci EXPECT_FALSE(str.empty()); 7213f4cbf05Sopenharmony_ci} 7223f4cbf05Sopenharmony_ci 7233f4cbf05Sopenharmony_ci/* 7243f4cbf05Sopenharmony_ci * @tc.name: testCountStrInFile002 7253f4cbf05Sopenharmony_ci * @tc.desc: singleton template 7263f4cbf05Sopenharmony_ci */ 7273f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testCountStrInFile002, TestSize.Level0) 7283f4cbf05Sopenharmony_ci{ 7293f4cbf05Sopenharmony_ci string str = NULL_STR; 7303f4cbf05Sopenharmony_ci string filename = FILE_PATH; 7313f4cbf05Sopenharmony_ci string content = "hello world!"; 7323f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 7333f4cbf05Sopenharmony_ci EXPECT_EQ(CountStrInFile(filename, str, true), -1); 7343f4cbf05Sopenharmony_ci RemoveTestFile(filename); 7353f4cbf05Sopenharmony_ci} 7363f4cbf05Sopenharmony_ci 7373f4cbf05Sopenharmony_ci/* 7383f4cbf05Sopenharmony_ci * @tc.name: testCountStrInFile003 7393f4cbf05Sopenharmony_ci * @tc.desc: singleton template 7403f4cbf05Sopenharmony_ci */ 7413f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testCountStrInFile003, TestSize.Level1) 7423f4cbf05Sopenharmony_ci{ 7433f4cbf05Sopenharmony_ci string str1(32 * 1024 * 1024 + 1, 't'); 7443f4cbf05Sopenharmony_ci string str2(32 * 1024 * 1024, 't'); 7453f4cbf05Sopenharmony_ci string filename = FILE_PATH; 7463f4cbf05Sopenharmony_ci string content(32 * 1024 * 1024, 't'); 7473f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 7483f4cbf05Sopenharmony_ci EXPECT_EQ(CountStrInFile(filename, str1, true), 0); 7493f4cbf05Sopenharmony_ci EXPECT_EQ(CountStrInFile(filename, str2, true), 1); 7503f4cbf05Sopenharmony_ci RemoveTestFile(filename); 7513f4cbf05Sopenharmony_ci} 7523f4cbf05Sopenharmony_ci 7533f4cbf05Sopenharmony_ci/* 7543f4cbf05Sopenharmony_ci * @tc.name: testCountStrInFile004 7553f4cbf05Sopenharmony_ci * @tc.desc: singleton template 7563f4cbf05Sopenharmony_ci */ 7573f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testCountStrInFile004, TestSize.Level0) 7583f4cbf05Sopenharmony_ci{ 7593f4cbf05Sopenharmony_ci string str1 = "very"; 7603f4cbf05Sopenharmony_ci string str2 = "VERY"; 7613f4cbf05Sopenharmony_ci string str3 = "abc"; 7623f4cbf05Sopenharmony_ci string filename = FILE_PATH; 7633f4cbf05Sopenharmony_ci string content = "This is very very long string for test.\n Very Good,\r VERY HAPPY."; 7643f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 7653f4cbf05Sopenharmony_ci EXPECT_EQ(CountStrInFile(filename, str1, true), 2); 7663f4cbf05Sopenharmony_ci EXPECT_EQ(CountStrInFile(filename, str1, false), 4); 7673f4cbf05Sopenharmony_ci 7683f4cbf05Sopenharmony_ci EXPECT_EQ(CountStrInFile(filename, str2, true), 1); 7693f4cbf05Sopenharmony_ci EXPECT_EQ(CountStrInFile(filename, str2, false), 4); 7703f4cbf05Sopenharmony_ci 7713f4cbf05Sopenharmony_ci EXPECT_EQ(CountStrInFile(filename, str3, true), 0); 7723f4cbf05Sopenharmony_ci RemoveTestFile(filename); 7733f4cbf05Sopenharmony_ci} 7743f4cbf05Sopenharmony_ci 7753f4cbf05Sopenharmony_ci/* 7763f4cbf05Sopenharmony_ci * @tc.name: testCountStrInFile005 7773f4cbf05Sopenharmony_ci * @tc.desc: singleton template 7783f4cbf05Sopenharmony_ci */ 7793f4cbf05Sopenharmony_ciHWTEST_F(UtilsFileTest, testCountStrInFile005, TestSize.Level0) 7803f4cbf05Sopenharmony_ci{ 7813f4cbf05Sopenharmony_ci string str1 = "aba"; 7823f4cbf05Sopenharmony_ci string filename = FILE_PATH; 7833f4cbf05Sopenharmony_ci string content = "This is abababaBABa."; 7843f4cbf05Sopenharmony_ci CreateTestFile(filename, content); 7853f4cbf05Sopenharmony_ci EXPECT_EQ(CountStrInFile(filename, str1, true), 2); 7863f4cbf05Sopenharmony_ci EXPECT_EQ(CountStrInFile(filename, str1, false), 3); 7873f4cbf05Sopenharmony_ci RemoveTestFile(filename); 7883f4cbf05Sopenharmony_ci} 7893f4cbf05Sopenharmony_ci} // namespace 7903f4cbf05Sopenharmony_ci} // namespace OHOS