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 <fcntl.h>
17 #include <sys/stat.h>
18 #include "functionalext.h"
19
20 int success = 36863;
21 int SUCCESS = 33279;
22 int failed = -1;
23
24 /**
25 * @tc.name : fchmodat_0100
26 * @tc.desc : Verify the permission to modify the file (parameters are set separately)
27 * @tc.level : Level 0
28 */
fchmodat_0100(void)29 void fchmodat_0100(void)
30 {
31 int fd;
32 struct stat buf;
33 fd = open("/data/tests/libc-test/src/functionalext/supplement/stat/test.txt", O_RDWR | O_CREAT);
34 int result = fchmodat(fd,
35 "/data/tests/libc-test/src/functionalext/supplement/stat/test.txt",
36 S_ISUID | S_ISGID | S_ISVTX | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH |
37 S_IXOTH,
38 AT_SYMLINK_NOFOLLOW);
39 stat("/data/tests/libc-test/src/functionalext/supplement/stat/test.txt", &buf);
40 EXPECT_EQ("fchmodat_0100", result, 0);
41 EXPECT_EQ("fchmodat_0100", buf.st_mode, success);
42 close(fd);
43 remove("/data/tests/libc-test/src/functionalext/supplement/stat/test.txt");
44 }
45
46 /**
47 * @tc.name : fchmod_0200
48 * @tc.desc : Verify permission to modify files (overall parameter settings)
49 * @tc.level : Level 0
50 */
fchmodat_0200(void)51 void fchmodat_0200(void)
52 {
53 int fd;
54 struct stat buf;
55 fd = open("/data/tests/libc-test/src/functionalext/supplement/stat/test.txt", O_RDWR | O_CREAT);
56 int result = fchmodat(fd,
57 "/data/tests/libc-test/src/functionalext/supplement/stat/test.txt",
58 S_IRWXU | S_IRWXG | S_IRWXO,
59 AT_SYMLINK_NOFOLLOW);
60 stat("/data/tests/libc-test/src/functionalext/supplement/stat/test.txt", &buf);
61 EXPECT_EQ("fchmodat_0200", result, 0);
62 EXPECT_EQ("fchmodat_0200", buf.st_mode, SUCCESS);
63 close(fd);
64 remove("/data/tests/libc-test/src/functionalext/supplement/stat/test.txt");
65 }
66
67 /**
68 * @tc.name : fchmod_0300
69 * @tc.desc : Failed to verify permission to modify file (fd parameter is invalid)
70 * @tc.level : Level 2
71 */
fchmodat_0300(void)72 void fchmodat_0300(void)
73 {
74 int result = fchmodat(-1, "test.txt", S_IRWXU | S_IRWXG | S_IRWXO, AT_SYMLINK_NOFOLLOW);
75 EXPECT_EQ("fchmodat_0300", result, failed);
76 }
77
main(int argc,char * argv[])78 int main(int argc, char *argv[])
79 {
80 fchmodat_0100();
81 fchmodat_0200();
82 fchmodat_0300();
83
84 return t_status;
85 }