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 <gtest/gtest.h>
25#include <sys/epoll.h>
26#include <sys/stat.h>
27#include <sys/mman.h>
28#include <sys/syscall.h>
29#include <sys/types.h>
30#include "securec.h"
31
32using namespace testing::ext;
33
34class HatsEpollCtlTest : public testing::Test {
35public:
36    static void SetUpTestCase();
37    static void TearDownTestCase();
38    void SetUp();
39    void TearDown();
40private:
41};
42void HatsEpollCtlTest::SetUp()
43{
44}
45
46void HatsEpollCtlTest::TearDown()
47{
48}
49
50void HatsEpollCtlTest::SetUpTestCase()
51{
52}
53
54void HatsEpollCtlTest::TearDownTestCase()
55{
56}
57
58/*
59 * @tc.number : SUB_KERNEL_SYSCALL_EPOLLCTL_0100
60 * @tc.name   : EpollCtlModeTestSuccess_0001
61 * @tc.desc   : epoll_ctl mode EPOLL_CTL_ADDT/EPOLL_CTL_MOD/EPOLL_CTL_DEL test success.
62 * @tc.size   : MediumTest
63 * @tc.type   : Function
64 * @tc.level  : Level 1
65 */
66HWTEST_F(HatsEpollCtlTest, EpollCtlModeTestSuccess_0001, Function | MediumTest | Level1)
67{
68    int fds[2];
69    const uint64_t expected = 0x11223344;
70
71    int fd = epoll_create(1);
72    EXPECT_TRUE(fd > 0);
73
74    int ret = pipe(fds);
75    EXPECT_EQ(ret, 0);
76
77    struct epoll_event ev;
78    ev.events = EPOLLIN;
79    ev.data.u64 = expected;
80
81    ret = epoll_ctl(fd, EPOLL_CTL_ADD, fds[0], &ev);
82    EXPECT_EQ(ret, 0);
83
84    ret = epoll_ctl(fd, EPOLL_CTL_MOD, fds[0], &ev);
85    EXPECT_EQ(ret, 0);
86
87    ret = epoll_ctl(fd, EPOLL_CTL_DEL, fds[0], &ev);
88    EXPECT_EQ(ret, 0);
89
90    close(fds[0]);
91    close(fds[1]);
92    close(fd);
93}
94
95/*
96 * @tc.number : SUB_KERNEL_SYSCALL_EPOLLCTL_0200
97 * @tc.name   : EpollCtlUseInvalidFdTestFailed_0002
98 * @tc.desc   : epoll_ctl use invalid fd test failed.
99 * @tc.size   : MediumTest
100 * @tc.type   : Function
101 * @tc.level  : Level 2
102 */
103HWTEST_F(HatsEpollCtlTest, EpollCtlUseInvalidFdTestFailed_0002, Function | MediumTest | Level2)
104{
105    int fds[2];
106    const uint64_t expected = 0x11223344;
107
108    int ret = pipe(fds);
109    EXPECT_EQ(ret, 0);
110
111    struct epoll_event ev;
112    ev.events = EPOLLIN;
113    ev.data.u64 = expected;
114
115    errno = 0;
116    ret = epoll_ctl(-1, EPOLL_CTL_ADD, fds[0], &ev);
117    EXPECT_EQ(ret, -1);
118    EXPECT_EQ(errno, EBADF);
119
120    close(fds[0]);
121    close(fds[1]);
122}