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