1 /*
2  * Copyright (c) 2021-2024 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 #include "running_lock_test.h"
17 
18 #include <ipc_skeleton.h>
19 
20 #include "actions/irunning_lock_action.h"
21 #include "power_mgr_service.h"
22 #include "running_lock_mgr.h"
23 
24 using namespace testing::ext;
25 using namespace OHOS::PowerMgr;
26 using namespace OHOS;
27 using namespace std;
28 
29 namespace {
30 constexpr int32_t US_PER_MS = 1000;
31 constexpr int32_t app0Uid = 8;
32 constexpr int32_t app1Uid = 9;
33 }
34 
35 namespace {
36 /**
37  * @tc.name: RunningLockTest001
38  * @tc.desc: Test RunningLockInnerKit function, connect PowerMgrService and call member function.
39  * @tc.type: FUNC
40  */
41 HWTEST_F (RunningLockTest, RunningLockTest001, TestSize.Level0)
42 {
43     auto& powerMgrClient = PowerMgrClient::GetInstance();
44     auto runningLock1 = powerMgrClient.CreateRunningLock("runninglock1", RunningLockType::RUNNINGLOCK_SCREEN);
45     ASSERT_TRUE(runningLock1 != nullptr);
46 
47     ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
48     runningLock1->Lock();
49     ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true";
50 
51     runningLock1->UnLock();
52     ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
53     POWER_HILOGI(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit01 end");
54 }
55 
56 /**
57  * @tc.name: RunningLockTest002
58  * @tc.desc: Test RunningLockInnerKit function, timeout lock.
59  * @tc.type: FUNC
60  */
61 HWTEST_F (RunningLockTest, RunningLockTest002, TestSize.Level1)
62 {
63     auto& powerMgrClient = PowerMgrClient::GetInstance();
64     auto runningLock1 = powerMgrClient.CreateRunningLock("runninglock1", RunningLockType::RUNNINGLOCK_SCREEN);
65     ASSERT_TRUE(runningLock1 != nullptr);
66     ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
67     runningLock1->Lock();
68     ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true";
69     runningLock1->UnLock();
70     ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
71     POWER_HILOGI(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit003 1");
72     // lock 50ms
73     runningLock1->Lock(50);
74     usleep(4000);
75     POWER_HILOGI(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit003 2");
76     ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true";
77     usleep(1000);
78     POWER_HILOGI(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit003 3");
79     ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true";
80     // wait 100ms
81     usleep(100000);
82     POWER_HILOGI(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit003 end");
83 }
84 
85 /**
86  * @tc.name: RunningLockTest003
87  * @tc.desc: Test RunningLockInnerKit function, timeout lock.
88  * @tc.type: FUNC
89  */
90 HWTEST_F (RunningLockTest, RunningLockTest003, TestSize.Level1)
91 {
92     if (false) {
93         auto& powerMgrClient = PowerMgrClient::GetInstance();
94         auto runningLock1 = powerMgrClient.CreateRunningLock("runninglock005",
95             RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
96         ASSERT_TRUE(runningLock1 != nullptr);
97         ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
98         // after 8ms unlock
99         runningLock1->Lock(30);
100         runningLock1->Lock(80);
101         POWER_HILOGI(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit005 1");
102         usleep(50000);
103         ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true";
104         usleep(50000);
105         ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
106         // no unlock
107         POWER_HILOGI(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit005 2");
108         runningLock1->Lock(2);
109         runningLock1->Lock(3);
110         runningLock1->Lock();
111         POWER_HILOGI(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit005 3");
112         usleep(8000);
113         ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true";
114         // after 3ms unlock
115         runningLock1->Lock(30);
116         POWER_HILOGI(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit005 4");
117         usleep(50000);
118         ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
119         runningLock1->Lock(5);
120         runningLock1->UnLock();
121         ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
122         POWER_HILOGI(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit005 5");
123     }
124 }
125 
126 /**
127  * @tc.name: RunningLockTest004
128  * @tc.desc: Test CreateRunningLock function.
129  * @tc.type: FUNC
130  * @tc.require: issueI6NWQD
131  */
132 HWTEST_F (RunningLockTest, RunningLockTest004, TestSize.Level1)
133 {
134     auto& powerMgrClient = PowerMgrClient::GetInstance();
135     std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock(
136         "background", RunningLockType::RUNNINGLOCK_BACKGROUND);
137     EXPECT_NE(runningLock, nullptr);
138 
139     runningLock = powerMgrClient.CreateRunningLock("backgroudPhone", RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
140     EXPECT_NE(runningLock, nullptr);
141 
142     runningLock = powerMgrClient.CreateRunningLock(
143         "backgroudNatification", RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION);
144     EXPECT_NE(runningLock, nullptr);
145 
146     runningLock = powerMgrClient.CreateRunningLock("backgroudAudio", RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO);
147     EXPECT_NE(runningLock, nullptr);
148 
149     runningLock = powerMgrClient.CreateRunningLock("backgroudSport", RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT);
150     EXPECT_NE(runningLock, nullptr);
151 
152     runningLock = powerMgrClient.CreateRunningLock(
153         "backgroudNavigation", RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION);
154     EXPECT_NE(runningLock, nullptr);
155 
156     runningLock = powerMgrClient.CreateRunningLock("backgroudTask", RunningLockType::RUNNINGLOCK_BACKGROUND_TASK);
157     EXPECT_NE(runningLock, nullptr);
158 
159     runningLock = powerMgrClient.CreateRunningLock("butt", RunningLockType::RUNNINGLOCK_BUTT);
160     EXPECT_EQ(runningLock, nullptr);
161 }
162 
163 /**
164  * @tc.name: RunningLockTest005
165  * @tc.desc: Test ProxyRunningLock function.
166  * @tc.type: FUNC
167  * @tc.require: issueI6S0YY
168  */
169 HWTEST_F (RunningLockTest, RunningLockTest005, TestSize.Level1)
170 {
171     auto& powerMgrClient = PowerMgrClient::GetInstance();
172     std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock(
173         "background.test005", RunningLockType::RUNNINGLOCK_BACKGROUND);
174     ASSERT_NE(runningLock, nullptr);
175 
176     pid_t curUid = getuid();
177     pid_t curPid = getpid();
178     runningLock->Lock();
179     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, curUid));
180     EXPECT_FALSE(runningLock->IsUsed());
181     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, curUid));
182     runningLock->UnLock();
183 }
184 
185 /**
186  * @tc.name: RunningLockTest006
187  * @tc.desc: Test ProxyRunningLock function.
188  * @tc.type: FUNC
189  * @tc.require: issueI6S0YY
190  */
191 HWTEST_F (RunningLockTest, RunningLockTest006, TestSize.Level1)
192 {
193     auto& powerMgrClient = PowerMgrClient::GetInstance();
194     std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock(
195         "background.test006", RunningLockType::RUNNINGLOCK_SCREEN);
196     ASSERT_NE(runningLock, nullptr);
197 
198     pid_t curUid = getuid();
199     pid_t curPid = getpid();
200 
201     runningLock->Lock(-1);
202     EXPECT_TRUE(runningLock->IsUsed());
203 
204     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, curUid));
205     EXPECT_FALSE(runningLock->IsUsed());
206 
207     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, curUid));
208     EXPECT_TRUE(runningLock->IsUsed());
209     runningLock->UnLock();
210 }
211 
212 /**
213  * @tc.name: RunningLockTest007
214  * @tc.desc: Test ProxyRunningLock function.
215  * @tc.type: FUNC
216  * @tc.require: issueI6S0YY
217  */
218 HWTEST_F (RunningLockTest, RunningLockTest007, TestSize.Level1)
219 {
220     auto& powerMgrClient = PowerMgrClient::GetInstance();
221     std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock(
222         "background.test007", RunningLockType::RUNNINGLOCK_SCREEN);
223     ASSERT_NE(runningLock, nullptr);
224 
225     pid_t curUid = getuid();
226     pid_t curPid = getpid();
227     int32_t timeoutMs = 1000;
228 
229     runningLock->Lock(timeoutMs);
230     EXPECT_TRUE(runningLock->IsUsed());
231 
232     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, curUid));
233     EXPECT_FALSE(runningLock->IsUsed());
234 
235     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, curUid));
236     runningLock->UnLock();
237     EXPECT_FALSE(runningLock->IsUsed());
238 }
239 
240 /**
241  * @tc.name: RunningLockTest008
242  * @tc.desc: Test ProxyRunningLock function, create runninglock first, then proxy
243  * @tc.type: FUNC
244  * @tc.require: issueI6S0YY
245  */
246 HWTEST_F (RunningLockTest, RunningLockTest008, TestSize.Level1)
247 {
248     auto& powerMgrClient = PowerMgrClient::GetInstance();
249 
250     pid_t curUid = getuid();
251     pid_t curPid = getpid();
252     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, curUid));
253 
254     std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock(
255         "background.test008", RunningLockType::RUNNINGLOCK_BACKGROUND);
256     ASSERT_NE(runningLock, nullptr);
257     runningLock->Lock();
258     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, curUid));
259     runningLock->UnLock();
260 }
261 
262 /**
263  * @tc.name: RunningLockTest009
264  * @tc.desc: Test ProxyRunningLock function.
265  * @tc.type: FUNC
266  * @tc.require: issueI6S0YY
267  */
268 HWTEST_F (RunningLockTest, RunningLockTest009, TestSize.Level1)
269 {
270     auto& powerMgrClient = PowerMgrClient::GetInstance();
271     std::shared_ptr<RunningLock> screenRunningLock = powerMgrClient.CreateRunningLock(
272         "screen.test009", RunningLockType::RUNNINGLOCK_SCREEN);
273     ASSERT_NE(screenRunningLock, nullptr);
274     std::shared_ptr<RunningLock> proximityRunningLock = powerMgrClient.CreateRunningLock(
275         "proximity.test009", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
276     ASSERT_NE(proximityRunningLock, nullptr);
277 
278     pid_t curUid = getuid();
279     pid_t curPid = getpid();
280     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, curUid));
281 
282     screenRunningLock->Lock();
283     EXPECT_TRUE(screenRunningLock->IsUsed());
284     screenRunningLock->UnLock();
285 
286     proximityRunningLock->Lock();
287     EXPECT_TRUE(proximityRunningLock->IsUsed());
288     proximityRunningLock->UnLock();
289 
290     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, curUid));
291 }
292 
293 /**
294  * @tc.name: RunningLockTest010
295  * @tc.desc: Test ProxyRunningLock function, pid is invalid
296  * @tc.type: FUNC
297  * @tc.require: issueI6S0YY
298  */
299 HWTEST_F (RunningLockTest, RunningLockTest010, TestSize.Level1)
300 {
301     pid_t curUid = 1;
302     pid_t curPid = -1;
303     auto& powerMgrClient = PowerMgrClient::GetInstance();
304     EXPECT_FALSE(powerMgrClient.ProxyRunningLock(true, curPid, curUid));
305     EXPECT_FALSE(powerMgrClient.ProxyRunningLock(false, curPid, curUid));
306 }
307 
308 /**
309  * @tc.name: RunningLockTest011
310  * @tc.desc: Test RunningLockProxt AddRunningLock function
311  * @tc.type: FUNC
312  * @tc.require: issueI7405P
313  */
314 HWTEST_F (RunningLockTest, RunningLockTest011, TestSize.Level1)
315 {
316     auto runninglockProxy = std::make_shared<RunningLockProxy>();
317     pid_t pid = 1;
318     pid_t uid = -1;
319     sptr<IRemoteObject> remoteObj = new RunningLockTokenStub();
320     runninglockProxy->AddRunningLock(0, 0, remoteObj);
321     EXPECT_TRUE(runninglockProxy->IncreaseProxyCnt(pid, uid));
322     EXPECT_TRUE(runninglockProxy->DecreaseProxyCnt(pid, uid));
323 }
324 
325 /**
326  * @tc.name: RunningLockTest012
327  * @tc.desc: Test RunningLockProxt RemoveRunningLock function
328  * @tc.type: FUNC
329  * @tc.require: issueI7405P
330  */
331 HWTEST_F (RunningLockTest, RunningLockTest012, TestSize.Level1)
332 {
333     auto runninglockProxy = std::make_shared<RunningLockProxy>();
334     pid_t pid = 1;
335     pid_t uid = -1;
336     sptr<IRemoteObject> remoteObj = new RunningLockTokenStub();
337     sptr<IRemoteObject> remoteObj2 = nullptr;
338     runninglockProxy->AddRunningLock(pid, uid, remoteObj);
339     runninglockProxy->RemoveRunningLock(pid, uid, remoteObj2);
340     EXPECT_TRUE(runninglockProxy->IncreaseProxyCnt(0, 0));
341     EXPECT_TRUE(runninglockProxy->DecreaseProxyCnt(0, 0));
342 }
343 
344 /**
345  * @tc.name: RunningLockTest013
346  * @tc.desc: Test RunningLockProxt RemoveRunningLock function
347  * @tc.type: FUNC
348  * @tc.require: issueI7405P
349  */
350 HWTEST_F (RunningLockTest, RunningLockTest013, TestSize.Level1)
351 {
352     auto runninglockProxy = std::make_shared<RunningLockProxy>();
353     pid_t pid = 1;
354     pid_t uid = -1;
355     sptr<IRemoteObject> remoteObj = new RunningLockTokenStub();
356     runninglockProxy->RemoveRunningLock(pid, uid, remoteObj);
357     EXPECT_TRUE(runninglockProxy->IncreaseProxyCnt(pid, uid));
358 }
359 
360 /**
361  * @tc.name: RunningLockTest014
362  * @tc.desc: Test ProxyRunningLocks function
363  * @tc.type: FUNC
364  * @tc.require: issueI7MNRN
365  */
366 HWTEST_F(RunningLockTest, RunningLockTest014, TestSize.Level1)
367 {
368     pid_t curUid = getuid();
369     pid_t curPid = getpid();
370     std::vector<std::pair<pid_t, pid_t>> processInfos;
371     processInfos.push_back(std::pair<pid_t, pid_t>(curPid, curUid));
372     auto& powerMgrClient = PowerMgrClient::GetInstance();
373     EXPECT_TRUE(powerMgrClient.ProxyRunningLocks(true, processInfos));
374 }
375 
376 /**
377  * @tc.name: RunningLockTest015
378  * @tc.desc: Test Recover function
379  * @tc.type: FUNC
380  * @tc.require:
381  */
382 HWTEST_F(RunningLockTest, RunningLockTest015, TestSize.Level1)
383 {
384     auto& powerMgrClient = PowerMgrClient::GetInstance();
385     auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
386     pms->OnStart();
387     auto runningLockMgr = pms->GetRunningLockMgr();
388     sptr<IPowerMgr> ptr;
389     ptr.ForceSetRefPtr(static_cast<IPowerMgr*>(pms.GetRefPtr()));
390     pms.GetRefPtr()->IncStrongRef(pms.GetRefPtr());
391     RunningLock runninglock1(ptr, "runninglock_recover_test", RunningLockType::RUNNINGLOCK_SCREEN);
392     runninglock1.Init();
393     const auto& infos = runningLockMgr->GetRunningLockMap();
__anon3ba52d300302(const auto& pair) 394     const auto iter = std::find_if(infos.begin(), infos.end(), [](const auto& pair) {
395         return pair.second->GetName() == "runninglock_recover_test";
396     });
397     EXPECT_TRUE(iter != infos.end());
398     const sptr<IRemoteObject> token = iter->first;
399     runningLockMgr->ReleaseLock(token);
400     EXPECT_TRUE(infos.count(token) == 0);
401     runninglock1.Recover(ptr);
402     EXPECT_TRUE(infos.count(token) > 0);
403 }
404 
405 /**
406  * @tc.name: RunningLockTest016
407  * @tc.desc: Test ProxyRunningLock function
408  * @tc.type: FUNC
409  * @tc.require
410  */
411 HWTEST_F (RunningLockTest, RunningLockTest016, TestSize.Level1)
412 {
413     auto& powerMgrClient = PowerMgrClient::GetInstance();
414 
415     pid_t curUid = getuid();
416     pid_t curPid = getpid();
417 
418     std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock(
419         "background.test016", RunningLockType::RUNNINGLOCK_BACKGROUND);
420     ASSERT_NE(runningLock, nullptr);
421     runningLock->Lock();
422     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, curUid));
423     EXPECT_FALSE(runningLock->IsUsed());
424     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, curUid));
425     EXPECT_TRUE(runningLock->IsUsed());
426     runningLock->UnLock();
427 }
428 
429 /**
430  * @tc.name: RunningLockTest017
431  * @tc.desc: Test UpdateWorkSource function
432  * @tc.type: FUNC
433  * @tc.require
434  */
435 HWTEST_F (RunningLockTest, RunningLockTest017, TestSize.Level1)
436 {
437     auto& powerMgrClient = PowerMgrClient::GetInstance();
438 
439     pid_t curUid = getuid();
440     pid_t curPid = getpid();
441 
442     std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock(
443         "background.test017", RunningLockType::RUNNINGLOCK_BACKGROUND);
444     ASSERT_NE(runningLock, nullptr);
445     runningLock->Lock();
446     std::vector<int32_t> workSource { 0 };
447     EXPECT_TRUE(runningLock->UpdateWorkSource(workSource) == 0);
448     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, 0));
449     EXPECT_FALSE(runningLock->IsUsed());
450     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, 0));
451     EXPECT_TRUE(runningLock->IsUsed());
452     runningLock->UnLock();
453 }
454 
455 /**
456  * @tc.name: RunningLockTest018
457  * @tc.desc: Test UpdateWorkSource function
458  * @tc.type: FUNC
459  * @tc.require
460  */
461 HWTEST_F (RunningLockTest, RunningLockTest018, TestSize.Level1)
462 {
463     auto runninglockProxy = std::make_shared<RunningLockProxy>();
464     pid_t pid = 1;
465     pid_t uid = -1;
466     sptr<IRemoteObject> remoteObj = new RunningLockTokenStub();
467     sptr<IRemoteObject> remoteObj2 = nullptr;
468     runninglockProxy->AddRunningLock(pid, uid, remoteObj);
469     runninglockProxy->RemoveRunningLock(pid, uid, remoteObj2);
470 
471     EXPECT_TRUE(runninglockProxy->UpdateWorkSource(pid, uid, remoteObj, {{0, {"test", false}}}));
472     runninglockProxy->RemoveRunningLock(pid, uid, remoteObj);
473     EXPECT_TRUE(runninglockProxy->IncreaseProxyCnt(pid, uid));
474     EXPECT_TRUE(runninglockProxy->DecreaseProxyCnt(pid, uid));
475 }
476 
477 /**
478  * @tc.name: RunningLockTest019
479  * @tc.desc: Test UpdateWorkSource function
480  * @tc.type: FUNC
481  * @tc.require
482  */
483 HWTEST_F (RunningLockTest, RunningLockTest019, TestSize.Level1)
484 {
485     auto runninglockProxy = std::make_shared<RunningLockProxy>();
486     pid_t pid = 1;
487     pid_t uid = -1;
488     sptr<IRemoteObject> remoteObj = new RunningLockTokenStub();
489     sptr<IRemoteObject> remoteObj2 = nullptr;
490     runninglockProxy->AddRunningLock(pid, uid, remoteObj);
491     runninglockProxy->RemoveRunningLock(pid, uid, remoteObj2);
492 
493     EXPECT_TRUE(runninglockProxy->UpdateWorkSource(pid, uid, remoteObj, {{0, {"test019", false}}}));
494     EXPECT_TRUE(runninglockProxy->UpdateProxyState(pid, uid, remoteObj, true));
495     EXPECT_TRUE(runninglockProxy->UpdateProxyState(pid, uid, remoteObj, false));
496     runninglockProxy->RemoveRunningLock(pid, uid, remoteObj);
497     EXPECT_TRUE(runninglockProxy->IncreaseProxyCnt(pid, uid));
498     EXPECT_TRUE(runninglockProxy->DecreaseProxyCnt(pid, uid));
499 }
500 } // namespace