• 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 <sys/stat.h>
18 #include <unistd.h>
19 #include "functionalext.h"
20 
21 /**
22  * @tc.name      : chown_0100
23  * @tc.desc      : Verify the owner user and group of the changed file
24  * @tc.level     : Level 0
25  */
chown_0100(void)26 void chown_0100(void)
27 {
28     struct stat buf;
29     const char *ptr = "/data/test.txt";
30     FILE *fp = fopen(ptr, "w+");
31     EXPECT_PTRNE("chown_0100", fp, NULL);
32 
33     int result = chown(ptr, 0, 0);
34     int ret = stat(ptr, &buf);
35     EXPECT_EQ("chown_0100", ret, 0);
36     EXPECT_EQ("chown_0100", result, 0);
37     EXPECT_EQ("chown_0100", buf.st_uid, 0);
38     EXPECT_EQ("chown_0100", buf.st_gid, 0);
39 
40     fclose(fp);
41     remove(ptr);
42 }
43 
44 /**
45  * @tc.name      : chown_0200
46  * @tc.desc      : Verify the owner user of the changed file
47  * @tc.level     : Level 0
48  */
chown_0200(void)49 void chown_0200(void)
50 {
51     struct stat buf;
52     const char *ptr = "/data/test.txt";
53     FILE *fp = fopen(ptr, "w+");
54     EXPECT_PTRNE("chown_0200", fp, NULL);
55 
56     int result = chown(ptr, 0, -1);
57     int ret = stat(ptr, &buf);
58     EXPECT_EQ("chown_0200", ret, 0);
59     EXPECT_EQ("chown_0200", result, 0);
60     EXPECT_EQ("chown_0200", buf.st_uid, 0);
61     printf("0200: %d\n", buf.st_gid);
62     fclose(fp);
63     remove(ptr);
64 }
65 
66 /**
67  * @tc.name      : chown_0300
68  * @tc.desc      : Verify the group that changed the owner of the file
69  * @tc.level     : Level 0
70  */
chown_0300(void)71 void chown_0300(void)
72 {
73     struct stat buf;
74     const char *ptr = "/data/test.txt";
75     FILE *fp = fopen(ptr, "w+");
76     EXPECT_PTRNE("chown_0300", fp, NULL);
77 
78     int result = chown(ptr, -1, 0);
79     int ret = stat(ptr, &buf);
80     EXPECT_EQ("chown_0300", ret, 0);
81     EXPECT_EQ("chown_0300", result, 0);
82     EXPECT_EQ("chown_0300", buf.st_gid, 0);
83     printf("0300: %d\n", buf.st_uid);
84 
85     fclose(fp);
86     remove(ptr);
87 }
88 
89 /**
90  * @tc.name      : chown_0400
91  * @tc.desc      : Verify that the user and group cannot change the owner of the file (parameter path is invalid)
92  * @tc.level     : Level 2
93  */
chown_0400(void)94 void chown_0400(void)
95 {
96     int result = chown("/data/test.txt", 0, 0);
97     EXPECT_EQ("chown_0400", result, -1);
98 }
99 
100 /**
101  * @tc.name      : chown_0500
102  * @tc.desc      : Verify user and group that cannot change the owner of the file (parameter path is NULL)
103  * @tc.level     : Level 2
104  */
chown_0500(void)105 void chown_0500(void)
106 {
107     int result = chown(NULL, 0, 0);
108     EXPECT_EQ("chown_0500", result, -1);
109 }
110 
main(int argc,char * argv[])111 int main(int argc, char *argv[])
112 {
113     chown_0100();
114     chown_0200();
115     chown_0300();
116     chown_0400();
117     chown_0500();
118     return t_status;
119 }