From 7c3029084a34f80cc6f3693c68bd13f895748e91 Mon Sep 17 00:00:00 2001 From: fangJinliang1 Date: Mon, 5 Sep 2022 11:39:07 +0800 Subject: [PATCH] tdd modify Signed-off-by: fangJinliang1 Change-Id: I68111c9e45054ffede7362bc0286be4b8d7c6f17 Signed-off-by: fangJinliang1 --- frameworks/test/moduletest/BUILD.gn | 30 +- .../ans_innerkits_module_publish_test.cpp | 4 +- .../ans_innerkits_module_setting_test.cpp | 3 +- .../ans_innerkits_module_slot_test.cpp | 5 +- .../mock/include/remote_native_token.h | 30 ++ .../moduletest/mock/mock_message_parcel.cpp | 328 ------------------ .../moduletest/mock/remote_native_token.cpp | 41 +++ 7 files changed, 88 insertions(+), 353 deletions(-) create mode 100644 frameworks/test/moduletest/mock/include/remote_native_token.h delete mode 100644 frameworks/test/moduletest/mock/mock_message_parcel.cpp create mode 100644 frameworks/test/moduletest/mock/remote_native_token.cpp diff --git a/frameworks/test/moduletest/BUILD.gn b/frameworks/test/moduletest/BUILD.gn index d4a7dd480..cbae18102 100644 --- a/frameworks/test/moduletest/BUILD.gn +++ b/frameworks/test/moduletest/BUILD.gn @@ -121,17 +121,10 @@ ohos_moduletest("ans_innerkits_module_publish_test") { sources = [ "ans_innerkits_module_publish_test.cpp", - "mock/blob.cpp", - "mock/distributed_kv_data_manager.cpp", "mock/mock_accesstoken_kit.cpp", - "mock/mock_bundle_manager.cpp", "mock/mock_bundle_manager_helper.cpp", - "mock/mock_bundle_mgr_proxy.cpp", - "mock/mock_change_notification.cpp", - "mock/mock_common_event_data.cpp", - "mock/mock_common_event_manager.cpp", "mock/mock_ipc.cpp", - "mock/mock_single_kv_store.cpp", + "mock/remote_native_token.cpp", ] configs = [ "//commonlibrary/c_utils/base:utils_config" ] @@ -156,6 +149,8 @@ ohos_moduletest("ans_innerkits_module_publish_test") { "ability_base:zuri", "ability_runtime:wantagent_innerkits", "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", @@ -195,18 +190,12 @@ ohos_moduletest("ans_innerkits_module_slot_test") { ] sources = [ - "${services_path}/test/moduletest/mock/mock_access_token_helper.cpp", "ans_innerkits_module_slot_test.cpp", - "mock/blob.cpp", "mock/distributed_kv_data_manager.cpp", - "mock/mock_bundle_manager.cpp", "mock/mock_bundle_manager_helper.cpp", - "mock/mock_bundle_mgr_proxy.cpp", - "mock/mock_change_notification.cpp", - "mock/mock_common_event_data.cpp", - "mock/mock_common_event_manager.cpp", "mock/mock_ipc.cpp", "mock/mock_single_kv_store.cpp", + "mock/remote_native_token.cpp", ] configs = [ "//commonlibrary/c_utils/base:utils_config" ] @@ -229,6 +218,8 @@ ohos_moduletest("ans_innerkits_module_slot_test") { "ability_base:zuri", "ability_runtime:wantagent_innerkits", "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", @@ -269,16 +260,11 @@ ohos_moduletest("ans_innerkits_module_setting_test") { sources = [ "ans_innerkits_module_setting_test.cpp", - "mock/blob.cpp", "mock/distributed_kv_data_manager.cpp", - "mock/mock_bundle_manager.cpp", "mock/mock_bundle_manager_helper.cpp", - "mock/mock_bundle_mgr_proxy.cpp", - "mock/mock_change_notification.cpp", - "mock/mock_common_event_data.cpp", - "mock/mock_common_event_manager.cpp", "mock/mock_ipc.cpp", "mock/mock_single_kv_store.cpp", + "mock/remote_native_token.cpp", ] configs = [ "//commonlibrary/c_utils/base:utils_config" ] @@ -301,6 +287,8 @@ ohos_moduletest("ans_innerkits_module_setting_test") { "ability_base:zuri", "ability_runtime:wantagent_innerkits", "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", diff --git a/frameworks/test/moduletest/ans_innerkits_module_publish_test.cpp b/frameworks/test/moduletest/ans_innerkits_module_publish_test.cpp index 594f9295b..0befae437 100644 --- a/frameworks/test/moduletest/ans_innerkits_module_publish_test.cpp +++ b/frameworks/test/moduletest/ans_innerkits_module_publish_test.cpp @@ -24,7 +24,7 @@ #include "iservice_registry.h" #include "notification_helper.h" #include "notification_json_convert.h" -#include "mock_bundle_manager.h" +#include "remote_native_token.h" #include "system_ability_definition.h" #include "want_agent_info.h" #include "want_agent_helper.h" @@ -435,6 +435,7 @@ public: void AnsInnerKitsModulePublishTest::SetUpTestCase() { + RemoteNativeToken::SetNativeToken(); sptr service = OHOS::Notification::AdvancedNotificationService::GetInstance(); OHOS::ISystemAbilityManager::SAExtraProp saExtraProp; systemAbilityManager->AddSystemAbility(OHOS::ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID, service, saExtraProp); @@ -1490,6 +1491,7 @@ HWTEST_F(AnsInnerKitsModulePublishTest, ANS_Interface_MT_Slot_Enalbe_00100, Func GTEST_LOG_(INFO) << "ANS_Interface_MT_Slot_Enalbe_00100::end:" << enable; EXPECT_EQ(enable, false); EXPECT_EQ(ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_ENABLED, NotificationHelper::PublishNotification(req)); + EXPECT_EQ(0, NotificationHelper::SetEnabledForBundleSlot(bo, NotificationConstant::CONTENT_INFORMATION, true)); } /** diff --git a/frameworks/test/moduletest/ans_innerkits_module_setting_test.cpp b/frameworks/test/moduletest/ans_innerkits_module_setting_test.cpp index a94994a06..01adf3674 100644 --- a/frameworks/test/moduletest/ans_innerkits_module_setting_test.cpp +++ b/frameworks/test/moduletest/ans_innerkits_module_setting_test.cpp @@ -21,7 +21,7 @@ #include "if_system_ability_manager.h" #include "iservice_registry.h" #include "notification_helper.h" -#include "mock_bundle_manager.h" +#include "remote_native_token.h" #include "system_ability_definition.h" using namespace testing::ext; @@ -42,6 +42,7 @@ public: void AnsInnerKitsModuleSettingTest::SetUpTestCase() { + RemoteNativeToken::SetNativeToken(); sptr service = OHOS::Notification::AdvancedNotificationService::GetInstance(); OHOS::ISystemAbilityManager::SAExtraProp saExtraProp; systemAbilityManager->AddSystemAbility(OHOS::ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID, service, saExtraProp); diff --git a/frameworks/test/moduletest/ans_innerkits_module_slot_test.cpp b/frameworks/test/moduletest/ans_innerkits_module_slot_test.cpp index 82e87976d..bdf631aa4 100644 --- a/frameworks/test/moduletest/ans_innerkits_module_slot_test.cpp +++ b/frameworks/test/moduletest/ans_innerkits_module_slot_test.cpp @@ -21,8 +21,8 @@ #include "ans_manager_proxy.h" #include "if_system_ability_manager.h" #include "iservice_registry.h" -#include "mock_bundle_manager.h" #include "notification_helper.h" +#include "remote_native_token.h" #include "system_ability_definition.h" using namespace testing::ext; @@ -41,6 +41,7 @@ public: void AnsInnerKitsModuleSlotTest::SetUpTestCase() { + RemoteNativeToken::SetNativeToken(); sptr service = OHOS::Notification::AdvancedNotificationService::GetInstance(); OHOS::ISystemAbilityManager::SAExtraProp saExtraProp; systemAbilityManager->AddSystemAbility(OHOS::ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID, service, saExtraProp); @@ -412,7 +413,7 @@ HWTEST_F(AnsInnerKitsModuleSlotTest, ANS_Interface_MT_SetEnabledForBundleSlot_00 HWTEST_F(AnsInnerKitsModuleSlotTest, ANS_Interface_MT_SetEnabledForBundleSlot_00200, Function | MediumTest | Level1) { bool enable = true; - NotificationBundleOption bo("hello", 1); + NotificationBundleOption bo("bundleName", 1); EXPECT_EQ(0, NotificationHelper::SetEnabledForBundleSlot(bo, NotificationConstant::SOCIAL_COMMUNICATION, enable)); enable = false; EXPECT_EQ(0, NotificationHelper::GetEnabledForBundleSlot(bo, NotificationConstant::SOCIAL_COMMUNICATION, enable)); diff --git a/frameworks/test/moduletest/mock/include/remote_native_token.h b/frameworks/test/moduletest/mock/include/remote_native_token.h new file mode 100644 index 000000000..85107fd1b --- /dev/null +++ b/frameworks/test/moduletest/mock/include/remote_native_token.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021-2022 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 REMOTE_NATIVE_TOKEN_H +#define REMOTE_NATIVE_TOKEN_H + +#include "nativetoken_kit.h" +#include "token_setproc.h" + +namespace OHOS { +namespace Notification { +class RemoteNativeToken { +public: + static void SetNativeToken(); +}; +} // namespace AppExecFwk +} // namespace OHOS +#endif // REMOTE_NATIVE_TOKEN_H diff --git a/frameworks/test/moduletest/mock/mock_message_parcel.cpp b/frameworks/test/moduletest/mock/mock_message_parcel.cpp deleted file mode 100644 index 49c18fba8..000000000 --- a/frameworks/test/moduletest/mock/mock_message_parcel.cpp +++ /dev/null @@ -1,328 +0,0 @@ -/* - * Copyright (c) 2021 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 -#include "message_parcel.h" - -#include "ipc_debug.h" -#include "iremote_object.h" -#include "ipc_file_descriptor.h" -#include "sys_binder.h" -#include "ashmem.h" -#include "securec.h" -#include "ans_manager_stub.h" - -namespace OHOS { -MessageParcel::MessageParcel() - : Parcel(), - writeRawDataFd_(-1), - readRawDataFd_(-1), - kernelMappedWrite_(nullptr), - kernelMappedRead_(nullptr), - rawData_(nullptr), - rawDataSize_(0) -{} - -MessageParcel::MessageParcel(Allocator *allocator) - : Parcel(allocator), - writeRawDataFd_(-1), - readRawDataFd_(-1), - kernelMappedWrite_(nullptr), - kernelMappedRead_(nullptr), - rawData_(nullptr), - rawDataSize_(0) -{} - -MessageParcel::~MessageParcel() -{ - if (kernelMappedWrite_ != nullptr) { - ::munmap(kernelMappedWrite_, rawDataSize_); - kernelMappedWrite_ = nullptr; - } - if (kernelMappedRead_ != nullptr) { - ::munmap(kernelMappedRead_, rawDataSize_); - kernelMappedRead_ = nullptr; - } - - if (readRawDataFd_ > 0) { - ::close(readRawDataFd_); - readRawDataFd_ = -1; - } - if (writeRawDataFd_ > 0) { - ::close(writeRawDataFd_); - writeRawDataFd_ = -1; - } - - ClearFileDescriptor(); - - rawData_ = nullptr; - rawDataSize_ = 0; -} - -bool MessageParcel::WriteRemoteObject(const sptr &object) -{ - holders_.push_back(object); - return WriteObject(object); -} - -sptr MessageParcel::ReadRemoteObject() -{ - return ReadObject(); -} - -bool MessageParcel::WriteFileDescriptor(int fd) -{ - if (fd < 0) { - return false; - } - int dupFd = dup(fd); - if (dupFd < 0) { - return false; - } - sptr descriptor = new IPCFileDescriptor(dupFd); - return WriteObject(descriptor); -} - -int MessageParcel::ReadFileDescriptor() -{ - sptr descriptor = ReadObject(); - if (descriptor == nullptr) { - return -1; - } - int fd = descriptor->GetFd(); - if (fd < 0) { - return -1; - } - holders_.push_back(descriptor); - return dup(fd); -} - -void MessageParcel::ClearFileDescriptor() -{ - binder_size_t *object = reinterpret_cast(GetObjectOffsets()); - size_t objectNum = GetOffsetsSize(); - uintptr_t data = GetData(); - for (size_t i = 0; i < objectNum; i++) { - const flat_binder_object *flat = reinterpret_cast(data + object[i]); - if (flat->hdr.type == BINDER_TYPE_FD && flat->handle > 0) { - ::close(flat->handle); - } - } -} - -bool MessageParcel::ContainFileDescriptors() const -{ - binder_size_t *object = reinterpret_cast(GetObjectOffsets()); - size_t objectNum = GetOffsetsSize(); - uintptr_t data = GetData(); - for (size_t i = 0; i < objectNum; i++) { - const flat_binder_object *flat = reinterpret_cast(data + object[i]); - if (flat->hdr.type == BINDER_TYPE_FD) { - return true; - } - } - - return false; -} - -bool MessageParcel::WriteInterfaceToken(std::u16string name) -{ - constexpr int strictModePolicy = 0x100; - constexpr int workSource = 0; - size_t rewindPos = GetWritePosition(); - if (!WriteInt32(strictModePolicy)) { - return false; - } - - if (!WriteInt32(workSource)) { - if (!RewindWrite(rewindPos)) { - FlushBuffer(); - } - return false; - } - - return WriteString16(name); -} - -std::u16string MessageParcel::ReadInterfaceToken() -{ - [[maybe_unused]] int strictModePolicy = ReadInt32(); - [[maybe_unused]] int workSource = ReadInt32(); - return ReadString16(); -} - -bool MessageParcel::WriteRawData(const void *data, size_t size) -{ - if (data == nullptr || size > MAX_RAWDATA_SIZE) { - return false; - } - if (kernelMappedWrite_ != nullptr) { - return false; - } - if (!WriteInt32(size)) { - return false; - } - if (size <= MIN_RAWDATA_SIZE) { - return WriteUnpadBuffer(data, size); - } - int fd = AshmemCreate("Parcel RawData", size); - if (fd < 0) { - return false; - } - writeRawDataFd_ = fd; - - int result = AshmemSetProt(fd, PROT_READ | PROT_WRITE); - if (result < 0) { - return false; - } - void *ptr = ::mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if (ptr == MAP_FAILED) { - return false; - } - if (!WriteFileDescriptor(fd)) { - ::munmap(ptr, size); - return false; - } - if (memcpy_s(ptr, size, data, size) != EOK) { - ::munmap(ptr, size); - return false; - } - kernelMappedWrite_ = ptr; - rawDataSize_ = size; - return true; -} - -bool MessageParcel::RestoreRawData(std::shared_ptr rawData, size_t size) -{ - if (rawData_ != nullptr || rawData == nullptr) { - return false; - } - rawData_ = rawData; - rawDataSize_ = size; - writeRawDataFd_ = 0; - return true; -} - -const void *MessageParcel::ReadRawData(size_t size) -{ - int32_t bufferSize = ReadInt32(); - if (static_cast(bufferSize) != size) { - return nullptr; - } - - if (static_cast(bufferSize) <= MIN_RAWDATA_SIZE) { - return ReadUnpadBuffer(size); - } - - /* if rawDataFd_ == 0 means rawData is received from remote - */ - if (rawData_ != nullptr && writeRawDataFd_ == 0) { - /* should read fd for move readCursor of parcel */ - if (ReadFileDescriptor()) { - // do nothing - } - if (rawDataSize_ != size) { - return nullptr; - } - return rawData_.get(); - } - int fd = ReadFileDescriptor(); - if (fd < 0) { - return nullptr; - } - readRawDataFd_ = fd; - - int ashmemSize = AshmemGetSize(fd); - if (ashmemSize < 0 || size_t(ashmemSize) < size) { - // do not close fd here. fd will be closed in FileDescriptor, ::close(fd) - return nullptr; - } - void *ptr = ::mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0); - if (ptr == MAP_FAILED) { - // do not close fd here. fd will be closed in FileDescriptor, ::close(fd) - return nullptr; - } - kernelMappedRead_ = ptr; - rawDataSize_ = size; - return ptr; -} - -const void *MessageParcel::GetRawData() const -{ - if (rawData_ != nullptr) { - return rawData_.get(); - } - if (kernelMappedWrite_ != nullptr) { - return kernelMappedWrite_; - } - if (kernelMappedRead_ != nullptr) { - return kernelMappedRead_; - } - return nullptr; -} - -size_t MessageParcel::GetRawDataSize() const -{ - return rawDataSize_; -} - -size_t MessageParcel::GetRawDataCapacity() const -{ - return MAX_RAWDATA_SIZE; -} - -void MessageParcel::WriteNoException() -{ - WriteInt32(0); -} - -int32_t MessageParcel::ReadException() -{ - int32_t errorCode = ReadInt32(); - if (errorCode != 0) { - ReadString16(); - } - return errorCode; -} - -bool MessageParcel::WriteAshmem(sptr ashmem) -{ - int fd = ashmem->GetAshmemFd(); - int32_t size = ashmem->GetAshmemSize(); - if (fd < 0 || size <= 0) { - return false; - } - if (!WriteFileDescriptor(fd) || !WriteInt32(size)) { - return false; - } - return true; -} - -sptr MessageParcel::ReadAshmem() -{ - int fd = ReadFileDescriptor(); - if (fd < 0) { - return nullptr; - } - - int32_t size = ReadInt32(); - if (size <= 0) { - ::close(fd); - return nullptr; - } - return new Ashmem(fd, size); -} -} // namespace OHOS diff --git a/frameworks/test/moduletest/mock/remote_native_token.cpp b/frameworks/test/moduletest/mock/remote_native_token.cpp new file mode 100644 index 000000000..21854cefe --- /dev/null +++ b/frameworks/test/moduletest/mock/remote_native_token.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2022 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 + +namespace OHOS { +namespace Notification { +void RemoteNativeToken::SetNativeToken() +{ + uint64_t tokenId; + const char **perms = new const char *[1]; + perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC"; // system_core + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 1, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .aplStr = "system_basic", + }; + + infoInstance.processName = "SetUpTestCase"; + tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + delete[] perms; +} +} // namespace AppExecFwk +} // namespace OHOS -- Gitee