1 /*
2 * Copyright (c) 2021 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
22 using namespace testing::ext;
23 using namespace OHOS::PowerMgr;
24 using namespace OHOS;
25 using namespace std;
26
27 sptr<PowerMgrService> RunningLockTest::pmsTest_ = nullptr;
28 std::shared_ptr<RunningLockMgr> RunningLockTest::runningLockMgr_ = nullptr;
29
SetUpTestCase(void)30 void RunningLockTest::SetUpTestCase(void)
31 {
32 }
33
34 namespace {
35 /**
36 * @tc.name: RunningLockInnerKit000
37 * @tc.desc: Test RunningLockInnerKit function, connect PowerMgrService and call member function.
38 * @tc.type: FUNC
39 */
40 HWTEST_F (RunningLockTest, RunningLockInnerKit000, TestSize.Level0)
41 {
42 auto& powerMgrClient = PowerMgrClient::GetInstance();
43 auto runningLock1 = powerMgrClient.CreateRunningLock("runninglock1", RunningLockType::RUNNINGLOCK_SCREEN);
44 ASSERT_TRUE(runningLock1 != nullptr);
45
46 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
47 runningLock1->Lock();
48 ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true";
49
50 runningLock1->UnLock();
51 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
52
53 WorkTriggerList worklist;
54 worklist.push_back(nullptr);
55 worklist.push_back(nullptr);
56 worklist.push_back(nullptr);
57 runningLock1->SetWorkTriggerList(worklist);
58 auto& list1 = runningLock1->GetWorkTriggerList();
59 ASSERT_TRUE(list1.empty());
60 runningLock1->Lock();
61 runningLock1->SetWorkTriggerList(worklist);
62 auto& list2 = runningLock1->GetWorkTriggerList();
63 ASSERT_TRUE(list2.empty());
64 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit01 end.");
65 }
66
67 /**
68 * @tc.name: RunningLockInnerKit001
69 * @tc.desc: Test RunningLock proxy function.
70 * @tc.type: FUNC
71 */
72 HWTEST_F (RunningLockTest, RunningLockInnerKit001, TestSize.Level0)
73 {
74 auto& powerMgrClient = PowerMgrClient::GetInstance();
75 auto runningLock = powerMgrClient.CreateRunningLock("runninglock", RunningLockType::RUNNINGLOCK_BACKGROUND);
76 ASSERT_TRUE(runningLock != nullptr);
77 std::shared_ptr<WorkTrigger> worker1 = std::make_shared<WorkTrigger>(1, "worker1");
78 std::shared_ptr<WorkTrigger> worker2 = std::make_shared<WorkTrigger>(2, "worker2", 20);
79 std::shared_ptr<WorkTrigger> worker3 = std::make_shared<WorkTrigger>(3, "worker3", 30);
80 std::shared_ptr<WorkTrigger> worker4 = std::make_shared<WorkTrigger>();
81 WorkTriggerList worklist;
82 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit02, 1 usecount = %ld", worker1.use_count());
83 runningLock->Lock();
84
85 worklist.push_back(worker1);
86 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit02, 2 usecount = %ld", worker1.use_count());
87 worklist.push_back(worker2);
88 worklist.push_back(worker3);
89 runningLock->SetWorkTriggerList(worklist);
90 {
91 auto& list = runningLock->GetWorkTriggerList();
92 for (auto& worker : list) {
93 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit02, 3 usecount = %ld, name = %s, "
94 "uid = %d, pid = %d, abilityid = %d", worker.use_count(), worker->GetName().c_str(),
95 worker->GetUid(), worker->GetPid(), worker->GetAbilityId());
96 }
97 }
98 worklist.remove(worker2);
99 worklist.remove(worker3);
100 runningLock->SetWorkTriggerList(worklist);
101 runningLock->UnLock();
102 {
103 auto& list2 = runningLock->GetWorkTriggerList();
104 for (auto& worker : list2) {
105 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit02, 4 usecount = %ld, name = %s, "
106 "uid = %d, pid = %d, abilityid = %d", worker.use_count(), worker->GetName().c_str(),
107 worker->GetUid(), worker->GetPid(), worker->GetAbilityId());
108 }
109 }
110 worklist.push_back(worker4);
111 runningLock->SetWorkTriggerList(worklist);
112 {
113 auto& list2 = runningLock->GetWorkTriggerList();
114 for (auto& worker : list2) {
115 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit02, 5 usecount = %ld, name = %s,"
116 "uid = %d, pid = %d, abilityid = %d", worker.use_count(), worker->GetName().c_str(),
117 worker->GetUid(), worker->GetPid(), worker->GetAbilityId());
118 }
119 }
120 runningLock->Lock();
121 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit002 end.");
122 }
123
124 /**
125 * @tc.name: RunningLockInnerKit002
126 * @tc.desc: Test RunningLockInnerKit function, timeout lock.
127 * @tc.type: FUNC
128 */
129 HWTEST_F (RunningLockTest, RunningLockInnerKit002, TestSize.Level1)
130 {
131 auto& powerMgrClient = PowerMgrClient::GetInstance();
132 auto runningLock1 = powerMgrClient.CreateRunningLock("runninglock1", RunningLockType::RUNNINGLOCK_SCREEN);
133 ASSERT_TRUE(runningLock1 != nullptr);
134 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
135 runningLock1->Lock();
136 ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true";
137 runningLock1->UnLock();
138 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
139 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit003 1.");
140 // lock 50ms
141 runningLock1->Lock(50);
142 usleep(4000);
143 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit003 2.");
144 ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true";
145 usleep(1000);
146 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit003 3.");
147 ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true";
148 // wait 60ms
149 usleep(60000);
150 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
151 RunningLockInfo inInfo {"runninglockTest1", RunningLockType::RUNNINGLOCK_SCREEN};
152 std::shared_ptr<WorkTrigger> worker1 = std::make_shared<WorkTrigger>(1, "worker1");
153 std::shared_ptr<WorkTrigger> worker2 = std::make_shared<WorkTrigger>(2, "worker2", 20);
154 auto& worklist = inInfo.workTriggerlist;
155 worklist.push_back(worker1);
156 worklist.push_back(worker2);
157 Parcel data;
158 inInfo.Marshalling(data);
159 RunningLockInfo *outInfo = inInfo.Unmarshalling(data);
160 ASSERT_TRUE(outInfo != nullptr) << "outInfo != nullptr";
161 ASSERT_TRUE(outInfo->name == inInfo.name) << "outInfo->name == inInfo.name";
162 ASSERT_TRUE(outInfo->type == inInfo.type) << "outInfo->name == inInfo.name";
163 ASSERT_TRUE(outInfo->workTriggerlist.size() == inInfo.workTriggerlist.size()) <<
164 "outInfo->workTriggerlist.size() == inInfo.workTriggerlist.size()";
165 auto& list1 = inInfo.workTriggerlist;
166 auto& list2 = outInfo->workTriggerlist;
167 for (auto it1 = list1.begin(), it2 = list2.begin(); (it1 != list1.end()) && (it2 != list2.end());
168 it1++, it2++) {
169 ASSERT_TRUE((*it1)->GetUid() == (*it2)->GetUid()) << "it1->GetUid() == it2->GetUid()";
170 ASSERT_TRUE((*it1)->GetPid() == (*it2)->GetPid()) << "it1->GetPid() == it2->GetPid()";
171 ASSERT_TRUE((*it1)->GetAbilityId() == (*it2)->GetAbilityId()) << "it1->GetAbilityId() == it2->GetAbilityId()";
172 ASSERT_TRUE((*it1)->GetName() == (*it2)->GetName()) << "it1->GetName() == it2->GetName()";
173 }
174 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit003 end.");
175 }
176
177 /**
178 * @tc.name: RunningLockInnerKit005
179 * @tc.desc: Test RunningLock proxy function.
180 * @tc.type: FUNC
181 * @tc.require: issue I63PN9
182 */
183 HWTEST_F (RunningLockTest, RunningLockInnerKit005, TestSize.Level0)
184 {
185 std::shared_ptr<WorkTrigger> worker = std::make_shared<WorkTrigger>(1, "worker");
186 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit05, 1 usecount = %ld", worker.use_count());
187
188 worker->SetPid(1);
189 EXPECT_EQ(worker->GetUid(), 1);
190 worker->SetAbilityId(1);
191 EXPECT_EQ(worker->GetAbilityId(), 1);
192 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit005 end.");
193 }
194
195 /**
196 * @tc.name: RunningLockInnerKit004
197 * @tc.desc: Test RunningLockInnerKit function, timeout lock.
198 * @tc.type: FUNC
199 */
200 HWTEST_F (RunningLockTest, RunningLockInnerKit004, TestSize.Level1)
201 {
202 if (false) {
203 auto& powerMgrClient = PowerMgrClient::GetInstance();
204 auto runningLock1 = powerMgrClient.CreateRunningLock("runninglock005",
205 RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
206 ASSERT_TRUE(runningLock1 != nullptr);
207 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
208 // after 8ms unlock
209 runningLock1->Lock(30);
210 runningLock1->Lock(80);
211 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit005 1.");
212 usleep(50000);
213 ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true";
214 usleep(50000);
215 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
216 // no unlock
217 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit005 2.");
218 runningLock1->Lock(2);
219 runningLock1->Lock(3);
220 runningLock1->Lock();
221 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit005 3.");
222 usleep(8000);
223 ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true";
224 // after 3ms unlock
225 runningLock1->Lock(30);
226 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit005 4.");
227 usleep(50000);
228 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
229 runningLock1->Lock(5);
230 runningLock1->UnLock();
231 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
232 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit005 5.");
233 }
234 }
235
236 #ifdef IPC_AVAILABLE
237 /**
238 * @tc.name: RunningLockInnerKit003
239 * @tc.desc: Test RunningLockInnerKit function, timeout lock.
240 * @tc.type: FUNC
241 */
242 HWTEST_F (RunningLockTest, RunningLockInnerKit003, TestSize.Level0)
243 {
244 auto& powerMgrClient = PowerMgrClient::GetInstance();
245 auto runningLock1 = powerMgrClient.CreateRunningLock("runninglock2", RunningLockType::RUNNINGLOCK_SCREEN);
246 ASSERT_TRUE(runningLock1 != nullptr);
247 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
248 // after 8ms unlock
249 runningLock1->Lock(30);
250 runningLock1->Lock(80);
251 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit004 1.");
252 usleep(50000);
253 ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true";
254 usleep(50000);
255 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
256 // no unlock
257 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit004 2.");
258 runningLock1->Lock(2);
259 runningLock1->Lock(3);
260 runningLock1->Lock();
261 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit004 3.");
262 usleep(8000);
263 ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true";
264 // after 3ms unlock
265 runningLock1->Lock(30);
266 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit004 4.");
267 usleep(50000);
268 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
269 runningLock1->Lock(5);
270 runningLock1->UnLock();
271 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
272 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit004 5.");
273 }
274
275 /**
276 * @tc.name: RunningLockMgr001
277 * @tc.desc: Test RunningLockMgr function, connect PowerMgrService and call member function.
278 * @tc.type: FUNC
279 */
280 HWTEST_F (RunningLockTest, RunningLockMgr001, TestSize.Level0)
281 {
282 sptr<IRemoteObject> token = new RunningLockTokenStub();
283 ASSERT_TRUE(token != nullptr);
284 sptr<IRemoteObject> token2 = new RunningLockTokenStub();
285 ASSERT_TRUE(token2 != nullptr);
286 RunningLockInfo runningLockInfo1 {"runninglockTest1", RunningLockType::RUNNINGLOCK_SCREEN};
287 UserIPCInfo userIPCinfo {IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingPid()};
288 {
289 runningLockMgr_->Lock(token, runningLockInfo1, userIPCinfo);
290 TestRunningLockInnerExisit(token, runningLockInfo1);
291 ASSERT_TRUE(1 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
292 ASSERT_TRUE(0 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_BACKGROUND));
293 }
294 RunningLockInfo runningLockInfo2 {"runninglockTest2", RunningLockType::RUNNINGLOCK_BACKGROUND};
295 {
296 runningLockMgr_->Lock(token2, runningLockInfo2, userIPCinfo);
297 TestRunningLockInnerExisit(token2, runningLockInfo2);
298 ASSERT_TRUE(1 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
299 ASSERT_TRUE(1 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_BACKGROUND));
300 }
301 {
302 runningLockMgr_->UnLock(token);
303 TestRunningLockInnerNoExisit(token);
304 ASSERT_TRUE(0 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
305 ASSERT_TRUE(1 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_BACKGROUND));
306 }
307 {
308 runningLockMgr_->UnLock(token2);
309 TestRunningLockInnerNoExisit(token2);
310 ASSERT_TRUE(0 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
311 ASSERT_TRUE(0 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_BACKGROUND));
312 }
313 POWER_HILOGD(LABEL_TEST, "RunningLockTest::RunningLockMgr001 end.");
314 }
315
316 /**
317 * @tc.name: RunningLockMgr002
318 * @tc.desc: Test RunningLockMgr SetWorkerList function.
319 * @tc.type: FUNC
320 */
321 HWTEST_F (RunningLockTest, RunningLockMgr002, TestSize.Level0)
322 {
323 sptr<IRemoteObject> token = new RunningLockTokenStub();
324 ASSERT_TRUE(token != nullptr);
325 RunningLockInfo runningLockInfo1 {"runninglockTest1", RunningLockType::RUNNINGLOCK_SCREEN};
326 UserIPCInfo userIPCinfo {IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid()};
327 {
328 runningLockMgr_->Lock(token, runningLockInfo1, userIPCinfo);
329 TestRunningLockInnerExisit(token, runningLockInfo1);
330 ASSERT_TRUE(1 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
331 ASSERT_TRUE(0 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_BACKGROUND));
332 }
333 {
334 std::shared_ptr<WorkTrigger> worker1 = std::make_shared<WorkTrigger>(1, "worker1");
335 std::shared_ptr<WorkTrigger> worker2 = std::make_shared<WorkTrigger>(2, "worker2", 20);
336 std::shared_ptr<WorkTrigger> worker3 = std::make_shared<WorkTrigger>(3, "worker3", 30);
337 std::shared_ptr<WorkTrigger> worker4 = std::make_shared<WorkTrigger>();
338 RunningLockInfo runningLockInfo1;
339 auto& worklist = runningLockInfo1.workTriggerlist;
340 worklist.push_back(worker1);
341 worklist.push_back(worker2);
342 worklist.push_back(worker3);
343 runningLockMgr_->SetWorkTriggerList(token, worklist);
344 worklist.remove(worker3);
345 worklist.push_back(worker4);
346 runningLockMgr_->SetWorkTriggerList(token, worklist);
347 }
348 {
349 runningLockMgr_->UnLock(token);
350 TestRunningLockInnerNoExisit(token);
351 ASSERT_TRUE(0 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
352 ASSERT_TRUE(0 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_BACKGROUND));
353 }
354 POWER_HILOGD(LABEL_TEST, "RunningLockTest::RunningLockMgr002 end.");
355 }
356
357
358 /**
359 * @tc.name: RunningLockMgr003
360 * @tc.desc: Test RunningLockMgr Proxy function.
361 * @tc.type: FUNC
362 */
363 HWTEST_F (RunningLockTest, RunningLockMgr003, TestSize.Level0)
364 {
365 sptr<IRemoteObject> token1 = new RunningLockTokenStub();
366 sptr<IRemoteObject> token2 = new RunningLockTokenStub();
367 RunningLockInfo runningLockInfo1 {"runninglocktest1", RunningLockType::RUNNINGLOCK_SCREEN};
368 RunningLockInfo runningLockInfo2 {"runninglocktest2", RunningLockType::RUNNINGLOCK_SCREEN};
369 UserIPCInfo userIPCinfo1 {1, 1};
370 UserIPCInfo userIPCinfo2 {2, 2};
371 runningLockMgr_->Lock(token1, runningLockInfo1, userIPCinfo1);
372 runningLockMgr_->Lock(token2, runningLockInfo2, userIPCinfo2);
373 TestRunningLockInnerExisit(token1, runningLockInfo1);
374 TestRunningLockInnerExisit(token2, runningLockInfo2);
375 ASSERT_TRUE(2 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
376 ASSERT_TRUE(2 == runningLockMgr_->GetValidRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
377 auto& proxymap = runningLockMgr_->GetRunningLockProxyMap();
378 ASSERT_TRUE(proxymap.empty());
379 {
380 // proxy by userIPCinfo1, lockinner1 disabled
381 runningLockMgr_->ProxyRunningLock(true, userIPCinfo1.uid, userIPCinfo1.pid);
382 auto lockInner1 = runningLockMgr_->GetRunningLockInner(token1);
383 ASSERT_TRUE(!lockInner1->GetReallyLocked());
384 ASSERT_TRUE(lockInner1->GetDisabled());
385 ASSERT_TRUE(!proxymap.empty() && (proxymap.count(userIPCinfo1.uid) > 0));
386
387 runningLockMgr_->ProxyRunningLock(true, userIPCinfo2.uid, userIPCinfo2.pid);
388 auto lockInner2 = runningLockMgr_->GetRunningLockInner(token2);
389 ASSERT_TRUE(!lockInner2->GetReallyLocked());
390 ASSERT_TRUE(lockInner2->GetDisabled());
391 ASSERT_TRUE(!proxymap.empty() && (proxymap.count(userIPCinfo2.uid) > 0));
392 ASSERT_TRUE(2 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
393 ASSERT_TRUE(0 == runningLockMgr_->GetValidRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
394
395 runningLockMgr_->ProxyRunningLock(false, userIPCinfo1.uid, userIPCinfo1.pid);
396 ASSERT_TRUE(lockInner1->GetReallyLocked());
397 ASSERT_TRUE(!lockInner1->GetDisabled());
398 ASSERT_TRUE(!proxymap.empty() && (proxymap.count(userIPCinfo1.uid) == 0));
399
400 runningLockMgr_->ProxyRunningLock(false, userIPCinfo2.uid, userIPCinfo2.pid);
401 ASSERT_TRUE(lockInner2->GetReallyLocked());
402 ASSERT_TRUE(!lockInner2->GetDisabled());
403 ASSERT_TRUE(proxymap.empty());
404 ASSERT_TRUE(2 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
405 ASSERT_TRUE(2 == runningLockMgr_->GetValidRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
406 }
407 runningLockMgr_->UnLock(token1);
408 runningLockMgr_->UnLock(token2);
409 POWER_HILOGD(LABEL_TEST, "RunningLockTest::RunningLockMgr003 end.");
410 }
411
412 /**
413 * @tc.name: RunningLockMgr004
414 * @tc.desc: Test RunningLockMgr Proxy function.
415 * @tc.type: FUNC
416 */
417 HWTEST_F (RunningLockTest, RunningLockMgr004, TestSize.Level0)
418 {
419 sptr<IRemoteObject> token3 = new RunningLockTokenStub();
420 sptr<IRemoteObject> token4 = new RunningLockTokenStub();
421 RunningLockInfo runningLockInfo3 {"runninglocktest3", RunningLockType::RUNNINGLOCK_SCREEN};
422 RunningLockInfo runningLockInfo4 {"runninglocktest4", RunningLockType::RUNNINGLOCK_SCREEN};
423 UserIPCInfo userIPCinfo3 {3, 3};
424 UserIPCInfo userIPCinfo4 {3, 4};
425 runningLockMgr_->Lock(token3, runningLockInfo3, userIPCinfo3);
426 runningLockMgr_->Lock(token4, runningLockInfo4, userIPCinfo4);
427 TestRunningLockInnerExisit(token3, runningLockInfo3);
428 TestRunningLockInnerExisit(token4, runningLockInfo4);
429 ASSERT_TRUE(2 == runningLockMgr_->GetValidRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
430 auto& proxymap = runningLockMgr_->GetRunningLockProxyMap();
431 ASSERT_TRUE(proxymap.empty());
432 {
433 // lockinner3 and lockinner4 have same pid, save uid key, pid set {uid3,{pid3, pid4}}
434 runningLockMgr_->ProxyRunningLock(true, userIPCinfo3.uid, userIPCinfo3.pid);
435 runningLockMgr_->ProxyRunningLock(true, userIPCinfo4.uid, userIPCinfo4.pid);
436 auto lockInner3 = runningLockMgr_->GetRunningLockInner(token3);
437 ASSERT_TRUE(!lockInner3->GetReallyLocked());
438 ASSERT_TRUE(lockInner3->GetDisabled());
439 ASSERT_TRUE(!proxymap.empty() && (proxymap.count(userIPCinfo3.uid) > 0));
440 {
441 auto it = proxymap.find(userIPCinfo3.uid);
442 ASSERT_TRUE(it != proxymap.end());
443 auto& pidset = it->second;
444 ASSERT_TRUE(pidset.count(userIPCinfo3.pid) == 1);
445 }
446 auto lockInner4 = runningLockMgr_->GetRunningLockInner(token4);
447 ASSERT_TRUE(lockInner4 != nullptr);
448 ASSERT_TRUE(!lockInner4->GetReallyLocked());
449 ASSERT_TRUE(lockInner4->GetDisabled());
450 ASSERT_TRUE(!proxymap.empty() && (proxymap.count(userIPCinfo4.uid) > 0));
451 {
452 auto it = proxymap.find(userIPCinfo4.uid);
453 ASSERT_TRUE(it != proxymap.end());
454 auto& pidset = it->second;
455 ASSERT_TRUE(pidset.count(userIPCinfo4.pid) == 1);
456 }
457 ASSERT_TRUE(0 == runningLockMgr_->GetValidRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
458 runningLockMgr_->ProxyRunningLock(false, userIPCinfo3.uid, INVALID_PID);
459 ASSERT_TRUE(proxymap.empty());
460 ASSERT_TRUE(lockInner3->GetReallyLocked() && !lockInner3->GetDisabled());
461 ASSERT_TRUE(lockInner4->GetReallyLocked() && !lockInner4->GetDisabled());
462 ASSERT_TRUE(2 == runningLockMgr_->GetValidRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
463 }
464 runningLockMgr_->UnLock(token3);
465 runningLockMgr_->UnLock(token4);
466 POWER_HILOGD(LABEL_TEST, "RunningLockTest::RunningLockMgr004 end.");
467 }
468
469 /**
470 * @tc.name: RunningLockMgr005
471 * @tc.desc: Test RunningLockMgr Proxy function.
472 * @tc.type: FUNC
473 */
474 HWTEST_F (RunningLockTest, RunningLockMgr005, TestSize.Level0)
475 {
476 sptr<IRemoteObject> token = new RunningLockTokenStub();
477 RunningLockInfo runningLockInfo1 {"runninglocktest1", RunningLockType::RUNNINGLOCK_SCREEN};
478 UserIPCInfo userIPCinfo1 {1, 1};
479 runningLockMgr_->Lock(token, runningLockInfo1, userIPCinfo1);
480 auto& proxymap = runningLockMgr_->GetRunningLockProxyMap();
481 ASSERT_TRUE(proxymap.empty());
482 {
483 auto lockInner1 = runningLockMgr_->GetRunningLockInner(token);
484 runningLockMgr_->ProxyRunningLock(true, userIPCinfo1.uid, userIPCinfo1.pid);
485 ASSERT_TRUE(!lockInner1->GetReallyLocked() && lockInner1->GetDisabled());
486 runningLockMgr_->ProxyRunningLock(false, userIPCinfo1.uid, userIPCinfo1.pid);
487 ASSERT_TRUE(lockInner1->GetReallyLocked() && !lockInner1->GetDisabled());
488 UserIPCInfo workeripc1 {10, 20};
489 UserIPCInfo workeripc2 {20, 20};
490 runningLockMgr_->ProxyRunningLock(true, workeripc1.uid, workeripc1.pid);
491 runningLockMgr_->ProxyRunningLock(true, workeripc2.uid, workeripc2.pid);
492 std::shared_ptr<WorkTrigger> worker1 = std::make_shared<WorkTrigger>(workeripc1.uid, "worker1", workeripc1.pid);
493 std::shared_ptr<WorkTrigger> worker2 = std::make_shared<WorkTrigger>(workeripc2.uid, "worker2", workeripc2.pid);
494 auto& worklist = runningLockInfo1.workTriggerlist;
495 worklist.push_back(worker1);
496 worklist.push_back(worker2);
497 runningLockMgr_->SetWorkTriggerList(token, worklist);
498 ASSERT_TRUE(!lockInner1->GetReallyLocked() && lockInner1->GetDisabled());
499 worklist.remove(worker2);
500 runningLockMgr_->SetWorkTriggerList(token, worklist);
501 ASSERT_TRUE(!lockInner1->GetReallyLocked() && lockInner1->GetDisabled());
502 runningLockMgr_->ProxyRunningLock(false, workeripc1.uid, workeripc1.pid);
503 runningLockMgr_->ProxyRunningLock(false, workeripc2.uid, workeripc2.pid);
504 ASSERT_TRUE(lockInner1->GetReallyLocked() && !lockInner1->GetDisabled());
505 UserIPCInfo workeripc3 {10, 30};
506 std::shared_ptr<WorkTrigger> worker3 = std::make_shared<WorkTrigger>(workeripc3.uid, "worker3", workeripc3.pid);
507 runningLockMgr_->ProxyRunningLock(true, workeripc1.uid, workeripc1.pid);
508 runningLockMgr_->ProxyRunningLock(true, workeripc3.uid, workeripc3.pid);
509 runningLockMgr_->SetWorkTriggerList(token, worklist);
510 ASSERT_TRUE(!lockInner1->GetReallyLocked() && lockInner1->GetDisabled());
511 runningLockMgr_->ProxyRunningLock(false, workeripc3.uid, INVALID_PID);
512 ASSERT_TRUE(lockInner1->GetReallyLocked() && !lockInner1->GetDisabled());
513 ASSERT_TRUE(proxymap.empty());
514 runningLockMgr_->ProxyRunningLock(true, workeripc1.uid, workeripc1.pid);
515 runningLockMgr_->ProxyRunningLock(true, workeripc3.uid, workeripc3.pid);
516 runningLockMgr_->ProxyRunningLock(true, workeripc1.uid, INVALID_PID);
517 auto it = proxymap.find(workeripc1.uid);
518 auto& pidset = it->second;
519 ASSERT_TRUE((pidset.size() == 1) && (pidset.count(INVALID_PID) == 1));
520 runningLockMgr_->ProxyRunningLock(false, workeripc1.uid, INVALID_PID);
521 ASSERT_TRUE(proxymap.empty());
522 }
523 runningLockMgr_->UnLock(token);
524 POWER_HILOGD(LABEL_TEST, "RunningLockTest::RunningLockMgr005 end.");
525 }
526 #endif // IPC_AVAILABLE
527 }
528