1 /*
2 * Copyright (c) 2021-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
16 #include <gtest/gtest.h>
17 #include "mock_bundle_manager.h"
18
19 #include "ability_info.h"
20 #include "application_info.h"
21 #include "hilog_tag_wrapper.h"
22 #include "mock_overlay_manager.h"
23 namespace {
24 const int32_t HQF_VERSION_CODE = 1000;
25 }
26
27 namespace OHOS {
28 namespace AppExecFwk {
29 namespace {
30 std::string g_CurrentBundleName;
31 int32_t appUid = 2100;
32 int32_t appUid1 = 2101;
33 }
34
MockSetCurrentBundleName(const std::string & bundleName)35 void MockSetCurrentBundleName(const std::string& bundleName)
36 {
37 g_CurrentBundleName = bundleName;
38 }
39
40 namespace {
ConstructHqfInfo(BundleInfo & bundleInfo)41 void ConstructHqfInfo(BundleInfo& bundleInfo)
42 {
43 std::vector<HqfInfo> hqfInfos;
44 bundleInfo.applicationInfo.appQuickFix.deployedAppqfInfo.versionCode = HQF_VERSION_CODE;
45 bundleInfo.applicationInfo.appQuickFix.deployedAppqfInfo.versionName = "1.0.0";
46 bundleInfo.applicationInfo.appQuickFix.deployedAppqfInfo.cpuAbi = "armeabi-v7a";
47 bundleInfo.applicationInfo.appQuickFix.deployedAppqfInfo.nativeLibraryPath = "patch_1000/libs/arm";
48 bundleInfo.applicationInfo.appQuickFix.deployedAppqfInfo.type = QuickFixType::PATCH;
49 HqfInfo hqfInfo1;
50 hqfInfo1.moduleName = "entry1";
51 hqfInfo1.hapSha256 = "12345678";
52 hqfInfo1.hqfFilePath = "/data/app/el1/bundle/public/com.ohos.quickfix/patch_1000/entry1.hqf";
53 hqfInfo1.nativeLibraryPath = "patch_1000/libs/arm";
54 HqfInfo hqfInfo2;
55 hqfInfo2.moduleName = "entry2";
56 hqfInfo2.hapSha256 = "12345678";
57 hqfInfo2.hqfFilePath = "/data/app/el1/bundle/public/com.ohos.quickfix/patch_1000/entry2.hqf";
58 hqfInfo2.nativeLibraryPath = "patch_1000/libs/arm";
59 hqfInfos.push_back(hqfInfo1);
60 hqfInfos.push_back(hqfInfo2);
61 bundleInfo.applicationInfo.appQuickFix.deployedAppqfInfo.hqfInfos = hqfInfos;
62
63 std::vector<HapModuleInfo> hapModuleInfos;
64 HapModuleInfo moduleInfo1;
65 moduleInfo1.name = "entry1";
66 moduleInfo1.moduleName = "entry1";
67 moduleInfo1.moduleType = AppExecFwk::ModuleType::ENTRY;
68 moduleInfo1.hapPath = "/data/app/el1/bundle/public/com.ohos.hotreload/entry1";
69 moduleInfo1.hqfInfo = hqfInfo1;
70 moduleInfo1.process = "test_quickfix";
71 moduleInfo1.isModuleJson = true;
72 moduleInfo1.isStageBasedModel = true;
73 moduleInfo1.isLibIsolated = true;
74 HapModuleInfo moduleInfo2;
75 moduleInfo2.name = "entry2";
76 moduleInfo2.moduleName = "entry2";
77 moduleInfo2.hapPath = "/data/app/el1/bundle/public/com.ohos.hotreload/entry2";
78 moduleInfo2.hqfInfo = hqfInfo2;
79 moduleInfo2.process = "test_quickfix";
80 moduleInfo2.isModuleJson = true;
81 moduleInfo2.isStageBasedModel = true;
82 moduleInfo2.isLibIsolated = true;
83 hapModuleInfos.push_back(moduleInfo1);
84 hapModuleInfos.push_back(moduleInfo2);
85 bundleInfo.hapModuleInfos = hapModuleInfos;
86 bundleInfo.versionCode = HQF_VERSION_CODE;
87 bundleInfo.versionName = "1.0.0";
88 bundleInfo.applicationInfo.nativeLibraryPath = "libs/arm";
89 }
90 } // namespace
91
QueryAbilityInfo(const AAFwk::Want & want,AbilityInfo & abilityInfo)92 bool BundleMgrProxy::QueryAbilityInfo(const AAFwk::Want& want, AbilityInfo& abilityInfo)
93 {
94 ElementName eleName = want.GetElement();
95 if (eleName.GetBundleName().empty()) {
96 return false;
97 }
98 abilityInfo.visible = true;
99 abilityInfo.name = eleName.GetAbilityName();
100 abilityInfo.bundleName = eleName.GetBundleName();
101 abilityInfo.applicationName = "Helloworld";
102 return true;
103 }
104
QueryAbilityInfoByUri(const std::string & uri,AbilityInfo & abilityInfo)105 bool BundleMgrProxy::QueryAbilityInfoByUri(const std::string& uri, AbilityInfo& abilityInfo)
106 {
107 return false;
108 }
109
GetApplicationInfo(const std::string & appName,const ApplicationFlag flag,const int userId,ApplicationInfo & appInfo)110 bool BundleMgrProxy::GetApplicationInfo(
111 const std::string& appName, const ApplicationFlag flag, const int userId, ApplicationInfo& appInfo)
112 {
113 if (appName.empty()) {
114 return false;
115 }
116 appInfo.name = "Helloworld";
117 appInfo.bundleName = "com.ohos.hiworld";
118 return true;
119 }
120
GetAppType(const std::string & bundleName)121 std::string BundleMgrProxy::GetAppType(const std::string& bundleName)
122 {
123 return "system";
124 }
125
GetOverlayManagerProxy()126 sptr<IOverlayManager> BundleMgrProxy::GetOverlayManagerProxy()
127 {
128 sptr<IOverlayManager> overlayModuleProxy = new (std::nothrow) OverlayManagerProxy(nullptr);
129 return overlayModuleProxy;
130 }
131
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)132 int BundleMgrStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
133 {
134 return 0;
135 }
136
BundleMgrService()137 BundleMgrService::BundleMgrService()
138 {
139 MakingPackageData();
140 }
141
QueryAbilityInfo(const AAFwk::Want & want,AbilityInfo & abilityInfo)142 bool BundleMgrService::QueryAbilityInfo(const AAFwk::Want& want, AbilityInfo& abilityInfo)
143 {
144 ElementName elementName = want.GetElement();
145 if (elementName.GetBundleName().empty()) {
146 return false;
147 }
148 if (std::string::npos != elementName.GetBundleName().find("service")) {
149 abilityInfo.type = AppExecFwk::AbilityType::SERVICE;
150 }
151 abilityInfo.visible = true;
152 abilityInfo.name = elementName.GetAbilityName();
153 abilityInfo.bundleName = elementName.GetBundleName();
154 abilityInfo.applicationName = elementName.GetBundleName();
155 if (want.HasEntity(Want::ENTITY_HOME) && want.GetAction() == Want::ACTION_HOME) {
156 abilityInfo.applicationInfo.isLauncherApp = true;
157 } else {
158 abilityInfo.applicationInfo.isLauncherApp = false;
159 }
160 return true;
161 }
162
QueryAbilityInfoByUri(const std::string & uri,AbilityInfo & abilityInfo)163 bool BundleMgrService::QueryAbilityInfoByUri(const std::string& uri, AbilityInfo& abilityInfo)
164 {
165 return false;
166 }
167
GetApplicationInfo(const std::string & appName,const ApplicationFlag flag,const int userId,ApplicationInfo & appInfo)168 bool BundleMgrService::GetApplicationInfo(
169 const std::string& appName, const ApplicationFlag flag, const int userId, ApplicationInfo& appInfo)
170 {
171 if (appName.empty()) {
172 return false;
173 }
174 appInfo.name = "Helloworld";
175 appInfo.bundleName = "com.ohos.hiworld";
176 return true;
177 }
178
GetAppType(const std::string & bundleName)179 std::string BundleMgrService::GetAppType(const std::string& bundleName)
180 {
181 return "system";
182 }
183
GetHapModuleInfo(const AbilityInfo & abilityInfo,HapModuleInfo & hapModuleInfo)184 bool BundleMgrService::GetHapModuleInfo(const AbilityInfo& abilityInfo, HapModuleInfo& hapModuleInfo)
185 {
186 hapModuleInfo.name = "Captain";
187 return true;
188 }
189
GetHapModuleInfo(const AbilityInfo & abilityInfo,int32_t userId,HapModuleInfo & hapModuleInfo)190 bool BundleMgrService::GetHapModuleInfo(const AbilityInfo& abilityInfo, int32_t userId, HapModuleInfo& hapModuleInfo)
191 {
192 hapModuleInfo.name = "Captain";
193 return true;
194 }
195
GetHapModuleInfo(const AbilityInfo & abilityInfo,HapModuleInfo & hapModuleInfo)196 bool BundleMgrProxy::GetHapModuleInfo(const AbilityInfo& abilityInfo, HapModuleInfo& hapModuleInfo)
197 {
198 hapModuleInfo.name = "Captain";
199 return true;
200 }
201
GetHapModuleInfo(const AbilityInfo & abilityInfo,int32_t userId,HapModuleInfo & hapModuleInfo)202 bool BundleMgrProxy::GetHapModuleInfo(const AbilityInfo& abilityInfo, int32_t userId, HapModuleInfo& hapModuleInfo)
203 {
204 hapModuleInfo.name = "Captain";
205 return true;
206 }
207
GetBundleInfo(const std::string & bundleName,const BundleFlag flag,BundleInfo & bundleInfo,int32_t userId)208 bool BundleMgrService::GetBundleInfo(
209 const std::string& bundleName, const BundleFlag flag, BundleInfo& bundleInfo, int32_t userId)
210 {
211 int32_t userUid = 10001;
212 int32_t userGid = 10001;
213 bundleInfo.uid = userUid;
214 bundleInfo.gid = userGid;
215 int index1 = 101;
216 int index2 = 102;
217 int index3 = 103;
218 int index4 = 104;
219 if (bundleName == COM_OHOS_HELLO + std::to_string(index1) ||
220 bundleName == COM_OHOS_HELLO + std::to_string(index2)) {
221 bundleInfo.jointUserId = "join";
222 bundleInfo.appId = bundleName + "_xxx";
223 }
224 if (bundleName == COM_OHOS_HELLO + std::to_string(index3) ||
225 bundleName == COM_OHOS_HELLO + std::to_string(index4)) {
226 bundleInfo.jointUserId = "";
227 bundleInfo.appId = bundleName + "_xxx";
228 }
229 if (bundleName == "KeepAliveApplication") {
230 HapModuleInfo hapModuleInfo;
231 hapModuleInfo.moduleName = "KeepAliveApplication";
232 ApplicationInfo appInfo;
233 appInfo.name = "KeepAliveApp";
234 appInfo.bundleName = bundleName;
235 appInfo.uid = appUid;
236 bundleInfo.uid = appUid;
237 bundleInfo.name = bundleName;
238 bundleInfo.applicationInfo = appInfo;
239 bundleInfo.hapModuleInfos.push_back(hapModuleInfo);
240 }
241 if (bundleName == "KeepAliveApplication1") {
242 HapModuleInfo hapModuleInfo;
243 hapModuleInfo.moduleName = "KeepAliveApplication1";
244 ApplicationInfo appInfo;
245 appInfo.name = "KeepAliveApp1";
246 appInfo.bundleName = bundleName;
247 appInfo.uid = appUid1;
248 bundleInfo.uid = appUid1;
249 bundleInfo.name = bundleName;
250 bundleInfo.applicationInfo = appInfo;
251 bundleInfo.hapModuleInfos.push_back(hapModuleInfo);
252 }
253 if (bundleName == "com.ohos.quickfix") {
254 TAG_LOGI(AAFwkTag::TEST, "GetBundleInfo of [com.ohos.quickfix].");
255 ConstructHqfInfo(bundleInfo);
256 }
257 return true;
258 }
GetBundleGids(const std::string & bundleName,std::vector<int> & gids)259 bool BundleMgrService::GetBundleGids(const std::string& bundleName, std::vector<int>& gids)
260 {
261 int32_t userGid1 = 10001;
262 int32_t userGid2 = 10002;
263 int32_t userGid3 = 10003;
264 gids.push_back(userGid1);
265 gids.push_back(userGid2);
266 gids.push_back(userGid3);
267 return true;
268 }
269
GetBundleInfos(const BundleFlag flag,std::vector<BundleInfo> & bundleInfos,int32_t userId)270 bool BundleMgrService::GetBundleInfos(
271 const BundleFlag flag, std::vector<BundleInfo>& bundleInfos, int32_t userId)
272 {
273 bundleInfos = bundleInfos_;
274 return true;
275 }
276
PushTestHelloIndexAbility(int index)277 void BundleMgrService::PushTestHelloIndexAbility(int index)
278 {
279 AbilityInfo info;
280 info.name = "com.ohos.test.helloworld.MainAbility";
281 info.bundleName = COM_OHOS_HELLO + std::to_string(index);
282 info.applicationInfo.bundleName = COM_OHOS_HELLO + std::to_string(index);
283 info.applicationName = "helloworld";
284 info.applicationInfo.name = "helloworld";
285 info.process = "p1";
286 info.applicationInfo.uid = -1;
287 info.deviceId = "deviceId";
288 info.visible = true;
289
290 BundleInfo bundleInfo;
291 bundleInfo.name = COM_OHOS_HELLO + std::to_string(index);
292 bundleInfo.uid = info.applicationInfo.uid;
293 bundleInfo.abilityInfos.emplace_back(info);
294 bundleInfo.applicationInfo = info.applicationInfo;
295 bundleInfos_.emplace_back(bundleInfo);
296 }
297
PushTestSpecialAbility()298 void BundleMgrService::PushTestSpecialAbility()
299 {
300 AbilityInfo info;
301 info.name = "com.ohos.test.helloworld.MainAbility";
302 info.bundleName = COM_OHOS_SPECIAL;
303 info.applicationInfo.bundleName = COM_OHOS_SPECIAL;
304 info.applicationName = "helloworld";
305 info.applicationInfo.name = "helloworld";
306 info.process = "p1";
307 info.applicationInfo.uid = -1;
308 info.deviceId = "deviceId";
309 info.visible = true;
310
311 BundleInfo bundleInfo;
312 bundleInfo.name = COM_OHOS_SPECIAL;
313 bundleInfo.uid = info.applicationInfo.uid;
314 bundleInfo.abilityInfos.emplace_back(info);
315 bundleInfo.applicationInfo = info.applicationInfo;
316 bundleInfos_.emplace_back(bundleInfo);
317 }
318
PushTestHelloAbility()319 void BundleMgrService::PushTestHelloAbility()
320 {
321 AbilityInfo info;
322 info.name = "com.ohos.test.helloworld.MainAbility";
323 info.bundleName = COM_OHOS_HELLO;
324 info.applicationInfo.bundleName = COM_OHOS_HELLO;
325 info.applicationName = "helloworld";
326 info.applicationInfo.name = "helloworld";
327 info.process = "p1";
328 info.applicationInfo.uid = -1;
329 info.deviceId = "deviceId";
330 info.visible = true;
331
332 BundleInfo bundleInfo;
333 bundleInfo.name = COM_OHOS_HELLO;
334 bundleInfo.uid = info.applicationInfo.uid;
335 bundleInfo.abilityInfos.emplace_back(info);
336 bundleInfo.applicationInfo = info.applicationInfo;
337 bundleInfos_.emplace_back(bundleInfo);
338 }
339
MakingPackageData()340 void BundleMgrService::MakingPackageData()
341 {
342 PushTestSpecialAbility();
343 PushTestHelloAbility();
344 MakingResidentProcData();
345 for (int i = 0; i <= APPLICATION_NUMHELLO; i++) {
346 PushTestHelloIndexAbility(i);
347 }
348 }
349
MakingResidentProcData()350 void BundleMgrService::MakingResidentProcData()
351 {
352 int appUid = 2100;
353 int appUid1 = 2101;
354 BundleInfo bundleInfo;
355 bundleInfo.uid = appUid;
356 bundleInfo.name = "KeepAliveApplication";
357
358 BundleInfo bundleInfo1;
359 bundleInfo1.uid = appUid1;
360 bundleInfo1.name = "KeepAliveApplication1";
361
362 bundleInfos_.emplace_back(bundleInfo);
363 bundleInfos_.emplace_back(bundleInfo1);
364 }
365
GetQuickFixManagerProxy()366 sptr<IQuickFixManager> BundleMgrService::GetQuickFixManagerProxy()
367 {
368 if (quickFixManager_ == nullptr) {
369 quickFixManager_ = new (std::nothrow) QuickFixManagerHostImpl();
370 if (quickFixManager_ == nullptr) {
371 GTEST_LOG_(ERROR) << "new quick fix manager failed.";
372 return nullptr;
373 }
374 }
375 return quickFixManager_;
376 }
377
GetBundleInfoForSelf(int32_t flags,BundleInfo & bundleInfo)378 ErrCode BundleMgrService::GetBundleInfoForSelf(int32_t flags, BundleInfo &bundleInfo)
379 {
380 HapModuleInfo hapModuleInfo;
381 bundleInfo.hapModuleInfos.push_back(hapModuleInfo);
382 if (g_CurrentBundleName == "com.ohos.quickfix") {
383 TAG_LOGI(AAFwkTag::TEST, "GetBundleInfo of [com.ohos.quickfix].");
384 ConstructHqfInfo(bundleInfo);
385 }
386 return ERR_OK;
387 }
388
GetOverlayManagerProxy()389 sptr<IOverlayManager> BundleMgrService::GetOverlayManagerProxy()
390 {
391 sptr<IOverlayManager> overlayManagerProxy = new (std::nothrow) OverlayManagerProxy(nullptr);
392 return overlayManagerProxy;
393 }
394 } // namespace AppExecFwk
395 } // namespace OHOS
396