1 /*
2 * Copyright (c) 2022 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 <unistd.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include "functionalext.h"
20 #include "filepath_util.h"
21
22 const int SUCCESS = 0;
23
24 extern int __fstatat_time64(int, const char *__restrict, struct stat *__restrict, int);
25
26 /**
27 * @tc.name : fstatat_0100
28 * @tc.desc : The parameter fd is equal to AT_FDCWD, the flag is 0,
29 * and the information of the file can be obtained.
30 * @tc.level : Level 0
31 */
fstatat_0100(void)32 void fstatat_0100(void)
33 {
34 const char *ptr = "fstatattest.txt";
35 struct stat st;
36 int fd = open(ptr, O_RDWR | O_CREAT, TEST_MODE);
37 EXPECT_TRUE("fstatat_0100", fd >= 0);
38 lseek(fd, 0, SEEK_SET);
39 int ret = fstatat(AT_FDCWD, ptr, &st, 0);
40 EXPECT_EQ("fstatat_0100", ret, SUCCESS);
41 uid_t uid = getuid();
42 EXPECT_EQ("fstatat_0100", st.st_uid, uid);
43 gid_t gid = getgid();
44 EXPECT_EQ("fstatat_0100", st.st_gid, gid);
45 close(fd);
46 remove(ptr);
47 ptr = NULL;
48 }
49
50 /**
51 * @tc.name : fstatat_0200
52 * @tc.desc : The parameter fd is equal to AT_FDCWD, the flag is AT_SYMLINK_NOFOLLOW,
53 * and the information of the file can be obtained.
54 * @tc.level : Level 0
55 */
fstatat_0200(void)56 void fstatat_0200(void)
57 {
58 const char *ptr = "fstatattest.txt";
59 struct stat st;
60 int fd = open(ptr, O_RDWR | O_CREAT, TEST_MODE);
61 EXPECT_TRUE("fstatat_0200", fd >= 0);
62 lseek(fd, 0, SEEK_SET);
63 const char *softptr = "./fstatattest.txt.soft";
64 int link = symlink(ptr, softptr);
65 EXPECT_EQ("fstatat_0200", link, 0);
66 int ret = fstatat(AT_FDCWD, softptr, &st, AT_SYMLINK_NOFOLLOW);
67 EXPECT_EQ("fstatat_0200", ret, SUCCESS);
68 uid_t uid = getuid();
69 EXPECT_EQ("fstatat_0200", st.st_uid, uid);
70 gid_t gid = getgid();
71 EXPECT_EQ("fstatat_0200", st.st_gid, gid);
72 EXPECT_TRUE("fstatat_0200", st.st_mode);
73 close(fd);
74 remove(softptr);
75 remove(ptr);
76 softptr = NULL;
77 ptr = NULL;
78 }
79
80 /**
81 * @tc.name : fstatat_0300
82 * @tc.desc : The parameter fd is equal to the return value of opening the test file, the flag is 0,
83 * and the information of the file can be obtained.
84 * @tc.level : Level 0
85 */
fstatat_0300(void)86 void fstatat_0300(void)
87 {
88 char ptr[PATH_MAX] = {0};
89 FILE_ABSOLUTE_PATH("fstatattest.txt", ptr);
90
91 struct stat st;
92 int fd = open(ptr, O_RDWR | O_CREAT, TEST_MODE);
93 EXPECT_TRUE("fstatat_0300", fd >= 0);
94 lseek(fd, 0, SEEK_SET);
95 int ret = fstatat(0, ptr, &st, 0);
96 EXPECT_EQ("fstatat_0300", ret, SUCCESS);
97 uid_t uid = getuid();
98 EXPECT_EQ("fstatat_0300", st.st_uid, uid);
99 gid_t gid = getgid();
100 EXPECT_EQ("fstatat_0300", st.st_gid, gid);
101 close(fd);
102 remove(ptr);
103 }
104
105 /**
106 * @tc.name : fstatat_time64_0100
107 * @tc.desc : The parameter fd is equal to AT_FDCWD, the flag is 0,
108 * and the information of the file can be obtained.
109 * @tc.level : Level 0
110 */
fstatat_time64_0100(void)111 void fstatat_time64_0100(void)
112 {
113 const char *ptr = "fstatat_time64_test.txt";
114 struct stat st;
115 int fd = open(ptr, O_RDWR | O_CREAT, TEST_MODE);
116 EXPECT_TRUE("fstatat_0100", fd >= 0);
117 lseek(fd, 0, SEEK_SET);
118 int ret = __fstatat_time64(AT_FDCWD, ptr, &st, 0);
119 EXPECT_EQ("fstatat_0100", ret, SUCCESS);
120 uid_t uid = getuid();
121 EXPECT_EQ("fstatat_0100", st.st_uid, uid);
122 gid_t gid = getgid();
123 EXPECT_EQ("fstatat_0100", st.st_gid, gid);
124 close(fd);
125 remove(ptr);
126 ptr = NULL;
127 }
128
main(int argc,char * argv[])129 int main(int argc, char *argv[])
130 {
131 fstatat_0100();
132 fstatat_0200();
133 fstatat_0300();
134 fstatat_time64_0100();
135 return t_status;
136 }