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