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