diff --git a/services/samgr/native/source/samgr_time_handler.cpp b/services/samgr/native/source/samgr_time_handler.cpp index d0b97552d9c7e5442d3caa4b6d32b6f701dbafe3..db5ed262c4551b4556236e72e6017ca1c06dc752 100644 --- a/services/samgr/native/source/samgr_time_handler.cpp +++ b/services/samgr/native/source/samgr_time_handler.cpp @@ -109,6 +109,10 @@ int SamgrTimeHandler::CreateAndRetry() bool SamgrTimeHandler::PostTask(TaskType func, uint64_t delayTime) { + if (!func) { + HILOGE("SamgrTimeHandler PostTask failed, func is null"); + return false; + } HILOGI("SamgrTimeHandler postTask start: %{public}" PRId64 "s", delayTime); int timerfd = CreateAndRetry(); if (timerfd == -1) { diff --git a/services/samgr/native/test/unittest/src/device_timed_collect_test.cpp b/services/samgr/native/test/unittest/src/device_timed_collect_test.cpp index cd3831498602b72b12713daca6f4d461852cfd85..20f84a4acf788c8c5dfb62f4baa7ea99e8466e0b 100644 --- a/services/samgr/native/test/unittest/src/device_timed_collect_test.cpp +++ b/services/samgr/native/test/unittest/src/device_timed_collect_test.cpp @@ -21,6 +21,7 @@ #define private public #include "device_status_collect_manager.h" #include "device_timed_collect.h" +#include "samgr_time_handler.h" #ifdef PREFERENCES_ENABLE #include "preferences_errno.h" #include "preferences_helper.h" @@ -983,4 +984,35 @@ HWTEST_F(DeviceTimedCollectTest, TestFFRTHandlerPostTask003, TestSize.Level3) EXPECT_FALSE(collectHandler->PostTask(func, "test", 1)); DTEST_LOG << " TestFFRTHandlerPostTask003 end" << std::endl; } + +/** + * @tc.name: SamgrTimeHandlerTest001 + * @tc.desc: test SamgrTimeHandler PostTask, with func is null + * @tc.type: FUNC + * @tc.require: I7VZ98 + */ + +HWTEST_F(DeviceTimedCollectTest, SamgrTimeHandlerTest001, TestSize.Level3) +{ + DTEST_LOG << " SamgrTimeHandlerTest001 begin" << std::endl; + std::function timedTask = nullptr; + bool bRet = SamgrTimeHandler::GetInstance()->PostTask(timedTask, 1); + EXPECT_FALSE(bRet); + DTEST_LOG << " SamgrTimeHandlerTest001 end" << std::endl; +} + +/** + * @tc.name: SamgrTimeHandlerTest002 + * @tc.desc: test SamgrTimeHandler PostTask, with func is not null + * @tc.type: FUNC + * @tc.require: I7VZ98 + */ +HWTEST_F(DeviceTimedCollectTest, SamgrTimeHandlerTest002, TestSize.Level3) +{ + DTEST_LOG << " SamgrTimeHandlerTest002 begin" << std::endl; + std::function timedTask = [] () {}; + bool bRet = SamgrTimeHandler::GetInstance()->PostTask(timedTask, 1); + EXPECT_TRUE(bRet); + DTEST_LOG << " SamgrTimeHandlerTest002 end" << std::endl; +} } \ No newline at end of file