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