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_scenario_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: RunningLockScenarioTest019
38  * @tc.desc: Test runninglock single app scenario
39  * @tc.type: FUNC
40  * @tc.require
41  */
42 HWTEST_F (RunningLockScenarioTest, RunningLockScenarioTest019, TestSize.Level1)
43 {
44     auto& powerMgrClient = PowerMgrClient::GetInstance();
45 
46     pid_t curUid = getuid();
47     pid_t curPid = getpid();
48 
49     std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock(
50         "background.test019", RunningLockType::RUNNINGLOCK_BACKGROUND);
51     ASSERT_NE(runningLock, nullptr);
52     runningLock->Lock();
53 
54     std::vector<int32_t> workSource { app0Uid };
55     EXPECT_TRUE(runningLock->UpdateWorkSource(workSource) == 0);
56     EXPECT_TRUE(runningLock->IsUsed());
57 
58     runningLock->UnLock();
59     EXPECT_FALSE(runningLock->IsUsed());
60 }
61 
62 /**
63  * @tc.name: RunningLockScenarioTest020
64  * @tc.desc: Test runninglock single app scenario with proxy
65  * @tc.type: FUNC
66  * @tc.require
67  */
68 HWTEST_F (RunningLockScenarioTest, RunningLockScenarioTest020, TestSize.Level1)
69 {
70     auto& powerMgrClient = PowerMgrClient::GetInstance();
71 
72     pid_t curUid = getuid();
73     pid_t curPid = getpid();
74 
75     std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock(
76         "background.test020", RunningLockType::RUNNINGLOCK_BACKGROUND);
77     ASSERT_NE(runningLock, nullptr);
78     runningLock->Lock();
79     // open app
80     std::vector<int32_t> workSource1 { app0Uid };
81     EXPECT_TRUE(runningLock->UpdateWorkSource(workSource1) == 0);
82     usleep(100*1000);
83     EXPECT_TRUE(runningLock->IsUsed());
84     // freeze app
85     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, app0Uid));
86     EXPECT_FALSE(runningLock->IsUsed());
87     // thaw app
88     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, app0Uid));
89     EXPECT_TRUE(runningLock->IsUsed());
90     // close app
91     std::vector<int32_t> workSource0 {};
92     EXPECT_TRUE(runningLock->UpdateWorkSource(workSource0) == 0);
93     usleep(100*1000);
94     EXPECT_TRUE(runningLock->IsUsed());
95 
96     runningLock->UnLock();
97     EXPECT_FALSE(runningLock->IsUsed());
98 }
99 
100 /**
101  * @tc.name: RunningLockScenarioTest021
102  * @tc.desc: Test runninglock multi apps scenario with proxy
103  * @tc.type: FUNC
104  * @tc.require
105  */
106 HWTEST_F (RunningLockScenarioTest, RunningLockScenarioTest021, TestSize.Level1)
107 {
108     auto& powerMgrClient = PowerMgrClient::GetInstance();
109 
110     pid_t curUid = getuid();
111     pid_t curPid = getpid();
112 
113     std::vector<int32_t> workSource00 {};
114     std::vector<int32_t> workSource10 { app0Uid };
115     std::vector<int32_t> workSource01 { app1Uid };
116     std::vector<int32_t> workSource11 { app0Uid, app1Uid };
117 
118     std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock(
119         "background.test021", RunningLockType::RUNNINGLOCK_BACKGROUND);
120     ASSERT_NE(runningLock, nullptr);
121     runningLock->Lock();
122     // open app0
123     EXPECT_TRUE(runningLock->UpdateWorkSource(workSource10) == 0);
124     EXPECT_TRUE(runningLock->IsUsed());
125     // open app1
126     EXPECT_TRUE(runningLock->UpdateWorkSource(workSource11) == 0);
127     EXPECT_TRUE(runningLock->IsUsed());
128     // freeze app0
129     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, app0Uid));
130     EXPECT_TRUE(runningLock->IsUsed());
131     // freeze app1
132     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, app1Uid));
133     EXPECT_FALSE(runningLock->IsUsed());
134     // thaw app0
135     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, app0Uid));
136     EXPECT_TRUE(runningLock->IsUsed());
137     // thaw app1
138     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, app1Uid));
139     EXPECT_TRUE(runningLock->IsUsed());
140     // close app0
141     EXPECT_TRUE(runningLock->UpdateWorkSource(workSource01) == 0);
142     EXPECT_TRUE(runningLock->IsUsed());
143     // close app1
144     EXPECT_TRUE(runningLock->UpdateWorkSource(workSource00) == 0);
145     EXPECT_TRUE(runningLock->IsUsed());
146 
147     runningLock->UnLock();
148     EXPECT_FALSE(runningLock->IsUsed());
149 }
150 
151 /**
152  * @tc.name: RunningLockScenarioTest022
153  * @tc.desc: Test runninglock multi apps scenario with proxy
154  * @tc.type: FUNC
155  * @tc.require
156  */
157 HWTEST_F (RunningLockScenarioTest, RunningLockScenarioTest022, TestSize.Level1)
158 {
159     auto& powerMgrClient = PowerMgrClient::GetInstance();
160 
161     pid_t curUid = getuid();
162     pid_t curPid = getpid();
163 
164     std::vector<int32_t> workSource00 {};
165     std::vector<int32_t> workSource10 { app0Uid };
166     std::vector<int32_t> workSource11 { app0Uid, app1Uid };
167 
168     std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock(
169         "background.test022", RunningLockType::RUNNINGLOCK_BACKGROUND);
170     ASSERT_NE(runningLock, nullptr);
171     runningLock->Lock();
172     // open app0
173     EXPECT_TRUE(runningLock->UpdateWorkSource(workSource10) == 0);
174     usleep(100*1000);
175     EXPECT_TRUE(runningLock->IsUsed());
176     // freeze app0
177     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, app0Uid));
178     EXPECT_FALSE(runningLock->IsUsed());
179     // open app1
180     EXPECT_TRUE(runningLock->UpdateWorkSource(workSource11) == 0);
181     usleep(100*1000);
182     EXPECT_TRUE(runningLock->IsUsed());
183     // close app1
184     EXPECT_TRUE(runningLock->UpdateWorkSource(workSource10) == 0);
185     usleep(100*1000);
186     EXPECT_FALSE(runningLock->IsUsed());
187     // thaw app0
188     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, app0Uid));
189     EXPECT_TRUE(runningLock->IsUsed());
190     // close app0
191     EXPECT_TRUE(runningLock->UpdateWorkSource(workSource00) == 0);
192     usleep(100*1000);
193     EXPECT_TRUE(runningLock->IsUsed());
194 
195     runningLock->UnLock();
196     EXPECT_FALSE(runningLock->IsUsed());
197 }
198 } // namespace