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_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 HILOG_INFO("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
GetBundleGidsByUid(const std::string & bundleName,const int & uid,std::vector<int> & gids)277 bool BundleMgrService::GetBundleGidsByUid(
278 const std::string& bundleName, const int& uid, std::vector<int>& gids)
279 {
280 return true;
281 }
282
PushTestHelloIndexAbility(int index)283 void BundleMgrService::PushTestHelloIndexAbility(int index)
284 {
285 AbilityInfo info;
286 info.name = "com.ohos.test.helloworld.MainAbility";
287 info.bundleName = COM_OHOS_HELLO + std::to_string(index);
288 info.applicationInfo.bundleName = COM_OHOS_HELLO + std::to_string(index);
289 info.applicationName = "helloworld";
290 info.applicationInfo.name = "helloworld";
291 info.process = "p1";
292 info.applicationInfo.uid = -1;
293 info.deviceId = "deviceId";
294 info.visible = true;
295
296 BundleInfo bundleInfo;
297 bundleInfo.name = COM_OHOS_HELLO + std::to_string(index);
298 bundleInfo.uid = info.applicationInfo.uid;
299 bundleInfo.abilityInfos.emplace_back(info);
300 bundleInfo.applicationInfo = info.applicationInfo;
301 bundleInfos_.emplace_back(bundleInfo);
302 }
303
PushTestSpecialAbility()304 void BundleMgrService::PushTestSpecialAbility()
305 {
306 AbilityInfo info;
307 info.name = "com.ohos.test.helloworld.MainAbility";
308 info.bundleName = COM_OHOS_SPECIAL;
309 info.applicationInfo.bundleName = COM_OHOS_SPECIAL;
310 info.applicationName = "helloworld";
311 info.applicationInfo.name = "helloworld";
312 info.process = "p1";
313 info.applicationInfo.uid = -1;
314 info.deviceId = "deviceId";
315 info.visible = true;
316
317 BundleInfo bundleInfo;
318 bundleInfo.name = COM_OHOS_SPECIAL;
319 bundleInfo.uid = info.applicationInfo.uid;
320 bundleInfo.abilityInfos.emplace_back(info);
321 bundleInfo.applicationInfo = info.applicationInfo;
322 bundleInfos_.emplace_back(bundleInfo);
323 }
324
PushTestHelloAbility()325 void BundleMgrService::PushTestHelloAbility()
326 {
327 AbilityInfo info;
328 info.name = "com.ohos.test.helloworld.MainAbility";
329 info.bundleName = COM_OHOS_HELLO;
330 info.applicationInfo.bundleName = COM_OHOS_HELLO;
331 info.applicationName = "helloworld";
332 info.applicationInfo.name = "helloworld";
333 info.process = "p1";
334 info.applicationInfo.uid = -1;
335 info.deviceId = "deviceId";
336 info.visible = true;
337
338 BundleInfo bundleInfo;
339 bundleInfo.name = COM_OHOS_HELLO;
340 bundleInfo.uid = info.applicationInfo.uid;
341 bundleInfo.abilityInfos.emplace_back(info);
342 bundleInfo.applicationInfo = info.applicationInfo;
343 bundleInfos_.emplace_back(bundleInfo);
344 }
345
MakingPackageData()346 void BundleMgrService::MakingPackageData()
347 {
348 PushTestSpecialAbility();
349 PushTestHelloAbility();
350 MakingResidentProcData();
351 for (int i = 0; i <= APPLICATION_NUMHELLO; i++) {
352 PushTestHelloIndexAbility(i);
353 }
354 }
355
MakingResidentProcData()356 void BundleMgrService::MakingResidentProcData()
357 {
358 int appUid = 2100;
359 int appUid1 = 2101;
360 BundleInfo bundleInfo;
361 bundleInfo.uid = appUid;
362 bundleInfo.name = "KeepAliveApplication";
363
364 BundleInfo bundleInfo1;
365 bundleInfo1.uid = appUid1;
366 bundleInfo1.name = "KeepAliveApplication1";
367
368 bundleInfos_.emplace_back(bundleInfo);
369 bundleInfos_.emplace_back(bundleInfo1);
370 }
371
GetQuickFixManagerProxy()372 sptr<IQuickFixManager> BundleMgrService::GetQuickFixManagerProxy()
373 {
374 if (quickFixManager_ == nullptr) {
375 quickFixManager_ = new (std::nothrow) QuickFixManagerHostImpl();
376 if (quickFixManager_ == nullptr) {
377 GTEST_LOG_(ERROR) << "new quick fix manager failed.";
378 return nullptr;
379 }
380 }
381 return quickFixManager_;
382 }
383
GetBundleInfoForSelf(int32_t flags,BundleInfo & bundleInfo)384 ErrCode BundleMgrService::GetBundleInfoForSelf(int32_t flags, BundleInfo &bundleInfo)
385 {
386 HapModuleInfo hapModuleInfo;
387 bundleInfo.hapModuleInfos.push_back(hapModuleInfo);
388 if (g_CurrentBundleName == "com.ohos.quickfix") {
389 HILOG_INFO("GetBundleInfo of [com.ohos.quickfix].");
390 ConstructHqfInfo(bundleInfo);
391 }
392 return ERR_OK;
393 }
394
GetOverlayManagerProxy()395 sptr<IOverlayManager> BundleMgrService::GetOverlayManagerProxy()
396 {
397 sptr<IOverlayManager> overlayManagerProxy = new (std::nothrow) OverlayManagerProxy(nullptr);
398 return overlayManagerProxy;
399 }
400 } // namespace AppExecFwk
401 } // namespace OHOS
402