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 FOUNDATION_EVENT_CESFWK_SERVICES_TEST_UNITTEST_MOCK_INCLUDE_MOCK_BUNDLE_MANAGER_H 17 #define FOUNDATION_EVENT_CESFWK_SERVICES_TEST_UNITTEST_MOCK_INCLUDE_MOCK_BUNDLE_MANAGER_H 18 19 #include <vector> 20 21 #include "ability_info.h" 22 #include "application_info.h" 23 #include "bundle_mgr_host.h" 24 #include "bundle_mgr_interface.h" 25 #include "ohos/aafwk/content/want.h" 26 #include "iremote_proxy.h" 27 #include "iremote_stub.h" 28 29 namespace OHOS { 30 namespace AppExecFwk { 31 class MockBundleMgrService : public BundleMgrHost { 32 public: MockBundleMgrService()33 MockBundleMgrService() 34 { 35 } 36 ~MockBundleMgrService()37 ~MockBundleMgrService() 38 {} 39 40 /** 41 * @brief Obtains the ApplicationInfo based on a given bundle name. 42 * @param appName Indicates the application bundle name to be queried. 43 * @param flag Indicates the flag used to specify information contained 44 * in the ApplicationInfo object that will be returned. 45 * @param userId Indicates the user ID. 46 * @param appInfo Indicates the obtained ApplicationInfo object. 47 * @return Returns true if the application is successfully obtained; returns false otherwise. 48 */ GetApplicationInfo(const std::string & appName,const ApplicationFlag flag,const int userId,ApplicationInfo & appInfo)49 virtual bool GetApplicationInfo( 50 const std::string &appName, const ApplicationFlag flag, const int userId, ApplicationInfo &appInfo) override 51 { 52 return true; 53 } 54 /** 55 * @brief Obtains information about all installed applications of a specified user. 56 * @param flag Indicates the flag used to specify information contained 57 * in the ApplicationInfo objects that will be returned. 58 * @param userId Indicates the user ID. 59 * @param appInfos Indicates all of the obtained ApplicationInfo objects. 60 * @return Returns true if the application is successfully obtained; returns false otherwise. 61 */ GetApplicationInfos(const ApplicationFlag flag,const int userId,std::vector<ApplicationInfo> & appInfos)62 virtual bool GetApplicationInfos( 63 const ApplicationFlag flag, const int userId, std::vector<ApplicationInfo> &appInfos) override 64 { 65 return true; 66 } 67 /** 68 * @brief Obtains the BundleInfo based on a given bundle name. 69 * @param bundleName Indicates the application bundle name to be queried. 70 * @param flag Indicates the information contained in the BundleInfo object to be returned. 71 * @param bundleInfo Indicates the obtained BundleInfo object. 72 * @param userId Indicates the user ID. 73 * @return Returns true if the BundleInfo is successfully obtained; returns false otherwise. 74 */ GetBundleInfo(const std::string & bundleName,const BundleFlag flag,BundleInfo & bundleInfo,int32_t userId)75 virtual bool GetBundleInfo( 76 const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo, int32_t userId) override 77 { 78 return true; 79 } 80 /** 81 * @brief Obtains BundleInfo of all bundles available in the system. 82 * @param flag Indicates the flag used to specify information contained in the BundleInfo that will be returned. 83 * @param bundleInfos Indicates all of the obtained BundleInfo objects. 84 * @param userId Indicates the user ID. 85 * @return Returns true if the BundleInfos is successfully obtained; returns false otherwise. 86 */ GetBundleInfos(const BundleFlag flag,std::vector<BundleInfo> & bundleInfos,int32_t userId)87 virtual bool GetBundleInfos( 88 const BundleFlag flag, std::vector<BundleInfo> &bundleInfos, int32_t userId) override 89 { 90 return true; 91 }; 92 /** 93 * @brief Obtains the application UID based on the given bundle name and user ID. 94 * @param bundleName Indicates the bundle name of the application. 95 * @param userId Indicates the user ID. 96 * @return Returns the uid if successfully obtained; returns -1 otherwise. 97 */ GetUidByBundleName(const std::string & bundleName,const int userId)98 virtual int GetUidByBundleName(const std::string &bundleName, const int userId) override 99 { 100 return 0; 101 } 102 /** 103 * @brief Obtains the bundle name of a specified application based on the given UID. 104 * @param uid Indicates the uid. 105 * @param bundleName Indicates the obtained bundle name. 106 * @return Returns true if the bundle name is successfully obtained; returns false otherwise. 107 */ GetBundleNameForUid(const int uid,std::string & bundleName)108 virtual bool GetBundleNameForUid(const int uid, std::string &bundleName) override 109 { 110 bundleName = "hello"; 111 return true; 112 } 113 /** 114 * @brief Obtains an array of all group IDs associated with a specified bundle. 115 * @param bundleName Indicates the bundle name. 116 * @param gids Indicates the group IDs associated with the specified bundle. 117 * @return Returns true if the gids is successfully obtained; returns false otherwise. 118 */ GetBundleGids(const std::string & bundleName,std::vector<int> & gids)119 virtual bool GetBundleGids(const std::string &bundleName, std::vector<int> &gids) override 120 { 121 return true; 122 } 123 /** 124 * @brief Obtains the type of a specified application based on the given bundle name. 125 * @param bundleName Indicates the bundle name. 126 * @return Returns "system" if the bundle is a system application; returns "third-party" otherwise. 127 */ GetAppType(const std::string & bundleName)128 virtual std::string GetAppType(const std::string &bundleName) override 129 { 130 return ""; 131 } 132 /** 133 * @brief Check whether the app is system app by it's UID. 134 * @param uid Indicates the uid. 135 * @return Returns true if the bundle is a system application; returns false otherwise. 136 */ 137 virtual bool CheckIsSystemAppByUid(const int uid) override; 138 139 /** 140 * @brief Obtains the BundleInfo of application bundles based on the specified metaData. 141 * @param metaData Indicates the metadata to get in the bundle. 142 * @param bundleInfos Indicates all of the obtained BundleInfo objects. 143 * @return Returns true if the BundleInfos is successfully obtained; returns false otherwise. 144 */ GetBundleInfosByMetaData(const std::string & metaData,std::vector<BundleInfo> & bundleInfos)145 virtual bool GetBundleInfosByMetaData(const std::string &metaData, std::vector<BundleInfo> &bundleInfos) override 146 { 147 return true; 148 } 149 /** 150 * @brief Query the AbilityInfo by the given Want. 151 * @param want Indicates the information of the ability. 152 * @param abilityInfo Indicates the obtained AbilityInfo object. 153 * @return Returns true if the AbilityInfo is successfully obtained; returns false otherwise. 154 */ QueryAbilityInfo(const AAFwk::Want & want,AbilityInfo & abilityInfo)155 virtual bool QueryAbilityInfo(const AAFwk::Want &want, AbilityInfo &abilityInfo) override 156 { 157 return true; 158 } 159 /** 160 * @brief Query the AbilityInfo by ability.uri in config.json. 161 * @param abilityUri Indicates the uri of the ability. 162 * @param abilityInfo Indicates the obtained AbilityInfo object. 163 * @return Returns true if the AbilityInfo is successfully obtained; returns false otherwise. 164 */ QueryAbilityInfoByUri(const std::string & abilityUri,AbilityInfo & abilityInfo)165 virtual bool QueryAbilityInfoByUri(const std::string &abilityUri, AbilityInfo &abilityInfo) override 166 { 167 return true; 168 } 169 /** 170 * @brief Obtains the BundleInfo of all keep-alive applications in the system. 171 * @param bundleInfos Indicates all of the obtained BundleInfo objects. 172 * @return Returns true if the BundleInfos is successfully obtained; returns false otherwise. 173 */ QueryKeepAliveBundleInfos(std::vector<BundleInfo> & bundleInfos)174 virtual bool QueryKeepAliveBundleInfos(std::vector<BundleInfo> &bundleInfos) override 175 { 176 return true; 177 }; 178 /** 179 * @brief Obtains the label of a specified ability. 180 * @param bundleName Indicates the bundle name. 181 * @param className Indicates the ability class name. 182 * @return Returns the label of the ability if exist; returns empty string otherwise. 183 */ GetAbilityLabel(const std::string & bundleName,const std::string & className)184 virtual std::string GetAbilityLabel(const std::string &bundleName, const std::string &className) override 185 { 186 return ""; 187 } 188 /** 189 * @brief Obtains information about an application bundle contained in a ohos Ability Package (HAP). 190 * @param hapFilePath Indicates the absolute file path of the HAP. 191 * @param flag Indicates the information contained in the BundleInfo object to be returned. 192 * @param bundleInfo Indicates the obtained BundleInfo object. 193 * @return Returns true if the BundleInfo is successfully obtained; returns false otherwise. 194 */ GetBundleArchiveInfo(const std::string & hapFilePath,const BundleFlag flag,BundleInfo & bundleInfo)195 virtual bool GetBundleArchiveInfo( 196 const std::string &hapFilePath, const BundleFlag flag, BundleInfo &bundleInfo) override 197 { 198 return true; 199 } 200 /** 201 * @brief Obtain the HAP module info of a specific ability. 202 * @param abilityInfo Indicates the ability. 203 * @param hapModuleInfo Indicates the obtained HapModuleInfo object. 204 * @return Returns true if the HapModuleInfo is successfully obtained; returns false otherwise. 205 */ GetHapModuleInfo(const AbilityInfo & abilityInfo,HapModuleInfo & hapModuleInfo)206 virtual bool GetHapModuleInfo(const AbilityInfo &abilityInfo, HapModuleInfo &hapModuleInfo) override 207 { 208 return true; 209 } 210 /** 211 * @brief Obtains the Want for starting the main ability of an application based on the given bundle name. 212 * @param bundleName Indicates the bundle name. 213 * @param want Indicates the obtained launch Want object. 214 * @return Returns true if the launch Want object is successfully obtained; returns false otherwise. 215 */ GetLaunchWantForBundle(const std::string & bundleName,Want & want)216 virtual bool GetLaunchWantForBundle(const std::string &bundleName, Want &want) override 217 { 218 return true; 219 } 220 /** 221 * @brief Confirms with the permission management module to check whether a request prompt is required for granting 222 * a certain permission. 223 * @param bundleName Indicates the name of the bundle to check. 224 * @param permission Indicates the permission to check. 225 * @param userId Indicates the user id. 226 * @return Returns true if the current application does not have the permission and the user does not turn off 227 * further requests; returns false if the current application already has the permission, the permission is rejected 228 * by the system, or the permission is denied by the user and the user has turned off further requests. 229 */ CanRequestPermission(const std::string & bundleName,const std::string & permissionName,const int userId)230 virtual bool CanRequestPermission( 231 const std::string &bundleName, const std::string &permissionName, const int userId) override 232 { 233 return true; 234 } 235 /** 236 * @brief Checks whether the publickeys of two bundles are the same. 237 * @param firstBundleName Indicates the first bundle name. 238 * @param secondBundleName Indicates the second bundle name. 239 * @return Returns SIGNATURE_UNKNOWN_BUNDLE if at least one of the given bundles is not found; 240 * returns SIGNATURE_NOT_MATCHED if their publickeys are different; 241 * returns SIGNATURE_MATCHED if their publickeys are the same. 242 */ CheckPublicKeys(const std::string & firstBundleName,const std::string & secondBundleName)243 virtual int CheckPublicKeys(const std::string &firstBundleName, const std::string &secondBundleName) override 244 { 245 return 0; 246 } 247 /** 248 * @brief Checks whether a specified bundle has been granted a specific permission. 249 * @param bundleName Indicates the name of the bundle to check. 250 * @param permission Indicates the permission to check. 251 * @return Returns 0 if the bundle has the permission; returns -1 otherwise. 252 */ 253 virtual int CheckPermission(const std::string &bundleName, const std::string &permission) override; 254 /** 255 * @brief Obtains detailed information about a specified permission. 256 * @param permissionName Indicates the name of the ohos permission. 257 * @param permissionDef Indicates the object containing detailed information about the given ohos permission. 258 * @return Returns true if the PermissionDef object is successfully obtained; returns false otherwise. 259 */ GetPermissionDef(const std::string & permissionName,PermissionDef & permissionDef)260 virtual bool GetPermissionDef(const std::string &permissionName, PermissionDef &permissionDef) override 261 { 262 return true; 263 } 264 /** 265 * @brief Obtains all known permission groups in the system. 266 * @param permissionDefs Indicates the list of objects containing the permission group information. 267 * @return Returns true if the PermissionDef objects is successfully obtained; returns false otherwise. 268 */ GetAllPermissionGroupDefs(std::vector<PermissionDef> & permissionDefs)269 virtual bool GetAllPermissionGroupDefs(std::vector<PermissionDef> &permissionDefs) override 270 { 271 return true; 272 } 273 /** 274 * @brief Obtains all known permission groups in the system. 275 * @param permissions Indicates the permission array. 276 * @param appNames Indicates the list of application names that have the specified permissions. 277 * @return Returns true if the application names is successfully obtained; returns false otherwise. 278 */ GetAppsGrantedPermissions(const std::vector<std::string> & permissions,std::vector<std::string> & appNames)279 virtual bool GetAppsGrantedPermissions( 280 const std::vector<std::string> &permissions, std::vector<std::string> &appNames) override 281 { 282 return true; 283 } 284 /** 285 * @brief Requests a certain permission from user. 286 * @param bundleName Indicates the name of the bundle to request permission. 287 * @param permission Indicates the permission to request permission. 288 * @param userId Indicates the user id. 289 * @return Returns true if the permission request successfully; returns false otherwise. 290 */ RequestPermissionFromUser(const std::string & bundleName,const std::string & permission,const int userId)291 virtual bool RequestPermissionFromUser( 292 const std::string &bundleName, const std::string &permission, const int userId) override 293 { 294 return true; 295 } 296 /** 297 * @brief Checks whether the system has a specified capability. 298 * @param capName Indicates the name of the system feature to check. 299 * @return Returns true if the given feature specified by name is available in the system; returns false otherwise. 300 */ HasSystemCapability(const std::string & capName)301 virtual bool HasSystemCapability(const std::string &capName) override 302 { 303 return true; 304 } 305 /** 306 * @brief Obtains the capabilities that are available in the system. 307 * @param systemCaps Indicates the list of capabilities available in the system. 308 * @return Returns true if capabilities in the system are successfully obtained; returns false otherwise. 309 */ GetSystemAvailableCapabilities(std::vector<std::string> & systemCaps)310 virtual bool GetSystemAvailableCapabilities(std::vector<std::string> &systemCaps) override 311 { 312 return true; 313 } 314 /** 315 * @brief Checks whether the current device has been started in safe mode. 316 * @return Returns true if the device is in safe mode; returns false otherwise. 317 */ IsSafeMode()318 virtual bool IsSafeMode() override 319 { 320 return true; 321 } 322 /** 323 * @brief Clears application running data of a specified application. 324 * @param bundleName Indicates the bundle name of the application whose data is to be cleared. 325 * @return Returns true if the data cleared successfully; returns false otherwise. 326 */ CleanBundleDataFiles(const std::string & bundleName,const int userId)327 virtual bool CleanBundleDataFiles(const std::string &bundleName, const int userId) override 328 { 329 return true; 330 } 331 /** 332 * @brief Register the specific bundle status callback. 333 * @param bundleStatusCallback Indicates the callback to be invoked for returning the bundle status changed result. 334 * @return Returns true if this function is successfully called; returns false otherwise. 335 */ RegisterBundleStatusCallback(const sptr<IBundleStatusCallback> & bundleStatusCallback)336 virtual bool RegisterBundleStatusCallback(const sptr<IBundleStatusCallback> &bundleStatusCallback) override 337 { 338 return true; 339 } 340 /** 341 * @brief Clear the specific bundle status callback. 342 * @param bundleStatusCallback Indicates the callback to be cleared. 343 * @return Returns true if this function is successfully called; returns false otherwise. 344 */ ClearBundleStatusCallback(const sptr<IBundleStatusCallback> & bundleStatusCallback)345 virtual bool ClearBundleStatusCallback(const sptr<IBundleStatusCallback> &bundleStatusCallback) override 346 { 347 return true; 348 } 349 /** 350 * @brief Unregister all the callbacks of status changed. 351 * @return Returns true if this function is successfully called; returns false otherwise. 352 */ UnregisterBundleStatusCallback()353 virtual bool UnregisterBundleStatusCallback() override 354 { 355 return true; 356 } 357 /** 358 * @brief Dump the bundle informations with specific flags. 359 * @param flag Indicates the information contained in the dump result. 360 * @param bundleName Indicates the bundle name if needed. 361 * @param userId Indicates the user ID. 362 * @param result Indicates the dump information result. 363 * @return Returns true if the dump result is successfully obtained; returns false otherwise. 364 */ DumpInfos(const DumpFlag flag,const std::string & bundleName,int32_t userId,std::string & result)365 virtual bool DumpInfos( 366 const DumpFlag flag, const std::string &bundleName, int32_t userId, std::string &result) override 367 { 368 return true; 369 } 370 /** 371 * @brief Checks whether a specified application is enabled. 372 * @param bundleName Indicates the bundle name of the application. 373 * @return Returns true if the application is enabled; returns false otherwise. 374 */ IsApplicationEnabled(const std::string & bundleName)375 virtual bool IsApplicationEnabled(const std::string &bundleName) override 376 { 377 return true; 378 } 379 /** 380 * @brief Obtains the interface used to install and uninstall bundles. 381 * @return Returns a pointer to IBundleInstaller class if exist; returns nullptr otherwise. 382 */ GetBundleInstaller()383 virtual sptr<IBundleInstaller> GetBundleInstaller() override 384 { 385 return nullptr; 386 } 387 /** 388 * @brief Obtains the interface used to create or delete user. 389 * @return Returns a pointer to IBundleUserMgr class if exist; returns nullptr otherwise. 390 */ GetBundleUserMgr()391 virtual sptr<IBundleUserMgr> GetBundleUserMgr() override 392 { 393 return nullptr; 394 } 395 396 void MockSetIsSystemApp(bool isSystemApp); 397 398 private: 399 bool isSystemApp_ = false; 400 bool isSystemAppMock_ = false; 401 }; 402 } // namespace AppExecFwk 403 } // namespace OHOS 404 405 #endif // FOUNDATION_EVENT_CESFWK_SERVICES_TEST_UNITTEST_MOCK_INCLUDE_MOCK_BUNDLE_MANAGER_H