• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
43 namespace {
44 using namespace testing::ext;
45 using namespace OHOS;
46 using namespace OHOS::MiscServices;
47 using namespace std::chrono;
48 using namespace OHOS::Security::AccessToken;
49 
50 const int32_t RESERVED_UID = 99999;
51 const std::string NTP_CN_SERVER = "ntp.aliyun.com";
52 const std::string AUTOTIME_FILE_PATH = "/data/service/el1/public/time/autotime.json";
53 const std::string NETWORK_TIME_STATUS_OFF = "OFF";
54 const std::string NETWORK_TIME_STATUS_ON = "ON";
55 uint64_t g_idleTimerId = 0;
56 
57 static HapPolicyParams g_policyA = {
58     .apl = APL_SYSTEM_CORE,
59     .domain = "test.domain",
60     .permList = {
61         {
62             .permissionName = "ohos.permission.SET_TIME",
63             .bundleName = "ohos.permission_test.demoB",
64             .grantMode = 1,
65             .availableLevel = APL_NORMAL,
66             .label = "label",
67             .labelId = 1,
68             .description = "test",
69             .descriptionId = 1
70         },
71         {
72             .permissionName = "ohos.permission.SET_TIME_ZONE",
73             .bundleName = "ohos.permission_test.demoB",
74             .grantMode = 1,
75             .availableLevel = APL_NORMAL,
76             .label = "label",
77             .labelId = 1,
78             .description = "test",
79             .descriptionId = 1
80         }
81     },
82     .permStateList = {
83         {
84             .permissionName = "ohos.permission.SET_TIME",
85             .isGeneral = true,
86             .resDeviceID = { "local" },
87             .grantStatus = { PermissionState::PERMISSION_GRANTED },
88             .grantFlags = { 1 }
89         },
90         {
91             .permissionName = "ohos.permission.SET_TIME_ZONE",
92             .isGeneral = true,
93             .resDeviceID = { "local" },
94             .grantStatus = { PermissionState::PERMISSION_GRANTED },
95             .grantFlags = { 1 }
96         }
97     }
98 };
99 
100 static HapInfoParams g_systemInfoParams = {
101     .userID = 1,
102     .bundleName = "timer",
103     .instIndex = 0,
104     .appIDDesc = "test",
105     .apiVersion = 8,
106     .isSystemApp = true
107 };
108 
109 static HapPolicyParams g_policyB = { .apl = APL_NORMAL, .domain = "test.domain" };
110 
111 static HapInfoParams g_notSystemInfoParams = {
112     .userID = 1,
113     .bundleName = "timer",
114     .instIndex = 0,
115     .appIDDesc = "test",
116     .apiVersion = 8,
117 };
118 
119 class TimeServiceTest : public testing::Test {
120 public:
121     static void SetUpTestCase(void);
122     static void TearDownTestCase(void);
123     void SetUp();
124     void TearDown();
125     void AddPermission();
126     void DeletePermission();
127     void StartIdleTimer();
128     void DestroyIdleTimer();
129 };
130 
SetUpTestCase(void)131 void TimeServiceTest::SetUpTestCase(void)
132 {
133 }
134 
TearDownTestCase(void)135 void TimeServiceTest::TearDownTestCase(void)
136 {
137 }
138 
SetUp(void)139 void TimeServiceTest::SetUp(void)
140 {
141 }
142 
TearDown(void)143 void TimeServiceTest::TearDown(void)
144 {
145 }
146 
AddPermission()147 void TimeServiceTest::AddPermission()
148 {
149     AccessTokenIDEx tokenIdEx = { 0 };
150     tokenIdEx = AccessTokenKit::AllocHapToken(g_systemInfoParams, g_policyA);
151     SetSelfTokenID(tokenIdEx.tokenIDEx);
152 }
153 
DeletePermission()154 void TimeServiceTest::DeletePermission()
155 {
156     AccessTokenIDEx tokenIdEx = { 0 };
157     tokenIdEx = AccessTokenKit::AllocHapToken(g_notSystemInfoParams, g_policyB);
158     SetSelfTokenID(tokenIdEx.tokenIDEx);
159 }
160 
StartIdleTimer()161 void TimeServiceTest::StartIdleTimer()
162 {
163     auto timerInfo = std::make_shared<TimerInfoTest>();
164     timerInfo->SetType(timerInfo->TIMER_TYPE_IDLE);
165     timerInfo->SetRepeat(false);
166     TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, g_idleTimerId);
167     struct timeval currentTime {};
168     gettimeofday(&currentTime, nullptr);
169     int64_t time = currentTime.tv_sec * 1000 + currentTime.tv_usec / 1000;
170     TimeServiceClient::GetInstance()->StartTimerV9(g_idleTimerId, time + 5000);
171 }
172 
DestroyIdleTimer()173 void TimeServiceTest::DestroyIdleTimer()
174 {
175     TimeServiceClient::GetInstance()->DestroyTimerV9(g_idleTimerId);
176 }
177 
178 /**
179 * @tc.name: ProxyTimer001.
180 * @tc.desc: proxy timer.
181 * @tc.type: FUNC
182 * @tc.require: SR000H0GQ6 AR000H2VTQ
183 */
184 HWTEST_F(TimeServiceTest, ProxyTimer001, TestSize.Level0)
185 {
186     auto ret = TimeServiceClient::GetInstance()->ProxyTimer(RESERVED_UID, true, true);
187     EXPECT_TRUE(ret);
188     ret = TimeServiceClient::GetInstance()->ProxyTimer(RESERVED_UID, false, true);
189     EXPECT_TRUE(ret);
190 }
191 
192 /**
193 * @tc.name: ProxyTimer002.
194 * @tc.desc: proxy timer.
195 * @tc.type: FUNC
196 * @tc.require: SR000H0GQ6 AR000H2VTQ
197 */
198 HWTEST_F(TimeServiceTest, ProxyTimer002, TestSize.Level0)
199 {
200     auto ret = TimeServiceClient::GetInstance()->ProxyTimer(RESERVED_UID, true, true);
201     EXPECT_TRUE(ret);
202     ret = TimeServiceClient::GetInstance()->ResetAllProxy();
203     EXPECT_TRUE(ret);
204 }
205 
206 /**
207 * @tc.name: ProxyTimer003.
208 * @tc.desc: proxy timer.
209 * @tc.type: FUNC
210 * @tc.require: SR000H0GQ6 AR000H2VTQ
211 */
212 HWTEST_F(TimeServiceTest, ProxyTimer003, TestSize.Level0)
213 {
214     auto ret = TimeServiceClient::GetInstance()->ProxyTimer(RESERVED_UID, false, true);
215     EXPECT_FALSE(ret);
216 }
217 
218 /**
219 * @tc.name: ProxyTimer004.
220 * @tc.desc: proxy timer.
221 * @tc.type: FUNC
222 * @tc.require: SR000H0GQ6 AR000H2VTQ
223 */
224 HWTEST_F(TimeServiceTest, ProxyTimer004, TestSize.Level0)
225 {
226     auto ret = TimeServiceClient::GetInstance()->ProxyTimer(RESERVED_UID, true, false);
227     EXPECT_TRUE(ret);
228     ret = TimeServiceClient::GetInstance()->ProxyTimer(RESERVED_UID, false, false);
229     EXPECT_TRUE(ret);
230 }
231 
232 /**
233 * @tc.name: IdleTimer001.
234 * @tc.desc: test create idle timer for app.
235 * @tc.type: FUNC
236 * @tc.require:
237 */
238 HWTEST_F(TimeServiceTest, IdleTimer001, TestSize.Level0)
239 {
240     auto timerInfo = std::make_shared<TimerInfoTest>();
241     timerInfo->SetType(timerInfo->TIMER_TYPE_IDLE);
242     timerInfo->SetRepeat(false);
243     uint64_t timerId = 0;
244     TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId);
245     EXPECT_NE(timerId, static_cast<uint64_t>(0));
246     TimeServiceClient::GetInstance()->DestroyTimerV9(timerId);
247 }
248 
249 /**
250 * @tc.name: IdleTimer002
251 * @tc.desc: test public app start timer when device is sleeping and device sleep quit greater than timer callback.
252 * @tc.type: FUNC
253 * @tc.require:
254 */
255 HWTEST_F(TimeServiceTest, IdleTimer002, TestSize.Level0)
256 {
257     g_data1 = 0;
258     auto timerInfo = std::make_shared<TimerInfoTest>();
259     timerInfo->SetType(timerInfo->TIMER_TYPE_INEXACT_REMINDER);
260     timerInfo->SetRepeat(false);
261     timerInfo->SetCallbackInfo(TimeOutCallback1);
262     uint64_t timerId = 0;
263     TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId);
264     EXPECT_NE(timerId, static_cast<uint64_t>(0));
265     StartIdleTimer();
266     struct timeval currentTime {};
267     gettimeofday(&currentTime, nullptr);
268     int64_t time = currentTime.tv_sec * 1000 + currentTime.tv_usec / 1000;
269     TimeServiceClient::GetInstance()->StartTimerV9(timerId, static_cast<uint64_t>(time) + 2000);
270     sleep(2);
271     EXPECT_EQ(g_data1, 0);
272     DestroyIdleTimer();
273     sleep(1);
274     EXPECT_EQ(g_data1, 1);
275     TimeServiceClient::GetInstance()->DestroyTimerV9(timerId);
276 }
277 
278 /**
279 * @tc.name: IdleTimer003
280 * @tc.desc: test public app start timer when device is sleeping and device sleep quit less than timer callback.
281 * @tc.type: FUNC
282 * @tc.require:
283 */
284 HWTEST_F(TimeServiceTest, IdleTimer003, TestSize.Level0)
285 {
286     g_data1 = 0;
287     auto timerInfo = std::make_shared<TimerInfoTest>();
288     timerInfo->SetType(timerInfo->TIMER_TYPE_INEXACT_REMINDER);
289     timerInfo->SetRepeat(false);
290     timerInfo->SetCallbackInfo(TimeOutCallback1);
291     uint64_t timerId = 0;
292     TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId);
293     EXPECT_NE(timerId, static_cast<uint64_t>(0));
294     StartIdleTimer();
295     struct timeval currentTime {};
296     gettimeofday(&currentTime, nullptr);
297     int64_t time = currentTime.tv_sec * 1000 + currentTime.tv_usec / 1000;
298     TimeServiceClient::GetInstance()->StartTimerV9(timerId, static_cast<uint64_t>(time) + 6000);
299     sleep(6);
300     EXPECT_EQ(g_data1, 0);
301     DestroyIdleTimer();
302     sleep(6);
303     EXPECT_EQ(g_data1, 1);
304     TimeServiceClient::GetInstance()->DestroyTimerV9(timerId);
305 }
306 
307 /**
308 * @tc.name: IdleTimer004
309 * @tc.desc: test public app start timer when device is working, device sleep immediately
310 *           and timer callback greater than idle quit.
311 * @tc.type: FUNC
312 * @tc.require:
313 */
314 HWTEST_F(TimeServiceTest, IdleTimer004, TestSize.Level0)
315 {
316     g_data1 = 0;
317     auto timerInfo = std::make_shared<TimerInfoTest>();
318     timerInfo->SetType(timerInfo->TIMER_TYPE_INEXACT_REMINDER);
319     timerInfo->SetRepeat(false);
320     timerInfo->SetCallbackInfo(TimeOutCallback1);
321     uint64_t timerId = 0;
322     TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId);
323     EXPECT_NE(timerId, static_cast<uint64_t>(0));
324     struct timeval currentTime {};
325     gettimeofday(&currentTime, nullptr);
326     int64_t time = currentTime.tv_sec * 1000 + currentTime.tv_usec / 1000;
327     TimeServiceClient::GetInstance()->StartTimerV9(timerId, static_cast<uint64_t>(time + 6000));
328     StartIdleTimer();
329     sleep(6);
330     DestroyIdleTimer();
331     EXPECT_EQ(g_data1, 1);
332     TimeServiceClient::GetInstance()->DestroyTimerV9(timerId);
333 }
334 
335 /**
336 * @tc.name: SetTime001
337 * @tc.desc: set system time.
338 * @tc.type: FUNC
339 */
340 HWTEST_F(TimeServiceTest, SetTime001, TestSize.Level1)
341 {
342     AddPermission();
343     struct timeval currentTime {
344     };
345     gettimeofday(&currentTime, NULL);
346     int64_t time = (currentTime.tv_sec + 1000) * 1000 + currentTime.tv_usec / 1000;
347     ASSERT_TRUE(time > 0);
348     TIME_HILOGI(TIME_MODULE_CLIENT, "Time now : %{public}" PRId64 "", time);
349     bool result = TimeServiceClient::GetInstance()->SetTime(time);
350     EXPECT_TRUE(result);
351     DeletePermission();
352 }
353 
354 /**
355 * @tc.name: SetTime002
356 * @tc.desc: set system time.
357 * @tc.type: FUNC
358 */
359 HWTEST_F(TimeServiceTest, SetTime002, TestSize.Level1)
360 {
361     bool result = TimeServiceClient::GetInstance()->SetTime(-1);
362     EXPECT_FALSE(result);
363 }
364 
365 /**
366 * @tc.name: SetTime003
367 * @tc.desc: set system time.
368 * @tc.type: FUNC
369 */
370 HWTEST_F(TimeServiceTest, SetTime003, TestSize.Level1)
371 {
372     bool result = TimeServiceClient::GetInstance()->SetTime(LLONG_MAX);
373     EXPECT_FALSE(result);
374 }
375 
376 /**
377 * @tc.name: SetTime004
378 * @tc.desc: set system time.
379 * @tc.type: FUNC
380 */
381 HWTEST_F(TimeServiceTest, SetTime004, TestSize.Level1)
382 {
383     AddPermission();
384     struct timeval currentTime {
385     };
386     gettimeofday(&currentTime, NULL);
387     int64_t time = (currentTime.tv_sec + 1000) * 1000 + currentTime.tv_usec / 1000;
388     ASSERT_TRUE(time > 0);
389     TIME_HILOGI(TIME_MODULE_CLIENT, "Time now : %{public}" PRId64 "", time);
390     int32_t code;
391     bool result = TimeServiceClient::GetInstance()->SetTime(time, code);
392     EXPECT_TRUE(result);
393     EXPECT_EQ(code, 0);
394     DeletePermission();
395 }
396 
397 /**
398 * @tc.name: SetTimeZone001
399 * @tc.desc: set system time zone.
400 * @tc.type: FUNC
401 */
402 HWTEST_F(TimeServiceTest, SetTimeZone001, TestSize.Level1)
403 {
404     AddPermission();
405     time_t t;
406     (void)time(&t);
407     TIME_HILOGI(TIME_MODULE_CLIENT, "Time before: %{public}s", asctime(localtime(&t)));
408     auto getCurrentTimeZone = TimeServiceClient::GetInstance()->GetTimeZone();
409     EXPECT_FALSE(getCurrentTimeZone.empty());
410 
411     std::string timeZoneNicosia("Asia/Nicosia");
412     bool result = TimeServiceClient::GetInstance()->SetTimeZone(timeZoneNicosia);
413     EXPECT_TRUE(result);
414     auto getTimeZoneNicosia = TimeServiceClient::GetInstance()->GetTimeZone();
415     EXPECT_EQ(timeZoneNicosia, getTimeZoneNicosia);
416     bool ret = TimeServiceClient::GetInstance()->SetTimeZone(getCurrentTimeZone);
417     EXPECT_TRUE(ret);
418     DeletePermission();
419 }
420 
421 /**
422 * @tc.name: SetTimeZone002
423 * @tc.desc: set system time zone.
424 * @tc.type: FUNC
425 */
426 HWTEST_F(TimeServiceTest, SetTimeZone002, TestSize.Level1)
427 {
428     bool result = TimeServiceClient::GetInstance()->SetTimeZone("123");
429     EXPECT_FALSE(result);
430 }
431 
432 /**
433 * @tc.name: SetTimeZone003
434 * @tc.desc: set system time zone.
435 * @tc.type: FUNC
436 */
437 HWTEST_F(TimeServiceTest, SetTimeZone003, TestSize.Level1)
438 {
439     AddPermission();
440     time_t t;
441     (void)time(&t);
442     TIME_HILOGI(TIME_MODULE_CLIENT, "Time before: %{public}s", asctime(localtime(&t)));
443     auto getCurrentTimeZone = TimeServiceClient::GetInstance()->GetTimeZone();
444     EXPECT_FALSE(getCurrentTimeZone.empty());
445 
446     std::string timeZoneShanghai("Asia/Shanghai");
447     int32_t code;
448     bool result = TimeServiceClient::GetInstance()->SetTimeZone(timeZoneShanghai, code);
449     EXPECT_TRUE(result);
450     EXPECT_EQ(code, 0);
451     auto getTimeZone = TimeServiceClient::GetInstance()->GetTimeZone();
452     EXPECT_EQ(getTimeZone, timeZoneShanghai);
453     bool ret = TimeServiceClient::GetInstance()->SetTimeZone(getCurrentTimeZone);
454     EXPECT_TRUE(ret);
455     DeletePermission();
456 }
457 
458 /**
459 * @tc.name: GetWallTimeMs001
460 * @tc.desc: get wall time (ms).
461 * @tc.type: FUNC
462 */
463 HWTEST_F(TimeServiceTest, GetWallTimeMs001, TestSize.Level1)
464 {
465     auto time1 = TimeServiceClient::GetInstance()->GetWallTimeMs();
466     EXPECT_TRUE(time1 != -1);
467     auto time2 = TimeServiceClient::GetInstance()->GetWallTimeMs();
468     EXPECT_TRUE(time2 >= time1);
469 }
470 
471 /**
472 * @tc.name: GetWallTimeNs001
473 * @tc.desc: get wall time (ns).
474 * @tc.type: FUNC
475 */
476 HWTEST_F(TimeServiceTest, GetWallTimeNs001, TestSize.Level1)
477 {
478     auto time1 = TimeServiceClient::GetInstance()->GetWallTimeNs();
479     EXPECT_TRUE(time1 != -1);
480     auto time2 = TimeServiceClient::GetInstance()->GetWallTimeNs();
481     EXPECT_TRUE(time2 >= time1);
482 }
483 
484 /**
485 * @tc.name: GetBootTimeNs001
486 * @tc.desc: get boot time (ns).
487 * @tc.type: FUNC
488 */
489 HWTEST_F(TimeServiceTest, GetBootTimeNs001, TestSize.Level1)
490 {
491     auto time1 = TimeServiceClient::GetInstance()->GetBootTimeNs();
492     EXPECT_TRUE(time1 != -1);
493     auto time2 = TimeServiceClient::GetInstance()->GetBootTimeNs();
494     EXPECT_TRUE(time2 >= time1);
495 }
496 
497 /**
498 * @tc.name: GetMonotonicTimeMs001
499 * @tc.desc: get monotonic time (ms).
500 * @tc.type: FUNC
501 */
502 HWTEST_F(TimeServiceTest, GetMonotonicTimeMs001, TestSize.Level1)
503 {
504     auto time1 = TimeServiceClient::GetInstance()->GetMonotonicTimeMs();
505     EXPECT_TRUE(time1 != -1);
506     auto time2 = TimeServiceClient::GetInstance()->GetMonotonicTimeMs();
507     EXPECT_TRUE(time2 >= time1);
508 }
509 
510 /**
511 * @tc.name: GetMonotonicTimeNs001
512 * @tc.desc: get monotonic time (ns).
513 * @tc.type: FUNC
514 */
515 HWTEST_F(TimeServiceTest, GetMonotonicTimeNs001, TestSize.Level1)
516 {
517     auto time1 = TimeServiceClient::GetInstance()->GetMonotonicTimeNs();
518     EXPECT_TRUE(time1 != -1);
519     auto time2 = TimeServiceClient::GetInstance()->GetMonotonicTimeNs();
520     EXPECT_TRUE(time2 >= time1);
521 }
522 
523 /**
524 * @tc.name: GetThreadTimeMs001
525 * @tc.desc: get thread time (ms).
526 * @tc.type: FUNC
527 */
528 HWTEST_F(TimeServiceTest, GetThreadTimeMs001, TestSize.Level1)
529 {
530     auto time1 = TimeServiceClient::GetInstance()->GetThreadTimeMs();
531     EXPECT_TRUE(time1 != -1);
532 }
533 
534 /**
535 * @tc.name: GetThreadTimeNs001
536 * @tc.desc: get thread time (ns).
537 * @tc.type: FUNC
538 */
539 HWTEST_F(TimeServiceTest, GetThreadTimeNs001, TestSize.Level1)
540 {
541     auto time1 = TimeServiceClient::GetInstance()->GetThreadTimeNs();
542     EXPECT_TRUE(time1 != -1);
543 }
544 
545 /**
546 * @tc.name: CreateTimer001
547 * @tc.desc: Create system timer.
548 * @tc.type: FUNC
549 */
550 HWTEST_F(TimeServiceTest, CreateTimer001, TestSize.Level1)
551 {
552     AddPermission();
553     uint64_t timerId = 0;
554     auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId, 5);
555     EXPECT_FALSE(ret);
556     ret = TimeServiceClient::GetInstance()->StopTimer(timerId);
557     EXPECT_FALSE(ret);
558     ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId);
559     EXPECT_FALSE(ret);
560     DeletePermission();
561 }
562 
563 /**
564 * @tc.name: CreateTimer002
565 * @tc.desc: Create system timer.
566 * @tc.type: FUNC
567 */
568 HWTEST_F(TimeServiceTest, CreateTimer002, TestSize.Level1)
569 {
570     AddPermission();
571     auto timerInfo = std::make_shared<TimerInfoTest>();
572     timerInfo->SetType(1);
573     timerInfo->SetRepeat(false);
574     timerInfo->SetInterval(0);
575     timerInfo->SetWantAgent(nullptr);
576     timerInfo->SetCallbackInfo(TimeOutCallback1);
577     auto timerId1 = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
578     TIME_HILOGI(TIME_MODULE_CLIENT, "timerId now : %{public}" PRId64 "", timerId1);
579     EXPECT_TRUE(timerId1 > 0);
580     auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId1, 2000);
581     EXPECT_TRUE(ret);
582     ret = TimeServiceClient::GetInstance()->StopTimer(timerId1);
583     EXPECT_TRUE(ret);
584     ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId1);
585     EXPECT_TRUE(ret);
586     DeletePermission();
587 }
588 
589 /**
590 * @tc.name: CreateTimer003
591 * @tc.desc: Create system timer.
592 * @tc.type: FUNC
593 */
594 HWTEST_F(TimeServiceTest, CreateTimer003, TestSize.Level1)
595 {
596     AddPermission();
597     auto timerInfo = std::make_shared<TimerInfoTest>();
598     timerInfo->SetType(1);
599     timerInfo->SetRepeat(false);
600     timerInfo->SetInterval(0);
601     auto ability = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
602     timerInfo->SetWantAgent(ability);
603     timerInfo->SetCallbackInfo(TimeOutCallback1);
604     auto timerId1 = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
605     EXPECT_TRUE(timerId1 > 0);
606     DeletePermission();
607 }
608 
609 /**
610 * @tc.name: CreateTimer004
611 * @tc.desc: Create system timer.
612 * @tc.type: FUNC
613 */
614 HWTEST_F(TimeServiceTest, CreateTimer004, TestSize.Level1)
615 {
616     AddPermission();
617     g_data1 = 0;
618     auto timerInfo = std::make_shared<TimerInfoTest>();
619     timerInfo->SetType(1);
620     timerInfo->SetRepeat(false);
621     timerInfo->SetInterval(0);
622     timerInfo->SetWantAgent(nullptr);
623     timerInfo->SetCallbackInfo(TimeOutCallback1);
624     auto timerId1 = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
625     EXPECT_TRUE(timerId1 > 0);
626     auto BootTimeNano = system_clock::now().time_since_epoch().count();
627     auto BootTimeMilli = BootTimeNano / NANO_TO_MILESECOND;
628     auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId1, BootTimeMilli + 2000);
629     EXPECT_TRUE(ret);
630     ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId1);
631     EXPECT_TRUE(ret);
632     EXPECT_TRUE(g_data1 == 0);
633     ret = TimeServiceClient::GetInstance()->StopTimer(timerId1);
634     EXPECT_FALSE(ret);
635     DeletePermission();
636 }
637 
638 /**
639 * @tc.name: CreateTimer005
640 * @tc.desc: Create system timer.
641 * @tc.type: FUNC
642 */
643 HWTEST_F(TimeServiceTest, CreateTimer005, TestSize.Level1)
644 {
645     AddPermission();
646     g_data1 = 1;
647     auto timerInfo = std::make_shared<TimerInfoTest>();
648     timerInfo->SetType(0);
649     timerInfo->SetRepeat(false);
650     timerInfo->SetInterval(0);
651     timerInfo->SetWantAgent(nullptr);
652     timerInfo->SetCallbackInfo(TimeOutCallback1);
653 
654     struct timeval timeOfDay {
655     };
656     gettimeofday(&timeOfDay, NULL);
657     int64_t currentTime = (timeOfDay.tv_sec + 100) * 1000 + timeOfDay.tv_usec / 1000;
658     if (currentTime < 0) {
659         currentTime = 0;
660     }
661     auto timerId1 = TimeServiceClient::GetInstance()->CreateTimer(timerInfo);
662     EXPECT_TRUE(timerId1 > 0);
663 
664     auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId1, static_cast<uint64_t>(currentTime));
665     EXPECT_TRUE(ret);
666     ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId1);
667     EXPECT_TRUE(ret);
668     EXPECT_TRUE(g_data1 == 1);
669 
670     ret = TimeServiceClient::GetInstance()->StopTimer(timerId1);
671     EXPECT_FALSE(ret);
672     DeletePermission();
673 }
674 
675 /**
676 * @tc.name: CreateTimer006
677 * @tc.desc: Create system timer.
678 * @tc.type: FUNC
679 */
680 HWTEST_F(TimeServiceTest, CreateTimer006, TestSize.Level1)
681 {
682     AddPermission();
683     auto timerId1 = TimeServiceClient::GetInstance()->CreateTimer(nullptr);
684     uint64_t ret = 0;
685     EXPECT_EQ(timerId1, ret);
686     DeletePermission();
687 }
688 
689 /**
690 * @tc.name: SntpClient001.
691 * @tc.desc: test SntpClient.
692 * @tc.type: FUNC
693 * @tc.require:
694 */
695 HWTEST_F(TimeServiceTest, SntpClient001, TestSize.Level0)
696 {
697     std::shared_ptr<SNTPClient> ntpClient = std::make_shared<SNTPClient>();
698     auto ret = ntpClient->RequestTime(NTP_CN_SERVER);
699     EXPECT_FALSE(ret);
700 
701     auto buffer = std::string("31234114451");
702     auto millisecond = ntpClient->GetNtpTimestamp64(0, buffer.c_str());
703     EXPECT_GT(millisecond, 0);
704     millisecond = 0;
705     millisecond = ntpClient->GetNtpField32(0, buffer.c_str());
706     EXPECT_GT(millisecond, 0);
707 
708     auto timeStamp = ntpClient->ConvertNtpToStamp(0);
709     EXPECT_EQ(timeStamp, 0);
710     timeStamp = ntpClient->ConvertNtpToStamp(100);
711     EXPECT_EQ(timeStamp, 0);
712     timeStamp = ntpClient->ConvertNtpToStamp(2147483648);
713     EXPECT_EQ(timeStamp, 0);
714     timeStamp = ntpClient->ConvertNtpToStamp(31234114451);
715     EXPECT_EQ(timeStamp, 0);
716     uint64_t time = 999999999911;
717     timeStamp = ntpClient->ConvertNtpToStamp(time << 32);
718     EXPECT_GT(timeStamp, 0);
719 }
720 
721 /**
722 * @tc.name: NtpUpdateTime001.
723 * @tc.desc: test NtpUpdateTime.
724 * @tc.type: FUNC
725 * @tc.require:
726 */
727 HWTEST_F(TimeServiceTest, NtpUpdateTime001, TestSize.Level0)
728 {
729     auto ntpUpdateTime = std::make_shared<NtpUpdateTime>();
730     ntpUpdateTime->autoTimeInfo_.lastUpdateTime = 0;
731 
732     ntpUpdateTime->autoTimeInfo_.status = NETWORK_TIME_STATUS_OFF;
733     ntpUpdateTime->RefreshNetworkTimeByTimer(123);
734     ntpUpdateTime->autoTimeInfo_.status = NETWORK_TIME_STATUS_ON;
735     ntpUpdateTime->RefreshNetworkTimeByTimer(123);
736     ntpUpdateTime->nitzUpdateTimeMilli_ = steady_clock::now().time_since_epoch().count();
737     ntpUpdateTime->RefreshNetworkTimeByTimer(123);
738     EXPECT_EQ(ntpUpdateTime->autoTimeInfo_.lastUpdateTime, 0);
739 }
740 
741 /**
742 * @tc.name: NtpTrustedTime001.
743 * @tc.desc: test NtpTrustedTime.
744 * @tc.type: FUNC
745 * @tc.require:
746 */
747 HWTEST_F(TimeServiceTest, NtpTrustedTime001, TestSize.Level0)
748 {
749     std::shared_ptr<NtpTrustedTime> ntpTrustedTime = std::make_shared<NtpTrustedTime>();
750     ntpTrustedTime->mTimeResult = nullptr;
751     int64_t errCode = ntpTrustedTime->CurrentTimeMillis();
752     EXPECT_EQ(errCode, -1);
753     errCode = ntpTrustedTime->GetCacheAge();
754     EXPECT_EQ(errCode, INT_MAX);
755 
756     ntpTrustedTime->mTimeResult = std::make_shared<NtpTrustedTime::TimeResult>(0, 0, 0);
757     int64_t time = ntpTrustedTime->CurrentTimeMillis();
758     EXPECT_GT(time, 0);
759     int64_t cacheAge = ntpTrustedTime->GetCacheAge();
760     EXPECT_GT(cacheAge, 0);
761 }
762 
763 /**
764 * @tc.name: PowerSubscriber001
765 * @tc.desc: test power subscriber data is invalid.
766 * @tc.type: FUNC
767 * @tc.require:
768 */
769 HWTEST_F(TimeServiceTest, PowerSubscriber001, TestSize.Level0)
770 {
771     auto timerId = TimeTickNotify::GetInstance().timerId_;
772     std::string commonEvent = EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED;
773     EventFwk::Want want;
774     want.SetAction(commonEvent);
775     int32_t code = 100;
776     std::string data(commonEvent);
777     EventFwk::CommonEventData eventData(want, code, data);
778     OHOS::EventFwk::MatchingSkills matchingSkills;
779     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED);
780     auto subscriber = std::make_shared<PowerSubscriber>(CommonEventSubscribeInfo(matchingSkills));
781     subscriber->OnReceiveEvent(eventData);
782     EXPECT_EQ(timerId, TimeTickNotify::GetInstance().timerId_);
783 }
784 
785 /**
786 * @tc.name: PowerSubscriber002
787 * @tc.desc: test power subscriber data is valid.
788 * @tc.type: FUNC
789 * @tc.require:
790 */
791 HWTEST_F(TimeServiceTest, PowerSubscriber002, TestSize.Level0)
792 {
793     auto timerId = TimeTickNotify::GetInstance().timerId_;
794     std::string commonEvent = EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON;
795     EventFwk::Want want;
796     want.SetAction(commonEvent);
797     int32_t code = RESERVED_UID;
798     std::string data(commonEvent);
799     EventFwk::CommonEventData eventData(want, code, data);
800     OHOS::EventFwk::MatchingSkills matchingSkills;
801     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
802     auto subscriber = std::make_shared<PowerSubscriber>(CommonEventSubscribeInfo(matchingSkills));
803     subscriber->OnReceiveEvent(eventData);
804     EXPECT_NE(timerId, TimeTickNotify::GetInstance().timerId_);
805 }
806 } // namespace