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