diff --git a/test/commonunittest/UTTest_dm_auth_manager_first.cpp b/test/commonunittest/UTTest_dm_auth_manager_first.cpp index 450babef2cfdd2ee137d67057c71a0b07fd74f5f..a6e4b5da884475cb95db18d67293d7660c3fdad3 100644 --- a/test/commonunittest/UTTest_dm_auth_manager_first.cpp +++ b/test/commonunittest/UTTest_dm_auth_manager_first.cpp @@ -1925,8 +1925,13 @@ HWTEST_F(DmAuthManagerTest, StopAuthenticateDevice_002, testing::ext::TestSize.L int32_t sessionId = 1; authManager_->remoteUdidHash_ = "remoteUdidhash"; + std::string udidHashTemp = "remoteUdidhash"; EXPECT_CALL(*softbusSessionMock_, GetPeerDeviceId(_, _)).WillOnce(Return(DM_OK)); - EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).Times(::testing::AtLeast(1)).WillOnce(Return(DM_OK)); + EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).Times(::testing::AtLeast(1)) + .WillOnce(WithArgs<1>(Invoke([udidHashTemp](unsigned char *udidHash) { + memcpy_s(udidHash, (udidHashTemp.length() + 1), udidHashTemp.c_str(), (udidHashTemp.length())); + return DM_OK; + }))); authManager_->DeleteOffLineTimer(sessionId); authManager_->authMessageProcessor_ = std::make_shared(authManager_); diff --git a/test/commonunittest/UTTest_dm_deviceprofile_connector_second.cpp b/test/commonunittest/UTTest_dm_deviceprofile_connector_second.cpp index 09a88d4ff212fdbabb7df4b4a3bf1f5ee4cadd29..e12cbc5f6803b37169231728b313d98c3bed6be5 100644 --- a/test/commonunittest/UTTest_dm_deviceprofile_connector_second.cpp +++ b/test/commonunittest/UTTest_dm_deviceprofile_connector_second.cpp @@ -19,11 +19,13 @@ #include "deviceprofile_connector.h" #include #include "dp_inited_callback_stub.h" +#include "dm_error_type.h" using namespace testing; using namespace testing::ext; namespace OHOS { namespace DistributedHardware { + void DeviceProfileConnectorSecondTest::SetUp() { } @@ -500,5 +502,29 @@ HWTEST_F(DeviceProfileConnectorSecondTest, DeleteAccessControlList_201, testing: remoteDeviceId, bindLevel, extra); EXPECT_EQ(offlineParam.bindType, INVALIED_TYPE); } + +HWTEST_F(DeviceProfileConnectorSecondTest, PutSessionKey_201, testing::ext::TestSize.Level0) +{ + std::vector sessionKeyArray; + int32_t sessionKeyId = 1; + int32_t ret = DeviceProfileConnector::GetInstance().PutSessionKey(sessionKeyArray, sessionKeyId); + EXPECT_EQ(ret, ERR_DM_FAILED); + + sessionKeyArray.push_back('1'); + sessionKeyArray.push_back('2'); + sessionKeyArray.push_back('3'); + sessionKeyArray.push_back('4'); + sessionKeyArray.push_back('5'); + ret = DeviceProfileConnector::GetInstance().PutSessionKey(sessionKeyArray, sessionKeyId); + EXPECT_EQ(ret, ERR_DM_FAILED); + + EXPECT_CALL(*distributedDeviceProfileClientMock_, PutSessionKey(_, _, _)).WillOnce(Return(ERR_DM_FAILED)); + ret = DeviceProfileConnector::GetInstance().PutSessionKey(sessionKeyArray, sessionKeyId); + EXPECT_EQ(ret, ERR_DM_FAILED); + + EXPECT_CALL(*distributedDeviceProfileClientMock_, PutSessionKey(_, _, _)).WillOnce(Return(DM_OK)); + ret = DeviceProfileConnector::GetInstance().PutSessionKey(sessionKeyArray, sessionKeyId); + EXPECT_EQ(ret, DM_OK); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/test/softbusunittest/UTTest_softbus_connector.cpp b/test/softbusunittest/UTTest_softbus_connector.cpp index dada780955a0f8daec9d9aa9cabeeaccd6d9caea..1b5f8f796cd2a43171b3dea44b017dd164566426 100644 --- a/test/softbusunittest/UTTest_softbus_connector.cpp +++ b/test/softbusunittest/UTTest_softbus_connector.cpp @@ -33,6 +33,8 @@ #include "system_ability_definition.h" #include "softbus_error_code.h" +using namespace testing; +using namespace testing::ext; namespace OHOS { namespace DistributedHardware { @@ -53,9 +55,15 @@ void SoftbusConnectorTest::TearDown() } void SoftbusConnectorTest::SetUpTestCase() { + SoftbusCenterInterface::softbusCenterInterface_ = softbusCenterMock_; + DmCrypto::dmCrypto = cryptoMock_; } void SoftbusConnectorTest::TearDownTestCase() { + SoftbusCenterInterface::softbusCenterInterface_ = nullptr; + softbusCenterMock_ = nullptr; + DmCrypto::dmCrypto = nullptr; + cryptoMock_ = nullptr; } namespace { @@ -103,8 +111,9 @@ HWTEST_F(SoftbusConnectorTest, GetUdidByNetworkId_001, testing::ext::TestSize.Le const char *networkId = "123456"; std::string udid; std::shared_ptr softbusConnector = std::make_shared(); + EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _)).WillOnce(Return(ERR_DM_FAILED)); int ret = softbusConnector->GetUdidByNetworkId(networkId, udid); - EXPECT_NE(ret, 0); + EXPECT_EQ(ret, ERR_DM_FAILED); } /** @@ -118,8 +127,9 @@ HWTEST_F(SoftbusConnectorTest, GetUuidByNetworkId_001, testing::ext::TestSize.Le const char *networkId = "123456"; std::string uuid; std::shared_ptr softbusConnector = std::make_shared(); + EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _)).WillOnce(Return(ERR_DM_FAILED)); int ret = softbusConnector->GetUuidByNetworkId(networkId, uuid); - EXPECT_NE(ret, 0); + EXPECT_EQ(ret, ERR_DM_FAILED); } /** @@ -484,8 +494,9 @@ HWTEST_F(SoftbusConnectorTest, GetDeviceUdidHashByUdid_001, testing::ext::TestSi { std::string udid = "123456789"; std::shared_ptr softbusConnector = std::make_shared(); + EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).WillOnce(Return(ERR_DM_FAILED)); std::string ret = softbusConnector->GetDeviceUdidHashByUdid(udid); - EXPECT_EQ(ret.empty(), false); + EXPECT_EQ(ret.empty(), true); } /** @@ -497,7 +508,7 @@ HWTEST_F(SoftbusConnectorTest, EraseUdidFromMap_001, testing::ext::TestSize.Leve std::string udid = "123456789"; std::shared_ptr softbusConnector = std::make_shared(); softbusConnector->EraseUdidFromMap(udid); - EXPECT_EQ(softbusConnector->deviceUdidMap_.empty(), false); + EXPECT_EQ(softbusConnector->deviceUdidMap_.empty(), true); } /** @@ -507,8 +518,13 @@ HWTEST_F(SoftbusConnectorTest, EraseUdidFromMap_001, testing::ext::TestSize.Leve HWTEST_F(SoftbusConnectorTest, GetLocalDeviceName_001, testing::ext::TestSize.Level0) { std::shared_ptr softbusConnector = std::make_shared(); + EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).WillOnce(Return(ERR_DM_FAILED)); std::string ret = softbusConnector->GetLocalDeviceName(); EXPECT_EQ(ret.empty(), true); + + EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).WillOnce(Return(DM_OK)); + ret = softbusConnector->GetLocalDeviceName(); + EXPECT_EQ(ret.empty(), true); } /** @@ -518,9 +534,33 @@ HWTEST_F(SoftbusConnectorTest, GetLocalDeviceName_001, testing::ext::TestSize.Le HWTEST_F(SoftbusConnectorTest, GetNetworkIdByDeviceId_001, testing::ext::TestSize.Level0) { std::string deviceId = "deviceId"; - std::shared_ptr softbusConnector = std::make_shared(); + std::string strNetworkId = "net******12"; + int32_t deviceCount = 1; + NodeBasicInfo nodeBasicInfo = { + .networkId = "network*1", + .deviceName = "deviceName", + .deviceTypeId = 1 + }; + + NodeBasicInfo *basicInfo = &nodeBasicInfo; + std::shared_ptr softbusConnector = std::make_shared(); + EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)) + .WillOnce(WithArgs<1, 2>(Invoke([&basicInfo, &deviceCount](NodeBasicInfo **info, int32_t *infoNum) { + infoNum = &deviceCount; + info = &basicInfo; + return DM_OK; + }))); + EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _)) + .WillOnce(WithArgs<3>(Invoke([deviceId](uint8_t *info) { + memcpy_s(info, (deviceId.length() + 1), deviceId.c_str(), deviceId.length()); + return DM_OK; + }))); std::string ret = softbusConnector->GetNetworkIdByDeviceId(deviceId); EXPECT_EQ(ret.empty(), true); + + EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).WillOnce(Return(DM_OK)); + ret = softbusConnector->GetNetworkIdByDeviceId(deviceId); + EXPECT_EQ(ret.empty(), true); } /** @@ -591,10 +631,37 @@ HWTEST_F(SoftbusConnectorTest, HandleDeviceOffline_001, testing::ext::TestSize.L */ HWTEST_F(SoftbusConnectorTest, CheckIsOnline_001, testing::ext::TestSize.Level0) { - std::string targetDeviceId = "targetDeviceId"; + std::string targetId = "targetDeviceId"; + int32_t deviceCount = 1; std::shared_ptr softbusConnector = std::make_shared(); - softbusConnector->CheckIsOnline(targetDeviceId); - EXPECT_EQ(softbusConnector->processInfoVec_.empty(), true); + EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).WillOnce(Return(ERR_DM_FAILED)); + bool ret = softbusConnector->CheckIsOnline(targetId); + EXPECT_FALSE(ret); + + NodeBasicInfo nodeBasicInfo = { + .networkId = "network*1", + .deviceName = "deviceName", + .deviceTypeId = 1 + }; + + NodeBasicInfo *basicInfo = &nodeBasicInfo; + EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)) + .WillOnce(WithArgs<1, 2>(Invoke([&basicInfo, &deviceCount](NodeBasicInfo **info, int32_t *infoNum) { + infoNum = &deviceCount; + info = &basicInfo; + return DM_OK; + }))); + EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _)) + .WillOnce(WithArgs<3>(Invoke([targetId](uint8_t *info) { + memcpy_s(info, (targetId.length() + 1), targetId.c_str(), targetId.length()); + return DM_OK; + }))); + ret = softbusConnector->CheckIsOnline(targetId); + EXPECT_FALSE(ret); + + EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).WillOnce(Return(DM_OK)); + ret = softbusConnector->CheckIsOnline(targetId); + EXPECT_FALSE(ret); } /** @@ -604,9 +671,49 @@ HWTEST_F(SoftbusConnectorTest, CheckIsOnline_001, testing::ext::TestSize.Level0) HWTEST_F(SoftbusConnectorTest, GetDeviceInfoByDeviceId_001, testing::ext::TestSize.Level0) { std::string deviceId = "deviceId"; + int32_t deviceCount = 1; + std::string strNetworkId = "networkId**1"; std::shared_ptr softbusConnector = std::make_shared(); + EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)).WillOnce(Return(ERR_DM_FAILED)); auto ret = softbusConnector->GetDeviceInfoByDeviceId(deviceId); EXPECT_EQ(ret.deviceId == deviceId, false); + + EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)) + .WillOnce(WithArgs<2>(Invoke([&deviceCount](int32_t *infoNum) { + infoNum = &deviceCount; + return DM_OK; + }))); + EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).WillOnce(Return(ERR_DM_FAILED)); + EXPECT_EQ(ret.deviceId == deviceId, false); + + EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)) + .WillOnce(WithArgs<2>(Invoke([&deviceCount](int32_t *infoNum) { + infoNum = &deviceCount; + return DM_OK; + }))); + EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).WillOnce(Return(DM_OK)); + EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _)) + .WillOnce(WithArgs<3>(Invoke([](uint8_t *info) { + info[0] = 'd'; + info[1] = 'e'; + info[2] = 'v'; + info[3] = 'i'; + return DM_OK; + }))); + EXPECT_EQ(ret.deviceId == deviceId, false); + + EXPECT_CALL(*softbusCenterMock_, GetAllNodeDeviceInfo(_, _, _)) + .WillOnce(WithArgs<2>(Invoke([&deviceCount](int32_t *infoNum) { + infoNum = &deviceCount; + return DM_OK; + }))); + EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).WillOnce(Return(DM_OK)); + EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _)) + .WillOnce(WithArgs<3>(Invoke([deviceId, strNetworkId](uint8_t *info) { + memcpy_s(info, (strNetworkId.length() + 1), strNetworkId.c_str(), (strNetworkId.length())); + return DM_OK; + }))); + EXPECT_EQ(ret.deviceId == deviceId, false); } /** @@ -620,10 +727,82 @@ HWTEST_F(SoftbusConnectorTest, ConvertNodeBasicInfoToDmDevice_001, testing::ext: .deviceName = "name", }; DmDeviceInfo dmDeviceInfo; + nlohmann::json extraJson; + extraJson[PARAM_KEY_OS_TYPE] = 1; + extraJson[PARAM_KEY_OS_VERSION] = {0}; + dmDeviceInfo.extraData = extraJson.dump(); std::shared_ptr softbusConnector = std::make_shared(); softbusConnector->ConvertNodeBasicInfoToDmDevice(nodeBasicInfo, dmDeviceInfo); EXPECT_EQ(softbusConnector->processInfoVec_.empty(), true); } + +HWTEST_F(SoftbusConnectorTest, GetLocalDeviceTypeId_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + EXPECT_CALL(*softbusCenterMock_, GetLocalNodeDeviceInfo(_, _)).WillOnce(Return(ERR_DM_FAILED)); + int32_t ret = softbusConnector->GetLocalDeviceTypeId(); + EXPECT_EQ(ret, static_cast(DmDeviceType::DEVICE_TYPE_UNKNOWN)); + + EXPECT_CALL(*softbusCenterMock_, GetLocalNodeDeviceInfo(_, _)) + .WillOnce(WithArgs<1>(Invoke([](NodeBasicInfo *info) { + if (info != nullptr) { + info->deviceTypeId = 1; + } + return DM_OK; + }))); + ret = softbusConnector->GetLocalDeviceTypeId(); + EXPECT_EQ(ret, 1); +} + +HWTEST_F(SoftbusConnectorTest, GetLocalDeviceNetworkId_001, testing::ext::TestSize.Level0) +{ + std::string networkId = "network*1"; + std::shared_ptr softbusConnector = std::make_shared(); + EXPECT_CALL(*softbusCenterMock_, GetLocalNodeDeviceInfo(_, _)).WillOnce(Return(ERR_DM_FAILED)); + auto ret = softbusConnector->GetLocalDeviceNetworkId(); + EXPECT_EQ(ret.empty(), true); + + EXPECT_CALL(*softbusCenterMock_, GetLocalNodeDeviceInfo(_, _)) + .WillOnce(WithArgs<1>(Invoke([networkId](NodeBasicInfo *info) { + if (info != nullptr) { + memcpy_s(info->networkId, sizeof(info->networkId), networkId.c_str(), networkId.length()); + } + return DM_OK; + }))); + ret = softbusConnector->GetLocalDeviceNetworkId(); + EXPECT_EQ(ret.empty(), false); +} + +HWTEST_F(SoftbusConnectorTest, GetDeviceUdidHashByUdid_002, testing::ext::TestSize.Level0) +{ + std::string udid = "1********69"; + std::string udidHashTemp = "ajj*********47"; + std::shared_ptr softbusConnector = std::make_shared(); + EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).WillOnce(Return(ERR_DM_FAILED)); + std::string ret = softbusConnector->GetDeviceUdidHashByUdid(udid); + EXPECT_EQ(ret.empty(), true); + + EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).WillOnce(WithArgs<1>(Invoke([udidHashTemp](unsigned char *udidHash) { + memcpy_s(udidHash, (udidHashTemp.length() + 1), udidHashTemp.c_str(), (udidHashTemp.length())); + return DM_OK; + }))); + ret = softbusConnector->GetDeviceUdidHashByUdid(udid); + EXPECT_EQ(ret.empty(), false); + + ret = softbusConnector->GetDeviceUdidHashByUdid(udid); + EXPECT_EQ(ret.empty(), false); + + int32_t sessionId = 1; + int32_t sessionKeyId = 1; + int32_t remoteSessionKeyId = 1; + softbusConnector->JoinLnnByHml(sessionId, sessionKeyId, remoteSessionKeyId); + + sessionId = 0; + softbusConnector->JoinLnnByHml(sessionId, sessionKeyId, remoteSessionKeyId); + std::string deviceId = "deviceId"; + bool isForceJoin = false; + softbusConnector->JoinLnn(deviceId, isForceJoin); +} } // namespace } // namespace DistributedHardware } // namespace OHOS- \ No newline at end of file diff --git a/test/softbusunittest/UTTest_softbus_connector.h b/test/softbusunittest/UTTest_softbus_connector.h index a74b6d338e31673732d6c09176ae78fd1f266e62..28debcede26aacfc6d3f4c665352dc132b7fbdad 100644 --- a/test/softbusunittest/UTTest_softbus_connector.h +++ b/test/softbusunittest/UTTest_softbus_connector.h @@ -31,6 +31,8 @@ #include "softbus_connector.h" #include "softbus_listener.h" #include "softbus_session.h" +#include "softbus_center_mock.h" +#include "dm_crypto_mock.h" namespace OHOS { namespace DistributedHardware { @@ -41,6 +43,8 @@ public: void SetUp() override; void TearDown() override; bool CheckReturnResult(int ret); + static inline std::shared_ptr softbusCenterMock_ = std::make_shared(); + static inline std::shared_ptr cryptoMock_ = std::make_shared(); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/test/softbusunittest/UTTest_softbus_listener.cpp b/test/softbusunittest/UTTest_softbus_listener.cpp index e422812819a6d7067acaa4c2e2639d338cb6d939..6b71169219517c8c0e5030218ff1811e36ca8927 100644 --- a/test/softbusunittest/UTTest_softbus_listener.cpp +++ b/test/softbusunittest/UTTest_softbus_listener.cpp @@ -47,6 +47,7 @@ void SoftbusListenerTest::SetUpTestCase() DmCrypto::dmCrypto = cryptoMock_; DmSoftbusCache::dmSoftbusCache = softbusCacheMock_; DMIPCSkeleton::dmIpcSkeleton_ = ipcSkeletonMock_; + SoftbusCenterInterface::softbusCenterInterface_ = softbusCenterMock_; } void SoftbusListenerTest::TearDownTestCase() { @@ -58,6 +59,8 @@ void SoftbusListenerTest::TearDownTestCase() softbusCacheMock_ = nullptr; DMIPCSkeleton::dmIpcSkeleton_ = nullptr; ipcSkeletonMock_ = nullptr; + SoftbusCenterInterface::softbusCenterInterface_ = nullptr; + softbusCenterMock_ = nullptr; } namespace { @@ -278,6 +281,10 @@ HWTEST_F(SoftbusListenerTest, PublishSoftbusLNN_001, testing::ext::TestSize.Leve } int32_t ret = softbusListener->PublishSoftbusLNN(dmPubInfo, capability, customData); EXPECT_EQ(true, checkSoftbusRes(ret)); + + capability = DM_CAPABILITY_APPROACH; + ret = softbusListener->PublishSoftbusLNN(dmPubInfo, capability, customData); + EXPECT_EQ(true, checkSoftbusRes(ret)); } HWTEST_F(SoftbusListenerTest, StopPublishSoftbusLNN_001, testing::ext::TestSize.Level0) @@ -489,8 +496,13 @@ HWTEST_F(SoftbusListenerTest, GetNetworkTypeByNetworkId_001, testing::ext::TestS if (softbusListener == nullptr) { softbusListener = std::make_shared(); } + EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _)).WillOnce(Return(SOFTBUS_INVALID_PARAM)); int32_t ret = softbusListener->GetNetworkTypeByNetworkId(networkId, networkType); EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); + + EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _)).WillOnce(Return(DM_OK)); + ret = softbusListener->GetNetworkTypeByNetworkId(networkId, networkType); + EXPECT_EQ(ret, DM_OK); } HWTEST_F(SoftbusListenerTest, CacheDiscoveredDevice_001, testing::ext::TestSize.Level0) @@ -1071,8 +1083,13 @@ HWTEST_F(SoftbusListenerTest, GetDeviceScreenStatus_001, testing::ext::TestSize. if (softbusListener == nullptr) { softbusListener = std::make_shared(); } + EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _)).WillOnce(Return(SOFTBUS_INVALID_PARAM)); int32_t ret = softbusListener->GetDeviceScreenStatus(networkId.c_str(), screenStatus); EXPECT_TRUE(checkSoftbusRes(ret)); + + EXPECT_CALL(*softbusCenterMock_, GetNodeKeyInfo(_, _, _, _, _)).WillOnce(Return(DM_OK)); + ret = softbusListener->GetDeviceScreenStatus(networkId.c_str(), screenStatus); + EXPECT_FALSE(checkSoftbusRes(ret)); softbusListener = nullptr; } @@ -1096,13 +1113,13 @@ HWTEST_F(SoftbusListenerTest, SetForegroundUserIdsToDSoftBus_001, testing::ext:: HWTEST_F(SoftbusListenerTest, GetUdidFromDp_001, testing::ext::TestSize.Level0) { - std::string udidHash = "udidHash"; + std::string udidHashTemp = "udidHash"; std::string udid = "udid"; if (softbusListener == nullptr) { softbusListener = std::make_shared(); } EXPECT_CALL(*distributedDeviceProfileClientMock_, GetAllAccessControlProfile(_)).WillOnce(Return(ERR_DM_FAILED)); - int32_t ret = softbusListener->GetUdidFromDp(udidHash, udid); + int32_t ret = softbusListener->GetUdidFromDp(udidHashTemp, udid); EXPECT_EQ(ret, ERR_DM_FAILED); std::vector allProfile; @@ -1111,7 +1128,7 @@ HWTEST_F(SoftbusListenerTest, GetUdidFromDp_001, testing::ext::TestSize.Level0) allProfile.push_back(profile); EXPECT_CALL(*distributedDeviceProfileClientMock_, GetAllAccessControlProfile(_)) .WillOnce(DoAll(SetArgReferee<0>(allProfile), Return(DM_OK))); - ret = softbusListener->GetUdidFromDp(udidHash, udid); + ret = softbusListener->GetUdidFromDp(udidHashTemp, udid); EXPECT_EQ(ret, ERR_DM_FAILED); profile.SetBindType(2); @@ -1120,14 +1137,18 @@ HWTEST_F(SoftbusListenerTest, GetUdidFromDp_001, testing::ext::TestSize.Level0) EXPECT_CALL(*distributedDeviceProfileClientMock_, GetAllAccessControlProfile(_)) .WillOnce(DoAll(SetArgReferee<0>(allProfile), Return(DM_OK))); EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).Times(::testing::AtLeast(1)).WillOnce(Return(ERR_DM_FAILED)); - ret = softbusListener->GetUdidFromDp(udidHash, udid); + ret = softbusListener->GetUdidFromDp(udidHashTemp, udid); EXPECT_EQ(ret, ERR_DM_FAILED); EXPECT_CALL(*distributedDeviceProfileClientMock_, GetAllAccessControlProfile(_)) .WillOnce(DoAll(SetArgReferee<0>(allProfile), Return(DM_OK))); - EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).Times(::testing::AtLeast(1)).WillOnce(Return(DM_OK)); - ret = softbusListener->GetUdidFromDp(udidHash, udid); - EXPECT_EQ(ret, ERR_DM_FAILED); + EXPECT_CALL(*cryptoMock_, GetUdidHash(_, _)).Times(::testing::AtLeast(1)) + .WillOnce(WithArgs<1>(Invoke([udidHashTemp](unsigned char *udidHash) { + memcpy_s(udidHash, (udidHashTemp.length() + 1), udidHashTemp.c_str(), udidHashTemp.length()); + return DM_OK; + }))); + ret = softbusListener->GetUdidFromDp(udidHashTemp, udid); + EXPECT_EQ(ret, DM_OK); } HWTEST_F(SoftbusListenerTest, SetLocalDisplayName_001, testing::ext::TestSize.Level0) @@ -1325,6 +1346,31 @@ HWTEST_F(SoftbusListenerTest, GetAttrFromCustomData_001, testing::ext::TestSize. int32_t actionId = 1; int32_t ret = softbusListener->GetAttrFromCustomData(customDataJson, dmDevInfo, actionId); EXPECT_EQ(ret, DM_OK); + + EXPECT_CALL(*softbusCenterMock_, GetLocalNodeDeviceInfo(_, _)).WillOnce(Return(ERR_DM_FAILED)); + softbusListener->OnLocalDevInfoChange(); + + EXPECT_CALL(*softbusCenterMock_, GetLocalNodeDeviceInfo(_, _)).WillOnce(Return(DM_OK)); + EXPECT_CALL(*softbusCacheMock_, GetUdidFromCache(_, _)).WillOnce(Return(ERR_DM_FAILED)); + softbusListener->OnLocalDevInfoChange(); + + EXPECT_CALL(*softbusCenterMock_, GetLocalNodeDeviceInfo(_, _)).WillOnce(Return(DM_OK)); + EXPECT_CALL(*softbusCacheMock_, GetUdidFromCache(_, _)).WillOnce(Return(DM_OK)); + softbusListener->OnLocalDevInfoChange(); + + NodeBasicInfo *info = nullptr; + softbusListener->UpdateDeviceName(info); + + NodeBasicInfo nodeBasicInfo = { + .networkId = "123456", + .deviceName = "123456", + .deviceTypeId = 1 + }; + EXPECT_CALL(*softbusCacheMock_, GetUdidFromCache(_, _)).WillOnce(Return(DM_OK)); + softbusListener->UpdateDeviceName(&nodeBasicInfo); + + EXPECT_CALL(*softbusCacheMock_, GetUdidFromCache(_, _)).WillOnce(Return(DM_OK)); + softbusListener->UpdateDeviceName(&nodeBasicInfo); } } // namespace } // namespace DistributedHardware diff --git a/test/softbusunittest/UTTest_softbus_listener.h b/test/softbusunittest/UTTest_softbus_listener.h index c5dd4475987cb05a5cce1b71ea504b399f063fba..0f59fbe1587850778185897f6622d8616da5f5a8 100644 --- a/test/softbusunittest/UTTest_softbus_listener.h +++ b/test/softbusunittest/UTTest_softbus_listener.h @@ -33,6 +33,7 @@ #include "dm_crypto_mock.h" #include "dm_softbus_cache_mock.h" #include "dm_ipc_skeleton_mock.h" +#include "softbus_center_mock.h" namespace OHOS { namespace DistributedHardware { @@ -48,6 +49,7 @@ public: static inline std::shared_ptr cryptoMock_ = std::make_shared(); static inline std::shared_ptr softbusCacheMock_ = std::make_shared(); static inline std::shared_ptr ipcSkeletonMock_ = std::make_shared(); + static inline std::shared_ptr softbusCenterMock_ = std::make_shared(); }; class ISoftbusDiscoveringCallbackTest : public ISoftbusDiscoveringCallback { diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index eadcdfe5603ce2a7b13f7783f58f8e9a69780d84..7c7edfdd08ebdd5b73123219eb270467d2c4d3e4 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -575,6 +575,8 @@ ohos_unittest("UTTest_softbus_connector") { sources = [ "${devicemanager_path}/test/softbusunittest/UTTest_softbus_connector.cpp", + "mock/dm_crypto_mock.cpp", + "mock/softbus_center_mock.cpp", ] deps = [ ":device_manager_test_common" ] @@ -587,6 +589,7 @@ ohos_unittest("UTTest_softbus_connector") { "dsoftbus:softbus_client", "ffrt:libffrt", "googletest:gmock", + "googletest:gmock_main", "hilog:libhilog", "hisysevent:libhisysevent", "hitrace:hitrace_meter", @@ -610,8 +613,9 @@ ohos_unittest("UTTest_softbus_listener") { "${devicemanager_path}/test/unittest/mock/parameter.cpp", "mock/distributed_device_profile_client_mock.cpp", "mock/dm_crypto_mock.cpp", + "mock/dm_ipc_skeleton_mock.cpp", "mock/dm_softbus_cache_mock.cpp", - "mock/ipc_skeleton_mock.cpp", + "mock/softbus_center_mock.cpp", ] deps = [ ":device_manager_test_common" ] diff --git a/test/unittest/UTTest_device_manager_impl.cpp b/test/unittest/UTTest_device_manager_impl.cpp index 1eee8ed5d7e6bc35f0273a83d3d64e0363ac52a4..ecd7ed2ee8ef75c2b824da5e56f0893f15329b0d 100644 --- a/test/unittest/UTTest_device_manager_impl.cpp +++ b/test/unittest/UTTest_device_manager_impl.cpp @@ -476,8 +476,14 @@ HWTEST_F(DeviceManagerImplTest, GetDeviceType_103, testing::ext::TestSize.Level0 std::string packName = "com.ohos.test"; std::string networkId = "networkId"; int32_t deviceType = 0; + std::shared_ptr mockInstance = std::make_shared(); + std::shared_ptr ipcClientProxy = DeviceManagerImpl::GetInstance().ipcClientProxy_; + DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance; + EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_)) + .Times(1).WillOnce(testing::Return(ERR_DM_FAILED)); int32_t ret = DeviceManager::GetInstance().GetDeviceType(packName, networkId, deviceType); - ASSERT_EQ(ret, DM_OK); + ASSERT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED); + DeviceManagerImpl::GetInstance().ipcClientProxy_ = ipcClientProxy; } /** diff --git a/test/unittest/UTTest_device_manager_service.cpp b/test/unittest/UTTest_device_manager_service.cpp index 0bc009aa4cc25c82e7e2407cb503d3ea206fb164..d00099fbbbee8a5e2c0c235f84ad8003e2277844 100644 --- a/test/unittest/UTTest_device_manager_service.cpp +++ b/test/unittest/UTTest_device_manager_service.cpp @@ -1847,8 +1847,9 @@ HWTEST_F(DeviceManagerServiceTest, DisableDiscoveryListener_004, testing::ext::T std::string pkgName = "pkgName"; std::map extraParam; DeviceManagerService::GetInstance().InitDMServiceListener(); + EXPECT_CALL(*softbusListenerMock_, StopRefreshSoftbusLNN(_)).WillOnce(Return(SOFTBUS_NETWORK_NOT_INIT)); int32_t ret = DeviceManagerService::GetInstance().DisableDiscoveryListener(pkgName, extraParam); - EXPECT_NE(ret, DM_OK); + EXPECT_EQ(ret, SOFTBUS_NETWORK_NOT_INIT); DeviceManagerService::GetInstance().UninitDMServiceListener(); } diff --git a/test/unittest/UTTest_device_manager_service_impl.cpp b/test/unittest/UTTest_device_manager_service_impl.cpp index 40bc76691b677e9288832f95c13ade0bd1af76d5..54430129527c7da96f37eb9b8ad3763837f757cb 100644 --- a/test/unittest/UTTest_device_manager_service_impl.cpp +++ b/test/unittest/UTTest_device_manager_service_impl.cpp @@ -1933,6 +1933,23 @@ HWTEST_F(DeviceManagerServiceImplTest, DeleteCredential_010, testing::ext::TestS } int32_t ret = deviceManagerServiceImpl_->DeleteCredential(pkgName, deleteInfo); EXPECT_EQ(ret, ERR_DM_FAILED); + + std::vector profiles; + DistributedDeviceProfile::AccessControlProfile accessProfile; + accessProfile.SetBindType(1); + profiles.push_back(accessProfile); + EXPECT_CALL(*deviceProfileConnectorMock_, GetAllAccessControlProfile()).WillOnce(Return(profiles)); + deviceManagerServiceImpl_->DeleteAlwaysAllowTimeOut(); + + std::string remoteUdid = "remoteUdid"; + profiles.clear(); + EXPECT_CALL(*deviceProfileConnectorMock_, GetAllAccessControlProfile()).WillOnce(Return(profiles)); + deviceManagerServiceImpl_->CheckDeleteCredential(remoteUdid); + + accessProfile.SetTrustDeviceId(remoteUdid); + profiles.push_back(accessProfile); + EXPECT_CALL(*deviceProfileConnectorMock_, GetAllAccessControlProfile()).WillOnce(Return(profiles)); + deviceManagerServiceImpl_->CheckDeleteCredential(remoteUdid); } } // namespace } // namespace DistributedHardware diff --git a/test/unittest/UTTest_dm_softbus_cache.cpp b/test/unittest/UTTest_dm_softbus_cache.cpp index 0cffa012e5a611fbdec95d40482ddbc256a6f3fe..9fc8e4f5fee18c10a63d1b3b7906e89d331faaa2 100644 --- a/test/unittest/UTTest_dm_softbus_cache.cpp +++ b/test/unittest/UTTest_dm_softbus_cache.cpp @@ -76,7 +76,7 @@ namespace { bool CheckSoftbusRes(int32_t ret) { return ret == SOFTBUS_INVALID_PARAM || ret == SOFTBUS_NETWORK_NOT_INIT || ret == SOFTBUS_NETWORK_LOOPER_ERR || - ret == SOFTBUS_IPC_ERR || ret == ERR_DM_FAILED || ret == SOFTBUS_NETWORK_GET_ALL_NODE_INFO_ERR; + ret == SOFTBUS_IPC_ERR || ret == ERR_DM_FAILED || ret == SOFTBUS_NETWORK_GET_NODE_INFO_ERR; } HWTEST_F(DMSoftbusCacheTest, GetDeviceInfoFromCache_001, testing::ext::TestSize.Level0) diff --git a/test/unittest/UTTest_dm_transport_msg.cpp b/test/unittest/UTTest_dm_transport_msg.cpp index 24955cd307c71bc4ad2cd41aff64ac68ab8afa65..b3817ed5e7741d7dd0b90874055068e7a85e6942 100644 --- a/test/unittest/UTTest_dm_transport_msg.cpp +++ b/test/unittest/UTTest_dm_transport_msg.cpp @@ -183,5 +183,25 @@ HWTEST_F(DMTransportMsgTest, NotifyUserIds_ToString_EmptyInput, testing::ext::Te std::string result = notifyUserIds.ToString(); EXPECT_FALSE(result.empty()); } + +HWTEST_F(DMTransportMsgTest, ToJson_UserIdsMsg, testing::ext::TestSize.Level0) +{ + std::vector foregroundUserIds{1, 2, 3}; + std::vector backgroundUserIds{4, 5, 6}; + UserIdsMsg userIdsMsg(foregroundUserIds, backgroundUserIds); + const char* jsonStr = R"({ + "MsgType": "0", + "msg": "messgaeinfo", + "code": 145, + "userIds": [ + {"type": 1, "userId": 111}, + {"type": 0, "userId": 222} + ] + })"; + cJSON *jsonObject = cJSON_Parse(jsonStr); + ToJson(jsonObject, userIdsMsg); + cJSON_Delete(jsonObject); + EXPECT_FALSE(userIdsMsg.foregroundUserIds.empty()); +} } // DistributedHardware } // OHOS \ No newline at end of file diff --git a/test/unittest/mock/distributed_device_profile_client_mock.cpp b/test/unittest/mock/distributed_device_profile_client_mock.cpp index c6c1406d5b603c6b9d4ac78813cf8df479c0a4d6..be97b815421344fccb30b2d942058875d3abe754 100644 --- a/test/unittest/mock/distributed_device_profile_client_mock.cpp +++ b/test/unittest/mock/distributed_device_profile_client_mock.cpp @@ -56,5 +56,12 @@ int32_t DistributedDeviceProfileClient::PutAllTrustedDevices( { return DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient->PutAllTrustedDevices(deviceInfos); } + +int32_t DistributedDeviceProfileClient::PutSessionKey( + uint32_t userId, const std::vector& sessionKey, int32_t& sessionKeyId) +{ + return DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient-> + PutSessionKey(userId, sessionKey, sessionKeyId); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/distributed_device_profile_client_mock.h b/test/unittest/mock/distributed_device_profile_client_mock.h index dc55b8205a973a976202d98a2489a24f5360e2b3..1d65cceb5578d8a9e3b46477c6f4eb612e17f71c 100644 --- a/test/unittest/mock/distributed_device_profile_client_mock.h +++ b/test/unittest/mock/distributed_device_profile_client_mock.h @@ -34,6 +34,7 @@ public: virtual int32_t UnSubscribeDeviceProfileInited(int32_t saId) = 0; virtual int32_t PutAllTrustedDevices(const std::vector &deviceInfos) = 0; + virtual int32_t PutSessionKey(uint32_t userId, const std::vector& sessionKey, int32_t& sessionKeyId) = 0; public: static inline std::shared_ptr dpDistributedDeviceProfileClient = nullptr; }; @@ -47,6 +48,7 @@ public: MOCK_METHOD(int32_t, SubscribeDeviceProfileInited, (int32_t, sptr)); MOCK_METHOD(int32_t, UnSubscribeDeviceProfileInited, (int32_t)); MOCK_METHOD(int32_t, PutAllTrustedDevices, (const std::vector &)); + MOCK_METHOD(int32_t, PutSessionKey, (uint32_t, (const std::vector&), int32_t&)); }; } } diff --git a/test/unittest/mock/softbus_center_mock.cpp b/test/unittest/mock/softbus_center_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d01cfba3cdcbed39c0510722cf8a6c5cb0f6108 --- /dev/null +++ b/test/unittest/mock/softbus_center_mock.cpp @@ -0,0 +1,48 @@ +/* + * 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 "softbus_center_mock.h" + +#include "gtest/gtest.h" + +namespace OHOS { +namespace DistributedHardware { + +SoftbusCenterMock::SoftbusCenterMock() +{ +} +SoftbusCenterMock::~SoftbusCenterMock() +{ +} + +extern "C" { +int32_t GetNodeKeyInfo(const char *pkgName, const char *networkId, NodeDeviceInfoKey key, uint8_t *info, + int32_t infoLen) +{ + return SoftbusCenterInterface::softbusCenterInterface_->GetNodeKeyInfo(pkgName, networkId, key, info, infoLen); +} + +int32_t GetLocalNodeDeviceInfo(const char *pkgName, NodeBasicInfo *info) +{ + return SoftbusCenterInterface::softbusCenterInterface_->GetLocalNodeDeviceInfo(pkgName, info); +} + +int32_t GetAllNodeDeviceInfo(const char *pkgName, NodeBasicInfo **info, int32_t *infoNum) +{ + return SoftbusCenterInterface::softbusCenterInterface_->GetAllNodeDeviceInfo(pkgName, info, infoNum); +} +} +} //namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/softbus_center_mock.h b/test/unittest/mock/softbus_center_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..149d0b61f21e9de6a55ff37d23f5f84e2182218d --- /dev/null +++ b/test/unittest/mock/softbus_center_mock.h @@ -0,0 +1,47 @@ +/* + * 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_SOFTBUS_CENTER_MOCK_H +#define OHOS_SOFTBUS_CENTER_MOCK_H + +#include +#include + +#include "softbus_bus_center.h" + +namespace OHOS { +namespace DistributedHardware { +class SoftbusCenterInterface { +public: + virtual ~SoftbusCenterInterface() = default; +public: + virtual int32_t GetNodeKeyInfo(const char *pkgName, const char *networkId, NodeDeviceInfoKey key, uint8_t *info, + int32_t infoLen) = 0; + virtual int32_t GetLocalNodeDeviceInfo(const char *pkgName, NodeBasicInfo *info) = 0; + virtual int32_t GetAllNodeDeviceInfo(const char *pkgName, NodeBasicInfo **info, int32_t *infoNum) = 0; +public: + static inline std::shared_ptr softbusCenterInterface_ = nullptr; +}; + +class SoftbusCenterMock : public SoftbusCenterInterface { +public: + SoftbusCenterMock(); + ~SoftbusCenterMock() override; + MOCK_METHOD(int32_t, GetNodeKeyInfo, (const char *, const char *, NodeDeviceInfoKey, uint8_t *, int32_t)); + MOCK_METHOD(int32_t, GetLocalNodeDeviceInfo, (const char *, NodeBasicInfo *)); + MOCK_METHOD(int32_t, GetAllNodeDeviceInfo, (const char *, NodeBasicInfo **, int32_t *)); +}; +} +} +#endif diff --git a/test/unittest/mock/softbus_listener_mock.cpp b/test/unittest/mock/softbus_listener_mock.cpp index b983217b19fe0a9b612def88da8fa2ce311d5d4b..3049008b36aaaadb66d2f803c216634cc6064a82 100644 --- a/test/unittest/mock/softbus_listener_mock.cpp +++ b/test/unittest/mock/softbus_listener_mock.cpp @@ -91,5 +91,10 @@ int32_t SoftbusListener::GetAllTrustedDeviceList(const std::string &pkgName, con { return DmSoftbusListener::dmSoftbusListener->GetAllTrustedDeviceList(pkgName, extra, deviceList); } + +int32_t SoftbusListener::StopRefreshSoftbusLNN(uint16_t subscribeId) +{ + return DmSoftbusListener::dmSoftbusListener->StopRefreshSoftbusLNN(subscribeId); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/softbus_listener_mock.h b/test/unittest/mock/softbus_listener_mock.h index 6f5984e1819bf46e17e2c56f0afbdde817782cb6..808d34b6fbbee7b00a52e44fed11eb1a15245d24 100644 --- a/test/unittest/mock/softbus_listener_mock.h +++ b/test/unittest/mock/softbus_listener_mock.h @@ -43,6 +43,7 @@ public: virtual int32_t GetUdidFromDp(const std::string &udidHash, std::string &udid) = 0; virtual int32_t GetAllTrustedDeviceList(const std::string &pkgName, const std::string &extra, std::vector &deviceList) = 0; + virtual int32_t StopRefreshSoftbusLNN(uint16_t subscribeId) = 0; public: static inline std::shared_ptr dmSoftbusListener = nullptr; }; @@ -64,6 +65,7 @@ public: MOCK_METHOD(int32_t, GetUdidFromDp, (const std::string &, std::string &)); MOCK_METHOD(int32_t, GetAllTrustedDeviceList, (const std::string &, const std::string &, std::vector &)); + MOCK_METHOD(int32_t, StopRefreshSoftbusLNN, (uint16_t)); }; } }