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