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 #ifndef MOCK_OHOS_ABILITY_RUNTIME_MOCK_BUNDLE_MANAGER_H
17 #define MOCK_OHOS_ABILITY_RUNTIME_MOCK_BUNDLE_MANAGER_H
18 
19 #include <vector>
20 #include <gmock/gmock.h>
21 #include "ability_info.h"
22 #include "application_info.h"
23 #include "bundlemgr/bundle_mgr_interface.h"
24 #include "want.h"
25 #include "iremote_proxy.h"
26 #include "iremote_stub.h"
27 #include "form_info.h"
28 #include "mock_app_control_manager.h"
29 #include "shortcut_info.h"
30 
31 namespace OHOS {
32 namespace AppExecFwk {
33 namespace {
34 const std::string COM_IX_HIWORLD = "com.ix.hiworld";
35 const std::string COM_IX_HIMUSIC = "com.ix.hiMusic";
36 const std::string COM_IX_HIRADIO = "com.ix.hiRadio";
37 const std::string COM_IX_HISERVICE = "com.ix.hiService";
38 const std::string COM_IX_MUSICSERVICE = "com.ix.musicService";
39 const std::string COM_IX_HIDATA = "com.ix.hiData";
40 const std::string COM_IX_HIEXTENSION = "com.ix.hiExtension";
41 const std::string COM_IX_HIACCOUNT = "com.ix.hiAccount";
42 const std::string COM_IX_HIBACKGROUNDMUSIC = "com.ix.hiBackgroundMusic";
43 const std::string COM_IX_HIBACKGROUNDDATA = "com.ix.hiBackgroundData";
44 const std::string COM_IX_HISINGLEMUSIC = "com.ix.hiSingleMusicInfo";
45 const std::string COM_IX_ACCOUNTSERVICE = "com.ix.accountService";
46 const std::string COM_OHOS_TEST = "com.ohos.test";
47 constexpr int32_t MAX_SYS_UID = 2899;
48 const int32_t BASE_USER_RANGE = 200000;
49 const int32_t APPLICATIONINFO_UID = 20000000;
50 
51 auto HiWordInfo = [](std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) {
52     abilityInfo.name = elementTemp.GetAbilityName();
53     abilityInfo.bundleName = elementTemp.GetBundleName();
54     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
55     abilityInfo.applicationName = "Helloworld";
56     abilityInfo.applicationInfo.name = "Helloworld";
57     abilityInfo.type = AbilityType::PAGE;
58     abilityInfo.applicationInfo.isLauncherApp = true;
59     abilityInfo.applicationInfo.apiCompatibleVersion = 8;
60     return true;
61 };
62 
63 auto HiMusicInfo = [](std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) {
64     abilityInfo.name = elementTemp.GetAbilityName();
65     abilityInfo.bundleName = elementTemp.GetBundleName();
66     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
67     abilityInfo.applicationName = "hiMusic";
68     abilityInfo.applicationInfo.name = "hiMusic";
69     abilityInfo.type = AbilityType::PAGE;
70     abilityInfo.applicationInfo.isLauncherApp = false;
71 
72     if (elementTemp.GetAbilityName() == "MusicAbility") {
73         abilityInfo.process = "p1";
74         abilityInfo.launchMode = LaunchMode::STANDARD;
75     }
76     if (elementTemp.GetAbilityName() == "MusicTopAbility") {
77         abilityInfo.process = "p1";
78         abilityInfo.launchMode = LaunchMode::STANDARD;
79     }
80     if (elementTemp.GetAbilityName() == "MusicSAbility") {
81         abilityInfo.process = "p2";
82         abilityInfo.launchMode = LaunchMode::SINGLETON;
83     }
84     return true;
85 };
86 
87 auto HiRadioInfo = [](std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) {
88     abilityInfo.name = elementTemp.GetAbilityName();
89     abilityInfo.bundleName = elementTemp.GetBundleName();
90     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
91     abilityInfo.applicationName = "hiRadio";
92     abilityInfo.applicationInfo.name = "hiRadio";
93     abilityInfo.type = AbilityType::PAGE;
94     abilityInfo.process = "p3";
95     if (elementTemp.GetAbilityName() == "RadioAbility") {
96         abilityInfo.launchMode = LaunchMode::STANDARD;
97     }
98     if (elementTemp.GetAbilityName() == "RadioTopAbility") {
99         abilityInfo.launchMode = LaunchMode::SINGLETON;
100     }
101     return true;
102 };
103 
104 auto HiServiceInfo = [](std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) {
105     abilityInfo.name = elementTemp.GetAbilityName();
106     abilityInfo.bundleName = elementTemp.GetBundleName();
107     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
108     abilityInfo.applicationName = "hiService";
109     abilityInfo.applicationInfo.name = "hiService";
110     abilityInfo.type = AbilityType::SERVICE;
111     abilityInfo.process = "p4";
112     return true;
113 };
114 
115 auto MusicServiceInfo = [](std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) {
116     abilityInfo.name = elementTemp.GetAbilityName();
117     abilityInfo.bundleName = elementTemp.GetBundleName();
118     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
119     abilityInfo.applicationInfo.uid = APPLICATIONINFO_UID;
120     abilityInfo.applicationName = "musicService";
121     abilityInfo.applicationInfo.name = "musicService";
122     abilityInfo.type = AbilityType::SERVICE;
123     abilityInfo.process = "p5";
124     return true;
125 };
126 
127 auto HiDataInfo = [](std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) {
128     abilityInfo.name = elementTemp.GetAbilityName();
129     abilityInfo.bundleName = elementTemp.GetBundleName();
130     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
131     abilityInfo.applicationName = "hiData";
132     abilityInfo.applicationInfo.name = "hiData";
133     abilityInfo.type = AbilityType::DATA;
134     abilityInfo.process = "p6";
135     return true;
136 };
137 
138 auto HiExtensionInfo = [](std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) {
139     abilityInfo.name = elementTemp.GetAbilityName();
140     abilityInfo.bundleName = elementTemp.GetBundleName();
141     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
142     abilityInfo.applicationName = "hiExtension";
143     abilityInfo.applicationInfo.name = "hiExtension";
144     abilityInfo.type = AbilityType::EXTENSION;
145     return true;
146 };
147 
148 auto HiAccountInfo = [](std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) {
149     abilityInfo.name = elementTemp.GetAbilityName();
150     abilityInfo.bundleName = elementTemp.GetBundleName();
151     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
152     abilityInfo.applicationInfo.uid = APPLICATIONINFO_UID;
153     abilityInfo.applicationName = "AccountTest";
154     abilityInfo.applicationInfo.name = "AccountTest";
155     abilityInfo.type = AbilityType::PAGE;
156     abilityInfo.applicationInfo.isLauncherApp = true;
157     return true;
158 };
159 
160 auto HiBAckgroundMusicInfo = [](std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) {
161     abilityInfo.name = elementTemp.GetAbilityName();
162     abilityInfo.bundleName = elementTemp.GetBundleName();
163     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
164     abilityInfo.applicationInfo.singleton = true;
165     abilityInfo.applicationName = "hiBackgroundMusic";
166     abilityInfo.applicationInfo.name = "hiBackgroundMusic";
167     abilityInfo.type = AbilityType::SERVICE;
168     abilityInfo.process = "p4";
169     return true;
170 };
171 
172 auto HiBAckgroundDataInfo = [](std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) {
173     abilityInfo.name = elementTemp.GetAbilityName();
174     abilityInfo.bundleName = elementTemp.GetBundleName();
175     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
176     abilityInfo.applicationInfo.singleton = true;
177     abilityInfo.applicationName = "hiBackgroundData";
178     abilityInfo.applicationInfo.name = "hiBackgroundData";
179     abilityInfo.type = AbilityType::SERVICE;
180     abilityInfo.process = "p4";
181     return true;
182 };
183 
184 auto HiSingleMusicInfo = [](std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) {
185     abilityInfo.name = elementTemp.GetAbilityName();
186     abilityInfo.bundleName = elementTemp.GetBundleName();
187     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
188     abilityInfo.applicationInfo.singleton = true;
189     abilityInfo.applicationName = "hiSingleMusic";
190     abilityInfo.applicationInfo.name = "hiSingleMusic";
191     abilityInfo.type = AbilityType::PAGE;
192     abilityInfo.process = "p3";
193     if (elementTemp.GetAbilityName() == "SingleMusicAbility") {
194         abilityInfo.launchMode = LaunchMode::STANDARD;
195     }
196     if (elementTemp.GetAbilityName() == "SingleMusicTopAbility") {
197         abilityInfo.launchMode = LaunchMode::SINGLETON;
198     }
199     return true;
200 };
201 
202 auto AccountServiceInfo = [](std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) {
203     abilityInfo.name = elementTemp.GetAbilityName();
204     abilityInfo.bundleName = elementTemp.GetBundleName();
205     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
206     abilityInfo.applicationInfo.uid = APPLICATIONINFO_UID;
207     abilityInfo.applicationName = "accountService";
208     abilityInfo.applicationInfo.name = "accountService";
209     abilityInfo.type = AbilityType::SERVICE;
210     abilityInfo.process = "p9";
211     return true;
212 };
213 
214 auto TestInfo = [](std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) {
215     abilityInfo.name = elementTemp.GetAbilityName();
216     abilityInfo.bundleName = elementTemp.GetBundleName();
217     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
218     abilityInfo.applicationName = "com.ohos.test";
219     abilityInfo.applicationInfo.name = "com.ohos.test";
220     abilityInfo.moduleName = ".MyApplication";
221     abilityInfo.package = "com.ohos.test";
222     abilityInfo.type = AbilityType::PAGE;
223     abilityInfo.applicationInfo.isLauncherApp = false;
224     abilityInfo.kind = "page";
225     abilityInfo.process = "p8";
226     abilityInfo.labelId = 10001;
227     abilityInfo.label = "$string:label";
228     abilityInfo.descriptionId = 10002;
229     abilityInfo.description = "$string:mainability_description";
230     abilityInfo.iconId = 10003;
231     abilityInfo.iconPath = "$media:icon";
232     abilityInfo.theme = "mytheme";
233     abilityInfo.readPermission = "readPermission";
234     abilityInfo.writePermission = "writePermission";
235     abilityInfo.resourcePath = "/data/app/com.ohos.test";
236     abilityInfo.srcPath = "/resources/base/profile";
237     abilityInfo.srcLanguage = "C++";
238     abilityInfo.isLauncherAbility = false;
239     abilityInfo.isNativeAbility = false;
240     abilityInfo.enabled = false;
241     abilityInfo.supportPipMode = false;
242     abilityInfo.formEnabled = false;
243     return true;
244 };
245 }  // namespace
246 class BundleMgrProxy : public IRemoteProxy<IBundleMgr> {
247 public:
BundleMgrProxy(const sptr<IRemoteObject> & impl)248     explicit BundleMgrProxy(const sptr<IRemoteObject>& impl) : IRemoteProxy<IBundleMgr>(impl)
249     {}
~BundleMgrProxy()250     virtual ~BundleMgrProxy()
251     {}
252 
253     bool QueryAbilityInfo(const AAFwk::Want& want, AbilityInfo& abilityInfo) override;
254 
255     bool GetApplicationInfo(
256         const std::string& appName, const ApplicationFlag flag, const int userId, ApplicationInfo& appInfo) override;
257 
QueryAbilityInfo(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,const sptr<IRemoteObject> & callBack)258     bool QueryAbilityInfo(const Want& want, int32_t flags, int32_t userId, AbilityInfo& abilityInfo,
259         const sptr<IRemoteObject>& callBack)
260     {
261         if (userId == 1) {
262             return false;
263         }
264         return true;
265     }
266 
267     bool GetBundleInfo(
268         const std::string& bundleName, const BundleFlag flag, BundleInfo& bundleInfo, int32_t userId) override;
269 
270     bool GetBundleInfos(const BundleFlag flag,
271         std::vector<BundleInfo> &bundleInfos, int32_t userId = Constants::UNSPECIFIED_USERID)
272     {
273         OHOS::AppExecFwk::BundleInfo bundleInfo;
274         bundleInfo.name = "com.ix.residentservcie";
275         bundleInfo.isKeepAlive = true;
276         bundleInfo.applicationInfo.process = "com.ix.residentservcie";
277 
278         OHOS::AppExecFwk::HapModuleInfo hapModuleInfo;
279         hapModuleInfo.isModuleJson = true;
280         hapModuleInfo.mainElementName = "residentServiceAbility";
281         hapModuleInfo.process = "com.ix.residentservcie";
282         bundleInfo.hapModuleInfos.emplace_back(hapModuleInfo);
283 
284         bundleInfos.emplace_back(bundleInfo);
285         return true;
286     }
287 
ImplicitQueryInfoByPriority(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,ExtensionAbilityInfo & extensionInfo)288     virtual bool ImplicitQueryInfoByPriority(const Want& want, int32_t flags, int32_t userId,
289         AbilityInfo& abilityInfo, ExtensionAbilityInfo& extensionInfo) override
290     {
291         abilityInfo.name = "MainAbility";
292         abilityInfo.bundleName = "com.ohos.launcher";
293         return true;
294     }
295 
GetBaseSharedBundleInfos(const std::string & bundleName,std::vector<BaseSharedBundleInfo> & baseSharedBundleInfos,AppExecFwk::GetDependentBundleInfoFlag flag)296     ErrCode GetBaseSharedBundleInfos(const std::string &bundleName,
297         std::vector<BaseSharedBundleInfo> &baseSharedBundleInfos,
298         AppExecFwk::GetDependentBundleInfoFlag flag) override
299     {
300         return ERR_OK;
301     }
302 };
303 
304 class BundleMgrStub : public IRemoteStub<IBundleMgr> {
305 public:
~BundleMgrStub()306     virtual ~BundleMgrStub() {}
307     virtual int OnRemoteRequest(
308         uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override;
309 };
310 
311 class BundleMgrService : public BundleMgrStub {
312 public:
313     BundleMgrService();
314     ~BundleMgrService();
315 
316     bool QueryAbilityInfo(const AAFwk::Want& want, AbilityInfo& abilityInfo) override;
317     bool QueryAbilityInfo(const Want& want, int32_t flags, int32_t userId, AbilityInfo& abilityInfo) override;
318     bool GetApplicationInfo(
319         const std::string& appName, const ApplicationFlag flag, const int userId, ApplicationInfo& appInfo) override;
320     bool GetBundleInfo(
321         const std::string& bundleName, const BundleFlag flag, BundleInfo& bundleInfo, int32_t userId) override;
322     int GetUidByBundleName(const std::string& bundleName, int32_t userId, int32_t appCloneIndex) override;
323 
324     bool CheckWantEntity(const AAFwk::Want&, AbilityInfo&);
325 
ImplicitQueryInfoByPriority(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,ExtensionAbilityInfo & extensionAbilityInfo)326     virtual bool ImplicitQueryInfoByPriority(const Want& want, int32_t flags, int32_t userId,
327         AbilityInfo& abilityInfo, ExtensionAbilityInfo& extensionAbilityInfo) override
328     {
329         abilityInfo.name = "MainAbility";
330         abilityInfo.bundleName = "com.ohos.launcher";
331         return true;
332     }
333 
GetAppControlProxy()334     virtual sptr<IAppControlMgr> GetAppControlProxy()
335     {
336         if (isAppControlProxyNull_) {
337             isAppControlProxyNull_ = false;
338             return nullptr;
339         }
340         sptr<IAppControlMgr> appControlMgr = new (std::nothrow) AppControlProxy(nullptr);
341         return appControlMgr;
342     }
GetBaseSharedBundleInfos(const std::string & bundleName,std::vector<BaseSharedBundleInfo> & baseSharedBundleInfos,AppExecFwk::GetDependentBundleInfoFlag flag)343     ErrCode GetBaseSharedBundleInfos(const std::string &bundleName,
344         std::vector<BaseSharedBundleInfo> &baseSharedBundleInfos,
345         AppExecFwk::GetDependentBundleInfoFlag flag) override
346     {
347         return ERR_OK;
348     }
349 public:
350     using QueryAbilityInfoFunType =
351         std::function<bool(std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp)>;
352     std::map<std::string, QueryAbilityInfoFunType> abilityInfoMap_;
353     bool isAppControlProxyNull_ = false;
354 };
355 }  // namespace AppExecFwk
356 }  // namespace OHOS
357 
358 #endif  // MOCK_OHOS_ABILITY_RUNTIME_MOCK_BUNDLE_MANAGER_H
359