1570af302Sopenharmony_ci/**
2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci *   http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#include <errno.h>
17570af302Sopenharmony_ci#include <stdlib.h>
18570af302Sopenharmony_ci#include <sys/inotify.h>
19570af302Sopenharmony_ci#include "functionalext.h"
20570af302Sopenharmony_ci
21570af302Sopenharmony_ci#define TEST_BUFFER_SIZE 128
22570af302Sopenharmony_ci#define TEST_DATA_COUNT 10
23570af302Sopenharmony_ci
24570af302Sopenharmony_cistruct inotify_test {
25570af302Sopenharmony_ci    int fd;
26570af302Sopenharmony_ci    int wd;
27570af302Sopenharmony_ci    uint32_t mask;
28570af302Sopenharmony_ci};
29570af302Sopenharmony_ci
30570af302Sopenharmony_cistatic int inotify_init_test(void)
31570af302Sopenharmony_ci{
32570af302Sopenharmony_ci    errno = 0;
33570af302Sopenharmony_ci    return inotify_init();
34570af302Sopenharmony_ci}
35570af302Sopenharmony_ci
36570af302Sopenharmony_cistatic int inotify_add_watch_test(int fd, uint32_t mask)
37570af302Sopenharmony_ci{
38570af302Sopenharmony_ci    errno = 0;
39570af302Sopenharmony_ci    return inotify_add_watch(fd, "/data", mask);
40570af302Sopenharmony_ci}
41570af302Sopenharmony_ci
42570af302Sopenharmony_cistatic int inotify_rm_watch_test(int fd, int wd)
43570af302Sopenharmony_ci{
44570af302Sopenharmony_ci    errno = 0;
45570af302Sopenharmony_ci    return inotify_rm_watch(fd, wd);
46570af302Sopenharmony_ci}
47570af302Sopenharmony_ci
48570af302Sopenharmony_cistatic void inotify_test(const char *msg)
49570af302Sopenharmony_ci{
50570af302Sopenharmony_ci    int fd = inotify_init_test();
51570af302Sopenharmony_ci    EXPECT_NE(msg, fd, ERREXPECT);
52570af302Sopenharmony_ci    if (fd == -1) {
53570af302Sopenharmony_ci        return;
54570af302Sopenharmony_ci    }
55570af302Sopenharmony_ci
56570af302Sopenharmony_ci    int i, j;
57570af302Sopenharmony_ci    struct inotify_test data[TEST_DATA_COUNT] = {{.fd = fd, .wd = -1, .mask = IN_ACCESS},
58570af302Sopenharmony_ci        {.fd = fd, .wd = -1, .mask = IN_MODIFY},
59570af302Sopenharmony_ci        {.fd = fd, .wd = -1, .mask = IN_ATTRIB},
60570af302Sopenharmony_ci        {.fd = fd, .wd = -1, .mask = IN_CLOSE_WRITE},
61570af302Sopenharmony_ci        {.fd = fd, .wd = -1, .mask = IN_CLOSE_NOWRITE},
62570af302Sopenharmony_ci        {.fd = fd, .wd = -1, .mask = IN_OPEN},
63570af302Sopenharmony_ci        {.fd = fd, .wd = -1, .mask = IN_MOVED_FROM},
64570af302Sopenharmony_ci        {.fd = fd, .wd = -1, .mask = IN_MOVED_TO},
65570af302Sopenharmony_ci        {.fd = fd, .wd = -1, .mask = IN_CREATE},
66570af302Sopenharmony_ci        {.fd = fd, .wd = -1, .mask = IN_DELETE}};
67570af302Sopenharmony_ci
68570af302Sopenharmony_ci    for (i = 0; i < TEST_DATA_COUNT; i++) {
69570af302Sopenharmony_ci        data[i].wd = inotify_add_watch_test(fd, data[i].mask);
70570af302Sopenharmony_ci        EXPECT_NE(msg, data[i].wd, ERREXPECT);
71570af302Sopenharmony_ci    }
72570af302Sopenharmony_ci
73570af302Sopenharmony_ci    for (i = 0; i < TEST_DATA_COUNT; i++) {
74570af302Sopenharmony_ci        for (j = i; j >= 0; j--) {
75570af302Sopenharmony_ci            if (data[j].wd != data[i].wd) {
76570af302Sopenharmony_ci                int ret = inotify_rm_watch_test(fd, data[i].wd);
77570af302Sopenharmony_ci                EXPECT_EQ(msg, ret, CMPFLAG);
78570af302Sopenharmony_ci                break;
79570af302Sopenharmony_ci            }
80570af302Sopenharmony_ci        }
81570af302Sopenharmony_ci    }
82570af302Sopenharmony_ci    close(fd);
83570af302Sopenharmony_ci}
84570af302Sopenharmony_ci
85570af302Sopenharmony_ci/**
86570af302Sopenharmony_ci * @tc.name      : inotify_init_0100
87570af302Sopenharmony_ci * @tc.desc      : initialize an inotify instance
88570af302Sopenharmony_ci * @tc.level     : Level 0
89570af302Sopenharmony_ci */
90570af302Sopenharmony_civoid inotify_init_0100(void)
91570af302Sopenharmony_ci{
92570af302Sopenharmony_ci    int fd = inotify_init_test();
93570af302Sopenharmony_ci    EXPECT_NE("inotify_init_0100", fd, ERREXPECT);
94570af302Sopenharmony_ci    EXPECT_EQ("inotify_init_0100", errno, CMPFLAG);
95570af302Sopenharmony_ci    close(fd);
96570af302Sopenharmony_ci}
97570af302Sopenharmony_ci
98570af302Sopenharmony_ci/**
99570af302Sopenharmony_ci * @tc.name      : inotify_add_watch_0100
100570af302Sopenharmony_ci * @tc.desc      : add some watches to an initialized inotify instance
101570af302Sopenharmony_ci * @tc.level     : Level 0
102570af302Sopenharmony_ci */
103570af302Sopenharmony_civoid inotify_add_watch_0100(void)
104570af302Sopenharmony_ci{
105570af302Sopenharmony_ci    inotify_test("inotify_add_watch_0100");
106570af302Sopenharmony_ci}
107570af302Sopenharmony_ci
108570af302Sopenharmony_ci/**
109570af302Sopenharmony_ci * @tc.name      : inotify_add_watch_0200
110570af302Sopenharmony_ci * @tc.desc      : Using the wrong fd, add some monitoring events to the inotify instance
111570af302Sopenharmony_ci * @tc.level     : Level 2
112570af302Sopenharmony_ci */
113570af302Sopenharmony_civoid inotify_add_watch_0200(void)
114570af302Sopenharmony_ci{
115570af302Sopenharmony_ci    int i;
116570af302Sopenharmony_ci    struct inotify_test data[TEST_DATA_COUNT] = {{.fd = -1, .wd = -1, .mask = IN_ACCESS},
117570af302Sopenharmony_ci        {.fd = -1, .wd = -1, .mask = IN_MODIFY},
118570af302Sopenharmony_ci        {.fd = -1, .wd = -1, .mask = IN_ATTRIB},
119570af302Sopenharmony_ci        {.fd = -1, .wd = -1, .mask = IN_CLOSE_WRITE},
120570af302Sopenharmony_ci        {.fd = -1, .wd = -1, .mask = IN_CLOSE_NOWRITE},
121570af302Sopenharmony_ci        {.fd = -1, .wd = -1, .mask = IN_OPEN},
122570af302Sopenharmony_ci        {.fd = -1, .wd = -1, .mask = IN_MOVED_FROM},
123570af302Sopenharmony_ci        {.fd = -1, .wd = -1, .mask = IN_MOVED_TO},
124570af302Sopenharmony_ci        {.fd = -1, .wd = -1, .mask = IN_CREATE},
125570af302Sopenharmony_ci        {.fd = -1, .wd = -1, .mask = IN_DELETE}};
126570af302Sopenharmony_ci
127570af302Sopenharmony_ci    for (i = 0; i < TEST_DATA_COUNT; i++) {
128570af302Sopenharmony_ci        data[i].wd = inotify_add_watch_test(data[i].fd, data[i].mask);
129570af302Sopenharmony_ci        EXPECT_EQ("inotify_add_watch_0100", data[i].wd, ERREXPECT);
130570af302Sopenharmony_ci    }
131570af302Sopenharmony_ci}
132570af302Sopenharmony_ci
133570af302Sopenharmony_ci/**
134570af302Sopenharmony_ci * @tc.name      : inotify_rm_watch_0100
135570af302Sopenharmony_ci * @tc.desc      : Remove some monitoring events from the inotify instance
136570af302Sopenharmony_ci * @tc.level     : Level 0
137570af302Sopenharmony_ci */
138570af302Sopenharmony_civoid inotify_rm_watch_0100(void)
139570af302Sopenharmony_ci{
140570af302Sopenharmony_ci    inotify_test("inotify_rm_watch_0100");
141570af302Sopenharmony_ci}
142570af302Sopenharmony_ci
143570af302Sopenharmony_ci/**
144570af302Sopenharmony_ci * @tc.name      : inotify_rm_watch_0200
145570af302Sopenharmony_ci * @tc.desc      : Provide exception parameter to remove monitoring event from inotify instance
146570af302Sopenharmony_ci * @tc.level     : Level 2
147570af302Sopenharmony_ci */
148570af302Sopenharmony_civoid inotify_rm_watch_0200(void)
149570af302Sopenharmony_ci{
150570af302Sopenharmony_ci    int fd = inotify_init_test();
151570af302Sopenharmony_ci    if (fd == -1) {
152570af302Sopenharmony_ci        return;
153570af302Sopenharmony_ci    }
154570af302Sopenharmony_ci
155570af302Sopenharmony_ci    int ret = inotify_rm_watch(fd, -1);
156570af302Sopenharmony_ci    EXPECT_EQ("inotify_rm_watch_0200", ret, ERREXPECT);
157570af302Sopenharmony_ci    close(fd);
158570af302Sopenharmony_ci
159570af302Sopenharmony_ci    ret = inotify_rm_watch(-1, -1);
160570af302Sopenharmony_ci    EXPECT_EQ("inotify_rm_watch_0200", ret, ERREXPECT);
161570af302Sopenharmony_ci}
162570af302Sopenharmony_ci
163570af302Sopenharmony_ciint main(void)
164570af302Sopenharmony_ci{
165570af302Sopenharmony_ci    inotify_init_0100();
166570af302Sopenharmony_ci
167570af302Sopenharmony_ci    inotify_add_watch_0100();
168570af302Sopenharmony_ci    inotify_add_watch_0200();
169570af302Sopenharmony_ci
170570af302Sopenharmony_ci    inotify_rm_watch_0100();
171570af302Sopenharmony_ci    inotify_rm_watch_0200();
172570af302Sopenharmony_ci    return t_status;
173570af302Sopenharmony_ci}