1 /*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
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 #include <chrono>
16 #include <climits>
17 #include <cstdlib>
18 #include <ctime>
19 #include <fstream>
20
21 #include "accesstoken_kit.h"
22 #include "ipc_skeleton.h"
23 #include "json/json.h"
24 #include "nativetoken_kit.h"
25 #include "time_common.h"
26 #include "time_service_test.h"
27 #include "timer_info_test.h"
28 #include "token_setproc.h"
29 #include "want_agent.h"
30
31 namespace {
32 using namespace testing::ext;
33 using namespace OHOS;
34 using namespace OHOS::MiscServices;
35 using namespace std::chrono;
36 using namespace OHOS::Security::AccessToken;
37
38 uint64_t g_selfTokenId = 0;
39
40 static HapPolicyParams g_policyA = { .apl = APL_SYSTEM_CORE,
41 .domain = "test.domain",
42 .permList = { { .permissionName = "ohos.permission.SET_TIME",
43 .bundleName = "ohos.permission_test.demoB",
44 .grantMode = 1,
45 .availableLevel = APL_NORMAL,
46 .label = "label",
47 .labelId = 1,
48 .description = "test",
49 .descriptionId = 1 },
50 { .permissionName = "ohos.permission.SET_TIME_ZONE",
51 .bundleName = "ohos.permission_test.demoB",
52 .grantMode = 1,
53 .availableLevel = APL_NORMAL,
54 .label = "label",
55 .labelId = 1,
56 .description = "test",
57 .descriptionId = 1 } },
58 .permStateList = { { .permissionName = "ohos.permission.SET_TIME",
59 .isGeneral = true,
60 .resDeviceID = { "local" },
61 .grantStatus = { PermissionState::PERMISSION_GRANTED },
62 .grantFlags = { 1 } },
63 { .permissionName = "ohos.permission.SET_TIME_ZONE",
64 .isGeneral = true,
65 .resDeviceID = { "local" },
66 .grantStatus = { PermissionState::PERMISSION_GRANTED },
67 .grantFlags = { 1 } } } };
68
69 HapInfoParams g_systemInfoParams = {
70 .userID = 1,
71 .bundleName = "timer",
72 .instIndex = 0,
73 .appIDDesc = "test",
74 .apiVersion = 8,
75 };
76
77 static HapPolicyParams g_policyB = { .apl = APL_NORMAL, .domain = "test.domain" };
78
79 HapInfoParams g_notSystemInfoParams = {
80 .userID = 1,
81 .bundleName = "timer",
82 .instIndex = 0,
83 .appIDDesc = "test",
84 .apiVersion = 8,
85 };
86
87 class TimeClientTest : public testing::Test {
88 public:
89 static void SetUpTestCase(void);
90 static void TearDownTestCase(void);
91 void SetUp();
92 void TearDown();
93 };
94
SetUpTestCase(void)95 void TimeClientTest::SetUpTestCase(void)
96 {
97 g_selfTokenId = GetSelfTokenID();
98 AccessTokenIDEx tokenIdEx = { 0 };
99 tokenIdEx = AccessTokenKit::AllocHapToken(g_systemInfoParams, g_policyA);
100 SetSelfTokenID(tokenIdEx.tokenIDEx);
101 }
102
TearDownTestCase(void)103 void TimeClientTest::TearDownTestCase(void)
104 {
105 }
106
SetUp(void)107 void TimeClientTest::SetUp(void)
108 {
109 }
110
TearDown(void)111 void TimeClientTest::TearDown(void)
112 {
113 }
114
115 /**
116 * @tc.name: SetTime001
117 * @tc.desc: set system time.
118 * @tc.type: FUNC
119 */
120 HWTEST_F(TimeClientTest, SetTime001, TestSize.Level1)
121 {
122 struct timeval currentTime {};
123 gettimeofday(¤tTime, NULL);
124 int64_t time = (currentTime.tv_sec + 1000) * 1000 + currentTime.tv_usec / 1000;
125 ASSERT_TRUE(time > 0);
126 TIME_HILOGI(TIME_MODULE_CLIENT, "Time now : %{public}" PRId64 "", time);
127 int32_t result = TimeServiceClient::GetInstance()->SetTimeV9(time);
128 EXPECT_TRUE(result == TimeError::E_TIME_OK);
129 }
130
131 /**
132 * @tc.name: SetTime002
133 * @tc.desc: set system time.
134 * @tc.type: FUNC
135 */
136 HWTEST_F(TimeClientTest, SetTime002, TestSize.Level1)
137 {
138 int32_t result = TimeServiceClient::GetInstance()->SetTimeV9(-1);
139 EXPECT_TRUE(result != TimeError::E_TIME_OK);
140 }
141
142 /**
143 * @tc.name: SetTime003
144 * @tc.desc: set system time.
145 * @tc.type: FUNC
146 */
147 HWTEST_F(TimeClientTest, SetTime003, TestSize.Level1)
148 {
149 int32_t result = TimeServiceClient::GetInstance()->SetTimeV9(LLONG_MAX);
150 EXPECT_TRUE(result != TimeError::E_TIME_OK);
151 }
152
153 /**
154 * @tc.name: SetTimeZone001
155 * @tc.desc: set system time zone.
156 * @tc.type: FUNC
157 */
158 HWTEST_F(TimeClientTest, SetTimeZone001, TestSize.Level1)
159 {
160 time_t t;
161 (void)time(&t);
162 TIME_HILOGI(TIME_MODULE_CLIENT, "Time before: %{public}s", asctime(localtime(&t)));
163 auto getCurrentTimeZone = TimeServiceClient::GetInstance()->GetTimeZone();
164 EXPECT_FALSE(getCurrentTimeZone.empty());
165
166 std::string timeZoneNicosia("Asia/Nicosia");
167 int32_t result = TimeServiceClient::GetInstance()->SetTimeZoneV9(timeZoneNicosia);
168 EXPECT_TRUE(result == TimeError::E_TIME_OK);
169 auto getTimeZoneNicosia = TimeServiceClient::GetInstance()->GetTimeZone();
170 EXPECT_EQ(timeZoneNicosia, getTimeZoneNicosia);
171 int32_t ret = TimeServiceClient::GetInstance()->SetTimeZoneV9(getCurrentTimeZone);
172 EXPECT_TRUE(ret == TimeError::E_TIME_OK);
173 }
174
175 /**
176 * @tc.name: SetTimeZone002
177 * @tc.desc: set system time zone.
178 * @tc.type: FUNC
179 */
180 HWTEST_F(TimeClientTest, SetTimeZone002, TestSize.Level1)
181 {
182 int32_t result = TimeServiceClient::GetInstance()->SetTimeZoneV9("123");
183 EXPECT_TRUE(result != TimeError::E_TIME_OK);
184 }
185
186 /**
187 * @tc.name: GetWallTimeMs001
188 * @tc.desc: get wall time (ms).
189 * @tc.type: FUNC
190 */
191 HWTEST_F(TimeClientTest, GetWallTimeMs001, TestSize.Level1)
192 {
193 int64_t time;
194 auto errCode = TimeServiceClient::GetInstance()->GetWallTimeMs(time);
195 EXPECT_TRUE(errCode == TimeError::E_TIME_OK);
196 }
197
198 /**
199 * @tc.name: GetWallTimeNs001
200 * @tc.desc: get wall time (ns).
201 * @tc.type: FUNC
202 */
203 HWTEST_F(TimeClientTest, GetWallTimeNs001, TestSize.Level1)
204 {
205 int64_t time;
206 auto errCode = TimeServiceClient::GetInstance()->GetWallTimeNs(time);
207 EXPECT_TRUE(errCode == TimeError::E_TIME_OK);
208 }
209
210 /**
211 * @tc.name: GetBootTimeNs001
212 * @tc.desc: get boot time (ns).
213 * @tc.type: FUNC
214 */
215 HWTEST_F(TimeClientTest, GetBootTimeNs001, TestSize.Level1)
216 {
217 int64_t time;
218 auto errCode = TimeServiceClient::GetInstance()->GetBootTimeNs(time);
219 EXPECT_TRUE(errCode == TimeError::E_TIME_OK);
220 }
221
222 /**
223 * @tc.name: GetMonotonicTimeMs001
224 * @tc.desc: get monotonic time (ms).
225 * @tc.type: FUNC
226 */
227 HWTEST_F(TimeClientTest, GetMonotonicTimeMs001, TestSize.Level1)
228 {
229 int64_t time;
230 auto errCode = TimeServiceClient::GetInstance()->GetMonotonicTimeMs(time);
231 EXPECT_TRUE(errCode == TimeError::E_TIME_OK);
232 }
233
234 /**
235 * @tc.name: GetMonotonicTimeNs001
236 * @tc.desc: get monotonic time (ns).
237 * @tc.type: FUNC
238 */
239 HWTEST_F(TimeClientTest, GetMonotonicTimeNs001, TestSize.Level1)
240 {
241 int64_t time;
242 auto errCode = TimeServiceClient::GetInstance()->GetMonotonicTimeNs(time);
243 EXPECT_TRUE(errCode == TimeError::E_TIME_OK);
244 }
245
246 /**
247 * @tc.name: GetThreadTimeMs001
248 * @tc.desc: get thread time (ms).
249 * @tc.type: FUNC
250 */
251 HWTEST_F(TimeClientTest, GetThreadTimeMs001, TestSize.Level1)
252 {
253 int64_t time;
254 auto errCode = TimeServiceClient::GetInstance()->GetThreadTimeMs(time);
255 EXPECT_TRUE(errCode == TimeError::E_TIME_OK);
256 }
257
258 /**
259 * @tc.name: GetThreadTimeNs001
260 * @tc.desc: get thread time (ns).
261 * @tc.type: FUNC
262 */
263 HWTEST_F(TimeClientTest, GetThreadTimeNs001, TestSize.Level1)
264 {
265 int64_t time;
266 auto errCode = TimeServiceClient::GetInstance()->GetThreadTimeNs(time);
267 EXPECT_TRUE(errCode == TimeError::E_TIME_OK);
268 }
269
270 /**
271 * @tc.name: CreateTimer001
272 * @tc.desc: Create system timer.
273 * @tc.type: FUNC
274 */
275 HWTEST_F(TimeClientTest, CreateTimer001, TestSize.Level1)
276 {
277 uint64_t timerId = 0;
278 auto ret = TimeServiceClient::GetInstance()->StartTimerV9(timerId, 5);
279 EXPECT_TRUE(ret != TimeError::E_TIME_OK);
280 ret = TimeServiceClient::GetInstance()->StopTimerV9(timerId);
281 EXPECT_TRUE(ret != TimeError::E_TIME_OK);
282 ret = TimeServiceClient::GetInstance()->DestroyTimerV9(timerId);
283 EXPECT_TRUE(ret != TimeError::E_TIME_OK);
284 }
285
286 /**
287 * @tc.name: CreateTimer002
288 * @tc.desc: Create system timer.
289 * @tc.type: FUNC
290 */
291 HWTEST_F(TimeClientTest, CreateTimer002, TestSize.Level1)
292 {
293 auto timerInfo = std::make_shared<TimerInfoTest>();
294 timerInfo->SetType(1);
295 timerInfo->SetRepeat(false);
296 timerInfo->SetInterval(0);
297 timerInfo->SetWantAgent(nullptr);
298 timerInfo->SetCallbackInfo(TimeOutCallback1);
299 uint64_t timerId;
300 auto errCode = TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId);
301 TIME_HILOGI(TIME_MODULE_CLIENT, "timerId now : %{public}" PRId64 "", timerId);
302 EXPECT_TRUE(errCode == TimeError::E_TIME_OK);
303 auto ret = TimeServiceClient::GetInstance()->StartTimerV9(timerId, 2000);
304 EXPECT_TRUE(ret == TimeError::E_TIME_OK);
305 ret = TimeServiceClient::GetInstance()->StopTimerV9(timerId);
306 EXPECT_TRUE(ret == TimeError::E_TIME_OK);
307 ret = TimeServiceClient::GetInstance()->DestroyTimerV9(timerId);
308 EXPECT_TRUE(ret == TimeError::E_TIME_OK);
309 }
310
311 /**
312 * @tc.name: CreateTimer003
313 * @tc.desc: Create system timer.
314 * @tc.type: FUNC
315 */
316 HWTEST_F(TimeClientTest, CreateTimer003, TestSize.Level1)
317 {
318 auto timerInfo = std::make_shared<TimerInfoTest>();
319 timerInfo->SetType(1);
320 timerInfo->SetRepeat(false);
321 timerInfo->SetInterval(0);
322 auto ability = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
323 timerInfo->SetWantAgent(ability);
324 timerInfo->SetCallbackInfo(TimeOutCallback1);
325 uint64_t timerId;
326 auto errCode = TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId);
327 EXPECT_TRUE(errCode == TimeError::E_TIME_OK);
328 }
329
330 /**
331 * @tc.name: CreateTimer004
332 * @tc.desc: Create system timer.
333 * @tc.type: FUNC
334 */
335 HWTEST_F(TimeClientTest, CreateTimer004, TestSize.Level1)
336 {
337 g_data1 = 0;
338 auto timerInfo = std::make_shared<TimerInfoTest>();
339 timerInfo->SetType(1);
340 timerInfo->SetRepeat(false);
341 timerInfo->SetInterval(0);
342 timerInfo->SetWantAgent(nullptr);
343 timerInfo->SetCallbackInfo(TimeOutCallback1);
344 uint64_t timerId;
345 auto errCode = TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId);
346 EXPECT_TRUE(errCode == TimeError::E_TIME_OK);
347 auto bootTimeNano = system_clock::now().time_since_epoch().count();
348 auto bootTimeMilli = bootTimeNano / NANO_TO_MILESECOND;
349 errCode = TimeServiceClient::GetInstance()->StartTimerV9(timerId, bootTimeMilli + 2000);
350 EXPECT_TRUE(errCode == TimeError::E_TIME_OK);
351 errCode = TimeServiceClient::GetInstance()->DestroyTimerV9(timerId);
352 EXPECT_TRUE(errCode == TimeError::E_TIME_OK);
353 EXPECT_TRUE(g_data1 == 0);
354 errCode = TimeServiceClient::GetInstance()->StopTimerV9(timerId);
355 EXPECT_TRUE(errCode != TimeError::E_TIME_OK);
356 }
357
358 /**
359 * @tc.name: CreateTimer005
360 * @tc.desc: Create system timer.
361 * @tc.type: FUNC
362 */
363 HWTEST_F(TimeClientTest, CreateTimer005, TestSize.Level1)
364 {
365 g_data1 = 1;
366 auto timerInfo = std::make_shared<TimerInfoTest>();
367 timerInfo->SetType(0);
368 timerInfo->SetRepeat(false);
369 timerInfo->SetInterval(0);
370 timerInfo->SetWantAgent(nullptr);
371 timerInfo->SetCallbackInfo(TimeOutCallback1);
372
373 struct timeval timeOfDay {};
374 gettimeofday(&timeOfDay, NULL);
375 int64_t currentTime = (timeOfDay.tv_sec + 100) * 1000 + timeOfDay.tv_usec / 1000;
376 if (currentTime < 0) {
377 currentTime = 0;
378 }
379 uint64_t timerId;
380 auto errCode = TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId);
381 EXPECT_TRUE(errCode == TimeError::E_TIME_OK);
382
383 errCode = TimeServiceClient::GetInstance()->StartTimerV9(timerId, static_cast<uint64_t>(currentTime));
384 EXPECT_TRUE(errCode == TimeError::E_TIME_OK);
385 errCode = TimeServiceClient::GetInstance()->DestroyTimerV9(timerId);
386 EXPECT_TRUE(errCode == TimeError::E_TIME_OK);
387 EXPECT_TRUE(g_data1 == 1);
388 errCode = TimeServiceClient::GetInstance()->StopTimerV9(timerId);
389 EXPECT_TRUE(errCode != TimeError::E_TIME_OK);
390 }
391
392 /**
393 * @tc.name: CreateTimer006
394 * @tc.desc: Create system timer.
395 * @tc.type: FUNC
396 */
397 HWTEST_F(TimeClientTest, CreateTimer006, TestSize.Level1)
398 {
399 uint64_t timerId;
400 auto errCode = TimeServiceClient::GetInstance()->CreateTimerV9(nullptr, timerId);
401 uint64_t ret = 0;
402 EXPECT_TRUE(errCode != TimeError::E_TIME_OK);
403 EXPECT_EQ(timerId, ret);
404 }
405
406 } // namespace