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_spawn_client.h"
35 #include "mock_bundle_installer_service.h"
36 #include "mock_bundle_manager.h"
37 #include "mock_bundle_manager_service.h"
38 #include "mock_iapp_state_callback.h"
39 #include "mock_native_token.h"
40 #include "mock_system_ability_manager.h"
41 #include "param.h"
42 #include "permission_verification.h"
43 #include "refbase.h"
44 #include "system_ability_definition.h"
45
46 using namespace testing::ext;
47 using OHOS::iface_cast;
48 using OHOS::IRemoteObject;
49 using OHOS::sptr;
50 using testing::_;
51 using testing::Invoke;
52 using testing::InvokeWithoutArgs;
53 using testing::Return;
54 using testing::SetArgReferee;
55 using ::testing::DoAll;
56
57 namespace OHOS {
58 namespace AppExecFwk {
59 sptr<MockBundleInstallerService> mockBundleInstaller = new (std::nothrow) MockBundleInstallerService();
60 sptr<BundleMgrService> mockBundleMgr = new (std::nothrow) BundleMgrService();
61 class AppRunningProcessesInfoTest : public testing::Test {
62 public:
63 static void SetUpTestCase();
64 static void TearDownTestCase();
65 void SetUp();
66 void TearDown();
67 void MockBundleInstaller();
68 sptr<ISystemAbilityManager> iSystemAbilityMgr_ = nullptr;
69 sptr<AppExecFwk::MockSystemAbilityManager> mockSystemAbility_ = nullptr;
70 std::shared_ptr<AppExecFwk::BundleMgrHelper> bundleMgrClient =
71 DelayedSingleton<AppExecFwk::BundleMgrHelper>::GetInstance();
72
73 protected:
GetTestProcessName()74 static const std::string GetTestProcessName()
75 {
76 return "com.ohos.test.helloworld";
77 }
GetTestAppName()78 static const std::string GetTestAppName()
79 {
80 return "com.ohos.test.helloworld";
81 }
GetTestAbilityName()82 static const std::string GetTestAbilityName()
83 {
84 return "test_ability_name";
85 }
GetTestUid()86 static int GetTestUid()
87 {
88 // a valid inner uid value which is not border value.
89 const static int VALID_UID_VALUE = 1010;
90 return VALID_UID_VALUE;
91 }
92
93 std::shared_ptr<AppRunningRecord> GetTestAppRunningRecord();
94 sptr<IAppScheduler> GetMockedAppSchedulerClient() const;
95 std::shared_ptr<AppRunningRecord> StartLoadAbility(const sptr<IRemoteObject>& token,
96 const std::shared_ptr<AbilityInfo>& abilityInfo, const std::shared_ptr<ApplicationInfo>& appInfo,
97 const pid_t newPid) const;
GetMockToken() const98 sptr<MockAbilityToken> GetMockToken() const
99 {
100 return mock_token_;
101 }
102
103 protected:
104 std::shared_ptr<AbilityRunningRecord> testAbilityRecord_;
105 sptr<IAppScheduler> client_;
106 sptr<MockAppScheduler> mockAppSchedulerClient_;
107 std::shared_ptr<AppRunningRecord> testAppRecord_;
108 std::unique_ptr<AppMgrServiceInner> service_;
109 sptr<MockAbilityToken> mock_token_;
110 };
111
SetUpTestCase()112 void AppRunningProcessesInfoTest::SetUpTestCase()
113 {
114 MockNativeToken::SetNativeToken();
115 }
116
TearDownTestCase()117 void AppRunningProcessesInfoTest::TearDownTestCase()
118 {}
119
SetUp()120 void AppRunningProcessesInfoTest::SetUp()
121 {
122 mockAppSchedulerClient_ = new (std::nothrow) MockAppScheduler();
123 service_.reset(new (std::nothrow) AppMgrServiceInner());
124 SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = mockSystemAbility_;
125 service_->SetBundleManagerHelper(bundleMgrClient);
126 }
127
TearDown()128 void AppRunningProcessesInfoTest::TearDown()
129 {
130 testAbilityRecord_.reset();
131 testAppRecord_.reset();
132 SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = iSystemAbilityMgr_;
133 }
134
MockBundleInstaller()135 void AppRunningProcessesInfoTest::MockBundleInstaller()
136 {
137 auto mockGetBundleInstaller = [] () {
138 return mockBundleInstaller;
139 };
140 auto mockGetSystemAbility = [bms = mockBundleMgr, saMgr = iSystemAbilityMgr_] (int32_t systemAbilityId) {
141 if (systemAbilityId == BUNDLE_MGR_SERVICE_SYS_ABILITY_ID) {
142 return bms->AsObject();
143 } else {
144 return saMgr->GetSystemAbility(systemAbilityId);
145 }
146 };
147 EXPECT_CALL(*mockBundleMgr, GetBundleInstaller()).Times(1).WillOnce(testing::Invoke(mockGetBundleInstaller));
148 EXPECT_CALL(*mockSystemAbility_, GetSystemAbility(testing::_)).WillOnce(testing::Invoke(mockGetSystemAbility));
149 }
150
GetMockedAppSchedulerClient() const151 sptr<IAppScheduler> AppRunningProcessesInfoTest::GetMockedAppSchedulerClient() const
152 {
153 if (client_) {
154 return client_;
155 }
156 return nullptr;
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 record = service_->CreateAppRunningRecord(
222 GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr, 0);
223 EXPECT_TRUE(record != nullptr);
224 record->SetState(ApplicationState::APP_STATE_FOREGROUND);
225 record->SetApplicationClient(GetMockedAppSchedulerClient());
226 AppExecFwk::RunningProcessInfo info;
227 sptr<IRemoteObject> token;
228 service_->GetRunningProcessInfoByToken(token, info);
229 EXPECT_TRUE(service_ != nullptr);
230 }
231
232 /*
233 * Feature: AppMgrServiceInner
234 * Function: GetAllRunningProcesses
235 * SubFunction: NA
236 * FunctionPoints: get running process info by token.
237 * EnvConditions: NA
238 * CaseDescription: creat apprunningrecord, set record state, call query function.
239 */
240 HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_002, TestSize.Level1)
241 {
242 auto abilityInfo = std::make_shared<AbilityInfo>();
243 int uid = 0;
244 abilityInfo->name = GetTestAbilityName();
245 abilityInfo->applicationInfo.uid = uid;
246 auto appInfo = std::make_shared<ApplicationInfo>();
247 appInfo->name = GetTestAppName();
248 appInfo->uid = uid;
249 BundleInfo bundleInfo;
250 HapModuleInfo hapModuleInfo;
251 EXPECT_TRUE(service_ != nullptr);
252 auto record = service_->CreateAppRunningRecord(
253 GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr, 0);
254 EXPECT_TRUE(record != nullptr);
255
256 record->SetUid(uid);
257 EXPECT_TRUE(record != nullptr) << ",create apprunningrecord fail!";
258
259 sptr<MockApplication> mockApplication(new MockApplication());
260 sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
261 record->SetApplicationClient(client);
262 EXPECT_CALL(*mockApplication, ScheduleLaunchApplication(_, _))
263 .Times(1)
264 .WillOnce(Invoke(mockApplication.GetRefPtr(), &MockApplication::LaunchApplication));
265 Configuration config;
266 record->LaunchApplication(config);
267 mockApplication->Wait();
268
269 EXPECT_CALL(*mockApplication, ScheduleForegroundApplication())
270 .Times(1)
__anonbed7ece40302() 271 .WillOnce([mockApplication]() {
272 mockApplication->Post();
273 return true;
274 });
275 // application enter in foreground and check the result
276 record->ScheduleForegroundRunning();
277 mockApplication->Wait();
278
279 // update application state and check the state
280 record->SetState(ApplicationState::APP_STATE_FOREGROUND);
281 auto newRecord = service_->appRunningManager_->CheckAppRunningRecordIsExist(
282 appInfo->name, GetTestProcessName(), appInfo->uid, bundleInfo);
283 EXPECT_TRUE(newRecord);
284 newRecord->SetUid(uid);
285 auto stateFromRec = newRecord->GetState();
286 EXPECT_EQ(stateFromRec, ApplicationState::APP_STATE_FOREGROUND);
287
288 std::vector<RunningProcessInfo> info;
289 size_t infoCount{ 1 };
290 record->SetSpawned();
291 auto res = service_->GetAllRunningProcesses(info);
292 EXPECT_TRUE(res == ERR_OK);
293 }
294
295 /*
296 * Feature: AppMgrServiceInner
297 * Function: GetRunningProcessInfoByToken
298 * SubFunction: NA
299 * FunctionPoints: get running process info by token.
300 * EnvConditions: NA
301 * CaseDescription: creat apprunningrecords, set record state, call query function.
302 */
303 HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_004, TestSize.Level1)
304 {
305 auto abilityInfo = std::make_shared<AbilityInfo>();
306 abilityInfo->name = GetTestAbilityName();
307 auto appInfo = std::make_shared<ApplicationInfo>();
308 appInfo->name = GetTestAppName();
309 BundleInfo bundleInfo;
310 bundleInfo.appId = "com.ohos.test.helloworld_code123";
311 bundleInfo.jointUserId = "joint456";
312 HapModuleInfo hapModuleInfo;
313 hapModuleInfo.moduleName = "module789";
314 EXPECT_TRUE(service_ != nullptr);
315 auto record = service_->CreateAppRunningRecord(
316 GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr, 0);
317 EXPECT_TRUE(record != nullptr);
318 record->SetState(ApplicationState::APP_STATE_BACKGROUND);
319 record->SetApplicationClient(GetMockedAppSchedulerClient());
320 RunningProcessInfo info;
321 service_->appRunningManager_->GetRunningProcessInfoByToken(GetMockToken(), info);
322 EXPECT_TRUE(service_ != nullptr);
323 }
324
325 /*
326 * Feature: AppMgrServiceInner
327 * Function: GetRunningProcessInfoByPid
328 * SubFunction: NA
329 * FunctionPoints: get running process info by pid.
330 * EnvConditions: NA
331 * CaseDescription: creat apprunningrecords, set record state, call query function.
332 */
333 HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_005, TestSize.Level1)
334 {
335 auto abilityInfo = std::make_shared<AbilityInfo>();
336 abilityInfo->name = GetTestAbilityName();
337 auto appInfo = std::make_shared<ApplicationInfo>();
338 appInfo->name = GetTestAppName();
339 BundleInfo bundleInfo;
340 bundleInfo.appId = "com.ohos.test.helloworld_code123";
341 bundleInfo.jointUserId = "joint456";
342 HapModuleInfo hapModuleInfo;
343 hapModuleInfo.moduleName = "module789";
344 EXPECT_TRUE(service_ != nullptr);
345 auto record = service_->CreateAppRunningRecord(
346 GetMockToken(), nullptr, appInfo, abilityInfo, GetTestProcessName(), bundleInfo, hapModuleInfo, nullptr, 0);
347 EXPECT_TRUE(record != nullptr);
348 record->SetState(ApplicationState::APP_STATE_BACKGROUND);
349 record->SetApplicationClient(GetMockedAppSchedulerClient());
350 pid_t pid = 16738;
351 record->GetPriorityObject()->SetPid(pid);
352 RunningProcessInfo info;
353 service_->appRunningManager_->GetRunningProcessInfoByPid(pid, info);
354 EXPECT_TRUE(info.processName_ == GetTestProcessName());
355 }
356
357 } // namespace AppExecFwk
358 } // namespace OHOS
359