• 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 <errno.h>
17 #include <stdlib.h>
18 #include <sys/inotify.h>
19 #include "functionalext.h"
20 
21 #define TEST_BUFFER_SIZE 128
22 #define TEST_DATA_COUNT 10
23 
24 struct inotify_test {
25     int fd;
26     int wd;
27     uint32_t mask;
28 };
29 
inotify_init_test(void)30 static int inotify_init_test(void)
31 {
32     errno = 0;
33     return inotify_init();
34 }
35 
inotify_add_watch_test(int fd,uint32_t mask)36 static int inotify_add_watch_test(int fd, uint32_t mask)
37 {
38     errno = 0;
39     return inotify_add_watch(fd, "/data", mask);
40 }
41 
inotify_rm_watch_test(int fd,int wd)42 static int inotify_rm_watch_test(int fd, int wd)
43 {
44     errno = 0;
45     return inotify_rm_watch(fd, wd);
46 }
47 
inotify_test(const char * msg)48 static void inotify_test(const char *msg)
49 {
50     int fd = inotify_init_test();
51     EXPECT_NE(msg, fd, ERREXPECT);
52     if (fd == -1) {
53         return;
54     }
55 
56     int i, j;
57     struct inotify_test data[TEST_DATA_COUNT] = {{.fd = fd, .wd = -1, .mask = IN_ACCESS},
58         {.fd = fd, .wd = -1, .mask = IN_MODIFY},
59         {.fd = fd, .wd = -1, .mask = IN_ATTRIB},
60         {.fd = fd, .wd = -1, .mask = IN_CLOSE_WRITE},
61         {.fd = fd, .wd = -1, .mask = IN_CLOSE_NOWRITE},
62         {.fd = fd, .wd = -1, .mask = IN_OPEN},
63         {.fd = fd, .wd = -1, .mask = IN_MOVED_FROM},
64         {.fd = fd, .wd = -1, .mask = IN_MOVED_TO},
65         {.fd = fd, .wd = -1, .mask = IN_CREATE},
66         {.fd = fd, .wd = -1, .mask = IN_DELETE}};
67 
68     for (i = 0; i < TEST_DATA_COUNT; i++) {
69         data[i].wd = inotify_add_watch_test(fd, data[i].mask);
70         EXPECT_NE(msg, data[i].wd, ERREXPECT);
71     }
72 
73     for (i = 0; i < TEST_DATA_COUNT; i++) {
74         for (j = i; j >= 0; j--) {
75             if (data[j].wd != data[i].wd) {
76                 int ret = inotify_rm_watch_test(fd, data[i].wd);
77                 EXPECT_EQ(msg, ret, CMPFLAG);
78                 break;
79             }
80         }
81     }
82     close(fd);
83 }
84 
85 /**
86  * @tc.name      : inotify_init_0100
87  * @tc.desc      : initialize an inotify instance
88  * @tc.level     : Level 0
89  */
inotify_init_0100(void)90 void inotify_init_0100(void)
91 {
92     int fd = inotify_init_test();
93     EXPECT_NE("inotify_init_0100", fd, ERREXPECT);
94     EXPECT_EQ("inotify_init_0100", errno, CMPFLAG);
95     close(fd);
96 }
97 
98 /**
99  * @tc.name      : inotify_add_watch_0100
100  * @tc.desc      : add some watches to an initialized inotify instance
101  * @tc.level     : Level 0
102  */
inotify_add_watch_0100(void)103 void inotify_add_watch_0100(void)
104 {
105     inotify_test("inotify_add_watch_0100");
106 }
107 
108 /**
109  * @tc.name      : inotify_add_watch_0200
110  * @tc.desc      : Using the wrong fd, add some monitoring events to the inotify instance
111  * @tc.level     : Level 2
112  */
inotify_add_watch_0200(void)113 void inotify_add_watch_0200(void)
114 {
115     int i;
116     struct inotify_test data[TEST_DATA_COUNT] = {{.fd = -1, .wd = -1, .mask = IN_ACCESS},
117         {.fd = -1, .wd = -1, .mask = IN_MODIFY},
118         {.fd = -1, .wd = -1, .mask = IN_ATTRIB},
119         {.fd = -1, .wd = -1, .mask = IN_CLOSE_WRITE},
120         {.fd = -1, .wd = -1, .mask = IN_CLOSE_NOWRITE},
121         {.fd = -1, .wd = -1, .mask = IN_OPEN},
122         {.fd = -1, .wd = -1, .mask = IN_MOVED_FROM},
123         {.fd = -1, .wd = -1, .mask = IN_MOVED_TO},
124         {.fd = -1, .wd = -1, .mask = IN_CREATE},
125         {.fd = -1, .wd = -1, .mask = IN_DELETE}};
126 
127     for (i = 0; i < TEST_DATA_COUNT; i++) {
128         data[i].wd = inotify_add_watch_test(data[i].fd, data[i].mask);
129         EXPECT_EQ("inotify_add_watch_0100", data[i].wd, ERREXPECT);
130     }
131 }
132 
133 /**
134  * @tc.name      : inotify_rm_watch_0100
135  * @tc.desc      : Remove some monitoring events from the inotify instance
136  * @tc.level     : Level 0
137  */
inotify_rm_watch_0100(void)138 void inotify_rm_watch_0100(void)
139 {
140     inotify_test("inotify_rm_watch_0100");
141 }
142 
143 /**
144  * @tc.name      : inotify_rm_watch_0200
145  * @tc.desc      : Provide exception parameter to remove monitoring event from inotify instance
146  * @tc.level     : Level 2
147  */
inotify_rm_watch_0200(void)148 void inotify_rm_watch_0200(void)
149 {
150     int fd = inotify_init_test();
151     if (fd == -1) {
152         return;
153     }
154 
155     int ret = inotify_rm_watch(fd, -1);
156     EXPECT_EQ("inotify_rm_watch_0200", ret, ERREXPECT);
157     close(fd);
158 
159     ret = inotify_rm_watch(-1, -1);
160     EXPECT_EQ("inotify_rm_watch_0200", ret, ERREXPECT);
161 }
162 
main(void)163 int main(void)
164 {
165     inotify_init_0100();
166 
167     inotify_add_watch_0100();
168     inotify_add_watch_0200();
169 
170     inotify_rm_watch_0100();
171     inotify_rm_watch_0200();
172     return t_status;
173 }