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 "event_manager.h"
34 #include "ntp_update_time.h"
35 #include "cjson_helper.h"
36
37 #define private public
38 #define protected public
39 #include "sntp_client.h"
40 #include "ntp_update_time.h"
41 #include "time_system_ability.h"
42 #include "ntp_trusted_time.h"
43 #include "time_tick_notify.h"
44 #include "timer_proxy.h"
45 #include "time_service_test.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 std::set<int> RESERVED_PIDLIST = {1111, 2222};
55 const std::string NETWORK_TIME_STATUS_OFF = "OFF";
56 const std::string NETWORK_TIME_STATUS_ON = "ON";
57 const std::string AUTO_TIME_STATUS_ON = "ON";
58 const uint64_t TIMER_ID = 88888;
59 constexpr int64_t MINUTE_TO_MILLISECOND = 60000;
60 constexpr char BYTE_SNTP_MESSAGE = 0xD8;
61
62 static HapPolicyParams g_policyA = {
63 .apl = APL_SYSTEM_CORE,
64 .domain = "test.domain",
65 .permList = {
66 {
67 .permissionName = "ohos.permission.SET_TIME",
68 .bundleName = "ohos.permission_test.demoB",
69 .grantMode = 1,
70 .availableLevel = APL_NORMAL,
71 .label = "label",
72 .labelId = 1,
73 .description = "test",
74 .descriptionId = 1
75 },
76 {
77 .permissionName = "ohos.permission.SET_TIME_ZONE",
78 .bundleName = "ohos.permission_test.demoB",
79 .grantMode = 1,
80 .availableLevel = APL_NORMAL,
81 .label = "label",
82 .labelId = 1,
83 .description = "test",
84 .descriptionId = 1
85 },
86 {
87 .permissionName = "ohos.permission.MANAGE_LOCAL_ACCOUNTS",
88 .bundleName = "ohos.permission_test.demoB",
89 .grantMode = 1,
90 .availableLevel = APL_NORMAL,
91 .label = "label",
92 .labelId = 1,
93 .description = "test",
94 .descriptionId = 1
95 }
96 },
97 .permStateList = {
98 {
99 .permissionName = "ohos.permission.SET_TIME",
100 .isGeneral = true,
101 .resDeviceID = { "local" },
102 .grantStatus = { PermissionState::PERMISSION_GRANTED },
103 .grantFlags = { 1 }
104 },
105 {
106 .permissionName = "ohos.permission.SET_TIME_ZONE",
107 .isGeneral = true,
108 .resDeviceID = { "local" },
109 .grantStatus = { PermissionState::PERMISSION_GRANTED },
110 .grantFlags = { 1 }
111 },
112 {
113 .permissionName = "ohos.permission.MANAGE_LOCAL_ACCOUNTS",
114 .isGeneral = true,
115 .resDeviceID = { "local" },
116 .grantStatus = { PermissionState::PERMISSION_GRANTED },
117 .grantFlags = { 1 }
118 }
119 }
120 };
121
122 static HapInfoParams g_systemInfoParams = {
123 .userID = 1,
124 .bundleName = "timer",
125 .instIndex = 0,
126 .appIDDesc = "test",
127 .apiVersion = 8,
128 .isSystemApp = true
129 };
130
131 static HapPolicyParams g_policyB = { .apl = APL_NORMAL, .domain = "test.domain" };
132
133 static HapInfoParams g_notSystemInfoParams = {
134 .userID = 100,
135 .bundleName = "timer",
136 .instIndex = 0,
137 .appIDDesc = "test",
138 .apiVersion = 8,
139 .isSystemApp = false
140 };
141
142 class TimeServiceTimeTest : public testing::Test {
143 public:
144 static void SetUpTestCase(void);
145 static void TearDownTestCase(void);
146 void SetUp();
147 void TearDown();
148 void AddPermission();
149 void DeletePermission();
150 void StartIdleTimer();
151 void DestroyIdleTimer();
152 };
153
SetUpTestCase(void)154 void TimeServiceTimeTest::SetUpTestCase(void)
155 {
156 }
157
TearDownTestCase(void)158 void TimeServiceTimeTest::TearDownTestCase(void)
159 {
160 }
161
SetUp(void)162 void TimeServiceTimeTest::SetUp(void)
163 {
164 }
165
TearDown(void)166 void TimeServiceTimeTest::TearDown(void)
167 {
168 }
169
AddPermission()170 void TimeServiceTimeTest::AddPermission()
171 {
172 AccessTokenIDEx tokenIdEx = { 0 };
173 tokenIdEx = AccessTokenKit::AllocHapToken(g_systemInfoParams, g_policyA);
174 SetSelfTokenID(tokenIdEx.tokenIDEx);
175 }
176
DeletePermission()177 void TimeServiceTimeTest::DeletePermission()
178 {
179 AccessTokenIDEx tokenIdEx = { 0 };
180 tokenIdEx = AccessTokenKit::AllocHapToken(g_notSystemInfoParams, g_policyB);
181 SetSelfTokenID(tokenIdEx.tokenIDEx);
182 }
183
TestNtpThread(const char * name)184 void TestNtpThread(const char *name)
185 {
186 {
187 std::lock_guard<std::mutex> autoLock(NtpUpdateTime::requestMutex_);
188 }
189 NtpUpdateTime::SetSystemTime();
190 }
191
192 /**
193 * @tc.name: SetTime001
194 * @tc.desc: set system time.
195 * @tc.type: FUNC
196 */
197 HWTEST_F(TimeServiceTimeTest, SetTime001, TestSize.Level1)
198 {
199 AddPermission();
200 struct timeval currentTime {
201 };
202 gettimeofday(¤tTime, NULL);
203 int64_t time = (currentTime.tv_sec + 1000) * 1000 + currentTime.tv_usec / 1000;
204 ASSERT_GT(time, 0);
205 TIME_HILOGI(TIME_MODULE_CLIENT, "Time now : %{public}" PRId64 "", time);
206 bool result = TimeServiceClient::GetInstance()->SetTime(time);
207 EXPECT_TRUE(result);
208 DeletePermission();
209 }
210
211 /**
212 * @tc.name: SetTime002
213 * @tc.desc: Verify set system time with negative value will return false.
214 * @tc.type: FUNC
215 */
216 HWTEST_F(TimeServiceTimeTest, SetTime002, TestSize.Level1)
217 {
218 bool result = TimeServiceClient::GetInstance()->SetTime(-1);
219 EXPECT_FALSE(result);
220 }
221
222 /**
223 * @tc.name: SetTime003
224 * @tc.desc: Verify set system time with LLONG_MAX will return false.
225 * @tc.type: FUNC
226 */
227 HWTEST_F(TimeServiceTimeTest, SetTime003, TestSize.Level1)
228 {
229 bool result = TimeServiceClient::GetInstance()->SetTime(LLONG_MAX);
230 EXPECT_FALSE(result);
231 }
232
233 /**
234 * @tc.name: SetTime004
235 * @tc.desc: set system time.
236 * @tc.type: FUNC
237 */
238 HWTEST_F(TimeServiceTimeTest, SetTime004, TestSize.Level1)
239 {
240 AddPermission();
241 struct timeval currentTime {
242 };
243 gettimeofday(¤tTime, NULL);
244 int64_t time = (currentTime.tv_sec + 1000) * 1000 + currentTime.tv_usec / 1000;
245 ASSERT_GT(time, 0);
246 TIME_HILOGI(TIME_MODULE_CLIENT, "Time now : %{public}" PRId64 "", time);
247 int32_t code;
248 bool result = TimeServiceClient::GetInstance()->SetTime(time, code);
249 EXPECT_TRUE(result);
250 EXPECT_EQ(code, 0);
251 DeletePermission();
252 auto res = TimeSystemAbility::GetInstance()->SetTime(0, 0);
253 EXPECT_EQ(res, E_TIME_NO_PERMISSION);
254 }
255
256 /**
257 * @tc.name: SetTimeInner001
258 * @tc.desc: set system time by ability.
259 * @tc.type: FUNC
260 */
261 HWTEST_F(TimeServiceTimeTest, SetTimeInner001, TestSize.Level1)
262 {
263 struct timeval currentTime {
264 };
265 gettimeofday(¤tTime, NULL);
266 int64_t time = (currentTime.tv_sec + 1000) * 1000 + currentTime.tv_usec / 1000;
267 ASSERT_GT(time, 0);
268 TIME_HILOGI(TIME_MODULE_CLIENT, "Time now : %{public}" PRId64 "", time);
269 auto result = TimeSystemAbility::GetInstance()->SetTimeInner(time);
270 EXPECT_EQ(result, E_TIME_OK);
271 }
272
273
274 /**
275 * @tc.name: SetTimeZone001
276 * @tc.desc: set system time zone.
277 * @tc.type: FUNC
278 */
279 HWTEST_F(TimeServiceTimeTest, SetTimeZone001, TestSize.Level1)
280 {
281 AddPermission();
282 time_t t;
283 (void)time(&t);
284 TIME_HILOGI(TIME_MODULE_CLIENT, "Time before: %{public}s", asctime(localtime(&t)));
285 auto getCurrentTimeZone = TimeServiceClient::GetInstance()->GetTimeZone();
286 EXPECT_FALSE(getCurrentTimeZone.empty());
287
288 std::string timeZoneNicosia("Asia/Nicosia");
289 bool result = TimeServiceClient::GetInstance()->SetTimeZone(timeZoneNicosia);
290 EXPECT_TRUE(result);
291 auto getTimeZoneNicosia = TimeServiceClient::GetInstance()->GetTimeZone();
292 EXPECT_EQ(timeZoneNicosia, getTimeZoneNicosia);
293 bool ret = TimeServiceClient::GetInstance()->SetTimeZone(getCurrentTimeZone);
294 EXPECT_TRUE(ret);
295 DeletePermission();
296 }
297
298 /**
299 * @tc.name: SetTimeZone002
300 * @tc.desc: Verify set system time zone with invalid timezone will return false.
301 * @tc.type: FUNC
302 */
303 HWTEST_F(TimeServiceTimeTest, SetTimeZone002, TestSize.Level1)
304 {
305 bool result = TimeServiceClient::GetInstance()->SetTimeZone("123");
306 EXPECT_FALSE(result);
307 }
308
309 /**
310 * @tc.name: SetTimeZone003
311 * @tc.desc: set system time zone.
312 * @tc.type: FUNC
313 */
314 HWTEST_F(TimeServiceTimeTest, SetTimeZone003, TestSize.Level1)
315 {
316 AddPermission();
317 time_t t;
318 (void)time(&t);
319 TIME_HILOGI(TIME_MODULE_CLIENT, "Time before: %{public}s", asctime(localtime(&t)));
320 auto getCurrentTimeZone = TimeServiceClient::GetInstance()->GetTimeZone();
321 EXPECT_FALSE(getCurrentTimeZone.empty());
322
323 std::string timeZoneShanghai("Asia/Shanghai");
324 int32_t code;
325 bool result = TimeServiceClient::GetInstance()->SetTimeZone(timeZoneShanghai, code);
326 EXPECT_TRUE(result);
327 EXPECT_EQ(code, 0);
328 auto getTimeZone = TimeServiceClient::GetInstance()->GetTimeZone();
329 EXPECT_EQ(getTimeZone, timeZoneShanghai);
330 bool ret = TimeServiceClient::GetInstance()->SetTimeZone(getCurrentTimeZone);
331 EXPECT_TRUE(ret);
332 DeletePermission();
333 auto res = TimeSystemAbility::GetInstance()->SetTimeZone("", 0);
334 EXPECT_EQ(res, E_TIME_NO_PERMISSION);
335 }
336
337 /**
338 * @tc.name: SetTimeZoneInner001
339 * @tc.desc: set system time zone by ability.
340 * @tc.type: FUNC
341 */
342 HWTEST_F(TimeServiceTimeTest, SetTimeZoneInner001, TestSize.Level1)
343 {
344 time_t t;
345 (void)time(&t);
346 TIME_HILOGI(TIME_MODULE_CLIENT, "Time before: %{public}s", asctime(localtime(&t)));
347 auto getCurrentTimeZone = TimeServiceClient::GetInstance()->GetTimeZone();
348 EXPECT_FALSE(getCurrentTimeZone.empty());
349
350 std::string timeZoneNicosia("Asia/Shanghai");
351 bool result = TimeSystemAbility::GetInstance()->SetTimeZoneInner(timeZoneNicosia);
352 EXPECT_EQ(result, E_TIME_OK);
353 auto getTimeZoneNicosia = TimeServiceClient::GetInstance()->GetTimeZone();
354 EXPECT_EQ(timeZoneNicosia, getTimeZoneNicosia);
355 auto ret = TimeSystemAbility::GetInstance()->SetTimeZoneInner(getCurrentTimeZone);
356 EXPECT_EQ(ret, E_TIME_OK);
357 }
358
359 /**
360 * @tc.name: GetWallTimeMs001
361 * @tc.desc: get wall time (ms).
362 * @tc.type: FUNC
363 */
364 HWTEST_F(TimeServiceTimeTest, GetWallTimeMs001, TestSize.Level1)
365 {
366 auto time1 = TimeServiceClient::GetInstance()->GetWallTimeMs();
367 EXPECT_NE(time1, -1);
368 auto time2 = TimeServiceClient::GetInstance()->GetWallTimeMs();
369 EXPECT_GE(time2, time1);
370 }
371
372 /**
373 * @tc.name: GetWallTimeNs001
374 * @tc.desc: get wall time (ns).
375 * @tc.type: FUNC
376 */
377 HWTEST_F(TimeServiceTimeTest, GetWallTimeNs001, TestSize.Level1)
378 {
379 auto time1 = TimeServiceClient::GetInstance()->GetWallTimeNs();
380 EXPECT_NE(time1, -1);
381 auto time2 = TimeServiceClient::GetInstance()->GetWallTimeNs();
382 EXPECT_GE(time2, time1);
383 }
384
385 /**
386 * @tc.name: GetBootTimeNs001
387 * @tc.desc: get boot time (ns).
388 * @tc.type: FUNC
389 */
390 HWTEST_F(TimeServiceTimeTest, GetBootTimeNs001, TestSize.Level1)
391 {
392 auto time1 = TimeServiceClient::GetInstance()->GetBootTimeNs();
393 EXPECT_NE(time1, -1);
394 auto time2 = TimeServiceClient::GetInstance()->GetBootTimeNs();
395 EXPECT_GE(time2, time1);
396 }
397
398 /**
399 * @tc.name: GetMonotonicTimeMs001
400 * @tc.desc: get monotonic time (ms).
401 * @tc.type: FUNC
402 */
403 HWTEST_F(TimeServiceTimeTest, GetMonotonicTimeMs001, TestSize.Level1)
404 {
405 auto time1 = TimeServiceClient::GetInstance()->GetMonotonicTimeMs();
406 EXPECT_NE(time1, -1);
407 auto time2 = TimeServiceClient::GetInstance()->GetMonotonicTimeMs();
408 EXPECT_GE(time2, time1);
409 }
410
411 /**
412 * @tc.name: GetMonotonicTimeNs001
413 * @tc.desc: get monotonic time (ns).
414 * @tc.type: FUNC
415 */
416 HWTEST_F(TimeServiceTimeTest, GetMonotonicTimeNs001, TestSize.Level1)
417 {
418 auto time1 = TimeServiceClient::GetInstance()->GetMonotonicTimeNs();
419 EXPECT_NE(time1, -1);
420 auto time2 = TimeServiceClient::GetInstance()->GetMonotonicTimeNs();
421 EXPECT_GE(time2, time1);
422 }
423
424 /**
425 * @tc.name: GetThreadTimeMs001
426 * @tc.desc: get thread time (ms).
427 * @tc.type: FUNC
428 */
429 HWTEST_F(TimeServiceTimeTest, GetThreadTimeMs001, TestSize.Level1)
430 {
431 auto time1 = TimeServiceClient::GetInstance()->GetThreadTimeMs();
432 EXPECT_NE(time1, -1);
433 }
434
435 /**
436 * @tc.name: GetThreadTimeNs001
437 * @tc.desc: get thread time (ns).
438 * @tc.type: FUNC
439 */
440 HWTEST_F(TimeServiceTimeTest, GetThreadTimeNs001, TestSize.Level1)
441 {
442 auto time1 = TimeServiceClient::GetInstance()->GetThreadTimeNs();
443 EXPECT_NE(time1, -1);
444 }
445
446 /**
447 * @tc.name: SntpClient001
448 * @tc.desc: test GetNtpTimestamp64 will return timestamp.
449 * @tc.type: FUNC
450 */
451 HWTEST_F(TimeServiceTimeTest, SntpClient001, TestSize.Level0)
452 {
453 std::shared_ptr<SNTPClient> ntpClient = std::make_shared<SNTPClient>();
454 auto buffer = std::string("31234114451");
455 auto millisecond = ntpClient->GetNtpTimestamp64(0, buffer.c_str());
456 EXPECT_GT(millisecond, 0);
457 }
458
459 /**
460 * @tc.name: SntpClient002
461 * @tc.desc: test GetNtpField32 will return time.
462 * @tc.type: FUNC
463 */
464 HWTEST_F(TimeServiceTimeTest, SntpClient002, TestSize.Level0)
465 {
466 std::shared_ptr<SNTPClient> ntpClient = std::make_shared<SNTPClient>();
467 auto buffer = std::string("31234114451");
468 auto millisecond = ntpClient->GetNtpField32(0, buffer.c_str());
469 EXPECT_GT(millisecond, 0);
470 }
471
472 /**
473 * @tc.name: SntpClient003
474 * @tc.desc: test ConvertNtpToStamp with invalid value will return 0 timestamp.
475 * @tc.type: FUNC
476 */
477 HWTEST_F(TimeServiceTimeTest, SntpClient003, TestSize.Level0)
478 {
479 std::shared_ptr<SNTPClient> ntpClient = std::make_shared<SNTPClient>();
480 auto timeStamp = ntpClient->ConvertNtpToStamp(0);
481 EXPECT_EQ(timeStamp, 0);
482 timeStamp = ntpClient->ConvertNtpToStamp(100);
483 EXPECT_EQ(timeStamp, 0);
484 timeStamp = ntpClient->ConvertNtpToStamp(2147483648);
485 EXPECT_EQ(timeStamp, 0);
486 timeStamp = ntpClient->ConvertNtpToStamp(31234114451);
487 EXPECT_EQ(timeStamp, 0);
488 }
489
490 /**
491 * @tc.name: SntpClient004
492 * @tc.desc: test ConvertNtpToStamp with valid value will return true timestamp.
493 * @tc.type: FUNC
494 */
495 HWTEST_F(TimeServiceTimeTest, SntpClient004, TestSize.Level0)
496 {
497 std::shared_ptr<SNTPClient> ntpClient = std::make_shared<SNTPClient>();
498 uint64_t time = 999999999911;
499 auto timeStamp = ntpClient->ConvertNtpToStamp(time << 32);
500 EXPECT_EQ(timeStamp, 1358598439000);
501 }
502
503 /**
504 * @tc.name: SntpClient005
505 * @tc.desc: Verify RequestTime with null string will return false.
506 * @tc.type: FUNC
507 */
508 HWTEST_F(TimeServiceTimeTest, SntpClient005, TestSize.Level0)
509 {
510 std::shared_ptr<SNTPClient> ntpClient = std::make_shared<SNTPClient>();
511 auto res = ntpClient -> RequestTime("");
512 EXPECT_FALSE(res);
513 }
514
515 /**
516 * @tc.name: SntpClient006
517 * @tc.desc: test SetClockOffset of SntpClient.
518 * @tc.type: FUNC
519 */
520 HWTEST_F(TimeServiceTimeTest, SntpClient006, TestSize.Level0)
521 {
522 std::shared_ptr<SNTPClient> ntpClient = std::make_shared<SNTPClient>();
523 ntpClient -> SetClockOffset(1);
524 EXPECT_EQ(ntpClient -> m_clockOffset, 1);
525 }
526
527 /**
528 * @tc.name: SntpClient007
529 * @tc.desc: test ConvertUnixToNtp of SntpClient.
530 * @tc.type: FUNC
531 */
532 HWTEST_F(TimeServiceTimeTest, SntpClient007, TestSize.Level0)
533 {
534 std::shared_ptr<SNTPClient> ntpClient = std::make_shared<SNTPClient>();
535 OHOS::MiscServices::SNTPClient::ntp_timestamp ntp{.second = 0, .fraction = 0};
536 struct timeval unix;
537 gettimeofday(&unix, nullptr);
538 ntpClient -> ConvertUnixToNtp(&ntp, &unix);
539 EXPECT_NE(ntp.second, 0);
540 EXPECT_NE(ntp.fraction, 0);
541 }
542
543 /**
544 * @tc.name: SntpClient008
545 * @tc.desc: test CreateMessage of SntpClient.
546 * @tc.type: FUNC
547 */
548 HWTEST_F(TimeServiceTimeTest, SntpClient008, TestSize.Level0)
549 {
550 std::shared_ptr<SNTPClient> ntpClient = std::make_shared<SNTPClient>();
551 char sendBuf[48] = { 0 };
552 ntpClient -> CreateMessage(sendBuf);
553 EXPECT_EQ(sendBuf[0], '\x1B');
554 }
555
556 /**
557 * @tc.name: SntpClient009
558 * @tc.desc: test ReceivedMessage will return true when received valid message.
559 * @tc.type: FUNC
560 */
561 HWTEST_F(TimeServiceTimeTest, SntpClient009, TestSize.Level0)
562 {
563 std::shared_ptr<SNTPClient> ntpClient = std::make_shared<SNTPClient>();
564 char buf[48] = {0};
565 buf[32] = BYTE_SNTP_MESSAGE;
566 buf[40] = BYTE_SNTP_MESSAGE;
567 auto res = ntpClient -> ReceivedMessage(buf);
568 EXPECT_TRUE(res);
569 }
570
571 /**
572 * @tc.name: SntpClient010
573 * @tc.desc: test ReceivedMessage will return false when received invalid message.
574 * @tc.type: FUNC
575 */
576 HWTEST_F(TimeServiceTimeTest, SntpClient010, TestSize.Level0)
577 {
578 std::shared_ptr<SNTPClient> ntpClient = std::make_shared<SNTPClient>();
579 char buf[48] = {0};
580 auto res = ntpClient -> ReceivedMessage(buf);
581 EXPECT_FALSE(res);
582 buf[32] = BYTE_SNTP_MESSAGE;
583 buf[40] = 0;
584 res = ntpClient -> ReceivedMessage(buf);
585 EXPECT_FALSE(res);
586 buf[32] = 0;
587 buf[40] = BYTE_SNTP_MESSAGE;
588 res = ntpClient -> ReceivedMessage(buf);
589 EXPECT_FALSE(res);
590 }
591
592 /**
593 * @tc.name: SntpClient011
594 * @tc.desc: test GetReferenceId of SntpClient.
595 * @tc.type: FUNC
596 */
597 HWTEST_F(TimeServiceTimeTest, SntpClient011, TestSize.Level0)
598 {
599 char buf[5] = {'1', '2', '3', '4', '5'};
600 int array[5] = {0};
601 std::shared_ptr<SNTPClient> ntpClient = std::make_shared<SNTPClient>();
602 ntpClient -> GetReferenceId(0, buf, array);
603 EXPECT_EQ(array[0], '1');
604 }
605
606 /**
607 * @tc.name: SntpClient012
608 * @tc.desc: test Get of SntpClient.
609 * @tc.type: FUNC
610 */
611 HWTEST_F(TimeServiceTimeTest, SntpClient012, TestSize.Level0)
612 {
613 std::shared_ptr<SNTPClient> ntpClient = std::make_shared<SNTPClient>();
614 std::shared_ptr<SNTPClient::SNTPMessage> sntpMessage = std::make_shared<SNTPClient::SNTPMessage>();
615 sntpMessage -> clear();
616 ntpClient -> mNtpTime = 1;
617 ntpClient -> mNtpTimeReference = 2;
618 ntpClient -> mRoundTripTime = 3;
619 EXPECT_EQ(ntpClient -> getNtpTime(), 1);
620 EXPECT_EQ(ntpClient -> getNtpTimeReference(), 2);
621 EXPECT_EQ(ntpClient -> getRoundTripTime(), 3);
622 }
623
624 /**
625 * @tc.name: NtpTrustedTime001
626 * @tc.desc: test NtpTrustedTime with invalid value.
627 * @tc.type: FUNC
628 */
629 HWTEST_F(TimeServiceTimeTest, NtpTrustedTime001, TestSize.Level0)
630 {
631 std::shared_ptr<NtpTrustedTime> ntpTrustedTime = std::make_shared<NtpTrustedTime>();
632 ntpTrustedTime->mTimeResult = nullptr;
633 int64_t errCode = ntpTrustedTime->CurrentTimeMillis();
634 EXPECT_EQ(errCode, -1);
635 errCode = ntpTrustedTime->GetCacheAge();
636 EXPECT_EQ(errCode, INT_MAX);
637 }
638
639 /**
640 * @tc.name: NtpTrustedTime002
641 * @tc.desc: test NtpTrustedTime with valid value.
642 * @tc.type: FUNC
643 */
644 HWTEST_F(TimeServiceTimeTest, NtpTrustedTime002, TestSize.Level0)
645 {
646 std::shared_ptr<NtpTrustedTime> ntpTrustedTime = std::make_shared<NtpTrustedTime>();
647 ntpTrustedTime->mTimeResult = std::make_shared<NtpTrustedTime::TimeResult>(1, 1, 0);
648 int64_t time = ntpTrustedTime->CurrentTimeMillis();
649 EXPECT_GT(time, 0);
650 int64_t cacheAge = ntpTrustedTime->GetCacheAge();
651 EXPECT_GT(cacheAge, 0);
652 }
653
654 /**
655 * @tc.name: NtpTrustedTime003
656 * @tc.desc: test NtpTrustedTime clear.
657 * @tc.type: FUNC
658 */
659 HWTEST_F(TimeServiceTimeTest, NtpTrustedTime003, TestSize.Level0)
660 {
661 auto TimeResult = std::make_shared<OHOS::MiscServices::NtpTrustedTime::TimeResult>();
662 TimeResult->Clear();
663 EXPECT_EQ(TimeResult->mTimeMillis, 0);
664 EXPECT_EQ(TimeResult->mElapsedRealtimeMillis, 0);
665 EXPECT_EQ(TimeResult->mCertaintyMillis, 0);
666 }
667
668 /**
669 * @tc.name: NtpTrustedTime004
670 * @tc.desc: test GetTimeMillis.
671 * @tc.type: FUNC
672 */
673 HWTEST_F(TimeServiceTimeTest, NtpTrustedTime004, TestSize.Level0)
674 {
675 auto TimeResult = std::make_shared<OHOS::MiscServices::NtpTrustedTime::TimeResult>();
676 TimeResult->Clear();
677 auto res = TimeResult->GetTimeMillis();
678 EXPECT_EQ(res, 0);
679 }
680
681 /**
682 * @tc.name: TimeTick001
683 * @tc.desc: Check RefreshNextTriggerTime.
684 * @tc.type: FUNC
685 */
686 HWTEST_F(TimeServiceTimeTest, TimeTick001, TestSize.Level0)
687 {
688 AddPermission();
689 int64_t time = 0;
690 TimeUtils::GetWallTimeMs(time);
691 time = (time / MINUTE_TO_MILLISECOND) * MINUTE_TO_MILLISECOND;
692 bool result = TimeServiceClient::GetInstance()->SetTime(time);
693 EXPECT_TRUE(result);
694 auto pair = TimeTickNotify::GetInstance().RefreshNextTriggerTime();
695 EXPECT_EQ(pair.first, time + MINUTE_TO_MILLISECOND);
696 EXPECT_TRUE(pair.second);
697 DeletePermission();
698 }
699
700 /**
701 * @tc.name: SetSystemTime001
702 * @tc.desc: get ntp time.
703 * @tc.type: FUNC
704 */
705 HWTEST_F(TimeServiceTimeTest, SetSystemTime001, TestSize.Level1)
706 {
707 NtpUpdateTime::GetInstance().Init();
708 std::string tmp = NtpUpdateTime::autoTimeInfo_.status;
709 NtpUpdateTime::autoTimeInfo_.status = AUTO_TIME_STATUS_ON;
710
711 std::thread thread1(TestNtpThread, "thread1");
712 std::thread thread2(TestNtpThread, "thread2");
713 std::thread thread3(TestNtpThread, "thread3");
714 std::thread thread4(TestNtpThread, "thread4");
715 thread1.join();
716 thread2.join();
717 thread3.join();
718 thread4.join();
719
720 NtpUpdateTime::autoTimeInfo_.status = tmp;
721 }
722
723 /**
724 * @tc.name: NtpTime001
725 * @tc.desc: test SplitNtpAddrs return max size.
726 * @tc.type: FUNC
727 */
728 HWTEST_F(TimeServiceTimeTest, NtpTime001, TestSize.Level0)
729 {
730 const std::string ntpStr = "aaa,bbb,ccc,ddd,eee,fff";
731 auto res = NtpUpdateTime::GetInstance().SplitNtpAddrs(ntpStr);
732 EXPECT_EQ(res.size(), 5);
733 }
734
735 /**
736 * @tc.name: NtpTime002
737 * @tc.desc: test RefreshNetworkTimeByTimer.
738 * @tc.type: FUNC
739 */
740 HWTEST_F(TimeServiceTimeTest, NtpTime002, TestSize.Level0)
741 {
742 auto status = NtpUpdateTime::GetInstance().autoTimeInfo_.status;
743
744 NtpUpdateTime::GetInstance().autoTimeInfo_.status = NETWORK_TIME_STATUS_OFF;
745 NtpUpdateTime::GetInstance().RefreshNetworkTimeByTimer(TIMER_ID);
746
747 NtpUpdateTime::GetInstance().autoTimeInfo_.status = NETWORK_TIME_STATUS_ON;
748 NtpUpdateTime::GetInstance().RefreshNetworkTimeByTimer(TIMER_ID);
749
750 NtpUpdateTime::GetInstance().nitzUpdateTimeMilli_ = 0;
751 auto res = NtpUpdateTime::GetInstance().IsValidNITZTime();
752 EXPECT_FALSE(res);
753
754 NtpUpdateTime::GetInstance().UpdateNITZSetTime();
755 res = NtpUpdateTime::GetInstance().IsValidNITZTime();
756 EXPECT_TRUE(res);
757 NtpUpdateTime::GetInstance().RefreshNetworkTimeByTimer(TIMER_ID);
758
759 NtpUpdateTime::GetInstance().autoTimeInfo_.status = status;
760 }
761
762 /**
763 * @tc.name: GetNtpTimeMsWithNoPermission001
764 * @tc.desc: test GetNtpTimeMs with no permission.
765 * @tc.type: FUNC
766 */
767 HWTEST_F(TimeServiceTimeTest, GetNtpTimeMsWithNoPermission001, TestSize.Level0)
768 {
769 DeletePermission();
770 int64_t time = 0;
771 auto res = TimeSystemAbility::GetInstance()->GetNtpTimeMs(time);
772 EXPECT_EQ(res, E_TIME_NOT_SYSTEM_APP);
773 }
774
775 /**
776 * @tc.name: GetRealTimeMsWithNoPermission001
777 * @tc.desc: test GetRealTimeMs with no permission.
778 * @tc.type: FUNC
779 */
780 HWTEST_F(TimeServiceTimeTest, GetRealTimeMsWithNoPermission001, TestSize.Level0)
781 {
782 DeletePermission();
783 int64_t time = 0;
784 auto res = TimeSystemAbility::GetInstance()->GetRealTimeMs(time);
785 EXPECT_EQ(res, E_TIME_NOT_SYSTEM_APP);
786 }
787 } // namespace