From 88bb0d0612210fa20952b0847c4120034a2896a1 Mon Sep 17 00:00:00 2001 From: zhangzezhong Date: Thu, 12 Jun 2025 18:40:30 +0800 Subject: [PATCH] add UriUtils unittest cases for ability_runtime Signed-off-by: zhangzezhong --- test/unittest/BUILD.gn | 1 + test/unittest/uri_utils_second_test/BUILD.gn | 78 ++++++++ .../mock/include/mock_accesstoken_kit.h | 38 ++++ .../mock/include/mock_my_flag.h | 101 +++++++++++ .../mock/src/mock_accesstoken_kit.cpp | 59 +++++++ .../mock/src/mock_my_flag.cpp | 43 +++++ .../uri_utils_second_test.cpp | 167 ++++++++++++++++++ 7 files changed, 487 insertions(+) create mode 100644 test/unittest/uri_utils_second_test/BUILD.gn create mode 100644 test/unittest/uri_utils_second_test/mock/include/mock_accesstoken_kit.h create mode 100644 test/unittest/uri_utils_second_test/mock/include/mock_my_flag.h create mode 100644 test/unittest/uri_utils_second_test/mock/src/mock_accesstoken_kit.cpp create mode 100644 test/unittest/uri_utils_second_test/mock/src/mock_my_flag.cpp create mode 100644 test/unittest/uri_utils_second_test/uri_utils_second_test.cpp diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 0c925708f31..edb0b012983 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -464,6 +464,7 @@ group("unittest") { "ui_extension_utils_test:unittest", "unlock_screen_test:unittest", "update_caller_info_util_test:unittest", + "uri_utils_second_test", "uri_utils_test:unittest", "user_controller_test:unittest", "user_event_handler_test:unittest", diff --git a/test/unittest/uri_utils_second_test/BUILD.gn b/test/unittest/uri_utils_second_test/BUILD.gn new file mode 100644 index 00000000000..8f7883c6247 --- /dev/null +++ b/test/unittest/uri_utils_second_test/BUILD.gn @@ -0,0 +1,78 @@ +# 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. + +import("//build/test.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +module_output_path = "ability_runtime/ability_runtime/abilitymgr" + +ohos_unittest("uri_utils_second_test") { + module_out_path = module_output_path + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + blocklist = "../../cfi_blocklist.txt" + } + branch_protector_ret = "pac_ret" + cflags = [ + "-Dprivate=public", + "-Dprotected=public", + ] + defines = [ "SUPPORT_UPMS" ] + + include_dirs = [ + "${ability_runtime_path}/interfaces/kits/native/appkit/ability_bundle_manager_helper", + "${ability_runtime_test_path}/mock/common/include", + "${ability_runtime_test_path}/mock/services_appmgr_test/include/", + "mock/include", + ] + sources = [ + "${ability_runtime_services_path}/abilitymgr/src/utils/uri_utils.cpp", + "mock/src/mock_accesstoken_kit.cpp", + "mock/src/mock_my_flag.cpp", + "uri_utils_second_test.cpp", + ] + + configs = [ "${ability_runtime_services_path}/abilitymgr:abilityms_config" ] + + 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 = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "ffrt:libffrt", + "hilog:libhilog", + "hisysevent:libhisysevent", + "hitrace:hitrace_meter", + "ipc:ipc_core", + "jsoncpp:jsoncpp", + ] +} + +group("unittest") { + testonly = true + + deps = [ ":uri_utils_second_test" ] +} diff --git a/test/unittest/uri_utils_second_test/mock/include/mock_accesstoken_kit.h b/test/unittest/uri_utils_second_test/mock/include/mock_accesstoken_kit.h new file mode 100644 index 00000000000..060567f4b34 --- /dev/null +++ b/test/unittest/uri_utils_second_test/mock/include/mock_accesstoken_kit.h @@ -0,0 +1,38 @@ +/* + * 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); + + static int GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo &nativeTokenInfoRes); + + static int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo &hapInfo); +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif diff --git a/test/unittest/uri_utils_second_test/mock/include/mock_my_flag.h b/test/unittest/uri_utils_second_test/mock/include/mock_my_flag.h new file mode 100644 index 00000000000..1cb9e857db6 --- /dev/null +++ b/test/unittest/uri_utils_second_test/mock/include/mock_my_flag.h @@ -0,0 +1,101 @@ +/* + * 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 MOCK_MY_FLAG_H +#define MOCK_MY_FLAG_H + +#include +#include +#include + +#include "access_token.h" + +namespace OHOS { +namespace AAFwk { +struct TokenInfo; +using MyATokenTypeEnum = Security::AccessToken::ATokenTypeEnum; +using TokenInfoMap = std::unordered_map; + +struct TokenInfo { + uint32_t tokenId = 0; + std::string processName = ""; + std::string bundleName = ""; + MyATokenTypeEnum tokenType = MyATokenTypeEnum::TOKEN_INVALID; + + TokenInfo() {} + + TokenInfo(uint32_t tokenId, MyATokenTypeEnum tokenType, std::string processName = "", std::string bundleName = "") + { + this->tokenId = tokenId; + this->tokenType = tokenType; + this->processName = processName; + this->bundleName = bundleName; + } +}; + +class MyFlag { +public: + enum FLAG { + IS_SA_CALL = 1, + IS_SHELL_CALL, + IS_SA_AND_SHELL_CALL + }; + + static void Init() + { + flag_ = 0; + permissionFileAccessManager_ = false; + permissionWriteImageVideo_ = false; + permissionReadImageVideo_ = false; + permissionAllMedia_ = false; + permissionWriteAudio_ = false; + permissionReadAudio_ = false; + permissionProxyAuthorization_ = false; + permissionAll_ = false; + permissionPrivileged_ = false; + permissionReadWriteDownload_ = false; + permissionReadWriteDesktop_ = false; + permissionReadWriteDocuments_ = false; + IsSystempAppCall_ = false; + tokenInfos = {}; + } + + static int flag_; + static bool permissionFileAccessManager_; + static bool permissionWriteImageVideo_; + static bool permissionReadImageVideo_; + static bool permissionWriteAudio_; + static bool permissionReadAudio_; + static bool permissionAllMedia_; + static bool permissionProxyAuthorization_; + static bool permissionAll_; + static bool permissionPrivileged_; + static bool permissionReadWriteDownload_; + static bool permissionReadWriteDesktop_; + static bool permissionReadWriteDocuments_; + static bool IsSystempAppCall_; + static bool permissionFileAccessPersist_; + static bool isSAOrSystemAppCall_; + static bool isUriTypeValid_; + static bool isSystemAppCall_; + static bool isDocsCloudUri_; + static std::string bundleName_; + static int32_t getTokenIdByBundleNameStatus_; + + static TokenInfoMap tokenInfos; +}; +} // namespace AAFwk +} // namespace OHOS +#endif // MOCK_MY_FLAG_H \ No newline at end of file diff --git a/test/unittest/uri_utils_second_test/mock/src/mock_accesstoken_kit.cpp b/test/unittest/uri_utils_second_test/mock/src/mock_accesstoken_kit.cpp new file mode 100644 index 00000000000..dd67c598dc5 --- /dev/null +++ b/test/unittest/uri_utils_second_test/mock/src/mock_accesstoken_kit.cpp @@ -0,0 +1,59 @@ +/* + * 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 "mock_accesstoken_kit.h" +#include "mock_my_flag.h" + +namespace { +constexpr int32_t BASE_USER_RANGE = 200000; +} // namespace + +namespace OHOS { +namespace Security { +namespace AccessToken { +using MyFlag = OHOS::AAFwk::MyFlag; + +ATokenTypeEnum AccessTokenKit::GetTokenTypeFlag(AccessTokenID tokenID) +{ + if (MyFlag::flag_ == 0) { + return ATokenTypeEnum::TOKEN_HAP; + } else if (MyFlag::flag_ == 1) { + return ATokenTypeEnum::TOKEN_NATIVE; + } + return ATokenTypeEnum::TOKEN_INVALID; +} + +int AccessTokenKit::GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo &nativeTokenInfoRes) +{ + nativeTokenInfoRes.processName = MyFlag::bundleName_; + if (nativeTokenInfoRes.processName != "") { + return 0; + } + return -1; +} + +int AccessTokenKit::GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo &hapInfo) +{ + hapInfo.instIndex = BASE_USER_RANGE; + hapInfo.bundleName = MyFlag::bundleName_; + hapInfo.apiVersion = MyFlag::getTokenIdByBundleNameStatus_; + if (hapInfo.bundleName != "") { + return 0; + } + return -1; +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/uri_utils_second_test/mock/src/mock_my_flag.cpp b/test/unittest/uri_utils_second_test/mock/src/mock_my_flag.cpp new file mode 100644 index 00000000000..8088f36d783 --- /dev/null +++ b/test/unittest/uri_utils_second_test/mock/src/mock_my_flag.cpp @@ -0,0 +1,43 @@ +/* + * 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 "mock_my_flag.h" + +namespace OHOS { +namespace AAFwk { +int MyFlag::flag_ = 0; +bool MyFlag::permissionFileAccessManager_ = false; +bool MyFlag::permissionWriteImageVideo_ = false; +bool MyFlag::permissionReadImageVideo_ = false; +bool MyFlag::permissionAllMedia_ = false; +bool MyFlag::permissionWriteAudio_ = false; +bool MyFlag::permissionReadAudio_ = false; +bool MyFlag::permissionProxyAuthorization_ = false; +bool MyFlag::permissionAll_ = false; +bool MyFlag::permissionPrivileged_ = false; +bool MyFlag::permissionReadWriteDownload_ = false; +bool MyFlag::permissionReadWriteDesktop_ = false; +bool MyFlag::permissionReadWriteDocuments_ = false; +bool MyFlag::IsSystempAppCall_ = false; +bool MyFlag::permissionFileAccessPersist_ = false; +bool MyFlag::isSAOrSystemAppCall_ = false; +bool MyFlag::isSystemAppCall_ = false; +bool MyFlag::isUriTypeValid_ = false; +bool MyFlag::isDocsCloudUri_ = false; +int32_t MyFlag::getTokenIdByBundleNameStatus_ = 0; +std::string MyFlag::bundleName_ = ""; +TokenInfoMap MyFlag::tokenInfos = {}; +} // namespace AAFwk +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/uri_utils_second_test/uri_utils_second_test.cpp b/test/unittest/uri_utils_second_test/uri_utils_second_test.cpp new file mode 100644 index 00000000000..58e60e09a86 --- /dev/null +++ b/test/unittest/uri_utils_second_test/uri_utils_second_test.cpp @@ -0,0 +1,167 @@ +/* + * 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 + +#include "mock_my_flag.h" +#include "mock_accesstoken_kit.h" + +#include "app_utils.h" +#include "array_wrapper.h" +#include "string_wrapper.h" + +#include "uri_utils.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace AAFwk { +namespace { +const uint32_t TOKEN_ID = 1001; +} +class UriUtilsSecondTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override; + void TearDown() override; +}; + +void UriUtilsSecondTest::SetUpTestCase() {} + +void UriUtilsSecondTest::TearDownTestCase() {} + +void UriUtilsSecondTest::SetUp() {} + +void UriUtilsSecondTest::TearDown() {} + +/* + * Feature: UriUtils + * Function: PublishFileOpenEvent + * SubFunction: NA + * FunctionPoints: UriUtils PublishFileOpenEvent + */ +HWTEST_F(UriUtilsSecondTest, PublishFileOpenEvent_001, TestSize.Level1) +{ + Want want; + WantParams params; + auto wangUri = want.GetUri(); + std::string uriStr = wangUri.ToString(); + std::string schemeStr = wangUri.GetScheme(); + EXPECT_EQ(uriStr, ""); + EXPECT_EQ(schemeStr, ""); + 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); + want.SetUri("file://data/storage/el2/distributedfiles/test.txt"); + UriUtils::GetInstance().PublishFileOpenEvent(want); + wangUri = want.GetUri(); + EXPECT_NE(wangUri.ToString(), ""); +} + +/* + * Feature: UriUtils + * Function: PublishFileOpenEvent + * SubFunction: NA + * FunctionPoints: UriUtils PublishFileOpenEvent + */ +HWTEST_F(UriUtilsSecondTest, PublishFileOpenEvent_002, TestSize.Level1) +{ + Want want; + UriUtils::GetInstance().PublishFileOpenEvent(want); + auto wangUri = want.GetUri(); + EXPECT_EQ(wangUri.ToString(), ""); +} + +/* + * Feature: UriUtils + * Function: IsDmsCall + * SubFunction: NA + * FunctionPoints: UriUtils IsDmsCall + */ +HWTEST_F(UriUtilsSecondTest, IsDmsCall_002, TestSize.Level1) +{ + uint32_t fromTokenId = TOKEN_ID; + MyFlag::flag_ = 1; + MyFlag::bundleName_ = ""; + bool ret = UriUtils::GetInstance().IsDmsCall(fromTokenId); + EXPECT_FALSE(ret); +} + +/* + * Feature: UriUtils + * Function: IsDmsCall + * SubFunction: NA + * FunctionPoints: UriUtils IsDmsCall + */ +HWTEST_F(UriUtilsSecondTest, IsDmsCall_003, TestSize.Level1) +{ + uint32_t fromTokenId = TOKEN_ID; + MyFlag::flag_ = 1; + MyFlag::bundleName_ = "distributedsched"; + bool ret = UriUtils::GetInstance().IsDmsCall(fromTokenId); + EXPECT_TRUE(ret); +} + +/* + * Feature: UriUtils + * Function: IsDmsCall + * SubFunction: NA + * FunctionPoints: UriUtils IsDmsCall + */ +HWTEST_F(UriUtilsSecondTest, IsDmsCall_004, TestSize.Level1) +{ + uint32_t fromTokenId = TOKEN_ID; + MyFlag::flag_ = 1; + MyFlag::bundleName_ = "distributedschedtest"; + bool ret = UriUtils::GetInstance().IsDmsCall(fromTokenId); + EXPECT_FALSE(ret); +} + +/* + * Feature: UriUtils + * Function: IsSandboxApp + * SubFunction: NA + * FunctionPoints: UriUtils IsSandboxApp + */ +HWTEST_F(UriUtilsSecondTest, IsSandboxApp_001, TestSize.Level1) +{ + MyFlag::flag_ = 0; + MyFlag::bundleName_ = ""; + uint32_t tokenId = 0; + bool ret = UriUtils::GetInstance().IsSandboxApp(tokenId); + EXPECT_FALSE(ret); +} + +/* + * Feature: UriUtils + * Function: IsSandboxApp + * SubFunction: NA + * FunctionPoints: UriUtils IsSandboxApp + */ +HWTEST_F(UriUtilsSecondTest, IsSandboxApp_002, TestSize.Level1) +{ + MyFlag::flag_ = 0; + MyFlag::bundleName_ = "bundleName"; + uint32_t tokenId = 0; + bool ret = UriUtils::GetInstance().IsSandboxApp(tokenId); + EXPECT_TRUE(ret); +} +} +} \ No newline at end of file -- Gitee