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 <unistd.h>
20 #include <gtest/gtest.h>
21 #include <sys/eventfd.h>
22 #include <sys/syscall.h>
23 
24 using namespace testing::ext;
25 
26 class HatsEventfd2Test : public testing::Test {
27 public:
28     static void SetUpTestCase();
29     static void TearDownTestCase();
30     void SetUp();
31     void TearDown();
32 private:
33 };
SetUp()34 void HatsEventfd2Test::SetUp()
35 {
36 }
37 
TearDown()38 void HatsEventfd2Test::TearDown()
39 {
40 }
41 
SetUpTestCase()42 void HatsEventfd2Test::SetUpTestCase()
43 {
44 }
45 
TearDownTestCase()46 void HatsEventfd2Test::TearDownTestCase()
47 {
48 }
49 
50 /*
51  * @tc.number : SUB_KERNEL_SYSCALL_EVENTFD2_0100
52  * @tc.name   : Eventfd2CreateFdSuccess_0001
53  * @tc.desc   : eventfd2 create fd use EFD_NONBLOCK/EFD_SEMAPHORE success.
54  * @tc.size   : MediumTest
55  * @tc.type   : Function
56  * @tc.level  : Level 1
57  */
HWTEST_F(HatsEventfd2Test, Eventfd2CreateFdSuccess_0001, Function | MediumTest | Level1)58 HWTEST_F(HatsEventfd2Test, Eventfd2CreateFdSuccess_0001, Function | MediumTest | Level1)
59 {
60     int fd = syscall(__NR_eventfd2, 1, EFD_NONBLOCK);
61     EXPECT_TRUE(fd > 0);
62     close(fd);
63 
64     fd = syscall(__NR_eventfd2, 1, EFD_SEMAPHORE);
65     EXPECT_TRUE(fd > 0);
66 
67     close(fd);
68 }
69 
70 /*
71  * @tc.number : SUB_KERNEL_SYSCALL_EVENTFD2_0200
72  * @tc.name   : Eventfd2InvalidParamFailed_0002
73  * @tc.desc   : eventfd2 create fd use invalid param failed.
74  * @tc.size   : MediumTest
75  * @tc.type   : Function
76  * @tc.level  : Level 2
77  */
HWTEST_F(HatsEventfd2Test, Eventfd2InvalidParamFailed_0002, Function | MediumTest | Level2)78 HWTEST_F(HatsEventfd2Test, Eventfd2InvalidParamFailed_0002, Function | MediumTest | Level2)
79 {
80     errno = 0;
81     int fd = syscall(__NR_eventfd2, 1, -1);
82     EXPECT_TRUE(fd < 0);
83     EXPECT_EQ(errno, EINVAL);
84 }