1 /*
2  * Copyright (C) 2023-2023 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 
16 #define private public
17 #include <gtest/gtest.h>
18 #include <unistd.h>
19 #include "timer_manager.h"
20 #include "timer_proxy.h"
21 #include "time_common.h"
22 
23 namespace OHOS {
24 namespace MiscServices {
25 using namespace testing::ext;
26 using namespace std::chrono;
27 
28 namespace {
29 constexpr uint64_t NANO_TO_MILESECOND = 100000;
30 constexpr int BLOCK_TEST_TIME = 100000;
31 const uint64_t TIMER_ID = 88887;
32 const int UID = 999996;
33 const int PID = 999997;
34 const int UID_PROXY_MASK = 32;
35 }
36 TimerManager* timerManagerHandler_ = nullptr;
37 
38 class TimeProxyTest : public testing::Test {
39 public:
40     static void SetUpTestCase(void);
41 
42     static void TearDownTestCase(void);
43 
44     void SetUp();
45 
46     void TearDown();
47 };
48 
SetUpTestCase(void)49 void TimeProxyTest::SetUpTestCase(void)
50 {}
51 
TearDownTestCase(void)52 void TimeProxyTest::TearDownTestCase(void)
53 {}
54 
SetUp(void)55 void TimeProxyTest::SetUp(void)
56 {
57     TIME_HILOGI(TIME_MODULE_SERVICE, "start SetUp.");
58     timerManagerHandler_ = TimerManager::GetInstance();
59     EXPECT_NE(timerManagerHandler_, nullptr);
60     TIME_HILOGI(TIME_MODULE_SERVICE, "end SetUp.");
61     usleep(BLOCK_TEST_TIME);
62 }
63 
TearDown(void)64 void TimeProxyTest::TearDown(void)
65 {
66     TIME_HILOGI(TIME_MODULE_SERVICE, "start TearDown.");
67     timerManagerHandler_ = nullptr;
68     TIME_HILOGI(TIME_MODULE_SERVICE, "end TearDown.");
69 }
70 
71 /**
72 * @tc.name: UidTimerMap001
73 * @tc.desc: 启动timer时uid timer map数据更新测试
74 * @tc.type: FUNC
75 */
76 HWTEST_F(TimeProxyTest, UidTimerMap001, TestSize.Level1)
77 {
78     /* 创建一个timer,可以创建成功 */
79     TimerPara paras;
80     paras.timerType = 2;
81     paras.windowLength = -1;
82     paras.interval = 0;
83     paras.flag = 0;
84     auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
85     int32_t uid = 2000;
86     int pid = 1000;
87     uint64_t timerId = 0;
__anon4322b06b0202(const uint64_t) 88     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
89                                                     wantAgent, uid, pid, timerId, NOT_STORE);
90     EXPECT_EQ(ret, TimeError::E_TIME_OK);
91     usleep(BLOCK_TEST_TIME);
92 
93     /* 启动一个timer, 可以启动成功,可以记录到uidTimerMap_中 */
94     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
95     uint64_t triggerTime = 10000000 + nowElapsed;
96     ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
97     EXPECT_EQ(ret, TimeError::E_TIME_OK);
98     usleep(BLOCK_TEST_TIME);
99     EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)1);
100     auto itUidTimerMap = TimerProxy::GetInstance().uidTimersMap_.find(uid);
101     EXPECT_NE(itUidTimerMap, TimerProxy::GetInstance().uidTimersMap_.end());
102     EXPECT_EQ(itUidTimerMap->second.size(), (const unsigned int)1);
103     auto itTimerId = itUidTimerMap->second.find(timerId);
104     EXPECT_NE(itTimerId, itUidTimerMap->second.end());
105     EXPECT_NE(itTimerId->second, nullptr);
106 
107     /* 清理uidTimerMap_,可以清理成功 */
108     TimerProxy::GetInstance().uidTimersMap_.clear();
109     EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)0);
110     timerManagerHandler_->DestroyTimer(timerId);
111     usleep(BLOCK_TEST_TIME);
112 }
113 
114 /**
115 * @tc.name: UidTimerMap002
116 * @tc.desc: 停止timer时uid timer map数据更新测试
117 * @tc.type: FUNC
118 */
119 HWTEST_F(TimeProxyTest, UidTimerMap002, TestSize.Level1)
120 {
121     /* 创建一个timer,可以创建成功 */
122     TimerPara paras;
123     paras.timerType = 2;
124     paras.windowLength = -1;
125     paras.interval = 0;
126     paras.flag = 0;
127     auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
128     int32_t uid = 2000;
129     int pid = 1000;
130     uint64_t timerId = 0;
__anon4322b06b0302(const uint64_t) 131     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
132                                                     wantAgent, uid, pid, timerId, NOT_STORE);
133     EXPECT_EQ(ret, TimeError::E_TIME_OK);
134     usleep(BLOCK_TEST_TIME);
135 
136     /* 启动一个timer, 可以启动成功,可以记录到uidTimerMap_中 */
137     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
138     uint64_t triggerTime = 10000000 + nowElapsed;
139     ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
140     EXPECT_EQ(ret, TimeError::E_TIME_OK);
141     usleep(BLOCK_TEST_TIME);
142     EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)1);
143 
144     /* 停止一个timer,可以停止成功,可以从uidTimerMap_中删除 */
145     ret = timerManagerHandler_->StopTimerInner(timerId, true);
146     EXPECT_EQ(ret, TimeError::E_TIME_OK);
147     usleep(BLOCK_TEST_TIME);
148     EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)0);
149     timerManagerHandler_->DestroyTimer(timerId);
150 }
151 
152 /**
153 * @tc.name: UidTimerMap003
154 * @tc.desc: 触发timer时uid timer map数据更新测试
155 * @tc.type: FUNC
156 */
157 HWTEST_F(TimeProxyTest, UidTimerMap003, TestSize.Level1)
158 {
159     /* 创建一个timer,可以创建成功 */
160     TimerPara paras;
161     paras.timerType = 2;
162     paras.windowLength = -1;
163     paras.interval = 0;
164     paras.flag = 0;
165     auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
166     int32_t uid = 2000;
167     int pid = 1000;
168     uint64_t timerId = 0;
__anon4322b06b0402(const uint64_t) 169     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
170                                                     wantAgent, uid, pid, timerId, NOT_STORE);
171 
172     EXPECT_EQ(ret, TimeError::E_TIME_OK);
173     usleep(BLOCK_TEST_TIME);
174 
175     /* 启动一个timer, 可以启动成功,可以记录到uidTimerMap_中 */
176     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
177     uint64_t triggerTime = 10000000 + nowElapsed;
178     ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
179     EXPECT_EQ(ret, TimeError::E_TIME_OK);
180     usleep(BLOCK_TEST_TIME);
181     EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)1);
182 
183     /* 触发一个timer,可以触发成功,可以从uidTimerMap_中删除 */
184     std::vector<std::shared_ptr<TimerInfo>> triggerList;
185     std::shared_ptr<Batch> batch = timerManagerHandler_->alarmBatches_.at(0);
186     std::chrono::steady_clock::time_point tpRpoch(nanoseconds(1000000000));
187     batch->start_ = tpRpoch;
188     auto retTrigger = timerManagerHandler_->TriggerTimersLocked(triggerList, timerManagerHandler_->GetBootTimeNs());
189     EXPECT_EQ(retTrigger, true);
190     usleep(BLOCK_TEST_TIME);
191     EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)0);
192     timerManagerHandler_->DestroyTimer(timerId);
193 }
194 
195 /**
196 * @tc.name: PidTimerMap001
197 * @tc.desc: 启动timer时pid timer map数据更新测试
198 * @tc.type: FUNC
199 */
200 HWTEST_F(TimeProxyTest, PidTimerMap001, TestSize.Level1)
201 {
202     /* 创建一个timer,可以创建成功 */
203     TimerPara paras;
204     paras.timerType = 2;
205     paras.windowLength = -1;
206     paras.interval = 0;
207     paras.flag = 0;
208     auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
209     int32_t uid = 2000;
210     int pid = 1001;
211     uint64_t timerId = 0;
__anon4322b06b0502(const uint64_t) 212     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
213                                                     wantAgent, uid, pid, timerId, NOT_STORE);
214     EXPECT_EQ(ret, TimeError::E_TIME_OK);
215     usleep(BLOCK_TEST_TIME);
216 
217     /* 启动一个timer, 可以启动成功,可以记录到PidTimerMap_中 */
218     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
219     uint64_t triggerTime = 10000000 + nowElapsed;
220     ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
221     EXPECT_EQ(ret, TimeError::E_TIME_OK);
222     usleep(BLOCK_TEST_TIME);
223     EXPECT_EQ(TimerProxy::GetInstance().pidTimersMap_.size(), (const unsigned int)1);
224     auto itPidTimerMap = TimerProxy::GetInstance().pidTimersMap_.find(pid);
225     EXPECT_NE(itPidTimerMap, TimerProxy::GetInstance().pidTimersMap_.end());
226     EXPECT_EQ(itPidTimerMap->second.size(), (const unsigned int)1);
227     auto itTimerId = itPidTimerMap->second.find(timerId);
228     EXPECT_NE(itTimerId, itPidTimerMap->second.end());
229     EXPECT_NE(itTimerId->second, nullptr);
230 
231     /* 清理pidTimerMap_,可以清理成功 */
232     TimerProxy::GetInstance().pidTimersMap_.clear();
233     EXPECT_EQ(TimerProxy::GetInstance().pidTimersMap_.size(), (const unsigned int)0);
234     timerManagerHandler_->DestroyTimer(timerId);
235     usleep(BLOCK_TEST_TIME);
236 }
237 
238 /**
239 * @tc.name: PidTimerMap002
240 * @tc.desc: 停止timer时pid timer map数据更新测试
241 * @tc.type: FUNC
242 */
243 HWTEST_F(TimeProxyTest, PidTimerMap002, TestSize.Level1)
244 {
245     /* 创建一个timer,可以创建成功 */
246     TimerPara paras;
247     paras.timerType = 2;
248     paras.windowLength = -1;
249     paras.interval = 0;
250     paras.flag = 0;
251     auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
252     int32_t uid = 2000;
253     int pid = 1000;
254     uint64_t timerId = 0;
255 
256     /* 清理pidTimersMap_,保证测试前pidTimersMap_内无其他测试中曾记录的pid影响 */
257     TimerProxy::GetInstance().pidTimersMap_.clear();
258 
__anon4322b06b0602(const uint64_t) 259     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
260                                                     wantAgent, uid, pid, timerId, NOT_STORE);
261     EXPECT_EQ(ret, TimeError::E_TIME_OK);
262     usleep(BLOCK_TEST_TIME);
263 
264     /* 启动一个timer, 可以启动成功,可以记录到pidTimerMap_中 */
265     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
266     uint64_t triggerTime = 10000000 + nowElapsed;
267     ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
268     EXPECT_EQ(ret, TimeError::E_TIME_OK);
269     usleep(BLOCK_TEST_TIME);
270     EXPECT_EQ(TimerProxy::GetInstance().pidTimersMap_.size(), (const unsigned int)1);
271 
272     /* 停止一个timer,可以停止成功,可以从pidTimerMap_中删除 */
273     ret = timerManagerHandler_->StopTimerInner(timerId, true);
274     EXPECT_EQ(ret, TimeError::E_TIME_OK);
275     usleep(BLOCK_TEST_TIME);
276     EXPECT_EQ(TimerProxy::GetInstance().pidTimersMap_.size(), (const unsigned int)0);
277     timerManagerHandler_->DestroyTimer(timerId);
278 }
279 
280 /**
281 * @tc.name: PidTimerMap003
282 * @tc.desc: 触发timer时pid timer map数据更新测试
283 * @tc.type: FUNC
284 */
285 HWTEST_F(TimeProxyTest, PidTimerMap003, TestSize.Level1)
286 {
287     /* 创建一个timer,可以创建成功 */
288     TimerPara paras;
289     paras.timerType = 2;
290     paras.windowLength = -1;
291     paras.interval = 0;
292     paras.flag = 0;
293     auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
294     int32_t uid = 2000;
295     int pid = 1002;
296     uint64_t timerId = 0;
__anon4322b06b0702(const uint64_t) 297     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
298                                                     wantAgent, uid, pid, timerId, NOT_STORE);
299     EXPECT_EQ(ret, TimeError::E_TIME_OK);
300     usleep(BLOCK_TEST_TIME);
301 
302     /* 启动一个timer, 可以启动成功,可以记录到pidTimerMap_中 */
303     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
304     uint64_t triggerTime = 10000000 + nowElapsed;
305     ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
306     EXPECT_EQ(ret, TimeError::E_TIME_OK);
307     usleep(BLOCK_TEST_TIME);
308     EXPECT_EQ(TimerProxy::GetInstance().pidTimersMap_.size(), (const unsigned int)1);
309 
310     /* 触发一个timer,可以触发成功,可以从pidTimerMap_中删除 */
311     std::vector<std::shared_ptr<TimerInfo>> triggerList;
312     std::shared_ptr<Batch> batch = timerManagerHandler_->alarmBatches_.at(0);
313     std::chrono::steady_clock::time_point tpRpoch(nanoseconds(1000000000));
314     batch->start_ = tpRpoch;
315     auto retTrigger = timerManagerHandler_->TriggerTimersLocked(triggerList, timerManagerHandler_->GetBootTimeNs());
316     EXPECT_EQ(retTrigger, true);
317     usleep(BLOCK_TEST_TIME);
318     EXPECT_EQ(TimerProxy::GetInstance().pidTimersMap_.size(), (const unsigned int)0);
319     timerManagerHandler_->DestroyTimer(timerId);
320 }
321 
322 /**
323 * @tc.name: ProxyTimer001
324 * @tc.desc: 代理解代理基本功能测试
325 * @tc.type: FUNC
326 */
327 HWTEST_F(TimeProxyTest, ProxyTimer001, TestSize.Level1)
328 {
329     /* 代理一个timer,可以代理成功,可以记录到proxyUid_中 */
330     int32_t uid = 1000;
331     bool isProxy = true;
332     bool needRetrigger = true;
333     bool ret = timerManagerHandler_->ProxyTimer(uid, isProxy, needRetrigger);
334     EXPECT_TRUE(ret);
335     usleep(BLOCK_TEST_TIME);
336     EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)1);
337     auto it = TimerProxy::GetInstance().proxyUids_.find(uid);
338     EXPECT_NE(it, TimerProxy::GetInstance().proxyUids_.end());
339     EXPECT_EQ(it->second.size(), (const unsigned int)0);
340 
341     /* 解代理一个timer,可以解代理成功,可以从proxyUid_中删除 */
342     isProxy = false;
343     ret = timerManagerHandler_->ProxyTimer(uid, isProxy, needRetrigger);
344     EXPECT_TRUE(ret);
345     usleep(BLOCK_TEST_TIME);
346     EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)0);
347 }
348 
349 /**
350 * @tc.name: ProxyTimer002
351 * @tc.desc: 代理解代理时proxy timer map数据更新测试
352 * @tc.type: FUNC
353 */
354 HWTEST_F(TimeProxyTest, ProxyTimer002, TestSize.Level1)
355 {
356     /* 创建一个timer,可以创建成功 */
357     TimerPara paras;
358     paras.timerType = 2;
359     paras.windowLength = -1;
360     paras.interval = 0;
361     paras.flag = 0;
362     auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
363     int32_t uid = 2000;
364     int pid = 1000;
365     uint64_t timerId = 0;
__anon4322b06b0802(const uint64_t) 366     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
367                                                     wantAgent, uid, pid, timerId, NOT_STORE);
368     EXPECT_EQ(ret, TimeError::E_TIME_OK);
369     usleep(BLOCK_TEST_TIME);
370 
371     /* 启动一个timer, 可以启动成功,可以记录到uidTimerMap_中 */
372     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
373     uint64_t triggerTime = 10000000 + nowElapsed;
374     ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
375     EXPECT_EQ(ret, TimeError::E_TIME_OK);
376     usleep(BLOCK_TEST_TIME);
377     EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)1);
378     std::chrono::steady_clock::time_point time = TimerProxy::GetInstance().uidTimersMap_[uid][timerId]->whenElapsed;
379 
380     /* 代理一个timer,可以代理成功,可以记录到proxyUid_中 */
381     bool retProxy = timerManagerHandler_->ProxyTimer(uid, true, true);
382     EXPECT_TRUE(retProxy);
383     usleep(BLOCK_TEST_TIME);
384     EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)1);
385     auto it = TimerProxy::GetInstance().proxyUids_.find(uid);
386     EXPECT_NE(it, TimerProxy::GetInstance().proxyUids_.end());
387     EXPECT_EQ(it->second.size(), (const unsigned int)1);
388 
389     /* uidTimerMap_中的触发时间成功更新,proxyUid_中可以记录老的触发时间 */
390     it = TimerProxy::GetInstance().proxyUids_.find(uid);
391     auto it2 = it->second.find(timerId);
392     EXPECT_NE(it2, it->second.end());
393     EXPECT_EQ(it2->second, time);
394 
395     auto it3 = TimerProxy::GetInstance().uidTimersMap_.find(uid);
396     EXPECT_NE(it3, TimerProxy::GetInstance().uidTimersMap_.end());
397     auto it4 = it3->second.find(timerId);
398     EXPECT_NE(it4, it3->second.end());
399     EXPECT_NE(it4->second->whenElapsed, time);
400 
401     /* 解代理一个timer,可以解代理成功,可以更新proxyUid_表 */
402     ret = timerManagerHandler_->ProxyTimer(uid, false, true);
403     EXPECT_TRUE(retProxy);
404     usleep(BLOCK_TEST_TIME);
405     EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)0);
406 
407     /* uidTimerMap_中的触发时间被恢复回老的触发时间 */
408     auto it5 = TimerProxy::GetInstance().uidTimersMap_.find(uid);
409     EXPECT_NE(it5, TimerProxy::GetInstance().uidTimersMap_.end());
410     auto it6 = it5->second.find(timerId);
411     EXPECT_NE(it6, it5->second.end());
412     EXPECT_EQ(it6->second->whenElapsed, time);
413     timerManagerHandler_->DestroyTimer(timerId);
414     usleep(BLOCK_TEST_TIME);
415 }
416 
417 /**
418 * @tc.name: ProxyTimer003
419 * @tc.desc: reset all proxy测试
420 * @tc.type: FUNC
421 */
422 HWTEST_F(TimeProxyTest, ProxyTimer003, TestSize.Level1)
423 {
424     /* 代理三个timer,可以代理成功,可以记录到proxyUid_中 */
425     int32_t uid = 2000;
426     bool retProxy = timerManagerHandler_->ProxyTimer(uid, true, true);
427     EXPECT_TRUE(retProxy);
428     usleep(BLOCK_TEST_TIME);
429     EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)1);
430     auto it = TimerProxy::GetInstance().proxyUids_.find(uid);
431     EXPECT_NE(it, TimerProxy::GetInstance().proxyUids_.end());
432     EXPECT_EQ(it->second.size(), (const unsigned int)0);
433 
434     uid = 3000;
435     retProxy = timerManagerHandler_->ProxyTimer(uid, true, true);
436     EXPECT_TRUE(retProxy);
437     usleep(BLOCK_TEST_TIME);
438     EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)2);
439     it = TimerProxy::GetInstance().proxyUids_.find(uid);
440     EXPECT_NE(it, TimerProxy::GetInstance().proxyUids_.end());
441     EXPECT_EQ(it->second.size(), (const unsigned int)0);
442 
443     uid = 4000;
444     retProxy = timerManagerHandler_->ProxyTimer(uid, true, true);
445     EXPECT_TRUE(retProxy);
446     usleep(BLOCK_TEST_TIME);
447     EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)3);
448     it = TimerProxy::GetInstance().proxyUids_.find(uid);
449     EXPECT_NE(it, TimerProxy::GetInstance().proxyUids_.end());
450     EXPECT_EQ(it->second.size(), (const unsigned int)0);
451 
452     /* 可以正常reset,且map会清空 */
453     retProxy = timerManagerHandler_->ResetAllProxy();
454     EXPECT_TRUE(retProxy);
455     EXPECT_TRUE(TimerProxy::GetInstance().proxyUids_.empty());
456 }
457 
458 /**
459 * @tc.name: AdjustTimer001
460 * @tc.desc: adjust timer test
461 * @tc.type: FUNC
462 */
463 HWTEST_F(TimeProxyTest, AdjustTimer001, TestSize.Level1)
464 {
465     /* The system timers can be aligned to a unified time and recorded in adjustTimers_. */
466     bool isAdjust = true;
467     uint32_t interval = 100;
468     bool ret = timerManagerHandler_->AdjustTimer(isAdjust, interval);
469     EXPECT_TRUE(ret);
470     usleep(BLOCK_TEST_TIME);
471     EXPECT_EQ(TimerProxy::GetInstance().adjustTimers_.size(), (const unsigned int)0);
472 
473     /* The unified heartbeat can be deleted successfully and deleted from adjustTimers_. */
474     isAdjust = false;
475     interval = 0;
476     ret = timerManagerHandler_->AdjustTimer(isAdjust, interval);
477     EXPECT_TRUE(ret);
478     usleep(BLOCK_TEST_TIME);
479     EXPECT_EQ(TimerProxy::GetInstance().adjustTimers_.size(), (const unsigned int)0);
480 }
481 
482 /**
483 * @tc.name: AdjustTimer002
484 * @tc.desc: set timer exemption
485 * @tc.type: FUNC
486 */
487 HWTEST_F(TimeProxyTest, AdjustTimer002, TestSize.Level1)
488 {
489     /* Create a timer with windowLen set to 0. */
490     TimerPara paras{.timerType = 2, .windowLength = 0, .interval = 0, .flag = 0};
491     auto wantAgent = std::make_shared<OHOS::AbilityRuntime::WantAgent::WantAgent>();
492     int32_t uid = 2000;
493     int32_t pid = 1000;
494     uint64_t timerId = 0;
__anon4322b06b0902(const uint64_t) 495     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
496                                                      wantAgent, uid, pid, timerId, NOT_STORE);
497     EXPECT_EQ(ret, TimeError::E_TIME_OK);
498     usleep(BLOCK_TEST_TIME);
499 
500     /* Create a timer */
501     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
502     uint64_t triggerTime = 10000000 + nowElapsed;
503     ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
504     EXPECT_EQ(ret, TimeError::E_TIME_OK);
505     usleep(BLOCK_TEST_TIME);
506 
507     /* Exempt the timer of the app and update the record to adjustExemptionList_. */
508     std::unordered_set<std::string> nameArr{"time_service"};
509     timerManagerHandler_->SetTimerExemption(nameArr, true);
510     usleep(BLOCK_TEST_TIME);
511     EXPECT_NE(TimerProxy::GetInstance().adjustExemptionList_.size(), (const unsigned int)0);
512 
513     /* Unified heartbeat is triggered. The heartbeat of exempted applications is not unified. */
514     bool isAdjust = true;
515     uint32_t interval = 200;
516     bool adjustRet = timerManagerHandler_->AdjustTimer(isAdjust, interval);
517     EXPECT_TRUE(adjustRet);
518     usleep(BLOCK_TEST_TIME);
519     EXPECT_NE(TimerProxy::GetInstance().adjustTimers_.size(), (const unsigned int)0);
520     bool isExemption = true;
521     for (auto timer : TimerProxy::GetInstance().adjustTimers_) {
522         if (timer->bundleName == "time_service") {
523             isExemption = false;
524         }
525     }
526     EXPECT_TRUE(isExemption);
527 }
528 
529 /**
530 * @tc.name: PidProxyTimer001
531 * @tc.desc: 代理解代理基本功能测试
532 * @tc.type: FUNC
533 */
534 HWTEST_F(TimeProxyTest, PidProxyTimer001, TestSize.Level1)
535 {
536     /* 代理一个timer,可以代理成功,可以记录到proxyUid_中 */
537     int pid = 1003;
538     int uid = 2003;
539     std::set<int> pidList;
540     pidList.insert(pid);
541     bool isProxy = true;
542     bool needRetrigger = true;
543     bool ret = timerManagerHandler_->ProxyTimer(uid, pidList, isProxy, needRetrigger);
544     EXPECT_TRUE(ret);
545     usleep(BLOCK_TEST_TIME);
546     uint64_t key = (static_cast<uint64_t>(uid) << UID_PROXY_MASK) | static_cast<uint64_t>(pid);
547     EXPECT_EQ(TimerProxy::GetInstance().proxyPids_.size(), (const unsigned int)1);
548     auto it = TimerProxy::GetInstance().proxyPids_.find(key);
549     EXPECT_NE(it, TimerProxy::GetInstance().proxyPids_.end());
550     EXPECT_EQ(it->second.size(), (const unsigned int)0);
551 
552     /* 解代理一个timer,可以解代理成功,可以从proxyPid_中删除 */
553     isProxy = false;
554     ret = timerManagerHandler_->ProxyTimer(uid, pidList, isProxy, needRetrigger);
555     EXPECT_TRUE(ret);
556     usleep(BLOCK_TEST_TIME);
557     EXPECT_EQ(TimerProxy::GetInstance().proxyPids_.size(), (const unsigned int)0);
558 }
559 
560 /**
561 * @tc.name: PidProxyTimer002
562 * @tc.desc: 代理解代理时proxy timer map数据更新测试
563 * @tc.type: FUNC
564 */
565 HWTEST_F(TimeProxyTest, PidProxyTimer002, TestSize.Level1)
566 {
567     TimerPara paras;
568     paras.timerType = 2;
569     paras.windowLength = -1;
570     paras.interval = 0;
571     paras.flag = 0;
572     auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
573     int32_t uid = 2000;
574     int pid = 1004;
575     std::set<int> pidList;
576     pidList.insert(pid);
577     uint64_t timerId = 0;
578 
579     /* 清理pidTimersMap_,保证测试前pidTimersMap_内无其他测试中曾记录的pid影响 */
580     TimerProxy::GetInstance().pidTimersMap_.clear();
581 
__anon4322b06b0a02(const uint64_t) 582     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
583                                                     wantAgent, uid, pid, timerId, NOT_STORE);
584     EXPECT_EQ(ret, TimeError::E_TIME_OK);
585 
586     /* 启动一个timer, 可以启动成功,可以记录到pidTimerMap_中 */
587     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
588     uint64_t triggerTime = 10000000 + nowElapsed;
589     ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
590     EXPECT_EQ(ret, TimeError::E_TIME_OK);
591     usleep(BLOCK_TEST_TIME);
592     EXPECT_EQ(TimerProxy::GetInstance().pidTimersMap_.size(), (const unsigned int)1);
593     std::chrono::steady_clock::time_point time = TimerProxy::GetInstance().pidTimersMap_[pid][timerId]->whenElapsed;
594 
595     /* 代理一个timer,可以代理成功,可以记录到proxyPid_中 */
596     bool retProxy = timerManagerHandler_->ProxyTimer(uid, pidList, true, true);
597     EXPECT_TRUE(retProxy);
598     usleep(BLOCK_TEST_TIME);
599     EXPECT_EQ(TimerProxy::GetInstance().proxyPids_.size(), (const unsigned int)1);
600     uint64_t key = (static_cast<uint64_t>(uid) << UID_PROXY_MASK) | static_cast<uint64_t>(pid);
601     auto it = TimerProxy::GetInstance().proxyPids_.find(key);
602     EXPECT_NE(it, TimerProxy::GetInstance().proxyPids_.end());
603     EXPECT_EQ(it->second.size(), (const unsigned int)1);
604 
605     /* pidTimerMap_中的触发时间成功更新,proxyPid_中可以记录老的触发时间 */
606     auto it2 = it->second.find(timerId);
607     EXPECT_NE(it2, it->second.end());
608     EXPECT_EQ(it2->second, time);
609 
610     auto it3 = TimerProxy::GetInstance().pidTimersMap_.find(pid);
611     EXPECT_NE(it3, TimerProxy::GetInstance().pidTimersMap_.end());
612     auto it4 = it3->second.find(timerId);
613     EXPECT_NE(it4, it3->second.end());
614     EXPECT_NE(it4->second->whenElapsed, time);
615 
616     /* 解代理一个timer,可以解代理成功,可以更新proxyPid_表 */
617     ret = timerManagerHandler_->ProxyTimer(uid, pidList, false, true);
618     EXPECT_TRUE(retProxy);
619     usleep(BLOCK_TEST_TIME);
620     EXPECT_EQ(TimerProxy::GetInstance().proxyPids_.size(), (const unsigned int)0);
621 
622     /* pidTimerMap_中的触发时间被恢复回老的触发时间 */
623     auto it5 = TimerProxy::GetInstance().pidTimersMap_.find(pid);
624     EXPECT_NE(it5, TimerProxy::GetInstance().pidTimersMap_.end());
625     auto it6 = it5->second.find(timerId);
626     EXPECT_NE(it6, it5->second.end());
627     EXPECT_EQ(it6->second->whenElapsed, time);
628     timerManagerHandler_->DestroyTimer(timerId);
629 }
630 
631 /**
632 * @tc.name: PidProxyTimer003
633 * @tc.desc: reset all proxy测试
634 * @tc.type: FUNC
635 */
636 HWTEST_F(TimeProxyTest, PidProxyTimer003, TestSize.Level1)
637 {
638     int uid = 1000;
639     /* 代理三个timer,可以代理成功,可以记录到proxyPid_中 */
640     int pid1 = 2000;
641     std::set<int> pidList;
642     pidList.insert(pid1);
643 
644     int pid2 = 3000;
645     pidList.insert(pid2);
646 
647     int pid3 = 4000;
648     pidList.insert(pid3);
649 
650     bool retProxy = timerManagerHandler_->ProxyTimer(uid, pidList, true, true);
651     EXPECT_TRUE(retProxy);
652     usleep(BLOCK_TEST_TIME);
653     EXPECT_EQ(TimerProxy::GetInstance().proxyPids_.size(), (const unsigned int)3);
654     uint64_t key1 = static_cast<uint64_t>(uid) << UID_PROXY_MASK | static_cast<uint64_t>(pid1);
655     uint64_t key2 = static_cast<uint64_t>(uid) << UID_PROXY_MASK | static_cast<uint64_t>(pid2);
656     uint64_t key3 = static_cast<uint64_t>(uid) << UID_PROXY_MASK | static_cast<uint64_t>(pid3);
657     auto it = TimerProxy::GetInstance().proxyPids_.find(key1);
658     EXPECT_NE(it, TimerProxy::GetInstance().proxyPids_.end());
659     it = TimerProxy::GetInstance().proxyPids_.find(key2);
660     EXPECT_NE(it, TimerProxy::GetInstance().proxyPids_.end());
661     it = TimerProxy::GetInstance().proxyPids_.find(key3);
662     EXPECT_NE(it, TimerProxy::GetInstance().proxyPids_.end());
663     EXPECT_EQ(it->second.size(), (const unsigned int)0);
664 
665     /* 可以正常reset,且map会清空 */
666     retProxy = timerManagerHandler_->ResetAllProxy();
667     EXPECT_TRUE(retProxy);
668     EXPECT_TRUE(TimerProxy::GetInstance().proxyPids_.empty());
669 }
670 
671 /**
672 * @tc.name: PidProxyTimer004
673 * @tc.desc: test proxy of same pid but different uid.
674 * @tc.type: FUNC
675 */
676 HWTEST_F(TimeProxyTest, PidProxyTimer004, TestSize.Level1)
677 {
678     TimerPara paras;
679     paras.timerType = 2;
680     paras.windowLength = -1;
681     paras.interval = 0;
682     paras.flag = 0;
683     auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
684     int32_t uid1 = 2000;
685     int32_t uid2 = 2001;
686     int pid = 1000;
687     uint64_t timerId1 = 0;
688     uint64_t timerId2 = 0;
689 
690     TimerProxy::GetInstance().pidTimersMap_.clear();
691     TimerProxy::GetInstance().proxyPids_.clear();
692     /* create timer by uid1 */
__anon4322b06b0b02(const uint64_t) 693     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
694                                                     wantAgent, uid1, pid, timerId1, NOT_STORE);
695     EXPECT_EQ(ret, TimeError::E_TIME_OK);
696     usleep(BLOCK_TEST_TIME);
697     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
698     uint64_t triggerTime = 10000000 + nowElapsed;
699     ret = timerManagerHandler_->StartTimer(timerId1, triggerTime);
700     EXPECT_EQ(ret, TimeError::E_TIME_OK);
701     usleep(BLOCK_TEST_TIME);
702     EXPECT_EQ(TimerProxy::GetInstance().pidTimersMap_[pid].size(), (const unsigned int)1);
703 
704     /* create timer by uid2 */
__anon4322b06b0c02(const uint64_t) 705     ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
706                                                     wantAgent, uid2, pid, timerId2, NOT_STORE);
707     EXPECT_EQ(ret, TimeError::E_TIME_OK);
708     usleep(BLOCK_TEST_TIME);
709     nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
710     triggerTime = 10000000 + nowElapsed;
711     ret = timerManagerHandler_->StartTimer(timerId2, triggerTime);
712     EXPECT_EQ(ret, TimeError::E_TIME_OK);
713     usleep(BLOCK_TEST_TIME);
714     EXPECT_EQ(TimerProxy::GetInstance().pidTimersMap_[pid].size(), (const unsigned int)2);
715 
716     std::set<int> pidList;
717     pidList.insert(pid);
718     /* proxy uid1 expect proxyPids_ only has one element */
719     ret = timerManagerHandler_->ProxyTimer(uid1, pidList, true, true);
720     uint64_t key = (static_cast<uint64_t>(uid1) << UID_PROXY_MASK) | static_cast<uint64_t>(pid);
721     EXPECT_EQ(TimerProxy::GetInstance().proxyPids_[key].size(), (const unsigned int)1);
722     EXPECT_EQ(TimerProxy::GetInstance().IsPidProxy(uid1, pid), true);
723     EXPECT_EQ(TimerProxy::GetInstance().IsPidProxy(uid2, pid), false);
724 }
725 
726 /**
727 * @tc.name: AdjustTimerProxy001
728 * @tc.desc: Determine whether to unify the heartbeat when the timer proxy is disabled.
729 * @tc.type: FUNC
730 */
731 HWTEST_F(TimeProxyTest, AdjustTimerProxy001, TestSize.Level1)
732 {
733     TimerPara paras;
734     paras.timerType = 2;
735     paras.windowLength = -1;
736     paras.interval = 0;
737     paras.flag = 0;
738     auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
739     int32_t uid = 2001;
740     int pid = 1111;
741     std::set<int> pidList;
742     pidList.insert(pid);
743     uint64_t timerId = 0;
744 
745     /* clear pidTimersMap_ */
746     TimerProxy::GetInstance().pidTimersMap_.clear();
747     TimerProxy::GetInstance().proxyPids_.clear();
748 
__anon4322b06b0d02(const uint64_t) 749     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
750                                                     wantAgent, uid, pid, timerId, NOT_STORE);
751     EXPECT_EQ(ret, TimeError::E_TIME_OK);
752 
753     /* Start a timer. The timer can be started successfully and can be recorded in pidTimerMap_. */
754     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
755     uint64_t triggerTime = 10000000 + nowElapsed;
756     ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
757     EXPECT_EQ(ret, TimeError::E_TIME_OK);
758     usleep(BLOCK_TEST_TIME);
759     EXPECT_EQ(TimerProxy::GetInstance().pidTimersMap_.size(), (const unsigned int)1);
760 
761     /* The proxy of a timer is successful and can be recorded in proxyPid_. */
762     bool retProxy = timerManagerHandler_->ProxyTimer(uid, pidList, true, true);
763     EXPECT_TRUE(retProxy);
764     usleep(BLOCK_TEST_TIME);
765     EXPECT_EQ(TimerProxy::GetInstance().proxyPids_.size(), (const unsigned int)1);
766     uint64_t key = (static_cast<uint64_t>(uid) << UID_PROXY_MASK) | static_cast<uint64_t>(pid);
767     auto it = TimerProxy::GetInstance().proxyPids_.find(key);
768     EXPECT_NE(it, TimerProxy::GetInstance().proxyPids_.end());
769     EXPECT_EQ(it->second.size(), (const unsigned int)1);
770 
771     /* Cancel a proxy timer. The proxy is canceled successfully, and the proxyPid_ table is updated. */
772     ret = timerManagerHandler_->ProxyTimer(uid, pidList, false, true);
773     EXPECT_TRUE(retProxy);
774     usleep(BLOCK_TEST_TIME);
775     EXPECT_EQ(TimerProxy::GetInstance().proxyPids_.size(), (const unsigned int)0);
776 
777     /* After the proxy is disabled, determine whether unified heartbeat is required again. */
778     bool isAdjust = true;
779     uint32_t interval = 300;
780     bool adjret = timerManagerHandler_->AdjustTimer(isAdjust, interval);
781     EXPECT_TRUE(adjret);
782     EXPECT_NE(TimerProxy::GetInstance().adjustTimers_.size(), (const unsigned int)0);
783 }
784 
785 /**
786 * @tc.name: ProxyTimerCover001
787 * @tc.desc: test CallbackAlarmIfNeed
788 * @tc.type: FUNC
789 */
790 HWTEST_F(TimeProxyTest, ProxyTimerCover001, TestSize.Level1)
791 {
792     auto res = TimerProxy::GetInstance().CallbackAlarmIfNeed(nullptr);
793     EXPECT_EQ(res, E_TIME_NULLPTR);
794 }
795 
796 /**
797 * @tc.name: ProxyTimerCover002
798 * @tc.desc: test UID
799 * @tc.type: FUNC
800 */
801 HWTEST_F(TimeProxyTest, ProxyTimerCover002, TestSize.Level1)
802 {
803     bool retProxy = timerManagerHandler_->ProxyTimer(UID, true, true);
804     EXPECT_TRUE(retProxy);
805     usleep(BLOCK_TEST_TIME);
806     {
807         std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().proxyMutex_);
808         EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int) 1);
809         auto it = TimerProxy::GetInstance().proxyUids_.find(UID);
810         EXPECT_NE(it, TimerProxy::GetInstance().proxyUids_.end());
811         EXPECT_EQ(it->second.size(), (const unsigned int) 0);
812     }
813 
814     auto duration = std::chrono::milliseconds::zero();
815     auto timePoint = std::chrono::steady_clock::now();
816     auto timerInfo1 = std::make_shared<TimerInfo>(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration,
817                                                  nullptr, nullptr, 0, UID, 0, "");
818     auto res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo1);
819     EXPECT_EQ(res, E_TIME_OK);
820     auto timerInfo2 = std::make_shared<TimerInfo>(TIMER_ID + 1, 0, duration, timePoint, duration, timePoint, duration,
821                                                  nullptr, nullptr, 0, UID, 0, "");
822     res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo2);
823     EXPECT_EQ(res, E_TIME_OK);
824 
825     TimerProxy::GetInstance().RemoveProxy(TIMER_ID, UID);
826     TimerProxy::GetInstance().RemoveProxy(TIMER_ID + 1, UID);
827 
828     {
829         std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().proxyMutex_);
830         auto it = TimerProxy::GetInstance().proxyMap_.find(UID);
831         EXPECT_EQ(it, TimerProxy::GetInstance().proxyMap_.end());
832     }
833 
834     res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo1);
835     EXPECT_EQ(res, E_TIME_OK);
836     retProxy = timerManagerHandler_->ProxyTimer(UID, false, true);
837     EXPECT_TRUE(retProxy);
838 
839     retProxy = timerManagerHandler_->ProxyTimer(UID, true, true);
840     EXPECT_TRUE(retProxy);
841     usleep(BLOCK_TEST_TIME);
842     res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo1);
843     EXPECT_EQ(res, E_TIME_OK);
844 
845     TimerProxy::GetInstance().ResetProxyMaps();
846 
847     TimerProxy::GetInstance().EraseTimerFromProxyUidMap(0, UID);
848 }
849 
850 
851 /**
852 * @tc.name: ProxyTimerCover003
853 * @tc.desc: test PID
854 * @tc.type: FUNC
855 */
856 HWTEST_F(TimeProxyTest, ProxyTimerCover003, TestSize.Level1)
857 {
858     TimerProxy::GetInstance().pidTimersMap_.clear();
859     TimerProxy::GetInstance().proxyPids_.clear();
860     int uid = 2000;
861     std::set<int> pidList;
862     pidList.insert(PID);
863     bool retProxy = timerManagerHandler_->ProxyTimer(uid, pidList, true, true);
864     EXPECT_TRUE(retProxy);
865     usleep(BLOCK_TEST_TIME);
866     EXPECT_EQ(TimerProxy::GetInstance().proxyPids_.size(), (const unsigned int)1);
867     uint64_t key = (static_cast<uint64_t>(uid) << UID_PROXY_MASK) | static_cast<uint64_t>(PID);
868     auto it = TimerProxy::GetInstance().proxyPids_.find(key);
869     EXPECT_NE(it, TimerProxy::GetInstance().proxyPids_.end());
870 
871     auto duration = std::chrono::milliseconds::zero();
872     auto timePoint = std::chrono::steady_clock::now();
873     auto timerInfo1 = std::make_shared<TimerInfo>(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration,
874                                                   nullptr, nullptr, 0, uid, PID, "");
875     auto res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo1);
876     EXPECT_EQ(res, E_TIME_OK);
877     auto timerInfo2 = std::make_shared<TimerInfo>(TIMER_ID + 1, 0, duration, timePoint, duration, timePoint, duration,
878                                                   nullptr, nullptr, 0, uid, PID, "");
879     res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo2);
880     EXPECT_EQ(res, E_TIME_OK);
881 
882     TimerProxy::GetInstance().RemovePidProxy(TIMER_ID, PID);
883     TimerProxy::GetInstance().RemovePidProxy(TIMER_ID + 1, PID);
884 
885     {
886         std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().proxyPidMutex_);
887         auto it = TimerProxy::GetInstance().proxyPidMap_.find(PID + 1);
888         EXPECT_EQ(it, TimerProxy::GetInstance().proxyPidMap_.end());
889     }
890 
891     res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo1);
892     EXPECT_EQ(res, E_TIME_OK);
893     retProxy = timerManagerHandler_->ProxyTimer(uid, pidList, false, true);
894     EXPECT_TRUE(retProxy);
895 
896     retProxy = timerManagerHandler_->ProxyTimer(uid, pidList, true, true);
897     EXPECT_TRUE(retProxy);
898     usleep(BLOCK_TEST_TIME);
899     res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo1);
900     EXPECT_EQ(res, E_TIME_OK);
901 
902     TimerProxy::GetInstance().ResetProxyPidMaps();
903 
904     TimerProxy::GetInstance().EraseTimerFromProxyPidMap(0, uid, PID);
905 }
906 
907 /**
908 * @tc.name: ProxyTimerCover004
909 * @tc.desc: test CallbackAlarmIfNeed
910 * @tc.type: FUNC
911 */
912 HWTEST_F(TimeProxyTest, ProxyTimerCover004, TestSize.Level1)
913 {
914     TimerProxy::GetInstance().RecordUidTimerMap(nullptr, false);
915     TimerProxy::GetInstance().RemoveUidTimerMap(nullptr);
916 
917     auto duration = std::chrono::milliseconds::zero();
918     auto timePoint = std::chrono::steady_clock::now();
919     auto timerInfo = std::make_shared<TimerInfo>(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration,
920                                                   nullptr, nullptr, 0, UID, PID, "");
921     TimerProxy::GetInstance().RecordUidTimerMap(timerInfo, false);
922     {
923         std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().uidTimersMutex_);
924         auto it = TimerProxy::GetInstance().uidTimersMap_.find(UID);
925         EXPECT_NE(it, TimerProxy::GetInstance().uidTimersMap_.end());
926     }
927     TimerProxy::GetInstance().RemoveUidTimerMap(timerInfo);
928     {
929         std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().uidTimersMutex_);
930         auto it = TimerProxy::GetInstance().uidTimersMap_.find(UID);
931         EXPECT_EQ(it, TimerProxy::GetInstance().uidTimersMap_.end());
932     }
933 
934     TimerProxy::GetInstance().RecordPidTimerMap(nullptr, false);
935     TimerProxy::GetInstance().RemovePidTimerMap(nullptr);
936 
937     TimerProxy::GetInstance().RecordPidTimerMap(timerInfo, false);
938     {
939         std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().pidTimersMutex_);
940         auto it = TimerProxy::GetInstance().pidTimersMap_.find(PID);
941         EXPECT_NE(it, TimerProxy::GetInstance().pidTimersMap_.end());
942     }
943     TimerProxy::GetInstance().RemovePidTimerMap(timerInfo);
944     {
945         std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().pidTimersMutex_);
946         auto it = TimerProxy::GetInstance().pidTimersMap_.find(PID);
947         EXPECT_EQ(it, TimerProxy::GetInstance().pidTimersMap_.end());
948     }
949 }
950 
951 }  // MiscServices
952 }  // OHOS