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(&currentTime, 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: SR000H0GQ6 AR000H2VTQ
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: AR20240306656104
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: AR20240306656104
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(&currentTime, 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(&currentTime, 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(&currentTime, 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(&currentTime, 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(&currentTime, 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(&currentTime, 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, 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(TIMER_ID,
1087                                             0,
1088                                             max,
1089                                             10,
1090                                             0,
1091                                             1,
1092                                             nullptr,
1093                                             nullptr,
1094                                             0,
1095                                             0,
1096                                             "bundleName");
1097     std::lock_guard<std::mutex> lock(TimerManager::GetInstance()->entryMapMutex_);
1098     auto map = TimerManager::GetInstance()->timerEntryMap_;
1099     auto it = map.find(TIMER_ID);
1100     EXPECT_NE(it, map.end());
1101     if (it != map.end()) {
1102         map.erase(it);
1103     }
1104 }
1105 
1106 /**
1107 * @tc.name: TimerManager003.
1108 * @tc.desc: test Set() with type > ALARM_TYPE_COUNT.
1109 * @tc.type: FUNC
1110 */
1111 HWTEST_F(TimeServiceTest, TimerManager003, TestSize.Level0)
1112 {
1113     auto when = std::chrono::nanoseconds::zero();
1114     auto bootTime = std::chrono::steady_clock::now();
1115     auto res = TimerManager::GetInstance()->handler_->Set(6, when, bootTime);
1116     EXPECT_EQ(res, -1);
1117 }
1118 
1119 /**
1120 * @tc.name: TimerManager004.
1121 * @tc.desc: test StartTimer with UidProxy and PidProxy.
1122 * @tc.type: FUNC
1123 */
1124 HWTEST_F(TimeServiceTest, TimerManager004, TestSize.Level0)
1125 {
1126     TimerManager::GetInstance()->DestroyTimer(TIMER_ID);
1127     auto entry = std::make_shared<TimerEntry>(
1128             TimerEntry{TIMER_ID, 0, 0, 0, 0, nullptr, nullptr, UID, PID, "bundleName"});
1129     TimerManager::GetInstance()->ReCreateTimer(TIMER_ID, entry);
1130 
1131     {
1132         std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().proxyMutex_);
1133         std::unordered_map<uint64_t, std::chrono::steady_clock::time_point> timePointMap {};
1134         TimerProxy::GetInstance().proxyUids_.insert(std::make_pair(UID, timePointMap));
1135     }
1136     auto res = TimerManager::GetInstance()->StartTimer(TIMER_ID, 0);
1137     EXPECT_EQ(res, E_TIME_OK);
1138 
1139     {
1140         std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().proxyMutex_);
1141         auto map = TimerProxy::GetInstance().proxyUids_;
1142         auto it = map.find(UID);
1143         if (it != map.end()) {
1144             map.erase(it);
1145         }
1146     }
1147     {
1148         std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().proxyPidMutex_);
1149         auto map = TimerProxy::GetInstance().proxyPids_;
1150         std::unordered_map<uint64_t, std::chrono::steady_clock::time_point> timePointMap {};
1151         map.insert(std::make_pair(UID, timePointMap));
1152     }
1153     res = TimerManager::GetInstance()->StartTimer(TIMER_ID, 0);
1154     EXPECT_EQ(res, E_TIME_OK);
1155 
1156     {
1157         std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().proxyPidMutex_);
1158         auto map = TimerProxy::GetInstance().proxyPids_;
1159         auto it = map.find(PID);
1160         if (it != map.end()) {
1161             map.erase(it);
1162         }
1163     }
1164 
1165     TimerManager::GetInstance()->DestroyTimer(TIMER_ID);
1166 }
1167 
1168 /**
1169 * @tc.name: TimerManager005.
1170 * @tc.desc: test NotifyWantAgent.
1171 * @tc.type: FUNC
1172 */
1173 HWTEST_F(TimeServiceTest, TimerManager005, TestSize.Level0)
1174 {
1175     TimerManager::GetInstance()->NotifyWantAgentRetry(nullptr);
1176 
1177     auto duration = std::chrono::milliseconds::zero();
1178     auto timePoint = std::chrono::steady_clock::now();
1179     auto timerInfo = std::make_shared<TimerInfo>(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration,
1180                                                  nullptr, nullptr, 0, 0, 0, "");
1181     auto res = TimerManager::GetInstance()->NotifyWantAgent(timerInfo);
1182     EXPECT_FALSE(res);
1183 
1184     OHOS::NativeRdb::ValuesBucket insertValues;
1185     insertValues.PutLong("timerId", TIMER_ID);
1186     insertValues.PutInt("type", 0);
1187     insertValues.PutInt("flag", 0);
1188     insertValues.PutLong("windowLength", 0);
1189     insertValues.PutLong("interval", 0);
1190     insertValues.PutInt("uid", 0);
1191     insertValues.PutString("bundleName", "");
1192     std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent = nullptr;
1193     insertValues.PutString("wantAgent", OHOS::AbilityRuntime::WantAgent::WantAgentHelper::ToString(wantAgent));
1194     insertValues.PutInt("state", 0);
1195     insertValues.PutLong("triggerTime", static_cast<int64_t>(std::numeric_limits<int64_t>::max()));
1196     TimeDatabase::GetInstance().Insert(HOLD_ON_REBOOT, insertValues);
1197 
1198     res = TimerManager::GetInstance()->NotifyWantAgent(timerInfo);
1199     EXPECT_FALSE(res);
1200 
1201     OHOS::NativeRdb::RdbPredicates rdbPredicatesDelete(HOLD_ON_REBOOT);
1202     rdbPredicatesDelete.EqualTo("timerId", static_cast<int64_t>(TIMER_ID));
1203     TimeDatabase::GetInstance().Delete(rdbPredicatesDelete);
1204 }
1205 
1206 /**
1207 * @tc.name: TimerManager006.
1208 * @tc.desc: test AdjustTimer.
1209 * @tc.type: FUNC
1210 */
1211 HWTEST_F(TimeServiceTest, TimerManager006, TestSize.Level0)
1212 {
1213     uint32_t interval;
1214     bool isAdjust;
1215     // Set 1000 as interval, because interval can not be 0;
1216     uint32_t intervalSet = 1000;
1217     {
1218         std::lock_guard<std::mutex> lock(TimerManager::GetInstance()->mutex_);
1219         interval = TimerManager::GetInstance()->adjustInterval_;
1220         TimerManager::GetInstance()->adjustInterval_ = intervalSet;
1221         isAdjust = TimerManager::GetInstance()->adjustPolicy_;
1222     }
1223 
1224     auto res = TimerManager::GetInstance()->AdjustTimer(isAdjust, intervalSet);
1225     EXPECT_FALSE(res);
1226     res = TimerManager::GetInstance()->AdjustTimer(!isAdjust, intervalSet);
1227     EXPECT_TRUE(res);
1228     res = TimerManager::GetInstance()->AdjustTimer(isAdjust, intervalSet + 1);
1229     EXPECT_TRUE(res);
1230     res = TimerManager::GetInstance()->AdjustTimer(isAdjust, intervalSet);
1231     EXPECT_TRUE(res);
1232 
1233     std::lock_guard<std::mutex> lock(TimerManager::GetInstance()->mutex_);
1234     TimerManager::GetInstance()->adjustInterval_ = interval;
1235     TimerManager::GetInstance()->adjustPolicy_ = isAdjust;
1236 }
1237 
1238 /**
1239 * @tc.name: TimerManager007.
1240 * @tc.desc: test AdjustDeliveryTimeBasedOnDeviceIdle.
1241 * @tc.type: FUNC
1242 */
1243 HWTEST_F(TimeServiceTest, TimerManager007, TestSize.Level0)
1244 {
1245     auto duration = std::chrono::milliseconds::zero();
1246     auto timePoint = std::chrono::steady_clock::now();
1247     auto timerInfo1 = std::make_shared<TimerInfo>(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration,
1248                                                  nullptr, nullptr, 0, 0, 0, "");
1249     std::lock_guard<std::mutex> lock(TimerManager::GetInstance()->mutex_);
1250     auto alarm = TimerManager::GetInstance()->mPendingIdleUntil_;
1251     TimerManager::GetInstance()->mPendingIdleUntil_ = timerInfo1;
1252     auto res = TimerManager::GetInstance()->AdjustDeliveryTimeBasedOnDeviceIdle(timerInfo1);
1253     EXPECT_FALSE(res);
1254 
1255     TimerManager::GetInstance()->mPendingIdleUntil_ = nullptr;
1256     TimerManager::GetInstance()->delayedTimers_[TIMER_ID] = std::chrono::steady_clock::now();
1257     res = TimerManager::GetInstance()->AdjustDeliveryTimeBasedOnDeviceIdle(timerInfo1);
1258     EXPECT_TRUE(res);
1259     auto duration1 = std::chrono::duration_cast<std::chrono::milliseconds>(
1260             (timePoint + std::chrono::hours(1)).time_since_epoch());
1261     auto timerInfo2 = std::make_shared<TimerInfo>(TIMER_ID, 1, duration1, timePoint, duration, timePoint, duration,
1262                                                   nullptr, nullptr, 0, 0, 0, "");
1263     res = TimerManager::GetInstance()->AdjustDeliveryTimeBasedOnDeviceIdle(timerInfo2);
1264     EXPECT_TRUE(res);
1265     auto timerInfo3 = std::make_shared<TimerInfo>(TIMER_ID, 2, duration, timePoint, duration, timePoint, duration,
1266                                                   nullptr, nullptr, 0, 0, 0, "");
1267     res = TimerManager::GetInstance()->AdjustDeliveryTimeBasedOnDeviceIdle(timerInfo3);
1268     EXPECT_TRUE(res);
1269 
1270     TimerManager::GetInstance()->mPendingIdleUntil_ = alarm;
1271 }
1272 
1273 /**
1274 * @tc.name: TimerManager008.
1275 * @tc.desc: test ShowTimerEntryById TIMER_ID not in timerEntryMap_.
1276 * @tc.type: FUNC
1277 */
1278 HWTEST_F(TimeServiceTest, TimerManager008, TestSize.Level0)
1279 {
1280     TimerManager::GetInstance()->DestroyTimer(TIMER_ID);
1281 
1282     auto res = TimerManager::GetInstance()->ShowTimerEntryById(0, TIMER_ID);
1283     EXPECT_FALSE(res);
1284 }
1285 
1286 /**
1287 * @tc.name: TimerManager009.
1288 * @tc.desc: test ShowTimerTriggerById TIMER_ID in alarmBatches_.
1289 * @tc.type: FUNC
1290 */
1291 HWTEST_F(TimeServiceTest, TimerManager009, TestSize.Level0)
1292 {
1293     auto entry = std::make_shared<TimerEntry>(
1294             TimerEntry{TIMER_ID, 0, 0, 0, 0, nullptr, nullptr, 0, 0, "bundleName"});
1295     TimerManager::GetInstance()->ReCreateTimer(TIMER_ID, entry);
1296     uint64_t triggerTime = std::numeric_limits<uint64_t>::max();
1297     TimerManager::GetInstance()->StartTimer(TIMER_ID, triggerTime);
1298     auto res = TimerManager::GetInstance()->ShowTimerTriggerById(0, TIMER_ID);
1299     TimerManager::GetInstance()->DestroyTimer(TIMER_ID);
1300     EXPECT_TRUE(res);
1301 }
1302 
1303 /**
1304 * @tc.name: TimerManager010.
1305 * @tc.desc: test HandleRSSDeath.
1306 * @tc.type: FUNC
1307 */
1308 HWTEST_F(TimeServiceTest, TimerManager010, TestSize.Level0)
1309 {
1310     std::shared_ptr<TimerInfo> alarm;
1311     {
1312         std::lock_guard <std::mutex> lock(TimerManager::GetInstance()->mutex_);
1313         alarm = TimerManager::GetInstance()->mPendingIdleUntil_;
1314         TimerManager::GetInstance()->mPendingIdleUntil_ = nullptr;
1315     }
1316     TimerManager::GetInstance()->HandleRSSDeath();
1317 
1318     auto duration = std::chrono::milliseconds::zero();
1319     auto timePoint = std::chrono::steady_clock::now();
1320     auto timerInfo = std::make_shared<TimerInfo>(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration,
1321                                                   nullptr, nullptr, 0, 0, 0, "");
1322     {
1323         std::lock_guard <std::mutex> lock(TimerManager::GetInstance()->mutex_);
1324         TimerManager::GetInstance()->mPendingIdleUntil_ = timerInfo;
1325     }
1326     auto entry = std::make_shared<TimerEntry>(
1327             TimerEntry{TIMER_ID, 0, 0, 0, 0, nullptr, nullptr, 0, 0, "bundleName"});
1328     TimerManager::GetInstance()->ReCreateTimer(TIMER_ID, entry);
1329     TimerManager::GetInstance()->HandleRSSDeath();
1330     auto res = TimerManager::GetInstance()->DestroyTimer(TIMER_ID);
1331     EXPECT_EQ(res, E_TIME_DEAL_FAILED);
1332 
1333     {
1334         std::lock_guard <std::mutex> lock(TimerManager::GetInstance()->mutex_);
1335         TimerManager::GetInstance()->mPendingIdleUntil_ = alarm;
1336     }
1337 }
1338 
1339 /**
1340 * @tc.name: TimerManager011.
1341 * @tc.desc: test TimerNotifyCallback GetInstance.
1342 * @tc.type: FUNC
1343 */
1344 HWTEST_F(TimeServiceTest, TimerManager011, TestSize.Level0)
1345 {
1346     auto timerManager = TimerManager::GetInstance();
1347     auto res = TimerNotifyCallback::GetInstance(timerManager);
1348     EXPECT_NE(res, nullptr);
1349     res = TimerNotifyCallback::GetInstance(timerManager);
1350     EXPECT_NE(res, nullptr);
1351 }
1352 
1353 /**
1354 * @tc.name: TimerManager013.
1355 * @tc.desc: test record and delete of timerCount_.
1356 * @tc.type: FUNC
1357 */
1358 HWTEST_F(TimeServiceTest, TimerManager013, TestSize.Level0)
1359 {
1360     int uid1 = UID;
1361     int uid2 = UID + 1;
1362     TimerManager::GetInstance()->timerCount_.clear();
1363     for (int i = 0; i < 10; ++i) {
1364         TimerManager::GetInstance()->IncreaseTimerCount(uid1);
1365     }
1366     EXPECT_EQ(TimerManager::GetInstance()->timerCount_.size(), 1);
1367     auto it = std::find_if(TimerManager::GetInstance()->timerCount_.begin(),
1368         TimerManager::GetInstance()->timerCount_.end(),
__anon9de147fa0202(const std::pair<int32_t, size_t>& pair) 1369         [uid1](const std::pair<int32_t, size_t>& pair) {
1370             return pair.first == uid1;
1371     });
1372     EXPECT_EQ(it->second, 10);
1373     for (int i = 0; i < 10; ++i) {
1374         TimerManager::GetInstance()->IncreaseTimerCount(uid2);
1375     }
1376     EXPECT_EQ(TimerManager::GetInstance()->timerCount_.size(), 2);
1377     for (int i = 0; i < 5; ++i) {
1378         TimerManager::GetInstance()->DecreaseTimerCount(uid2);
1379     }
1380     it = std::find_if(TimerManager::GetInstance()->timerCount_.begin(),
1381         TimerManager::GetInstance()->timerCount_.end(),
__anon9de147fa0302(const std::pair<int32_t, size_t>& pair) 1382         [uid2](const std::pair<int32_t, size_t>& pair) {
1383             return pair.first == uid2;
1384     });
1385     EXPECT_EQ(it->second, 5);
1386 }
1387 
1388 /**
1389 * @tc.name: TimerManager014.
1390 * @tc.desc: test when create and delete timer, the change of timerOutOfRangeTimes_.
1391 * @tc.type: FUNC
1392 */
1393 HWTEST_F(TimeServiceTest, TimerManager014, TestSize.Level0)
1394 {
1395     TimerManager::GetInstance()->timerEntryMap_.clear();
1396     TimerManager::GetInstance()->timerCount_.clear();
1397     TimerManager::GetInstance()->timerOutOfRangeTimes_ = 0;
1398     uint64_t i = 0;
1399     for (; i <= TIMER_ALARM_COUNT; ++i) {
1400         auto entry = std::make_shared<TimerEntry>(
1401             TimerEntry{i, 0, 0, 0, 0, nullptr, nullptr, 0, 0, "bundleName"});
1402         TimerManager::GetInstance()->ReCreateTimer(i, entry);
1403     }
1404     EXPECT_EQ(TimerManager::GetInstance()->timerOutOfRangeTimes_, 1);
1405     for (; i <= TIMER_ALARM_COUNT * 2; ++i) {
1406         auto entry = std::make_shared<TimerEntry>(
1407             TimerEntry{i, 0, 0, 0, 0, nullptr, nullptr, 0, 0, "bundleName"});
1408         TimerManager::GetInstance()->ReCreateTimer(i, entry);
1409     }
1410     EXPECT_EQ(TimerManager::GetInstance()->timerOutOfRangeTimes_, 2);
1411 }
1412 /**
1413 * @tc.name: SystemAbility001.
1414 * @tc.desc: test OnStop.
1415 * @tc.type: FUNC
1416 */
1417 HWTEST_F(TimeServiceTest, SystemAbility001, TestSize.Level0)
1418 {
1419     TimeSystemAbility::GetInstance()->OnStop();
1420     EXPECT_EQ(TimeSystemAbility::GetInstance()->state_, ServiceRunningState::STATE_NOT_START);
1421     TimeSystemAbility::GetInstance()->OnStop();
1422     EXPECT_EQ(TimeSystemAbility::GetInstance()->state_, ServiceRunningState::STATE_NOT_START);
1423 }
1424 
1425 /**
1426 * @tc.name: SystemAbility002.
1427 * @tc.desc: test RecoverTimer.
1428 * @tc.type: FUNC
1429 */
1430 HWTEST_F(TimeServiceTest, SystemAbility002, TestSize.Level0)
1431 {
1432     uint64_t timerId1 = TIMER_ID;
1433     uint64_t timerId2 = TIMER_ID + 1;
1434 
1435     auto map = TimerManager::GetInstance()->timerEntryMap_;
1436     auto it = map.find(timerId1);
1437     if (it != map.end()) {
1438         map.erase(it);
1439     }
1440     it = map.find(timerId2);
1441     if (it != map.end()) {
1442         map.erase(it);
1443     }
1444 
1445     OHOS::NativeRdb::ValuesBucket insertValues1;
1446     insertValues1.PutLong("timerId", timerId1);
1447     insertValues1.PutInt("type", 0);
1448     insertValues1.PutInt("flag", 0);
1449     insertValues1.PutLong("windowLength", 0);
1450     insertValues1.PutLong("interval", 0);
1451     insertValues1.PutInt("uid", 0);
1452     insertValues1.PutString("bundleName", "");
1453     std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent = nullptr;
1454     insertValues1.PutString("wantAgent", OHOS::AbilityRuntime::WantAgent::WantAgentHelper::ToString(wantAgent));
1455     insertValues1.PutInt("state", 0);
1456     insertValues1.PutLong("triggerTime", static_cast<int64_t>(std::numeric_limits<int64_t>::max()));
1457     TimeDatabase::GetInstance().Insert(HOLD_ON_REBOOT, insertValues1);
1458 
1459     OHOS::NativeRdb::ValuesBucket insertValues2;
1460     insertValues2.PutLong("timerId", timerId2);
1461     insertValues2.PutInt("type", 0);
1462     insertValues2.PutInt("flag", 0);
1463     insertValues2.PutLong("windowLength", 0);
1464     insertValues2.PutLong("interval", 0);
1465     insertValues2.PutInt("uid", 0);
1466     insertValues2.PutString("bundleName", "");
1467     wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
1468     insertValues2.PutString("wantAgent", OHOS::AbilityRuntime::WantAgent::WantAgentHelper::ToString(wantAgent));
1469     insertValues2.PutInt("state", 0);
1470     insertValues2.PutLong("triggerTime", static_cast<int64_t>(std::numeric_limits<int64_t>::max()));
1471     TimeDatabase::GetInstance().Insert(DROP_ON_REBOOT, insertValues2);
1472 
1473     TimeSystemAbility::GetInstance()->RecoverTimer();
1474 
1475     it = map.find(timerId1);
1476     EXPECT_EQ(it, map.end());
1477     it = map.find(timerId2);
1478     EXPECT_EQ(it, map.end());
1479 
1480     OHOS::NativeRdb::RdbPredicates rdbPredicatesDelete1(HOLD_ON_REBOOT);
1481     rdbPredicatesDelete1.EqualTo("timerId", static_cast<int64_t>(timerId1));
1482     TimeDatabase::GetInstance().Delete(rdbPredicatesDelete1);
1483 
1484     OHOS::NativeRdb::RdbPredicates rdbPredicatesDelete2(DROP_ON_REBOOT);
1485     rdbPredicatesDelete2.EqualTo("timerId", static_cast<int64_t>(timerId2));
1486     TimeDatabase::GetInstance().Delete(rdbPredicatesDelete2);
1487 }
1488 
1489 /**
1490 * @tc.name: SystemAbility003.
1491 * @tc.desc: test SetAutoReboot.
1492 * @tc.type: FUNC
1493 */
1494 HWTEST_F(TimeServiceTest, SystemAbility003, TestSize.Level0)
1495 {
1496     uint64_t timerId1 = TIMER_ID;
1497     uint64_t timerId2 = TIMER_ID + 1;
1498 
1499     TimeSystemAbility::GetInstance()->SetAutoReboot();
1500 
1501     OHOS::NativeRdb::ValuesBucket insertValues1;
1502     insertValues1.PutLong("timerId", timerId1);
1503     insertValues1.PutInt("type", 0);
1504     insertValues1.PutInt("flag", 0);
1505     insertValues1.PutLong("windowLength", 0);
1506     insertValues1.PutLong("interval", 0);
1507     insertValues1.PutInt("uid", 0);
1508     insertValues1.PutString("bundleName", "anything");
1509     insertValues1.PutString("wantAgent", "");
1510     insertValues1.PutInt("state", 1);
1511     insertValues1.PutLong("triggerTime", static_cast<int64_t>(0));
1512     auto res = TimeDatabase::GetInstance().Insert(HOLD_ON_REBOOT, insertValues1);
1513     EXPECT_EQ(res, true);
1514 
1515     OHOS::NativeRdb::ValuesBucket insertValues2;
1516     insertValues2.PutLong("timerId", timerId2);
1517     insertValues2.PutInt("type", 0);
1518     insertValues2.PutInt("flag", 0);
1519     insertValues2.PutLong("windowLength", 0);
1520     insertValues2.PutLong("interval", 0);
1521     insertValues2.PutInt("uid", 0);
1522     insertValues2.PutString("bundleName", NEED_RECOVER_ON_REBOOT[0]);
1523     insertValues2.PutString("wantAgent", "");
1524     insertValues2.PutInt("state", 1);
1525     insertValues2.PutLong("triggerTime", static_cast<int64_t>(std::numeric_limits<int64_t>::max()));
1526     res = TimeDatabase::GetInstance().Insert(HOLD_ON_REBOOT, insertValues2);
1527     EXPECT_EQ(res, true);
1528 
1529     TimeSystemAbility::GetInstance()->SetAutoReboot();
1530 
1531     OHOS::NativeRdb::RdbPredicates rdbPredicatesDelete1(HOLD_ON_REBOOT);
1532     rdbPredicatesDelete1.EqualTo("timerId", static_cast<int64_t>(timerId1));
1533     TimeDatabase::GetInstance().Delete(rdbPredicatesDelete1);
1534     OHOS::NativeRdb::RdbPredicates rdbPredicatesDelete2(HOLD_ON_REBOOT);
1535     rdbPredicatesDelete2.EqualTo("timerId", static_cast<int64_t>(timerId2));
1536     TimeDatabase::GetInstance().Delete(rdbPredicatesDelete2);
1537 }
1538 
1539 /**
1540 * @tc.name: SystemAbility004.
1541 * @tc.desc: test SetRealTime.
1542 * @tc.type: FUNC
1543 */
1544 HWTEST_F(TimeServiceTest, SystemAbility004, TestSize.Level0)
1545 {
1546     auto res = TimeSystemAbility::GetInstance()->SetRealTime(-1);
1547     EXPECT_FALSE(res);
1548 }
1549 
1550 /**
1551 * @tc.name: TimeDatabase001.
1552 * @tc.desc: test TimeDatabase Insert.
1553 * @tc.type: FUNC
1554 */
1555 HWTEST_F(TimeServiceTest, TimeDatabase001, TestSize.Level0)
1556 {
1557     OHOS::NativeRdb::ValuesBucket insertValues;
1558     insertValues.PutLong("something", 0);
1559     auto res = TimeDatabase::GetInstance().Insert(DROP_ON_REBOOT, insertValues);
1560     EXPECT_FALSE(res);
1561 }
1562 
1563 /**
1564 * @tc.name: TimeDatabase002.
1565 * @tc.desc: test TimeDatabase Update.
1566 * @tc.type: FUNC
1567 */
1568 HWTEST_F(TimeServiceTest, TimeDatabase002, TestSize.Level0)
1569 {
1570     OHOS::NativeRdb::ValuesBucket values;
1571     values.PutInt("something", 1);
1572     OHOS::NativeRdb::RdbPredicates rdbPredicates(DROP_ON_REBOOT);
1573     rdbPredicates.EqualTo("something", 0)->And()->EqualTo("something", static_cast<int64_t>(0));
1574     auto res = TimeDatabase::GetInstance().Update(values, rdbPredicates);
1575     EXPECT_FALSE(res);
1576 }
1577 
1578 /**
1579 * @tc.name: TimeDatabase003.
1580 * @tc.desc: test TimeDatabase Delete.
1581 * @tc.type: FUNC
1582 */
1583 HWTEST_F(TimeServiceTest, TimeDatabase003, TestSize.Level0)
1584 {
1585     OHOS::NativeRdb::RdbPredicates rdbPredicatesDelete(DROP_ON_REBOOT);
1586     rdbPredicatesDelete.EqualTo("something", static_cast<int64_t>(0));
1587     auto res = TimeDatabase::GetInstance().Delete(rdbPredicatesDelete);
1588     EXPECT_FALSE(res);
1589 }
1590 
1591 /**
1592 * @tc.name: TimerInfo001.
1593 * @tc.desc: test UpdateWhenElapsedFromNow.
1594 * @tc.type: FUNC
1595 */
1596 HWTEST_F(TimeServiceTest, TimerInfo001, TestSize.Level0)
1597 {
1598     auto duration = std::chrono::milliseconds::zero();
1599     auto timePoint = std::chrono::steady_clock::now();
1600     auto timerInfo = TimerInfo(0, 0, duration, timePoint, duration, timePoint, duration, nullptr,
1601                                           nullptr, 0, 0, 0, "");
1602     auto res = timerInfo.UpdateWhenElapsedFromNow(timePoint, duration);
1603     EXPECT_FALSE(res);
1604 }
1605 
1606 /**
1607 * @tc.name: TimerInfo002.
1608 * @tc.desc: test AdjustTimer.
1609 * @tc.type: FUNC
1610 */
1611 HWTEST_F(TimeServiceTest, TimerInfo002, TestSize.Level0)
1612 {
1613     auto duration = std::chrono::milliseconds(0);
1614     auto timePoint = std::chrono::steady_clock::now();
1615     auto timerInfo = TimerInfo(0, 0, duration, timePoint, duration, timePoint, duration, nullptr,
1616                                           nullptr, 0, 0, 0, "");
1617     auto res = timerInfo.AdjustTimer(timePoint, 1);
1618     EXPECT_TRUE(res);
1619 }
1620 
1621 /**
1622 * @tc.name: NtpTime001.
1623 * @tc.desc: test SplitNtpAddrs return max size.
1624 * @tc.type: FUNC
1625 */
1626 HWTEST_F(TimeServiceTest, NtpTime001, TestSize.Level0)
1627 {
1628     const std::string ntpStr = "aaa,bbb,ccc,ddd,eee,fff";
1629     auto res = NtpUpdateTime::GetInstance().SplitNtpAddrs(ntpStr);
1630     EXPECT_EQ(res.size(), 5);
1631 }
1632 
1633 /**
1634 * @tc.name: NtpTime002.
1635 * @tc.desc: test RefreshNetworkTimeByTimer.
1636 * @tc.type: FUNC
1637 */
1638 HWTEST_F(TimeServiceTest, NtpTime002, TestSize.Level0)
1639 {
1640     auto status = NtpUpdateTime::GetInstance().autoTimeInfo_.status;
1641 
1642     NtpUpdateTime::GetInstance().autoTimeInfo_.status = NETWORK_TIME_STATUS_OFF;
1643     NtpUpdateTime::GetInstance().RefreshNetworkTimeByTimer(TIMER_ID);
1644 
1645     NtpUpdateTime::GetInstance().autoTimeInfo_.status = NETWORK_TIME_STATUS_ON;
1646     NtpUpdateTime::GetInstance().RefreshNetworkTimeByTimer(TIMER_ID);
1647 
1648     NtpUpdateTime::GetInstance().nitzUpdateTimeMilli_ = 0;
1649     auto res = NtpUpdateTime::GetInstance().IsValidNITZTime();
1650     EXPECT_FALSE(res);
1651 
1652     NtpUpdateTime::GetInstance().UpdateNITZSetTime();
1653     res = NtpUpdateTime::GetInstance().IsValidNITZTime();
1654     EXPECT_TRUE(res);
1655     NtpUpdateTime::GetInstance().RefreshNetworkTimeByTimer(TIMER_ID);
1656 
1657     NtpUpdateTime::GetInstance().autoTimeInfo_.status = status;
1658 }
1659 } // namespace