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 <cstdlib>
17 #include <cstdio>
18 #include <string>
19 #include <vector>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <gtest/gtest.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include "securec.h"
26
27 using namespace testing::ext;
28 using namespace std;
29
30 static time_t g_time;
31 static struct timespec g_timeTs;
32 static const int DELAY_TIME = 100;
33
34 class ClockApiTest : public testing::Test {
35 public:
36 static void SetUpTestCase();
37 static void TearDownTestCase();
38 void SetUp();
39 void TearDown();
40 private:
41 };
SetUp()42 void ClockApiTest::SetUp()
43 {
44 g_time = time(nullptr);
45 g_timeTs.tv_sec = g_time;
46 g_timeTs.tv_nsec = 0;
47 }
TearDown()48 void ClockApiTest::TearDown()
49 {
50 }
SetUpTestCase()51 void ClockApiTest::SetUpTestCase()
52 {
53 }
TearDownTestCase()54 void ClockApiTest::TearDownTestCase()
55 {
56 }
57
58 /*
59 * @tc.number : SUB_KERNEL_SYSCALL_CLOCKGETTIME_0100
60 * @tc.name : GetRealtimeClockSuccess_0001
61 * @tc.desc : Get the realtime clock successfully and compare with time function.
62 * @tc.size : MediumTest
63 * @tc.type : Function
64 * @tc.level : Level 1
65 */
66 HWTEST_F(ClockApiTest, GetRealtimeClockSuccess_0001, Function | MediumTest | Level1)
67 {
68 struct timespec ts;
69 ts.tv_sec = 0;
70 int ret = clock_gettime(CLOCK_REALTIME, &ts);
71 EXPECT_TRUE(ret == 0);
72 EXPECT_TRUE(ts.tv_sec > 0);
73
74 long diff = abs(ts.tv_sec - g_timeTs.tv_sec);
75 EXPECT_TRUE(diff <= 1);
76 }
77
78 /*
79 * @tc.number : SUB_KERNEL_SYSCALL_CLOCKGETTIME_0200
80 * @tc.name : ClockGetTimeGetRealTimeCoarseSuccess_0002
81 * @tc.desc : Get the realtime coarse clock successfully.
82 * @tc.size : MediumTest
83 * @tc.type : Function
84 * @tc.level : Level 1
85 */
86 HWTEST_F(ClockApiTest, ClockGetTimeGetRealTimeCoarseSuccess_0002, Function | MediumTest | Level1)
87 {
88 struct timespec ts;
89 int ret = clock_gettime(CLOCK_REALTIME_COARSE, &ts);
90 EXPECT_TRUE(ret == 0);
91 EXPECT_TRUE(ts.tv_sec >= 0);
92
93 long diff = abs(ts.tv_sec - g_timeTs.tv_sec);
94 EXPECT_TRUE(diff <= 1);
95 }
96
97 /*
98 * @tc.number : SUB_KERNEL_SYSCALL_CLOCKGETTIME_0300
99 * @tc.name : ClockGetTimeMonotonicSuccess_0003
100 * @tc.desc : Get the monotonic clock successfully.
101 * @tc.size : MediumTest
102 * @tc.type : Function
103 * @tc.level : Level 1
104 */
105 HWTEST_F(ClockApiTest, ClockGetTimeMonotonicSuccess_0003, Function | MediumTest | Level1)
106 {
107 struct timespec ts;
108 struct timespec tsSec;
109 int ret = clock_gettime(CLOCK_MONOTONIC, &ts);
110 EXPECT_TRUE(ret == 0);
111 EXPECT_TRUE(ts.tv_sec >= 0);
112
113 usleep(DELAY_TIME);
114 clock_gettime(CLOCK_MONOTONIC, &tsSec);
115 long diff = abs(ts.tv_sec - tsSec.tv_sec);
116 EXPECT_TRUE(diff <= 1);
117 }
118
119 /*
120 * @tc.number : SUB_KERNEL_SYSCALL_CLOCKGETTIME_0400
121 * @tc.name : ClockGetTimeMonotonicCoarseSuccess_0004
122 * @tc.desc : Get the monotonic coarse clock successfully.
123 * @tc.size : MediumTest
124 * @tc.type : Function
125 * @tc.level : Level 1
126 */
127 HWTEST_F(ClockApiTest, ClockGetTimeMonotonicCoarseSuccess_0004, Function | MediumTest | Level1)
128 {
129 struct timespec ts;
130 struct timespec tsSec;
131 int ret = clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
132 EXPECT_TRUE(ret == 0);
133 EXPECT_TRUE(ts.tv_sec >= 0);
134
135 usleep(DELAY_TIME);
136 clock_gettime(CLOCK_MONOTONIC_COARSE, &tsSec);
137 long diff = abs(ts.tv_sec - tsSec.tv_sec);
138 EXPECT_TRUE(diff <= 10);
139 }
140
141 /*
142 * @tc.number : SUB_KERNEL_SYSCALL_CLOCKGETTIME_0500
143 * @tc.name : ClockGetTimeBoottimeSuccess_0005
144 * @tc.desc : Get the boottime clock successfully.
145 * @tc.size : MediumTest
146 * @tc.type : Function
147 * @tc.level : Level 1
148 */
149 HWTEST_F(ClockApiTest, ClockGetTimeBoottimeSuccess_0005, Function | MediumTest | Level1)
150 {
151 struct timespec ts;
152 struct timespec tsSec;
153 int ret = clock_gettime(CLOCK_BOOTTIME, &ts);
154 EXPECT_TRUE(ret == 0);
155 EXPECT_TRUE(ts.tv_sec >= 0);
156
157 usleep(DELAY_TIME);
158 clock_gettime(CLOCK_BOOTTIME, &tsSec);
159 long diff = abs(ts.tv_sec - tsSec.tv_sec);
160 EXPECT_TRUE(diff <= 10);
161 }
162
163 /*
164 * @tc.number : SUB_KERNEL_SYSCALL_CLOCKGETTIME_0600
165 * @tc.name : ClockGetTimeProcessCputimeSuccess_0006
166 * @tc.desc : Get the process cputime clock successfully.
167 * @tc.size : MediumTest
168 * @tc.type : Function
169 * @tc.level : Level 1
170 */
171 HWTEST_F(ClockApiTest, ClockGetTimeProcessCputimeSuccess_0006, Function | MediumTest | Level1)
172 {
173 struct timespec ts;
174 struct timespec tsSec;
175 int ret = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
176 EXPECT_TRUE(ret == 0);
177 EXPECT_TRUE(ts.tv_sec >= 0);
178
179 usleep(DELAY_TIME);
180 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tsSec);
181 long diff = abs(ts.tv_sec - tsSec.tv_sec);
182 EXPECT_TRUE(diff <= 1);
183 }
184
185 /*
186 * @tc.number : SUB_KERNEL_SYSCALL_CLOCKGETTIME_0700
187 * @tc.name : ClockGetTimeThreadCputimeSuccess_0007
188 * @tc.desc : Get the thread cputime clock successfully.
189 * @tc.size : MediumTest
190 * @tc.type : Function
191 * @tc.level : Level 1
192 */
193 HWTEST_F(ClockApiTest, ClockGetTimeThreadCputimeSuccess_0007, Function | MediumTest | Level1)
194 {
195 struct timespec ts;
196 struct timespec tsSec;
197 int ret = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
198 EXPECT_TRUE(ret == 0);
199 EXPECT_TRUE(ts.tv_sec >= 0);
200
201 usleep(DELAY_TIME);
202 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tsSec);
203 long diff = abs(ts.tv_sec - tsSec.tv_sec);
204 EXPECT_TRUE(diff <= 1);
205 }
206