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 typedef void (*TEST_FUN)();
21
22 /**
23 * @tc.name : fchmod_0100
24 * @tc.desc : Verify the permission to modify the file (parameters are set separately)
25 * @tc.level : Level 0
26 */
fchmod_0100null27 void fchmod_0100()
28 {
29 struct stat buf;
30 const char *path = "/data/test.txt";
31 int fd = open(path, O_RDWR | O_CREAT, TEST_MODE);
32 EXPECT_NE("fchmod_0100", fd, -1);
33
34 int result = fchmod(fd,
35 S_IRUSR | S_ISGID | S_ISVTX | S_IWUSR | S_IROTH | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH |
36 S_IWOTH | S_IXOTH);
37 stat(path, &buf);
38 EXPECT_EQ("fchmod_0100", result, 0);
39 EXPECT_EQ("fchmod_0100", buf.st_mode, 34815);
40 close(fd);
41 remove(path);
42 }
43
44 /**
45 * @tc.name : fchmod_0200
46 * @tc.desc : Verify permission to modify files (overall parameter settings)
47 * @tc.level : Level 0
48 */
fchmod_0200null49 void fchmod_0200()
50 {
51 struct stat buf;
52 const char *path = "/data/test.txt";
53 int fd = open(path, O_RDWR | O_CREAT, TEST_MODE);
54 int result = fchmod(fd, S_IRWXU | S_IRWXG | S_IRWXO);
55 stat(path, &buf);
56 EXPECT_EQ("fchmod_0200", result, 0);
57 EXPECT_EQ("fchmod_0200", buf.st_mode, 33279);
58 close(fd);
59 remove(path);
60 }
61
62 /**
63 * @tc.name : fchmod_0300
64 * @tc.desc : Failed to verify permission to modify file (fd parameter is invalid)
65 * @tc.level : Level 2
66 */
fchmod_0300null67 void fchmod_0300()
68 {
69 int result = fchmod(-1, S_IRWXU | S_IRWXG | S_IRWXO);
70 EXPECT_EQ("fchmod_0300", result, -1);
71 }
72
73 TEST_FUN G_Fun_Array[] = {
74 fchmod_0100,
75 fchmod_0200,
76 fchmod_0300,
77 };
78
main(int argc, char *argv[])79 int main(int argc, char *argv[])
80 {
81 int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
82 for (int pos = 0; pos < num; ++pos) {
83 G_Fun_Array[pos]();
84 }
85
86 return t_status;
87 }