1 /*
2 * Copyright (c) 2022 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 #include <gtest/gtest.h>
16 #include <limits>
17
18 #define private public
19 #include "app_mgr_service_inner.h"
20 #include "iservice_registry.h"
21 #undef private
22 #include "ability_info.h"
23 #include "ability_running_record.h"
24 #include "application_info.h"
25 #include "app_record_id.h"
26 #include "app_scheduler_host.h"
27 #include "bundle_mgr_interface.h"
28 #include "iremote_object.h"
29 #include "iservice_registry.h"
30 #include "mock_ability_token.h"
31 #include "mock_application.h"
32 #include "mock_app_mgr_service_inner.h"
33 #include "mock_app_scheduler.h"
34 #include "mock_app_scheduler_client.h"
35 #include "mock_application_proxy.h"
36 #include "mock_app_spawn_client.h"
37 #include "mock_bundle_installer_service.h"
38 #include "mock_bundle_manager.h"
39 #include "mock_bundle_manager_service.h"
40 #include "mock_iapp_state_callback.h"
41 #include "mock_native_token.h"
42 #include "mock_system_ability_manager.h"
43 #include "param.h"
44 #include "permission_verification.h"
45 #include "refbase.h"
46 #include "system_ability_definition.h"
47
48 using namespace testing::ext;
49 using OHOS::iface_cast;
50 using OHOS::IRemoteObject;
51 using OHOS::sptr;
52 using testing::_;
53 using testing::Invoke;
54 using testing::InvokeWithoutArgs;
55 using testing::Return;
56 using testing::SetArgReferee;
57 using ::testing::DoAll;
58
59 namespace OHOS {
60 namespace AppExecFwk {
61 sptr<MockBundleInstallerService> mockBundleInstaller = new (std::nothrow) MockBundleInstallerService();
62 sptr<BundleMgrService> mockBundleMgr = new (std::nothrow) BundleMgrService();
63 class AppRunningProcessesInfoTest : public testing::Test {
64 public:
65 static void SetUpTestCase();
66 static void TearDownTestCase();
67 void SetUp();
68 void TearDown();
69 void MockBundleInstaller();
70 sptr<ISystemAbilityManager> iSystemAbilityMgr_ = nullptr;
71 sptr<AppExecFwk::MockSystemAbilityManager> mockSystemAbility_ = nullptr;
72 std::shared_ptr<AppExecFwk::BundleMgrHelper> bundleMgrClient =
73 DelayedSingleton<AppExecFwk::BundleMgrHelper>::GetInstance();
74
75 protected:
GetTestProcessName()76 static const std::string GetTestProcessName()
77 {
78 return "com.ohos.test.helloworld";
79 }
GetTestAppName()80 static const std::string GetTestAppName()
81 {
82 return "com.ohos.test.helloworld";
83 }
GetTestAbilityName()84 static const std::string GetTestAbilityName()
85 {
86 return "test_ability_name";
87 }
GetTestUid()88 static int GetTestUid()
89 {
90 // a valid inner uid value which is not border value.
91 const static int VALID_UID_VALUE = 1010;
92 return VALID_UID_VALUE;
93 }
94
95 std::shared_ptr<AppRunningRecord> GetTestAppRunningRecord();
96 sptr<IAppScheduler> GetMockedAppSchedulerClient() const;
97 std::shared_ptr<AppRunningRecord> StartLoadAbility(const sptr<IRemoteObject>& token,
98 const std::shared_ptr<AbilityInfo>& abilityInfo, const std::shared_ptr<ApplicationInfo>& appInfo,
99 const pid_t newPid) const;
GetMockToken() const100 sptr<MockAbilityToken> GetMockToken() const
101 {
102 return mock_token_;
103 }
104
105 protected:
106 std::shared_ptr<AbilityRunningRecord> testAbilityRecord_;
107 sptr<IAppScheduler> client_;
108 sptr<MockAppSchedulerClient> mockAppSchedulerClient_;
109 std::shared_ptr<AppRunningRecord> testAppRecord_;
110 std::unique_ptr<AppMgrServiceInner> service_;
111 sptr<MockAbilityToken> mock_token_;
112 };
113
SetUpTestCase()114 void AppRunningProcessesInfoTest::SetUpTestCase()
115 {
116 MockNativeToken::SetNativeToken();
117 }
118
TearDownTestCase()119 void AppRunningProcessesInfoTest::TearDownTestCase()
120 {}
121
SetUp()122 void AppRunningProcessesInfoTest::SetUp()
123 {
124 sptr<IRemoteObject> impl = nullptr;
125 mockAppSchedulerClient_ = sptr<MockAppSchedulerClient>::MakeSptr(impl);
126 service_.reset(new (std::nothrow) AppMgrServiceInner());
127 SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = mockSystemAbility_;
128 service_->SetBundleManagerHelper(bundleMgrClient);
129 }
130
TearDown()131 void AppRunningProcessesInfoTest::TearDown()
132 {
133 testAbilityRecord_.reset();
134 testAppRecord_.reset();
135 SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = iSystemAbilityMgr_;
136 }
137
MockBundleInstaller()138 void AppRunningProcessesInfoTest::MockBundleInstaller()
139 {
140 auto mockGetBundleInstaller = [] () {
141 return mockBundleInstaller;
142 };
143 auto mockGetSystemAbility = [bms = mockBundleMgr, saMgr = iSystemAbilityMgr_] (int32_t systemAbilityId) {
144 if (systemAbilityId == BUNDLE_MGR_SERVICE_SYS_ABILITY_ID) {
145 return bms->AsObject();
146 } else {
147 return saMgr->GetSystemAbility(systemAbilityId);
148 }
149 };
150 EXPECT_CALL(*mockBundleMgr, GetBundleInstaller()).Times(1).WillOnce(testing::Invoke(mockGetBundleInstaller));
151 EXPECT_CALL(*mockSystemAbility_, GetSystemAbility(testing::_)).WillOnce(testing::Invoke(mockGetSystemAbility));
152 }
153
GetMockedAppSchedulerClient() const154 sptr<IAppScheduler> AppRunningProcessesInfoTest::GetMockedAppSchedulerClient() const
155 {
156 return mockAppSchedulerClient_;
157 }
158
GetTestAppRunningRecord()159 std::shared_ptr<AppRunningRecord> AppRunningProcessesInfoTest::GetTestAppRunningRecord()
160 {
161 if (!testAppRecord_) {
162 auto appInfo = std::make_shared<ApplicationInfo>();
163 appInfo->name = GetTestAppName();
164 testAppRecord_ = std::make_shared<AppRunningRecord>(appInfo, AppRecordId::Create(), GetTestProcessName());
165 testAppRecord_->SetApplicationClient(GetMockedAppSchedulerClient());
166 auto abilityInfo = std::make_shared<AbilityInfo>();
167 abilityInfo->name = GetTestAbilityName();
168 HapModuleInfo hapModuleInfo;
169 hapModuleInfo.moduleName = "module789";
170 testAppRecord_->AddModule(appInfo, abilityInfo, GetMockToken(), hapModuleInfo, nullptr, 0);
171 }
172 return testAppRecord_;
173 }
174
StartLoadAbility(const sptr<IRemoteObject> & token,const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<ApplicationInfo> & appInfo,const pid_t newPid) const175 std::shared_ptr<AppRunningRecord> AppRunningProcessesInfoTest::StartLoadAbility(const sptr<IRemoteObject>& token,
176 const std::shared_ptr<AbilityInfo>& abilityInfo, const std::shared_ptr<ApplicationInfo>& appInfo,
177 const pid_t newPid) const
178 {
179 std::shared_ptr<MockAppSpawnClient> mockClientPtr = std::make_shared<MockAppSpawnClient>();
180 service_->SetAppSpawnClient(mockClientPtr);
181 EXPECT_CALL(*mockClientPtr, StartProcess(_, _)).Times(1).WillOnce(DoAll(SetArgReferee<1>(newPid), Return(ERR_OK)));
182 AbilityRuntime::LoadParam loadParam;
183 loadParam.token = token;
184 auto loadParamPtr = std::make_shared<AbilityRuntime::LoadParam>(loadParam);
185 service_->LoadAbility(abilityInfo, appInfo, nullptr, loadParamPtr);
186
187 BundleInfo bundleInfo;
188 bundleInfo.appId = "com.ohos.test.helloworld_code123";
189
190 auto record = service_->appRunningManager_->CheckAppRunningRecordIsExist(
191 appInfo->name, GetTestProcessName(), appInfo->uid, bundleInfo);
192
193 EXPECT_TRUE(record);
194 auto clent = GetMockedAppSchedulerClient();
195 record->SetApplicationClient(clent);
196 EXPECT_EQ(record->GetPriorityObject()->GetPid(), newPid);
197 EXPECT_NE(record->GetApplicationClient(), nullptr);
198 return record;
199 }
200
201 /*
202 * Feature: AppMgrServiceInner
203 * Function: GetRunningProcessInfoByToken
204 * SubFunction: NA
205 * FunctionPoints: get running process info by token.
206 * EnvConditions: NA
207 * CaseDescription: creat apprunningrecord, set record state, call query function.
208 */
209 HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_001, TestSize.Level1)
210 {
211 auto abilityInfo = std::make_shared<AbilityInfo>();
212 abilityInfo->name = GetTestAbilityName();
213 auto appInfo = std::make_shared<ApplicationInfo>();
214 appInfo->name = GetTestAppName();
215 BundleInfo bundleInfo;
216 bundleInfo.appId = "com.ohos.test.helloworld_code123";
217 bundleInfo.jointUserId = "joint456";
218 HapModuleInfo hapModuleInfo;
219 hapModuleInfo.moduleName = "module789";
220 EXPECT_TRUE(service_ != nullptr);
221 auto loadParam = std::make_shared<AbilityRuntime::LoadParam>();
222 loadParam->token = GetMockToken();
223 auto record = service_->CreateAppRunningRecord(
224 loadParam, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr);
225 EXPECT_TRUE(record != nullptr);
226 record->SetState(ApplicationState::APP_STATE_FOREGROUND);
227 record->SetApplicationClient(GetMockedAppSchedulerClient());
228 AppExecFwk::RunningProcessInfo info;
229 sptr<IRemoteObject> token;
230 service_->GetRunningProcessInfoByToken(token, info);
231 EXPECT_TRUE(service_ != nullptr);
232 }
233
234 /*
235 * Feature: AppMgrServiceInner
236 * Function: GetAllRunningProcesses
237 * SubFunction: NA
238 * FunctionPoints: get running process info by token.
239 * EnvConditions: NA
240 * CaseDescription: creat apprunningrecord, set record state, call query function.
241 */
242 HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_002, TestSize.Level1)
243 {
244 auto abilityInfo = std::make_shared<AbilityInfo>();
245 int uid = 0;
246 abilityInfo->name = GetTestAbilityName();
247 abilityInfo->applicationInfo.uid = uid;
248 auto appInfo = std::make_shared<ApplicationInfo>();
249 appInfo->name = GetTestAppName();
250 appInfo->uid = uid;
251 BundleInfo bundleInfo;
252 HapModuleInfo hapModuleInfo;
253 EXPECT_TRUE(service_ != nullptr);
254 auto loadParam = std::make_shared<AbilityRuntime::LoadParam>();
255 loadParam->token = GetMockToken();
256 auto record = service_->CreateAppRunningRecord(
257 loadParam, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr);
258 EXPECT_TRUE(record != nullptr);
259
260 record->SetUid(uid);
261 EXPECT_TRUE(record != nullptr) << ",create apprunningrecord fail!";
262
263 sptr<IRemoteObject> impl = nullptr;
264 sptr<MockApplicationProxy> mockApplication = new MockApplicationProxy(impl);
265 record->SetApplicationClient(mockApplication);
266 EXPECT_CALL(*mockApplication, ScheduleLaunchApplication(_, _))
267 .Times(1)
268 .WillOnce(Invoke(mockApplication.GetRefPtr(), &MockApplicationProxy::LaunchApplication));
269 Configuration config;
270 record->LaunchApplication(config);
271 mockApplication->Wait();
272
273 EXPECT_CALL(*mockApplication, ScheduleForegroundApplication())
274 .Times(1)
__anon086f6cec0302() 275 .WillOnce([mockApplication]() {
276 mockApplication->Post();
277 return true;
278 });
279 // application enter in foreground and check the result
280 record->ScheduleForegroundRunning();
281 mockApplication->Wait();
282
283 // update application state and check the state
284 record->SetState(ApplicationState::APP_STATE_FOREGROUND);
285 auto newRecord = service_->appRunningManager_->CheckAppRunningRecordIsExist(
286 appInfo->name, GetTestProcessName(), appInfo->uid, bundleInfo);
287 EXPECT_TRUE(newRecord);
288 newRecord->SetUid(uid);
289 auto stateFromRec = newRecord->GetState();
290 EXPECT_EQ(stateFromRec, ApplicationState::APP_STATE_FOREGROUND);
291
292 std::vector<RunningProcessInfo> info;
293 size_t infoCount{ 1 };
294 record->SetSpawned();
295 auto res = service_->GetAllRunningProcesses(info);
296 EXPECT_TRUE(res == ERR_OK);
297 }
298
299 /*
300 * Feature: AppMgrServiceInner
301 * Function: GetRunningProcessInfoByToken
302 * SubFunction: NA
303 * FunctionPoints: get running process info by token.
304 * EnvConditions: NA
305 * CaseDescription: creat apprunningrecords, set record state, call query function.
306 */
307 HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_004, TestSize.Level1)
308 {
309 auto abilityInfo = std::make_shared<AbilityInfo>();
310 abilityInfo->name = GetTestAbilityName();
311 auto appInfo = std::make_shared<ApplicationInfo>();
312 appInfo->name = GetTestAppName();
313 BundleInfo bundleInfo;
314 bundleInfo.appId = "com.ohos.test.helloworld_code123";
315 bundleInfo.jointUserId = "joint456";
316 HapModuleInfo hapModuleInfo;
317 hapModuleInfo.moduleName = "module789";
318 EXPECT_TRUE(service_ != nullptr);
319 auto loadParam = std::make_shared<AbilityRuntime::LoadParam>();
320 loadParam->token = GetMockToken();
321 auto record = service_->CreateAppRunningRecord(
322 loadParam, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr);
323 EXPECT_TRUE(record != nullptr);
324 record->SetState(ApplicationState::APP_STATE_BACKGROUND);
325 record->SetApplicationClient(GetMockedAppSchedulerClient());
326 RunningProcessInfo info;
327 service_->appRunningManager_->GetRunningProcessInfoByToken(GetMockToken(), info);
328 EXPECT_TRUE(service_ != nullptr);
329 }
330
331 /*
332 * Feature: AppMgrServiceInner
333 * Function: GetRunningProcessInfoByPid
334 * SubFunction: NA
335 * FunctionPoints: get running process info by pid.
336 * EnvConditions: NA
337 * CaseDescription: creat apprunningrecords, set record state, call query function.
338 */
339 HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_005, TestSize.Level1)
340 {
341 auto abilityInfo = std::make_shared<AbilityInfo>();
342 abilityInfo->name = GetTestAbilityName();
343 auto appInfo = std::make_shared<ApplicationInfo>();
344 appInfo->name = GetTestAppName();
345 BundleInfo bundleInfo;
346 bundleInfo.appId = "com.ohos.test.helloworld_code123";
347 bundleInfo.jointUserId = "joint456";
348 HapModuleInfo hapModuleInfo;
349 hapModuleInfo.moduleName = "module789";
350 EXPECT_TRUE(service_ != nullptr);
351 auto loadParam = std::make_shared<AbilityRuntime::LoadParam>();
352 loadParam->token = GetMockToken();
353 auto record = service_->CreateAppRunningRecord(
354 loadParam, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr);
355 EXPECT_TRUE(record != nullptr);
356 record->SetState(ApplicationState::APP_STATE_BACKGROUND);
357 record->SetApplicationClient(GetMockedAppSchedulerClient());
358 pid_t pid = 16738;
359 record->GetPriorityObject()->SetPid(pid);
360 RunningProcessInfo info;
361 service_->appRunningManager_->GetRunningProcessInfoByPid(pid, info);
362 EXPECT_TRUE(info.processName_ == GetTestProcessName());
363 }
364
365 } // namespace AppExecFwk
366 } // namespace OHOS
367