From 30562817e0b087b02e1d1ebe34c569e4b28ea470 Mon Sep 17 00:00:00 2001 From: gaozhichao Date: Fri, 15 Aug 2025 09:52:17 +0800 Subject: [PATCH 1/4] =?UTF-8?q?[add]=20NTP=E6=9C=8D=E5=8A=A1=E5=99=A8?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=97=B6=E9=97=B4=E5=8A=A0=E5=9B=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaozhichao --- services/time/include/ntp_trusted_time.h | 7 +- services/time/src/ntp_trusted_time.cpp | 108 ++++++++-- services/time/src/ntp_update_time.cpp | 4 + .../src/time_service_time_test.cpp | 187 ++++++++++++++++++ 4 files changed, 287 insertions(+), 19 deletions(-) diff --git a/services/time/include/ntp_trusted_time.h b/services/time/include/ntp_trusted_time.h index 56661134..c13c90ed 100644 --- a/services/time/include/ntp_trusted_time.h +++ b/services/time/include/ntp_trusted_time.h @@ -30,6 +30,7 @@ public: int64_t CurrentTimeMillis(); int64_t ElapsedRealtimeMillis(); std::chrono::steady_clock::time_point GetBootTimeNs(); + bool FindBestTimeResult(); class TimeResult : std::enable_shared_from_this { public: TimeResult(); @@ -37,8 +38,8 @@ public: ~TimeResult(); int64_t GetTimeMillis(); int64_t GetElapsedRealtimeMillis(); - int64_t CurrentTimeMillis(); - int64_t GetAgeMillis(); + int64_t CurrentTimeMillis(int64_t bootTime); + int64_t GetAgeMillis(int64_t bootTime); void Clear(); private: @@ -48,9 +49,11 @@ public: int64_t mElapsedRealtimeMillis; int64_t mCertaintyMillis; }; + bool IsTimeResultTrusted(std::shared_ptr timeResult); private: std::shared_ptr mTimeResult {}; + std::vector> TimeResultCandidates_ {}; static std::mutex mTimeResultMutex_; }; } // namespace MiscServices diff --git a/services/time/src/ntp_trusted_time.cpp b/services/time/src/ntp_trusted_time.cpp index edd931c4..e83ee46e 100644 --- a/services/time/src/ntp_trusted_time.cpp +++ b/services/time/src/ntp_trusted_time.cpp @@ -26,6 +26,9 @@ constexpr int64_t TIME_RESULT_UNINITED = -1; constexpr int64_t HALF = 2; constexpr int NANO_TO_SECOND = 1000000000; constexpr int64_t ONE_DAY = 86400000; +// RTC has 1.7s gap one day +constexpr int64_t ONE_DAY_KERNEL_GAP = 1700; +constexpr int64_t TRUSTED_NTP_TIME_GAP = 20; } // namespace std::mutex NtpTrustedTime::mTimeResultMutex_; @@ -41,19 +44,92 @@ bool NtpTrustedTime::ForceRefresh(const std::string &ntpServer) TIME_HILOGD(TIME_MODULE_SERVICE, "start"); SNTPClient client; if (client.RequestTime(ntpServer)) { + auto timeResult = std::make_shared(client.getNtpTime(), client.getNtpTimeReference(), + client.getRoundTripTime() / HALF); std::lock_guard lock(mTimeResultMutex_); - if (mTimeResult != nullptr) { - mTimeResult->Clear(); + if (IsTimeResultTrusted(timeResult)) { + return true; } - int64_t ntpCertainty = client.getRoundTripTime() / HALF; - mTimeResult = std::make_shared(client.getNtpTime(), client.getNtpTimeReference(), ntpCertainty); - TIME_HILOGD(TIME_MODULE_SERVICE, "Get Ntp time result"); - return true; } TIME_HILOGD(TIME_MODULE_SERVICE, "false end"); return false; } +// needs to acquire the lock `mTimeResultMutex_` before calling this method +bool NtpTrustedTime::IsTimeResultTrusted(std::shared_ptr timeResult) +{ + // system has not got ntp time, push into candidate list + if (mTimeResult == nullptr) { + TIME_HILOGW(TIME_MODULE_SERVICE, "mTimeResult is nullptr"); + TimeResultCandidates_.push_back(timeResult); + return false; + } + // mTimeResult is invaild, push into candidate list + auto oldNtpTime = mTimeResult->CurrentTimeMillis(timeResult->GetElapsedRealtimeMillis()); + if (oldNtpTime == TIME_RESULT_UNINITED) { + TIME_HILOGW(TIME_MODULE_SERVICE, "mTimeResult time is invaild"); + TimeResultCandidates_.push_back(timeResult); + return false; + } + // mTimeResult is beyond max value of kernel gap, this server is untrusted + auto newNtpTime = timeResult->GetTimeMillis(); + if (std::abs(newNtpTime - oldNtpTime) > ONE_DAY_KERNEL_GAP) { + TIME_HILOGE(TIME_MODULE_SERVICE, "NTP server is untrusted"); + TimeResultCandidates_.push_back(timeResult); + return false; + } + // cause refresh time success, old value is invaild + TimeResultCandidates_.clear(); + mTimeResult = timeResult; + return true; +} + +// needs to acquire the lock `mTimeResultMutex_` before calling this method +bool NtpTrustedTime::FindBestTimeResult() +{ + if (TimeResultCandidates_.size() == 0) { + return false; + } + std::vector sortedTimes; + int64_t bootTime; + int res = TimeUtils::GetBootTimeMs(bootTime); + if (res != E_TIME_OK) { + return false; + } + // calculate value with same boottime + for (size_t i = 0; i < TimeResultCandidates_.size(); i++) { + auto result = TimeResultCandidates_[i]; + auto ntpTime = result->CurrentTimeMillis(bootTime); + sortedTimes.push_back(ntpTime); + } + + std::sort(sortedTimes.begin(), sortedTimes.end()); + + int32_t maxVoteCount = 0; + int64_t maxVoteTime = 0; + for (size_t i = 0; i < sortedTimes.size(); ++i) { + int64_t candidateValue = sortedTimes[i]; + int32_t voteCount = 0; + + auto lower = std::lower_bound(sortedTimes.begin(), sortedTimes.end(), candidateValue - TRUSTED_NTP_TIME_GAP); + auto upper = std::upper_bound(sortedTimes.begin(), sortedTimes.end(), candidateValue + TRUSTED_NTP_TIME_GAP); + + voteCount = std::distance(lower, upper); + if (voteCount > maxVoteCount) { + maxVoteCount = voteCount; + maxVoteTime = candidateValue; + } + } + + TimeResultCandidates_.clear(); + if (maxVoteCount == 1) { + return false; + } else { + mTimeResult = std::make_shared(maxVoteTime, bootTime, 0); + return true; + } +} + int64_t NtpTrustedTime::CurrentTimeMillis() { TIME_HILOGD(TIME_MODULE_SERVICE, "start"); @@ -63,7 +139,12 @@ int64_t NtpTrustedTime::CurrentTimeMillis() return TIME_RESULT_UNINITED; } TIME_HILOGD(TIME_MODULE_SERVICE, "end"); - return mTimeResult->CurrentTimeMillis(); + int64_t bootTime = 0; + int res = TimeUtils::GetBootTimeMs(bootTime); + if (res != E_TIME_OK) { + return TIME_RESULT_UNINITED; + } + return mTimeResult->CurrentTimeMillis(bootTime); } int64_t NtpTrustedTime::ElapsedRealtimeMillis() @@ -113,28 +194,21 @@ int64_t NtpTrustedTime::TimeResult::GetElapsedRealtimeMillis() return mElapsedRealtimeMillis; } -int64_t NtpTrustedTime::TimeResult::CurrentTimeMillis() +int64_t NtpTrustedTime::TimeResult::CurrentTimeMillis(int64_t bootTime) { if (mTimeMillis == 0 || mElapsedRealtimeMillis == 0) { TIME_HILOGD(TIME_MODULE_SERVICE, "Missing authoritative time source"); return TIME_RESULT_UNINITED; } - int64_t bootTime = 0; - int res = TimeUtils::GetBootTimeMs(bootTime); - if (res != E_TIME_OK) { - return TIME_RESULT_UNINITED; - } if (bootTime - mElapsedRealtimeMillis > ONE_DAY) { Clear(); return TIME_RESULT_UNINITED; } - return mTimeMillis + GetAgeMillis(); + return mTimeMillis + GetAgeMillis(bootTime); } -int64_t NtpTrustedTime::TimeResult::GetAgeMillis() +int64_t NtpTrustedTime::TimeResult::GetAgeMillis(int64_t bootTime) { - int64_t bootTime = 0; - TimeUtils::GetBootTimeMs(bootTime); return bootTime - this->mElapsedRealtimeMillis; } diff --git a/services/time/src/ntp_update_time.cpp b/services/time/src/ntp_update_time.cpp index f5f2cd63..76c84106 100644 --- a/services/time/src/ntp_update_time.cpp +++ b/services/time/src/ntp_update_time.cpp @@ -147,6 +147,7 @@ bool NtpUpdateTime::IsInUpdateInterval() return false; } +// needs to acquire the lock `requestMutex_` before calling this method NtpRefreshCode NtpUpdateTime::GetNtpTimeInner() { if (IsInUpdateInterval()) { @@ -163,6 +164,9 @@ NtpRefreshCode NtpUpdateTime::GetNtpTimeInner() return REFRESH_SUCCESS; } } + if (NtpTrustedTime::GetInstance().FindBestTimeResult()) { + return REFRESH_SUCCESS; + } } return REFRESH_FAILED; } diff --git a/test/unittest/service_test/src/time_service_time_test.cpp b/test/unittest/service_test/src/time_service_time_test.cpp index 9be85083..db02d646 100644 --- a/test/unittest/service_test/src/time_service_time_test.cpp +++ b/test/unittest/service_test/src/time_service_time_test.cpp @@ -58,6 +58,12 @@ const std::string AUTO_TIME_STATUS_ON = "ON"; const uint64_t TIMER_ID = 88888; constexpr int64_t MINUTE_TO_MILLISECOND = 60000; constexpr char BYTE_SNTP_MESSAGE = 0xD8; +constexpr int64_t ONE_DAY_KERNEL_GAP = 1700; +constexpr int64_t TRUSTED_NTP_TIME_GAP = 10; +constexpr int64_t ONE_DAY = 86400000; +constexpr int64_t ONE_HOUR = 3600000; +constexpr int64_t TWO_SECOND = 2000; +constexpr int64_t ONE_SECOND = 1000; static HapPolicyParams g_policyA = { .apl = APL_SYSTEM_CORE, @@ -678,6 +684,187 @@ HWTEST_F(TimeServiceTimeTest, NtpTrustedTime004, TestSize.Level0) EXPECT_EQ(res, 0); } +/** +* @tc.name: NtpTrustedTime005 +* @tc.desc: test func IsTimeResultTrusted. +* @tc.type: FUNC +*/ +HWTEST_F(TimeServiceTimeTest, NtpTrustedTime005, TestSize.Level0) +{ + int64_t wallTime = 0; + TimeUtils::GetWallTimeMs(wallTime); + int64_t bootTime = 0; + TimeUtils::GetBootTimeMs(bootTime); + // test mTimeResult is nullptr + std::shared_ptr ntpTrustedTime = std::make_shared(); + ntpTrustedTime->mTimeResult = nullptr; + auto timeResult = std::make_shared(wallTime, bootTime, 0); + bool ret = true; + ret = ntpTrustedTime->IsTimeResultTrusted(timeResult); + EXPECT_EQ(ret, false); + EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 1); + + // test mTimeResult is unavailable due to time out + auto wallTime1 = wallTime - 2 * ONE_DAY; + auto bootTime1 = bootTime - 2 * ONE_DAY; + ntpTrustedTime->mTimeResult = std::make_shared(wallTime1, bootTime1, 0); + ret = true; + ret = ntpTrustedTime->IsTimeResultTrusted(timeResult); + EXPECT_EQ(ret, false); + EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 2); + + // test new result Within the margin of error + auto wallTime2 = wallTime - ONE_HOUR - ONE_DAY_KERNEL_GAP; + auto bootTime2 = bootTime - ONE_HOUR; + ntpTrustedTime->mTimeResult = std::make_shared(wallTime2, bootTime2, 0); + ret = false; + ret = ntpTrustedTime->IsTimeResultTrusted(timeResult); + EXPECT_EQ(ret, true); + EXPECT_EQ(ntpTrustedTime->mTimeResult->GetTimeMillis(), wallTime); + EXPECT_EQ(ntpTrustedTime->mTimeResult->GetElapsedRealtimeMillis(), bootTime); + EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 0); + + auto wallTime3 = wallTime - ONE_HOUR + ONE_DAY_KERNEL_GAP; + auto bootTime3 = bootTime - ONE_HOUR; + ntpTrustedTime->mTimeResult = std::make_shared(wallTime3, bootTime3, 0); + ret = false; + ret = ntpTrustedTime->IsTimeResultTrusted(timeResult); + EXPECT_EQ(ret, true); + EXPECT_EQ(ntpTrustedTime->mTimeResult->GetTimeMillis(), wallTime); + EXPECT_EQ(ntpTrustedTime->mTimeResult->GetElapsedRealtimeMillis(), bootTime); + EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 0); +} + +/** +* @tc.name: NtpTrustedTime006 +* @tc.desc: test func IsTimeResultTrusted. +* @tc.type: FUNC +*/ +HWTEST_F(TimeServiceTimeTest, NtpTrustedTime006, TestSize.Level0) +{ + int64_t wallTime = 0; + TimeUtils::GetWallTimeMs(wallTime); + int64_t bootTime = 0; + TimeUtils::GetBootTimeMs(bootTime); + // test mTimeResult is nullptr + std::shared_ptr ntpTrustedTime = std::make_shared(); + auto timeResult = std::make_shared(wallTime, bootTime, 0); + + // test new result out of the margin of error + auto wallTime1 = wallTime - ONE_HOUR - ONE_DAY_KERNEL_GAP - TWO_SECOND; + auto bootTime1 = bootTime - ONE_HOUR; + ntpTrustedTime->mTimeResult = std::make_shared(wallTime1, bootTime1, 0); + auto ret = true; + ret = ntpTrustedTime->IsTimeResultTrusted(timeResult); + EXPECT_EQ(ret, false); + EXPECT_EQ(ntpTrustedTime->mTimeResult->GetTimeMillis(), wallTime1); + EXPECT_EQ(ntpTrustedTime->mTimeResult->GetElapsedRealtimeMillis(), bootTime1); + EXPECT_EQ(ret, false); + + auto wallTime2 = wallTime - ONE_HOUR + ONE_DAY_KERNEL_GAP + TWO_SECOND; + auto bootTime2 = bootTime - ONE_HOUR; + ntpTrustedTime->mTimeResult = std::make_shared(wallTime2, bootTime2, 0); + ret = true; + ret = ntpTrustedTime->IsTimeResultTrusted(timeResult); + EXPECT_EQ(ret, false); + EXPECT_EQ(ntpTrustedTime->mTimeResult->GetTimeMillis(), wallTime2); + EXPECT_EQ(ntpTrustedTime->mTimeResult->GetElapsedRealtimeMillis(), bootTime2); +} + +/** +* @tc.name: NtpTrustedTime007 +* @tc.desc: test func FindBestTimeResult. +* @tc.type: FUNC +*/ +HWTEST_F(TimeServiceTimeTest, NtpTrustedTime007, TestSize.Level0) +{ + int64_t wallTime = 0; + TimeUtils::GetWallTimeMs(wallTime); + int64_t bootTime = 0; + TimeUtils::GetBootTimeMs(bootTime); + std::shared_ptr ntpTrustedTime = std::make_shared(); + + // three time is similar, vote for 3 candidate + auto timeResult1 = std::make_shared(wallTime, bootTime, 0); + auto wallTime2 = wallTime + ONE_SECOND + TRUSTED_NTP_TIME_GAP; + auto bootTime2 = bootTime + ONE_SECOND; + auto timeResult2 = std::make_shared(wallTime2, bootTime2, 0); + auto wallTime3 = wallTime + TWO_SECOND + TRUSTED_NTP_TIME_GAP / 2; + auto bootTime3 = bootTime + TWO_SECOND; + auto timeResult3 = std::make_shared(wallTime3, bootTime3, 0); + bool ret = true; + ntpTrustedTime->TimeResultCandidates_.push_back(timeResult1); + ntpTrustedTime->TimeResultCandidates_.push_back(timeResult2); + ntpTrustedTime->TimeResultCandidates_.push_back(timeResult3); + ret = ntpTrustedTime->FindBestTimeResult(); + EXPECT_EQ(ret, true); + EXPECT_EQ(ntpTrustedTime->mTimeResult->GetTimeMillis()- ntpTrustedTime->mTimeResult->GetElapsedRealtimeMillis(), + wallTime - bootTime); + EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 0); +} + +/** +* @tc.name: NtpTrustedTime008 +* @tc.desc: test func FindBestTimeResult return false. +* @tc.type: FUNC +*/ +HWTEST_F(TimeServiceTimeTest, NtpTrustedTime008, TestSize.Level0) +{ + int64_t wallTime = 0; + TimeUtils::GetWallTimeMs(wallTime); + int64_t bootTime = 0; + TimeUtils::GetBootTimeMs(bootTime); + std::shared_ptr ntpTrustedTime = std::make_shared(); + + // two time is similar, vote for 3 candidate + auto timeResult1 = std::make_shared(wallTime, bootTime, 0); + auto wallTime2 = wallTime + TWO_SECOND; + auto bootTime2 = bootTime + ONE_SECOND; + auto timeResult2 = std::make_shared(wallTime2, bootTime2, 0); + auto wallTime3 = wallTime + TWO_SECOND; + auto bootTime3 = bootTime + TWO_SECOND; + auto timeResult3 = std::make_shared(wallTime3, bootTime3, 0); + ntpTrustedTime->TimeResultCandidates_.push_back(timeResult1); + ntpTrustedTime->TimeResultCandidates_.push_back(timeResult2); + ntpTrustedTime->TimeResultCandidates_.push_back(timeResult3); + bool ret = false; + ret = ntpTrustedTime->FindBestTimeResult(); + EXPECT_EQ(ret, true); + EXPECT_EQ(ntpTrustedTime->mTimeResult->GetTimeMillis() - ntpTrustedTime->mTimeResult->GetElapsedRealtimeMillis(), + wallTime - bootTime); + EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 0); +} + +/** +* @tc.name: NtpTrustedTime009 +* @tc.desc: test func FindBestTimeResult return false. +* @tc.type: FUNC +*/ +HWTEST_F(TimeServiceTimeTest, NtpTrustedTime009, TestSize.Level0) +{ + int64_t wallTime = 0; + TimeUtils::GetWallTimeMs(wallTime); + int64_t bootTime = 0; + TimeUtils::GetBootTimeMs(bootTime); + std::shared_ptr ntpTrustedTime = std::make_shared(); + + // test 3 timeResult got different time + auto timeResult1 = std::make_shared(wallTime, bootTime, 0); + auto wallTime2 = wallTime + TWO_SECOND; + auto bootTime2 = bootTime + ONE_SECOND; + auto timeResult2 = std::make_shared(wallTime2, bootTime2, 0); + auto wallTime3 = wallTime + TWO_SECOND + TRUSTED_NTP_TIME_GAP * 2; + auto bootTime3 = bootTime + TWO_SECOND; + auto timeResult3 = std::make_shared(wallTime3, bootTime3, 0); + ntpTrustedTime->TimeResultCandidates_.push_back(timeResult1); + ntpTrustedTime->TimeResultCandidates_.push_back(timeResult2); + ntpTrustedTime->TimeResultCandidates_.push_back(timeResult3); + bool ret = true; + ret = ntpTrustedTime->FindBestTimeResult(); + EXPECT_EQ(ret, false); + EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 0); +} + /** * @tc.name: TimeTick001 * @tc.desc: Check RefreshNextTriggerTime. -- Gitee From b06534bb65aaf0f1caad0eee97e77707a92c2dde Mon Sep 17 00:00:00 2001 From: gaozhichao Date: Sat, 30 Aug 2025 16:06:42 +0800 Subject: [PATCH 2/4] =?UTF-8?q?[fix]=20=E4=BF=AE=E6=94=B9=E9=83=A8?= =?UTF-8?q?=E5=88=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaozhichao --- services/time/include/ntp_trusted_time.h | 1 + services/time/src/ntp_trusted_time.cpp | 64 ++++++++++--------- .../src/time_service_time_test.cpp | 18 +++--- 3 files changed, 44 insertions(+), 39 deletions(-) diff --git a/services/time/include/ntp_trusted_time.h b/services/time/include/ntp_trusted_time.h index c13c90ed..9b203922 100644 --- a/services/time/include/ntp_trusted_time.h +++ b/services/time/include/ntp_trusted_time.h @@ -50,6 +50,7 @@ public: int64_t mCertaintyMillis; }; bool IsTimeResultTrusted(std::shared_ptr timeResult); + int32_t GetSameTimeResultCount(std::shared_ptr candidateTimeResult); private: std::shared_ptr mTimeResult {}; diff --git a/services/time/src/ntp_trusted_time.cpp b/services/time/src/ntp_trusted_time.cpp index e83ee46e..f4334079 100644 --- a/services/time/src/ntp_trusted_time.cpp +++ b/services/time/src/ntp_trusted_time.cpp @@ -27,8 +27,8 @@ constexpr int64_t HALF = 2; constexpr int NANO_TO_SECOND = 1000000000; constexpr int64_t ONE_DAY = 86400000; // RTC has 1.7s gap one day -constexpr int64_t ONE_DAY_KERNEL_GAP = 1700; -constexpr int64_t TRUSTED_NTP_TIME_GAP = 20; +constexpr int64_t MAX_TIME_DRIFT_IN_ONE_DAY = 2000; +constexpr int64_t MAX_TIME_TOLERANCE_BETWEEN_NTP_SERVERS = 100; } // namespace std::mutex NtpTrustedTime::mTimeResultMutex_; @@ -60,7 +60,6 @@ bool NtpTrustedTime::IsTimeResultTrusted(std::shared_ptr timeResult) { // system has not got ntp time, push into candidate list if (mTimeResult == nullptr) { - TIME_HILOGW(TIME_MODULE_SERVICE, "mTimeResult is nullptr"); TimeResultCandidates_.push_back(timeResult); return false; } @@ -73,8 +72,9 @@ bool NtpTrustedTime::IsTimeResultTrusted(std::shared_ptr timeResult) } // mTimeResult is beyond max value of kernel gap, this server is untrusted auto newNtpTime = timeResult->GetTimeMillis(); - if (std::abs(newNtpTime - oldNtpTime) > ONE_DAY_KERNEL_GAP) { - TIME_HILOGE(TIME_MODULE_SERVICE, "NTP server is untrusted"); + if (std::abs(newNtpTime - oldNtpTime) > MAX_TIME_DRIFT_IN_ONE_DAY) { + TIME_HILOGW(TIME_MODULE_SERVICE, "NTP server is untrusted old:%{public}" PRId64 " new:%{public}" PRId64 "", + oldNtpTime, newNtpTime); TimeResultCandidates_.push_back(timeResult); return false; } @@ -84,48 +84,52 @@ bool NtpTrustedTime::IsTimeResultTrusted(std::shared_ptr timeResult) return true; } +int32_t NtpTrustedTime::GetSameTimeResultCount(std::shared_ptr candidateTimeResult) +{ + auto candidateBootTime = candidateTimeResult->GetElapsedRealtimeMillis(); + auto candidateRealTime = candidateTimeResult->GetTimeMillis(); + int count = 0; + for (size_t i = 0; i < TimeResultCandidates_.size(); i++) { + auto compareTime = TimeResultCandidates_[i]->CurrentTimeMillis(candidateBootTime); + if (std::abs(compareTime - candidateRealTime) < MAX_TIME_TOLERANCE_BETWEEN_NTP_SERVERS) { + count += 1; + } + } + return count; +} + // needs to acquire the lock `mTimeResultMutex_` before calling this method bool NtpTrustedTime::FindBestTimeResult() { - if (TimeResultCandidates_.size() == 0) { + if (TimeResultCandidates_.size() == 0 || TimeResultCandidates_.size() == 1) { + TIME_HILOGW(TIME_MODULE_SERVICE, "no or one candidate"); return false; } - std::vector sortedTimes; + int64_t bootTime; int res = TimeUtils::GetBootTimeMs(bootTime); if (res != E_TIME_OK) { + TIME_HILOGW(TIME_MODULE_SERVICE, "getboottime failed"); return false; } - // calculate value with same boottime - for (size_t i = 0; i < TimeResultCandidates_.size(); i++) { - auto result = TimeResultCandidates_[i]; - auto ntpTime = result->CurrentTimeMillis(bootTime); - sortedTimes.push_back(ntpTime); - } - - std::sort(sortedTimes.begin(), sortedTimes.end()); - int32_t maxVoteCount = 0; - int64_t maxVoteTime = 0; - for (size_t i = 0; i < sortedTimes.size(); ++i) { - int64_t candidateValue = sortedTimes[i]; - int32_t voteCount = 0; - - auto lower = std::lower_bound(sortedTimes.begin(), sortedTimes.end(), candidateValue - TRUSTED_NTP_TIME_GAP); - auto upper = std::upper_bound(sortedTimes.begin(), sortedTimes.end(), candidateValue + TRUSTED_NTP_TIME_GAP); - - voteCount = std::distance(lower, upper); - if (voteCount > maxVoteCount) { - maxVoteCount = voteCount; - maxVoteTime = candidateValue; + int32_t maxVotedTimeResultCount = 0; + std::shared_ptr maxVotedTimeResult; + for (size_t i = 0; i < TimeResultCandidates_.size(); i++) { + auto timeResult = TimeResultCandidates_[i]; + int32_t count = GetSameTimeResultCount(TimeResultCandidates_[i]); + if (count > maxVotedTimeResultCount) { + maxVotedTimeResultCount = count; + maxVotedTimeResult = timeResult; } } TimeResultCandidates_.clear(); - if (maxVoteCount == 1) { + if (maxVotedTimeResultCount == 1) { + TIME_HILOGW(TIME_MODULE_SERVICE, "no best candidate"); return false; } else { - mTimeResult = std::make_shared(maxVoteTime, bootTime, 0); + mTimeResult = maxVotedTimeResult; return true; } } diff --git a/test/unittest/service_test/src/time_service_time_test.cpp b/test/unittest/service_test/src/time_service_time_test.cpp index db02d646..cb8be277 100644 --- a/test/unittest/service_test/src/time_service_time_test.cpp +++ b/test/unittest/service_test/src/time_service_time_test.cpp @@ -58,8 +58,8 @@ const std::string AUTO_TIME_STATUS_ON = "ON"; const uint64_t TIMER_ID = 88888; constexpr int64_t MINUTE_TO_MILLISECOND = 60000; constexpr char BYTE_SNTP_MESSAGE = 0xD8; -constexpr int64_t ONE_DAY_KERNEL_GAP = 1700; -constexpr int64_t TRUSTED_NTP_TIME_GAP = 10; +constexpr int64_t MAX_TIME_DRIFT_IN_ONE_DAY = 2000; +constexpr int64_t MAX_TIME_TOLERANCE_BETWEEN_NTP_SERVERS = 100; constexpr int64_t ONE_DAY = 86400000; constexpr int64_t ONE_HOUR = 3600000; constexpr int64_t TWO_SECOND = 2000; @@ -714,7 +714,7 @@ HWTEST_F(TimeServiceTimeTest, NtpTrustedTime005, TestSize.Level0) EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 2); // test new result Within the margin of error - auto wallTime2 = wallTime - ONE_HOUR - ONE_DAY_KERNEL_GAP; + auto wallTime2 = wallTime - ONE_HOUR - MAX_TIME_DRIFT_IN_ONE_DAY; auto bootTime2 = bootTime - ONE_HOUR; ntpTrustedTime->mTimeResult = std::make_shared(wallTime2, bootTime2, 0); ret = false; @@ -724,7 +724,7 @@ HWTEST_F(TimeServiceTimeTest, NtpTrustedTime005, TestSize.Level0) EXPECT_EQ(ntpTrustedTime->mTimeResult->GetElapsedRealtimeMillis(), bootTime); EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 0); - auto wallTime3 = wallTime - ONE_HOUR + ONE_DAY_KERNEL_GAP; + auto wallTime3 = wallTime - ONE_HOUR + MAX_TIME_DRIFT_IN_ONE_DAY; auto bootTime3 = bootTime - ONE_HOUR; ntpTrustedTime->mTimeResult = std::make_shared(wallTime3, bootTime3, 0); ret = false; @@ -751,7 +751,7 @@ HWTEST_F(TimeServiceTimeTest, NtpTrustedTime006, TestSize.Level0) auto timeResult = std::make_shared(wallTime, bootTime, 0); // test new result out of the margin of error - auto wallTime1 = wallTime - ONE_HOUR - ONE_DAY_KERNEL_GAP - TWO_SECOND; + auto wallTime1 = wallTime - ONE_HOUR - MAX_TIME_DRIFT_IN_ONE_DAY - TWO_SECOND; auto bootTime1 = bootTime - ONE_HOUR; ntpTrustedTime->mTimeResult = std::make_shared(wallTime1, bootTime1, 0); auto ret = true; @@ -761,7 +761,7 @@ HWTEST_F(TimeServiceTimeTest, NtpTrustedTime006, TestSize.Level0) EXPECT_EQ(ntpTrustedTime->mTimeResult->GetElapsedRealtimeMillis(), bootTime1); EXPECT_EQ(ret, false); - auto wallTime2 = wallTime - ONE_HOUR + ONE_DAY_KERNEL_GAP + TWO_SECOND; + auto wallTime2 = wallTime - ONE_HOUR + MAX_TIME_DRIFT_IN_ONE_DAY + TWO_SECOND; auto bootTime2 = bootTime - ONE_HOUR; ntpTrustedTime->mTimeResult = std::make_shared(wallTime2, bootTime2, 0); ret = true; @@ -786,10 +786,10 @@ HWTEST_F(TimeServiceTimeTest, NtpTrustedTime007, TestSize.Level0) // three time is similar, vote for 3 candidate auto timeResult1 = std::make_shared(wallTime, bootTime, 0); - auto wallTime2 = wallTime + ONE_SECOND + TRUSTED_NTP_TIME_GAP; + auto wallTime2 = wallTime + ONE_SECOND + MAX_TIME_TOLERANCE_BETWEEN_NTP_SERVERS; auto bootTime2 = bootTime + ONE_SECOND; auto timeResult2 = std::make_shared(wallTime2, bootTime2, 0); - auto wallTime3 = wallTime + TWO_SECOND + TRUSTED_NTP_TIME_GAP / 2; + auto wallTime3 = wallTime + TWO_SECOND + MAX_TIME_TOLERANCE_BETWEEN_NTP_SERVERS / 2; auto bootTime3 = bootTime + TWO_SECOND; auto timeResult3 = std::make_shared(wallTime3, bootTime3, 0); bool ret = true; @@ -853,7 +853,7 @@ HWTEST_F(TimeServiceTimeTest, NtpTrustedTime009, TestSize.Level0) auto wallTime2 = wallTime + TWO_SECOND; auto bootTime2 = bootTime + ONE_SECOND; auto timeResult2 = std::make_shared(wallTime2, bootTime2, 0); - auto wallTime3 = wallTime + TWO_SECOND + TRUSTED_NTP_TIME_GAP * 2; + auto wallTime3 = wallTime + TWO_SECOND + MAX_TIME_TOLERANCE_BETWEEN_NTP_SERVERS * 2; auto bootTime3 = bootTime + TWO_SECOND; auto timeResult3 = std::make_shared(wallTime3, bootTime3, 0); ntpTrustedTime->TimeResultCandidates_.push_back(timeResult1); -- Gitee From c974500490794981116382c93e4d8701eababa8c Mon Sep 17 00:00:00 2001 From: gaozhichao Date: Mon, 1 Sep 2025 11:10:01 +0800 Subject: [PATCH 3/4] =?UTF-8?q?[fix]=20=E4=BF=AE=E6=94=B9=E5=8F=AA?= =?UTF-8?q?=E6=9C=89=E4=B8=80=E4=B8=AA=E7=BB=93=E6=9E=9C=E7=9A=84=E5=9C=BA?= =?UTF-8?q?=E6=99=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaozhichao --- services/time/include/ntp_trusted_time.h | 2 +- services/time/src/ntp_trusted_time.cpp | 31 ++++++------- services/time/src/ntp_update_time.cpp | 7 +-- .../src/time_service_time_test.cpp | 45 ++++++++++++++----- 4 files changed, 56 insertions(+), 29 deletions(-) diff --git a/services/time/include/ntp_trusted_time.h b/services/time/include/ntp_trusted_time.h index 9b203922..2088ad86 100644 --- a/services/time/include/ntp_trusted_time.h +++ b/services/time/include/ntp_trusted_time.h @@ -30,7 +30,7 @@ public: int64_t CurrentTimeMillis(); int64_t ElapsedRealtimeMillis(); std::chrono::steady_clock::time_point GetBootTimeNs(); - bool FindBestTimeResult(); + bool FindBestTimeResult(size_t ntpListSize); class TimeResult : std::enable_shared_from_this { public: TimeResult(); diff --git a/services/time/src/ntp_trusted_time.cpp b/services/time/src/ntp_trusted_time.cpp index f4334079..34ca7a95 100644 --- a/services/time/src/ntp_trusted_time.cpp +++ b/services/time/src/ntp_trusted_time.cpp @@ -99,37 +99,38 @@ int32_t NtpTrustedTime::GetSameTimeResultCount(std::shared_ptr candi } // needs to acquire the lock `mTimeResultMutex_` before calling this method -bool NtpTrustedTime::FindBestTimeResult() +bool NtpTrustedTime::FindBestTimeResult(size_t ntpListSize) { - if (TimeResultCandidates_.size() == 0 || TimeResultCandidates_.size() == 1) { - TIME_HILOGW(TIME_MODULE_SERVICE, "no or one candidate"); - return false; + // if only one server, use this result as best result + if (TimeResultCandidates_.size() == 1 && ntpListSize == 1) { + mTimeResult = TimeResultCandidates_[0]; + TimeResultCandidates_.clear(); + return true; } - int64_t bootTime; - int res = TimeUtils::GetBootTimeMs(bootTime); - if (res != E_TIME_OK) { - TIME_HILOGW(TIME_MODULE_SERVICE, "getboottime failed"); + if (TimeResultCandidates_.size() == 0 || TimeResultCandidates_.size() == 1) { + TIME_HILOGW(TIME_MODULE_SERVICE, "no or one candidate"); + TimeResultCandidates_.clear(); return false; } - int32_t maxVotedTimeResultCount = 0; - std::shared_ptr maxVotedTimeResult; + int32_t mostVotedTimeResultCount = 0; + std::shared_ptr mostVotedTimeResult; for (size_t i = 0; i < TimeResultCandidates_.size(); i++) { auto timeResult = TimeResultCandidates_[i]; int32_t count = GetSameTimeResultCount(TimeResultCandidates_[i]); - if (count > maxVotedTimeResultCount) { - maxVotedTimeResultCount = count; - maxVotedTimeResult = timeResult; + if (count > mostVotedTimeResultCount) { + mostVotedTimeResultCount = count; + mostVotedTimeResult = timeResult; } } TimeResultCandidates_.clear(); - if (maxVotedTimeResultCount == 1) { + if (mostVotedTimeResultCount == 1) { TIME_HILOGW(TIME_MODULE_SERVICE, "no best candidate"); return false; } else { - mTimeResult = maxVotedTimeResult; + mTimeResult = mostVotedTimeResult; return true; } } diff --git a/services/time/src/ntp_update_time.cpp b/services/time/src/ntp_update_time.cpp index 76c84106..424f6c70 100644 --- a/services/time/src/ntp_update_time.cpp +++ b/services/time/src/ntp_update_time.cpp @@ -158,13 +158,14 @@ NtpRefreshCode NtpUpdateTime::GetNtpTimeInner() std::vector ntpList = SplitNtpAddrs(autoTimeInfo_.ntpServer); ntpSpecList.insert(ntpSpecList.end(), ntpList.begin(), ntpList.end()); for (int i = 0; i < RETRY_TIMES; i++) { - for (size_t j = 0; j < ntpSpecList.size(); j++) { - TIME_HILOGI(TIME_MODULE_SERVICE, "ntpServer is : %{public}s", ntpSpecList[j].c_str()); + auto ntpSpecListSize = ntpSpecList.size(); + for (size_t j = 0; j < ntpSpecListSize; j++) { + TIME_HILOGI(TIME_MODULE_SERVICE, "ntpServer is:%{public}s", ntpSpecList[j].c_str()); if (NtpTrustedTime::GetInstance().ForceRefresh(ntpSpecList[j])) { return REFRESH_SUCCESS; } } - if (NtpTrustedTime::GetInstance().FindBestTimeResult()) { + if (NtpTrustedTime::GetInstance().FindBestTimeResult(ntpSpecListSize)) { return REFRESH_SUCCESS; } } diff --git a/test/unittest/service_test/src/time_service_time_test.cpp b/test/unittest/service_test/src/time_service_time_test.cpp index cb8be277..ab540065 100644 --- a/test/unittest/service_test/src/time_service_time_test.cpp +++ b/test/unittest/service_test/src/time_service_time_test.cpp @@ -773,7 +773,7 @@ HWTEST_F(TimeServiceTimeTest, NtpTrustedTime006, TestSize.Level0) /** * @tc.name: NtpTrustedTime007 -* @tc.desc: test func FindBestTimeResult. +* @tc.desc: test func FindBestTimeResult with 3 same candidates, return true. * @tc.type: FUNC */ HWTEST_F(TimeServiceTimeTest, NtpTrustedTime007, TestSize.Level0) @@ -796,16 +796,14 @@ HWTEST_F(TimeServiceTimeTest, NtpTrustedTime007, TestSize.Level0) ntpTrustedTime->TimeResultCandidates_.push_back(timeResult1); ntpTrustedTime->TimeResultCandidates_.push_back(timeResult2); ntpTrustedTime->TimeResultCandidates_.push_back(timeResult3); - ret = ntpTrustedTime->FindBestTimeResult(); + ret = ntpTrustedTime->FindBestTimeResult(5); EXPECT_EQ(ret, true); - EXPECT_EQ(ntpTrustedTime->mTimeResult->GetTimeMillis()- ntpTrustedTime->mTimeResult->GetElapsedRealtimeMillis(), - wallTime - bootTime); EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 0); } /** * @tc.name: NtpTrustedTime008 -* @tc.desc: test func FindBestTimeResult return false. +* @tc.desc: test func FindBestTimeResult with 2 same candidates, return true. * @tc.type: FUNC */ HWTEST_F(TimeServiceTimeTest, NtpTrustedTime008, TestSize.Level0) @@ -828,16 +826,14 @@ HWTEST_F(TimeServiceTimeTest, NtpTrustedTime008, TestSize.Level0) ntpTrustedTime->TimeResultCandidates_.push_back(timeResult2); ntpTrustedTime->TimeResultCandidates_.push_back(timeResult3); bool ret = false; - ret = ntpTrustedTime->FindBestTimeResult(); + ret = ntpTrustedTime->FindBestTimeResult(5); EXPECT_EQ(ret, true); - EXPECT_EQ(ntpTrustedTime->mTimeResult->GetTimeMillis() - ntpTrustedTime->mTimeResult->GetElapsedRealtimeMillis(), - wallTime - bootTime); EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 0); } /** * @tc.name: NtpTrustedTime009 -* @tc.desc: test func FindBestTimeResult return false. +* @tc.desc: test func FindBestTimeResult with no same candidates, return false. * @tc.type: FUNC */ HWTEST_F(TimeServiceTimeTest, NtpTrustedTime009, TestSize.Level0) @@ -860,7 +856,36 @@ HWTEST_F(TimeServiceTimeTest, NtpTrustedTime009, TestSize.Level0) ntpTrustedTime->TimeResultCandidates_.push_back(timeResult2); ntpTrustedTime->TimeResultCandidates_.push_back(timeResult3); bool ret = true; - ret = ntpTrustedTime->FindBestTimeResult(); + ret = ntpTrustedTime->FindBestTimeResult(5); + EXPECT_EQ(ret, false); + EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 0); +} + +/** +* @tc.name: NtpTrustedTime010 +* @tc.desc: test func FindBestTimeResult with 1 candidates. +* @tc.type: FUNC +*/ +HWTEST_F(TimeServiceTimeTest, NtpTrustedTime010, TestSize.Level0) +{ + int64_t wallTime = 0; + TimeUtils::GetWallTimeMs(wallTime); + int64_t bootTime = 0; + TimeUtils::GetBootTimeMs(bootTime); + std::shared_ptr ntpTrustedTime = std::make_shared(); + + bool ret = true; + ret = ntpTrustedTime->FindBestTimeResult(3); + EXPECT_EQ(ret, false); + EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 0); + + auto timeResult = std::make_shared(wallTime, bootTime, 0); + ntpTrustedTime->TimeResultCandidates_.push_back(timeResult); + ret = ntpTrustedTime->FindBestTimeResult(1); + EXPECT_EQ(ret, true); + EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 0); + + ret = ntpTrustedTime->FindBestTimeResult(3); EXPECT_EQ(ret, false); EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 0); } -- Gitee From d56c85ef38b0a3ae0d4ecd3680f88bd3c5588dc9 Mon Sep 17 00:00:00 2001 From: gaozhichao Date: Tue, 2 Sep 2025 16:47:38 +0800 Subject: [PATCH 4/4] =?UTF-8?q?[fix]=20=E4=BF=AE=E6=94=B9=E6=B8=85?= =?UTF-8?q?=E7=A9=BA=E5=88=97=E8=A1=A8=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaozhichao --- services/time/include/ntp_trusted_time.h | 3 ++- services/time/src/ntp_trusted_time.cpp | 15 ++++++--------- services/time/src/ntp_update_time.cpp | 6 +++--- .../src/time_service_time_test.cpp | 18 ++++++------------ 4 files changed, 17 insertions(+), 25 deletions(-) diff --git a/services/time/include/ntp_trusted_time.h b/services/time/include/ntp_trusted_time.h index 2088ad86..c04b5eba 100644 --- a/services/time/include/ntp_trusted_time.h +++ b/services/time/include/ntp_trusted_time.h @@ -30,7 +30,8 @@ public: int64_t CurrentTimeMillis(); int64_t ElapsedRealtimeMillis(); std::chrono::steady_clock::time_point GetBootTimeNs(); - bool FindBestTimeResult(size_t ntpListSize); + bool FindBestTimeResult(); + void ClearTimeResultCandidates(); class TimeResult : std::enable_shared_from_this { public: TimeResult(); diff --git a/services/time/src/ntp_trusted_time.cpp b/services/time/src/ntp_trusted_time.cpp index 34ca7a95..985234d0 100644 --- a/services/time/src/ntp_trusted_time.cpp +++ b/services/time/src/ntp_trusted_time.cpp @@ -99,18 +99,10 @@ int32_t NtpTrustedTime::GetSameTimeResultCount(std::shared_ptr candi } // needs to acquire the lock `mTimeResultMutex_` before calling this method -bool NtpTrustedTime::FindBestTimeResult(size_t ntpListSize) +bool NtpTrustedTime::FindBestTimeResult() { - // if only one server, use this result as best result - if (TimeResultCandidates_.size() == 1 && ntpListSize == 1) { - mTimeResult = TimeResultCandidates_[0]; - TimeResultCandidates_.clear(); - return true; - } - if (TimeResultCandidates_.size() == 0 || TimeResultCandidates_.size() == 1) { TIME_HILOGW(TIME_MODULE_SERVICE, "no or one candidate"); - TimeResultCandidates_.clear(); return false; } @@ -135,6 +127,11 @@ bool NtpTrustedTime::FindBestTimeResult(size_t ntpListSize) } } +void NtpTrustedTime::ClearTimeResultCandidates() +{ + TimeResultCandidates_.clear(); +} + int64_t NtpTrustedTime::CurrentTimeMillis() { TIME_HILOGD(TIME_MODULE_SERVICE, "start"); diff --git a/services/time/src/ntp_update_time.cpp b/services/time/src/ntp_update_time.cpp index 424f6c70..d72c4670 100644 --- a/services/time/src/ntp_update_time.cpp +++ b/services/time/src/ntp_update_time.cpp @@ -158,17 +158,17 @@ NtpRefreshCode NtpUpdateTime::GetNtpTimeInner() std::vector ntpList = SplitNtpAddrs(autoTimeInfo_.ntpServer); ntpSpecList.insert(ntpSpecList.end(), ntpList.begin(), ntpList.end()); for (int i = 0; i < RETRY_TIMES; i++) { - auto ntpSpecListSize = ntpSpecList.size(); - for (size_t j = 0; j < ntpSpecListSize; j++) { + for (size_t j = 0; j < ntpSpecList.size(); j++) { TIME_HILOGI(TIME_MODULE_SERVICE, "ntpServer is:%{public}s", ntpSpecList[j].c_str()); if (NtpTrustedTime::GetInstance().ForceRefresh(ntpSpecList[j])) { return REFRESH_SUCCESS; } } - if (NtpTrustedTime::GetInstance().FindBestTimeResult(ntpSpecListSize)) { + if (NtpTrustedTime::GetInstance().FindBestTimeResult()) { return REFRESH_SUCCESS; } } + NtpTrustedTime::GetInstance().ClearTimeResultCandidates(); return REFRESH_FAILED; } diff --git a/test/unittest/service_test/src/time_service_time_test.cpp b/test/unittest/service_test/src/time_service_time_test.cpp index ab540065..094b612f 100644 --- a/test/unittest/service_test/src/time_service_time_test.cpp +++ b/test/unittest/service_test/src/time_service_time_test.cpp @@ -796,7 +796,7 @@ HWTEST_F(TimeServiceTimeTest, NtpTrustedTime007, TestSize.Level0) ntpTrustedTime->TimeResultCandidates_.push_back(timeResult1); ntpTrustedTime->TimeResultCandidates_.push_back(timeResult2); ntpTrustedTime->TimeResultCandidates_.push_back(timeResult3); - ret = ntpTrustedTime->FindBestTimeResult(5); + ret = ntpTrustedTime->FindBestTimeResult(); EXPECT_EQ(ret, true); EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 0); } @@ -826,7 +826,7 @@ HWTEST_F(TimeServiceTimeTest, NtpTrustedTime008, TestSize.Level0) ntpTrustedTime->TimeResultCandidates_.push_back(timeResult2); ntpTrustedTime->TimeResultCandidates_.push_back(timeResult3); bool ret = false; - ret = ntpTrustedTime->FindBestTimeResult(5); + ret = ntpTrustedTime->FindBestTimeResult(); EXPECT_EQ(ret, true); EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 0); } @@ -856,14 +856,14 @@ HWTEST_F(TimeServiceTimeTest, NtpTrustedTime009, TestSize.Level0) ntpTrustedTime->TimeResultCandidates_.push_back(timeResult2); ntpTrustedTime->TimeResultCandidates_.push_back(timeResult3); bool ret = true; - ret = ntpTrustedTime->FindBestTimeResult(5); + ret = ntpTrustedTime->FindBestTimeResult(); EXPECT_EQ(ret, false); EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 0); } /** * @tc.name: NtpTrustedTime010 -* @tc.desc: test func FindBestTimeResult with 1 candidates. +* @tc.desc: test func FindBestTimeResult with 0 or 1 candidates. * @tc.type: FUNC */ HWTEST_F(TimeServiceTimeTest, NtpTrustedTime010, TestSize.Level0) @@ -875,19 +875,13 @@ HWTEST_F(TimeServiceTimeTest, NtpTrustedTime010, TestSize.Level0) std::shared_ptr ntpTrustedTime = std::make_shared(); bool ret = true; - ret = ntpTrustedTime->FindBestTimeResult(3); + ret = ntpTrustedTime->FindBestTimeResult(); EXPECT_EQ(ret, false); - EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 0); auto timeResult = std::make_shared(wallTime, bootTime, 0); ntpTrustedTime->TimeResultCandidates_.push_back(timeResult); - ret = ntpTrustedTime->FindBestTimeResult(1); - EXPECT_EQ(ret, true); - EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 0); - - ret = ntpTrustedTime->FindBestTimeResult(3); + ret = ntpTrustedTime->FindBestTimeResult(); EXPECT_EQ(ret, false); - EXPECT_EQ(ntpTrustedTime->TimeResultCandidates_.size(), 0); } /** -- Gitee