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