• 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 "functionalext.h"
19 
20 /**
21  * @tc.name      : chmod_0100
22  * @tc.desc      : Verify the permission to modify the file (the mode parameter is set separately)
23  * @tc.level     : Level 0
24  */
chmod_0100(void)25 void chmod_0100(void)
26 {
27     struct stat buf;
28     open("test.txt", O_RDWR | O_CREAT);
29     int result = chmod("test.txt",
30         S_IRUSR | S_ISGID | S_ISVTX | S_IWUSR | S_IROTH | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH |
31             S_IWOTH | S_IXOTH);
32     stat("test.txt", &buf);
33     EXPECT_EQ("chmod_0100", result, 0);
34     EXPECT_EQ("chmod_0100", buf.st_mode, 34815);
35     remove("test.txt");
36 }
37 
38 /*
39  * @tc.name      : chmod_0200
40  * @tc.desc      : Verify the permission to modify the file (the overall setting of the mode parameter)
41  * @tc.level     : Level 2
42  */
chmod_0200(void)43 void chmod_0200(void)
44 {
45 
46     struct stat buf;
47     open("test.txt", O_RDWR | O_CREAT);
48     int result = chmod("test.txt", S_IRWXU | S_IRWXG | S_IRWXO);
49     stat("test.txt", &buf);
50     EXPECT_EQ("chmod_0200", result, 0);
51     EXPECT_EQ("chmod_0200", buf.st_mode, 33279);
52     remove("test.txt");
53 }
54 
55 /*
56  * @tc.name      : chmod_0300
57  * @tc.desc      : Verify that file permissions cannot be modified (parameter invalid)
58  * @tc.level     : Level 2
59  */
chmod_0300(void)60 void chmod_0300(void)
61 {
62     int result = chmod("data/AAA.txt",
63         S_ISUID | S_ISGID | S_ISVTX | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH |
64             S_IXOTH);
65     EXPECT_EQ("chmod_0300", result, -1);
66 }
67 
main(int argc,char * argv[])68 int main(int argc, char *argv[])
69 {
70     chmod_0100();
71     chmod_0200();
72     chmod_0300();
73     return t_status;
74 }