• 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 <csignal>
17 #include <ctime>
18 #include <iostream>
19 #include <unistd.h>
20 #include <gtest/gtest.h>
21 #include <sys/timerfd.h>
22 #include "securec.h"
23 
24 using namespace testing::ext;
25 
26 class TimerFdCreateApiTest : 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 TimerFdCreateApiTest::SetUp()
35 {
36 }
TearDown()37 void TimerFdCreateApiTest::TearDown()
38 {
39 }
SetUpTestCase()40 void TimerFdCreateApiTest::SetUpTestCase()
41 {
42 }
TearDownTestCase()43 void TimerFdCreateApiTest::TearDownTestCase()
44 {
45 }
46 
47 /*
48  * @tc.number : SUB_KERNEL_SYSCALL_TIMERFD_SETTIME_0100
49  * @tc.name   : TimerfdSetTimeClockRealTimeSuccess_0001
50  * @tc.desc   : Test the timerfd_settime with CLOCK_REALTIME.
51  * @tc.size   : MediumTest
52  * @tc.type   : Function
53  * @tc.level  : Level 1
54  */
55 HWTEST_F(TimerFdCreateApiTest, TimerfdSetTimeClockRealTimeSuccess_0001, Function | MediumTest | Level1)
56 {
57     int timerfd = timerfd_create(CLOCK_REALTIME, 0);
58     EXPECT_NE(timerfd, -1);
59 
60     struct itimerspec newValue;
61     struct itimerspec oldValue;
62     newValue.it_value.tv_sec = 0;
63     newValue.it_value.tv_nsec = 100;
64     newValue.it_interval.tv_sec = 0;
65 
66     EXPECT_EQ(timerfd_settime(timerfd, 0, &newValue, nullptr), 0);
67 
68     uint64_t expirations;
69     EXPECT_EQ(read(timerfd, &expirations, sizeof(expirations)), sizeof(expirations));
70     EXPECT_EQ(expirations, 1);
71 
72     int ret = timerfd_gettime(timerfd, &oldValue);
73     EXPECT_GE(ret, 0);
74     EXPECT_GE(newValue.it_value.tv_nsec, 0);
75 
76     close(timerfd);
77 }
78 
79 /*
80  * @tc.number : SUB_KERNEL_SYSCALL_TIMERFD_SETTIME_0200
81  * @tc.name   : TimerfdSetTimeClockMonotonicSuccess_0002
82  * @tc.desc   : Test the timerfd_settime with CLOCK_MONOTONIC.
83  * @tc.size   : MediumTest
84  * @tc.type   : Function
85  * @tc.level  : Level 1
86  */
87 HWTEST_F(TimerFdCreateApiTest, TimerfdCreateClockMonotonicSuccess_0002, Function | MediumTest | Level1)
88 {
89     int timerfd = timerfd_create(CLOCK_MONOTONIC, 0);
90     EXPECT_NE(timerfd, -1);
91 
92     struct itimerspec newValue;
93     newValue.it_value.tv_nsec = 100;
94     newValue.it_interval.tv_nsec = 0;
95 
96     EXPECT_EQ(timerfd_settime(timerfd, 0, &newValue, nullptr), 0);
97 
98     uint64_t expirations;
99     EXPECT_EQ(read(timerfd, &expirations, sizeof(expirations)), sizeof(expirations));
100     EXPECT_EQ(expirations, 1);
101 
102     close(timerfd);
103 }
104 
105 /*
106  * @tc.number : SUB_KERNEL_SYSCALL_TIMERFD_SETTIME_0300
107  * @tc.name   : TimerfdSetTimeClockBoottimeSuccess_0003
108  * @tc.desc   : Test the timerfd_settime with CLOCK_BOOTTIME.
109  * @tc.size   : MediumTest
110  * @tc.type   : Function
111  * @tc.level  : Level 1
112  */
113 HWTEST_F(TimerFdCreateApiTest, TimerfdSetTimeClockBoottimeSuccess_0003, Function | MediumTest | Level1)
114 {
115     int timerfd = timerfd_create(CLOCK_BOOTTIME, 0);
116     EXPECT_NE(timerfd, -1);
117 
118     struct itimerspec newValue;
119     newValue.it_value.tv_sec = 0;
120     newValue.it_value.tv_nsec = 100;
121     newValue.it_interval.tv_sec = 0;
122 
123     EXPECT_EQ(timerfd_settime(timerfd, 0, &newValue, nullptr), 0);
124 
125     uint64_t expirations;
126     EXPECT_EQ(read(timerfd, &expirations, sizeof(expirations)), sizeof(expirations));
127     EXPECT_EQ(expirations, 1);
128 
129     close(timerfd);
130 }
131 
132 /*
133  * @tc.number : SUB_KERNEL_SYSCALL_TIMERFD_SETTIME_0400
134  * @tc.name   : TimerfdSetTimeClockRealTimeAlarmSuccess_0004
135  * @tc.desc   : Test the timerfd_settime with CLOCK_REALTIME_ALARM.
136  * @tc.size   : MediumTest
137  * @tc.type   : Function
138  * @tc.level  : Level 1
139  */
140 HWTEST_F(TimerFdCreateApiTest, TimerfdSetTimeClockRealTimeAlarmSuccess_0004, Function | MediumTest | Level1)
141 {
142     int timerfd = timerfd_create(CLOCK_REALTIME_ALARM, 0);
143     EXPECT_NE(timerfd, -1);
144 
145     struct itimerspec newValue;
146     newValue.it_value.tv_sec = 0;
147     newValue.it_value.tv_nsec = 100;
148     newValue.it_interval.tv_nsec = 0;
149 
150     EXPECT_EQ(timerfd_settime(timerfd, 0, &newValue, nullptr), 0);
151 
152     uint64_t expirations;
153     EXPECT_EQ(read(timerfd, &expirations, sizeof(expirations)), sizeof(expirations));
154     EXPECT_EQ(expirations, 1);
155 
156     close(timerfd);
157 }
158 
159 /*
160  * @tc.number : SUB_KERNEL_SYSCALL_TIMERFD_SETTIME_0500
161  * @tc.name   : TimerfdSetTimeClockBoottimeAlarmSuccess_0005
162  * @tc.desc   : Test the timerfd_settime with CLOCK_BOOTTIME_ALARM.
163  * @tc.size   : MediumTest
164  * @tc.type   : Function
165  * @tc.level  : Level 1
166  */
167 HWTEST_F(TimerFdCreateApiTest, TimerfdSetTimeClockBoottimeAlarmSuccess_0005, Function | MediumTest | Level1)
168 {
169     int timerfd = timerfd_create(CLOCK_BOOTTIME_ALARM, 0);
170     EXPECT_NE(timerfd, -1);
171 
172     struct itimerspec newValue;
173     newValue.it_value.tv_nsec = 500;
174     newValue.it_interval.tv_nsec = 0;
175 
176     EXPECT_EQ(timerfd_settime(timerfd, 0, &newValue, nullptr), 0);
177 
178     uint64_t expirations;
179     EXPECT_EQ(read(timerfd, &expirations, sizeof(expirations)), sizeof(expirations));
180     EXPECT_EQ(expirations, 1);
181 
182     close(timerfd);
183 }
184 
185 /*
186  * @tc.number : SUB_KERNEL_SYSCALL_TIMERFD_CREATE_0600
187  * @tc.name   : TimerfdCreateTFdNonBlockTest_0006
188  * @tc.desc   : Test the timerfd_create with TFD_NONBLOCK.
189  * @tc.size   : MediumTest
190  * @tc.type   : Function
191  * @tc.level  : Level 2
192  */
193 HWTEST_F(TimerFdCreateApiTest, TimerfdCreateTFdNonBlockTest_0006, Function | MediumTest | Level2)
194 {
195     int timerfd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK);
196     EXPECT_NE(timerfd, -1);
197     errno = 0;
198 
199     struct itimerspec newValue;
200     newValue.it_value.tv_sec = 0;
201     newValue.it_value.tv_nsec = 10000000;
202     newValue.it_interval.tv_sec = 0;
203 
204     EXPECT_EQ(timerfd_settime(timerfd, 0, &newValue, nullptr), 0);
205 
206     uint64_t expirations;
207     ssize_t result = read(timerfd, &expirations, sizeof(expirations));
208     EXPECT_EQ(result, -1);
209     EXPECT_EQ(errno, EAGAIN);
210 
211     close(timerfd);
212 }
213 
214 /*
215  * @tc.number : SUB_KERNEL_SYSCALL_TIMERFD_SETTIME_0700
216  * @tc.name   : TimerfdSetTimeTFdTimerAbstimeSuccess_0007
217  * @tc.desc   : Test the timerfd_settime with TFD_TIMER_ABSTIME.
218  * @tc.size   : MediumTest
219  * @tc.type   : Function
220  * @tc.level  : Level 1
221  */
222 HWTEST_F(TimerFdCreateApiTest, TimerfdSetTimeTFdTimerAbstimeSuccess_0007, Function | MediumTest | Level1)
223 {
224     int timerfd = timerfd_create(CLOCK_REALTIME, 0);
225     EXPECT_NE(timerfd, -1);
226 
227     struct itimerspec newValue;
228     newValue.it_value.tv_sec = 1;
229     newValue.it_value.tv_nsec = 0;
230     newValue.it_interval.tv_sec = 0;
231 
232     EXPECT_EQ(timerfd_settime(timerfd, TFD_TIMER_ABSTIME, &newValue, nullptr), 0);
233 
234     uint64_t expirations;
235     EXPECT_EQ(read(timerfd, &expirations, sizeof(expirations)), sizeof(expirations));
236     EXPECT_EQ(expirations, 1);
237 
238     close(timerfd);
239 }