1 /*
2 * Copyright (C) 2021 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 "time_service_test.h"
16
17 #include <chrono>
18 #include <climits>
19 #include <cstdlib>
20 #include <ctime>
21 #include <fstream>
22 #include <sys/stat.h>
23
24 #include "accesstoken_kit.h"
25 #include "ipc_skeleton.h"
26 #include "nativetoken_kit.h"
27 #include "time_common.h"
28 #include "timer_info_test.h"
29 #include "token_setproc.h"
30 #include "want_agent.h"
31 #include "timer_call_back.h"
32 #include "time_common.h"
33 #include "power_subscriber.h"
34 #include "ntp_update_time.h"
35
36 #define private public
37 #define protected public
38 #include "sntp_client.h"
39 #include "ntp_update_time.h"
40 #include "time_system_ability.h"
41 #include "ntp_trusted_time.h"
42 #include "time_tick_notify.h"
43 #include "timer_database.h"
44 #include "timer_proxy.h"
45 #include "timer_notify_callback.h"
46
47 namespace {
48 using namespace testing::ext;
49 using namespace OHOS;
50 using namespace OHOS::MiscServices;
51 using namespace std::chrono;
52 using namespace OHOS::Security::AccessToken;
53
54 const int32_t RESERVED_UID = 99999;
55 std::set<int> RESERVED_PIDLIST = {1111, 2222};
56 const std::string NETWORK_TIME_STATUS_OFF = "OFF";
57 const std::string NETWORK_TIME_STATUS_ON = "ON";
58 const std::string AUTO_TIME_STATUS_ON = "ON";
59 uint64_t g_idleTimerId = 0;
60 const uint64_t TIMER_ID = 88888;
61 const int UID = 999998;
62 const int PID = 999999;
63 constexpr int TIMER_ALARM_COUNT = 50;
64 constexpr int64_t MINUTE_TO_MILLISECOND = 60000;
65
66 static HapPolicyParams g_policyA = {
67 .apl = APL_SYSTEM_CORE,
68 .domain = "test.domain",
69 .permList = {
70 {
71 .permissionName = "ohos.permission.SET_TIME",
72 .bundleName = "ohos.permission_test.demoB",
73 .grantMode = 1,
74 .availableLevel = APL_NORMAL,
75 .label = "label",
76 .labelId = 1,
77 .description = "test",
78 .descriptionId = 1
79 },
80 {
81 .permissionName = "ohos.permission.SET_TIME_ZONE",
82 .bundleName = "ohos.permission_test.demoB",
83 .grantMode = 1,
84 .availableLevel = APL_NORMAL,
85 .label = "label",
86 .labelId = 1,
87 .description = "test",
88 .descriptionId = 1
89 },
90 {
91 .permissionName = "ohos.permission.MANAGE_LOCAL_ACCOUNTS",
92 .bundleName = "ohos.permission_test.demoB",
93 .grantMode = 1,
94 .availableLevel = APL_NORMAL,
95 .label = "label",
96 .labelId = 1,
97 .description = "test",
98 .descriptionId = 1
99 }
100 },
101 .permStateList = {
102 {
103 .permissionName = "ohos.permission.SET_TIME",
104 .isGeneral = true,
105 .resDeviceID = { "local" },
106 .grantStatus = { PermissionState::PERMISSION_GRANTED },
107 .grantFlags = { 1 }
108 },
109 {
110 .permissionName = "ohos.permission.SET_TIME_ZONE",
111 .isGeneral = true,
112 .resDeviceID = { "local" },
113 .grantStatus = { PermissionState::PERMISSION_GRANTED },
114 .grantFlags = { 1 }
115 },
116 {
117 .permissionName = "ohos.permission.MANAGE_LOCAL_ACCOUNTS",
118 .isGeneral = true,
119 .resDeviceID = { "local" },
120 .grantStatus = { PermissionState::PERMISSION_GRANTED },
121 .grantFlags = { 1 }
122 }
123 }
124 };
125
126 static HapInfoParams g_systemInfoParams = {
127 .userID = 1,
128 .bundleName = "timer",
129 .instIndex = 0,
130 .appIDDesc = "test",
131 .apiVersion = 8,
132 .isSystemApp = true
133 };
134
135 static HapPolicyParams g_policyB = { .apl = APL_NORMAL, .domain = "test.domain" };
136
137 static HapInfoParams g_notSystemInfoParams = {
138 .userID = 1,
139 .bundleName = "timer",
140 .instIndex = 0,
141 .appIDDesc = "test",
142 .apiVersion = 8,
143 };
144
145 class TimeServiceTest : public testing::Test {
146 public:
147 static void SetUpTestCase(void);
148 static void TearDownTestCase(void);
149 void SetUp();
150 void TearDown();
151 void AddPermission();
152 void DeletePermission();
153 void StartIdleTimer();
154 void DestroyIdleTimer();
155 };
156
SetUpTestCase(void)157 void TimeServiceTest::SetUpTestCase(void)
158 {
159 }
160
TearDownTestCase(void)161 void TimeServiceTest::TearDownTestCase(void)
162 {
163 }
164
SetUp(void)165 void TimeServiceTest::SetUp(void)
166 {
167 }
168
TearDown(void)169 void TimeServiceTest::TearDown(void)
170 {
171 }
172
AddPermission()173 void TimeServiceTest::AddPermission()
174 {
175 AccessTokenIDEx tokenIdEx = { 0 };
176 tokenIdEx = AccessTokenKit::AllocHapToken(g_systemInfoParams, g_policyA);
177 SetSelfTokenID(tokenIdEx.tokenIDEx);
178 }
179
DeletePermission()180 void TimeServiceTest::DeletePermission()
181 {
182 AccessTokenIDEx tokenIdEx = { 0 };
183 tokenIdEx = AccessTokenKit::AllocHapToken(g_notSystemInfoParams, g_policyB);
184 SetSelfTokenID(tokenIdEx.tokenIDEx);
185 }
186
StartIdleTimer()187 void TimeServiceTest::StartIdleTimer()
188 {
189 auto timerInfo = std::make_shared<TimerInfoTest>();
190 timerInfo->SetType(timerInfo->TIMER_TYPE_IDLE);
191 timerInfo->SetRepeat(false);
192 TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, g_idleTimerId);
193 struct timeval currentTime {};
194 gettimeofday(¤tTime, nullptr);
195 int64_t time = currentTime.tv_sec * 1000 + currentTime.tv_usec / 1000;
196 // 5000 means timer triggers after 5s
197 TimeServiceClient::GetInstance()->StartTimerV9(g_idleTimerId, time + 5000);
198 }
199
DestroyIdleTimer()200 void TimeServiceTest::DestroyIdleTimer()
201 {
202 TimeServiceClient::GetInstance()->DestroyTimerV9(g_idleTimerId);
203 }
204
TestNtpThread(const char * name)205 void TestNtpThread(const char *name)
206 {
207 {
208 std::lock_guard<std::mutex> autoLock(NtpUpdateTime::requestMutex_);
209 }
210 NtpUpdateTime::SetSystemTime();
211 }
212
213 /**
214 * @tc.name: ProxyTimer001.
215 * @tc.desc: proxy timer.
216 * @tc.type: FUNC
217 * @tc.require: SR000H0GQ6 AR000H2VTQ
218 */
219 HWTEST_F(TimeServiceTest, ProxyTimer001, TestSize.Level0)
220 {
221 auto ret = TimeServiceClient::GetInstance()->ProxyTimer(RESERVED_UID, true, true);
222 EXPECT_TRUE(ret);
223 ret = TimeServiceClient::GetInstance()->ProxyTimer(RESERVED_UID, false, true);
224 EXPECT_TRUE(ret);
225 }
226
227 /**
228 * @tc.name: ProxyTimer002.
229 * @tc.desc: proxy timer.
230 * @tc.type: FUNC
231 * @tc.require: SR000H0GQ6 AR000H2VTQ
232 */
233 HWTEST_F(TimeServiceTest, ProxyTimer002, TestSize.Level0)
234 {
235 auto ret = TimeServiceClient::GetInstance()->ProxyTimer(RESERVED_UID, true, true);
236 EXPECT_TRUE(ret);
237 ret = TimeServiceClient::GetInstance()->ResetAllProxy();
238 EXPECT_TRUE(ret);
239 }
240
241 /**
242 * @tc.name: ProxyTimer003.
243 * @tc.desc: proxy timer.
244 * @tc.type: FUNC
245 * @tc.require: SR000H0GQ6 AR000H2VTQ
246 */
247 HWTEST_F(TimeServiceTest, ProxyTimer003, TestSize.Level0)
248 {
249 auto ret = TimeServiceClient::GetInstance()->ProxyTimer(RESERVED_UID, false, true);
250 EXPECT_FALSE(ret);
251 }
252
253 /**
254 * @tc.name: ProxyTimer004.
255 * @tc.desc: proxy timer.
256 * @tc.type: FUNC
257 * @tc.require: SR000H0GQ6 AR000H2VTQ
258 */
259 HWTEST_F(TimeServiceTest, ProxyTimer004, TestSize.Level0)
260 {
261 auto ret = TimeServiceClient::GetInstance()->ProxyTimer(RESERVED_UID, true, false);
262 EXPECT_TRUE(ret);
263 ret = TimeServiceClient::GetInstance()->ProxyTimer(RESERVED_UID, false, false);
264 EXPECT_TRUE(ret);
265 }
266
267 /**
268 * @tc.name: PidProxyTimer001.
269 * @tc.desc: proxy timer.
270 * @tc.type: FUNC
271 */
272 HWTEST_F(TimeServiceTest, PidProxyTimer001, TestSize.Level0)
273 {
274 auto ret = TimeServiceClient::GetInstance()->ProxyTimer(RESERVED_UID, RESERVED_PIDLIST, true, true);
275 EXPECT_TRUE(ret);
276 ret = TimeServiceClient::GetInstance()->ProxyTimer(RESERVED_UID, RESERVED_PIDLIST, false, true);
277 EXPECT_TRUE(ret);
278 }
279
280 /**
281 * @tc.name: PidProxyTimer002.
282 * @tc.desc: proxy timer.
283 * @tc.type: FUNC
284 */
285 HWTEST_F(TimeServiceTest, PidProxyTimer002, TestSize.Level0)
286 {
287 auto ret = TimeServiceClient::GetInstance()->ProxyTimer(RESERVED_UID, RESERVED_PIDLIST, true, true);
288 EXPECT_TRUE(ret);
289 ret = TimeServiceClient::GetInstance()->ResetAllProxy();
290 EXPECT_TRUE(ret);
291 }
292
293 /**
294 * @tc.name: PidProxyTimer003.
295 * @tc.desc: proxy timer.
296 * @tc.type: FUNC
297 */
298 HWTEST_F(TimeServiceTest, PidProxyTimer003, TestSize.Level0)
299 {
300 auto ret = TimeServiceClient::GetInstance()->ProxyTimer(RESERVED_UID, RESERVED_PIDLIST, false, true);
301 EXPECT_FALSE(ret);
302 }
303
304 /**
305 * @tc.name: PidProxyTimer004.
306 * @tc.desc: proxy timer.
307 * @tc.type: FUNC
308 * @tc.require:
309 */
310 HWTEST_F(TimeServiceTest, PidProxyTimer004, TestSize.Level0)
311 {
312 auto ret = TimeServiceClient::GetInstance()->ProxyTimer(RESERVED_UID, RESERVED_PIDLIST, true, false);
313 EXPECT_TRUE(ret);
314 ret = TimeServiceClient::GetInstance()->ProxyTimer(RESERVED_UID, RESERVED_PIDLIST, false, false);
315 EXPECT_TRUE(ret);
316 }
317
318 /**
319 * @tc.name: AdjustTimer001.
320 * @tc.desc: adjust timer.
321 * @tc.type: FUNC
322 * @tc.require:
323 */
324 HWTEST_F(TimeServiceTest, AdjustTimer001, TestSize.Level0)
325 {
326 auto errCode = TimeServiceClient::GetInstance()->AdjustTimer(true, 5);
327 EXPECT_EQ(errCode, TimeError::E_TIME_OK);
328 errCode = TimeServiceClient::GetInstance()->AdjustTimer(false, 0);
329 EXPECT_EQ(errCode, TimeError::E_TIME_OK);
330 }
331
332 /**
333 * @tc.name: AdjustTimer002.
334 * @tc.desc: exemption timer.
335 * @tc.type: FUNC
336 * @tc.require:
337 */
338 HWTEST_F(TimeServiceTest, AdjustTimer002, TestSize.Level0)
339 {
340 std::unordered_set<std::string> nameArr{"timer"};
341 auto errCode = TimeServiceClient::GetInstance()->SetTimerExemption(nameArr, false);
342 EXPECT_EQ(errCode, TimeError::E_TIME_OK);
343 errCode = TimeServiceClient::GetInstance()->SetTimerExemption(nameArr, true);
344 EXPECT_EQ(errCode, TimeError::E_TIME_OK);
345 }
346
347 /**
348 * @tc.name: IdleTimer001.
349 * @tc.desc: test create idle timer for app.
350 * @tc.type: FUNC
351 * @tc.require:
352 */
353 HWTEST_F(TimeServiceTest, IdleTimer001, TestSize.Level0)
354 {
355 auto timerInfo = std::make_shared<TimerInfoTest>();
356 timerInfo->SetType(timerInfo->TIMER_TYPE_IDLE);
357 timerInfo->SetRepeat(false);
358 uint64_t timerId = 0;
359 TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId);
360 EXPECT_NE(timerId, static_cast<uint64_t>(0));
361 TimeServiceClient::GetInstance()->DestroyTimerV9(timerId);
362 }
363
364 /**
365 * @tc.name: IdleTimer002
366 * @tc.desc: test public app start timer when device is sleeping and device sleep quit greater than timer callback.
367 * @tc.type: FUNC
368 * @tc.require:
369 */
370 HWTEST_F(TimeServiceTest, IdleTimer002, TestSize.Level0)
371 {
372 g_data1 = 0;
373 auto timerInfo = std::make_shared<TimerInfoTest>();
374 timerInfo->SetType(timerInfo->TIMER_TYPE_INEXACT_REMINDER);
375 timerInfo->SetRepeat(false);
376 timerInfo->SetCallbackInfo(TimeOutCallback1);
377 uint64_t timerId = 0;
378 TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId);
379 EXPECT_NE(timerId, static_cast<uint64_t>(0));
380 StartIdleTimer();
381 struct timeval currentTime {};
382 gettimeofday(¤tTime, nullptr);
383 int64_t time = currentTime.tv_sec * 1000 + currentTime.tv_usec / 1000;
384 TimeServiceClient::GetInstance()->StartTimerV9(timerId, static_cast<uint64_t>(time) + 2000);
385 sleep(2);
386 EXPECT_EQ(g_data1, 0);
387 DestroyIdleTimer();
388 sleep(1);
389 EXPECT_EQ(g_data1, 1);
390 TimeServiceClient::GetInstance()->DestroyTimerV9(timerId);
391 }
392
393 /**
394 * @tc.name: IdleTimer003
395 * @tc.desc: test public app start timer when device is sleeping and device sleep quit less than timer callback.
396 * @tc.type: FUNC
397 * @tc.require:
398 */
399 HWTEST_F(TimeServiceTest, IdleTimer003, TestSize.Level0)
400 {
401 g_data1 = 0;
402 auto timerInfo = std::make_shared<TimerInfoTest>();
403 timerInfo->SetType(timerInfo->TIMER_TYPE_INEXACT_REMINDER);
404 timerInfo->SetRepeat(false);
405 timerInfo->SetCallbackInfo(TimeOutCallback1);
406 uint64_t timerId = 0;
407 TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId);
408 EXPECT_NE(timerId, static_cast<uint64_t>(0));
409 StartIdleTimer();
410 struct timeval currentTime {};
411 gettimeofday(¤tTime, nullptr);
412 int64_t time = currentTime.tv_sec * 1000 + currentTime.tv_usec / 1000;
413 TimeServiceClient::GetInstance()->StartTimerV9(timerId, static_cast<uint64_t>(time) + 6000);
414 sleep(6);
415 EXPECT_EQ(g_data1, 0);
416 DestroyIdleTimer();
417 sleep(6);
418 EXPECT_EQ(g_data1, 1);
419 TimeServiceClient::GetInstance()->DestroyTimerV9(timerId);
420 }
421
422 /**
423 * @tc.name: IdleTimer004
424 * @tc.desc: test public app start timer when device is working, device sleep immediately
425 * and timer callback greater than idle quit.
426 * @tc.type: FUNC
427 * @tc.require:
428 */
429 HWTEST_F(TimeServiceTest, IdleTimer004, TestSize.Level0)
430 {
431 g_data1 = 0;
432 auto timerInfo = std::make_shared<TimerInfoTest>();
433 timerInfo->SetType(timerInfo->TIMER_TYPE_INEXACT_REMINDER);
434 timerInfo->SetRepeat(false);
435 timerInfo->SetCallbackInfo(TimeOutCallback1);
436 uint64_t timerId = 0;
437 TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId);
438 EXPECT_NE(timerId, static_cast<uint64_t>(0));
439 struct timeval currentTime {};
440 gettimeofday(¤tTime, nullptr);
441 int64_t time = currentTime.tv_sec * 1000 + currentTime.tv_usec / 1000;
442 TimeServiceClient::GetInstance()->StartTimerV9(timerId, static_cast<uint64_t>(time + 6000));
443 StartIdleTimer();
444 sleep(6);
445 DestroyIdleTimer();
446 EXPECT_EQ(g_data1, 1);
447 TimeServiceClient::GetInstance()->DestroyTimerV9(timerId);
448 }
449
450 /**
451 * @tc.name: SetTime001
452 * @tc.desc: set system time.
453 * @tc.type: FUNC
454 */
455 HWTEST_F(TimeServiceTest, SetTime001, TestSize.Level1)
456 {
457 AddPermission();
458 struct timeval currentTime {
459 };
460 gettimeofday(¤tTime, NULL);
461 int64_t time = (currentTime.tv_sec + 1000) * 1000 + currentTime.tv_usec / 1000;
462 ASSERT_GT(time, 0);
463 TIME_HILOGI(TIME_MODULE_CLIENT, "Time now : %{public}" PRId64 "", time);
464 bool result = TimeServiceClient::GetInstance()->SetTime(time);
465 EXPECT_TRUE(result);
466 DeletePermission();
467 }
468
469 /**
470 * @tc.name: SetTime002
471 * @tc.desc: set system time.
472 * @tc.type: FUNC
473 */
474 HWTEST_F(TimeServiceTest, SetTime002, TestSize.Level1)
475 {
476 bool result = TimeServiceClient::GetInstance()->SetTime(-1);
477 EXPECT_FALSE(result);
478 }
479
480 /**
481 * @tc.name: SetTime003
482 * @tc.desc: set system time.
483 * @tc.type: FUNC
484 */
485 HWTEST_F(TimeServiceTest, SetTime003, TestSize.Level1)
486 {
487 bool result = TimeServiceClient::GetInstance()->SetTime(LLONG_MAX);
488 EXPECT_FALSE(result);
489 }
490
491 /**
492 * @tc.name: SetTime004
493 * @tc.desc: set system time.
494 * @tc.type: FUNC
495 */
496 HWTEST_F(TimeServiceTest, SetTime004, TestSize.Level1)
497 {
498 AddPermission();
499 struct timeval currentTime {
500 };
501 gettimeofday(¤tTime, NULL);
502 int64_t time = (currentTime.tv_sec + 1000) * 1000 + currentTime.tv_usec / 1000;
503 ASSERT_GT(time, 0);
504 TIME_HILOGI(TIME_MODULE_CLIENT, "Time now : %{public}" PRId64 "", time);
505 int32_t code;
506 bool result = TimeServiceClient::GetInstance()->SetTime(time, code);
507 EXPECT_TRUE(result);
508 EXPECT_EQ(code, 0);
509 DeletePermission();
510 }
511
512 /**
513 * @tc.name: SetTimeZone001
514 * @tc.desc: set system time zone.
515 * @tc.type: FUNC
516 */
517 HWTEST_F(TimeServiceTest, SetTimeZone001, TestSize.Level1)
518 {
519 AddPermission();
520 time_t t;
521 (void)time(&t);
522 TIME_HILOGI(TIME_MODULE_CLIENT, "Time before: %{public}s", asctime(localtime(&t)));
523 auto getCurrentTimeZone = TimeServiceClient::GetInstance()->GetTimeZone();
524 EXPECT_FALSE(getCurrentTimeZone.empty());
525
526 std::string timeZoneNicosia("Asia/Nicosia");
527 bool result = TimeServiceClient::GetInstance()->SetTimeZone(timeZoneNicosia);
528 EXPECT_TRUE(result);
529 auto getTimeZoneNicosia = TimeServiceClient::GetInstance()->GetTimeZone();
530 EXPECT_EQ(timeZoneNicosia, getTimeZoneNicosia);
531 bool ret = TimeServiceClient::GetInstance()->SetTimeZone(getCurrentTimeZone);
532 EXPECT_TRUE(ret);
533 DeletePermission();
534 }
535
536 /**
537 * @tc.name: SetTimeZone002
538 * @tc.desc: set system time zone.
539 * @tc.type: FUNC
540 */
541 HWTEST_F(TimeServiceTest, SetTimeZone002, TestSize.Level1)
542 {
543 bool result = TimeServiceClient::GetInstance()->SetTimeZone("123");
544 EXPECT_FALSE(result);
545 }
546
547 /**
548 * @tc.name: SetTimeZone003
549 * @tc.desc: set system time zone.
550 * @tc.type: FUNC
551 */
552 HWTEST_F(TimeServiceTest, SetTimeZone003, TestSize.Level1)
553 {
554 AddPermission();
555 time_t t;
556 (void)time(&t);
557 TIME_HILOGI(TIME_MODULE_CLIENT, "Time before: %{public}s", asctime(localtime(&t)));
558 auto getCurrentTimeZone = TimeServiceClient::GetInstance()->GetTimeZone();
559 EXPECT_FALSE(getCurrentTimeZone.empty());
560
561 std::string timeZoneShanghai("Asia/Shanghai");
562 int32_t code;
563 bool result = TimeServiceClient::GetInstance()->SetTimeZone(timeZoneShanghai, code);
564 EXPECT_TRUE(result);
565 EXPECT_EQ(code, 0);
566 auto getTimeZone = TimeServiceClient::GetInstance()->GetTimeZone();
567 EXPECT_EQ(getTimeZone, timeZoneShanghai);
568 bool ret = TimeServiceClient::GetInstance()->SetTimeZone(getCurrentTimeZone);
569 EXPECT_TRUE(ret);
570 DeletePermission();
571 }
572
573 /**
574 * @tc.name: GetWallTimeMs001
575 * @tc.desc: get wall time (ms).
576 * @tc.type: FUNC
577 */
578 HWTEST_F(TimeServiceTest, GetWallTimeMs001, TestSize.Level1)
579 {
580 auto time1 = TimeServiceClient::GetInstance()->GetWallTimeMs();
581 EXPECT_NE(time1, -1);
582 auto time2 = TimeServiceClient::GetInstance()->GetWallTimeMs();
583 EXPECT_GE(time2, time1);
584 }
585
586 /**
587 * @tc.name: GetWallTimeNs001
588 * @tc.desc: get wall time (ns).
589 * @tc.type: FUNC
590 */
591 HWTEST_F(TimeServiceTest, GetWallTimeNs001, TestSize.Level1)
592 {
593 auto time1 = TimeServiceClient::GetInstance()->GetWallTimeNs();
594 EXPECT_NE(time1, -1);
595 auto time2 = TimeServiceClient::GetInstance()->GetWallTimeNs();
596 EXPECT_GE(time2, time1);
597 }
598
599 /**
600 * @tc.name: GetBootTimeNs001
601 * @tc.desc: get boot time (ns).
602 * @tc.type: FUNC
603 */
604 HWTEST_F(TimeServiceTest, GetBootTimeNs001, TestSize.Level1)
605 {
606 auto time1 = TimeServiceClient::GetInstance()->GetBootTimeNs();
607 EXPECT_NE(time1, -1);
608 auto time2 = TimeServiceClient::GetInstance()->GetBootTimeNs();
609 EXPECT_GE(time2, time1);
610 }
611
612 /**
613 * @tc.name: GetMonotonicTimeMs001
614 * @tc.desc: get monotonic time (ms).
615 * @tc.type: FUNC
616 */
617 HWTEST_F(TimeServiceTest, GetMonotonicTimeMs001, TestSize.Level1)
618 {
619 auto time1 = TimeServiceClient::GetInstance()->GetMonotonicTimeMs();
620 EXPECT_NE(time1, -1);
621 auto time2 = TimeServiceClient::GetInstance()->GetMonotonicTimeMs();
622 EXPECT_GE(time2, time1);
623 }
624
625 /**
626 * @tc.name: GetMonotonicTimeNs001
627 * @tc.desc: get monotonic time (ns).
628 * @tc.type: FUNC
629 */
630 HWTEST_F(TimeServiceTest, GetMonotonicTimeNs001, TestSize.Level1)
631 {
632 auto time1 = TimeServiceClient::GetInstance()->GetMonotonicTimeNs();
633 EXPECT_NE(time1, -1);
634 auto time2 = TimeServiceClient::GetInstance()->GetMonotonicTimeNs();
635 EXPECT_GE(time2, time1);
636 }
637
638 /**
639 * @tc.name: GetThreadTimeMs001
640 * @tc.desc: get thread time (ms).
641 * @tc.type: FUNC
642 */
643 HWTEST_F(TimeServiceTest, GetThreadTimeMs001, TestSize.Level1)
644 {
645 auto time1 = TimeServiceClient::GetInstance()->GetThreadTimeMs();
646 EXPECT_NE(time1, -1);
647 }
648
649 /**
650 * @tc.name: GetThreadTimeNs001
651 * @tc.desc: get thread time (ns).
652 * @tc.type: FUNC
653 */
654 HWTEST_F(TimeServiceTest, GetThreadTimeNs001, TestSize.Level1)
655 {
656 auto time1 = TimeServiceClient::GetInstance()->GetThreadTimeNs();
657 EXPECT_NE(time1, -1);
658 }
659
660 /**
661 * @tc.name: CreateTimer001
662 * @tc.desc: Create system timer.
663 * @tc.type: FUNC
664 */
665 HWTEST_F(TimeServiceTest, CreateTimer001, TestSize.Level1)
666 {
667 AddPermission();
668 uint64_t timerId = 0;
669 auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId, 5);
670 EXPECT_FALSE(ret);
671 ret = TimeServiceClient::GetInstance()->StopTimer(timerId);
672 EXPECT_FALSE(ret);
673 ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId);
674 EXPECT_FALSE(ret);
675 DeletePermission();
676 }
677
678 /**
679 * @tc.name: CreateTimer002
680 * @tc.desc: Create system timer.
681 * @tc.type: FUNC
682 */
683 HWTEST_F(TimeServiceTest, CreateTimer002, TestSize.Level1)
684 {
685 AddPermission();
686 auto timerInfo = std::make_shared<TimerInfoTest>();
687 timerInfo->SetType(timerInfo->TIMER_TYPE_REALTIME);
688 timerInfo->SetRepeat(false);
689 timerInfo->SetInterval(0);
690 timerInfo->SetWantAgent(nullptr);
691 timerInfo->SetCallbackInfo(TimeOutCallback1);
692 auto timerId = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
693 TIME_HILOGI(TIME_MODULE_CLIENT, "timerId now : %{public}" PRId64 "", timerId);
694 EXPECT_GT(timerId, 0);
695 auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId, 2000);
696 EXPECT_TRUE(ret);
697 ret = TimeServiceClient::GetInstance()->StopTimer(timerId);
698 EXPECT_TRUE(ret);
699 ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId);
700 EXPECT_TRUE(ret);
701 DeletePermission();
702 }
703
704 /**
705 * @tc.name: CreateTimer003
706 * @tc.desc: Create system timer.
707 * @tc.type: FUNC
708 */
709 HWTEST_F(TimeServiceTest, CreateTimer003, TestSize.Level1)
710 {
711 AddPermission();
712 auto timerInfo = std::make_shared<TimerInfoTest>();
713 timerInfo->SetType(timerInfo->TIMER_TYPE_REALTIME);
714 timerInfo->SetRepeat(false);
715 timerInfo->SetInterval(0);
716 auto ability = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
717 timerInfo->SetWantAgent(ability);
718 timerInfo->SetCallbackInfo(TimeOutCallback1);
719 auto timerId = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
720 EXPECT_GT(timerId, 0);
721 DeletePermission();
722 }
723
724 /**
725 * @tc.name: CreateTimer004
726 * @tc.desc: Create system timer.
727 * @tc.type: FUNC
728 */
729 HWTEST_F(TimeServiceTest, CreateTimer004, TestSize.Level1)
730 {
731 AddPermission();
732 g_data1 = 0;
733 auto timerInfo = std::make_shared<TimerInfoTest>();
734 timerInfo->SetType(timerInfo->TIMER_TYPE_REALTIME);
735 timerInfo->SetRepeat(false);
736 timerInfo->SetInterval(0);
737 timerInfo->SetWantAgent(nullptr);
738 timerInfo->SetCallbackInfo(TimeOutCallback1);
739 auto timerId = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
740 EXPECT_GT(timerId, 0);
741 auto BootTimeNano = system_clock::now().time_since_epoch().count();
742 auto BootTimeMilli = BootTimeNano / NANO_TO_MILESECOND;
743 auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId, BootTimeMilli + 2000);
744 EXPECT_TRUE(ret);
745 ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId);
746 EXPECT_TRUE(ret);
747 EXPECT_EQ(g_data1, 0);
748 ret = TimeServiceClient::GetInstance()->StopTimer(timerId);
749 EXPECT_FALSE(ret);
750 DeletePermission();
751 }
752
753 /**
754 * @tc.name: CreateTimer005
755 * @tc.desc: Create system timer.
756 * @tc.type: FUNC
757 */
758 HWTEST_F(TimeServiceTest, CreateTimer005, TestSize.Level1)
759 {
760 AddPermission();
761 g_data1 = 1;
762 auto timerInfo = std::make_shared<TimerInfoTest>();
763 timerInfo->SetType(0);
764 timerInfo->SetRepeat(false);
765 timerInfo->SetInterval(0);
766 timerInfo->SetWantAgent(nullptr);
767 timerInfo->SetCallbackInfo(TimeOutCallback1);
768
769 struct timeval timeOfDay {
770 };
771 gettimeofday(&timeOfDay, NULL);
772 int64_t currentTime = (timeOfDay.tv_sec + 100) * 1000 + timeOfDay.tv_usec / 1000;
773 if (currentTime < 0) {
774 currentTime = 0;
775 }
776 auto timerId = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
777 EXPECT_GT(timerId, 0);
778
779 auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId, static_cast<uint64_t>(currentTime));
780 EXPECT_TRUE(ret);
781 ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId);
782 EXPECT_TRUE(ret);
783 EXPECT_EQ(g_data1, 1);
784
785 ret = TimeServiceClient::GetInstance()->StopTimer(timerId);
786 EXPECT_FALSE(ret);
787 DeletePermission();
788 }
789
790 /**
791 * @tc.name: CreateTimer006
792 * @tc.desc: Create system timer.
793 * @tc.type: FUNC
794 */
795 HWTEST_F(TimeServiceTest, CreateTimer006, TestSize.Level1)
796 {
797 AddPermission();
798 auto timerId = TimeServiceClient::GetInstance()->CreateTimer(nullptr);
799 uint64_t ret = 0;
800 EXPECT_EQ(timerId, ret);
801 DeletePermission();
802 }
803
804 /**
805 * @tc.name: CreateTimer007
806 * @tc.desc: Create system timer with TIMER_TYPE_EXACT, then start timer with uint64_t::max.
807 * @tc.type: FUNC
808 */
809 HWTEST_F(TimeServiceTest, CreateTimer007, TestSize.Level1)
810 {
811 AddPermission();
812 g_data1 = 0;
813 auto timerInfo = std::make_shared<TimerInfoTest>();
814 timerInfo->SetType(timerInfo->TIMER_TYPE_EXACT);
815 timerInfo->SetRepeat(false);
816 timerInfo->SetInterval(0);
817 timerInfo->SetWantAgent(nullptr);
818 timerInfo->SetCallbackInfo(TimeOutCallback1);
819 auto timerId = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
820 EXPECT_GT(timerId, 0);
821 uint64_t max = std::numeric_limits<uint64_t>::max();
822 auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId, max);
823 sleep(1);
824 EXPECT_TRUE(ret);
825 EXPECT_EQ(g_data1, 0);
826 ret = TimeServiceClient::GetInstance()->StopTimer(timerId);
827 EXPECT_TRUE(ret);
828 ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId);
829 EXPECT_TRUE(ret);
830 DeletePermission();
831 }
832
833 /**
834 * @tc.name: CreateTimer008
835 * @tc.desc: Create system timer with TIMER_TYPE_REALTIME and TIMER_TYPE_EXACT, then start timer with uint64_t::max.
836 * @tc.type: FUNC
837 */
838 HWTEST_F(TimeServiceTest, CreateTimer008, TestSize.Level1)
839 {
840 AddPermission();
841 g_data1 = 0;
842 auto timerInfo = std::make_shared<TimerInfoTest>();
843 timerInfo->SetType(timerInfo->TIMER_TYPE_REALTIME | timerInfo->TIMER_TYPE_EXACT);
844 timerInfo->SetRepeat(false);
845 timerInfo->SetInterval(0);
846 timerInfo->SetWantAgent(nullptr);
847 timerInfo->SetCallbackInfo(TimeOutCallback1);
848 auto timerId = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
849 EXPECT_GT(timerId, 0);
850 uint64_t max = std::numeric_limits<uint64_t>::max();
851 auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId, max);
852 sleep(1);
853 EXPECT_TRUE(ret);
854 EXPECT_EQ(g_data1, 0);
855 ret = TimeServiceClient::GetInstance()->StopTimer(timerId);
856 EXPECT_TRUE(ret);
857 ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId);
858 EXPECT_TRUE(ret);
859 DeletePermission();
860 }
861
862 /**
863 * @tc.name: CreateTimer009
864 * @tc.desc: Create system timer start with one day later, then setTime to one day later.
865 * @tc.type: FUNC
866 */
867 HWTEST_F(TimeServiceTest, CreateTimer009, TestSize.Level1)
868 {
869 AddPermission();
870 g_data1 = 0;
871 struct timeval currentTime {
872 };
873 gettimeofday(¤tTime, NULL);
874 // Set the time to one day later
875 int64_t time = (currentTime.tv_sec + 86400) * 1000 + currentTime.tv_usec / 1000;
876 TIME_HILOGI(TIME_MODULE_CLIENT, "Time now : %{public}" PRId64 "", time);
877 ASSERT_GT(time, 0);
878
879 auto timerInfo = std::make_shared<TimerInfoTest>();
880 timerInfo->SetType(timerInfo->TIMER_TYPE_EXACT);
881 timerInfo->SetRepeat(true);
882 timerInfo->SetInterval(1000);
883 timerInfo->SetWantAgent(nullptr);
884 timerInfo->SetCallbackInfo(TimeOutCallback1);
885 auto timerId = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
886 EXPECT_GT(timerId, 0);
887 auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId, time);
888 EXPECT_TRUE(ret);
889
890 ret = TimeServiceClient::GetInstance()->SetTime(time);
891 EXPECT_TRUE(ret);
892
893 // wait for the second trigger success
894 while (g_data1 < 2) {
895 usleep(100000);
896 }
897 ret = TimeServiceClient::GetInstance()->StopTimer(timerId);
898 EXPECT_TRUE(ret);
899 ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId);
900 EXPECT_TRUE(ret);
901 DeletePermission();
902 }
903
904 /**
905 * @tc.name: CreateTimer010
906 * @tc.desc: Create system timer.
907 * @tc.type: FUNC
908 */
909 HWTEST_F(TimeServiceTest, CreateTimer010, TestSize.Level1) {
910 AddPermission();
911 uint64_t timerId = 0;
912 auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId, 5);
913 EXPECT_FALSE(ret);
914 ret = TimeServiceClient::GetInstance()->StopTimer(timerId);
915 EXPECT_FALSE(ret);
916 ret = TimeServiceClient::GetInstance()->DestroyTimerAsync(timerId);
917 EXPECT_TRUE(ret);
918 DeletePermission();
919 }
920
921 /**
922 * @tc.name: CreateTimer011
923 * @tc.desc: Create system timer.
924 * @tc.type: FUNC
925 */
926 HWTEST_F(TimeServiceTest, CreateTimer011, TestSize.Level1) {
927 AddPermission();
928 g_data1 = 0;
929 auto timerInfo = std::make_shared<TimerInfoTest>();
930 timerInfo->SetType(timerInfo->TIMER_TYPE_REALTIME |
931 timerInfo->TIMER_TYPE_EXACT);
932 timerInfo->SetRepeat(false);
933 timerInfo->SetInterval(0);
934 timerInfo->SetWantAgent(nullptr);
935 timerInfo->SetCallbackInfo(TimeOutCallback1);
936 auto timerId = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
937 EXPECT_GT(timerId, 0);
938 uint64_t max = std::numeric_limits<uint64_t>::max();
939 auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId, max);
940 EXPECT_TRUE(ret);
941 EXPECT_EQ(g_data1, 0);
942 ret = TimeServiceClient::GetInstance()->StopTimer(timerId);
943 EXPECT_TRUE(ret);
944 ret = TimeServiceClient::GetInstance()->DestroyTimerAsync(timerId);
945 EXPECT_TRUE(ret);
946 DeletePermission();
947 }
948
949 /**
950 * @tc.name: SntpClient001.
951 * @tc.desc: test SntpClient.
952 * @tc.type: FUNC
953 * @tc.require:
954 */
955 HWTEST_F(TimeServiceTest, SntpClient001, TestSize.Level0)
956 {
957 std::shared_ptr<SNTPClient> ntpClient = std::make_shared<SNTPClient>();
958
959 auto buffer = std::string("31234114451");
960 auto millisecond = ntpClient->GetNtpTimestamp64(0, buffer.c_str());
961 EXPECT_GT(millisecond, 0);
962 millisecond = 0;
963 millisecond = ntpClient->GetNtpField32(0, buffer.c_str());
964 EXPECT_GT(millisecond, 0);
965
966 auto timeStamp = ntpClient->ConvertNtpToStamp(0);
967 EXPECT_EQ(timeStamp, 0);
968 timeStamp = ntpClient->ConvertNtpToStamp(100);
969 EXPECT_EQ(timeStamp, 0);
970 timeStamp = ntpClient->ConvertNtpToStamp(2147483648);
971 EXPECT_EQ(timeStamp, 0);
972 timeStamp = ntpClient->ConvertNtpToStamp(31234114451);
973 EXPECT_EQ(timeStamp, 0);
974 uint64_t time = 999999999911;
975 timeStamp = ntpClient->ConvertNtpToStamp(time << 32);
976 EXPECT_GT(timeStamp, 0);
977 }
978
979 /**
980 * @tc.name: NtpTrustedTime001.
981 * @tc.desc: test NtpTrustedTime.
982 * @tc.type: FUNC
983 * @tc.require:
984 */
985 HWTEST_F(TimeServiceTest, NtpTrustedTime001, TestSize.Level0)
986 {
987 std::shared_ptr<NtpTrustedTime> ntpTrustedTime = std::make_shared<NtpTrustedTime>();
988 ntpTrustedTime->mTimeResult = nullptr;
989 int64_t errCode = ntpTrustedTime->CurrentTimeMillis();
990 EXPECT_EQ(errCode, -1);
991 errCode = ntpTrustedTime->GetCacheAge();
992 EXPECT_EQ(errCode, INT_MAX);
993
994 ntpTrustedTime->mTimeResult = std::make_shared<NtpTrustedTime::TimeResult>(1, 1, 0);
995 int64_t time = ntpTrustedTime->CurrentTimeMillis();
996 EXPECT_GT(time, 0);
997 int64_t cacheAge = ntpTrustedTime->GetCacheAge();
998 EXPECT_GT(cacheAge, 0);
999 }
1000
1001 /**
1002 * @tc.name: TimeTick001
1003 * @tc.desc: Check RefreshNextTriggerTime().
1004 * @tc.type: FUNC
1005 * @tc.require:
1006 */
1007 HWTEST_F(TimeServiceTest, TimeTick001, TestSize.Level0)
1008 {
1009 AddPermission();
1010 int64_t time = 0;
1011 TimeUtils::GetWallTimeMs(time);
1012 time = (time / MINUTE_TO_MILLISECOND) * MINUTE_TO_MILLISECOND;
1013 bool result = TimeServiceClient::GetInstance()->SetTime(time);
1014 EXPECT_TRUE(result);
1015 auto pair = TimeTickNotify::GetInstance().RefreshNextTriggerTime();
1016 EXPECT_EQ(pair.first, time + MINUTE_TO_MILLISECOND);
1017 EXPECT_TRUE(pair.second);
1018 DeletePermission();
1019 }
1020
1021 /**
1022 * @tc.name: SetSystemTime001
1023 * @tc.desc: get ntp time.
1024 * @tc.type: FUNC
1025 */
1026 HWTEST_F(TimeServiceTest, SetSystemTime001, TestSize.Level1)
1027 {
1028 NtpUpdateTime::GetInstance().Init();
1029 std::string tmp = NtpUpdateTime::autoTimeInfo_.status;
1030 NtpUpdateTime::autoTimeInfo_.status = AUTO_TIME_STATUS_ON;
1031
1032 std::thread thread1(TestNtpThread, "thread1");
1033 std::thread thread2(TestNtpThread, "thread2");
1034 std::thread thread3(TestNtpThread, "thread3");
1035 std::thread thread4(TestNtpThread, "thread4");
1036 thread1.join();
1037 thread2.join();
1038 thread3.join();
1039 thread4.join();
1040
1041 NtpUpdateTime::autoTimeInfo_.status = tmp;
1042 }
1043
1044 /**
1045 * @tc.name: Batch001.
1046 * @tc.desc: test Batch.
1047 * @tc.type: FUNC
1048 */
1049 HWTEST_F(TimeServiceTest, Batch001, TestSize.Level0)
1050 {
1051 Batch batch;
1052 EXPECT_EQ(batch.GetStart(), std::chrono::steady_clock::time_point::min());
1053 EXPECT_EQ(batch.GetEnd(), std::chrono::steady_clock::time_point::max());
1054 EXPECT_EQ(batch.GetFlags(), 0);
1055 }
1056
1057 /**
1058 * @tc.name: TimerManager001.
1059 * @tc.desc: test ReCreateTimer.
1060 * @tc.type: FUNC
1061 */
1062 HWTEST_F(TimeServiceTest, TimerManager001, TestSize.Level0)
1063 {
1064 auto timerId1 = TIMER_ID;
1065 auto entry = std::make_shared<TimerEntry>(
1066 TimerEntry{"", timerId1, 0, 0, 0, 0, false, nullptr, nullptr, 0, 0, "bundleName"});
1067 TimerManager::GetInstance()->ReCreateTimer(timerId1, entry);
1068 std::lock_guard<std::mutex> lock(TimerManager::GetInstance()->entryMapMutex_);
1069
1070 auto map = TimerManager::GetInstance()->timerEntryMap_;
1071 auto it = map.find(timerId1);
1072 EXPECT_NE(it, map.end());
1073 if (it != map.end()) {
1074 map.erase(it);
1075 }
1076 }
1077
1078 /**
1079 * @tc.name: TimerManager002.
1080 * @tc.desc: test SetHandler with interval = milliseconds(10) < second(1).
1081 * @tc.type: FUNC
1082 */
1083 HWTEST_F(TimeServiceTest, TimerManager002, TestSize.Level0)
1084 {
1085 uint64_t max = std::numeric_limits<uint64_t>::max();
1086 TimerManager::GetInstance()->SetHandler("",
1087 TIMER_ID,
1088 0,
1089 max,
1090 10,
1091 0,
1092 1,
1093 false,
1094 nullptr,
1095 nullptr,
1096 0,
1097 0,
1098 "bundleName");
1099 std::lock_guard<std::mutex> lock(TimerManager::GetInstance()->entryMapMutex_);
1100 auto map = TimerManager::GetInstance()->timerEntryMap_;
1101 auto it = map.find(TIMER_ID);
1102 EXPECT_NE(it, map.end());
1103 if (it != map.end()) {
1104 map.erase(it);
1105 }
1106 }
1107
1108 /**
1109 * @tc.name: TimerManager003.
1110 * @tc.desc: test Set() with type > ALARM_TYPE_COUNT.
1111 * @tc.type: FUNC
1112 */
1113 HWTEST_F(TimeServiceTest, TimerManager003, TestSize.Level0)
1114 {
1115 auto when = std::chrono::nanoseconds::zero();
1116 auto bootTime = std::chrono::steady_clock::now();
1117 auto res = TimerManager::GetInstance()->handler_->Set(6, when, bootTime);
1118 EXPECT_EQ(res, -1);
1119 }
1120
1121 /**
1122 * @tc.name: TimerManager004.
1123 * @tc.desc: test StartTimer with UidProxy and PidProxy.
1124 * @tc.type: FUNC
1125 */
1126 HWTEST_F(TimeServiceTest, TimerManager004, TestSize.Level0)
1127 {
1128 TimerManager::GetInstance()->DestroyTimer(TIMER_ID);
1129 auto entry = std::make_shared<TimerEntry>(
1130 TimerEntry{"", TIMER_ID, 0, 0, 0, 0, false, nullptr, nullptr, UID, PID, "bundleName"});
1131 TimerManager::GetInstance()->ReCreateTimer(TIMER_ID, entry);
1132
1133 {
1134 std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().proxyMutex_);
1135 std::unordered_map<uint64_t, std::chrono::steady_clock::time_point> timePointMap {};
1136 TimerProxy::GetInstance().proxyUids_.insert(std::make_pair(UID, timePointMap));
1137 }
1138 auto res = TimerManager::GetInstance()->StartTimer(TIMER_ID, 0);
1139 EXPECT_EQ(res, E_TIME_OK);
1140
1141 {
1142 std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().proxyMutex_);
1143 auto map = TimerProxy::GetInstance().proxyUids_;
1144 auto it = map.find(UID);
1145 if (it != map.end()) {
1146 map.erase(it);
1147 }
1148 }
1149 {
1150 std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().proxyPidMutex_);
1151 auto map = TimerProxy::GetInstance().proxyPids_;
1152 std::unordered_map<uint64_t, std::chrono::steady_clock::time_point> timePointMap {};
1153 map.insert(std::make_pair(UID, timePointMap));
1154 }
1155 res = TimerManager::GetInstance()->StartTimer(TIMER_ID, 0);
1156 EXPECT_EQ(res, E_TIME_OK);
1157
1158 {
1159 std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().proxyPidMutex_);
1160 auto map = TimerProxy::GetInstance().proxyPids_;
1161 auto it = map.find(PID);
1162 if (it != map.end()) {
1163 map.erase(it);
1164 }
1165 }
1166
1167 TimerManager::GetInstance()->DestroyTimer(TIMER_ID);
1168 }
1169
1170 /**
1171 * @tc.name: TimerManager005.
1172 * @tc.desc: test NotifyWantAgent.
1173 * @tc.type: FUNC
1174 */
1175 HWTEST_F(TimeServiceTest, TimerManager005, TestSize.Level0)
1176 {
1177 TimerManager::GetInstance()->NotifyWantAgentRetry(nullptr);
1178
1179 auto duration = std::chrono::milliseconds::zero();
1180 auto timePoint = std::chrono::steady_clock::now();
1181 auto timerInfo = std::make_shared<TimerInfo>("", TIMER_ID, 0, duration, timePoint, duration, timePoint, duration,
1182 nullptr, nullptr, 0, false, 0, 0, "");
1183 auto res = TimerManager::GetInstance()->NotifyWantAgent(timerInfo);
1184 EXPECT_FALSE(res);
1185
1186 OHOS::NativeRdb::ValuesBucket insertValues;
1187 insertValues.PutLong("timerId", TIMER_ID);
1188 insertValues.PutInt("type", 0);
1189 insertValues.PutInt("flag", 0);
1190 insertValues.PutLong("windowLength", 0);
1191 insertValues.PutLong("interval", 0);
1192 insertValues.PutInt("uid", 0);
1193 insertValues.PutString("bundleName", "");
1194 std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent = nullptr;
1195 insertValues.PutString("wantAgent", OHOS::AbilityRuntime::WantAgent::WantAgentHelper::ToString(wantAgent));
1196 insertValues.PutInt("state", 0);
1197 insertValues.PutLong("triggerTime", static_cast<int64_t>(std::numeric_limits<int64_t>::max()));
1198 TimeDatabase::GetInstance().Insert(HOLD_ON_REBOOT, insertValues);
1199
1200 res = TimerManager::GetInstance()->NotifyWantAgent(timerInfo);
1201 EXPECT_FALSE(res);
1202
1203 OHOS::NativeRdb::RdbPredicates rdbPredicatesDelete(HOLD_ON_REBOOT);
1204 rdbPredicatesDelete.EqualTo("timerId", static_cast<int64_t>(TIMER_ID));
1205 TimeDatabase::GetInstance().Delete(rdbPredicatesDelete);
1206 }
1207
1208 /**
1209 * @tc.name: TimerManager006.
1210 * @tc.desc: test AdjustTimer.
1211 * @tc.type: FUNC
1212 */
1213 HWTEST_F(TimeServiceTest, TimerManager006, TestSize.Level0)
1214 {
1215 uint32_t interval;
1216 bool isAdjust;
1217 // Set 1000 as interval, because interval can not be 0;
1218 uint32_t intervalSet = 1000;
1219 {
1220 std::lock_guard<std::mutex> lock(TimerManager::GetInstance()->mutex_);
1221 interval = TimerManager::GetInstance()->adjustInterval_;
1222 TimerManager::GetInstance()->adjustInterval_ = intervalSet;
1223 isAdjust = TimerManager::GetInstance()->adjustPolicy_;
1224 }
1225
1226 auto res = TimerManager::GetInstance()->AdjustTimer(isAdjust, intervalSet);
1227 EXPECT_FALSE(res);
1228 res = TimerManager::GetInstance()->AdjustTimer(!isAdjust, intervalSet);
1229 EXPECT_TRUE(res);
1230 res = TimerManager::GetInstance()->AdjustTimer(isAdjust, intervalSet + 1);
1231 EXPECT_TRUE(res);
1232 res = TimerManager::GetInstance()->AdjustTimer(isAdjust, intervalSet);
1233 EXPECT_TRUE(res);
1234
1235 std::lock_guard<std::mutex> lock(TimerManager::GetInstance()->mutex_);
1236 TimerManager::GetInstance()->adjustInterval_ = interval;
1237 TimerManager::GetInstance()->adjustPolicy_ = isAdjust;
1238 }
1239
1240 /**
1241 * @tc.name: TimerManager007.
1242 * @tc.desc: test AdjustDeliveryTimeBasedOnDeviceIdle.
1243 * @tc.type: FUNC
1244 */
1245 HWTEST_F(TimeServiceTest, TimerManager007, TestSize.Level0)
1246 {
1247 auto duration = std::chrono::milliseconds::zero();
1248 auto timePoint = std::chrono::steady_clock::now();
1249 auto timerInfo1 = std::make_shared<TimerInfo>("", TIMER_ID, 0, duration, timePoint, duration, timePoint, duration,
1250 nullptr, nullptr, 0, false, 0, 0, "");
1251 std::lock_guard<std::mutex> lock(TimerManager::GetInstance()->mutex_);
1252 auto alarm = TimerManager::GetInstance()->mPendingIdleUntil_;
1253 TimerManager::GetInstance()->mPendingIdleUntil_ = timerInfo1;
1254 auto res = TimerManager::GetInstance()->AdjustDeliveryTimeBasedOnDeviceIdle(timerInfo1);
1255 EXPECT_FALSE(res);
1256
1257 TimerManager::GetInstance()->mPendingIdleUntil_ = nullptr;
1258 TimerManager::GetInstance()->delayedTimers_[TIMER_ID] = std::chrono::steady_clock::now();
1259 res = TimerManager::GetInstance()->AdjustDeliveryTimeBasedOnDeviceIdle(timerInfo1);
1260 EXPECT_TRUE(res);
1261 auto duration1 = std::chrono::duration_cast<std::chrono::milliseconds>(
1262 (timePoint + std::chrono::hours(1)).time_since_epoch());
1263 auto timerInfo2 = std::make_shared<TimerInfo>("", TIMER_ID, 1, duration1, timePoint, duration, timePoint, duration,
1264 nullptr, nullptr, 0, false, 0, 0, "");
1265 res = TimerManager::GetInstance()->AdjustDeliveryTimeBasedOnDeviceIdle(timerInfo2);
1266 EXPECT_TRUE(res);
1267 auto timerInfo3 = std::make_shared<TimerInfo>("", TIMER_ID, 2, duration, timePoint, duration, timePoint, duration,
1268 nullptr, nullptr, 0, false, 0, 0, "");
1269 res = TimerManager::GetInstance()->AdjustDeliveryTimeBasedOnDeviceIdle(timerInfo3);
1270 EXPECT_TRUE(res);
1271
1272 TimerManager::GetInstance()->mPendingIdleUntil_ = alarm;
1273 }
1274
1275 /**
1276 * @tc.name: TimerManager008.
1277 * @tc.desc: test ShowTimerEntryById TIMER_ID not in timerEntryMap_.
1278 * @tc.type: FUNC
1279 */
1280 HWTEST_F(TimeServiceTest, TimerManager008, TestSize.Level0)
1281 {
1282 TimerManager::GetInstance()->DestroyTimer(TIMER_ID);
1283
1284 auto res = TimerManager::GetInstance()->ShowTimerEntryById(0, TIMER_ID);
1285 EXPECT_FALSE(res);
1286 }
1287
1288 /**
1289 * @tc.name: TimerManager009.
1290 * @tc.desc: test ShowTimerTriggerById TIMER_ID in alarmBatches_.
1291 * @tc.type: FUNC
1292 */
1293 HWTEST_F(TimeServiceTest, TimerManager009, TestSize.Level0)
1294 {
1295 auto entry = std::make_shared<TimerEntry>(
1296 TimerEntry{"", TIMER_ID, 0, 0, 0, 0, false, nullptr, nullptr, 0, 0, "bundleName"});
1297 TimerManager::GetInstance()->ReCreateTimer(TIMER_ID, entry);
1298 uint64_t triggerTime = std::numeric_limits<uint64_t>::max();
1299 TimerManager::GetInstance()->StartTimer(TIMER_ID, triggerTime);
1300 auto res = TimerManager::GetInstance()->ShowTimerTriggerById(0, TIMER_ID);
1301 TimerManager::GetInstance()->DestroyTimer(TIMER_ID);
1302 EXPECT_TRUE(res);
1303 }
1304
1305 /**
1306 * @tc.name: TimerManager010.
1307 * @tc.desc: test HandleRSSDeath.
1308 * @tc.type: FUNC
1309 */
1310 HWTEST_F(TimeServiceTest, TimerManager010, TestSize.Level0)
1311 {
1312 std::shared_ptr<TimerInfo> alarm;
1313 {
1314 std::lock_guard <std::mutex> lock(TimerManager::GetInstance()->mutex_);
1315 alarm = TimerManager::GetInstance()->mPendingIdleUntil_;
1316 TimerManager::GetInstance()->mPendingIdleUntil_ = nullptr;
1317 }
1318 TimerManager::GetInstance()->HandleRSSDeath();
1319
1320 auto duration = std::chrono::milliseconds::zero();
1321 auto timePoint = std::chrono::steady_clock::now();
1322 auto timerInfo = std::make_shared<TimerInfo>("", TIMER_ID, 0, duration, timePoint, duration, timePoint, duration,
1323 nullptr, nullptr, 0, false, 0, 0, "");
1324 {
1325 std::lock_guard <std::mutex> lock(TimerManager::GetInstance()->mutex_);
1326 TimerManager::GetInstance()->mPendingIdleUntil_ = timerInfo;
1327 }
1328 auto entry = std::make_shared<TimerEntry>(
1329 TimerEntry{"", TIMER_ID, 0, 0, 0, 0, false, nullptr, nullptr, 0, 0, "bundleName"});
1330 TimerManager::GetInstance()->ReCreateTimer(TIMER_ID, entry);
1331 TimerManager::GetInstance()->HandleRSSDeath();
1332 auto res = TimerManager::GetInstance()->DestroyTimer(TIMER_ID);
1333 EXPECT_EQ(res, E_TIME_DEAL_FAILED);
1334
1335 {
1336 std::lock_guard <std::mutex> lock(TimerManager::GetInstance()->mutex_);
1337 TimerManager::GetInstance()->mPendingIdleUntil_ = alarm;
1338 }
1339 }
1340
1341 /**
1342 * @tc.name: TimerManager011.
1343 * @tc.desc: test TimerNotifyCallback GetInstance.
1344 * @tc.type: FUNC
1345 */
1346 HWTEST_F(TimeServiceTest, TimerManager011, TestSize.Level0)
1347 {
1348 auto timerManager = TimerManager::GetInstance();
1349 auto res = TimerNotifyCallback::GetInstance(timerManager);
1350 EXPECT_NE(res, nullptr);
1351 res = TimerNotifyCallback::GetInstance(timerManager);
1352 EXPECT_NE(res, nullptr);
1353 }
1354
1355 /**
1356 * @tc.name: TimerManager013.
1357 * @tc.desc: test record and delete of timerCount_.
1358 * @tc.type: FUNC
1359 */
1360 HWTEST_F(TimeServiceTest, TimerManager013, TestSize.Level0)
1361 {
1362 int uid1 = UID;
1363 int uid2 = UID + 1;
1364 TimerManager::GetInstance()->timerCount_.clear();
1365 for (int i = 0; i < 10; ++i) {
1366 TimerManager::GetInstance()->IncreaseTimerCount(uid1);
1367 }
1368 EXPECT_EQ(TimerManager::GetInstance()->timerCount_.size(), 1);
1369 auto it = std::find_if(TimerManager::GetInstance()->timerCount_.begin(),
1370 TimerManager::GetInstance()->timerCount_.end(),
__anone6369fc40202(const std::pair<int32_t, size_t>& pair) 1371 [uid1](const std::pair<int32_t, size_t>& pair) {
1372 return pair.first == uid1;
1373 });
1374 EXPECT_EQ(it->second, 10);
1375 for (int i = 0; i < 10; ++i) {
1376 TimerManager::GetInstance()->IncreaseTimerCount(uid2);
1377 }
1378 EXPECT_EQ(TimerManager::GetInstance()->timerCount_.size(), 2);
1379 for (int i = 0; i < 5; ++i) {
1380 TimerManager::GetInstance()->DecreaseTimerCount(uid2);
1381 }
1382 it = std::find_if(TimerManager::GetInstance()->timerCount_.begin(),
1383 TimerManager::GetInstance()->timerCount_.end(),
__anone6369fc40302(const std::pair<int32_t, size_t>& pair) 1384 [uid2](const std::pair<int32_t, size_t>& pair) {
1385 return pair.first == uid2;
1386 });
1387 EXPECT_EQ(it->second, 5);
1388 }
1389
1390 /**
1391 * @tc.name: TimerManager014.
1392 * @tc.desc: test when create and delete timer, the change of timerOutOfRangeTimes_.
1393 * @tc.type: FUNC
1394 */
1395 HWTEST_F(TimeServiceTest, TimerManager014, TestSize.Level0)
1396 {
1397 TimerManager::GetInstance()->timerEntryMap_.clear();
1398 TimerManager::GetInstance()->timerCount_.clear();
1399 TimerManager::GetInstance()->timerOutOfRangeTimes_ = 0;
1400 uint64_t i = 0;
1401 for (; i <= TIMER_ALARM_COUNT; ++i) {
1402 auto entry = std::make_shared<TimerEntry>(
1403 TimerEntry{"", i, 0, 0, 0, 0, false, nullptr, nullptr, 0, 0, "bundleName"});
1404 TimerManager::GetInstance()->ReCreateTimer(i, entry);
1405 }
1406 EXPECT_EQ(TimerManager::GetInstance()->timerOutOfRangeTimes_, 1);
1407 for (; i <= TIMER_ALARM_COUNT * 2; ++i) {
1408 auto entry = std::make_shared<TimerEntry>(
1409 TimerEntry{"", i, 0, 0, 0, 0, false, nullptr, nullptr, 0, 0, "bundleName"});
1410 TimerManager::GetInstance()->ReCreateTimer(i, entry);
1411 }
1412 EXPECT_EQ(TimerManager::GetInstance()->timerOutOfRangeTimes_, 2);
1413 }
1414
1415 /**
1416 * @tc.name: TimerManager015.
1417 * @tc.desc: test create two timer with same name.
1418 * @tc.type: FUNC
1419 */
1420 HWTEST_F(TimeServiceTest, TimerManager015, TestSize.Level0)
1421 {
1422 TIME_HILOGI(TIME_MODULE_CLIENT, "TimerManager015 start");
1423 TimerManager::GetInstance()->timerNameMap_.clear();
1424 auto entry = std::make_shared<TimerEntry>(
1425 TimerEntry{"name", TIMER_ID, 0, 0, 0, 0, false, nullptr, nullptr, UID, 0, "bundleName"});
1426 TimerManager::GetInstance()->ReCreateTimer(TIMER_ID, entry);
1427 auto timerNameMap = TimerManager::GetInstance()->timerNameMap_;
1428 EXPECT_NE(timerNameMap.find(UID), timerNameMap.end());
1429 EXPECT_NE(timerNameMap[UID].find("name"), timerNameMap[UID].end());
1430 EXPECT_EQ(timerNameMap[UID]["name"], TIMER_ID);
1431
1432 entry = std::make_shared<TimerEntry>(
1433 TimerEntry{"name", TIMER_ID + 1, 0, 0, 0, 0, false, nullptr, nullptr, UID, 0, "bundleName"});
1434 TimerManager::GetInstance()->ReCreateTimer(TIMER_ID + 1, entry);
1435 timerNameMap = TimerManager::GetInstance()->timerNameMap_;
1436 EXPECT_NE(timerNameMap.find(UID), timerNameMap.end());
1437 EXPECT_NE(timerNameMap[UID].find("name"), timerNameMap[UID].end());
1438 EXPECT_EQ(timerNameMap[UID]["name"], TIMER_ID + 1);
1439 auto ret = TimerManager::GetInstance()->DestroyTimer(TIMER_ID);
1440 EXPECT_NE(ret, E_TIME_OK);
1441 }
1442
1443 /**
1444 * @tc.name: SystemAbility001.
1445 * @tc.desc: test OnStop.
1446 * @tc.type: FUNC
1447 */
1448 HWTEST_F(TimeServiceTest, SystemAbility001, TestSize.Level0)
1449 {
1450 TimeSystemAbility::GetInstance()->OnStop();
1451 EXPECT_EQ(TimeSystemAbility::GetInstance()->state_, ServiceRunningState::STATE_NOT_START);
1452 TimeSystemAbility::GetInstance()->OnStop();
1453 EXPECT_EQ(TimeSystemAbility::GetInstance()->state_, ServiceRunningState::STATE_NOT_START);
1454 }
1455
1456 /**
1457 * @tc.name: SystemAbility002.
1458 * @tc.desc: test RecoverTimer.
1459 * @tc.type: FUNC
1460 */
1461 HWTEST_F(TimeServiceTest, SystemAbility002, TestSize.Level0)
1462 {
1463 uint64_t timerId1 = TIMER_ID;
1464 uint64_t timerId2 = TIMER_ID + 1;
1465
1466 auto map = TimerManager::GetInstance()->timerEntryMap_;
1467 auto it = map.find(timerId1);
1468 if (it != map.end()) {
1469 map.erase(it);
1470 }
1471 it = map.find(timerId2);
1472 if (it != map.end()) {
1473 map.erase(it);
1474 }
1475
1476 OHOS::NativeRdb::ValuesBucket insertValues1;
1477 insertValues1.PutLong("timerId", timerId1);
1478 insertValues1.PutInt("type", 0);
1479 insertValues1.PutInt("flag", 0);
1480 insertValues1.PutLong("windowLength", 0);
1481 insertValues1.PutLong("interval", 0);
1482 insertValues1.PutInt("uid", 0);
1483 insertValues1.PutString("bundleName", "");
1484 std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent = nullptr;
1485 insertValues1.PutString("wantAgent", OHOS::AbilityRuntime::WantAgent::WantAgentHelper::ToString(wantAgent));
1486 insertValues1.PutInt("state", 0);
1487 insertValues1.PutLong("triggerTime", static_cast<int64_t>(std::numeric_limits<int64_t>::max()));
1488 TimeDatabase::GetInstance().Insert(HOLD_ON_REBOOT, insertValues1);
1489
1490 OHOS::NativeRdb::ValuesBucket insertValues2;
1491 insertValues2.PutLong("timerId", timerId2);
1492 insertValues2.PutInt("type", 0);
1493 insertValues2.PutInt("flag", 0);
1494 insertValues2.PutLong("windowLength", 0);
1495 insertValues2.PutLong("interval", 0);
1496 insertValues2.PutInt("uid", 0);
1497 insertValues2.PutString("bundleName", "");
1498 wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
1499 insertValues2.PutString("wantAgent", OHOS::AbilityRuntime::WantAgent::WantAgentHelper::ToString(wantAgent));
1500 insertValues2.PutInt("state", 0);
1501 insertValues2.PutLong("triggerTime", static_cast<int64_t>(std::numeric_limits<int64_t>::max()));
1502 TimeDatabase::GetInstance().Insert(DROP_ON_REBOOT, insertValues2);
1503
1504 TimeSystemAbility::GetInstance()->RecoverTimer();
1505
1506 it = map.find(timerId1);
1507 EXPECT_EQ(it, map.end());
1508 it = map.find(timerId2);
1509 EXPECT_EQ(it, map.end());
1510
1511 OHOS::NativeRdb::RdbPredicates rdbPredicatesDelete1(HOLD_ON_REBOOT);
1512 rdbPredicatesDelete1.EqualTo("timerId", static_cast<int64_t>(timerId1));
1513 TimeDatabase::GetInstance().Delete(rdbPredicatesDelete1);
1514
1515 OHOS::NativeRdb::RdbPredicates rdbPredicatesDelete2(DROP_ON_REBOOT);
1516 rdbPredicatesDelete2.EqualTo("timerId", static_cast<int64_t>(timerId2));
1517 TimeDatabase::GetInstance().Delete(rdbPredicatesDelete2);
1518 }
1519
1520 /**
1521 * @tc.name: SystemAbility003.
1522 * @tc.desc: test SetAutoReboot.
1523 * @tc.type: FUNC
1524 */
1525 HWTEST_F(TimeServiceTest, SystemAbility003, TestSize.Level0)
1526 {
1527 uint64_t timerId1 = TIMER_ID;
1528
1529 TimeSystemAbility::GetInstance()->SetAutoReboot();
1530
1531 OHOS::NativeRdb::ValuesBucket insertValues1;
1532 insertValues1.PutLong("timerId", timerId1);
1533 insertValues1.PutInt("type", 0);
1534 insertValues1.PutInt("flag", 0);
1535 insertValues1.PutLong("windowLength", 0);
1536 insertValues1.PutLong("interval", 0);
1537 insertValues1.PutInt("uid", 0);
1538 insertValues1.PutString("bundleName", "anything");
1539 insertValues1.PutString("wantAgent", "");
1540 insertValues1.PutInt("state", 1);
1541 insertValues1.PutLong("triggerTime", static_cast<int64_t>(0));
1542 auto res = TimeDatabase::GetInstance().Insert(HOLD_ON_REBOOT, insertValues1);
1543 EXPECT_EQ(res, true);
1544
1545 TimeSystemAbility::GetInstance()->SetAutoReboot();
1546
1547 OHOS::NativeRdb::RdbPredicates rdbPredicatesDelete1(HOLD_ON_REBOOT);
1548 rdbPredicatesDelete1.EqualTo("timerId", static_cast<int64_t>(timerId1));
1549 TimeDatabase::GetInstance().Delete(rdbPredicatesDelete1);
1550 }
1551
1552 /**
1553 * @tc.name: SystemAbility004.
1554 * @tc.desc: test SetRealTime.
1555 * @tc.type: FUNC
1556 */
1557 HWTEST_F(TimeServiceTest, SystemAbility004, TestSize.Level0)
1558 {
1559 auto res = TimeSystemAbility::GetInstance()->SetRealTime(-1);
1560 EXPECT_FALSE(res);
1561 }
1562
1563 /**
1564 * @tc.name: TimeDatabase001.
1565 * @tc.desc: test TimeDatabase Insert.
1566 * @tc.type: FUNC
1567 */
1568 HWTEST_F(TimeServiceTest, TimeDatabase001, TestSize.Level0)
1569 {
1570 OHOS::NativeRdb::ValuesBucket insertValues;
1571 insertValues.PutLong("something", 0);
1572 auto res = TimeDatabase::GetInstance().Insert(DROP_ON_REBOOT, insertValues);
1573 EXPECT_FALSE(res);
1574 }
1575
1576 /**
1577 * @tc.name: TimeDatabase002.
1578 * @tc.desc: test TimeDatabase Update.
1579 * @tc.type: FUNC
1580 */
1581 HWTEST_F(TimeServiceTest, TimeDatabase002, TestSize.Level0)
1582 {
1583 OHOS::NativeRdb::ValuesBucket values;
1584 values.PutInt("something", 1);
1585 OHOS::NativeRdb::RdbPredicates rdbPredicates(DROP_ON_REBOOT);
1586 rdbPredicates.EqualTo("something", 0)->And()->EqualTo("something", static_cast<int64_t>(0));
1587 auto res = TimeDatabase::GetInstance().Update(values, rdbPredicates);
1588 EXPECT_FALSE(res);
1589 }
1590
1591 /**
1592 * @tc.name: TimeDatabase003.
1593 * @tc.desc: test TimeDatabase Delete.
1594 * @tc.type: FUNC
1595 */
1596 HWTEST_F(TimeServiceTest, TimeDatabase003, TestSize.Level0)
1597 {
1598 OHOS::NativeRdb::RdbPredicates rdbPredicatesDelete(DROP_ON_REBOOT);
1599 rdbPredicatesDelete.EqualTo("something", static_cast<int64_t>(0));
1600 auto res = TimeDatabase::GetInstance().Delete(rdbPredicatesDelete);
1601 EXPECT_FALSE(res);
1602 }
1603
1604 /**
1605 * @tc.name: TimerInfo001.
1606 * @tc.desc: test UpdateWhenElapsedFromNow.
1607 * @tc.type: FUNC
1608 */
1609 HWTEST_F(TimeServiceTest, TimerInfo001, TestSize.Level0)
1610 {
1611 auto duration = std::chrono::milliseconds::zero();
1612 auto timePoint = std::chrono::steady_clock::now();
1613 auto timerInfo = TimerInfo("", 0, 0, duration, timePoint, duration, timePoint, duration, nullptr,
1614 nullptr, 0, false, 0, 0, "");
1615 auto res = timerInfo.UpdateWhenElapsedFromNow(timePoint, duration);
1616 EXPECT_FALSE(res);
1617 }
1618
1619 /**
1620 * @tc.name: TimerInfo002.
1621 * @tc.desc: test AdjustTimer.
1622 * @tc.type: FUNC
1623 */
1624 HWTEST_F(TimeServiceTest, TimerInfo002, TestSize.Level0)
1625 {
1626 auto duration = std::chrono::milliseconds(0);
1627 auto timePoint = std::chrono::steady_clock::now();
1628 auto timerInfo = TimerInfo("", 0, 0, duration, timePoint, duration, timePoint, duration, nullptr,
1629 nullptr, 0, false, 0, 0, "");
1630 auto res = timerInfo.AdjustTimer(timePoint, 1);
1631 EXPECT_TRUE(res);
1632 }
1633
1634 /**
1635 * @tc.name: NtpTime001.
1636 * @tc.desc: test SplitNtpAddrs return max size.
1637 * @tc.type: FUNC
1638 */
1639 HWTEST_F(TimeServiceTest, NtpTime001, TestSize.Level0)
1640 {
1641 const std::string ntpStr = "aaa,bbb,ccc,ddd,eee,fff";
1642 auto res = NtpUpdateTime::GetInstance().SplitNtpAddrs(ntpStr);
1643 EXPECT_EQ(res.size(), 5);
1644 }
1645
1646 /**
1647 * @tc.name: NtpTime002.
1648 * @tc.desc: test RefreshNetworkTimeByTimer.
1649 * @tc.type: FUNC
1650 */
1651 HWTEST_F(TimeServiceTest, NtpTime002, TestSize.Level0)
1652 {
1653 auto status = NtpUpdateTime::GetInstance().autoTimeInfo_.status;
1654
1655 NtpUpdateTime::GetInstance().autoTimeInfo_.status = NETWORK_TIME_STATUS_OFF;
1656 NtpUpdateTime::GetInstance().RefreshNetworkTimeByTimer(TIMER_ID);
1657
1658 NtpUpdateTime::GetInstance().autoTimeInfo_.status = NETWORK_TIME_STATUS_ON;
1659 NtpUpdateTime::GetInstance().RefreshNetworkTimeByTimer(TIMER_ID);
1660
1661 NtpUpdateTime::GetInstance().nitzUpdateTimeMilli_ = 0;
1662 auto res = NtpUpdateTime::GetInstance().IsValidNITZTime();
1663 EXPECT_FALSE(res);
1664
1665 NtpUpdateTime::GetInstance().UpdateNITZSetTime();
1666 res = NtpUpdateTime::GetInstance().IsValidNITZTime();
1667 EXPECT_TRUE(res);
1668 NtpUpdateTime::GetInstance().RefreshNetworkTimeByTimer(TIMER_ID);
1669
1670 NtpUpdateTime::GetInstance().autoTimeInfo_.status = status;
1671 }
1672 } // namespace