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