• 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 "FileSystemTest.h"
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <dirent.h>
25 #include <ftw.h>
26 #include <libgen.h>
27 
28 #include <gtest/gtest.h>
29 
30 #include "utils.h"
31 #include "log.h"
32 #include "KernelConstants.h"
33 #include "libfs.h"
34 
35 using namespace testing::ext;
36 
37 #ifndef COMMERCIAL
38 /**
39  * @tc.number   SUB_KERNEL_FS_STAT_0100
40  * @tc.name     basic function test : umask set and get file mode creation mask
41  * @tc.desc     [C- SOFTWARE -0200]
42  */
43 HWTEST_F(FileSystemTest, testUmask, Function | MediumTest | Level2)
44 {
45     // set mode
46     mode_t maskNew = GetRandom(077);
47     mode_t maskPre = umask(maskNew);
48     LOG("> maskPre = %d", maskPre);
49     LOG("> maskNew = %d", maskNew);
50     EXPECT_EQ(umask(maskPre), maskNew) << "> umask error";
51 }
52 #endif
53 #if 0
54 /**
55  * @tc.number   SUB_KERNEL_FS_STAT_0200
56  * @tc.name     basic function test : Run the stat function to obtain the file status.
57  * @tc.desc     [C- SOFTWARE -0200]
58  */
59 HWTEST_F(FileSystemTest, testStat, Function | MediumTest | Level3)
60 {
61     int fd = 0;
62     mode_t mode = 0777;
63     const char *filePath = TOP_DIR "/" FILE0;
64     struct stat buf = {0};
65     char writeBuf[] = "this is a file";
66 
67     fd = open(FILE0, O_CREAT | O_RDWR, mode);
68     EXPECT_NE(fd, -1) << "> open faild errno = " << errno;
69     WriteCloseTest(fd);
70 
71     EXPECT_NE(stat(filePath, &buf), -1) << "> fstat errno = " << errno;
72     LOG("> buf.st_dev = %lu", buf.st_dev);                          // IDs of device on which file resides
73     LOG("> buf.st_ino = %lu", buf.st_ino);                          // I-node number of file
74     EXPECT_EQ(buf.st_rdev, 0) << "> buf.st_rdev not expect";        // IDs for device special files
75     EXPECT_EQ(buf.st_size, sizeof(writeBuf)) << "> buf.st_size = " << buf.st_size;
76     LOG("> buf.st_atim = %lds,%ldns", buf.st_atim.tv_sec, buf.st_atim.tv_nsec);     // time for last file access
77     LOG("> buf.st_mtim = %lds,%ldns", buf.st_mtim.tv_sec, buf.st_mtim.tv_nsec);     // time for last file modification
78     LOG("> buf.st_ctim = %lds,%ldns", buf.st_ctim.tv_sec, buf.st_ctim.tv_nsec);     // time for last file status change
79 }
80 #endif
81 #if defined(LITE_FS_JFFS2)
82 #ifndef COMMERCIAL
83 /**
84  * @tc.number   SUB_KERNEL_FS_STAT_0210
85  * @tc.name     basic function test : Run the stat function to test limt.
86  * @tc.desc     [C- SOFTWARE -0200]
87  */
88 HWTEST_F(FileSystemTest, testStatLimt, Function | MediumTest | Level3)
89 {
90     int fd = 0;
91     mode_t mode = 0777;
92     const char *filePath = TOP_DIR "/" FILE0;
93     struct stat buf;
94 
95     // set mode
96     mode_t maskNew = GetRandom(077);
97     mode_t maskPre = umask(maskNew);
98     mode = 0700 + GetRandom(077);
99     LOG("> maskPre = %d", maskPre);
100     LOG("> maskNew = %d", maskNew);
101     LOG("> mode = %d", mode);
102 
103     fd = open(FILE0, O_CREAT | O_RDWR, mode);
104     EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
105     EXPECT_NE(stat(filePath, &buf), -1) << "> fstat errno = " << errno;
106 
107     EXPECT_NE((buf.st_mode & S_IFREG), 0) << "> check file type faild";
108     EXPECT_EQ((buf.st_mode & 0777), (mode & (~maskNew))) << "> check file permission faild";
109     EXPECT_EQ(buf.st_nlink, 1) << "> buf.st_nlink not expect";                      // Number of (hard) links to file
110     EXPECT_EQ(buf.st_uid, getuid()) << "> The UIDs are different";                  // uid
111     EXPECT_EQ(buf.st_gid, getgid()) << "> The GIDs are different";                  // giu
112     umask(maskPre);
113 }
114 #endif
115 #endif
116 #if 0
117 /**
118  * @tc.number   SUB_KERNEL_FS_STAT_0300
119  * @tc.name     basic function test : Run the lstat function to obtain the file status.
120  * @tc.desc     [C- SOFTWARE -0200]
121  */
122 HWTEST_F(FileSystemTest, testLstat, Function | MediumTest | Level3)
123 {
124     int fd = 0;
125     mode_t mode = 0777;
126     const char *filePath = TOP_DIR "/" FILE0;
127     struct stat buf = {0};
128     char writeBuf[] = "this is a file";
129 
130     fd = open(filePath, O_CREAT | O_RDWR, mode);
131     EXPECT_NE(fd, -1) << "> open faild errno = " << errno;
132     WriteCloseTest(fd);
133 
134     EXPECT_NE(lstat(filePath, &buf), -1) << "> fstat errno = " << errno;
135     LOG("> buf.st_dev = %lu", buf.st_dev);                          // IDs of device on which file resides
136     LOG("> buf.st_ino = %lu", buf.st_ino);                          // I-node number of file
137     EXPECT_EQ(buf.st_rdev, 0) << "> buf.st_rdev not expect";        // IDs for device special files
138     EXPECT_EQ(buf.st_size, sizeof(writeBuf)) << "> buf.st_size = " << buf.st_size;
139 
140     LOG("> buf.st_atim = %lds,%ldns", buf.st_atim.tv_sec, buf.st_atim.tv_nsec);     // time for last file access
141     LOG("> buf.st_mtim = %lds,%ldns", buf.st_mtim.tv_sec, buf.st_mtim.tv_nsec);     // time for last file modification
142     LOG("> buf.st_ctim = %lds,%ldns", buf.st_ctim.tv_sec, buf.st_ctim.tv_nsec);     // time for last file status change
143 }
144 #endif
145 #if defined(LITE_FS_JFFS2)
146 #ifndef COMMERCIAL
147 /**
148  * @tc.number   SUB_KERNEL_FS_STAT_0310
149  * @tc.name     basic function test : Run the lstat function to test limt.
150  * @tc.desc     [C- SOFTWARE -0200]
151  */
152 HWTEST_F(FileSystemTest, testLstatLimt, Function | MediumTest | Level3)
153 {
154     int fd = 0;
155     mode_t mode = 0777;
156     const char *filePath = TOP_DIR "/" FILE0;
157     struct stat buf;
158 
159     // set mode
160     mode_t maskNew = GetRandom(077);
161     mode_t maskPre = umask(maskNew);
162     mode = 0700 + GetRandom(077);
163     LOG("> maskPre = %d", maskPre);
164     LOG("> maskNew = %d", maskNew);
165     LOG("> mode = %d", mode);
166 
167     fd = open(filePath, O_CREAT | O_RDWR, mode);
168     EXPECT_NE(fd, -1) << "> open faild errno = " << errno;
169     EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
170 
171     EXPECT_NE(lstat(filePath, &buf), -1) << "> fstat errno = " << errno;
172     EXPECT_NE((buf.st_mode & S_IFREG), 0) << "> check file type faild";
173     EXPECT_EQ((buf.st_mode & 0777), (mode & (~maskNew))) << "> check file permission faild";
174     EXPECT_EQ(buf.st_nlink, 1) << "> buf.st_nlink not expect";                      // Number of (hard) links to file
175     EXPECT_EQ(buf.st_uid, getuid()) << "> The UIDs are different";                  // uid
176     EXPECT_EQ(buf.st_gid, getgid()) << "> The GIDs are different";                  // giu
177     umask(maskPre);
178 }
179 #endif
180 #endif
181 #if 0
182 /**
183  * @tc.number   SUB_KERNEL_FS_STAT_0400
184  * @tc.name     basic function test : Run the fstat function to obtain the file status.
185  * @tc.desc     [C- SOFTWARE -0200]
186  */
187 HWTEST_F(FileSystemTest, testFstat, Function | MediumTest | Level3)
188 {
189     int fd = 0;
190     mode_t mode = 0777;
191     struct stat buf = {0};
192     char writeBuf[] = "this is a file";
193 
194     fd = open(FILE0, O_CREAT | O_RDWR, mode);
195     EXPECT_NE(fd, -1) << "> open faild errno = " << errno;
196     EXPECT_NE(write(fd, writeBuf, sizeof(writeBuf)), -1) << "> write errno = " << errno;
197 
198     EXPECT_NE(fstat(fd, &buf), -1) << "> fstat errno = " << errno;
199     LOG("> buf.st_dev = %lu", buf.st_dev);                          // IDs of device on which file resides
200     LOG("> buf.st_ino = %lu", buf.st_ino);                          // I-node number of file
201     EXPECT_EQ(buf.st_rdev, 0) << "> buf.st_rdev not expect";        // IDs for device special files
202     EXPECT_EQ(buf.st_size, sizeof(writeBuf)) << "> buf.st_size = " << buf.st_size;
203     LOG("> buf.st_atim = %lds,%ldns", buf.st_atim.tv_sec, buf.st_atim.tv_nsec);     // time for last file access
204     LOG("> buf.st_mtim = %lds,%ldns", buf.st_mtim.tv_sec, buf.st_mtim.tv_nsec);     // time for last file modification
205     LOG("> buf.st_ctim = %lds,%ldns", buf.st_ctim.tv_sec, buf.st_ctim.tv_nsec);     // time for last file status change
206     EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
207 }
208 #endif
209 #if defined(LITE_FS_JFFS2)
210 #ifndef COMMERCIAL
211 /**
212  * @tc.number   SUB_KERNEL_FS_STAT_0410
213  * @tc.name     basic function test : Run the fstat function to test limt
214  * @tc.desc     [C- SOFTWARE -0200]
215  */
216 HWTEST_F(FileSystemTest, testFstatLimt, Function | MediumTest | Level3)
217 {
218     int fd = 0;
219     mode_t mode = 0777;
220     struct stat buf;
221 
222     // set mode
223     mode_t maskNew = GetRandom(077);
224     mode_t maskPre = umask(maskNew);
225     mode = 0700 + GetRandom(077);
226     LOG("> maskPre = %d", maskPre);
227     LOG("> maskNew = %d", maskNew);
228     LOG("> mode = %d", mode);
229 
230     fd = open(FILE0, O_CREAT | O_RDWR, mode);
231     EXPECT_NE(fd, -1) << "> open faild errno = " << errno;
232 
233     EXPECT_NE(fstat(fd, &buf), -1) << "> fstat errno = " << errno;
234     EXPECT_NE((buf.st_mode & S_IFREG), 0) << "> check file type faild";
235     EXPECT_EQ((buf.st_mode & 0777), (mode & (~maskNew))) << "> check file permission faild";
236     EXPECT_EQ(buf.st_nlink, 1) << "> buf.st_nlink not expect";                      // Number of (hard) links to file
237     EXPECT_EQ(buf.st_uid, getuid()) << "> The UIDs are different";                  // uid
238     EXPECT_EQ(buf.st_gid, getgid()) << "> The GIDs are different";                  // giu
239 
240     EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
241     umask(maskPre);
242 }
243 #endif
244 #endif
245 
246 /**
247  * @tc.number   SUB_KERNEL_FS_STAT_0500
248  * @tc.name     basic function test : Run the {mkdirat} function to create a directory.
249  * @tc.desc     [C- SOFTWARE -0200]
250  */
251 HWTEST_F(FileSystemTest, testMkdirat, Function | MediumTest | Level0)
252 {
253     const char *pathName = TOP_DIR "/" DIR0;
254     EXPECT_NE(mkdirat(AT_FDCWD, pathName, 0777), -1) << "> creat faild errno = " << errno;
255     EXPECT_EQ(access(pathName, F_OK), 0) << "> access F_OK errno = " << errno;
256     EXPECT_EQ(remove(pathName), 0) << "> remove errno = " << errno;
257     EXPECT_NE(access(pathName, F_OK), 0) << "> access F_OK expect faild but success";
258 }
259 
260 /**
261  * @tc.number   SUB_KERNEL_FS_STAT_0510
262  * @tc.name     basic function test : test mkdirat with error number EINVAL
263  * @tc.desc     [C- SOFTWARE -0200]
264  */
265 HWTEST_F(FileSystemTest, testMkdiratEinval, Function | MediumTest | Level3)
266 {
267     EXPECT_EQ(mkdirat(AT_FDCWD, nullptr, 0777), -1) << "> should be error";
268     EXPECT_EQ(errno, EINVAL);
269 }
270 
271 /**
272  * @tc.number   SUB_KERNEL_FS_STAT_0520
273  * @tc.name     basic function test : test mkdirat with error number ENAMETOOLONG
274  * @tc.desc     [C- SOFTWARE -0200]
275  */
276 HWTEST_F(FileSystemTest, testMkdiratEnametoolong, Function | MediumTest | Level3)
277 {
278     const char *pathName = "12345678901234567890123456789012345678901234567890\
279                             12345678901234567890123456789012345678901234567890\
280                             12345678901234567890123456789012345678901234567890\
281                             12345678901234567890123456789012345678901234567890\
282                             12345678901234567890123456789012345678901234567890\
283                             12345678901234567890123456789012345678901234567890\
284                             12345678901234567890123456789012345678901234567890\
285                             12345678901234567890123456789012345678901234567890";
286     EXPECT_EQ(mkdirat(AT_FDCWD, pathName, 0777), -1) << "> should be error";
287     EXPECT_EQ(errno, ENAMETOOLONG);
288 }
289 
290 /**
291  * @tc.number   SUB_KERNEL_FS_STAT_0530
292  * @tc.name     basic function test : test mkdirat with error number ENOENT
293  * @tc.desc     [C- SOFTWARE -0200]
294  */
295 HWTEST_F(FileSystemTest, testMkdiratEnoent, Function | MediumTest | Level3)
296 {
297     const char *pathName = TOP_DIR "/NoExist/NoExist";
298     EXPECT_EQ(mkdirat(AT_FDCWD, pathName, 0777), -1) << "> should be error";
299     EXPECT_EQ(errno, ENOENT);
300 }
301 
302 /**
303  * @tc.number   SUB_KERNEL_FS_STAT_0540
304  * @tc.name     basic function test : test mkdirat with error number EEXIST
305  * @tc.desc     [C- SOFTWARE -0200]
306  */
307 HWTEST_F(FileSystemTest, testMkdiratEexist, Function | MediumTest | Level3)
308 {
309     const char *pathName = TOP_DIR "/" DIR0;
310     EXPECT_NE(mkdirat(AT_FDCWD, pathName, 0777), -1) << "> creat faild errno = " << errno;
311     EXPECT_EQ(access(pathName, F_OK), 0) << "> access F_OK errno = " << errno;
312     EXPECT_EQ(mkdirat(AT_FDCWD, pathName, 0777), -1) << "> should be error";
313     EXPECT_EQ(errno, EEXIST);
314 }
315 
316 #if defined(LITE_FS_JFFS2)
317 /**
318  * @tc.number   SUB_KERNEL_FS_STAT_0600
319  * @tc.name     basic function test : use chmod function to change mode
320  * @tc.desc     [C- SOFTWARE -0200]
321  */
322 HWTEST_F(FileSystemTest, testChmod, Function | MediumTest | Level3)
323 {
324     struct stat buf;
325     mode_t maskPre = umask(0);
326     const char *fileName = TOP_DIR "/" FILE0;
327     int fd = creat(FILE0, 0777);
328     EXPECT_NE(fd, -1) << "> creat faild errno = " << errno;
329     EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
330 
331     mode_t mode = 0666;
332     EXPECT_EQ(chmod(fileName, mode), 0);
333     EXPECT_NE(stat(fileName, &buf), -1) << "> fstat errno = " << errno;
334     EXPECT_EQ((buf.st_mode & 0777), mode) << "> check file permission faild";
335     umask(maskPre);
336 }
337 #endif
338