• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 HiHope Open Source Organization.
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 <cerrno>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <csignal>
20 #include <string>
21 #include <vector>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <malloc.h>
25 #include <arpa/inet.h>
26 #include <gtest/gtest.h>
27 #include <netinet/in.h>
28 #include <sys/stat.h>
29 #include <sys/mman.h>
30 #include <sys/inotify.h>
31 #include <sys/socket.h>
32 #include <sys/syscall.h>
33 #include <sys/types.h>
34 #include "securec.h"
35 
36 using namespace testing::ext;
37 static const char* TEST_FILE = "/data/local/tmp/test_notify.txt";
38 static const char* TEST_DIR = "/data/local/tmp/notify";
39 
40 
41 class HatsInotifyTest : public testing::Test {
42 public:
43     static void SetUpTestCase();
44     static void TearDownTestCase();
45     void SetUp();
46     void TearDown();
47 private:
48 };
SetUp()49 void HatsInotifyTest::SetUp()
50 {
51 }
52 
TearDown()53 void HatsInotifyTest::TearDown()
54 {
55 }
56 
SetUpTestCase()57 void HatsInotifyTest::SetUpTestCase()
58 {
59 }
60 
TearDownTestCase()61 void HatsInotifyTest::TearDownTestCase()
62 {
63     unlink(TEST_FILE);
64     rmdir(TEST_DIR);
65 }
66 
67 /*
68  * @tc.number : SUB_KERNEL_SYSCALL_INOTIFY_0100
69  * @tc.name   : InotifyAddWatchAccessSuccess_0001
70  * @tc.desc   : Inotify adds watch with mask IN_ACCESS.
71  * @tc.size   : MediumTest
72  * @tc.type   : Function
73  * @tc.level  : Level 1
74  */
75 HWTEST_F(HatsInotifyTest, InotifyAddWatchAccessSuccess_0001, Function | MediumTest | Level1)
76 {
77     int fdTest = open(TEST_FILE, O_RDWR | O_CREAT, 0666);
78     EXPECT_TRUE(fdTest > 0);
79 
80     int fd = inotify_init1(0);
81     EXPECT_TRUE(fd >= 0);
82 
83     int wd = inotify_add_watch(fd, TEST_FILE, IN_ACCESS);
84     EXPECT_TRUE(wd >= 0);
85 
86     int ret = inotify_rm_watch(fd, wd);
87     EXPECT_EQ(ret, 0);
88 
89     close(fd);
90 }
91 
92 /*
93  * @tc.number : SUB_KERNEL_SYSCALL_INOTIFY_0200
94  * @tc.name   : InotifyAddWatchModifySuccess_0002
95  * @tc.desc   : Inotify adds watch with mask IN_MODIFY、IN_ATTRIB、IN_CLOSE.
96  * @tc.size   : MediumTest
97  * @tc.type   : Function
98  * @tc.level  : Level 1
99  */
100 HWTEST_F(HatsInotifyTest, InotifyAddWatchModifySuccess_0002, Function | MediumTest | Level1)
101 {
102     int fdTest = open(TEST_FILE, O_RDWR | O_CREAT, 0666);
103     EXPECT_TRUE(fdTest > 0);
104 
105     int fd = inotify_init1(IN_NONBLOCK);
106     EXPECT_TRUE(fd >= 0);
107 
108     int wd = inotify_add_watch(fd, TEST_FILE, IN_MODIFY | IN_ATTRIB | IN_CLOSE);
109     EXPECT_TRUE(wd >= 0);
110 
111     int ret = inotify_rm_watch(fd, wd);
112     EXPECT_EQ(ret, 0);
113 
114     close(fd);
115 }
116 
117 /*
118  * @tc.number : SUB_KERNEL_SYSCALL_INOTIFY_0300
119  * @tc.name   : InotifyAddWatchMoveSuccess_0003
120  * @tc.desc   : Inotify adds watch with mask IN_MOVE、IN_OPEN、IN_CLOSE_NOWRITE.
121  * @tc.size   : MediumTest
122  * @tc.type   : Function
123  * @tc.level  : Level 1
124  */
125 HWTEST_F(HatsInotifyTest, InotifyAddWatchMoveSuccess_0003, Function | MediumTest | Level1)
126 {
127     int fdTest = open(TEST_FILE, O_RDWR | O_CREAT, 0666);
128     EXPECT_TRUE(fdTest > 0);
129 
130     int fd = inotify_init1(IN_CLOEXEC);
131     EXPECT_TRUE(fd >= 0);
132 
133     int wd = inotify_add_watch(fd, TEST_FILE, IN_MOVE | IN_OPEN | IN_CLOSE_NOWRITE);
134     EXPECT_TRUE(wd >= 0);
135 
136     int ret = inotify_rm_watch(fd, wd);
137     EXPECT_EQ(ret, 0);
138 
139     close(fd);
140 }
141 
142 /*
143  * @tc.number : SUB_KERNEL_SYSCALL_INOTIFY_0400
144  * @tc.name   : InotifyAddWatchCreateSuccess_0004
145  * @tc.desc   : Inotify adds watch with mask IN_CLOSE_WRITE、IN_CREATE.
146  * @tc.size   : MediumTest
147  * @tc.type   : Function
148  * @tc.level  : Level 1
149  */
150 HWTEST_F(HatsInotifyTest, InotifyAddWatchCreateSuccess_0004, Function | MediumTest | Level1)
151 {
152     int fdTest = open(TEST_FILE, O_RDWR | O_CREAT, 0666);
153     EXPECT_TRUE(fdTest > 0);
154 
155     int fd = inotify_init1(IN_CLOEXEC);
156     EXPECT_TRUE(fd >= 0);
157 
158     int wd = inotify_add_watch(fd, TEST_FILE, IN_CLOSE_WRITE | IN_CREATE);
159     EXPECT_TRUE(wd >= 0);
160 
161     int ret = inotify_rm_watch(fd, wd);
162     EXPECT_EQ(ret, 0);
163 
164     close(fd);
165 }
166 
167 /*
168  * @tc.number : SUB_KERNEL_SYSCALL_INOTIFY_0500
169  * @tc.name   : InotifyAddWatchMoveFromSuccess_0005
170  * @tc.desc   : Inotify adds watch with mask IN_MOVED_FROM、IN_DELETE_SELF、IN_MOVE_SELF.
171  * @tc.size   : MediumTest
172  * @tc.type   : Function
173  * @tc.level  : Level 1
174  */
175 HWTEST_F(HatsInotifyTest, InotifyAddWatchMoveFromSuccess_0005, Function | MediumTest | Level1)
176 {
177     int fdTest = open(TEST_FILE, O_RDWR | O_CREAT, 0666);
178     EXPECT_TRUE(fdTest > 0);
179 
180     int fd = inotify_init1(IN_CLOEXEC);
181     EXPECT_TRUE(fd >= 0);
182 
183     int wd = inotify_add_watch(fd, TEST_FILE, IN_MOVED_FROM | IN_DELETE_SELF | IN_MOVE_SELF);
184     EXPECT_TRUE(wd >= 0);
185 
186     int ret = inotify_rm_watch(fd, wd);
187     EXPECT_EQ(ret, 0);
188 
189     close(fd);
190 }
191 
192 /*
193  * @tc.number : SUB_KERNEL_SYSCALL_INOTIFY_0600
194  * @tc.name   : InotifyAddWatchMoveToSuccess_0006
195  * @tc.desc   : Inotify adds watch with mask IN_MOVED_TO、IN_UNMOUNT、IN_IGNORED、IN_Q_OVERFLOW.
196  * @tc.size   : MediumTest
197  * @tc.type   : Function
198  * @tc.level  : Level 1
199  */
200 HWTEST_F(HatsInotifyTest, InotifyAddWatchMoveToSuccess_0006, Function | MediumTest | Level1)
201 {
202     int fdTest = open(TEST_FILE, O_RDWR | O_CREAT, 0666);
203     EXPECT_TRUE(fdTest > 0);
204 
205     int fd = inotify_init1(IN_CLOEXEC);
206     EXPECT_TRUE(fd >= 0);
207 
208     int wd = inotify_add_watch(fd, TEST_FILE, IN_MOVED_TO | IN_UNMOUNT | IN_IGNORED | IN_Q_OVERFLOW);
209     EXPECT_TRUE(wd >= 0);
210 
211     int ret = inotify_rm_watch(fd, wd);
212     EXPECT_EQ(ret, 0);
213 
214     close(fd);
215 }
216 
217 /*
218  * @tc.number : SUB_KERNEL_SYSCALL_INOTIFY_0700
219  * @tc.name   : InotifyAddWatchDirSuccess_0007
220  * @tc.desc   : Inotify adds watch with mask IN_ONLYDIR、IN_DONT_FOLLOW、IN_EXCL_UNLINK.
221  * @tc.size   : MediumTest
222  * @tc.type   : Function
223  * @tc.level  : Level 1
224  */
225 HWTEST_F(HatsInotifyTest, InotifyAddWatchDirSuccess_0007, Function | MediumTest | Level1)
226 {
227     mkdir(TEST_DIR, 0777);
228 
229     int fd = inotify_init1(IN_CLOEXEC);
230     EXPECT_TRUE(fd >= 0);
231 
232     int wd = inotify_add_watch(fd, TEST_DIR, IN_ONLYDIR | IN_DONT_FOLLOW | IN_EXCL_UNLINK);
233     EXPECT_TRUE(wd >= 0);
234 
235     int ret = inotify_rm_watch(fd, wd);
236     EXPECT_EQ(ret, 0);
237 
238     close(fd);
239 }
240 
241 /*
242  * @tc.number : SUB_KERNEL_SYSCALL_INOTIFY_0800
243  * @tc.name   : InotifyAddWatchMaskAddSuccess_0008
244  * @tc.desc   : Inotify adds watch with mask IN_MASK_ADD、IN_ISDIR、IN_ONESHOT.
245  * @tc.size   : MediumTest
246  * @tc.type   : Function
247  * @tc.level  : Level 1
248  */
249 HWTEST_F(HatsInotifyTest, InotifyAddWatchMaskAddSuccess_0008, Function | MediumTest | Level1)
250 {
251     int fdTest = open(TEST_FILE, O_RDWR | O_CREAT, 0666);
252     EXPECT_TRUE(fdTest > 0);
253 
254     int fd = inotify_init1(IN_CLOEXEC);
255     EXPECT_TRUE(fd >= 0);
256 
257     int wd = inotify_add_watch(fd, TEST_FILE, IN_MASK_ADD | IN_ISDIR | IN_ONESHOT);
258     EXPECT_TRUE(wd >= 0);
259 
260     int ret = inotify_rm_watch(fd, wd);
261     EXPECT_EQ(ret, 0);
262 
263     close(fd);
264 }
265 
266 /*
267  * @tc.number : SUB_KERNEL_SYSCALL_INOTIFY_0900
268  * @tc.name   : InotifyAddWatchCloExecFailed_009
269  * @tc.desc   : Inotify adds watch with mask IN_CLOEXEC.
270  * @tc.size   : MediumTest
271  * @tc.type   : Function
272  * @tc.level  : Level 2
273  */
274 HWTEST_F(HatsInotifyTest, InotifyAddWatchCloExecFailed_009, Function | MediumTest | Level2)
275 {
276     int fd = inotify_init1(IN_CLOEXEC);
277     EXPECT_TRUE(fd >= 0);
278 
279     errno = 0;
280     int wd = inotify_add_watch(fd, nullptr, IN_DELETE);
281     EXPECT_EQ(wd, -1);
282     EXPECT_EQ(errno, EFAULT);
283 
284     close(fd);
285 }
286 
287 /*
288  * @tc.number : SUB_KERNEL_SYSCALL_INOTIFY_1000
289  * @tc.name   : InotifyAddWatchIN_CLOEXECTestSuccess_0010
290  * @tc.desc   : inotify_adds_watch with mask IN_CLOEXEC success.
291  * @tc.size   : MediumTest
292  * @tc.type   : Function
293  * @tc.level  : Level 1
294  */
295 HWTEST_F(HatsInotifyTest, InotifyAddWatchIN_CLOEXECTestSuccess_0010, Function | MediumTest | Level1)
296 {
297     int fdTest = open(TEST_FILE, O_RDWR | O_CREAT, 0666);
298     EXPECT_TRUE(fdTest > 0);
299 
300     int fd = inotify_init1(IN_CLOEXEC);
301     EXPECT_TRUE(fd >= 0);
302 
303     int wd = inotify_add_watch(fd, TEST_FILE, IN_MASK_ADD);
304     EXPECT_TRUE(wd >= 0);
305 
306     int ret = inotify_rm_watch(fd, wd);
307     EXPECT_EQ(ret, 0);
308 
309     close(fd);
310 }