1 /* 2 * Copyright (c) 2021-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 #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 } 32 33 namespace { 34 /** 35 * @tc.name: RunningLockTest001 36 * @tc.desc: Test RunningLockInnerKit function, connect PowerMgrService and call member function. 37 * @tc.type: FUNC 38 */ 39 HWTEST_F (RunningLockTest, RunningLockTest001, TestSize.Level0) 40 { 41 auto& powerMgrClient = PowerMgrClient::GetInstance(); 42 auto runningLock1 = powerMgrClient.CreateRunningLock("runninglock1", RunningLockType::RUNNINGLOCK_SCREEN); 43 ASSERT_TRUE(runningLock1 != nullptr); 44 45 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false"; 46 runningLock1->Lock(); 47 ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true"; 48 49 runningLock1->UnLock(); 50 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false"; 51 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit01 end"); 52 } 53 54 /** 55 * @tc.name: RunningLockTest002 56 * @tc.desc: Test RunningLockInnerKit function, timeout lock. 57 * @tc.type: FUNC 58 */ 59 HWTEST_F (RunningLockTest, RunningLockTest002, TestSize.Level1) 60 { 61 auto& powerMgrClient = PowerMgrClient::GetInstance(); 62 auto runningLock1 = powerMgrClient.CreateRunningLock("runninglock1", RunningLockType::RUNNINGLOCK_SCREEN); 63 ASSERT_TRUE(runningLock1 != nullptr); 64 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false"; 65 runningLock1->Lock(); 66 ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true"; 67 runningLock1->UnLock(); 68 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false"; 69 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit003 1"); 70 // lock 50ms 71 runningLock1->Lock(50); 72 usleep(4000); 73 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit003 2"); 74 ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true"; 75 usleep(1000); 76 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit003 3"); 77 ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true"; 78 // wait 60ms 79 usleep(60000); 80 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false"; 81 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit003 end"); 82 } 83 84 /** 85 * @tc.name: RunningLockTest003 86 * @tc.desc: Test RunningLockInnerKit function, timeout lock. 87 * @tc.type: FUNC 88 */ 89 HWTEST_F (RunningLockTest, RunningLockTest003, TestSize.Level1) 90 { 91 if (false) { 92 auto& powerMgrClient = PowerMgrClient::GetInstance(); 93 auto runningLock1 = powerMgrClient.CreateRunningLock("runninglock005", 94 RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); 95 ASSERT_TRUE(runningLock1 != nullptr); 96 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false"; 97 // after 8ms unlock 98 runningLock1->Lock(30); 99 runningLock1->Lock(80); 100 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit005 1"); 101 usleep(50000); 102 ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true"; 103 usleep(50000); 104 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false"; 105 // no unlock 106 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit005 2"); 107 runningLock1->Lock(2); 108 runningLock1->Lock(3); 109 runningLock1->Lock(); 110 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit005 3"); 111 usleep(8000); 112 ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true"; 113 // after 3ms unlock 114 runningLock1->Lock(30); 115 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit005 4"); 116 usleep(50000); 117 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false"; 118 runningLock1->Lock(5); 119 runningLock1->UnLock(); 120 ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false"; 121 POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit005 5"); 122 } 123 } 124 125 /** 126 * @tc.name: RunningLockTest004 127 * @tc.desc: Test CreateRunningLock function. 128 * @tc.type: FUNC 129 * @tc.require: issueI6NWQD 130 */ 131 HWTEST_F (RunningLockTest, RunningLockTest004, TestSize.Level1) 132 { 133 auto& powerMgrClient = PowerMgrClient::GetInstance(); 134 std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock( 135 "background", RunningLockType::RUNNINGLOCK_BACKGROUND); 136 EXPECT_NE(runningLock, nullptr); 137 138 runningLock = powerMgrClient.CreateRunningLock("backgroudPhone", RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE); 139 EXPECT_NE(runningLock, nullptr); 140 141 runningLock = powerMgrClient.CreateRunningLock( 142 "backgroudNatification", RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION); 143 EXPECT_NE(runningLock, nullptr); 144 145 runningLock = powerMgrClient.CreateRunningLock("backgroudAudio", RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO); 146 EXPECT_NE(runningLock, nullptr); 147 148 runningLock = powerMgrClient.CreateRunningLock("backgroudSport", RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT); 149 EXPECT_NE(runningLock, nullptr); 150 151 runningLock = powerMgrClient.CreateRunningLock( 152 "backgroudNavigation", RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION); 153 EXPECT_NE(runningLock, nullptr); 154 155 runningLock = powerMgrClient.CreateRunningLock("backgroudTask", RunningLockType::RUNNINGLOCK_BACKGROUND_TASK); 156 EXPECT_NE(runningLock, nullptr); 157 158 runningLock = powerMgrClient.CreateRunningLock("butt", RunningLockType::RUNNINGLOCK_BUTT); 159 EXPECT_EQ(runningLock, nullptr); 160 } 161 162 /** 163 * @tc.name: RunningLockTest005 164 * @tc.desc: Test ProxyRunningLock function. 165 * @tc.type: FUNC 166 * @tc.require: issueI6S0YY 167 */ 168 HWTEST_F (RunningLockTest, RunningLockTest005, TestSize.Level1) 169 { 170 auto& powerMgrClient = PowerMgrClient::GetInstance(); 171 std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock( 172 "background.test005", RunningLockType::RUNNINGLOCK_BACKGROUND); 173 ASSERT_NE(runningLock, nullptr); 174 175 pid_t curUid = getuid(); 176 pid_t curPid = getpid(); 177 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, curUid)); 178 runningLock->Lock(); 179 EXPECT_FALSE(runningLock->IsUsed()); 180 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, curUid)); 181 } 182 183 /** 184 * @tc.name: RunningLockTest006 185 * @tc.desc: Test ProxyRunningLock function. 186 * @tc.type: FUNC 187 * @tc.require: issueI6S0YY 188 */ 189 HWTEST_F (RunningLockTest, RunningLockTest006, TestSize.Level1) 190 { 191 auto& powerMgrClient = PowerMgrClient::GetInstance(); 192 std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock( 193 "background.test006", RunningLockType::RUNNINGLOCK_BACKGROUND); 194 ASSERT_NE(runningLock, nullptr); 195 196 pid_t curUid = getuid(); 197 pid_t curPid = getpid(); 198 199 runningLock->Lock(); 200 EXPECT_TRUE(runningLock->IsUsed()); 201 202 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, curUid)); 203 EXPECT_FALSE(runningLock->IsUsed()); 204 205 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, curUid)); 206 EXPECT_TRUE(runningLock->IsUsed()); 207 } 208 209 /** 210 * @tc.name: RunningLockTest007 211 * @tc.desc: Test ProxyRunningLock function. 212 * @tc.type: FUNC 213 * @tc.require: issueI6S0YY 214 */ 215 HWTEST_F (RunningLockTest, RunningLockTest007, TestSize.Level1) 216 { 217 auto& powerMgrClient = PowerMgrClient::GetInstance(); 218 std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock( 219 "background.test007", RunningLockType::RUNNINGLOCK_BACKGROUND); 220 ASSERT_NE(runningLock, nullptr); 221 222 pid_t curUid = getuid(); 223 pid_t curPid = getpid(); 224 int32_t timeoutMs = 50; 225 226 runningLock->Lock(timeoutMs); 227 EXPECT_TRUE(runningLock->IsUsed()); 228 229 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, curUid)); 230 EXPECT_FALSE(runningLock->IsUsed()); 231 232 usleep(timeoutMs * US_PER_MS); 233 234 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, curUid)); 235 EXPECT_TRUE(runningLock->IsUsed()); 236 usleep(timeoutMs / 2 * US_PER_MS); 237 EXPECT_TRUE(runningLock->IsUsed()); 238 usleep(timeoutMs * US_PER_MS); 239 EXPECT_FALSE(runningLock->IsUsed()); 240 } 241 242 /** 243 * @tc.name: RunningLockTest008 244 * @tc.desc: Test ProxyRunningLock function, create runninglock first, then proxy 245 * @tc.type: FUNC 246 * @tc.require: issueI6S0YY 247 */ 248 HWTEST_F (RunningLockTest, RunningLockTest008, TestSize.Level1) 249 { 250 auto& powerMgrClient = PowerMgrClient::GetInstance(); 251 252 pid_t curUid = getuid(); 253 pid_t curPid = getpid(); 254 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, curUid)); 255 256 std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock( 257 "background.test008", RunningLockType::RUNNINGLOCK_BACKGROUND); 258 ASSERT_NE(runningLock, nullptr); 259 runningLock->Lock(); 260 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, curUid)); 261 } 262 263 /** 264 * @tc.name: RunningLockTest009 265 * @tc.desc: Test ProxyRunningLock function. 266 * @tc.type: FUNC 267 * @tc.require: issueI6S0YY 268 */ 269 HWTEST_F (RunningLockTest, RunningLockTest009, TestSize.Level1) 270 { 271 auto& powerMgrClient = PowerMgrClient::GetInstance(); 272 std::shared_ptr<RunningLock> screenRunningLock = powerMgrClient.CreateRunningLock( 273 "screen.test009", RunningLockType::RUNNINGLOCK_SCREEN); 274 ASSERT_NE(screenRunningLock, nullptr); 275 std::shared_ptr<RunningLock> proximityRunningLock = powerMgrClient.CreateRunningLock( 276 "proximity.test009", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); 277 ASSERT_NE(proximityRunningLock, nullptr); 278 279 pid_t curUid = getuid(); 280 pid_t curPid = getpid(); 281 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, curUid)); 282 283 screenRunningLock->Lock(); 284 EXPECT_FALSE(screenRunningLock->IsUsed()); 285 286 proximityRunningLock->Lock(); 287 EXPECT_FALSE(proximityRunningLock->IsUsed()); 288 289 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, curUid)); 290 } 291 292 /** 293 * @tc.name: RunningLockTest010 294 * @tc.desc: Test ProxyRunningLock function, pid is invalid 295 * @tc.type: FUNC 296 * @tc.require: issueI6S0YY 297 */ 298 HWTEST_F (RunningLockTest, RunningLockTest010, TestSize.Level1) 299 { 300 pid_t curUid = 1; 301 pid_t curPid = -1; 302 auto& powerMgrClient = PowerMgrClient::GetInstance(); 303 EXPECT_FALSE(powerMgrClient.ProxyRunningLock(true, curPid, curUid)); 304 EXPECT_FALSE(powerMgrClient.ProxyRunningLock(false, curPid, curUid)); 305 } 306 307 /** 308 * @tc.name: RunningLockTest011 309 * @tc.desc: Test RunningLockProxt AddRunningLock function 310 * @tc.type: FUNC 311 * @tc.require: issueI7405P 312 */ 313 HWTEST_F (RunningLockTest, RunningLockTest011, TestSize.Level1) 314 { 315 auto runninglockProxy = std::make_shared<RunningLockProxy>(); 316 pid_t pid = 1; 317 pid_t uid = -1; 318 sptr<IRemoteObject> remoteObj = new RunningLockTokenStub(); 319 runninglockProxy->AddRunningLock(pid, uid, remoteObj); 320 runninglockProxy->AddRunningLock(pid, uid, remoteObj); 321 auto ret = runninglockProxy->GetRemoteObjectList(pid, uid); 322 EXPECT_TRUE(!ret.empty()); 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 auto ret = runninglockProxy->GetRemoteObjectList(pid, uid); 341 EXPECT_TRUE(!ret.empty()); 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 auto ret = runninglockProxy->GetRemoteObjectList(pid, uid); 358 EXPECT_TRUE(ret.empty()); 359 } 360 361 /** 362 * @tc.name: RunningLockTest014 363 * @tc.desc: Test ProxyRunningLocks function 364 * @tc.type: FUNC 365 * @tc.require: issueI7MNRN 366 */ 367 HWTEST_F(RunningLockTest, RunningLockTest014, TestSize.Level1) 368 { 369 pid_t curUid = getuid(); 370 pid_t curPid = getpid(); 371 std::vector<std::pair<pid_t, pid_t>> processInfos; 372 processInfos.push_back(std::pair<pid_t, pid_t>(curPid, curUid)); 373 auto& powerMgrClient = PowerMgrClient::GetInstance(); 374 EXPECT_TRUE(powerMgrClient.ProxyRunningLocks(true, processInfos)); 375 } 376 } // namespace 377