diff --git a/services/abilitymgr/include/utils/uri_utils.h b/services/abilitymgr/include/utils/uri_utils.h index 61e3f1c6100688daa3d919a41d385689531be273..ed19ae33653a8d6723c327cc59ec55cd364f5c16 100644 --- a/services/abilitymgr/include/utils/uri_utils.h +++ b/services/abilitymgr/include/utils/uri_utils.h @@ -42,18 +42,18 @@ public: bool IsServiceExtensionType(AppExecFwk::ExtensionAbilityType extensionAbilityType); #ifdef SUPPORT_UPMS - void GrantDmsUriPermission(Want &want, uint32_t callerTokenId, std::string targetBundleName, int32_t appIndex); + bool GrantDmsUriPermission(Want &want, uint32_t callerTokenId, std::string targetBundleName, int32_t appIndex); void GrantUriPermissionForServiceExtension(const AbilityRequest &abilityRequest); void GrantUriPermissionForUIOrServiceExtension(const AbilityRequest &abilityRequest); - void GrantUriPermission(Want &want, std::string targetBundleName, int32_t appIndex, + bool GrantUriPermission(Want &want, std::string targetBundleName, int32_t appIndex, bool isSandboxApp, uint32_t callerTokenId, int32_t collaboratorType); - void CheckUriPermission(uint32_t callerTokenId, Want &want); + bool CheckUriPermission(uint32_t callerTokenId, Want &want); - void GrantUriPermission(const std::vector &uriVec, int32_t flag, + bool GrantUriPermission(const std::vector &uriVec, int32_t flag, const std::string &targetBundleName, int32_t appIndex, uint32_t initiatorTokenId); #endif // SUPPORT_UPMS private: @@ -74,7 +74,7 @@ private: std::vector GetUriListFromWantDms(Want &want); - void PublishFileOpenEvent(const Want &want); + bool PublishFileOpenEvent(const Want &want); bool IsInAncoAppIdentifier(const std::string &bundleName); diff --git a/services/abilitymgr/src/utils/uri_utils.cpp b/services/abilitymgr/src/utils/uri_utils.cpp index 3ac7370b22e9eebde4c98384a3ea482f9d6f3dc8..48fc745d7ade218aee06ed805a5a9117e4ed247e 100644 --- a/services/abilitymgr/src/utils/uri_utils.cpp +++ b/services/abilitymgr/src/utils/uri_utils.cpp @@ -229,7 +229,7 @@ bool UriUtils::IsDmsCall(uint32_t fromTokenId) } #ifdef SUPPORT_UPMS -void UriUtils::GrantDmsUriPermission(Want &want, uint32_t callerTokenId, +bool UriUtils::GrantDmsUriPermission(Want &want, uint32_t callerTokenId, std::string targetBundleName, int32_t appIndex) { HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); @@ -239,8 +239,9 @@ void UriUtils::GrantDmsUriPermission(Want &want, uint32_t callerTokenId, want.GetFlags(), targetBundleName, appIndex, callerTokenId, hideSensitiveType)); if (ret != ERR_OK) { TAG_LOGD(AAFwkTag::ABILITYMGR, "ret is %{public}d.", ret); - return; + return false; } + return true; } bool UriUtils::GrantShellUriPermission(const std::vector &strUriVec, uint32_t flag, @@ -266,29 +267,30 @@ bool UriUtils::GrantShellUriPermission(const std::vector &strUriVec return true; } -void UriUtils::CheckUriPermission(uint32_t callerTokenId, Want &want) +bool UriUtils::CheckUriPermission(uint32_t callerTokenId, Want &want) { // Check and clear no-permission uris in want, not support content uri uint32_t flag = want.GetFlags(); if (!IsGrantUriPermissionFlag(want)) { TAG_LOGD(AAFwkTag::ABILITYMGR, "No grant uri flag: %{public}u.", flag); - return; + return false; } std::vector uriVec; if (!UriUtils::GetUriListFromWant(want, uriVec)) { TAG_LOGD(AAFwkTag::ABILITYMGR, "No file uri neet grant."); - return; + return false; } auto checkResults = IN_PROCESS_CALL(UriPermissionManagerClient::GetInstance().CheckUriAuthorization( uriVec, flag, callerTokenId)); auto permissionUris = GetPermissionedUriList(uriVec, checkResults, want); if (permissionUris.empty()) { TAG_LOGE(AAFwkTag::ABILITYMGR, "all uris not permissioned."); - return; + return false; } + return true; } -void UriUtils::GrantUriPermission(const std::vector &uriVec, int32_t flag, +bool UriUtils::GrantUriPermission(const std::vector &uriVec, int32_t flag, const std::string &targetBundleName, int32_t appIndex, uint32_t initiatorTokenId) { std::vector permissionUris; @@ -297,15 +299,15 @@ void UriUtils::GrantUriPermission(const std::vector &uriVec, int32_ permissionUris.emplace_back(uri); } if (permissionUris.empty()) { - return; + return false; } auto ret = IN_PROCESS_CALL(UriPermissionManagerClient::GetInstance().GrantUriPermission(permissionUris, flag, targetBundleName, appIndex, initiatorTokenId)); if (ret != ERR_OK) { TAG_LOGE(AAFwkTag::ABILITYMGR, "failed, err:%{public}d", ret); + return false; } - - return; + return true; } #endif // SUPPORT_UPMS @@ -325,39 +327,38 @@ bool UriUtils::IsSandboxApp(uint32_t tokenId) } #ifdef SUPPORT_UPMS -void UriUtils::GrantUriPermission(Want &want, std::string targetBundleName, int32_t appIndex, +bool UriUtils::GrantUriPermission(Want &want, std::string targetBundleName, int32_t appIndex, bool isSandboxApp, uint32_t callerTokenId, int32_t collaboratorType) { uint32_t flag = want.GetFlags(); if (!IsGrantUriPermissionFlag(want)) { TAG_LOGD(AAFwkTag::ABILITYMGR, "No grant uri flag: %{public}u.", flag); - return; + return false; } if (targetBundleName == AppUtils::GetInstance().GetBrokerDelegateBundleName() && collaboratorType == CollaboratorType::OTHERS_TYPE) { TAG_LOGD(AAFwkTag::ABILITYMGR, "reject shell application to grant uri permission"); - return; + return false; } if (callerTokenId == 0) { TAG_LOGE(AAFwkTag::ABILITYMGR, "callerTokenId is invalid."); - return; + return false; } if (isSandboxApp || IsSandboxApp(callerTokenId)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "sandbox can not grant UriPermission"); - return; + return false; } ProcessUDMFKey(want); if (IsDmsCall(callerTokenId)) { - GrantDmsUriPermission(want, callerTokenId, targetBundleName, appIndex); - return; + return GrantDmsUriPermission(want, callerTokenId, targetBundleName, appIndex); } std::vector uriVec; if (!UriUtils::GetUriListFromWant(want, uriVec)) { TAG_LOGW(AAFwkTag::ABILITYMGR, "No file uri neet grant."); - return; + return false; } auto callerPkg = want.GetStringParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME); @@ -365,13 +366,14 @@ void UriUtils::GrantUriPermission(Want &want, std::string targetBundleName, int3 IPCSkeleton::GetCallingUid() == AppUtils::GetInstance().GetCollaboratorBrokerUID()); if (isBrokerCall && GrantShellUriPermission(uriVec, flag, targetBundleName, appIndex)) { TAG_LOGI(AAFwkTag::ABILITYMGR, "permission to shell"); - return; + return true; } if (!GrantUriPermissionInner(uriVec, callerTokenId, targetBundleName, appIndex, want)) { - return; + return false; } // report open file event PublishFileOpenEvent(want); + return true; } void UriUtils::ProcessUDMFKey(Want &want) @@ -406,7 +408,7 @@ bool UriUtils::GrantUriPermissionInner(std::vector uriVec, uint32_t } #endif // SUPPORT_UPMS -void UriUtils::PublishFileOpenEvent(const Want &want) +bool UriUtils::PublishFileOpenEvent(const Want &want) { auto wangUri = want.GetUri(); std::string uriStr = wangUri.ToString(); @@ -430,7 +432,9 @@ void UriUtils::PublishFileOpenEvent(const Want &want) commonEventPublishInfo.SetSubscriberPermissions(subscriberPermissions); IN_PROCESS_CALL(EventFwk::CommonEventManager::PublishCommonEventAsUser(commonData, commonEventPublishInfo, userId)); + return true; } + return false; } #ifdef SUPPORT_UPMS diff --git a/test/new_test/mock/ability_common/app_utils.h b/test/new_test/mock/ability_common/app_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..96632c71b813464a8ab33c3e4d72c1fee06669d0 --- /dev/null +++ b/test/new_test/mock/ability_common/app_utils.h @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ABILITY_RUNTIME_APP_UTILS_H +#define OHOS_ABILITY_RUNTIME_APP_UTILS_H + +#include +#include + +#include "nocopyable.h" +#include "oh_mock_utils.h" + +namespace OHOS { +namespace AAFwk { + +/** + * @class AppUtils + * provides app utils. + */ +class AppUtils { +public: + static AppUtils &GetInstance() + { + static AppUtils utils; + return utils; + } + ~AppUtils() {} + + OH_MOCK_METHOD(bool, AppUtils, IsLauncher, const std::string&); + OH_MOCK_METHOD(bool, AppUtils, IsLauncherAbility, const std::string&); + OH_MOCK_METHOD(bool, AppUtils, IsInheritWindowSplitScreenMode); + OH_MOCK_METHOD(bool, AppUtils, IsSupportAncoApp); + OH_MOCK_METHOD(bool, AppUtils, GetTimeoutUnitTimeRatio); + OH_MOCK_METHOD(bool, AppUtils, IsSelectorDialogDefaultPossion); + OH_MOCK_METHOD(bool, AppUtils, IsStartSpecifiedProcess); + OH_MOCK_METHOD(bool, AppUtils, IsUseMultiRenderProcess); + OH_MOCK_METHOD(bool, AppUtils, IsLimitMaximumOfRenderProcess); + OH_MOCK_METHOD(bool, AppUtils, IsGrantPersistUriPermission); + OH_MOCK_METHOD(bool, AppUtils, IsSupportGrantUriPermission); + OH_MOCK_METHOD(bool, AppUtils, IsStartOptionsWithAnimation); + OH_MOCK_METHOD(bool, AppUtils, IsMultiProcessModel); + OH_MOCK_METHOD(bool, AppUtils, AllowChildProcessInMultiProcessFeatureApp); + OH_MOCK_METHOD(bool, AppUtils, IsStartOptionsWithProcessOptions); + OH_MOCK_METHOD(bool, AppUtils, EnableMoveUIAbilityToBackgroundApi); + OH_MOCK_METHOD(bool, AppUtils, IsLaunchEmbededUIAbility); + OH_MOCK_METHOD(bool, AppUtils, IsSupportNativeChildProcess); + OH_MOCK_METHOD(bool, AppUtils, IsSupportMultiInstance); + OH_MOCK_METHOD(bool, AppUtils, IsAllowNativeChildProcess, const std::string&); + OH_MOCK_METHOD(bool, AppUtils, GetLimitMaximumExtensionsPerProc); + OH_MOCK_METHOD(bool, AppUtils, IsBigMemoryUnrelatedKeepAliveProc, const std::string&); + OH_MOCK_METHOD(bool, AppUtils, IsRequireBigMemoryProcess, const std::string&); + OH_MOCK_METHOD(bool, AppUtils, GetLimitMaximumExtensionsPerDevice); + OH_MOCK_METHOD(bool, AppUtils, GetCacheExtensionTypeList); + OH_MOCK_METHOD(std::string, AppUtils, GetAncoAppIdentifiers); + OH_MOCK_METHOD(bool, AppUtils, IsAllowStartAbilityWithoutCallerToken, const std::string&, const std::string&); + OH_MOCK_METHOD(std::string, AppUtils, GetBrokerDelegateBundleName); + OH_MOCK_METHOD(int32_t, AppUtils, GetCollaboratorBrokerUID); + OH_MOCK_METHOD(int32_t, AppUtils, GetCollaboratorBrokerReserveUID); + OH_MOCK_METHOD(int32_t, AppUtils, MaxChildProcess); + OH_MOCK_METHOD(int32_t, AppUtils, MaxMultiProcessFeatureChildProcess); + OH_MOCK_METHOD(std::string, AppUtils, GetMigrateClientBundleName); + OH_MOCK_METHOD(bool, AppUtils, IsConnectSupportCrossUser); + OH_MOCK_METHOD(bool, AppUtils, IsSupportAppServiceExtension); + OH_MOCK_METHOD(bool, AppUtils, IsPrepareTerminateEnabled); + OH_MOCK_METHOD(bool, AppUtils, IsCacheExtensionAbilityByList); + OH_MOCK_METHOD(bool, AppUtils, IsSystemReasonMessage, const std::string&); + OH_MOCK_METHOD(bool, AppUtils, InResidentWhiteList, const std::string&); + OH_MOCK_METHOD(const std::vector&, AppUtils, GetResidentWhiteList); + + bool IsAllowResidentInExtremeMemory(const std::string& bundleName, const std::string& abilityName = "") + { + return false; + } + +private: + AppUtils() {} +}; +} // namespace AAFwk +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_APP_UTILS_H \ No newline at end of file diff --git a/test/new_test/mock/security/accesstoken_kit.cpp b/test/new_test/mock/security/accesstoken_kit.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2831b2b0306625eb02a498a51c0da1f2e54f020 --- /dev/null +++ b/test/new_test/mock/security/accesstoken_kit.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "accesstoken_kit.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +NativeTokenInfo AccessTokenKit::nativeTokenInfo = NativeTokenInfo(); +int32_t AccessTokenKit::getNativeTokenInfoRet = 0; +HapTokenInfo AccessTokenKit::hapInfo = HapTokenInfo(); +int32_t AccessTokenKit::getHapTokenInfoRet = 0; +ATokenTypeEnum AccessTokenKit::getTokenTypeFlagRet = ATokenTypeEnum::TOKEN_INVALID; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/test/new_test/mock/security/accesstoken_kit.h b/test/new_test/mock/security/accesstoken_kit.h new file mode 100644 index 0000000000000000000000000000000000000000..08b0ed445110b17be9379ac512d4912a0fbf932c --- /dev/null +++ b/test/new_test/mock/security/accesstoken_kit.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_INNER_KITS_ACCESSTOKEN_KIT_H +#define INTERFACES_INNER_KITS_ACCESSTOKEN_KIT_H + +#include "access_token.h" +#include "native_token_info.h" +#include "hap_token_info.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { + +class AccessTokenKit { +public: + static ATokenTypeEnum GetTokenTypeFlag(AccessTokenID tokenID) + { + return getTokenTypeFlagRet; + } + + static int32_t GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo &nativeTokenInfoRes) + { + nativeTokenInfoRes = nativeTokenInfo; + return getNativeTokenInfoRet; + } + + static int32_t GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo &hapInfoRes) + { + hapInfoRes = hapInfo; + return getHapTokenInfoRet; + } + + static void InitMockResult() + { + getNativeTokenInfoRet = 0; + getHapTokenInfoRet = 0; + nativeTokenInfo = NativeTokenInfo(); + hapInfo = HapTokenInfo(); + getTokenTypeFlagRet = ATokenTypeEnum::TOKEN_INVALID; + } + + static NativeTokenInfo nativeTokenInfo; + static HapTokenInfo hapInfo; + static int32_t getNativeTokenInfoRet; + static int32_t getHapTokenInfoRet; + static ATokenTypeEnum getTokenTypeFlagRet; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif diff --git a/test/new_test/mock/uri_permission_manager/policy_info.h b/test/new_test/mock/uri_permission_manager/policy_info.h new file mode 100644 index 0000000000000000000000000000000000000000..8b0646e46825ba3cb1ef9021e37fa6e30a9ea1da --- /dev/null +++ b/test/new_test/mock/uri_permission_manager/policy_info.h @@ -0,0 +1,31 @@ +/* +* Copyright (c) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef OHOS_ABILITY_RUNTIME_UPMS_POLICY_INFO_H +#define OHOS_ABILITY_RUNTIME_UPMS_POLICY_INFO_H + +#include + +namespace OHOS { +namespace AAFwk { + +struct PolicyInfo final { + std::string path; + uint64_t mode; +}; + +} // AAFwk +} // OHOS +#endif // OHOS_ABILITY_RUNTIME_UPMS_POLICY_INFO_H \ No newline at end of file diff --git a/test/new_test/mock/uri_permission_manager/uri_permission_manager_client.cpp b/test/new_test/mock/uri_permission_manager/uri_permission_manager_client.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d4a66f6960c36e5451b1b6ab4c60d29da16f0e55 --- /dev/null +++ b/test/new_test/mock/uri_permission_manager/uri_permission_manager_client.cpp @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "uri_permission_manager_client.h" + +namespace OHOS { +namespace AAFwk { +int32_t UriPermissionManagerClient::grantUriPermissionPrivilegedRet = 0; +} // namespace AAFwk +} // namespace OHOS \ No newline at end of file diff --git a/test/new_test/mock/uri_permission_manager/uri_permission_manager_client.h b/test/new_test/mock/uri_permission_manager/uri_permission_manager_client.h new file mode 100644 index 0000000000000000000000000000000000000000..36af828973f498b0995c85443fe41fe911cb35c8 --- /dev/null +++ b/test/new_test/mock/uri_permission_manager/uri_permission_manager_client.h @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ABILITY_RUNTIME_URI_PERMISSION_MANAGER_CLIENT_H +#define OHOS_ABILITY_RUNTIME_URI_PERMISSION_MANAGER_CLIENT_H + +#include "uri.h" +#include "policy_info.h" +#include "oh_mock_utils.h" + +namespace OHOS { +namespace AAFwk { +class UriPermissionManagerClient { +public: + static UriPermissionManagerClient& GetInstance() + { + static UriPermissionManagerClient instance; + return instance; + } + + ~UriPermissionManagerClient() = default; + + OH_MOCK_METHOD(int32_t, UriPermissionManagerClient, GrantUriPermission, const Uri&, + unsigned int, const std::string, int32_t, uint32_t); + + OH_MOCK_METHOD(int32_t, UriPermissionManagerClient, GrantUriPermission, const std::vector&, + unsigned int, const std::string, int32_t, uint32_t); + + int32_t GrantUriPermissionPrivileged(const std::vector &uriVec, uint32_t flag, + const std::string &targetBundleName, int32_t appIndex = 0, uint32_t initiatorTokenId = 0, + int32_t hideSensitiveType = 0) + { + return grantUriPermissionPrivilegedRet; + } + + int32_t RevokeAllUriPermissions(const uint32_t tokenId) + { + return 0; + } + + int32_t RevokeUriPermissionManually(const Uri &uri, const std::string bundleName, int32_t appIndex = 0) + { + return 0; + } + + bool VerifyUriPermission(const Uri &uri, uint32_t flag, uint32_t tokenId) + { + return true; + } + + OH_MOCK_METHOD(std::vector, UriPermissionManagerClient, CheckUriAuthorization, + const std::vector&, uint32_t, uint32_t); + + int32_t ClearPermissionTokenByMap(const uint32_t tokenId) + { + return 0; + } + + int32_t GrantUriPermissionByKey(const std::string &key, uint32_t flag, uint32_t targetTokenId) + { + return 0; + } + + int32_t GrantUriPermissionByKeyAsCaller(const std::string &key, uint32_t flag, uint32_t callerTokenId, + uint32_t targetTokenId) + { + return 0; + } + + int32_t Active(const std::vector &policy, std::vector &result) + { + return 0; + } +public: + static void InitMockResult() + { + grantUriPermissionPrivilegedRet = 0; + } + static int32_t grantUriPermissionPrivilegedRet; +}; +} // namespace AAFwk +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_URI_PERMISSION_MANAGER_CLIENT_H diff --git a/test/new_test/mock_utils/oh_mock_utils.h b/test/new_test/mock_utils/oh_mock_utils.h index 5987df848516d549914e6297ffc5e34d36ae9f9d..2ab8b8cea09222f615bcf1adffda2e7329442edc 100644 --- a/test/new_test/mock_utils/oh_mock_utils.h +++ b/test/new_test/mock_utils/oh_mock_utils.h @@ -215,4 +215,12 @@ do { g_mockMap[key] = v; \ } while (0) +/** + * @brief Initial global mock map. + */ +#define OH_MOCK_INIT_ALL_EXPECT_RET() \ +do { \ + g_mockMap.clear(); \ +} while (0) + #endif // OHOS_MOCK_UTIL_H diff --git a/test/unittest/ui_ability_lifecycle_manager_test/ui_ability_lifecycle_manager_test.cpp b/test/unittest/ui_ability_lifecycle_manager_test/ui_ability_lifecycle_manager_test.cpp index 4aae8d6d20a4055358ab27d898a072c88df7cd5a..26acde63e9f30f94db9a7c1756bc73c47d90bac4 100644 --- a/test/unittest/ui_ability_lifecycle_manager_test/ui_ability_lifecycle_manager_test.cpp +++ b/test/unittest/ui_ability_lifecycle_manager_test/ui_ability_lifecycle_manager_test.cpp @@ -5808,5 +5808,273 @@ HWTEST_F(UIAbilityLifecycleManagerTest, BackToCallerAbilityWithResultLocked_0002 int ret = mgr->BackToCallerAbilityWithResultLocked(currentSessionInfo, callerAbilityRecord); EXPECT_EQ(ret, ERR_INVALID_VALUE); } + +/** + * @tc.name: CloseUIAbility_0001 + * @tc.desc: GetEventHandler is null + */ +HWTEST_F(UIAbilityLifecycleManagerTest, CloseUIAbility_0001, TestSize.Level1) +{ + auto mgr = std::make_shared(); + AbilityRequest request; + request.abilityInfo.name = "AbilityA"; + auto record = AbilityRecord::CreateAbilityRecord(request); + record->SetAbilityState(AbilityState::INITIAL); + int ret = mgr->CloseUIAbility(record, 0, nullptr, false); + EXPECT_EQ(ret, ERR_INVALID_VALUE); +} + +/** + * @tc.name: CloseUIAbility_0002 + * @tc.desc: abilityRecord is debug and isClearSession is true, should call TerminateAbility + */ +HWTEST_F(UIAbilityLifecycleManagerTest, CloseUIAbility_DebugAndClearSession, TestSize.Level1) +{ + auto mgr = std::make_shared(); + AbilityRequest request; + request.abilityInfo.name = "AbilityB"; + request.abilityInfo.applicationInfo.debug = true; + auto record = AbilityRecord::CreateAbilityRecord(request); + record->SetAbilityState(AbilityState::FOREGROUND); + int ret = mgr->CloseUIAbility(record, 0, nullptr, true); + EXPECT_EQ(ret, 0); +} + +/** + * @tc.name: CloseUIAbility_0003 + * @tc.desc: abilityRecord pending state is not INITIAL, should set to BACKGROUND and return ERR_OK + */ +HWTEST_F(UIAbilityLifecycleManagerTest, CloseUIAbility_0003, TestSize.Level1) +{ + auto mgr = std::make_shared(); + AbilityRequest request; + request.abilityInfo.name = "AbilityC"; + auto record = AbilityRecord::CreateAbilityRecord(request); + record->SetAbilityState(AbilityState::FOREGROUND); + record->SetPendingState(AbilityState::FOREGROUND); + int ret = mgr->CloseUIAbility(record, 0, nullptr, false); + EXPECT_EQ(record->GetPendingState(), AbilityState::BACKGROUND); + EXPECT_EQ(ret, ERR_OK); +} + +/** + * @tc.name: DelayCompleteTerminate_002 + * @tc.desc: Should call PrintTimeOutLog and schedule CompleteTerminate task + */ +HWTEST_F(UIAbilityLifecycleManagerTest, DelayCompleteTerminate_002, TestSize.Level1) +{ + auto mgr = std::make_shared(); + AbilityRequest request; + request.abilityInfo.name = "AbilityA"; + auto record = AbilityRecord::CreateAbilityRecord(request); + int oldState = record->GetAbilityState(); + mgr->DelayCompleteTerminate(record); + + bool completed = false; + for (int i = 0; i < 50; ++i) { + if (record->GetAbilityState() != oldState) { + completed = true; + break; + } + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } + EXPECT_TRUE(completed == oldState); +} + +/** + * @tc.name: GetPersistentIdByAbilityRequest_0001 + * @tc.desc: Should return 0 when launchMode is not SINGLETON + */ +HWTEST_F(UIAbilityLifecycleManagerTest, GetPersistentIdByAbilityRequest_0001, TestSize.Level1) +{ + auto mgr = std::make_shared(); + AbilityRequest request; + request.collaboratorType = CollaboratorType::DEFAULT_TYPE; + request.abilityInfo.launchMode = AppExecFwk::LaunchMode::STANDARD; + bool reuse = false; + int32_t ret = mgr->GetPersistentIdByAbilityRequest(request, reuse); + EXPECT_EQ(ret, 0); +} + +/** + * @tc.name: OnTimeOut_0001 + * @tc.desc: abilityRecord not found in both sessionAbilityMap_ and terminateAbilityList_ + */ +HWTEST_F(UIAbilityLifecycleManagerTest, OnTimeOut_0001, TestSize.Level1) +{ + auto mgr = std::make_shared(); + uint32_t msgId = 1; + int64_t abilityRecordId = 99999; + bool isHalf = false; + bool found = false; + for (const auto &[_, record] : mgr->sessionAbilityMap_) { + if (record && record->GetAbilityRecordId() == abilityRecordId) { + found = true; + break; + } + } + for (const auto &record : mgr->terminateAbilityList_) { + if (record && record->GetAbilityRecordId() == abilityRecordId) { + found = true; + break; + } + } + EXPECT_FALSE(found); + + mgr->OnTimeOut(msgId, abilityRecordId, isHalf); + + found = false; + for (const auto &[_, record] : mgr->sessionAbilityMap_) { + if (record && record->GetAbilityRecordId() == abilityRecordId) { + found = true; + break; + } + } + for (const auto &record : mgr->terminateAbilityList_) { + if (record && record->GetAbilityRecordId() == abilityRecordId) { + found = true; + break; + } + } + EXPECT_FALSE(found); +} + +/** + * @tc.name: OnTimeOut_0002 + * @tc.desc: abilityRecord found in sessionAbilityMap_, isHalf is true, should only call PrintTimeOutLog + */ +HWTEST_F(UIAbilityLifecycleManagerTest, OnTimeOut_0002, TestSize.Level1) +{ + auto mgr = std::make_shared(); + AbilityRequest request; + request.abilityInfo.name = "AbilityA"; + auto record = AbilityRecord::CreateAbilityRecord(request); + int64_t abilityRecordId = record->GetAbilityRecordId(); + mgr->sessionAbilityMap_[1] = record; + + uint32_t msgId = 1; + bool isHalf = true; + mgr->OnTimeOut(msgId, abilityRecordId, isHalf); + EXPECT_EQ(record->GetAbilityRecordId(), abilityRecordId); +} + +/** + * @tc.name: OnTimeOut_0003 + * @tc.desc: abilityRecord found in terminateAbilityList_, isHalf is true, should only call PrintTimeOutLog + */ +HWTEST_F(UIAbilityLifecycleManagerTest, OnTimeOut_0003, TestSize.Level1) +{ + auto mgr = std::make_shared(); + AbilityRequest request; + request.abilityInfo.name = "AbilityB"; + auto record = AbilityRecord::CreateAbilityRecord(request); + int64_t abilityRecordId = record->GetAbilityRecordId(); + mgr->terminateAbilityList_.push_back(record); + + uint32_t msgId = 1; + bool isHalf = true; + mgr->OnTimeOut(msgId, abilityRecordId, isHalf); + EXPECT_EQ(record->GetAbilityRecordId(), abilityRecordId); +} + +/** + * @tc.name: OnTimeOut_0004 + * @tc.desc: abilityRecord found, isHalf is false, msgId is LOAD_TIMEOUT_MSG + */ +HWTEST_F(UIAbilityLifecycleManagerTest, OnTimeOut_0004, TestSize.Level1) +{ + auto mgr = std::make_shared(); + AbilityRequest request; + request.abilityInfo.name = "AbilityC"; + auto record = AbilityRecord::CreateAbilityRecord(request); + int64_t abilityRecordId = record->GetAbilityRecordId(); + mgr->sessionAbilityMap_[2] = record; + + uint32_t msgId = AbilityManagerService::LOAD_TIMEOUT_MSG; + bool isHalf = false; + record->SetLoading(true); + mgr->OnTimeOut(msgId, abilityRecordId, isHalf); + EXPECT_FALSE(record->IsLoading()); +} + +/** + * @tc.name: OnTimeOut_0005 + * @tc.desc: abilityRecord found, isHalf is false, msgId is FOREGROUND_TIMEOUT_MSG + */ +HWTEST_F(UIAbilityLifecycleManagerTest, OnTimeOut_0005, TestSize.Level1) +{ + auto mgr = std::make_shared(); + AbilityRequest request; + request.abilityInfo.name = "AbilityD"; + auto record = AbilityRecord::CreateAbilityRecord(request); + int64_t abilityRecordId = record->GetAbilityRecordId(); + mgr->sessionAbilityMap_[3] = record; + + uint32_t msgId = AbilityManagerService::FOREGROUND_TIMEOUT_MSG; + bool isHalf = false; + mgr->OnTimeOut(msgId, abilityRecordId, isHalf); + EXPECT_EQ(record->GetAbilityRecordId(), abilityRecordId); +} + +/** + * @tc.name: OnTimeOut_0006 + * @tc.desc: abilityRecord found, isHalf is false, msgId is unknown (default branch) + */ +HWTEST_F(UIAbilityLifecycleManagerTest, OnTimeOut_0006, TestSize.Level1) +{ + auto mgr = std::make_shared(); + AbilityRequest request; + request.abilityInfo.name = "AbilityE"; + auto record = AbilityRecord::CreateAbilityRecord(request); + int64_t abilityRecordId = record->GetAbilityRecordId(); + mgr->sessionAbilityMap_[4] = record; + + uint32_t msgId = 0xDEADBEEF; + bool isHalf = false; + mgr->OnTimeOut(msgId, abilityRecordId, isHalf); + EXPECT_EQ(record->GetAbilityRecordId(), abilityRecordId); +} + + +/** + * @tc.name: CleanUIAbility_0001 + * @tc.desc: Should return ERR_INVALID_VALUE when abilityRecord is nullptr + */ +HWTEST_F(UIAbilityLifecycleManagerTest, CleanUIAbility_0001, TestSize.Level1) +{ + auto mgr = std::make_shared(); + int ret = mgr->CleanUIAbility(nullptr); + EXPECT_EQ(ret, ERR_INVALID_VALUE); +} + +/** + * @tc.name: CleanUIAbility_0002 + * @tc.desc: Should call CloseUIAbility when CleanAbilityByUserRequest returns false + */ +HWTEST_F(UIAbilityLifecycleManagerTest, CleanUIAbility_0002, TestSize.Level1) +{ + auto mgr = std::make_shared(); + AbilityRequest request; + request.abilityInfo.name = "AbilityB"; + auto record = AbilityRecord::CreateAbilityRecord(request); + int ret = mgr->CleanUIAbility(record); + EXPECT_NE(ret, ERR_OK); +} + +/** + * @tc.name: EnableListForSCBRecovery_001 + * @tc.desc: Should set isSCBRecovery_ to true and clear coldStartInSCBRecovery_ + */ +HWTEST_F(UIAbilityLifecycleManagerTest, EnableListForSCBRecovery_001, TestSize.Level1) +{ + auto mgr = std::make_shared(); + mgr->isSCBRecovery_ = false; + mgr->coldStartInSCBRecovery_.insert(1); + mgr->coldStartInSCBRecovery_.insert(2); + mgr->EnableListForSCBRecovery(); + + EXPECT_TRUE(mgr->isSCBRecovery_); + EXPECT_TRUE(mgr->coldStartInSCBRecovery_.empty()); +} } // namespace AAFwk } // namespace OHOS diff --git a/test/unittest/uri_utils_test/BUILD.gn b/test/unittest/uri_utils_test/BUILD.gn index a837748a98e81abadb1271817903f01921b71024..5e114b0bf07f0d74e825a63adbde5460be5b8abf 100644 --- a/test/unittest/uri_utils_test/BUILD.gn +++ b/test/unittest/uri_utils_test/BUILD.gn @@ -30,8 +30,16 @@ ohos_unittest("uri_utils_test") { "-Dprotected=public", ] defines = [ "SUPPORT_UPMS" ] + include_dirs = [ + "${ability_runtime_test_path}/new_test/mock_utils", + "${ability_runtime_test_path}/new_test/mock/ability_common", + "${ability_runtime_test_path}/new_test/mock/security", + "${ability_runtime_test_path}/new_test/mock/uri_permission_manager", + ] sources = [ "${ability_runtime_services_path}/abilitymgr/src/utils/uri_utils.cpp", + "${ability_runtime_test_path}/new_test/mock/security/accesstoken_kit.cpp", + "${ability_runtime_test_path}/new_test/mock/uri_permission_manager/uri_permission_manager_client.cpp", "uri_utils_test.cpp", ] @@ -39,10 +47,8 @@ ohos_unittest("uri_utils_test") { deps = [ "${ability_runtime_innerkits_path}/app_manager:app_manager", - "${ability_runtime_innerkits_path}/uri_permission:uri_permission_mgr", "${ability_runtime_native_path}/appkit:appkit_manager_helper", "${ability_runtime_services_path}/abilitymgr:abilityms", - "${ability_runtime_services_path}/common:app_util", ] external_deps = [ diff --git a/test/unittest/uri_utils_test/uri_utils_test.cpp b/test/unittest/uri_utils_test/uri_utils_test.cpp index 0df8901cc02797e274471bd0b4d7ac0851d77d79..43661a111efa3356d2146f9c8108c80e6f1661d8 100644 --- a/test/unittest/uri_utils_test/uri_utils_test.cpp +++ b/test/unittest/uri_utils_test/uri_utils_test.cpp @@ -15,14 +15,18 @@ #include +#include "accesstoken_kit.h" #include "app_utils.h" #include "array_wrapper.h" +#include "oh_mock_utils.h" #include "string_wrapper.h" +#include "uri_permission_manager_client.h" #include "uri_utils.h" using namespace testing; using namespace testing::ext; +using namespace OHOS::Security::AccessToken; namespace OHOS { namespace AAFwk { @@ -43,7 +47,12 @@ void UriUtilsTest::SetUpTestCase() {} void UriUtilsTest::TearDownTestCase() {} -void UriUtilsTest::SetUp() {} +void UriUtilsTest::SetUp() +{ + OH_MOCK_INIT_ALL_EXPECT_RET(); + AccessTokenKit::InitMockResult(); + UriPermissionManagerClient::InitMockResult(); +} void UriUtilsTest::TearDown() {} @@ -490,6 +499,16 @@ HWTEST_F(UriUtilsTest, IsDmsCall_001, TestSize.Level1) uint32_t fromTokenId = 1001; bool res1 = UriUtils::GetInstance().IsDmsCall(fromTokenId); EXPECT_EQ(res1, false); + + CollaboratorType collaboratorType = CollaboratorType::DEFAULT_TYPE; + AccessTokenKit::getTokenTypeFlagRet = ATokenTypeEnum::TOKEN_NATIVE; + res1 = UriUtils::GetInstance().IsDmsCall(fromTokenId); + EXPECT_EQ(res1, false); + + AccessTokenKit::getNativeTokenInfoRet = 0; + AccessTokenKit::nativeTokenInfo.processName = "distributedsched"; + res1 = UriUtils::GetInstance().IsDmsCall(fromTokenId); + EXPECT_EQ(res1, true); } #ifdef SUPPORT_UPMS @@ -512,9 +531,12 @@ HWTEST_F(UriUtilsTest, GrantDmsUriPermission_001, TestSize.Level1) params.SetParam("ability.verify.uri", ao2); } want.SetParams(params); - UriUtils::GetInstance().GrantDmsUriPermission(want, callerTokenId, targetBundleName, appIndex); - bool res = want.GetParams().HasParam("ability.verify.uri"); - EXPECT_EQ(res, true); + UriPermissionManagerClient::grantUriPermissionPrivilegedRet = -1; + auto ret = UriUtils::GetInstance().GrantDmsUriPermission(want, callerTokenId, targetBundleName, appIndex); + EXPECT_EQ(ret, false); + UriPermissionManagerClient::grantUriPermissionPrivilegedRet = 0; + ret = UriUtils::GetInstance().GrantDmsUriPermission(want, callerTokenId, targetBundleName, appIndex); + EXPECT_EQ(ret, true); } /* @@ -558,7 +580,11 @@ HWTEST_F(UriUtilsTest, CheckUriPermission_001, TestSize.Level1) params.SetParam("ability.params.stream", ao); } want.SetParams(params); - UriUtils::GetInstance().CheckUriPermission(callerTokenId, want); + std::vector expectRet(BEYOND_MAX_URI_COUNT - 1, false); + OH_MOCK_METHOD_EXPECT_RET({expectRet}, UriPermissionManagerClient, CheckUriAuthorization, + const std::vector&, uint32_t, uint32_t); + bool ret = UriUtils::GetInstance().CheckUriPermission(callerTokenId, want); + EXPECT_EQ(ret, false); sptr value = want.GetParams().GetParam("ability.params.stream"); IArray *arr = IArray::Query(value); long arrSize = 0; @@ -570,50 +596,73 @@ HWTEST_F(UriUtilsTest, CheckUriPermission_001, TestSize.Level1) /* * Feature: UriUtils - * Function: GrantUriPermission + * Function: CheckUriPermission * SubFunction: NA - * FunctionPoints: UriUtils GrantUriPermission + * FunctionPoints: UriUtils CheckUriPermission, flag is 0 */ -HWTEST_F(UriUtilsTest, GrantUriPermission_001, TestSize.Level1) +HWTEST_F(UriUtilsTest, CheckUriPermission_002, TestSize.Level1) { + uint32_t callerTokenId = 1001; Want want; - std::string targetBundleName = ""; - int32_t appIndex = 101; - bool isSandboxApp = false; - int32_t callerTokenId = 0; - int32_t collaboratorType = 2; - want.SetFlags(0x00000003); - UriUtils::GetInstance().GrantUriPermission(want, targetBundleName, appIndex, isSandboxApp, callerTokenId, - collaboratorType); - - want.SetFlags(0x00000001); - UriUtils::GetInstance().GrantUriPermission(want, targetBundleName, appIndex, isSandboxApp, callerTokenId, - collaboratorType); - - targetBundleName = "com.example.tsapplication"; - UriUtils::GetInstance().GrantUriPermission(want, targetBundleName, appIndex, isSandboxApp, callerTokenId, - collaboratorType); - - callerTokenId = 1001; - UriUtils::GetInstance().GrantUriPermission(want, targetBundleName, appIndex, isSandboxApp, callerTokenId, - collaboratorType); + std::vector uris(1, "file://com.example.test/1.txt"); + want.SetParam("ability.params.stream", uris); + bool ret = UriUtils::GetInstance().CheckUriPermission(callerTokenId, want); + EXPECT_EQ(ret, false); +} - WantParams params; - sptr ao = new (std::nothrow) AAFwk::Array(1, AAFwk::g_IID_IString); - if (ao != nullptr) { - ao->Set(0, String::Box("file")); - params.SetParam("ability.params.stream", ao); - } - want.SetParams(params); - UriUtils::GetInstance().GrantUriPermission(want, targetBundleName, appIndex, isSandboxApp, callerTokenId, - collaboratorType); +/* + * Feature: UriUtils + * Function: CheckUriPermission + * SubFunction: NA + * FunctionPoints: UriUtils CheckUriPermission, uri is empty + */ +HWTEST_F(UriUtilsTest, CheckUriPermission_003, TestSize.Level1) +{ + uint32_t callerTokenId = 1001; + Want want; + want.SetFlags(1); + bool ret = UriUtils::GetInstance().CheckUriPermission(callerTokenId, want); + EXPECT_EQ(ret, false); +} - want.SetUri("file://data/storage/el2/distributedfiles/test.txt"); - UriUtils::GetInstance().GrantUriPermission(want, targetBundleName, appIndex, isSandboxApp, callerTokenId, - collaboratorType); +/* + * Feature: UriUtils + * Function: CheckUriPermission + * SubFunction: NA + * FunctionPoints: UriUtils CheckUriPermission, CheckUriAuthorizatoion falied + */ +HWTEST_F(UriUtilsTest, CheckUriPermission_004, TestSize.Level1) +{ + uint32_t callerTokenId = 1001; + Want want; + want.SetFlags(1); + std::vector uris(1, "file://com.example.test/1.txt"); + want.SetParam("ability.params.stream", uris); + std::vector expectRet(1, false); + OH_MOCK_METHOD_EXPECT_RET({expectRet}, UriPermissionManagerClient, CheckUriAuthorization, + const std::vector&, uint32_t, uint32_t); + bool ret = UriUtils::GetInstance().CheckUriPermission(callerTokenId, want); + EXPECT_EQ(ret, false); +} - std::string bundleName = AppUtils::GetInstance().GetBrokerDelegateBundleName(); - EXPECT_EQ(bundleName.empty(), true); +/* + * Feature: UriUtils + * Function: CheckUriPermission + * SubFunction: NA + * FunctionPoints: UriUtils CheckUriPermission, CheckUriAuthorizatoion success + */ +HWTEST_F(UriUtilsTest, CheckUriPermission_005, TestSize.Level1) +{ + uint32_t callerTokenId = 1001; + Want want; + want.SetFlags(1); + std::vector uris(1, "file://com.example.test/1.txt"); + want.SetParam("ability.params.stream", uris); + std::vector expectRet(1, true); + OH_MOCK_METHOD_EXPECT_RET({expectRet}, UriPermissionManagerClient, CheckUriAuthorization, + const std::vector&, uint32_t, uint32_t); + bool ret = UriUtils::GetInstance().CheckUriPermission(callerTokenId, want); + EXPECT_EQ(ret, true); } /* @@ -629,6 +678,9 @@ HWTEST_F(UriUtilsTest, GrantUriPermissionInner_001, TestSize.Level1) std::string targetBundleName = "com.example.tsapplication"; int32_t appIndex = 0; Want want; + std::vector expectRet(1, false); + OH_MOCK_METHOD_EXPECT_RET({expectRet}, UriPermissionManagerClient, CheckUriAuthorization, + const std::vector&, uint32_t, uint32_t); bool res = UriUtils::GetInstance().GrantUriPermissionInner(uriVec, callerTokenId, targetBundleName, appIndex, want); EXPECT_EQ(res, false); } @@ -642,13 +694,25 @@ HWTEST_F(UriUtilsTest, GrantUriPermissionInner_001, TestSize.Level1) */ HWTEST_F(UriUtilsTest, IsSandboxApp_001, TestSize.Level1) { - uint32_t tokenId = 0; + uint32_t tokenId = 1001; + // 1. tokenId is invalid bool res = UriUtils::GetInstance().IsSandboxApp(tokenId); EXPECT_EQ(res, false); - - tokenId = 1001; + // 2. tokenId is TOKEN_HAP, but getHapTokenInfo failed + AccessTokenKit::getTokenTypeFlagRet = ATokenTypeEnum::TOKEN_HAP; + AccessTokenKit::getHapTokenInfoRet = -1; res = UriUtils::GetInstance().IsSandboxApp(tokenId); EXPECT_EQ(res, false); + // 3. hapInfo is not sandbox + AccessTokenKit::getHapTokenInfoRet = 0; + AccessTokenKit::hapInfo.instIndex = 0; + res = UriUtils::GetInstance().IsSandboxApp(tokenId); + EXPECT_EQ(res, false); + // 4. hapInfo is sandbox + AccessTokenKit::getHapTokenInfoRet = 0; + AccessTokenKit::hapInfo.instIndex = 10000; + res = UriUtils::GetInstance().IsSandboxApp(tokenId); + EXPECT_EQ(res, true); } /* @@ -763,36 +827,354 @@ HWTEST_F(UriUtilsTest, ProcessUDMFKey_002, TestSize.Level1) /* * Feature: UriUtils - * Function: IsInAncoAppIdentifier + * Function: PublishFileOpenEvent * SubFunction: NA - * FunctionPoints: UriUtils IsInAncoAppIdentifier + * FunctionPoints: want.GetUri empty */ HWTEST_F(UriUtilsTest, IsInAncoAppIdentifier_001, TestSize.Level1) { - auto &uriUtils = UriUtils::GetInstance(); - std::string uri = ""; + std::string bundleName = ""; + std::string brokerName = "com.example.broker"; + OH_MOCK_METHOD_EXPECT_RET({brokerName}, AppUtils, GetAncoAppIdentifiers); + auto ret = UriUtils::GetInstance().IsInAncoAppIdentifier(bundleName); + EXPECT_EQ(ret, false); +} + +/* + * Feature: UriUtils + * Function: PublishFileOpenEvent + * SubFunction: NA + * FunctionPoints: want.GetUri empty + */ +HWTEST_F(UriUtilsTest, PublishFileOpenEvent_001, TestSize.Level1) +{ Want want; - want.SetUri(uri); - uriUtils.PublishFileOpenEvent(want); - auto result = uriUtils.IsInAncoAppIdentifier("com.example.test"); - EXPECT_FALSE(result); + auto ret = UriUtils::GetInstance().PublishFileOpenEvent(want); + EXPECT_EQ(ret, false); } /* * Feature: UriUtils - * Function: IsInAncoAppIdentifier + * Function: PublishFileOpenEvent * SubFunction: NA - * FunctionPoints: UriUtils IsInAncoAppIdentifier + * FunctionPoints: scheme of uri is http */ -HWTEST_F(UriUtilsTest, IsInAncoAppIdentifier_002, TestSize.Level1) +HWTEST_F(UriUtilsTest, PublishFileOpenEvent_002, TestSize.Level1) { - auto &uriUtils = UriUtils::GetInstance(); - std::string uri = "file://com.example.test/test.txt"; Want want; - want.SetUri(uri); - uriUtils.PublishFileOpenEvent(want); - auto result = uriUtils.IsInAncoAppIdentifier("com.example.test"); - EXPECT_FALSE(result); + want.SetUri("http://www.aaa.com/11"); + auto ret = UriUtils::GetInstance().PublishFileOpenEvent(want); + EXPECT_EQ(ret, false); +} + +/* + * Feature: UriUtils + * Function: PublishFileOpenEvent + * SubFunction: NA + * FunctionPoints: scheme of uri is file + */ +HWTEST_F(UriUtilsTest, PublishFileOpenEvent_004, TestSize.Level1) +{ + Want want; + want.SetUri("file://com.example.temp/11.txt"); + auto ret = UriUtils::GetInstance().PublishFileOpenEvent(want); + EXPECT_EQ(ret, true); +} + +/* + * Feature: UriUtils + * Function: GrantUriPermission with uri vector + * SubFunction: NA + * FunctionPoints: uri vector empty + */ +HWTEST_F(UriUtilsTest, GrantUriPermissionWithUris_001, TestSize.Level1) +{ + std::vector uris; + int32_t flag = 1; + std::string targetBundleName = "com.example.test"; + int32_t appIndex = 0; + uint32_t initiatorTokenId= 1001; + bool ret = UriUtils::GetInstance().GrantUriPermission(uris, flag, targetBundleName, appIndex, initiatorTokenId); + EXPECT_EQ(ret, false); +} + +/* + * Feature: UriUtils + * Function: GrantUriPermission with uri vector + * SubFunction: NA + * FunctionPoints: GrantUriPermission failed + */ +HWTEST_F(UriUtilsTest, GrantUriPermissionWithUris_002, TestSize.Level1) +{ + std::vector uris = { "file://com.example.test/1.txt" }; + int32_t flag = 1; + std::string targetBundleName = "com.example.test"; + int32_t appIndex = 0; + uint32_t initiatorTokenId= 1001; + OH_MOCK_METHOD_EXPECT_RET({-1}, UriPermissionManagerClient, GrantUriPermission, const std::vector&, + unsigned int, const std::string, int32_t, uint32_t); + bool ret = UriUtils::GetInstance().GrantUriPermission(uris, flag, targetBundleName, appIndex, initiatorTokenId); + EXPECT_EQ(ret, false); +} + +/* + * Feature: UriUtils + * Function: GrantUriPermission with uri vector + * SubFunction: NA + * FunctionPoints: GrantUriPermission success + */ +HWTEST_F(UriUtilsTest, GrantUriPermissionWithUris_003, TestSize.Level1) +{ + std::vector uris = { "file://com.example.test/1.txt" }; + int32_t flag = 1; + std::string targetBundleName = "com.example.test"; + int32_t appIndex = 0; + uint32_t initiatorTokenId= 1001; + OH_MOCK_METHOD_EXPECT_RET({0}, UriPermissionManagerClient, GrantUriPermission, const std::vector&, + unsigned int, const std::string, int32_t, uint32_t); + bool ret = UriUtils::GetInstance().GrantUriPermission(uris, flag, targetBundleName, appIndex, initiatorTokenId); + EXPECT_EQ(ret, true); +} + +/* + * Feature: UriUtils + * Function: GrantUriPermission with want + * SubFunction: NA + * FunctionPoints: GrantUriPermission, flag is 0. + */ +HWTEST_F(UriUtilsTest, GrantUriPermissionWithWant_001, TestSize.Level1) +{ + Want want; + std::string targetBundleName = "com.example.test"; + int32_t appIndex = 0; + uint32_t callerTokenId = 1001; + bool isSandboxApp = true; + std::string brokerName = "com.example.broker"; + std::vector expectIdentifiers(1, brokerName); + OH_MOCK_METHOD_EXPECT_RET(expectIdentifiers, AppUtils, GetAncoAppIdentifiers); + CollaboratorType collaboratorType = CollaboratorType::DEFAULT_TYPE; + bool ret = UriUtils::GetInstance().GrantUriPermission(want, targetBundleName, appIndex, + isSandboxApp, callerTokenId, collaboratorType); + EXPECT_EQ(ret, false); +} + +/* + * Feature: UriUtils + * Function: GrantUriPermission with want + * SubFunction: NA + * FunctionPoints: GrantUriPermission, collaborator app. + */ +HWTEST_F(UriUtilsTest, GrantUriPermissionWithWant_002, TestSize.Level1) +{ + Want want; + want.SetFlags(1); + std::string brokerDelegateBundleName = "com.example.delegator"; + std::vector expectBrokerDelegateBundleNames(3, brokerDelegateBundleName); + OH_MOCK_METHOD_EXPECT_RET(expectBrokerDelegateBundleNames, AppUtils, GetBrokerDelegateBundleName); + std::string targetBundleName = brokerDelegateBundleName; + int32_t appIndex = 0; + uint32_t callerTokenId = 0; + bool isSandboxApp = true; + std::string brokerName = "com.example.broker"; + std::vector expectIdentifiers(3, brokerName); + OH_MOCK_METHOD_EXPECT_RET(expectIdentifiers, AppUtils, GetAncoAppIdentifiers); + CollaboratorType collaboratorType = CollaboratorType::OTHERS_TYPE; + bool ret = UriUtils::GetInstance().GrantUriPermission(want, targetBundleName, appIndex, + isSandboxApp, callerTokenId, collaboratorType); + EXPECT_EQ(ret, false); + // collaboratorType is others_type + targetBundleName = "com.example.test"; + collaboratorType = CollaboratorType::OTHERS_TYPE; + ret = UriUtils::GetInstance().GrantUriPermission(want, targetBundleName, appIndex, isSandboxApp, + callerTokenId, collaboratorType); + EXPECT_EQ(ret, false); + // collaboratorType is others_type + targetBundleName = brokerDelegateBundleName; + collaboratorType = CollaboratorType::DEFAULT_TYPE; + ret = UriUtils::GetInstance().GrantUriPermission(want, targetBundleName, appIndex, + isSandboxApp, callerTokenId, collaboratorType); + EXPECT_EQ(ret, false); +} + +/* + * Feature: UriUtils + * Function: GrantUriPermission with want + * SubFunction: NA + * FunctionPoints: GrantUriPermission, isSandboxApp test + */ +HWTEST_F(UriUtilsTest, GrantUriPermissionWithWant_003, TestSize.Level1) +{ + Want want; + want.SetFlags(1); + std::string targetBundleName = "com.example.test"; + int32_t appIndex = 0; + uint32_t callerTokenId = 1001; + bool isSandboxApp = true; + std::string brokerDelegateBundleName = "com.example.delegator"; + std::vector expectBrokerDelegateBundleNames(2, brokerDelegateBundleName); + OH_MOCK_METHOD_EXPECT_RET(expectBrokerDelegateBundleNames, AppUtils, GetBrokerDelegateBundleName); + + std::string brokerName = "com.example.broker"; + std::vector expectIdentifiers(2, brokerName); + OH_MOCK_METHOD_EXPECT_RET(expectIdentifiers, AppUtils, GetAncoAppIdentifiers); + CollaboratorType collaboratorType = CollaboratorType::DEFAULT_TYPE; + bool ret = UriUtils::GetInstance().GrantUriPermission(want, targetBundleName, appIndex, + isSandboxApp, callerTokenId, collaboratorType); + EXPECT_EQ(ret, false); + isSandboxApp = false; + // IsSandboxApp is true; + AccessTokenKit::getHapTokenInfoRet = 0; + AccessTokenKit::hapInfo.instIndex = 10000; + ret = UriUtils::GetInstance().GrantUriPermission(want, targetBundleName, appIndex, isSandboxApp, + callerTokenId, collaboratorType); + EXPECT_EQ(ret, false); +} + +/* + * Feature: UriUtils + * Function: GrantUriPermission with want + * SubFunction: NA + * FunctionPoints: GrantUriPermission, dms call + */ +HWTEST_F(UriUtilsTest, GrantUriPermissionWithWant_004, TestSize.Level1) +{ + Want want; + want.SetFlags(1); + std::string targetBundleName = "com.example.test"; + int32_t appIndex = 0; + uint32_t callerTokenId = 1001; + bool isSandboxApp = false; + std::string brokerDelegateBundleName = "com.example.delegator"; + std::vector expectBrokerDelegateBundleNames(1, brokerDelegateBundleName); + OH_MOCK_METHOD_EXPECT_RET(expectBrokerDelegateBundleNames, AppUtils, GetBrokerDelegateBundleName); + + std::string brokerName = "com.example.broker"; + std::vector expectIdentifiers(1, brokerName); + OH_MOCK_METHOD_EXPECT_RET(expectIdentifiers, AppUtils, GetAncoAppIdentifiers); + // dms call + CollaboratorType collaboratorType = CollaboratorType::DEFAULT_TYPE; + AccessTokenKit::getTokenTypeFlagRet = ATokenTypeEnum::TOKEN_NATIVE; + AccessTokenKit::getNativeTokenInfoRet = 0; + AccessTokenKit::nativeTokenInfo.processName = "distributedsched"; + UriPermissionManagerClient::grantUriPermissionPrivilegedRet = 0; + bool ret = UriUtils::GetInstance().GrantUriPermission(want, targetBundleName, appIndex, + isSandboxApp, callerTokenId, collaboratorType); + EXPECT_EQ(ret, true); +} + +/* + * Feature: UriUtils + * Function: GrantUriPermission with want + * SubFunction: NA + * FunctionPoints: GrantUriPermission, not uri in want + */ +HWTEST_F(UriUtilsTest, GrantUriPermissionWithWant_005, TestSize.Level1) +{ + Want want; + want.SetFlags(1); + std::string targetBundleName = "com.example.test"; + int32_t appIndex = 0; + uint32_t callerTokenId = 1001; + bool isSandboxApp = false; + std::string brokerDelegateBundleName = "com.example.delegator"; + std::vector expectBrokerDelegateBundleNames(1, brokerDelegateBundleName); + OH_MOCK_METHOD_EXPECT_RET(expectBrokerDelegateBundleNames, AppUtils, GetBrokerDelegateBundleName); + + std::string brokerName = "com.example.broker"; + std::vector expectIdentifiers(1, brokerName); + OH_MOCK_METHOD_EXPECT_RET(expectIdentifiers, AppUtils, GetAncoAppIdentifiers); + // get uri from want is empty + CollaboratorType collaboratorType = CollaboratorType::DEFAULT_TYPE; + bool ret = UriUtils::GetInstance().GrantUriPermission(want, targetBundleName, appIndex, + isSandboxApp, callerTokenId, collaboratorType); + EXPECT_EQ(ret, false); +} + +/* + * Feature: UriUtils + * Function: GrantUriPermission with want + * SubFunction: NA + * FunctionPoints: GrantUriPermission, broker call + */ +HWTEST_F(UriUtilsTest, GrantUriPermissionWithWant_006, TestSize.Level1) +{ + Want want; + want.SetFlags(1); + want.SetUri("file://com.example.test/1.txt"); + std::string targetBundleName = "com.example.test"; + int32_t appIndex = 0; + uint32_t callerTokenId = 1001; + bool isSandboxApp = false; + CollaboratorType collaboratorType = CollaboratorType::DEFAULT_TYPE; + std::string brokerDelegateBundleName = "com.example.delegator"; + std::vector expectBrokerDelegateBundleNames(2, brokerDelegateBundleName); + OH_MOCK_METHOD_EXPECT_RET(expectBrokerDelegateBundleNames, AppUtils, GetBrokerDelegateBundleName); + + // mock broker call + std::string brokerName = "com.example.broker"; + std::vector expectIdentifiers(2, brokerName); + OH_MOCK_METHOD_EXPECT_RET(expectIdentifiers, AppUtils, GetAncoAppIdentifiers); + want.SetParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME, brokerName); + + // broker caller, GrantShellUriPermission failed + std::vector expectRet(1, false); + OH_MOCK_METHOD_EXPECT_RET({expectRet}, UriPermissionManagerClient, CheckUriAuthorization, + const std::vector&, uint32_t, uint32_t); + UriPermissionManagerClient::grantUriPermissionPrivilegedRet = -1; + bool ret = UriUtils::GetInstance().GrantUriPermission(want, targetBundleName, appIndex, + isSandboxApp, callerTokenId, collaboratorType); + EXPECT_EQ(ret, false); + // broker caller, GrantShellUriPermission success + want.SetUri("content://com.example.test/1.txt"); + UriPermissionManagerClient::grantUriPermissionPrivilegedRet = 0; + ret = UriUtils::GetInstance().GrantUriPermission(want, targetBundleName, appIndex, isSandboxApp, + callerTokenId, collaboratorType); + EXPECT_EQ(ret, true); +} + +/* + * Feature: UriUtils + * Function: GrantUriPermission with want + * SubFunction: NA + * FunctionPoints: GrantUriPermission, GrantUriPermissionInner ret test + */ +HWTEST_F(UriUtilsTest, GrantUriPermissionWithWant_007, TestSize.Level1) +{ + Want want; + want.SetFlags(1); + want.SetUri("file://com.example.test/1.txt"); + std::string targetBundleName = "com.example.test"; + int32_t appIndex = 0; + uint32_t callerTokenId = 1001; + bool isSandboxApp = false; + std::string brokerDelegateBundleName = "com.example.delegator"; + std::vector expectBrokerDelegateBundleNames(2, brokerDelegateBundleName); + OH_MOCK_METHOD_EXPECT_RET(expectBrokerDelegateBundleNames, AppUtils, GetBrokerDelegateBundleName); + // broker uid + std::vector expectBrokerUIDs(2, 1001); + OH_MOCK_METHOD_EXPECT_RET(expectBrokerUIDs, AppUtils, GetCollaboratorBrokerUID); + + std::string brokerName = "com.example.broker"; + std::vector expectIdentifiers(2, brokerName); + OH_MOCK_METHOD_EXPECT_RET(expectIdentifiers, AppUtils, GetAncoAppIdentifiers); + CollaboratorType collaboratorType = CollaboratorType::DEFAULT_TYPE; + std::vector expectRet(1, true); + std::vector> expectRets; + expectRets.emplace_back(expectRet); + expectRets.emplace_back(expectRet); + // GrantUriPermissionInner failed + OH_MOCK_METHOD_EXPECT_RET(expectRets, UriPermissionManagerClient, CheckUriAuthorization, + const std::vector&, uint32_t, uint32_t); + UriPermissionManagerClient::grantUriPermissionPrivilegedRet = -1; + bool ret = UriUtils::GetInstance().GrantUriPermission(want, targetBundleName, appIndex, isSandboxApp, + callerTokenId, collaboratorType); + EXPECT_EQ(ret, false); + + // GrantUriPermissionInner success + UriPermissionManagerClient::grantUriPermissionPrivilegedRet = 0; + ret = UriUtils::GetInstance().GrantUriPermission(want, targetBundleName, appIndex, isSandboxApp, + callerTokenId, collaboratorType); + EXPECT_EQ(ret, true); } } } \ No newline at end of file