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 <cstdlib>
18 #include <cstdio>
19 #include <ctime>
20 #include <unistd.h>
21 #include <gtest/gtest.h>
22 
23 using namespace testing::ext;
24 using namespace std;
25 
26 class ClockNanosleepApiTest : 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 ClockNanosleepApiTest::SetUp()
35 {
36 }
TearDown()37 void ClockNanosleepApiTest::TearDown()
38 {
39 }
SetUpTestCase()40 void ClockNanosleepApiTest::SetUpTestCase()
41 {
42 }
TearDownTestCase()43 void ClockNanosleepApiTest::TearDownTestCase()
44 {
45 }
SignalHandler(int sig)46 void SignalHandler(int sig)
47 {
48 }
49 
50 /*
51  * @tc.number : SUB_KERNEL_SYSCALL_CLOCK_NANOSLEEP_0100
52  * @tc.name   : ClockNanosleepBlockedSuccess_0001
53  * @tc.desc   : Blocked by other progress.
54  * @tc.size   : MediumTest
55  * @tc.type   : Function
56  * @tc.level  : Level 1
57  */
HWTEST_F(ClockNanosleepApiTest, ClockNanosleepBlockedSuccess_0001, Function | MediumTest | Level1)58 HWTEST_F(ClockNanosleepApiTest, ClockNanosleepBlockedSuccess_0001, Function | MediumTest | Level1)
59 {
60     struct timespec req = {1, 0};
61     struct timespec rem;
62     timer_t timerId;
63     struct sigevent sev = {
64         .sigev_signo = SIGALRM,
65         .sigev_notify = SIGEV_SIGNAL,
66     };
67     struct itimerspec its = {
68         .it_interval = {
69             .tv_sec = 0,
70             .tv_nsec = 0,
71         },
72         .it_value = {
73             .tv_sec = 0,
74             .tv_nsec = 50000,
75         },
76     };
77     signal(SIGALRM, SignalHandler);
78     timer_create(CLOCK_REALTIME, &sev, &timerId);
79 
80     timer_settime(timerId, 0, &its, nullptr);
81 
82     errno = 0;
83     int ret = clock_nanosleep(CLOCK_REALTIME, 0, &req, &rem);
84 
85     timer_delete(timerId);
86 
87     EXPECT_EQ(ret, EINTR);
88 }
89 
90 /*
91  * @tc.number : SUB_KERNEL_SYSCALL_CLOCK_NANOSLEEP_0200
92  * @tc.name   : ClockNanosleepSuccess_0002
93  * @tc.desc   : Clock nanosleep successfully.
94  * @tc.size   : MediumTest
95  * @tc.type   : Function
96  * @tc.level  : Level 1
97  */
HWTEST_F(ClockNanosleepApiTest, ClockNanosleepSuccess_0002, Function | MediumTest | Level1)98 HWTEST_F(ClockNanosleepApiTest, ClockNanosleepSuccess_0002, Function | MediumTest | Level1)
99 {
100     struct timespec req = {0, 100};
101 
102     int ret = clock_nanosleep(CLOCK_REALTIME, 0, &req, nullptr);
103     EXPECT_EQ(ret, 0);
104 }