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