• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdio.h>
18 #include <stdlib.h>
19 #include <sys/stat.h>
20 #include <stdint.h>
21 #include <signal.h>
22 #include <unistd.h>
23 #include "functionalext.h"
24 
25 typedef void (*TEST_FUN)();
26 const int COUNT_ZERO = 0;
27 const int COUNT_NEFATIVE = -1;
28 
29 /**
30  * @tc.name      : fchown_0100
31  * @tc.desc      : Parameter is valid and can change the user and group of the file's owner
32  * @tc.level     : Level 0
33  */
fchown_0100()34 void fchown_0100()
35 {
36     int fd = open("test.txt", O_RDONLY | O_CREAT);
37     int result = fchown(fd, 0, 0);
38     struct stat buff;
39     EXPECT_EQ("fchown_0100", result, COUNT_ZERO);
40     stat("test.txt", &buff);
41     EXPECT_EQ("fchown_0100", buff.st_uid, COUNT_ZERO);
42     close(fd);
43     remove("test.txt");
44 }
45 
46 /**
47  * @tc.name      : fchown_0200
48  * @tc.desc      : Parameter valid, can change the file owner of the user
49  * @tc.level     : Level 0
50  */
fchown_0200()51 void fchown_0200()
52 {
53     int fd = open("test.txt", O_RDONLY | O_CREAT);
54     int result = fchown(fd, 0, -1);
55     struct stat buff;
56     EXPECT_EQ("fchown_0200", result, COUNT_ZERO);
57     stat("test.txt", &buff);
58     EXPECT_EQ("fchown_0200", buff.st_uid, COUNT_ZERO);
59     close(fd);
60     remove("test.txt");
61 }
62 
63 /**
64  * @tc.name      : fchown_0300
65  * @tc.desc      : Parameter valid to change the owner group of the file
66  * @tc.level     : Level 0
67  */
fchown_0300()68 void fchown_0300()
69 {
70     int fd = open("test.txt", O_RDONLY | O_CREAT);
71     int result = fchown(fd, -1, 0);
72     struct stat buff;
73     EXPECT_EQ("fchown_0300", result, COUNT_ZERO);
74     stat("test.txt", &buff);
75     EXPECT_EQ("fchown_0300", buff.st_gid, COUNT_ZERO);
76     close(fd);
77     remove("test.txt");
78 }
79 
80 /**
81  * @tc.name      : fchown_0400
82  * @tc.desc      : The user and group of the file's owner cannot be changed
83  * @tc.level     : Level 2
84  */
fchown_0400()85 void fchown_0400()
86 {
87     int fd = open("test.txt", O_RDONLY | O_CREAT);
88     close(fd);
89     int result = fchown(fd, 0, 0);
90     EXPECT_EQ("fchown_0400", result, COUNT_NEFATIVE);
91     remove("test.txt");
92 }
93 
94 /**
95  * @tc.name      : fchown_0500
96  * @tc.desc      : The user and group of the file's owner cannot be changed
97  * @tc.level     : Level 2
98  */
fchown_0500()99 void fchown_0500()
100 {
101     int fd = open("test.txt", O_RDONLY | O_CREAT);
102     int result = fchown(-1, 0, 0);
103     EXPECT_EQ("fchown_0500", result, COUNT_NEFATIVE);
104     close(fd);
105     remove("test.txt");
106 }
107 
108 TEST_FUN G_Fun_Array[] = {
109     fchown_0100,
110     fchown_0200,
111     fchown_0300,
112     fchown_0400,
113     fchown_0500,
114 };
115 
main(int argc,char * argv[])116 int main(int argc, char *argv[])
117 {
118     int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
119     for (int pos = 0; pos < num; ++pos) {
120         G_Fun_Array[pos]();
121     }
122 
123     return t_status;
124 }