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 #include <gtest/gtest.h>
16 #define private public
17 #include "app_death_recipient.h"
18 #include "app_mgr_service_inner.h"
19 #include "iservice_registry.h"
20 #undef private
21
22 #include "hilog_wrapper.h"
23 #include "iremote_object.h"
24 #include "mock_ability_token.h"
25 #include "mock_app_scheduler.h"
26 #include "mock_app_spawn_client.h"
27 #include "mock_bundle_installer_service.h"
28 #include "mock_bundle_manager_service.h"
29 #include "mock_system_ability_manager.h"
30 #include "singleton.h"
31
32 using namespace testing::ext;
33 using testing::_;
34 using testing::Return;
35 using testing::SetArgReferee;
36 using ::testing::DoAll;
37
38 namespace OHOS {
39 namespace AppExecFwk {
40 namespace {
41 constexpr int32_t BUNDLE_MGR_SERVICE_SYS_ABILITY_ID = 401;
42 sptr<MockBundleInstallerService> mockBundleInstaller = new (std::nothrow) MockBundleInstallerService();
43 sptr<MockBundleManagerService> mockBundleMgr = new (std::nothrow) MockBundleManagerService();
44 } // namespace
45 class AppDeathRecipientTest : public testing::Test {
46 public:
47 static void SetUpTestCase();
48 static void TearDownTestCase();
49 void SetUp();
50 void TearDown();
51 void MockBundleInstallerAndSA() const;
52 sptr<ISystemAbilityManager> iSystemAbilityMgr_ = nullptr;
53 sptr<AppExecFwk::MockSystemAbilityManager> mockSystemAbility_ = nullptr;
54
55 public:
56 const std::shared_ptr<AbilityInfo> GetAbilityInfoByIndex(const int32_t index) const;
57 const std::shared_ptr<ApplicationInfo> GetApplicationByIndex(const int32_t index) const;
58 const std::shared_ptr<AppRunningRecord> GetAppRunningRecordByIndex(const int32_t index) const;
59 sptr<IRemoteObject> GetApp(int32_t pid, int size);
60
61 public:
62 std::shared_ptr<AAFwk::TaskHandlerWrap> handler_;
63 std::shared_ptr<AppMgrServiceInner> appMgrServiceInner_;
64 sptr<AppDeathRecipient> appDeathRecipientObject_;
65 OHOS::sptr<MockAbilityToken> mockToken_;
66 };
67
WaitUntilTaskFinished(std::shared_ptr<AAFwk::TaskHandlerWrap> handler)68 static void WaitUntilTaskFinished(std::shared_ptr<AAFwk::TaskHandlerWrap> handler)
69 {
70 if (!handler) {
71 return;
72 }
73
74 const uint32_t MAX_RETRY_COUNT = 1000;
75 const uint32_t SLEEP_TIME = 1000;
76 uint32_t count = 0;
77 std::atomic<bool> taskCalled(false);
78 auto f = [&taskCalled]() { taskCalled.store(true); };
79 if (handler->SubmitTask(f)) {
80 while (!taskCalled.load()) {
81 ++count;
82 // if delay more than 1 second, break
83 if (count >= MAX_RETRY_COUNT) {
84 break;
85 }
86
87 usleep(SLEEP_TIME);
88 }
89 }
90 }
91
SetUpTestCase()92 void AppDeathRecipientTest::SetUpTestCase()
93 {}
94
TearDownTestCase()95 void AppDeathRecipientTest::TearDownTestCase()
96 {}
97
SetUp()98 void AppDeathRecipientTest::SetUp()
99 {
100 appMgrServiceInner_ = std::make_shared<AppMgrServiceInner>();
101 appMgrServiceInner_->Init();
102
103 handler_ = AAFwk::TaskHandlerWrap::CreateQueueHandler("AppDeathRecipientTest");
104
105 appDeathRecipientObject_ = new (std::nothrow) AppDeathRecipient();
106 mockSystemAbility_ = new (std::nothrow) AppExecFwk::MockSystemAbilityManager();
107 iSystemAbilityMgr_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
108 SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = mockSystemAbility_;
109 }
110
MockBundleInstallerAndSA() const111 void AppDeathRecipientTest::MockBundleInstallerAndSA() const
112 {
113 auto mockGetBundleInstaller = []() { return mockBundleInstaller; };
114 auto mockGetSystemAbility = [&](int32_t systemAbilityId) {
115 if (systemAbilityId == BUNDLE_MGR_SERVICE_SYS_ABILITY_ID) {
116 return mockBundleMgr->AsObject();
117 } else {
118 return iSystemAbilityMgr_->GetSystemAbility(systemAbilityId);
119 }
120 };
121 EXPECT_CALL(*mockBundleMgr, GetBundleInstaller()).WillOnce(testing::Invoke(mockGetBundleInstaller));
122 EXPECT_CALL(*mockSystemAbility_, GetSystemAbility(testing::_))
123 .WillOnce(testing::Invoke(mockGetSystemAbility))
124 .WillRepeatedly(testing::Invoke(mockGetSystemAbility));
125 }
126
TearDown()127 void AppDeathRecipientTest::TearDown()
128 {
129 SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = iSystemAbilityMgr_;
130 }
131
GetAbilityInfoByIndex(const int32_t index) const132 const std::shared_ptr<AbilityInfo> AppDeathRecipientTest::GetAbilityInfoByIndex(const int32_t index) const
133 {
134 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
135 abilityInfo->name = "AppDeathRecipientTest_ability" + std::to_string(index);
136 abilityInfo->applicationName = "com.ohos.test.helloworld" + std::to_string(index);
137 abilityInfo->applicationInfo.bundleName = "com.ohos.test.helloworld" + std::to_string(index);
138 return abilityInfo;
139 }
140
GetApplicationByIndex(const int32_t index) const141 const std::shared_ptr<ApplicationInfo> AppDeathRecipientTest::GetApplicationByIndex(const int32_t index) const
142 {
143 std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
144 appInfo->name = "com.ohos.test.helloworld" + std::to_string(index);
145 appInfo->bundleName = "com.ohos.test.helloworld" + std::to_string(index);
146 return appInfo;
147 }
148
GetAppRunningRecordByIndex(const int32_t index) const149 const std::shared_ptr<AppRunningRecord> AppDeathRecipientTest::GetAppRunningRecordByIndex(const int32_t index) const
150 {
151 auto appInfo = GetApplicationByIndex(index);
152 BundleInfo bundleInfo;
153 bundleInfo.appId = "com.ohos.test.helloworld_code123";
154
155 auto appRecord = appMgrServiceInner_->appRunningManager_->CheckAppRunningRecordIsExist(
156 appInfo->name, appInfo->name, appInfo->uid, bundleInfo);
157
158 EXPECT_NE(nullptr, appRecord);
159 return appRecord;
160 }
161
GetApp(int32_t pid,int size)162 sptr<IRemoteObject> AppDeathRecipientTest::GetApp(int32_t pid, int size)
163 {
164 EXPECT_CALL(*mockBundleMgr, GetHapModuleInfo(testing::_, testing::_, testing::_))
165 .WillOnce(testing::Return(true))
166 .WillRepeatedly(testing::Return(true));
167 auto abilityInfo = GetAbilityInfoByIndex(pid);
168 auto appInfo = GetApplicationByIndex(pid);
169 sptr<IRemoteObject> token = new MockAbilityToken();
170
171 std::shared_ptr<MockAppSpawnClient> mockClientPtr = std::make_shared<MockAppSpawnClient>();
172 EXPECT_CALL(*mockClientPtr, StartProcess(_, _)).Times(1).WillOnce(DoAll(SetArgReferee<1>(pid), Return(ERR_OK)));
173 std::shared_ptr<MockAppSpawnClient> mockClientstr(mockClientPtr);
174 appMgrServiceInner_->SetAppSpawnClient(mockClientstr);
175
176 appMgrServiceInner_->LoadAbility(token, nullptr, abilityInfo, appInfo, nullptr);
177
178 auto appRecord = GetAppRunningRecordByIndex(pid);
179
180 EXPECT_EQ(size, static_cast<int>(appMgrServiceInner_->GetRecentAppList().size()));
181
182 sptr<MockAppScheduler> mockAppScheduler = new (std::nothrow) MockAppScheduler();
183 sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockAppScheduler.GetRefPtr());
184 appRecord->SetApplicationClient(client);
185
186 return client->AsObject();
187 }
188
189 /*
190 * Feature: Ams
191 * Function: SetTaskHandler ande SetAppMgrServiceInner.
192 * SubFunction: AppDeathRecipient
193 * FunctionPoints: initialization
194 * EnvConditions: have to an application
195 * CaseDescription: How to set parameters
196 */
197
198 HWTEST_F(AppDeathRecipientTest, AppDeathRecipient_001, TestSize.Level1)
199 {
200 HILOG_INFO("AppDeathRecipient_001 start");
201 appDeathRecipientObject_->SetTaskHandler(handler_);
202 EXPECT_TRUE(appDeathRecipientObject_->handler_.lock() != nullptr);
203
204 appDeathRecipientObject_->SetAppMgrServiceInner(appMgrServiceInner_);
205 EXPECT_TRUE(appDeathRecipientObject_->appMgrServiceInner_.lock() != nullptr);
206 HILOG_INFO("AppDeathRecipient_001 end");
207 }
208
209 /*
210 * Feature: Ams
211 * Function: OnRemoteDied
212 * SubFunction: AppDeathRecipient
213 * FunctionPoints: Applied death notification
214 * EnvConditions: have to an application
215 * CaseDescription: Call back the death notification of the notification application
216 */
217 HWTEST_F(AppDeathRecipientTest, AppDeathRecipient_002, TestSize.Level1)
218 {
219 HILOG_INFO("AppDeathRecipient_002 start");
220 MockBundleInstallerAndSA();
221 pid_t pid1 = 24;
222 pid_t pid2 = 25;
223 sptr<IRemoteObject> appOne = GetApp(pid1, 1);
224 sptr<IRemoteObject> appTwo = GetApp(pid2, 2);
225
226 appDeathRecipientObject_->SetTaskHandler(handler_);
227 appDeathRecipientObject_->SetAppMgrServiceInner(appMgrServiceInner_);
228 appDeathRecipientObject_->OnRemoteDied(appOne);
229
230 WaitUntilTaskFinished(handler_);
231 EXPECT_EQ(1, static_cast<int>(appDeathRecipientObject_->appMgrServiceInner_.lock()->GetRecentAppList().size()));
232 HILOG_INFO("AppDeathRecipient_002 end");
233 }
234 } // namespace AppExecFwk
235 } // namespace OHOS
236