From 93ef639fd6c72f478176f384b16d8df4f9193336 Mon Sep 17 00:00:00 2001 From: wangpggg Date: Sun, 25 Aug 2024 16:21:34 +0800 Subject: [PATCH] modify unavailable ut Signed-off-by: wangpeng --- test/unittest/BUILD.gn | 14 ++- test/unittest/abnormal_file_access_test.cpp | 20 +--- test/unittest/alloc_test_token.cpp | 110 ++++++++++++++++++++ test/unittest/alloc_test_token.h | 27 +++++ test/unittest/external_file_access_test.cpp | 23 +--- test/unittest/external_notify_test.cpp | 25 +---- 6 files changed, 158 insertions(+), 61 deletions(-) create mode 100644 test/unittest/alloc_test_token.cpp create mode 100644 test/unittest/alloc_test_token.h diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index f844c853..55a8cdf5 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -77,6 +77,7 @@ ohos_unittest("external_file_access_basic_test") { module_out_path = "user_file_service/tests" sources = [ + "alloc_test_token.cpp", "external_file_access_test.cpp", "external_file_access_test_basic.cpp", "external_file_access_test_info.cpp", @@ -132,6 +133,7 @@ ohos_unittest("external_file_access_management_test") { module_out_path = "user_file_service/tests" sources = [ + "alloc_test_token.cpp", "external_file_access_test.cpp", "external_file_access_test_management.cpp", "external_file_access_test_operations.cpp", @@ -186,7 +188,10 @@ ohos_unittest("external_file_access_notify_test") { } module_out_path = "user_file_service/tests" - sources = [ "external_notify_test.cpp" ] + sources = [ + "alloc_test_token.cpp", + "external_notify_test.cpp", + ] include_dirs = [ "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/context", @@ -223,6 +228,8 @@ ohos_unittest("external_file_access_notify_test") { "ipc:rpc", "samgr:samgr_proxy", ] + + defines = [ "private=public" ] } ohos_unittest("abnormal_file_access_test") { @@ -235,7 +242,10 @@ ohos_unittest("abnormal_file_access_test") { } module_out_path = "user_file_service/tests" - sources = [ "abnormal_file_access_test.cpp" ] + sources = [ + "abnormal_file_access_test.cpp", + "alloc_test_token.cpp", + ] include_dirs = [ "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/context", diff --git a/test/unittest/abnormal_file_access_test.cpp b/test/unittest/abnormal_file_access_test.cpp index c8f89596..595b528c 100755 --- a/test/unittest/abnormal_file_access_test.cpp +++ b/test/unittest/abnormal_file_access_test.cpp @@ -17,6 +17,7 @@ #include #include "accesstoken_kit.h" +#include "alloc_test_token.h" #include "context_impl.h" #include "file_access_framework_errno.h" #include "iservice_registry.h" @@ -39,24 +40,7 @@ shared_ptr g_context = nullptr; static void SetNativeToken(bool isSystemApp) { - uint64_t tokenId; - const char *perms[] = { - "ohos.permission.FILE_ACCESS_MANAGER", - "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED", - "ohos.permission.CONNECT_FILE_ACCESS_EXTENSION" - }; - NativeTokenInfoParams infoInstance = { - .dcapsNum = 0, - .permsNum = 3, - .aclsNum = 0, - .dcaps = nullptr, - .perms = perms, - .acls = nullptr, - .aplStr = "system_core", - }; - - infoInstance.processName = "SetUpTestCase"; - tokenId = GetAccessTokenId(&infoInstance); + uint64_t tokenId = AllocToken::AllocTestToken(); if (isSystemApp) { const uint64_t systemAppMask = (static_cast(1) << 32); tokenId |= systemAppMask; diff --git a/test/unittest/alloc_test_token.cpp b/test/unittest/alloc_test_token.cpp new file mode 100644 index 00000000..ce02cfeb --- /dev/null +++ b/test/unittest/alloc_test_token.cpp @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2024 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 "access_token.h" +#include "accesstoken_kit.h" +#include "alloc_test_token.h" + +namespace OHOS::FileAccessFwk { +using namespace OHOS::Security::AccessToken; + +const int32_t DEFAULT_API_VERSION = 8; +const int TEST_USER_ID = 0; +const std::string TEST_BUNDLE_NAME = "com.example.fileaccess"; + +PermissionDef g_infoManagerTestPermDef1 = { + .permissionName = "ohos.permission.FILE_ACCESS_MANAGER", + .bundleName = TEST_BUNDLE_NAME, + .grantMode = 1, + .availableLevel = APL_SYSTEM_BASIC, + .label = "label0", + .labelId = 1, + .description = "file access manager", + .descriptionId = 1, + .availableType = NORMAL +}; + +PermissionDef g_infoManagerTestPermDef2 = { + .permissionName = "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED", + .bundleName = TEST_BUNDLE_NAME, + .grantMode = 1, + .availableLevel = APL_SYSTEM_BASIC, + .label = "label0", + .labelId = 1, + .description = "connect file access extension", + .descriptionId = 1, + .availableType = NORMAL +}; + +PermissionDef g_infoManagerTestPermDef3 = { + .permissionName = "ohos.permission.CONNECT_FILE_ACCESS_EXTENSION", + .bundleName = TEST_BUNDLE_NAME, + .grantMode = 1, + .availableLevel = APL_SYSTEM_BASIC, + .label = "label0", + .labelId = 1, + .description = "connect file access extension", + .descriptionId = 1, + .availableType = NORMAL +}; + +PermissionStateFull g_infoManagerTestState1 = { + .permissionName = "ohos.permission.FILE_ACCESS_MANAGER", + .isGeneral = true, + .resDeviceID = {"local", "device"}, + .grantStatus = {PermissionState::PERMISSION_GRANTED, PermissionState::PERMISSION_GRANTED}, + .grantFlags = {1, 2} +}; + +PermissionStateFull g_infoManagerTestState2 = { + .permissionName = "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED", + .isGeneral = true, + .resDeviceID = {"local", "device"}, + .grantStatus = {PermissionState::PERMISSION_GRANTED, PermissionState::PERMISSION_GRANTED}, + .grantFlags = {1, 2} +}; + +PermissionStateFull g_infoManagerTestState3 = { + .permissionName = "ohos.permission.CONNECT_FILE_ACCESS_EXTENSION", + .isGeneral = true, + .resDeviceID = {"local", "device"}, + .grantStatus = {PermissionState::PERMISSION_GRANTED, PermissionState::PERMISSION_GRANTED}, + .grantFlags = {1, 2} +}; + +HapInfoParams g_infoManagerTestInfoParms = { + .userID = TEST_USER_ID, + .bundleName = TEST_BUNDLE_NAME, + .instIndex = 0, + .appIDDesc = "test", + .apiVersion = DEFAULT_API_VERSION, + .appDistributionType = "os_integration", + .isSystemApp = true +}; + +HapPolicyParams g_infoManagerTestPolicyPrams = { + .apl = APL_SYSTEM_CORE, + .domain = "domain", + .permList = {g_infoManagerTestPermDef1, g_infoManagerTestPermDef2, g_infoManagerTestPermDef3}, + .permStateList = {g_infoManagerTestState1, g_infoManagerTestState2, g_infoManagerTestState3} +}; + +uint64_t AllocToken::AllocTestToken() +{ + AccessTokenIDEx tokenIdEx = {0}; + tokenIdEx = AccessTokenKit::AllocHapToken(g_infoManagerTestInfoParms, g_infoManagerTestPolicyPrams); + return tokenIdEx.tokenIdExStruct.tokenID; +} +} \ No newline at end of file diff --git a/test/unittest/alloc_test_token.h b/test/unittest/alloc_test_token.h new file mode 100644 index 00000000..f2b15d20 --- /dev/null +++ b/test/unittest/alloc_test_token.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2024 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_EXTERNAL_FILE_ACCESS_ALLOC_TEST_TOKEN_H +#define OHOS_EXTERNAL_FILE_ACCESS_ALLOC_TEST_TOKEN_H + +namespace OHOS { +namespace FileAccessFwk { +class AllocToken { +public: + static uint64_t AllocTestToken(); +}; +} // namespace FileAccessFwk +} // namespace OHOS + +#endif // OHOS_EXTERNAL_FILE_ACCESS_ALLOC_TEST_TOKEN_H diff --git a/test/unittest/external_file_access_test.cpp b/test/unittest/external_file_access_test.cpp index 62208f9e..b6266abd 100644 --- a/test/unittest/external_file_access_test.cpp +++ b/test/unittest/external_file_access_test.cpp @@ -20,8 +20,8 @@ #include #include -#include "external_file_access_test.h" #include "accesstoken_kit.h" +#include "alloc_test_token.h" #include "context_impl.h" #include "file_access_framework_errno.h" #include "file_info_shared_memory.h" @@ -29,30 +29,15 @@ #include "nativetoken_kit.h" #include "token_setproc.h" +#include "external_file_access_test.h" + namespace OHOS::FileAccessFwk { static shared_ptr g_fah = nullptr; static shared_ptr g_context = nullptr; void SetNativeToken() { - uint64_t tokenId; - const char *perms[] = { - "ohos.permission.FILE_ACCESS_MANAGER", - "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED", - "ohos.permission.CONNECT_FILE_ACCESS_EXTENSION" - }; - NativeTokenInfoParams infoInstance = { - .dcapsNum = 0, - .permsNum = 3, - .aclsNum = 0, - .dcaps = nullptr, - .perms = perms, - .acls = nullptr, - .aplStr = "system_core", - }; - - infoInstance.processName = "SetUpTestCase"; - tokenId = GetAccessTokenId(&infoInstance); + uint64_t tokenId = AllocToken::AllocTestToken(); const uint64_t systemAppMask = (static_cast(1) << 32); tokenId |= systemAppMask; SetSelfTokenID(tokenId); diff --git a/test/unittest/external_notify_test.cpp b/test/unittest/external_notify_test.cpp index bad7a6fb..99abb9c7 100644 --- a/test/unittest/external_notify_test.cpp +++ b/test/unittest/external_notify_test.cpp @@ -21,8 +21,10 @@ #include #include "accesstoken_kit.h" +#include "alloc_test_token.h" #include "context_impl.h" #include "file_access_framework_errno.h" +#include "file_access_helper.h" #include "file_access_observer_common.h" #include "iservice_registry.h" #include "iobserver_callback.h" @@ -30,10 +32,6 @@ #include "observer_callback_stub.h" #include "token_setproc.h" -#define private public -#include "file_access_helper.h" -#undef private - namespace { using namespace std; using namespace OHOS; @@ -55,24 +53,7 @@ shared_ptr g_context = nullptr; void SetNativeToken() { - uint64_t tokenId; - const char *perms[] = { - "ohos.permission.FILE_ACCESS_MANAGER", - "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED", - "ohos.permission.CONNECT_FILE_ACCESS_EXTENSION" - }; - NativeTokenInfoParams infoInstance = { - .dcapsNum = 0, - .permsNum = 3, - .aclsNum = 0, - .dcaps = nullptr, - .perms = perms, - .acls = nullptr, - .aplStr = "system_core", - }; - - infoInstance.processName = "SetUpTestCase"; - tokenId = GetAccessTokenId(&infoInstance); + uint64_t tokenId = AllocToken::AllocTestToken(); const uint64_t systemAppMask = (static_cast(1) << 32); tokenId |= systemAppMask; SetSelfTokenID(tokenId); -- Gitee