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