diff --git a/test/commonunittest/UTTest_dm_device_state_manager_two.cpp b/test/commonunittest/UTTest_dm_device_state_manager_two.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ae3721e648186148e7454777c9563ea3cef719b --- /dev/null +++ b/test/commonunittest/UTTest_dm_device_state_manager_two.cpp @@ -0,0 +1,355 @@ +/* + * 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 "UTTest_dm_device_state_manager_two.h" + +#include + +#include "dm_log.h" +#include "dm_constants.h" +#include "dm_adapter_manager.h" +#include "ipc_notify_device_state_req.h" +#include "ipc_notify_auth_result_req.h" +#include "dm_device_state_manager.h" +#include "ipc_notify_device_found_req.h" +#include "ipc_notify_discover_result_req.h" +#include "hichain_connector.h" +#include "device_manager_service_listener.h" + +using namespace testing; +using namespace testing::ext; +namespace OHOS { +namespace DistributedHardware { +void DmDeviceStateManagerTestTwo::SetUp() +{ +} + +void DmDeviceStateManagerTestTwo::TearDown() +{ +} + +void DmDeviceStateManagerTestTwo::SetUpTestCase() +{ + DmSoftbusConnector::dmSoftbusConnector = softbusConnectorMock_; +} + +void DmDeviceStateManagerTestTwo::TearDownTestCase() +{ + DmSoftbusConnector::dmSoftbusConnector = nullptr; + softbusConnectorMock_ = nullptr; +} +namespace { +std::shared_ptr hiChainConnector_ = std::make_shared(); +std::shared_ptr softbusConnector = std::make_shared(); +std::shared_ptr listener_ = std::make_shared(); +std::shared_ptr hiChainAuthConnector = std::make_shared(); +std::shared_ptr dmDeviceStateManager = + std::make_shared(softbusConnector, listener_, hiChainConnector_, hiChainAuthConnector); + +HWTEST_F(DmDeviceStateManagerTestTwo, RegisterSoftbusStateCallback_001, testing::ext::TestSize.Level0) +{ + auto softbusConnector = dmDeviceStateManager->softbusConnector_; + dmDeviceStateManager->softbusConnector_ = nullptr; + int32_t ret = dmDeviceStateManager->RegisterSoftbusStateCallback(); + EXPECT_EQ(ret, DM_OK); + dmDeviceStateManager->softbusConnector_ = softbusConnector; +} + +HWTEST_F(DmDeviceStateManagerTestTwo, DeleteOfflineDeviceInfo_002, testing::ext::TestSize.Level0) +{ + DmDeviceInfo info = { + .deviceId = "123", + .deviceName = "asda", + .deviceTypeId = 1, + }; + dmDeviceStateManager->remoteDeviceInfos_["123"] = info; + dmDeviceStateManager->stateDeviceInfos_["123"] = info; + dmDeviceStateManager->DeleteOfflineDeviceInfo(info); + for (auto iter: dmDeviceStateManager->remoteDeviceInfos_) { + if (iter.second.deviceId == info.deviceId) { + EXPECT_EQ(dmDeviceStateManager->remoteDeviceInfos_.count(iter.first), 0); + break; + } + } +} + +HWTEST_F(DmDeviceStateManagerTestTwo, DeleteOfflineDeviceInfo_003, testing::ext::TestSize.Level0) +{ + DmDeviceInfo info = { + .deviceId = "123", + .deviceName = "asda", + .deviceTypeId = 1, + }; + dmDeviceStateManager->remoteDeviceInfos_["123"] = info; + dmDeviceStateManager->stateDeviceInfos_["123"] = info; + strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "456"); + dmDeviceStateManager->DeleteOfflineDeviceInfo(info); + for (auto iter: dmDeviceStateManager->remoteDeviceInfos_) { + if (iter.second.deviceId == info.deviceId) { + EXPECT_EQ(dmDeviceStateManager->remoteDeviceInfos_.count(iter.first), 0); + break; + } + } +} + +HWTEST_F(DmDeviceStateManagerTestTwo, OnDeviceOffline_002, testing::ext::TestSize.Level0) +{ + std::string deviceId = "123"; + DmDeviceInfo info = { + .deviceId = "123", + .deviceName = "asda", + .deviceTypeId = 1, + }; + dmDeviceStateManager->stateDeviceInfos_["123"] = info; + dmDeviceStateManager->OnDeviceOffline(deviceId); + EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr); +} + +HWTEST_F(DmDeviceStateManagerTestTwo, HandleDeviceStatusChange_001, testing::ext::TestSize.Level0) +{ + DmDeviceInfo info = { + .deviceId = "123", + .deviceName = "asda", + .deviceTypeId = 1, + }; + auto softbusConnector = dmDeviceStateManager->softbusConnector_; + dmDeviceStateManager->softbusConnector_ = nullptr; + dmDeviceStateManager->HandleDeviceStatusChange(DmDeviceState::DEVICE_STATE_OFFLINE, info); + dmDeviceStateManager->softbusConnector_ = softbusConnector; + EXPECT_CALL(*softbusConnectorMock_, GetUdidByNetworkId(_, _)).WillOnce(Return(ERR_DM_FAILED)); + dmDeviceStateManager->HandleDeviceStatusChange(DmDeviceState::DEVICE_STATE_OFFLINE, info); + EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr); +} + +HWTEST_F(DmDeviceStateManagerTestTwo, ProcessDeviceStateChange_001, testing::ext::TestSize.Level0) +{ + ProcessInfo info; + DmDeviceInfo devInfo; + std::vector processInfoVec; + processInfoVec.push_back(info); + EXPECT_CALL(*softbusConnectorMock_, GetProcessInfo()).WillOnce(Return(processInfoVec)); + dmDeviceStateManager->ProcessDeviceStateChange(DmDeviceState::DEVICE_STATE_OFFLINE, devInfo); + EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr); +} + +HWTEST_F(DmDeviceStateManagerTestTwo, OnDeviceOnline_001, testing::ext::TestSize.Level0) +{ + DmDeviceInfo info = { + .deviceId = "123", + .deviceName = "asda", + .deviceTypeId = 1, + }; + std::vector processInfoVec; + EXPECT_CALL(*softbusConnectorMock_, GetDeviceInfoByDeviceId("123")).WillOnce(Return(info)); + EXPECT_CALL(*softbusConnectorMock_, GetProcessInfo()).WillOnce(Return(processInfoVec)); + dmDeviceStateManager->OnDeviceOnline("123", 0); + EXPECT_CALL(*softbusConnectorMock_, GetDeviceInfoByDeviceId("123")).WillOnce(Return(info)); + EXPECT_CALL(*softbusConnectorMock_, GetProcessInfo()).WillOnce(Return(processInfoVec)); + dmDeviceStateManager->OnDeviceOnline("123", 0); + EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr); +} + +HWTEST_F(DmDeviceStateManagerTestTwo, OnDbReady_001, testing::ext::TestSize.Level0) +{ + std::string pkgName = "123"; + std::string deviceId = "123"; + DmDeviceInfo info; + strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123"); + dmDeviceStateManager->OnDbReady(pkgName, deviceId); + DmDeviceInfo deviceInfo = { + .deviceId = "123", + .deviceName = "asda", + .deviceTypeId = 1, + }; + dmDeviceStateManager->remoteDeviceInfos_["123"] = deviceInfo; + dmDeviceStateManager->OnDbReady(pkgName, deviceId); + EXPECT_NE(dmDeviceStateManager->listener_, nullptr); + auto listener = dmDeviceStateManager->listener_; + dmDeviceStateManager->listener_ = nullptr; + dmDeviceStateManager->OnDbReady(pkgName, deviceId); + dmDeviceStateManager->listener_ = listener; + EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr); +} + +HWTEST_F(DmDeviceStateManagerTestTwo, RegisterOffLineTimer_001, testing::ext::TestSize.Level0) +{ + DmDeviceInfo info = { + .deviceId = "123", + .deviceName = "asda", + .deviceTypeId = 1, + }; + StateTimerInfo stateTimerInfo; + dmDeviceStateManager->stateTimerInfoMap_["123"] = stateTimerInfo; + EXPECT_CALL(*softbusConnectorMock_, GetUdidByNetworkId(_, _)).WillOnce(Return(DM_OK)); + dmDeviceStateManager->RegisterOffLineTimer(info); +} + +HWTEST_F(DmDeviceStateManagerTestTwo, StartOffLineTimer_001, testing::ext::TestSize.Level0) +{ + DmDeviceInfo deviceInfo; + dmDeviceStateManager->timer_ = std::make_shared(); + dmDeviceStateManager->StartOffLineTimer(deviceInfo); + EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr); +} + +HWTEST_F(DmDeviceStateManagerTestTwo, DeleteOffLineTimer_001, testing::ext::TestSize.Level0) +{ + dmDeviceStateManager->timer_ = nullptr; + dmDeviceStateManager->DeleteOffLineTimer("123"); + dmDeviceStateManager->timer_ = std::make_shared(); + dmDeviceStateManager->DeleteOffLineTimer(""); + EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr); +} + +HWTEST_F(DmDeviceStateManagerTestTwo, DeleteOffLineTimer_002, testing::ext::TestSize.Level0) +{ + dmDeviceStateManager->timer_ = std::make_shared(); + DmDeviceInfo info = { + .deviceId = "123", + .deviceName = "asda", + .deviceTypeId = 1, + }; + StateTimerInfo stateTimerInfo; + dmDeviceStateManager->stateTimerInfoMap_["123"] = stateTimerInfo; + dmDeviceStateManager->DeleteOffLineTimer("deviceInfo"); + dmDeviceStateManager->DeleteOffLineTimer("123"); + EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr); +} + +HWTEST_F(DmDeviceStateManagerTestTwo, DeleteOffLineTimer_003, testing::ext::TestSize.Level0) +{ + dmDeviceStateManager->timer_ = std::make_shared(); + DmDeviceInfo info = { + .deviceId = "123", + .deviceName = "asda", + .deviceTypeId = 1, + }; + StateTimerInfo stateTimerInfo; + dmDeviceStateManager->stateTimerInfoMap_["123"] = stateTimerInfo; + dmDeviceStateManager->DeleteOffLineTimer("123"); + dmDeviceStateManager->udidhash2udidMap_["123"] = "test"; + dmDeviceStateManager->DeleteOffLineTimer("123"); + EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr); +} + +HWTEST_F(DmDeviceStateManagerTestTwo, DeleteOffLineTimer_004, testing::ext::TestSize.Level0) +{ + dmDeviceStateManager->timer_ = std::make_shared(); + DmDeviceInfo info = { + .deviceId = "123", + .deviceName = "asda", + .deviceTypeId = 1, + }; + StateTimerInfo stateTimerInfo; + dmDeviceStateManager->stateTimerInfoMap_["123"] = stateTimerInfo; + dmDeviceStateManager->udidhash2udidMap_["123"] = "test"; + dmDeviceStateManager->DeleteOffLineTimer("123"); + EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr); +} + +HWTEST_F(DmDeviceStateManagerTestTwo, DeleteTimeOutGroup_001, testing::ext::TestSize.Level0) +{ + DmDeviceInfo info = { + .deviceId = "123", + .deviceName = "asda", + .deviceTypeId = 1, + }; + StateTimerInfo stateTimerInfo; + dmDeviceStateManager->stateTimerInfoMap_["test"] = stateTimerInfo; + dmDeviceStateManager->DeleteTimeOutGroup("123"); + stateTimerInfo.timerName = "123"; + dmDeviceStateManager->stateTimerInfoMap_["123"] = stateTimerInfo; + auto hiChainConnector = dmDeviceStateManager->hiChainConnector_; + dmDeviceStateManager->hiChainConnector_ = nullptr; + dmDeviceStateManager->DeleteTimeOutGroup("123"); + dmDeviceStateManager->hiChainConnector_ = hiChainConnector; + EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr); +} + +HWTEST_F(DmDeviceStateManagerTestTwo, DeleteTimeOutGroup_002, testing::ext::TestSize.Level0) +{ + DmDeviceInfo info = { + .deviceId = "123", + .deviceName = "asda", + .deviceTypeId = 1, + }; + StateTimerInfo stateTimerInfo; + stateTimerInfo.timerName = "123"; + dmDeviceStateManager->stateTimerInfoMap_["123"] = stateTimerInfo; + dmDeviceStateManager->DeleteTimeOutGroup("123"); + dmDeviceStateManager->udidhash2udidMap_["123"] = "test"; + dmDeviceStateManager->DeleteTimeOutGroup("123"); + EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr); +} + +HWTEST_F(DmDeviceStateManagerTestTwo, RunTask_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr task = std::make_shared(1, ""); + dmDeviceStateManager->RunTask(task); + EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr); +} + +HWTEST_F(DmDeviceStateManagerTestTwo, GetAuthForm_001, testing::ext::TestSize.Level0) +{ + EXPECT_CALL(*softbusConnectorMock_, GetUdidByNetworkId(_, _)).WillOnce(Return(DM_OK)); + dmDeviceStateManager->GetAuthForm("task"); + EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr); +} + +HWTEST_F(DmDeviceStateManagerTestTwo, ChangeDeviceInfo_001, testing::ext::TestSize.Level0) +{ + std::string deviceId = "123"; + DmDeviceInfo info = { + .deviceId = "123", + .deviceName = "asda", + .deviceTypeId = 1, + }; + dmDeviceStateManager->stateDeviceInfos_.clear(); + dmDeviceStateManager->stateDeviceInfos_["123"] = info; + dmDeviceStateManager->ChangeDeviceInfo(info); + strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "456"); + dmDeviceStateManager->ChangeDeviceInfo(info); + EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr); +} + +HWTEST_F(DmDeviceStateManagerTestTwo, CheckIsOnline_001, testing::ext::TestSize.Level0) +{ + DmDeviceInfo info = { + .deviceId = "123", + .deviceName = "asda", + .deviceTypeId = 1, + }; + dmDeviceStateManager->stateDeviceInfos_["123"] = info; + bool ret = dmDeviceStateManager->CheckIsOnline("123"); + EXPECT_EQ(ret, true); +} + +HWTEST_F(DmDeviceStateManagerTestTwo, GetUdidByNetWorkId_001, testing::ext::TestSize.Level0) +{ + DmDeviceInfo info = { + .deviceId = "123", + .deviceName = "asda", + .deviceTypeId = 1, + .networkId = "123" + }; + dmDeviceStateManager->stateDeviceInfos_["123"] = info; + std::string ret = dmDeviceStateManager->GetUdidByNetWorkId("123"); + EXPECT_EQ(ret, "123"); + ret = dmDeviceStateManager->GetUdidByNetWorkId("test"); + EXPECT_EQ(ret, ""); +} +} // namespace +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/commonunittest/UTTest_dm_device_state_manager_two.h b/test/commonunittest/UTTest_dm_device_state_manager_two.h new file mode 100644 index 0000000000000000000000000000000000000000..b6d26fe45ce993dab37489b6cb99db458024bd2b --- /dev/null +++ b/test/commonunittest/UTTest_dm_device_state_manager_two.h @@ -0,0 +1,39 @@ +/* + * 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_UTTEST_DM_DEVICE_STATE_MANAGER_H +#define OHOS_UTTEST_DM_DEVICE_STATE_MANAGER_H + +#include +#include + +#include "dm_adapter_manager.h" +#include "softbus_connector_mock.h" +#include "device_manager_service_listener.h" +#include "softbus_session_callback.h" + +namespace OHOS { +namespace DistributedHardware { +class DmDeviceStateManagerTestTwo : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override; + void TearDown() override; + static inline std::shared_ptr softbusConnectorMock_ = + std::make_shared(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index ed8f9ae3df472b8256e644eb1b1ed73ade0bceed..e044b243d3baea8de764e3040850351c7dc6daeb 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -43,6 +43,7 @@ group("unittest") { ":UTTest_dm_credential_manager", ":UTTest_dm_crypto", ":UTTest_dm_device_state_manager", + ":UTTest_dm_device_state_manager_two", ":UTTest_dm_deviceprofile_connector", ":UTTest_dm_deviceprofile_connector_second", ":UTTest_dm_dfx", @@ -1084,6 +1085,31 @@ ohos_unittest("UTTest_dm_device_state_manager") { ## UnitTest UTTest_dm_device_state_manager }}} +## UnitTest UTTest_dm_device_state_manager_two {{{ +ohos_unittest("UTTest_dm_device_state_manager_two") { + module_out_path = module_out_path + + include_dirs = [ "${devicemanager_path}/test/commonunittest" ] + + sources = [ + "${devicemanager_path}/test/commonunittest/UTTest_dm_device_state_manager_two.cpp", + "mock/softbus_connector_mock.cpp", + ] + + deps = [ ":device_manager_test_common" ] + + external_deps = [ + "device_auth:deviceauth_sdk", + "dsoftbus:softbus_client", + "ffrt:libffrt", + "googletest:gmock", + "hisysevent:libhisysevent", + "hitrace:hitrace_meter", + ] +} + +## UnitTest UTTest_dm_device_state_manager_two }}} + ## UnitTest UTTest_dm_dfx {{{ ohos_unittest("UTTest_dm_dfx") { module_out_path = module_out_path diff --git a/test/unittest/mock/accesstoken_kit_mock.cpp b/test/unittest/mock/accesstoken_kit_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9746497ada00ea72dd5c5108beb27528e1aad931 --- /dev/null +++ b/test/unittest/mock/accesstoken_kit_mock.cpp @@ -0,0 +1,67 @@ +/* + * 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_mock.h" + +using namespace OHOS::DistributedHardware; + +namespace OHOS { +namespace Security { +namespace AccessToken { +ATokenTypeEnum AccessTokenKit::GetTokenTypeFlag(AccessTokenID tokenID) +{ + return AccessTokenKitInterface::GetOrCreateAccessTokenKit()->GetTokenTypeFlag(tokenID); +} + +int AccessTokenKit::GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo &hapTokenInfoRes) +{ + return AccessTokenKitInterface::GetOrCreateAccessTokenKit()->GetHapTokenInfo(tokenID, hapTokenInfoRes); +} + +int AccessTokenKit::GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo &nativeTokenInfoRes) +{ + return AccessTokenKitInterface::GetOrCreateAccessTokenKit()->GetNativeTokenInfo(tokenID, nativeTokenInfoRes); +} + +AccessTokenID AccessTokenKit::GetNativeTokenId(const std::string &GetNativeTokenId) +{ + return AccessTokenKitInterface::GetOrCreateAccessTokenKit()->GetNativeTokenId(GetNativeTokenId); +} + +AccessTokenID AccessTokenKit::GetHapTokenID(int32_t userID, const std::string &bundleName, int32_t instIndex) +{ + return AccessTokenKitInterface::GetOrCreateAccessTokenKit()->GetHapTokenID(userID, bundleName, instIndex); +} +} // namespace AccessToken +} // namespace Security + +namespace DistributedHardware { +std::shared_ptr AccessTokenKitInterface::token_ = nullptr; + +std::shared_ptr AccessTokenKitInterface::GetOrCreateAccessTokenKit() +{ + if (!token_) { + token_ = std::make_shared(); + } + return token_; +} + +void AccessTokenKitInterface::ReleaseAccessTokenKit() +{ + token_.reset(); + token_ = nullptr; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/mock/accesstoken_kit_mock.h b/test/unittest/mock/accesstoken_kit_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..5634a580ee843f20793ceaef19312fc39b9cb19f --- /dev/null +++ b/test/unittest/mock/accesstoken_kit_mock.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License") = 0; + * 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_DM_ACCESS_TOKEN_KIT_MOCK_H +#define OHOS_DM_ACCESS_TOKEN_KIT_MOCK_H + +#include + +#include "accesstoken_kit.h" +#include "tokenid_kit.h" + +using OHOS::Security::AccessToken::ATokenTypeEnum; +using OHOS::Security::AccessToken::AccessTokenID; +using OHOS::Security::AccessToken::HapTokenInfo; +using OHOS::Security::AccessToken::NativeTokenInfo; + +namespace OHOS { +namespace DistributedHardware { +class AccessTokenKitInterface { +public: + AccessTokenKitInterface() = default; + virtual ~AccessTokenKitInterface() = default; + + virtual ATokenTypeEnum GetTokenTypeFlag(AccessTokenID) = 0; + virtual int GetHapTokenInfo(AccessTokenID, HapTokenInfo &) = 0; + virtual int GetNativeTokenInfo(AccessTokenID, NativeTokenInfo &) = 0; + virtual AccessTokenID GetNativeTokenId(const std::string &) = 0; + virtual AccessTokenID GetHapTokenID(int32_t, const std::string &, int32_t) = 0; + + static std::shared_ptr GetOrCreateAccessTokenKit(); + static void ReleaseAccessTokenKit(); +private: + static std::shared_ptr token_; +}; + +class AccessTokenKitMock : public AccessTokenKitInterface { +public: + AccessTokenKitMock() = default; + ~AccessTokenKitMock() override = default; + + MOCK_METHOD(ATokenTypeEnum, GetTokenTypeFlag, (AccessTokenID)); + MOCK_METHOD(int, GetHapTokenInfo, (AccessTokenID, HapTokenInfo &)); + MOCK_METHOD(int, GetNativeTokenInfo, (AccessTokenID, NativeTokenInfo &)); + MOCK_METHOD(AccessTokenID, GetNativeTokenId, (const std::string &)); + MOCK_METHOD(AccessTokenID, GetHapTokenID, (int32_t, const std::string &, int32_t)); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_ACCESS_TOKEN_KIT_MOCK_H diff --git a/test/unittest/mock/bundle_mgr_mock.h b/test/unittest/mock/bundle_mgr_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..005453e1b338a65f447c85161b7d9dbe9f7d6549 --- /dev/null +++ b/test/unittest/mock/bundle_mgr_mock.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License") = 0; + * 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_DM_BUNDLE_MGR_MOCK_H +#define OHOS_DM_BUNDLE_MGR_MOCK_H + +#include + +#include "bundle_mgr_interface.h" +#include "iremote_stub.h" + +using OHOS::AppExecFwk::BundleInfo; + +namespace OHOS { +namespace DistributedHardware { +class BundleMgrMock : public IRemoteStub { +public: + BundleMgrMock() = default; + ~BundleMgrMock() override = default; + + MOCK_METHOD(ErrCode, GetNameForUid, (const int, std::string &)); + MOCK_METHOD(ErrCode, GetBundleInfoV9, (const std::string &, int32_t, BundleInfo &, int32_t)); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_BUNDLE_MGR_MOCK_H diff --git a/test/unittest/mock/datashare_helper_mock.cpp b/test/unittest/mock/datashare_helper_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f3c3a10038879f521ea2113a33c843c2d847d15 --- /dev/null +++ b/test/unittest/mock/datashare_helper_mock.cpp @@ -0,0 +1,45 @@ +/* + * 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 "datashare_helper_mock.h" + +using namespace OHOS::DistributedHardware; + +namespace OHOS { +namespace DistributedHardware { +std::shared_ptr DataShareHelperMock::instance_; + +std::shared_ptr DataShareHelperMock::GetOrCreateInstance() +{ + if (!instance_) { + instance_ = std::make_shared(); + } + return instance_; +} + +void DataShareHelperMock::ReleaseInstance() +{ + instance_.reset(); + instance_ = nullptr; +} +} // namespace DistributedHardware +namespace DataShare { +std::pair> DataShareHelper::Create(const sptr &token, + const std::string &strUri, const std::string &extUri, const int waitTime) +{ + return std::make_pair(E_OK, DataShareHelperMock::GetOrCreateInstance()); +} +} // namespace DataShare +} // namespace OHOS diff --git a/test/unittest/mock/datashare_helper_mock.h b/test/unittest/mock/datashare_helper_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..5d675ea02565e9d14d00f19759acea4ade4b83a1 --- /dev/null +++ b/test/unittest/mock/datashare_helper_mock.h @@ -0,0 +1,89 @@ +/* + * 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_DM_DATASHARE_HELPER_MOCK_H +#define OHOS_DM_DATASHARE_HELPER_MOCK_H + +#include +#include "datashare_helper.h" + +namespace OHOS { +namespace DistributedHardware { +using OHOS::AAFwk::IDataAbilityObserver; +using OHOS::DataShare::BatchUpdateResult; +using OHOS::DataShare::Data; +using OHOS::DataShare::DatashareBusinessError; +using OHOS::DataShare::DataShareHelper; +using OHOS::DataShare::DataSharePredicates; +using OHOS::DataShare::DataShareResultSet; +using OHOS::DataShare::DataShareValuesBucket; +using OHOS::DataShare::OperationStatement; +using OHOS::DataShare::ExecResultSet; +using OHOS::DataShare::OperationResult; +using OHOS::DataShare::PublishedDataChangeNode; +using OHOS::DataShare::RdbChangeNode; +using OHOS::DataShare::Template; +using OHOS::DataShare::TemplateId; +using OHOS::DataShare::UpdateOperations; + +class DataShareHelperMock : public DataShareHelper { +public: + static std::shared_ptr GetOrCreateInstance(); + static void ReleaseInstance(); + + MOCK_METHOD(bool, Release, ()); + MOCK_METHOD(std::vector, GetFileTypes, (Uri &, const std::string &)); + MOCK_METHOD(int, OpenFile, (Uri &, const std::string &)); + MOCK_METHOD(int, OpenRawFile, (Uri &, const std::string &)); + MOCK_METHOD(int, Insert, (Uri &, const DataShareValuesBucket &)); + MOCK_METHOD(int, InsertExt, (Uri &, const DataShareValuesBucket &, std::string &)); + MOCK_METHOD(int, Update, (Uri &, const DataSharePredicates &, const DataShareValuesBucket &)); + MOCK_METHOD(int, BatchUpdate, (const UpdateOperations &, std::vector &)); + MOCK_METHOD(int, Delete, (Uri &, const DataSharePredicates &)); + MOCK_METHOD(std::shared_ptr, Query, + (Uri &, const DataSharePredicates &, std::vector &, DatashareBusinessError *)); + MOCK_METHOD(std::string, GetType, (Uri &)); + MOCK_METHOD(int, BatchInsert, (Uri &, const std::vector &)); + MOCK_METHOD(int, ExecuteBatch, (const std::vector &, ExecResultSet &)); + MOCK_METHOD(int, RegisterObserver, (const Uri &, const sptr &)); + MOCK_METHOD(int, UnregisterObserver, (const Uri &, const sptr &)); + MOCK_METHOD(void, NotifyChange, (const Uri &)); + MOCK_METHOD(Uri, NormalizeUri, (Uri &)); + MOCK_METHOD(Uri, DenormalizeUri, (Uri &)); + MOCK_METHOD(int, AddQueryTemplate, (const std::string &, int64_t, Template &)); + MOCK_METHOD(int, DelQueryTemplate, (const std::string &, int64_t)); + MOCK_METHOD(std::vector, Publish, (const Data &, const std::string &)); + MOCK_METHOD(Data, GetPublishedData, (const std::string &, int &)); + MOCK_METHOD(std::vector, SubscribeRdbData, (const std::vector &, + const TemplateId &, const std::function &)); + MOCK_METHOD(std::vector, UnsubscribeRdbData, (const std::vector &, + const TemplateId &)); + MOCK_METHOD(std::vector, EnableRdbSubs, (const std::vector &, + const TemplateId &)); + MOCK_METHOD(std::vector, DisableRdbSubs, (const std::vector &, + const TemplateId &)); + MOCK_METHOD(std::vector, SubscribePublishedData, (const std::vector &, int64_t, + const std::function &)); + MOCK_METHOD(std::vector, UnsubscribePublishedData, (const std::vector &, int64_t)); + MOCK_METHOD(std::vector, EnablePubSubs, (const std::vector &, int64_t)); + MOCK_METHOD(std::vector, DisablePubSubs, (const std::vector &, int64_t)); +private: + DataShareHelperMock() = default; + ~DataShareHelperMock() override = default; + static std::shared_ptr instance_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_DATASHARE_HELPER_MOCK_H diff --git a/test/unittest/mock/datashare_result_set_mock.cpp b/test/unittest/mock/datashare_result_set_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2508311c33da2c5e38fcdf439f9cbee512345cdb --- /dev/null +++ b/test/unittest/mock/datashare_result_set_mock.cpp @@ -0,0 +1,150 @@ +/* + * 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 "datashare_result_set_mock.h" + +namespace OHOS { +namespace DataShare { +DataShareAbsResultSet::DataShareAbsResultSet() +{} + +DataShareAbsResultSet::~DataShareAbsResultSet() {} + +int DataShareAbsResultSet::GetRowCount(int &count) +{ + return E_OK; +} + +int DataShareAbsResultSet::GetAllColumnNames(std::vector &columnNames) +{ + return E_OK; +} + +int DataShareAbsResultSet::GetBlob(int columnIndex, std::vector &blob) +{ + return E_OK; +} + +int DataShareAbsResultSet::GetString(int columnIndex, std::string &value) +{ + return E_OK; +} + +int DataShareAbsResultSet::GetInt(int columnIndex, int &value) +{ + return E_OK; +} + +int DataShareAbsResultSet::GetLong(int columnIndex, int64_t &value) +{ + return E_OK; +} + +int DataShareAbsResultSet::GetDouble(int columnIndex, double &value) +{ + return E_OK; +} + +int DataShareAbsResultSet::IsColumnNull(int columnIndex, bool &isNull) +{ + return E_OK; +} + +int DataShareAbsResultSet::GoToRow(int position) +{ + return E_OK; +} + +int DataShareAbsResultSet::GetDataType(int columnIndex, DataType &dataType) +{ + return E_OK; +} + +int DataShareAbsResultSet::GetRowIndex(int &position) const +{ + return E_OK; +} + +int DataShareAbsResultSet::GoTo(int offset) +{ + return E_OK; +} + +int DataShareAbsResultSet::GoToFirstRow() +{ + return E_OK; +} + +int DataShareAbsResultSet::GoToLastRow() +{ + return E_OK; +} + +int DataShareAbsResultSet::GoToNextRow() +{ + return E_OK; +} + +int DataShareAbsResultSet::GoToPreviousRow() +{ + return E_OK; +} + +int DataShareAbsResultSet::IsAtFirstRow(bool &result) const +{ + return E_OK; +} + +int DataShareAbsResultSet::IsAtLastRow(bool &result) +{ + return E_OK; +} + +int DataShareAbsResultSet::IsStarted(bool &result) const +{ + return E_OK; +} + +int DataShareAbsResultSet::IsEnded(bool &result) +{ + return E_OK; +} + +int DataShareAbsResultSet::GetColumnCount(int &count) +{ + return E_OK; +} + +int DataShareAbsResultSet::GetColumnIndex(const std::string &columnName, int &columnIndex) +{ + return E_OK; +} + +int DataShareAbsResultSet::GetColumnName(int columnIndex, std::string &columnName) +{ + return E_OK; +} + +bool DataShareAbsResultSet::IsClosed() const +{ + return true; +} + +int DataShareAbsResultSet::Close() +{ + return E_OK; +} +} // namespace DataShare +} // namespace OHOS diff --git a/test/unittest/mock/datashare_result_set_mock.h b/test/unittest/mock/datashare_result_set_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..c3c3663af09ec64cae8b8e0466bcfc05fcd4d78f --- /dev/null +++ b/test/unittest/mock/datashare_result_set_mock.h @@ -0,0 +1,41 @@ +/* + * 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_DM_DATASHARE_RESULT_SET_MOCK_H +#define OHOS_DM_DATASHARE_RESULT_SET_MOCK_H + +#include + +#include "datashare_result_set.h" + +namespace OHOS { +namespace DistributedHardware { +using OHOS::DataShare::DataShareResultSet; +using OHOS::DataShare::ResultSetBridge; + +class DataShareResultSetMock : public DataShareResultSet { +public: + explicit DataShareResultSetMock(std::shared_ptr bridge) : DataShareResultSet(bridge) + {} + ~DataShareResultSetMock() override = default; + + MOCK_METHOD(int, GetRowCount, (int &)); + MOCK_METHOD(int, GoToRow, (int)); + MOCK_METHOD(int, GetString, (int, std::string &)); + MOCK_METHOD(int, Close, ()); +}; +} // namespace namespace +} // namespace OHOS +#endif // OHOS_DM_DATASHARE_RESULT_SET_MOCK_H diff --git a/test/unittest/mock/ipc_skeleton_mock.cpp b/test/unittest/mock/ipc_skeleton_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d925e997c05963fe6881c35b2470380ba8232828 --- /dev/null +++ b/test/unittest/mock/ipc_skeleton_mock.cpp @@ -0,0 +1,53 @@ +/* + * 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 "ipc_skeleton_mock.h" + +using namespace OHOS::DistributedHardware; + +namespace OHOS { +pid_t IPCSkeleton::GetCallingUid() +{ + return IPCSkeletonInterface::GetOrCreateIPCSkeleton()->GetCallingUid(); +} + +uint32_t IPCSkeleton::GetCallingTokenID() +{ + return IPCSkeletonInterface::GetOrCreateIPCSkeleton()->GetCallingTokenID(); +} + +uint64_t IPCSkeleton::GetCallingFullTokenID() +{ + return IPCSkeletonInterface::GetOrCreateIPCSkeleton()->GetCallingFullTokenID(); +} + +namespace DistributedHardware { +std::shared_ptr IPCSkeletonInterface::skeleton_ = nullptr; + +std::shared_ptr IPCSkeletonInterface::GetOrCreateIPCSkeleton() +{ + if (!skeleton_) { + skeleton_ = std::make_shared(); + } + return skeleton_; +} + +void IPCSkeletonInterface::ReleaseIPCSkeleton() +{ + skeleton_.reset(); + skeleton_ = nullptr; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/mock/ipc_skeleton_mock.h b/test/unittest/mock/ipc_skeleton_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..b99c69d08f9656bdb99c569f03f487951e43ecdc --- /dev/null +++ b/test/unittest/mock/ipc_skeleton_mock.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License") = 0; + * 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_DM_IPC_SKELETON_MOCK_H +#define OHOS_DM_IPC_SKELETON_MOCK_H + +#include + +#include "ipc_skeleton.h" + +namespace OHOS { +namespace DistributedHardware { +class IPCSkeletonInterface { +public: + IPCSkeletonInterface() = default; + virtual ~IPCSkeletonInterface() = default; + + virtual pid_t GetCallingUid() = 0; + virtual uint32_t GetCallingTokenID() = 0; + virtual uint64_t GetCallingFullTokenID() = 0; + static std::shared_ptr GetOrCreateIPCSkeleton(); + static void ReleaseIPCSkeleton(); +private: + static std::shared_ptr skeleton_; +}; + +class IPCSkeletonMock : public IPCSkeletonInterface { +public: + IPCSkeletonMock() = default; + ~IPCSkeletonMock() override = default; + + MOCK_METHOD(pid_t, GetCallingUid, ()); + MOCK_METHOD(uint32_t, GetCallingTokenID, ()); + MOCK_METHOD(uint64_t, GetCallingFullTokenID, ()); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_SKELETON_MOCK_H diff --git a/test/unittest/mock/multiple_user_connector_mock.cpp b/test/unittest/mock/multiple_user_connector_mock.cpp index efc9e916338967c61172303b489dea2ae1fe961b..2bdba1e9275d244bb61f402de9017efb5082b240 100644 --- a/test/unittest/mock/multiple_user_connector_mock.cpp +++ b/test/unittest/mock/multiple_user_connector_mock.cpp @@ -48,5 +48,15 @@ std::string MultipleUserConnector::GetOhosAccountId(void) { return DmMultipleUserConnector::dmMultipleUserConnector->GetOhosAccountId(); } + +std::string MultipleUserConnector::GetAccountNickName(int32_t userId) +{ + return DmMultipleUserConnector::dmMultipleUserConnector->GetAccountNickName(userId); +} + +void MultipleUserConnector::GetCallerUserId(int32_t &userId) +{ + return DmMultipleUserConnector::dmMultipleUserConnector->GetCallerUserId(userId); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/multiple_user_connector_mock.h b/test/unittest/mock/multiple_user_connector_mock.h index dfd60d0ead8308c3b4c31925c446d8df3fed3e1b..2123d08e4cfb136e08f245f018885ca7e69a205b 100644 --- a/test/unittest/mock/multiple_user_connector_mock.h +++ b/test/unittest/mock/multiple_user_connector_mock.h @@ -32,6 +32,8 @@ public: virtual int32_t GetBackgroundUserIds(std::vector &userIdVec) = 0; virtual int32_t GetForegroundUserIds(std::vector &userVec) = 0; virtual std::string GetOhosAccountId(void) = 0; + virtual std::string GetAccountNickName(int32_t) = 0; + virtual void GetCallerUserId(int32_t &) = 0; public: static inline std::shared_ptr dmMultipleUserConnector = nullptr; }; @@ -44,6 +46,8 @@ public: MOCK_METHOD(int32_t, GetBackgroundUserIds, (std::vector &)); MOCK_METHOD(int32_t, GetForegroundUserIds, (std::vector &)); MOCK_METHOD(std::string, GetOhosAccountId, ()); + MOCK_METHOD(std::string, GetAccountNickName, (int32_t)); + MOCK_METHOD(void, GetCallerUserId, (int32_t &)); }; } } diff --git a/test/unittest/mock/os_account_manager_mock.cpp b/test/unittest/mock/os_account_manager_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..50f194432304fa33754b12a2771175cf8fe1dc64 --- /dev/null +++ b/test/unittest/mock/os_account_manager_mock.cpp @@ -0,0 +1,45 @@ +/* + * 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 "os_account_manager_mock.h" + +using namespace OHOS::DistributedHardware; + +namespace OHOS { +namespace DistributedHardware { +std::shared_ptr IOsAccountManager::accountManager_ = nullptr; + +std::shared_ptr IOsAccountManager::GetOrCreateOsAccountManager() +{ + if (!accountManager_) { + accountManager_ = std::make_shared(); + } + return accountManager_; +} + +void IOsAccountManager::ReleaseAccountManager() +{ + accountManager_.reset(); + accountManager_ = nullptr; +} +} // namespace DistributedHardware + +namespace AccountSA { +ErrCode OsAccountManager::GetForegroundOsAccountLocalId(int32_t &localId) +{ + return IOsAccountManager::GetOrCreateOsAccountManager()->GetForegroundOsAccountLocalId(localId); +} +} // namespace AccountSA +} // namespace OHOS diff --git a/test/unittest/mock/os_account_manager_mock.h b/test/unittest/mock/os_account_manager_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..bbaedbb8916c88da8bf36a75a955e3ae34c1505a --- /dev/null +++ b/test/unittest/mock/os_account_manager_mock.h @@ -0,0 +1,41 @@ +/* + * 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_DM_OS_ACCOUNT_MANAGER_MOCK_H +#define OHOS_DM_OS_ACCOUNT_MANAGER_MOCK_H + +#include +#include "os_account_manager.h" + +namespace OHOS { +namespace DistributedHardware { +class IOsAccountManager { +public: + IOsAccountManager() = default; + virtual ~IOsAccountManager() = default; + + virtual ErrCode GetForegroundOsAccountLocalId(int32_t &localId) = 0; + static std::shared_ptr GetOrCreateOsAccountManager(); + static void ReleaseAccountManager(); +private: + static std::shared_ptr accountManager_; +}; + +class OsAccountManagerMock : public IOsAccountManager { +public: + MOCK_METHOD(ErrCode, GetForegroundOsAccountLocalId, (int32_t &)); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_OS_ACCOUNT_MANAGER_MOCK_H diff --git a/test/unittest/mock/softbus_bus_center.cpp b/test/unittest/mock/softbus_bus_center.cpp index 6c55bca6ec9268f0c0c203aa453f957fdde5e58e..471ad110ea248e4e6cdc2183fa0174bd705fb162 100644 --- a/test/unittest/mock/softbus_bus_center.cpp +++ b/test/unittest/mock/softbus_bus_center.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -40,3 +40,27 @@ int32_t GetAllNodeDeviceInfo(const char *pkgName, NodeBasicInfo **info, int32_t (void)infoNum; return 0; } + +int32_t PublishLNN(const char *pkgName, const PublishInfo *info, const IPublishCb *cb) +{ + (void)pkgName; + (void)info; + (void)cb; + return 0; +} + +int32_t StopPublishLNN(const char *pkgName, int32_t publishId) +{ + (void)pkgName; + (void)publishId; + return 0; +} + +int CreateSessionServer(const char *pkgName, const char *sessionName, const ISessionListener *listener) +{ + (void)pkgName; + (void)sessionName; + (void)listener; + return 0; +} + diff --git a/test/unittest/mock/softbus_connector_mock.cpp b/test/unittest/mock/softbus_connector_mock.cpp index 70351519e868ff9aaaf7bf236db189a510ddf0c8..171311d9e12928d00987ab556840780d6f347a16 100644 --- a/test/unittest/mock/softbus_connector_mock.cpp +++ b/test/unittest/mock/softbus_connector_mock.cpp @@ -24,10 +24,20 @@ int32_t SoftbusConnector::GetUdidByNetworkId(const char *networkId, std::string { return DmSoftbusConnector::dmSoftbusConnector->GetUdidByNetworkId(networkId, udid); } + bool SoftbusConnector::CheckIsOnline(const std::string &targetDeviceId) { return DmSoftbusConnector::dmSoftbusConnector->CheckIsOnline(targetDeviceId); } +std::vector SoftbusConnector::GetProcessInfo() +{ + return DmSoftbusConnector::dmSoftbusConnector->GetProcessInfo(); +} + +DmDeviceInfo SoftbusConnector::GetDeviceInfoByDeviceId(const std::string &deviceId) +{ + return DmSoftbusConnector::dmSoftbusConnector->GetDeviceInfoByDeviceId(deviceId); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/softbus_connector_mock.h b/test/unittest/mock/softbus_connector_mock.h index 10145e8c52c49506a07f76824e07824f43ae39db..fa25cade0a505546bbd06a048f06b0a5412e04d6 100644 --- a/test/unittest/mock/softbus_connector_mock.h +++ b/test/unittest/mock/softbus_connector_mock.h @@ -28,6 +28,8 @@ public: public: virtual int32_t GetUdidByNetworkId(const char *networkId, std::string &udid) = 0; virtual bool CheckIsOnline(const std::string &targetDeviceId) = 0; + virtual std::vector GetProcessInfo() = 0; + virtual DmDeviceInfo GetDeviceInfoByDeviceId(const std::string &deviceId) = 0; public: static inline std::shared_ptr dmSoftbusConnector = nullptr; }; @@ -36,6 +38,8 @@ class SoftbusConnectorMock : public DmSoftbusConnector { public: MOCK_METHOD(int32_t, GetUdidByNetworkId, (const char *, std::string &)); MOCK_METHOD(bool, CheckIsOnline, (const std::string &)); + MOCK_METHOD(std::vector, GetProcessInfo, ()); + MOCK_METHOD(DmDeviceInfo, GetDeviceInfoByDeviceId, (const std::string &deviceId)); }; } } diff --git a/test/unittest/mock/system_ability_manager_client_mock.cpp b/test/unittest/mock/system_ability_manager_client_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d9b8bce1b64586d73fdc8ce046e89bd1f1c79ef --- /dev/null +++ b/test/unittest/mock/system_ability_manager_client_mock.cpp @@ -0,0 +1,49 @@ +/* +* 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 "system_ability_manager_client_mock.h" + +using namespace OHOS::DistributedHardware; + +namespace OHOS { +SystemAbilityManagerClient& SystemAbilityManagerClient::GetInstance() +{ + static auto instance = std::make_shared(); + return *instance; +} + +sptr SystemAbilityManagerClient::GetSystemAbilityManager() +{ + return ISystemAbilityManagerClient::GetOrCreateSAMgrClient()->GetSystemAbilityManager(); +} + +namespace DistributedHardware { +std::shared_ptr ISystemAbilityManagerClient::client_ = nullptr; + +std::shared_ptr ISystemAbilityManagerClient::GetOrCreateSAMgrClient() +{ + if (!client_) { + client_ = std::make_shared(); + } + return client_; +} + +void ISystemAbilityManagerClient::ReleaseSAMgrClient() +{ + client_.reset(); + client_ = nullptr; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/mock/system_ability_manager_client_mock.h b/test/unittest/mock/system_ability_manager_client_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..dc6e4fcc47a5fd75a8ad6d894b30b7eb90698986 --- /dev/null +++ b/test/unittest/mock/system_ability_manager_client_mock.h @@ -0,0 +1,41 @@ +/* +* 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_DM_SYSTEM_ABILITY_MANAGER_CLIENT_MOCK_H +#define OHOS_DM_SYSTEM_ABILITY_MANAGER_CLIENT_MOCK_H + +#include +#include "iservice_registry.h" + +namespace OHOS { +namespace DistributedHardware { +class ISystemAbilityManagerClient { +public: + ISystemAbilityManagerClient() = default; + virtual ~ISystemAbilityManagerClient() = default; + + virtual sptr GetSystemAbilityManager() = 0; + static std::shared_ptr GetOrCreateSAMgrClient(); + static void ReleaseSAMgrClient(); +private: + static std::shared_ptr client_; +}; + +class SystemAbilityManagerClientMock : public ISystemAbilityManagerClient { +public: + MOCK_METHOD0(GetSystemAbilityManager, sptr()); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_SYSTEM_ABILITY_MANAGER_CLIENT_MOCK_H