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