1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 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 #include <fcntl.h> 16 #include <fstream> 17 #include <gtest/gtest.h> 18 #include <string> 19 #include <thread> 20 #include <unistd.h> 21 22 #include "file_utils.h" 23 24 using namespace testing::ext; 25 26 namespace { 27 constexpr int FILE_MODE = 0644; 28 29 class FileUtilsTest : public ::testing::Test { 30 protected: 31 std::string data_; 32 std::string path_; 33 SetUp()34 void SetUp() override 35 { 36 data_ = "ABCDEFGH"; 37 path_ = "file_utils_test.txt"; 38 std::ofstream fout(path_); 39 fout << data_; 40 } 41 TearDown()42 void TearDown() override 43 { 44 unlink(path_.c_str()); 45 } 46 }; 47 48 /* 49 * @tc.name: ReadFileFromFd 50 * @tc.desc: test FileUtils::ReadFile with fd. 51 * @tc.type: FUNC 52 */ 53 HWTEST_F(FileUtilsTest, ReadFileFromFd, TestSize.Level1) 54 { 55 int fd = open(path_.c_str(), O_RDONLY); 56 ASSERT_GT(fd, 0); 57 58 std::string text = FileUtils::ReadFile(fd); 59 EXPECT_EQ(text, data_); 60 EXPECT_EQ(close(fd), 0); 61 } 62 63 /* 64 * @tc.name: ReadFileFromInvalidFd 65 * @tc.desc: test FileUtils::ReadFile with invalid fd. 66 * @tc.type: FUNC 67 */ 68 HWTEST_F(FileUtilsTest, ReadFileFromInvalidFd, TestSize.Level1) 69 { 70 int fd = -1; 71 std::string text = FileUtils::ReadFile(fd); 72 EXPECT_EQ(text, ""); 73 } 74 75 /* 76 * @tc.name: ReadFileFromPath 77 * @tc.desc: test FileUtils::ReadFile with path. 78 * @tc.type: FUNC 79 */ 80 HWTEST_F(FileUtilsTest, ReadFileFromPath, TestSize.Level1) 81 { 82 std::string text = FileUtils::ReadFile(path_); 83 EXPECT_EQ(text, data_); 84 } 85 86 /* 87 * @tc.name: ReadFileFromInvalidPath 88 * @tc.desc: test FileUtils::ReadFile with invalid path. 89 * @tc.type: FUNC 90 */ 91 HWTEST_F(FileUtilsTest, ReadFileFromInvalidPath, TestSize.Level1) 92 { 93 std::string path = "file-name-that-not-exists.txt"; 94 std::string text = FileUtils::ReadFile(path); 95 EXPECT_EQ(text, ""); 96 } 97 98 /* 99 * @tc.name: WriteFileWithPath 100 * @tc.desc: test FileUtils::ReadFile with path. 101 * @tc.type: FUNC 102 */ 103 HWTEST_F(FileUtilsTest, WriteFileWithPath, TestSize.Level1) 104 { 105 std::string path = "temp.txt"; 106 107 int nbytes = data_.size(); 108 int retval = FileUtils::WriteFile(path, data_, O_CREAT | O_RDWR, FILE_MODE); 109 EXPECT_EQ(retval, nbytes); // expect write success! 110 111 std::string text = FileUtils::ReadFile(path); 112 EXPECT_EQ(text, data_); // expect same content read back! 113 114 EXPECT_EQ(unlink(path.c_str()), 0); 115 } 116 117 /* 118 * @tc.name: WriteFileWithInvalidPath 119 * @tc.desc: test FileUtils::ReadFile with path. 120 * @tc.type: FUNC 121 */ 122 HWTEST_F(FileUtilsTest, WriteFileWithInvalidPath, TestSize.Level1) 123 { 124 std::string path = "dir_not_exists/temp.txt"; 125 126 int retval = FileUtils::WriteFile(path, data_); 127 EXPECT_LT(retval, 0); 128 } 129 130 /* 131 * @tc.name: WriteFileWithPathOptions 132 * @tc.desc: test FileUtils::ReadFile with path. 133 * @tc.type: FUNC 134 */ 135 HWTEST_F(FileUtilsTest, WriteFileWithPathOptions, TestSize.Level1) 136 { 137 std::string path = "temp.txt"; 138 139 int nbytes = data_.size(); 140 int retval = FileUtils::WriteFile(path, data_, O_CREAT | O_RDWR, FILE_MODE); 141 EXPECT_EQ(retval, nbytes); 142 143 std::string text = FileUtils::ReadFile(path); 144 EXPECT_EQ(text, data_); // expect same content read back! 145 146 EXPECT_EQ(unlink(path.c_str()), 0); 147 } 148 149 /* 150 * @tc.name: WriteFileWithInvalidPath 151 * @tc.desc: test FileUtils::ReadFile with path. 152 * @tc.type: FUNC 153 */ 154 HWTEST_F(FileUtilsTest, WriteFileWithInvalidPathOptions, TestSize.Level1) 155 { 156 std::string path = "dir_not_exists/temp.txt"; 157 158 int retval = FileUtils::WriteFile(path, data_); 159 EXPECT_LT(retval, 0); 160 } 161 162 /* 163 * @tc.name: ListDirNormal 164 * @tc.desc: test FileUtils::ListDir with path. 165 * @tc.type: FUNC 166 */ 167 HWTEST_F(FileUtilsTest, ListDirNormal, TestSize.Level1) 168 { 169 std::string path = "/"; 170 171 auto items = FileUtils::ListDir(path); 172 EXPECT_GT(items.size(), static_cast<size_t>(0)); 173 } 174 } // namespace