From f2821bcc05841b593afa02e345772fc89dec1a74 Mon Sep 17 00:00:00 2001 From: l60055366 Date: Mon, 11 Nov 2024 15:15:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Efoundation/distributedhardwar?= =?UTF-8?q?e/device=5Fmanager=E6=A8=A1=E5=9D=97=E7=9A=84ut=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: l60055366 --- .../hichain_connector_fuzzer.cpp | 1 - .../UTTest_dm_auth_manager_first.cpp | 2 +- .../UTTest_dm_deviceprofile_connector.cpp | 483 +++++++++++++++++- ...Test_dm_deviceprofile_connector_second.cpp | 115 +++++ ...UTTest_dm_deviceprofile_connector_second.h | 41 ++ test/unittest/BUILD.gn | 72 ++- .../UTTest_device_manager_service.cpp | 152 +++++- test/unittest/UTTest_device_manager_service.h | 4 +- .../UTTest_device_manager_service_impl.cpp | 13 + .../UTTest_device_manager_service_three.cpp | 380 ++++++++++++++ .../UTTest_device_manager_service_three.h | 58 +++ .../UTTest_device_manager_service_two.cpp | 2 + .../mock/device_manager_service_impl_mock.cpp | 12 + .../mock/device_manager_service_impl_mock.h | 5 + .../mock/device_manager_service_mock.cpp | 28 + .../mock/device_manager_service_mock.h | 40 ++ .../mock/deviceprofile_connector_mock.cpp | 21 + .../mock/deviceprofile_connector_mock.h | 10 + ...distributed_device_profile_client_mock.cpp | 43 ++ .../distributed_device_profile_client_mock.h | 46 ++ test/unittest/mock/dm_softbus_cache_mock.cpp | 28 + test/unittest/mock/dm_softbus_cache_mock.h | 40 ++ ...mock_distributed_device_profile_client.cpp | 209 ++++++++ test/unittest/mock/softbus_listener_mock.cpp | 16 + test/unittest/mock/softbus_listener_mock.h | 7 + 25 files changed, 1805 insertions(+), 23 deletions(-) create mode 100644 test/commonunittest/UTTest_dm_deviceprofile_connector_second.cpp create mode 100644 test/commonunittest/UTTest_dm_deviceprofile_connector_second.h create mode 100644 test/unittest/UTTest_device_manager_service_three.cpp create mode 100644 test/unittest/UTTest_device_manager_service_three.h create mode 100644 test/unittest/mock/device_manager_service_mock.cpp create mode 100644 test/unittest/mock/device_manager_service_mock.h create mode 100644 test/unittest/mock/distributed_device_profile_client_mock.cpp create mode 100644 test/unittest/mock/distributed_device_profile_client_mock.h create mode 100644 test/unittest/mock/dm_softbus_cache_mock.cpp create mode 100644 test/unittest/mock/dm_softbus_cache_mock.h diff --git a/test/commonfuzztest/hichainconnector_fuzzer/hichain_connector_fuzzer.cpp b/test/commonfuzztest/hichainconnector_fuzzer/hichain_connector_fuzzer.cpp index 4158807f0..b62626826 100644 --- a/test/commonfuzztest/hichainconnector_fuzzer/hichain_connector_fuzzer.cpp +++ b/test/commonfuzztest/hichainconnector_fuzzer/hichain_connector_fuzzer.cpp @@ -190,7 +190,6 @@ void HiChainConnectorThirdFuzzTest(const uint8_t* data, size_t size) hichainConnector->GetJsonInt(jsonOutObj, "key"); hichainConnector->deleteMultiMembers(groupType, userId, jsonOutObj); hichainConnector->DeleteAllGroupByUdid(reqParams); - hichainConnector->DeleteP2PGroup(switchUserId); } void HiChainConnectorForthFuzzTest(const uint8_t* data, size_t size) diff --git a/test/commonunittest/UTTest_dm_auth_manager_first.cpp b/test/commonunittest/UTTest_dm_auth_manager_first.cpp index 57f0ca81b..4af67b3a8 100644 --- a/test/commonunittest/UTTest_dm_auth_manager_first.cpp +++ b/test/commonunittest/UTTest_dm_auth_manager_first.cpp @@ -303,7 +303,7 @@ HWTEST_F(DmAuthManagerTest, GetPinCode_002, testing::ext::TestSize.Level0) authManager_->ShowStartAuthDialog(); int32_t code = 0; authManager_->GetPinCode(code); - ASSERT_EQ(code, ERR_DM_AUTH_NOT_START); + ASSERT_EQ(code, DM_OK); } HWTEST_F(DmAuthManagerTest, SetPageId_001, testing::ext::TestSize.Level0) diff --git a/test/commonunittest/UTTest_dm_deviceprofile_connector.cpp b/test/commonunittest/UTTest_dm_deviceprofile_connector.cpp index 3e2038dc2..c71f80db2 100644 --- a/test/commonunittest/UTTest_dm_deviceprofile_connector.cpp +++ b/test/commonunittest/UTTest_dm_deviceprofile_connector.cpp @@ -36,6 +36,274 @@ void DeviceProfileConnectorTest::TearDownTestCase() { } +void AddAccessControlProfileFirst(std::vector& accessControlProfiles) +{ + int32_t userId = 123456; + int32_t bindType = 256; + int32_t deviceIdType = 1; + uint32_t bindLevel = 1; + uint32_t status = 1; + uint32_t authenticationType = 2; + uint32_t accesserId = 1; + uint32_t tokenId = 1001; + + std::string oldAccountId = "oldAccountId"; + std::string newAccountId = "newAccountId"; + std::string deviceIdEr = "remoteDeviceId"; + std::string deviceIdEe = "localDeviceId"; + std::string trustDeviceId = "123456"; + + DistributedDeviceProfile::Accesser accesser; + accesser.SetAccesserId(accesserId); + accesser.SetAccesserDeviceId(deviceIdEr); + accesser.SetAccesserUserId(userId); + accesser.SetAccesserAccountId(oldAccountId); + accesser.SetAccesserTokenId(tokenId); + accesser.SetAccesserBundleName("bundleName"); + accesser.SetAccesserHapSignature("uph1"); + accesser.SetAccesserBindLevel(bindLevel); + + DistributedDeviceProfile::Accessee accessee; + accessee.SetAccesseeId(accesserId); + accessee.SetAccesseeDeviceId(deviceIdEe); + accessee.SetAccesseeUserId(userId); + accessee.SetAccesseeAccountId(newAccountId); + accessee.SetAccesseeTokenId(tokenId); + accessee.SetAccesseeBundleName("bundleName"); + accessee.SetAccesseeHapSignature("uph1"); + accessee.SetAccesseeBindLevel(bindLevel); + + DistributedDeviceProfile::AccessControlProfile profileFifth; + profileFifth.SetAccessControlId(accesserId); + profileFifth.SetAccesserId(accesserId); + profileFifth.SetAccesseeId(accesserId); + profileFifth.SetTrustDeviceId(trustDeviceId); + profileFifth.SetBindType(bindType); + profileFifth.SetAuthenticationType(authenticationType); + profileFifth.SetDeviceIdType(deviceIdType); + profileFifth.SetStatus(status); + profileFifth.SetBindLevel(bindLevel); + profileFifth.SetAccesser(accesser); + profileFifth.SetAccessee(accessee); + accessControlProfiles.push_back(profileFifth); +} + +void AddAccessControlProfileSecond(std::vector& accessControlProfiles) +{ + int32_t userId = 123456; + int32_t bindType = 256; + int32_t deviceIdType = 1; + uint32_t bindLevel = 3; + uint32_t status = 1; + uint32_t authenticationType = 2; + uint32_t accesserId = 1; + uint32_t tokenId = 1001; + + std::string oldAccountId = "oldAccountId"; + std::string newAccountId = "newAccountId"; + std::string deviceIdEr = "remoteDeviceId"; + std::string deviceIdEe = "localDeviceId"; + std::string trustDeviceId = "localDeviceId"; + + DistributedDeviceProfile::Accesser accesser; + accesser.SetAccesserId(accesserId); + accesser.SetAccesserDeviceId(deviceIdEr); + accesser.SetAccesserUserId(userId); + accesser.SetAccesserAccountId(oldAccountId); + accesser.SetAccesserTokenId(tokenId); + accesser.SetAccesserBundleName("bundleName1"); + accesser.SetAccesserHapSignature("uph1"); + accesser.SetAccesserBindLevel(bindLevel); + + DistributedDeviceProfile::Accessee accessee; + accessee.SetAccesseeId(accesserId); + accessee.SetAccesseeDeviceId(deviceIdEe); + accessee.SetAccesseeUserId(userId); + accessee.SetAccesseeAccountId(newAccountId); + accessee.SetAccesseeTokenId(tokenId); + accessee.SetAccesseeBundleName("bundleName2"); + accessee.SetAccesseeHapSignature("uph1"); + accessee.SetAccesseeBindLevel(bindLevel); + + DistributedDeviceProfile::AccessControlProfile profileFifth; + profileFifth.SetAccessControlId(accesserId); + profileFifth.SetAccesserId(accesserId); + profileFifth.SetAccesseeId(accesserId); + profileFifth.SetTrustDeviceId(trustDeviceId); + profileFifth.SetBindType(bindType); + profileFifth.SetAuthenticationType(authenticationType); + profileFifth.SetDeviceIdType(deviceIdType); + profileFifth.SetStatus(status); + profileFifth.SetBindLevel(bindLevel); + profileFifth.SetAccesser(accesser); + profileFifth.SetAccessee(accessee); + accessControlProfiles.push_back(profileFifth); +} + +void AddAccessControlProfileThird(std::vector& accessControlProfiles) +{ + int32_t userId = 123456; + int32_t bindType = 256; + int32_t deviceIdType = 1; + uint32_t bindLevel = 3; + uint32_t status = 1; + uint32_t authenticationType = 2; + uint32_t accesserId = 1; + uint32_t tokenId = 1001; + + std::string oldAccountId = "oldAccountId"; + std::string newAccountId = "newAccountId"; + std::string deviceIdEr = "remoteDeviceId"; + std::string deviceIdEe = "localDeviceId"; + std::string trustDeviceId = "remoteDeviceId"; + + DistributedDeviceProfile::Accesser accesser; + accesser.SetAccesserId(accesserId); + accesser.SetAccesserDeviceId(deviceIdEr); + accesser.SetAccesserUserId(userId); + accesser.SetAccesserAccountId(oldAccountId); + accesser.SetAccesserTokenId(tokenId); + accesser.SetAccesserBundleName("bundleName1"); + accesser.SetAccesserHapSignature("uph1"); + accesser.SetAccesserBindLevel(bindLevel); + + DistributedDeviceProfile::Accessee accessee; + accessee.SetAccesseeId(accesserId); + accessee.SetAccesseeDeviceId(deviceIdEe); + accessee.SetAccesseeUserId(userId); + accessee.SetAccesseeAccountId(newAccountId); + accessee.SetAccesseeTokenId(tokenId); + accessee.SetAccesseeBundleName("bundleName2"); + accessee.SetAccesseeHapSignature("uph1"); + accessee.SetAccesseeBindLevel(bindLevel); + + DistributedDeviceProfile::AccessControlProfile profileFifth; + profileFifth.SetAccessControlId(accesserId); + profileFifth.SetAccesserId(accesserId); + profileFifth.SetAccesseeId(accesserId); + profileFifth.SetTrustDeviceId(trustDeviceId); + profileFifth.SetBindType(bindType); + profileFifth.SetAuthenticationType(authenticationType); + profileFifth.SetDeviceIdType(deviceIdType); + profileFifth.SetStatus(status); + profileFifth.SetBindLevel(bindLevel); + profileFifth.SetAccesser(accesser); + profileFifth.SetAccessee(accessee); + accessControlProfiles.push_back(profileFifth); +} + +void AddAccessControlProfileForth(std::vector& accessControlProfiles) +{ + int32_t userId = 123456; + int32_t bindType = 256; + int32_t deviceIdType = 1; + uint32_t bindLevel = 2; + uint32_t status = 1; + uint32_t authenticationType = 2; + uint32_t accesserId = 1; + uint32_t tokenId = 1001; + + std::string oldAccountId = "oldAccountId"; + std::string newAccountId = "newAccountId"; + std::string deviceIdEr = "remoteDeviceId"; + std::string deviceIdEe = "localDeviceId"; + std::string trustDeviceId = "localDeviceId"; + + DistributedDeviceProfile::Accesser accesser; + accesser.SetAccesserId(accesserId); + accesser.SetAccesserDeviceId(deviceIdEr); + accesser.SetAccesserUserId(userId); + accesser.SetAccesserAccountId(oldAccountId); + accesser.SetAccesserTokenId(tokenId); + accesser.SetAccesserBundleName("bundleName1"); + accesser.SetAccesserHapSignature("uph1"); + accesser.SetAccesserBindLevel(bindLevel); + + DistributedDeviceProfile::Accessee accessee; + accessee.SetAccesseeId(accesserId); + accessee.SetAccesseeDeviceId(deviceIdEe); + accessee.SetAccesseeUserId(userId); + accessee.SetAccesseeAccountId(newAccountId); + accessee.SetAccesseeTokenId(tokenId); + accessee.SetAccesseeBundleName("bundleName2"); + accessee.SetAccesseeHapSignature("uph1"); + accessee.SetAccesseeBindLevel(bindLevel); + + DistributedDeviceProfile::AccessControlProfile profileFifth; + profileFifth.SetAccessControlId(accesserId); + profileFifth.SetAccesserId(accesserId); + profileFifth.SetAccesseeId(accesserId); + profileFifth.SetTrustDeviceId(trustDeviceId); + profileFifth.SetBindType(bindType); + profileFifth.SetAuthenticationType(authenticationType); + profileFifth.SetDeviceIdType(deviceIdType); + profileFifth.SetStatus(status); + profileFifth.SetBindLevel(bindLevel); + profileFifth.SetAccesser(accesser); + profileFifth.SetAccessee(accessee); + accessControlProfiles.push_back(profileFifth); +} + +void AddAccessControlProfileFifth(std::vector& accessControlProfiles) +{ + int32_t userId = 123456; + int32_t bindType = 256; + int32_t deviceIdType = 1; + uint32_t bindLevel = 2; + uint32_t status = 1; + uint32_t authenticationType = 2; + uint32_t accesserId = 1; + uint32_t tokenId = 1001; + + std::string oldAccountId = "oldAccountId"; + std::string newAccountId = "newAccountId"; + std::string deviceIdEr = "remoteDeviceId"; + std::string deviceIdEe = "localDeviceId"; + std::string trustDeviceId = "remoteDeviceId"; + + DistributedDeviceProfile::Accesser accesser; + accesser.SetAccesserId(accesserId); + accesser.SetAccesserDeviceId(deviceIdEr); + accesser.SetAccesserUserId(userId); + accesser.SetAccesserAccountId(oldAccountId); + accesser.SetAccesserTokenId(tokenId); + accesser.SetAccesserBundleName("bundleName1"); + accesser.SetAccesserHapSignature("uph1"); + accesser.SetAccesserBindLevel(bindLevel); + + DistributedDeviceProfile::Accessee accessee; + accessee.SetAccesseeId(accesserId); + accessee.SetAccesseeDeviceId(deviceIdEe); + accessee.SetAccesseeUserId(userId); + accessee.SetAccesseeAccountId(newAccountId); + accessee.SetAccesseeTokenId(tokenId); + accessee.SetAccesseeBundleName("bundleName2"); + accessee.SetAccesseeHapSignature("uph1"); + accessee.SetAccesseeBindLevel(bindLevel); + + DistributedDeviceProfile::AccessControlProfile profileFifth; + profileFifth.SetAccessControlId(accesserId); + profileFifth.SetAccesserId(accesserId); + profileFifth.SetAccesseeId(accesserId); + profileFifth.SetTrustDeviceId(trustDeviceId); + profileFifth.SetBindType(bindType); + profileFifth.SetAuthenticationType(authenticationType); + profileFifth.SetDeviceIdType(deviceIdType); + profileFifth.SetStatus(status); + profileFifth.SetBindLevel(bindLevel); + profileFifth.SetAccesser(accesser); + profileFifth.SetAccessee(accessee); + accessControlProfiles.push_back(profileFifth); +} + +void GetAccessControlProfiles(std::vector& accessControlProfiles) +{ + AddAccessControlProfileFirst(accessControlProfiles); + AddAccessControlProfileSecond(accessControlProfiles); + AddAccessControlProfileThird(accessControlProfiles); + AddAccessControlProfileForth(accessControlProfiles); + AddAccessControlProfileFifth(accessControlProfiles); +} HWTEST_F(DeviceProfileConnectorTest, GetAccessControlProfile_001, testing::ext::TestSize.Level0) { auto ret = DeviceProfileConnector::GetInstance().GetAccessControlProfile(); @@ -47,7 +315,7 @@ HWTEST_F(DeviceProfileConnectorTest, GetAppTrustDeviceList_001, testing::ext::Te std::string pkgName; std::string deviceId = "deviceId"; auto ret = DeviceProfileConnector::GetInstance().GetAppTrustDeviceList(pkgName, deviceId); - EXPECT_EQ(ret.empty(), true); + EXPECT_EQ(ret.empty(), false); } HWTEST_F(DeviceProfileConnectorTest, GetAppTrustDeviceList_002, testing::ext::TestSize.Level0) @@ -55,7 +323,15 @@ HWTEST_F(DeviceProfileConnectorTest, GetAppTrustDeviceList_002, testing::ext::Te std::string pkgName = "bundleName"; std::string deviceId = "deviceId"; auto ret = DeviceProfileConnector::GetInstance().GetAppTrustDeviceList(pkgName, deviceId); - EXPECT_EQ(ret.empty(), true); + EXPECT_EQ(ret.empty(), false); +} + +HWTEST_F(DeviceProfileConnectorTest, GetAppTrustDeviceList_003, testing::ext::TestSize.Level0) +{ + std::string pkgName = "bundleName"; + std::string deviceId = "deviceId_003"; + auto ret = DeviceProfileConnector::GetInstance().GetAppTrustDeviceList(pkgName, deviceId); + EXPECT_EQ(ret.empty(), false); } HWTEST_F(DeviceProfileConnectorTest, GetDeviceAclParam_001, testing::ext::TestSize.Level0) @@ -195,6 +471,15 @@ HWTEST_F(DeviceProfileConnectorTest, GetBindTypeByPkgName_001, testing::ext::Tes EXPECT_EQ(ret.empty(), true); } +HWTEST_F(DeviceProfileConnectorTest, GetBindTypeByPkgName_002, testing::ext::TestSize.Level0) +{ + std::string pkgName; + std::string requestDeviceId; + std::string trustUdid = "123456"; + auto ret = DeviceProfileConnector::GetInstance().GetBindTypeByPkgName(pkgName, requestDeviceId, trustUdid); + EXPECT_EQ(ret.empty(), false); +} + HWTEST_F(DeviceProfileConnectorTest, GetParamBindTypeVec_001, testing::ext::TestSize.Level0) { DistributedDeviceProfile::AccessControlProfile profiles; @@ -451,6 +736,45 @@ HWTEST_F(DeviceProfileConnectorTest, SyncAclByBindType_001, testing::ext::TestSi EXPECT_EQ(ret.empty(), true); } +HWTEST_F(DeviceProfileConnectorTest, SyncAclByBindType_002, testing::ext::TestSize.Level0) +{ + std::string pkgName; + std::vector bindTypeVec; + std::string localDeviceId; + std::string targetDeviceId = "123456"; + bindTypeVec.push_back(IDENTICAL_ACCOUNT_TYPE); + bindTypeVec.push_back(DEVICE_PEER_TO_PEER_TYPE); + auto ret = DeviceProfileConnector::GetInstance().SyncAclByBindType(pkgName, bindTypeVec, localDeviceId, + targetDeviceId); + EXPECT_EQ(ret.empty(), false); +} + +HWTEST_F(DeviceProfileConnectorTest, SyncAclByBindType_003, testing::ext::TestSize.Level0) +{ + std::string pkgName = "bundleName"; + std::vector bindTypeVec; + std::string localDeviceId = "deviceId"; + std::string targetDeviceId = "remoteDeviceId"; + bindTypeVec.push_back(DEVICE_ACROSS_ACCOUNT_TYPE); + bindTypeVec.push_back(APP_ACROSS_ACCOUNT_TYPE); + auto ret = DeviceProfileConnector::GetInstance().SyncAclByBindType(pkgName, bindTypeVec, localDeviceId, + targetDeviceId); + EXPECT_EQ(ret.empty(), false); +} + +HWTEST_F(DeviceProfileConnectorTest, SyncAclByBindType_004, testing::ext::TestSize.Level0) +{ + std::string pkgName = "bundleName"; + std::vector bindTypeVec; + std::string localDeviceId = "deviceId"; + std::string targetDeviceId = "remoteDeviceId"; + bindTypeVec.push_back(DEVICE_PEER_TO_PEER_TYPE); + bindTypeVec.push_back(IDENTICAL_ACCOUNT_TYPE); + auto ret = DeviceProfileConnector::GetInstance().SyncAclByBindType(pkgName, bindTypeVec, localDeviceId, + targetDeviceId); + EXPECT_EQ(ret.empty(), true); +} + HWTEST_F(DeviceProfileConnectorTest, GetPkgNameFromAcl_001, testing::ext::TestSize.Level0) { std::string localDeviceId = "localDeviceId"; @@ -488,7 +812,7 @@ HWTEST_F(DeviceProfileConnectorTest, GetOfflineParamFromAcl_002, testing::ext::T std::string trustDeviceId = "123456"; std::string requestDeviceId = "deviceId"; auto ret = DeviceProfileConnector::GetInstance().GetOfflineParamFromAcl(trustDeviceId, requestDeviceId); - EXPECT_EQ(ret.bindType, INVALIED_TYPE); + EXPECT_EQ(ret.bindType, 5); } HWTEST_F(DeviceProfileConnectorTest, GetOfflineParamFromAcl_003, testing::ext::TestSize.Level0) @@ -504,6 +828,8 @@ HWTEST_F(DeviceProfileConnectorTest, PutAccessControlList_001, testing::ext::Tes DmAclInfo aclInfo; DmAccesser dmAccesser; DmAccessee dmAccessee; + int32_t userId = 123456; + DeviceProfileConnector::GetInstance().DeleteAclForUserRemoved(userId); int32_t ret = DeviceProfileConnector::GetInstance().PutAccessControlList(aclInfo, dmAccesser, dmAccessee); EXPECT_EQ(ret, DM_OK); } @@ -833,5 +1159,156 @@ HWTEST_F(DeviceProfileConnectorTest, SingleUserProcess_001, testing::ext::TestSi ret = DeviceProfileConnector::GetInstance().SingleUserProcess(profile, caller, callee); EXPECT_EQ(ret, false); } + +HWTEST_F(DeviceProfileConnectorTest, GetAccessControlProfileByUserId_001, testing::ext::TestSize.Level0) +{ + int32_t userId = DEVICE; + std::vector profiles; + profiles = DeviceProfileConnector::GetInstance().GetAccessControlProfileByUserId(userId); + EXPECT_GE(profiles.size(), 0); +} + +HWTEST_F(DeviceProfileConnectorTest, DeleteAppBindLevel_001, testing::ext::TestSize.Level0) +{ + DmOfflineParam offlineParam; + std::string pkgName = "bundleName1"; + std::vector profiles; + std::string localUdid = "remoteDeviceId"; + std::string remoteUdid="localDeviceId"; + GetAccessControlProfiles(profiles); + DeviceProfileConnector::GetInstance().DeleteAppBindLevel(offlineParam, pkgName, profiles, localUdid, remoteUdid); + EXPECT_EQ(offlineParam.bindType, APP); + + pkgName = "bundleName2"; + localUdid = "localDeviceId"; + remoteUdid="remoteDeviceId"; + DeviceProfileConnector::GetInstance().DeleteAppBindLevel(offlineParam, pkgName, profiles, localUdid, remoteUdid); + EXPECT_EQ(offlineParam.bindType, APP); +} + +HWTEST_F(DeviceProfileConnectorTest, DeleteDeviceBindLevel_001, testing::ext::TestSize.Level0) +{ + DmOfflineParam offlineParam; + std::vector profiles; + std::string localUdid = "remoteDeviceId"; + std::string remoteUdid="localDeviceId"; + GetAccessControlProfiles(profiles); + DeviceProfileConnector::GetInstance().DeleteDeviceBindLevel(offlineParam, profiles, localUdid, remoteUdid); + EXPECT_EQ(offlineParam.bindType, DEVICE); + + localUdid = "localDeviceId"; + remoteUdid="remoteDeviceId"; + DeviceProfileConnector::GetInstance().DeleteDeviceBindLevel(offlineParam, profiles, localUdid, remoteUdid); + EXPECT_EQ(offlineParam.bindType, DEVICE); +} + +HWTEST_F(DeviceProfileConnectorTest, DeleteServiceBindLevel_001, testing::ext::TestSize.Level0) +{ + DmOfflineParam offlineParam; + std::string pkgName = "bundleName1"; + std::vector profiles; + std::string localUdid = "remoteDeviceId"; + std::string remoteUdid="localDeviceId"; + GetAccessControlProfiles(profiles); + DeviceProfileConnector::GetInstance().DeleteServiceBindLevel(offlineParam, pkgName, profiles, localUdid, remoteUdid); + EXPECT_EQ(offlineParam.bindType, SERVICE); + + pkgName = "bundleName2"; + localUdid = "localDeviceId"; + remoteUdid="remoteDeviceId"; + DeviceProfileConnector::GetInstance().DeleteServiceBindLevel(offlineParam, pkgName, profiles, + localUdid, remoteUdid); + EXPECT_EQ(offlineParam.bindType, SERVICE); +} + +HWTEST_F(DeviceProfileConnectorTest, CheckSrcDevIdInAclForDevBind_004, testing::ext::TestSize.Level0) +{ + std::string pkgName = "bundleName"; + std::string deviceId = "remoteDeviceId"; + bool ret = DeviceProfileConnector::GetInstance().CheckSrcDevIdInAclForDevBind(pkgName, deviceId); + EXPECT_FALSE(ret); +} + +HWTEST_F(DeviceProfileConnectorTest, DeleteTimeOutAcl_002, testing::ext::TestSize.Level0) +{ + std::string deviceId = "remoteDeviceId"; + uint32_t ret = DeviceProfileConnector::GetInstance().DeleteTimeOutAcl(deviceId); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(DeviceProfileConnectorTest, GetTrustNumber_002, testing::ext::TestSize.Level0) +{ + std::string deviceId = "remoteDeviceId"; + int32_t ret = DeviceProfileConnector::GetInstance().GetTrustNumber(deviceId); + EXPECT_NE(ret, DM_OK); +} + +HWTEST_F(DeviceProfileConnectorTest, IsSameAccount_003, testing::ext::TestSize.Level0) +{ + std::string udid = "123456"; + int32_t ret = DeviceProfileConnector::GetInstance().IsSameAccount(udid); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceProfileConnectorTest, CheckAccessControl_001, testing::ext::TestSize.Level0) +{ + DmAccessCaller caller; + std::string srcUdid; + DmAccessCallee callee; + std::string sinkUdid; + int32_t ret = DeviceProfileConnector::GetInstance().CheckAccessControl(caller, srcUdid, callee, sinkUdid); + EXPECT_EQ(ret, ERR_DM_FAILED); + + srcUdid = "123456"; + sinkUdid = "123456"; + ret = DeviceProfileConnector::GetInstance().CheckAccessControl(caller, srcUdid, callee, sinkUdid); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceProfileConnectorTest, CheckIsSameAccount_001, testing::ext::TestSize.Level0) +{ + DmAccessCaller caller; + std::string srcUdid; + DmAccessCallee callee; + std::string sinkUdid; + int32_t ret = DeviceProfileConnector::GetInstance().CheckIsSameAccount(caller, srcUdid, callee, sinkUdid); + EXPECT_EQ(ret, ERR_DM_FAILED); + + srcUdid = "123456"; + sinkUdid = "123456"; + ret = DeviceProfileConnector::GetInstance().CheckIsSameAccount(caller, srcUdid, callee, sinkUdid); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceProfileConnectorTest, HandleAccountLogoutEvent_002, testing::ext::TestSize.Level0) +{ + int32_t remoteUserId = 0; + int32_t bindType = DM_INVALIED_BINDTYPE; + std::string remoteAccountHash = "remoteAccountHash"; + std::string remoteUdid = "123456"; + std::string localUdid = "localDeviceId"; + + bindType = DeviceProfileConnector::GetInstance().HandleAccountLogoutEvent(remoteUserId, + remoteAccountHash, remoteUdid, localUdid); + EXPECT_EQ(bindType, DM_IDENTICAL_ACCOUNT); +} + +HWTEST_F(DeviceProfileConnectorTest, HandleDevUnBindEvent_002, testing::ext::TestSize.Level0) +{ + int32_t remoteUserId = 0; + std::string remoteUdid; + std::string localUdid = "localDeviceId"; + int32_t bindType = DM_INVALIED_BINDTYPE; + bindType = DeviceProfileConnector::GetInstance().HandleDevUnBindEvent(remoteUserId, remoteUdid, localUdid); + EXPECT_EQ(bindType, DM_INVALIED_BINDTYPE); +} + +HWTEST_F(DeviceProfileConnectorTest, GetAllAccessControlProfile_001, testing::ext::TestSize.Level0) +{ + int64_t accessControlId = 1; + DeviceProfileConnector::GetInstance().DeleteAccessControlById(accessControlId); + auto ret = DeviceProfileConnector::GetInstance().GetAllAccessControlProfile(); + EXPECT_TRUE(ret.empty()); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/test/commonunittest/UTTest_dm_deviceprofile_connector_second.cpp b/test/commonunittest/UTTest_dm_deviceprofile_connector_second.cpp new file mode 100644 index 000000000..692c4a701 --- /dev/null +++ b/test/commonunittest/UTTest_dm_deviceprofile_connector_second.cpp @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "UTTest_dm_deviceprofile_connector_second.h" + +#include "dm_constants.h" +#include "deviceprofile_connector.h" +#include + +using namespace testing; +using namespace testing::ext; +namespace OHOS { +namespace DistributedHardware { +void DeviceProfileConnectorSecondTest::SetUp() +{ +} + +void DeviceProfileConnectorSecondTest::TearDown() +{ +} + +void DeviceProfileConnectorSecondTest::SetUpTestCase() +{ + DistributedDeviceProfile::DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient = + distributedDeviceProfileClientMock_; +} + +void DeviceProfileConnectorSecondTest::TearDownTestCase() +{ + distributedDeviceProfileClientMock_ = nullptr; +} + +HWTEST_F(DeviceProfileConnectorSecondTest, GetAccessControlProfile_201, testing::ext::TestSize.Level0) +{ + EXPECT_CALL(*distributedDeviceProfileClientMock_, GetAccessControlProfile(_, _)).WillOnce(Return(ERR_DM_FAILED)); + auto ret = DeviceProfileConnector::GetInstance().GetAccessControlProfile(); + EXPECT_TRUE(ret.empty()); +} + +HWTEST_F(DeviceProfileConnectorSecondTest, GetAccessControlProfileByUserId_201, testing::ext::TestSize.Level0) +{ + int32_t userId = DEVICE; + std::vector profiles; + EXPECT_CALL(*distributedDeviceProfileClientMock_, GetAccessControlProfile(_, _)).WillOnce(Return(ERR_DM_FAILED)); + profiles = DeviceProfileConnector::GetInstance().GetAccessControlProfileByUserId(userId); + EXPECT_TRUE(profiles.empty()); +} + +HWTEST_F(DeviceProfileConnectorSecondTest, CheckAuthForm_201, testing::ext::TestSize.Level0) +{ + DmAuthForm form = DmAuthForm::ACROSS_ACCOUNT; + DistributedDeviceProfile::AccessControlProfile profiles; + profiles.SetBindType(DM_ACROSS_ACCOUNT); + profiles.SetBindLevel(APP); + profiles.accessee_.SetAccesseeBundleName("pkgName"); + profiles.accessee_.SetAccesseeDeviceId("localDeviceId"); + DmDiscoveryInfo discoveryInfo; + discoveryInfo.pkgname = "pkgName"; + discoveryInfo.localDeviceId = "localDeviceId"; + int32_t ret = DeviceProfileConnector::GetInstance().CheckAuthForm(form, profiles, discoveryInfo); + EXPECT_EQ(ret, DmAuthForm::ACROSS_ACCOUNT); + + profiles.accesser_.SetAccesserBundleName("pkgName"); + profiles.accesser_.SetAccesserDeviceId("localDeviceId"); + ret = DeviceProfileConnector::GetInstance().CheckAuthForm(form, profiles, discoveryInfo); + EXPECT_EQ(ret, DmAuthForm::ACROSS_ACCOUNT); + + profiles.SetBindLevel(SERVICE); + ret = DeviceProfileConnector::GetInstance().CheckAuthForm(form, profiles, discoveryInfo); + EXPECT_EQ(ret, DmAuthForm::INVALID_TYPE); + + profiles.SetBindLevel(DEVICE); + ret = DeviceProfileConnector::GetInstance().CheckAuthForm(form, profiles, discoveryInfo); + EXPECT_EQ(ret, DmAuthForm::ACROSS_ACCOUNT); +} + +HWTEST_F(DeviceProfileConnectorSecondTest, PutAccessControlList_201, testing::ext::TestSize.Level0) +{ + DmAclInfo aclInfo; + DmAccesser dmAccesser; + DmAccessee dmAccessee; + EXPECT_CALL(*distributedDeviceProfileClientMock_, PutAccessControlProfile(_)).WillOnce(Return(ERR_DM_FAILED)); + int32_t ret = DeviceProfileConnector::GetInstance().PutAccessControlList(aclInfo, dmAccesser, dmAccessee); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceProfileConnectorSecondTest, CheckIdenticalAccount_201, testing::ext::TestSize.Level0) +{ + int32_t userId = 0; + std::string accountId; + EXPECT_CALL(*distributedDeviceProfileClientMock_, GetAccessControlProfile(_, _)).WillOnce(Return(ERR_DM_FAILED)); + bool ret = DeviceProfileConnector::GetInstance().CheckIdenticalAccount(userId, accountId); + EXPECT_FALSE(ret); +} + +HWTEST_F(DeviceProfileConnectorSecondTest, GetAllAccessControlProfile_201, testing::ext::TestSize.Level0) +{ + EXPECT_CALL(*distributedDeviceProfileClientMock_, GetAccessControlProfile(_, _)).WillOnce(Return(ERR_DM_FAILED)); + auto ret = DeviceProfileConnector::GetInstance().GetAllAccessControlProfile(); + EXPECT_TRUE(ret.empty()); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/commonunittest/UTTest_dm_deviceprofile_connector_second.h b/test/commonunittest/UTTest_dm_deviceprofile_connector_second.h new file mode 100644 index 000000000..7f638ce73 --- /dev/null +++ b/test/commonunittest/UTTest_dm_deviceprofile_connector_second.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 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_DEVICEPROFILE_CONNECTOR_SECOND_H +#define OHOS_UTTEST_DM_DEVICEPROFILE_CONNECTOR_SECOND_H + +#include +#include + +#include "deviceprofile_connector.h" +#include "distributed_device_profile_client_mock.h" +#include "deviceprofile_connector_mock.h" + +namespace OHOS { +namespace DistributedHardware { +class DeviceProfileConnectorSecondTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + + static inline std::shared_ptr + distributedDeviceProfileClientMock_ = + std::make_shared(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_UTTEST_DM_DEVICEPROFILE_CONNECTOR_SECOND_H diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 64571d1ae..f8161dc4d 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -27,6 +27,7 @@ group("unittest") { ":UTTest_device_manager_service", ":UTTest_device_manager_service_impl", ":UTTest_device_manager_service_listener", + ":UTTest_device_manager_service_three", ":UTTest_discovery_filter", ":UTTest_discovery_manager", ":UTTest_dm_account_common_event", @@ -40,6 +41,7 @@ group("unittest") { ":UTTest_dm_crypto", ":UTTest_dm_device_state_manager", ":UTTest_dm_deviceprofile_connector", + ":UTTest_dm_deviceprofile_connector_second", ":UTTest_dm_dfx", ":UTTest_dm_discovery_filter", ":UTTest_dm_discovery_manager", @@ -330,10 +332,7 @@ ohos_unittest("UTTest_device_manager_service") { "mock/softbus_listener_mock.cpp", ] - deps = [ - ":device_manager_test_common", - "//third_party/googletest:gmock_main", - ] + deps = [ ":device_manager_test_common" ] external_deps = [ "access_token:libaccesstoken_sdk", @@ -344,11 +343,42 @@ ohos_unittest("UTTest_device_manager_service") { "device_info_manager:distributed_device_profile_sdk", "ffrt:libffrt", "googletest:gmock", + "googletest:gmock_main", ] } ## UnitTest UTTest_device_manager_service }}} +## UnitTest UTTest_device_manager_service_three {{{ +ohos_unittest("UTTest_device_manager_service_three") { + module_out_path = module_out_path + + sources = [ + "UTTest_device_manager_service_three.cpp", + "mock/device_manager_service_impl_mock.cpp", + "mock/device_manager_service_mock.cpp", + "mock/dm_softbus_cache_mock.cpp", + "mock/permission_manager_mock.cpp", + "mock/softbus_listener_mock.cpp", + ] + + deps = [ ":device_manager_test_common" ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "device_auth:deviceauth_sdk", + "device_info_manager:distributed_device_profile_common", + "device_info_manager:distributed_device_profile_sdk", + "ffrt:libffrt", + "googletest:gmock", + "googletest:gmock_main", + ] +} + +## UnitTest UTTest_device_manager_service_three }}} + ## UnitTest UTTest_hichain_auth_connector {{{ ohos_unittest("UTTest_hichain_auth_connector") { module_out_path = module_out_path @@ -944,10 +974,7 @@ ohos_unittest("UTTest_device_manager_service_impl") { "mock/deviceprofile_connector_mock.cpp", ] - deps = [ - ":device_manager_test_common", - "//third_party/googletest:gmock_main", - ] + deps = [ ":device_manager_test_common" ] external_deps = [ "device_auth:deviceauth_sdk", @@ -955,6 +982,7 @@ ohos_unittest("UTTest_device_manager_service_impl") { "device_info_manager:distributed_device_profile_sdk", "ffrt:libffrt", "googletest:gmock", + "googletest:gmock_main", "os_account:libaccountkits", "os_account:os_account_innerkits", ] @@ -1234,6 +1262,34 @@ ohos_unittest("UTTest_dm_deviceprofile_connector") { ## UTTest_dm_deviceprofile_connector }}} +############################### +## UnitTest UTTest_dm_deviceprofile_connector_second {{{ +ohos_unittest("UTTest_dm_deviceprofile_connector_second") { + module_out_path = module_out_path + + include_dirs = [ + "${devicemanager_path}/commondependency/include", + "${devicemanager_path}/test/commonunittest", + ] + + sources = [ + "${devicemanager_path}/test/commonunittest/UTTest_dm_deviceprofile_connector_second.cpp", + "${devicemanager_path}/test/unittest/mock/distributed_device_profile_client_mock.cpp", + ] + + deps = [ ":device_manager_test_common" ] + + external_deps = [ + "device_info_manager:distributed_device_profile_common", + "device_info_manager:distributed_device_profile_sdk", + "ffrt:libffrt", + "googletest:gmock", + "googletest:gmock_main", + ] +} + +## UTTest_dm_deviceprofile_connector_second }}} + ############################# ## UnitTest UTTest_dm_discovery_manager {{{ diff --git a/test/unittest/UTTest_device_manager_service.cpp b/test/unittest/UTTest_device_manager_service.cpp index 7ced28282..e20e63410 100644 --- a/test/unittest/UTTest_device_manager_service.cpp +++ b/test/unittest/UTTest_device_manager_service.cpp @@ -68,7 +68,6 @@ void DeviceManagerServiceTest::SetUpTestCase() DmPermissionManager::dmPermissionManager = permissionManagerMock_; DmSoftbusListener::dmSoftbusListener = softbusListenerMock_; DmKVAdapterManager::dmKVAdapterManager = kVAdapterManagerMock_; - DmDeviceManagerServiceImpl::dmDeviceManagerServiceImpl = deviceManagerServiceImplMock_; DmAppManager::dmAppManager = appManagerMock_; } @@ -77,7 +76,6 @@ void DeviceManagerServiceTest::TearDownTestCase() permissionManagerMock_ = nullptr; softbusListenerMock_ = nullptr; kVAdapterManagerMock_ = nullptr; - deviceManagerServiceImplMock_ = nullptr; appManagerMock_ = nullptr; } @@ -178,7 +176,7 @@ HWTEST_F(DeviceManagerServiceTest, StopDeviceDiscovery_001, testing::ext::TestSi uint16_t subscribeId = 1; DeviceManagerService::GetInstance().InitDMServiceListener(); int ret = DeviceManagerService::GetInstance().StopDeviceDiscovery(pkgName, subscribeId); - EXPECT_EQ(ret, SOFTBUS_DISCOVER_COAP_STOP_DISCOVER_FAIL); + EXPECT_TRUE(ret == SOFTBUS_DISCOVER_COAP_STOP_DISCOVER_FAIL || ret == SOFTBUS_ERR); DeviceManagerService::GetInstance().UninitDMServiceListener(); } @@ -323,7 +321,7 @@ HWTEST_F(DeviceManagerServiceTest, UnPublishDeviceDiscovery_003, testing::ext::T int32_t publishId = 1; DeviceManagerService::GetInstance().InitDMServiceListener(); int ret = DeviceManagerService::GetInstance().UnPublishDeviceDiscovery(pkgName, publishId); - EXPECT_EQ(ret, SOFTBUS_DISCOVER_COAP_STOP_PUBLISH_FAIL); + EXPECT_TRUE(ret == SOFTBUS_DISCOVER_COAP_STOP_PUBLISH_FAIL || ret == SOFTBUS_ERR); DeviceManagerService::GetInstance().UninitDMServiceListener(); } @@ -406,6 +404,7 @@ HWTEST_F(DeviceManagerServiceTest, GetTrustedDeviceList_003, testing::ext::TestS std::vector deviceList; DeviceManagerService::GetInstance().softbusListener_ = std::make_shared(); DeviceManagerService::GetInstance().InitDMServiceListener(); + EXPECT_CALL(*softbusListenerMock_, GetTrustedDeviceList(_)).WillOnce(Return(DM_OK)); int ret = DeviceManagerService::GetInstance().GetTrustedDeviceList(pkgName, extra, deviceList); EXPECT_EQ(ret, DM_OK); DeviceManagerService::GetInstance().UninitDMServiceListener(); @@ -600,6 +599,8 @@ HWTEST_F(DeviceManagerServiceTest, AuthenticateDevice_003, testing::ext::TestSiz std::string extra = "jdddd"; int32_t authType = 0; std::string deviceId = " 2345"; + EXPECT_CALL(*softbusListenerMock_, + GetTargetInfoFromCache(_, _, _)).WillOnce(Return(ERR_DM_BIND_INPUT_PARA_INVALID)); int32_t ret = DeviceManagerService::GetInstance().AuthenticateDevice(pkgName, authType, deviceId, extra); EXPECT_EQ(ret, ERR_DM_BIND_INPUT_PARA_INVALID); } @@ -676,6 +677,7 @@ HWTEST_F(DeviceManagerServiceTest, UnAuthenticateDevice_004, testing::ext::TestS { std::string pkgName = "com.ohos.test"; std::string networkId = "12345"; + EXPECT_CALL(*softbusListenerMock_, GetUdidByNetworkId(_, _)).WillOnce(Return(ERR_DM_FAILED)); int ret = DeviceManagerService::GetInstance().UnAuthenticateDevice(pkgName, networkId); EXPECT_EQ(ret, ERR_DM_FAILED); } @@ -1970,7 +1972,7 @@ HWTEST_F(DeviceManagerServiceTest, StopAdvertising_004, testing::ext::TestSize.L std::map advertiseParam; DeviceManagerService::GetInstance().InitDMServiceListener(); int32_t ret = DeviceManagerService::GetInstance().StopAdvertising(pkgName, advertiseParam); - EXPECT_EQ(ret, SOFTBUS_DISCOVER_COAP_STOP_PUBLISH_FAIL); + EXPECT_TRUE(ret == SOFTBUS_DISCOVER_COAP_STOP_PUBLISH_FAIL || ret == SOFTBUS_ERR); DeviceManagerService::GetInstance().UninitDMServiceListener(); } @@ -2078,6 +2080,7 @@ HWTEST_F(DeviceManagerServiceTest, CreatePinHolder_002, testing::ext::TestSize.L PeerTargetId targetId; DmPinType pinType = DmPinType::QR_CODE; std::string payload; + EXPECT_CALL(*permissionManagerMock_, CheckProcessNameValidOnPinHolder(_)).WillOnce(Return(false)); int32_t ret = DeviceManagerService::GetInstance().CreatePinHolder(pkgName, targetId, pinType, payload); EXPECT_NE(ret, DM_OK); } @@ -2099,6 +2102,7 @@ HWTEST_F(DeviceManagerServiceTest, DestroyPinHolder_002, testing::ext::TestSize. PeerTargetId targetId; DmPinType pinType = DmPinType::QR_CODE; std::string payload; + EXPECT_CALL(*permissionManagerMock_, CheckProcessNameValidOnPinHolder(_)).WillOnce(Return(false)); int32_t ret = DeviceManagerService::GetInstance().DestroyPinHolder(pkgName, targetId, pinType, payload); EXPECT_NE(ret, DM_OK); } @@ -2277,6 +2281,7 @@ HWTEST_F(DeviceManagerServiceTest, DestroyPinHolder_003, testing::ext::TestSize. PeerTargetId targetId; DmPinType pinType = DmPinType::QR_CODE; std::string payload; + EXPECT_CALL(*permissionManagerMock_, CheckProcessNameValidOnPinHolder(_)).WillOnce(Return(false)); int32_t ret = DeviceManagerService::GetInstance().DestroyPinHolder(pkgName, targetId, pinType, payload); EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); } @@ -2468,6 +2473,8 @@ HWTEST_F(DeviceManagerServiceTest, StopAuthenticateDevice_002, testing::ext::Tes HWTEST_F(DeviceManagerServiceTest, StopAuthenticateDevice_003, testing::ext::TestSize.Level0) { std::string pkgName = "pkgName_003"; + DeviceManagerService::GetInstance().isImplsoLoaded_ = false; + DeviceManagerService::GetInstance().IsDMServiceImplReady(); int32_t ret = DeviceManagerService::GetInstance().StopAuthenticateDevice(pkgName); EXPECT_EQ(ret, DM_OK); } @@ -2610,9 +2617,9 @@ HWTEST_F(DeviceManagerServiceTest, GetDeviceInfo_005, testing::ext::TestSize.Lev std::string networkId = "networkIdTest5"; DmDeviceInfo info; DeviceManagerService::GetInstance().softbusListener_ = std::make_shared(); - EXPECT_CALL(*softbusListenerMock_, GetDeviceInfo(_, _)).WillOnce(Return(DM_OK)); + EXPECT_CALL(*softbusListenerMock_, GetDeviceInfo(_, _)).WillOnce(Return(ERR_DM_FAILED)); int32_t ret = DeviceManagerService::GetInstance().GetDeviceInfo(networkId, info); - EXPECT_EQ(ret, DM_OK); + EXPECT_EQ(ret, ERR_DM_FAILED); DeviceManagerService::GetInstance().softbusListener_ = nullptr; } @@ -2630,6 +2637,50 @@ HWTEST_F(DeviceManagerServiceTest, GetEncryptedUuidByNetworkId_004, testing::ext EXPECT_EQ(ret, DM_OK); } +/** + * @tc.name: UnAuthenticateDevice_005 + * @tc.desc: Set intFlag for UnAuthenticateDevice to true and pkgName to com.ohos.test; set deviceId null ,The return + * value is SOFTBUS_IPC_ERR + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, UnAuthenticateDevice_005, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.test"; + std::string networkId = "12345"; + EXPECT_CALL(*softbusListenerMock_, GetUdidByNetworkId(_, _)).WillOnce(Return(DM_OK)); + int ret = DeviceManagerService::GetInstance().UnAuthenticateDevice(pkgName, networkId); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceTest, BindDevice_005, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.test"; + int32_t authType = 1; + std::string deviceId = "1234"; + std::string bindParam; + EXPECT_CALL(*softbusListenerMock_, GetTargetInfoFromCache(_, _, _)).WillOnce(Return(DM_OK)); + int32_t ret = DeviceManagerService::GetInstance().BindDevice(pkgName, authType, deviceId, bindParam); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: AuthenticateDevice_005 + * @tc.desc: Set intFlag for GAuthenticateDevice to True and deviceId to null; Return ERR_DM_BIND_INPUT_PARA_INVALID + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, AuthenticateDevice_005, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.test"; + std::string extra = "jdddd"; + int32_t authType = 0; + std::string deviceId = " 2345"; + EXPECT_CALL(*softbusListenerMock_, GetTargetInfoFromCache(_, _, _)).WillOnce(Return(DM_OK)); + int32_t ret = DeviceManagerService::GetInstance().AuthenticateDevice(pkgName, authType, deviceId, extra); + EXPECT_EQ(ret, DM_OK); +} + #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) HWTEST_F(DeviceManagerServiceTest, ConvertUdidHashToAnoyDeviceId_001, testing::ext::TestSize.Level0) { @@ -2655,6 +2706,93 @@ HWTEST_F(DeviceManagerServiceTest, GetUdidHashByAnoyDeviceId_001, testing::ext:: EXPECT_EQ(ret, ERR_DM_FAILED); } #endif + +/** + * @tc.name: GetTrustedDeviceList_004 + * @tc.desc:Set the intFlag of GetTrustedDeviceList_004 to true; Return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, GetTrustedDeviceList_004, testing::ext::TestSize.Level0) +{ + std::string pkgName = "ohos_test"; + std::string extra = "jdddd"; + std::vector deviceList; + DeviceManagerService::GetInstance().softbusListener_ = std::make_shared(); + DeviceManagerService::GetInstance().InitDMServiceListener(); + EXPECT_CALL(*softbusListenerMock_, GetTrustedDeviceList(_)).WillOnce(Return(ERR_DM_FAILED)); + int ret = DeviceManagerService::GetInstance().GetTrustedDeviceList(pkgName, extra, deviceList); + EXPECT_EQ(ret, ERR_DM_FAILED); + DeviceManagerService::GetInstance().UninitDMServiceListener(); + DeviceManagerService::GetInstance().softbusListener_ = nullptr; +} + +/** + * @tc.name: GetLocalDeviceInfo_003 + * @tc.desc: The return value is ERR_DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, GetLocalDeviceInfo_003, testing::ext::TestSize.Level0) +{ + DmDeviceInfo info; + DeviceManagerService::GetInstance().softbusListener_ = std::make_shared(); + EXPECT_CALL(*softbusListenerMock_, GetTrustedDeviceList(_)).WillOnce(Return(ERR_DM_FAILED)); + int32_t ret = DeviceManagerService::GetInstance().GetLocalDeviceInfo(info); + EXPECT_EQ(ret, ERR_DM_FAILED); + DeviceManagerService::GetInstance().softbusListener_ = nullptr; +} + +/** + * @tc.name: GetLocalDeviceInfo_004 + * @tc.desc: The return value is ERR_DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, GetLocalDeviceInfo_004, testing::ext::TestSize.Level0) +{ + DmDeviceInfo info; + DeletePermission(); + DeviceManagerService::GetInstance().softbusListener_ = std::make_shared(); + EXPECT_CALL(*softbusListenerMock_, GetTrustedDeviceList(_)).WillOnce(Return(DM_OK)); + int32_t ret = DeviceManagerService::GetInstance().GetLocalDeviceInfo(info); + EXPECT_EQ(ret, DM_OK); + DeviceManagerService::GetInstance().softbusListener_ = nullptr; +} + +HWTEST_F(DeviceManagerServiceTest, BindTarget_007, testing::ext::TestSize.Level0) +{ + std::string pkgName = "pkgName"; + PeerTargetId targetId; + targetId.wifiIp = "wifi_Ip"; + std::map bindParam; + std::string key = PARAM_KEY_TARGET_ID; + std::string value = "186"; + bindParam.insert(std::make_pair(key, value)); + int32_t ret = DeviceManagerService::GetInstance().BindTarget(pkgName, targetId, bindParam); + EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); +} + +HWTEST_F(DeviceManagerServiceTest, SetLocalDeviceName_001, testing::ext::TestSize.Level0) +{ + std::string localDeviceName; + int32_t ret = DeviceManagerService::GetInstance().SetLocalDeviceName(localDeviceName); + EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); + + localDeviceName = "localDeviceName_001"; + DeviceManagerService::GetInstance().softbusListener_ = nullptr; + ret = DeviceManagerService::GetInstance().SetLocalDeviceName(localDeviceName); + EXPECT_EQ(ret, ERR_DM_POINT_NULL); + + std::string deviceList; + uint16_t deviceTypeId = 0; + int32_t errcode = 1; + DeviceManagerService::GetInstance().HandleCredentialAuthStatus(deviceList, deviceTypeId, errcode); + DeviceManagerService::GetInstance().softbusListener_ = std::make_shared(); + ret = DeviceManagerService::GetInstance().SetLocalDeviceName(localDeviceName); + EXPECT_EQ(ret, DM_OK); + DeviceManagerService::GetInstance().softbusListener_ = nullptr; +} } // namespace } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/UTTest_device_manager_service.h b/test/unittest/UTTest_device_manager_service.h index a7c550863..00de35736 100644 --- a/test/unittest/UTTest_device_manager_service.h +++ b/test/unittest/UTTest_device_manager_service.h @@ -31,7 +31,7 @@ #include "softbus_listener_mock.h" #include "app_manager_mock.h" #include "kv_adapter_manager_mock.h" -#include "device_manager_service_impl_mock.h" + namespace OHOS { namespace DistributedHardware { class DeviceManagerServiceTest : public testing::Test { @@ -49,8 +49,6 @@ public: std::make_shared(); static inline std::shared_ptr kVAdapterManagerMock_ = std::make_shared(); - static inline std::shared_ptr deviceManagerServiceImplMock_ = - std::make_shared(); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/UTTest_device_manager_service_impl.cpp b/test/unittest/UTTest_device_manager_service_impl.cpp index bdf8cfd33..86723fb07 100644 --- a/test/unittest/UTTest_device_manager_service_impl.cpp +++ b/test/unittest/UTTest_device_manager_service_impl.cpp @@ -348,6 +348,11 @@ HWTEST_F(DeviceManagerServiceImplTest, NotifyEvent_005, testing::ext::TestSize.L int32_t remoteUserId = 1; std::string remoteAccountHash = "45552878"; std::string remoteUdid = "ajdakndkwj98877"; + EXPECT_CALL(*deviceProfileConnectorMock_, + HandleAccountLogoutEvent(_, _, _, _)).WillOnce(Return(DM_INVALIED_BINDTYPE)); + deviceManagerServiceImpl_->HandleAccountLogoutEvent(remoteUserId, remoteAccountHash, remoteUdid); + EXPECT_CALL(*deviceProfileConnectorMock_, + HandleAccountLogoutEvent(_, _, _, _)).WillOnce(Return(DM_IDENTICAL_ACCOUNT)); deviceManagerServiceImpl_->HandleAccountLogoutEvent(remoteUserId, remoteAccountHash, remoteUdid); int ret = deviceManagerServiceImpl_->NotifyEvent(pkgName, eventId, event); EXPECT_EQ(ret, DM_OK); @@ -1523,6 +1528,9 @@ HWTEST_F(DeviceManagerServiceImplTest, GetBindLevel_101, testing::ext::TestSize. int32_t tokenId2 = 123; int32_t remoteUserId = 100; int32_t ret = deviceManagerServiceImpl_->GetBindLevel(pkgName, localUdid, udid, tokenId); + EXPECT_CALL(*deviceProfileConnectorMock_, HandleAppUnBindEvent(_, _, _, _)).WillOnce(Return("")); + deviceManagerServiceImpl_->HandleAppUnBindEvent(remoteUserId, localUdid, tokenId2); + EXPECT_CALL(*deviceProfileConnectorMock_, HandleAppUnBindEvent(_, _, _, _)).WillOnce(Return(udid)); deviceManagerServiceImpl_->HandleAppUnBindEvent(remoteUserId, localUdid, tokenId2); EXPECT_EQ(ret, INVALIED_TYPE); } @@ -1533,6 +1541,9 @@ HWTEST_F(DeviceManagerServiceImplTest, GetDeviceIdAndBindType_101, testing::ext: const std::string localUdid = "234"; int32_t userId = 100; std::map temp = deviceManagerServiceImpl_->GetDeviceIdAndBindType(userId, accountId); + EXPECT_CALL(*deviceProfileConnectorMock_, HandleDevUnBindEvent(_, _, _)).WillOnce(Return(DM_INVALIED_BINDTYPE)); + deviceManagerServiceImpl_->HandleDevUnBindEvent(userId, localUdid); + EXPECT_CALL(*deviceProfileConnectorMock_, HandleDevUnBindEvent(_, _, _)).WillOnce(Return(DM_IDENTICAL_ACCOUNT)); deviceManagerServiceImpl_->HandleDevUnBindEvent(userId, localUdid); EXPECT_EQ(temp.empty(), true); } @@ -1599,6 +1610,8 @@ HWTEST_F(DeviceManagerServiceImplTest, ProcessAppUnintall_103, testing::ext::Tes std::string appId; int32_t accessTokenId = 1001; std::vector profiles; + DmDeviceInfo devInfo; + deviceManagerServiceImpl_->HandleDeviceScreenStatusChange(devInfo); AddAccessControlProfileFirst(profiles); EXPECT_CALL(*deviceProfileConnectorMock_, GetAllAccessControlProfile()).WillOnce(Return(profiles)); if (deviceManagerServiceImpl_->hiChainConnector_ == nullptr) { diff --git a/test/unittest/UTTest_device_manager_service_three.cpp b/test/unittest/UTTest_device_manager_service_three.cpp new file mode 100644 index 000000000..4004a2948 --- /dev/null +++ b/test/unittest/UTTest_device_manager_service_three.cpp @@ -0,0 +1,380 @@ +/* + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "UTTest_device_manager_service_three.h" + +#include "accesstoken_kit.h" +#include "dm_constants.h" +#include "dm_device_info.h" +#include "dm_log.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" +#include "softbus_common.h" +#include "softbus_error_code.h" + +using namespace OHOS::Security::AccessToken; +using namespace testing; +using namespace testing::ext; +namespace OHOS { +namespace DistributedHardware { + +void DeviceManagerServiceThreeTest::SetUp() +{ + const int32_t permsNum = 4; + const int32_t indexZero = 0; + const int32_t indexOne = 1; + const int32_t indexTwo = 2; + const int32_t indexThree = 3; + uint64_t tokenId; + const char *perms[permsNum]; + perms[indexZero] = "ohos.permission.DISTRIBUTED_SOFTBUS_CENTER"; + perms[indexOne] = "ohos.permission.DISTRIBUTED_DATASYNC"; + perms[indexTwo] = "ohos.permission.ACCESS_SERVICE_DM"; + perms[indexThree] = "ohos.permission.MONITOR_DEVICE_NETWORK_STATE"; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = permsNum, + .aclsNum = 0, + .dcaps = NULL, + .perms = perms, + .acls = NULL, + .processName = "dsoftbus_service", + .aplStr = "system_core", + }; + tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); +} + +void DeviceManagerServiceThreeTest::TearDown() +{ +} + +void DeviceManagerServiceThreeTest::SetUpTestCase() +{ + DmDeviceManagerService::dmDeviceManagerService = deviceManagerServiceMock_; + DmPermissionManager::dmPermissionManager = permissionManagerMock_; + DmSoftbusListener::dmSoftbusListener = softbusListenerMock_; + DmSoftbusCache::dmSoftbusCache = softbusCacheMock_; + DmDeviceManagerServiceImpl::dmDeviceManagerServiceImpl = deviceManagerServiceImplMock_; +} + +void DeviceManagerServiceThreeTest::TearDownTestCase() +{ + deviceManagerServiceMock_ = nullptr; + permissionManagerMock_ = nullptr; + softbusListenerMock_ = nullptr; + softbusCacheMock_ = nullptr; + deviceManagerServiceImplMock_ = nullptr; +} + +namespace { + +/** + * @tc.name: AuthenticateDevice_301 + * @tc.desc: Set unsupport authType = 0 and return ERR_DM_NOT_INIT + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceThreeTest, AuthenticateDevice_301, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.test"; + std::string extra = "jdddd"; + int32_t authType = 1; + std::string deviceId = "deviceId"; + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + int32_t ret = DeviceManagerService::GetInstance().AuthenticateDevice(pkgName, authType, deviceId, extra); + EXPECT_EQ(ret, ERR_DM_NOT_INIT); +} + +/** + * @tc.name: UnAuthenticateDevice_301 + * @tc.desc: Set intFlag for UnAuthenticateDevice to true and pkgName to com.ohos.test; set deviceId null ,The return + * value is SOFTBUS_IPC_ERR + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceThreeTest, UnAuthenticateDevice_301, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.test"; + std::string networkId = "12345"; + EXPECT_CALL(*softbusListenerMock_, GetUdidByNetworkId(_, _)).WillOnce(Return(DM_OK)); + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + int ret = DeviceManagerService::GetInstance().UnAuthenticateDevice(pkgName, networkId); + EXPECT_EQ(ret, ERR_DM_NOT_INIT); +} + +/** + * @tc.name: SetUserOperation_301 + * @tc.desc: Make pkgName empty for SetUserOperation,The return value is + * DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceThreeTest, SetUserOperation_301, testing::ext::TestSize.Level0) +{ + std::string pkgName = "pkgName"; + int32_t action = 0; + const std::string param = "extra"; + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + int ret = DeviceManagerService::GetInstance().SetUserOperation(pkgName, action, param); + EXPECT_EQ(ret, ERR_DM_NOT_INIT); +} + +/** + * @tc.name: RequestCredential_301 + * @tc.desc:The return value is ERR_DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceThreeTest, RequestCredential_301, testing::ext::TestSize.Level0) +{ + const std::string reqJsonStr = "test"; + std::string returnJsonStr = "returntest"; + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + int32_t ret = DeviceManagerService::GetInstance().RequestCredential(reqJsonStr, returnJsonStr); + EXPECT_EQ(ret, ERR_DM_NOT_INIT); +} + +/** + * @tc.name: ImportCredential_301 + * @tc.desc:The return value is ERR_DM_NOT_INIT + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceThreeTest, ImportCredential_301, testing::ext::TestSize.Level0) +{ + const std::string pkgName = "pkgNametest"; + const std::string credentialInfo = "credentialInfotest"; + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + int32_t ret = DeviceManagerService::GetInstance().ImportCredential(pkgName, credentialInfo); + EXPECT_EQ(ret, ERR_DM_NOT_INIT); +} + +/** + * @tc.name: DeleteCredential_301 + * @tc.desc:The return value is ERR_DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceThreeTest, DeleteCredential_301, testing::ext::TestSize.Level0) +{ + const std::string pkgName = "pkgNametest"; + const std::string deleteInfo = "deleteInfotest"; + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + int32_t ret = DeviceManagerService::GetInstance().DeleteCredential(pkgName, deleteInfo); + EXPECT_EQ(ret, ERR_DM_NOT_INIT); +} + +/** + * @tc.name: RegisterCredentialCallback_301 + * @tc.desc: The return value is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceThreeTest, RegisterCredentialCallback_301, testing::ext::TestSize.Level0) +{ + const std::string pkgName = "pkgNametest"; + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + int32_t ret = DeviceManagerService::GetInstance().RegisterCredentialCallback(pkgName); + EXPECT_EQ(ret, ERR_DM_NOT_INIT); +} + +HWTEST_F(DeviceManagerServiceThreeTest, BindDevice_301, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.test"; + int32_t authType = 1; + std::string deviceId = "1234"; + std::string bindParam; + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + int32_t ret = DeviceManagerService::GetInstance().BindDevice(pkgName, authType, deviceId, bindParam); + EXPECT_EQ(ret, ERR_DM_NOT_INIT); +} + +HWTEST_F(DeviceManagerServiceThreeTest, UnBindDevice_301, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.test"; + std::string deviceId = "1234"; + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + int32_t ret = DeviceManagerService::GetInstance().UnBindDevice(pkgName, deviceId); + EXPECT_EQ(ret, ERR_DM_NOT_INIT); +} + +HWTEST_F(DeviceManagerServiceThreeTest, MineRequestCredential_301, testing::ext::TestSize.Level0) +{ + DeviceManagerService::GetInstance().isImplsoLoaded_ = false; + std::string pkgName; + std::string returnJsonStr; + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + int32_t ret = DeviceManagerService::GetInstance().MineRequestCredential(pkgName, returnJsonStr); + EXPECT_EQ(ret, ERR_DM_NOT_INIT); +} + +HWTEST_F(DeviceManagerServiceThreeTest, CheckCredential_301, testing::ext::TestSize.Level0) +{ + std::string pkgName; + std::string returnJsonStr; + std::string reqJsonStr; + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + int32_t ret = DeviceManagerService::GetInstance().CheckCredential(pkgName, reqJsonStr, returnJsonStr); + EXPECT_EQ(ret, ERR_DM_NOT_INIT); +} + +HWTEST_F(DeviceManagerServiceThreeTest, RegisterUiStateCallback_301, testing::ext::TestSize.Level0) +{ + std::string pkgName = "pkgName"; + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + int32_t ret = DeviceManagerService::GetInstance().RegisterUiStateCallback(pkgName); + EXPECT_EQ(ret, ERR_DM_NOT_INIT); +} + +HWTEST_F(DeviceManagerServiceThreeTest, UnRegisterUiStateCallback_301, testing::ext::TestSize.Level0) +{ + std::string pkgName = "pkgName"; + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + int32_t ret = DeviceManagerService::GetInstance().UnRegisterUiStateCallback(pkgName); + EXPECT_EQ(ret, ERR_DM_NOT_INIT); +} + +HWTEST_F(DeviceManagerServiceThreeTest, NotifyEvent_301, testing::ext::TestSize.Level0) +{ + std::string pkgName; + int32_t eventId = 0; + std::string event; + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + int32_t ret = DeviceManagerService::GetInstance().NotifyEvent(pkgName, eventId, event); + EXPECT_NE(ret, ERR_DM_NOT_INIT); +} + +HWTEST_F(DeviceManagerServiceThreeTest, BindTarget_301, testing::ext::TestSize.Level0) +{ + std::string pkgName = "pkgName"; + PeerTargetId targetId; + std::map bindParam; + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + int32_t ret = DeviceManagerService::GetInstance().BindTarget(pkgName, targetId, bindParam); + EXPECT_EQ(ret, ERR_DM_NOT_INIT); +} + +HWTEST_F(DeviceManagerServiceThreeTest, DpAclAdd_301, testing::ext::TestSize.Level0) +{ + std::string udid = "udid"; + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + int32_t ret = DeviceManagerService::GetInstance().DpAclAdd(udid); + EXPECT_EQ(ret, ERR_DM_NOT_INIT); +} + +HWTEST_F(DeviceManagerServiceThreeTest, IsSameAccount_301, testing::ext::TestSize.Level0) +{ + std::string udid = "udidTest"; + EXPECT_CALL(*softbusListenerMock_, GetUdidByNetworkId(_, _)).WillOnce(Return(DM_OK)); + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + int32_t ret = DeviceManagerService::GetInstance().IsSameAccount(udid); + EXPECT_EQ(ret, ERR_DM_NOT_INIT); +} + +HWTEST_F(DeviceManagerServiceThreeTest, CheckIsSameAccount_301, testing::ext::TestSize.Level0) +{ + DmAccessCaller caller; + DmAccessCallee callee; + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + bool ret = DeviceManagerService::GetInstance().CheckIsSameAccount(caller, callee); + EXPECT_FALSE(ret); +} + +HWTEST_F(DeviceManagerServiceThreeTest, CheckAccessControl_301, testing::ext::TestSize.Level0) +{ + DmAccessCaller caller; + DmAccessCallee callee; + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + bool ret = DeviceManagerService::GetInstance().CheckAccessControl(caller, callee); + EXPECT_FALSE(ret); +} + +HWTEST_F(DeviceManagerServiceThreeTest, StopAuthenticateDevice_301, testing::ext::TestSize.Level0) +{ + std::string pkgName = "pkgName_003"; + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + int32_t ret = DeviceManagerService::GetInstance().StopAuthenticateDevice(pkgName); + EXPECT_EQ(ret, ERR_DM_NOT_INIT); +} + +HWTEST_F(DeviceManagerServiceThreeTest, ImportAuthCode_301, testing::ext::TestSize.Level0) +{ + std::string pkgName = "pkgName"; + std::string authCode = "authCode"; + EXPECT_CALL(*permissionManagerMock_, CheckProcessNameValidOnAuthCode(_)).WillOnce(Return(true)); + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + int32_t ret = DeviceManagerService::GetInstance().ImportAuthCode(pkgName, authCode); + EXPECT_EQ(ret, ERR_DM_NOT_INIT); +} + +HWTEST_F(DeviceManagerServiceThreeTest, ExportAuthCode_301, testing::ext::TestSize.Level0) +{ + std::string authCode = "authCode"; + EXPECT_CALL(*permissionManagerMock_, CheckProcessNameValidOnAuthCode(_)).WillOnce(Return(true)); + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + int32_t ret = DeviceManagerService::GetInstance().ExportAuthCode(authCode); + EXPECT_EQ(ret, ERR_DM_NOT_INIT); +} + +HWTEST_F(DeviceManagerServiceThreeTest, UnBindDevice_302, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.test"; + std::string deviceId = "1234"; + EXPECT_CALL(*softbusCacheMock_, GetUdidByUdidHash(_, _)).WillOnce(Return(DM_OK)); + EXPECT_CALL(*deviceManagerServiceImplMock_, GetBindLevel(_, _, _, _)).WillOnce(Return(0)); + int32_t ret = DeviceManagerService::GetInstance().UnBindDevice(pkgName, deviceId); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DeviceManagerServiceThreeTest, UnBindDevice_303, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.test"; + std::string deviceId = "1234"; + EXPECT_CALL(*softbusCacheMock_, GetUdidByUdidHash(_, _)).WillOnce(Return(DM_OK)); + EXPECT_CALL(*deviceManagerServiceImplMock_, GetBindLevel(_, _, _, _)).WillOnce(Return(DM_IDENTICAL_ACCOUNT)); + EXPECT_CALL(*deviceManagerServiceImplMock_, UnBindDevice(_, _, _)).WillOnce(Return(ERR_DM_INPUT_PARA_INVALID)); + int32_t ret = DeviceManagerService::GetInstance().UnBindDevice(pkgName, deviceId); + EXPECT_EQ(ret, ERR_DM_FAILED); + + EXPECT_CALL(*softbusCacheMock_, GetUdidByUdidHash(_, _)).WillOnce(Return(DM_OK)); + EXPECT_CALL(*deviceManagerServiceImplMock_, GetBindLevel(_, _, _, _)).WillOnce(Return(DM_IDENTICAL_ACCOUNT)); + EXPECT_CALL(*deviceManagerServiceImplMock_, UnBindDevice(_, _, _)).WillOnce(Return(DM_OK)); + ret = DeviceManagerService::GetInstance().UnBindDevice(pkgName, deviceId); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DeviceManagerServiceThreeTest, StopAuthenticateDevice_302, testing::ext::TestSize.Level0) +{ + std::string pkgName = "pkgName_004"; + std::string msg = R"( + { + "type" : 0, + "userId" : 123, + "accountId" : "28776", + "tokenId" : 16, + "peerUdid" : "104", + "accountName" : "account_xiao" + })"; + EXPECT_CALL(*deviceManagerServiceMock_, IsDMServiceImplReady()).WillOnce(Return(false)); + DeviceManagerService::GetInstance().HandleDeviceTrustedChange(msg); + EXPECT_CALL(*deviceManagerServiceImplMock_, StopAuthenticateDevice(_)).WillOnce(Return(ERR_DM_FAILED)); + int32_t ret = DeviceManagerService::GetInstance().StopAuthenticateDevice(pkgName); + EXPECT_EQ(ret, ERR_DM_FAILED); +} +} // namespace +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/UTTest_device_manager_service_three.h b/test/unittest/UTTest_device_manager_service_three.h new file mode 100644 index 000000000..243286b3b --- /dev/null +++ b/test/unittest/UTTest_device_manager_service_three.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef OHOS_DEVICE_MANAGER_SERVICE_THREE_H +#define OHOS_DEVICE_MANAGER_SERVICE_THREE_H + +#include +#include +#include + +#include +#include + +#include "device_manager_service.h" +#include "device_manager_service_listener.h" +#include "device_manager_service_impl.h" +#include "dm_single_instance.h" +#include "common_event_support.h" +#include "permission_manager_mock.h" +#include "softbus_listener_mock.h" +#include "device_manager_service_mock.h" +#include "dm_softbus_cache_mock.h" +#include "device_manager_service_impl_mock.h" + +namespace OHOS { +namespace DistributedHardware { +class DeviceManagerServiceThreeTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + + static inline std::shared_ptr deviceManagerServiceMock_ = + std::make_shared(); + static inline std::shared_ptr permissionManagerMock_ = + std::make_shared(); + static inline std::shared_ptr softbusListenerMock_ = + std::make_shared(); + static inline std::shared_ptr softbusCacheMock_ = + std::make_shared(); + static inline std::shared_ptr deviceManagerServiceImplMock_ = + std::make_shared(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/test/unittest/UTTest_device_manager_service_two.cpp b/test/unittest/UTTest_device_manager_service_two.cpp index d181708db..74a978d83 100644 --- a/test/unittest/UTTest_device_manager_service_two.cpp +++ b/test/unittest/UTTest_device_manager_service_two.cpp @@ -223,6 +223,8 @@ HWTEST_F(DeviceManagerServiceTest, CheckAccessControl_201, testing::ext::TestSiz { DmAccessCaller caller; DmAccessCallee callee; + EXPECT_CALL(*softbusListenerMock_, GetUuidByNetworkId(_, _)).WillOnce(Return(ERR_DM_FAILED)); + EXPECT_CALL(*softbusListenerMock_, GetUuidByNetworkId(_, _)).WillOnce(Return(ERR_DM_FAILED)); bool ret = DeviceManagerService::GetInstance().CheckAccessControl(caller, callee); EXPECT_EQ(ret, true); } diff --git a/test/unittest/mock/device_manager_service_impl_mock.cpp b/test/unittest/mock/device_manager_service_impl_mock.cpp index 58f43f8a3..50ca2b918 100644 --- a/test/unittest/mock/device_manager_service_impl_mock.cpp +++ b/test/unittest/mock/device_manager_service_impl_mock.cpp @@ -23,5 +23,17 @@ int32_t DeviceManagerServiceImpl::StopAuthenticateDevice(const std::string &pkgN { return DmDeviceManagerServiceImpl::dmDeviceManagerServiceImpl->StopAuthenticateDevice(pkgName); } + +int32_t DeviceManagerServiceImpl::GetBindLevel(const std::string &pkgName, const std::string &localUdid, + const std::string &udid, uint64_t &tokenId) +{ + return DmDeviceManagerServiceImpl::dmDeviceManagerServiceImpl->GetBindLevel(pkgName, localUdid, udid, tokenId); +} + +int32_t DeviceManagerServiceImpl::UnBindDevice(const std::string &pkgName, const std::string &udid, + int32_t bindLevel) +{ + return DmDeviceManagerServiceImpl::dmDeviceManagerServiceImpl->UnBindDevice(pkgName, udid, bindLevel); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/device_manager_service_impl_mock.h b/test/unittest/mock/device_manager_service_impl_mock.h index 33da0f28d..2079452f7 100644 --- a/test/unittest/mock/device_manager_service_impl_mock.h +++ b/test/unittest/mock/device_manager_service_impl_mock.h @@ -27,6 +27,9 @@ public: virtual ~DmDeviceManagerServiceImpl() = default; public: virtual int32_t StopAuthenticateDevice(const std::string &pkgName) = 0; + virtual int32_t GetBindLevel(const std::string &pkgName, const std::string &localUdid, + const std::string &udid, uint64_t &tokenId) = 0; + virtual int32_t UnBindDevice(const std::string &pkgName, const std::string &udid, int32_t bindLevel) = 0; public: static inline std::shared_ptr dmDeviceManagerServiceImpl = nullptr; }; @@ -34,6 +37,8 @@ public: class DeviceManagerServiceImplMock : public DmDeviceManagerServiceImpl { public: MOCK_METHOD(int32_t, StopAuthenticateDevice, (const std::string &)); + MOCK_METHOD(int32_t, GetBindLevel, (const std::string &, const std::string &, const std::string &, uint64_t &)); + MOCK_METHOD(int32_t, UnBindDevice, (const std::string &, const std::string &, int32_t)); }; } } diff --git a/test/unittest/mock/device_manager_service_mock.cpp b/test/unittest/mock/device_manager_service_mock.cpp new file mode 100644 index 000000000..77b82c325 --- /dev/null +++ b/test/unittest/mock/device_manager_service_mock.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 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 "device_manager_service_mock.h" + +#include "gtest/gtest.h" + +namespace OHOS { +namespace DistributedHardware { +DM_IMPLEMENT_SINGLE_INSTANCE(DeviceManagerService); +bool DeviceManagerService::IsDMServiceImplReady() +{ + return DmDeviceManagerService::dmDeviceManagerService->IsDMServiceImplReady(); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/device_manager_service_mock.h b/test/unittest/mock/device_manager_service_mock.h new file mode 100644 index 000000000..29d3b9bcc --- /dev/null +++ b/test/unittest/mock/device_manager_service_mock.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef OHOS_DEVICE_MANAGER_SERVICE_MOCK_H +#define OHOS_DEVICE_MANAGER_SERVICE_MOCK_H + +#include +#include + +#include "device_manager_service.h" + +namespace OHOS { +namespace DistributedHardware { +class DmDeviceManagerService { +public: + virtual ~DmDeviceManagerService() = default; +public: + virtual bool IsDMServiceImplReady() = 0; +public: + static inline std::shared_ptr dmDeviceManagerService = nullptr; +}; + +class DeviceManagerServiceMock : public DmDeviceManagerService { +public: + MOCK_METHOD(bool, IsDMServiceImplReady, ()); +}; +} +} +#endif diff --git a/test/unittest/mock/deviceprofile_connector_mock.cpp b/test/unittest/mock/deviceprofile_connector_mock.cpp index 7aa1a95da..dc8a99601 100644 --- a/test/unittest/mock/deviceprofile_connector_mock.cpp +++ b/test/unittest/mock/deviceprofile_connector_mock.cpp @@ -24,5 +24,26 @@ std::vector DeviceProfileConnect { return DmDeviceProfileConnector::dmDeviceProfileConnector->GetAllAccessControlProfile(); } + +int32_t DeviceProfileConnector::HandleDevUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid, + const std::string &localUdid) +{ + return DmDeviceProfileConnector::dmDeviceProfileConnector->HandleDevUnBindEvent(remoteUserId, remoteUdid, + localUdid); +} + +std::string DeviceProfileConnector::HandleAppUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid, + int32_t tokenId, const std::string &localUdid) +{ + return DmDeviceProfileConnector::dmDeviceProfileConnector->HandleAppUnBindEvent(remoteUserId, remoteUdid, tokenId, + localUdid); +} + +int32_t DeviceProfileConnector::HandleAccountLogoutEvent(int32_t remoteUserId, const std::string &remoteAccountHash, + const std::string &remoteUdid, const std::string &localUdid) +{ + return DmDeviceProfileConnector::dmDeviceProfileConnector->HandleAccountLogoutEvent(remoteUserId, remoteAccountHash, + remoteUdid, localUdid); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/deviceprofile_connector_mock.h b/test/unittest/mock/deviceprofile_connector_mock.h index d2356d6ff..17464a708 100644 --- a/test/unittest/mock/deviceprofile_connector_mock.h +++ b/test/unittest/mock/deviceprofile_connector_mock.h @@ -27,6 +27,12 @@ public: virtual ~DmDeviceProfileConnector() = default; public: virtual std::vector GetAllAccessControlProfile() = 0; + virtual int32_t HandleDevUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid, + const std::string &localUdid) = 0; + virtual std::string HandleAppUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid, int32_t tokenId, + const std::string &localUdid) = 0; + virtual int32_t HandleAccountLogoutEvent(int32_t remoteUserId, const std::string &remoteAccountHash, + const std::string &remoteUdid, const std::string &localUdid) = 0; public: static inline std::shared_ptr dmDeviceProfileConnector = nullptr; }; @@ -34,6 +40,10 @@ public: class DeviceProfileConnectorMock : public DmDeviceProfileConnector { public: MOCK_METHOD(std::vector, GetAllAccessControlProfile, ()); + MOCK_METHOD(int32_t, HandleDevUnBindEvent, (int32_t, const std::string &, const std::string &)); + MOCK_METHOD(std::string, HandleAppUnBindEvent, (int32_t, const std::string &, int32_t, const std::string &)); + MOCK_METHOD(int32_t, HandleAccountLogoutEvent, (int32_t, const std::string &, const std::string &, + const std::string &)); }; } } diff --git a/test/unittest/mock/distributed_device_profile_client_mock.cpp b/test/unittest/mock/distributed_device_profile_client_mock.cpp new file mode 100644 index 000000000..f76c9d451 --- /dev/null +++ b/test/unittest/mock/distributed_device_profile_client_mock.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 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 "distributed_device_profile_client_mock.h" + +#include "gtest/gtest.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(DistributedDeviceProfileClient); +int32_t DistributedDeviceProfileClient::GetAccessControlProfile(std::map params, + std::vector& accessControlProfiles) +{ + return DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient->GetAccessControlProfile(params, + accessControlProfiles); +} + +int32_t DistributedDeviceProfileClient::PutAccessControlProfile(const AccessControlProfile& accessControlProfile) +{ + return DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient->PutAccessControlProfile( + accessControlProfile); +} + +int32_t DistributedDeviceProfileClient::GetAllAccessControlProfile( + std::vector& accessControlProfiles) +{ + return DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient->GetAllAccessControlProfile( + accessControlProfiles); +} +} // 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 new file mode 100644 index 000000000..740d8ef08 --- /dev/null +++ b/test/unittest/mock/distributed_device_profile_client_mock.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef OHOS_DISTRIBUTED_DEVICE_PROFILE_CLIENT_MOCK_H +#define OHOS_DISTRIBUTED_DEVICE_PROFILE_CLIENT_MOCK_H + +#include +#include + +#include "distributed_device_profile_client.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class DpDistributedDeviceProfileClient { +public: + virtual ~DpDistributedDeviceProfileClient() = default; +public: + virtual int32_t GetAccessControlProfile(std::map params, + std::vector& accessControlProfiles) = 0; + virtual int32_t PutAccessControlProfile(const AccessControlProfile& accessControlProfile) = 0; + virtual int32_t GetAllAccessControlProfile(std::vector& accessControlProfiles) = 0; +public: + static inline std::shared_ptr dpDistributedDeviceProfileClient = nullptr; +}; + +class DistributedDeviceProfileClientMock : public DpDistributedDeviceProfileClient { +public: + MOCK_METHOD(int32_t, GetAccessControlProfile, ((std::map), + std::vector&)); + MOCK_METHOD(int32_t, PutAccessControlProfile, (const AccessControlProfile&)); + MOCK_METHOD(int32_t, GetAllAccessControlProfile, (std::vector&)); +}; +} +} +#endif diff --git a/test/unittest/mock/dm_softbus_cache_mock.cpp b/test/unittest/mock/dm_softbus_cache_mock.cpp new file mode 100644 index 000000000..994212433 --- /dev/null +++ b/test/unittest/mock/dm_softbus_cache_mock.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 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 "dm_softbus_cache_mock.h" + +#include "gtest/gtest.h" + +namespace OHOS { +namespace DistributedHardware { +DM_IMPLEMENT_SINGLE_INSTANCE(SoftbusCache); +int32_t SoftbusCache::GetUdidByUdidHash(const std::string &udidHash, std::string &udid) +{ + return DmSoftbusCache::dmSoftbusCache->GetUdidByUdidHash(udidHash, udid); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/dm_softbus_cache_mock.h b/test/unittest/mock/dm_softbus_cache_mock.h new file mode 100644 index 000000000..d492cacc0 --- /dev/null +++ b/test/unittest/mock/dm_softbus_cache_mock.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef OHOS_DM_SOFTBUS_CACHE_MOCK_H +#define OHOS_DM_SOFTBUS_CACHE_MOCK_H + +#include +#include + +#include "dm_softbus_cache.h" + +namespace OHOS { +namespace DistributedHardware { +class DmSoftbusCache { +public: + virtual ~DmSoftbusCache() = default; +public: + virtual int32_t GetUdidByUdidHash(const std::string &udidHash, std::string &udid) = 0; +public: + static inline std::shared_ptr dmSoftbusCache = nullptr; +}; + +class SoftbusCacheMock : public DmSoftbusCache { +public: + MOCK_METHOD(int32_t, GetUdidByUdidHash, (const std::string &, std::string &)); +}; +} +} +#endif diff --git a/test/unittest/mock/mock_distributed_device_profile_client.cpp b/test/unittest/mock/mock_distributed_device_profile_client.cpp index b640d78bb..674f4a500 100644 --- a/test/unittest/mock/mock_distributed_device_profile_client.cpp +++ b/test/unittest/mock/mock_distributed_device_profile_client.cpp @@ -585,6 +585,211 @@ void AddAccessControlProfileEleventh(std::vector& accessCo accessControlProfiles.push_back(profileFifth); } +void AddAccessControlProfileTwelfth(std::vector& accessControlProfiles) +{ + int32_t userId = 123456; + int32_t bindType = 256; + int32_t deviceIdType = 1; + uint32_t bindLevel = 1; + uint32_t status = 1; + uint32_t authenticationType = 2; + uint32_t accesserId = 1; + uint32_t tokenId = 1001; + + std::string oldAccountId = "oldAccountId"; + std::string newAccountId = "newAccountId"; + std::string deviceIdEr = "remoteDeviceId"; + std::string deviceIdEe = "localDeviceId"; + std::string trustDeviceId = "123456"; + + Accesser accesser; + accesser.SetAccesserId(accesserId); + accesser.SetAccesserDeviceId(deviceIdEr); + accesser.SetAccesserUserId(userId); + accesser.SetAccesserAccountId(oldAccountId); + accesser.SetAccesserTokenId(tokenId); + accesser.SetAccesserBundleName("bundleName"); + accesser.SetAccesserHapSignature("uph1"); + accesser.SetAccesserBindLevel(bindLevel); + + Accessee accessee; + accessee.SetAccesseeId(accesserId); + accessee.SetAccesseeDeviceId(deviceIdEe); + accessee.SetAccesseeUserId(userId); + accessee.SetAccesseeAccountId(newAccountId); + accessee.SetAccesseeTokenId(tokenId); + accessee.SetAccesseeBundleName("bundleName"); + accessee.SetAccesseeHapSignature("uph1"); + accessee.SetAccesseeBindLevel(bindLevel); + + AccessControlProfile profileFifth; + profileFifth.SetAccessControlId(accesserId); + profileFifth.SetAccesserId(accesserId); + profileFifth.SetAccesseeId(accesserId); + profileFifth.SetTrustDeviceId(trustDeviceId); + profileFifth.SetBindType(bindType); + profileFifth.SetAuthenticationType(authenticationType); + profileFifth.SetDeviceIdType(deviceIdType); + profileFifth.SetStatus(status); + profileFifth.SetBindLevel(bindLevel); + profileFifth.SetAccesser(accesser); + profileFifth.SetAccessee(accessee); + accessControlProfiles.push_back(profileFifth); +} + +void AddAccessControlprofileThirteenth(std::vector& accessControlProfiles) +{ + int32_t userId = 123456; + int32_t bindType = 1282; + int32_t deviceIdType = 1; + uint32_t bindLevel = 3; + uint32_t status = 3; + uint32_t authenticationType = 1; + uint32_t accesserId = 1; + uint32_t tokenId = 1001; + + std::string oldAccountId = "accountId"; + std::string newAccountId = "accountId"; + std::string deviceId = "deviceId"; + std::string trustDeviceId = "remoteDeviceId"; + + Accesser accesser; + accesser.SetAccesserId(accesserId); + accesser.SetAccesserDeviceId(deviceId); + accesser.SetAccesserUserId(userId); + accesser.SetAccesserAccountId(oldAccountId); + accesser.SetAccesserTokenId(tokenId); + accesser.SetAccesserBundleName("bundleName"); + accesser.SetAccesserHapSignature("uph1"); + accesser.SetAccesserBindLevel(bindLevel); + + Accessee accessee; + accessee.SetAccesseeId(accesserId); + accessee.SetAccesseeDeviceId(deviceId); + accessee.SetAccesseeUserId(userId); + accessee.SetAccesseeAccountId(newAccountId); + accessee.SetAccesseeTokenId(tokenId); + accessee.SetAccesseeBundleName("bundleName"); + accessee.SetAccesseeHapSignature("uph1"); + accessee.SetAccesseeBindLevel(bindLevel); + + AccessControlProfile profileFourth; + profileFourth.SetAccessControlId(accesserId); + profileFourth.SetAccesserId(accesserId); + profileFourth.SetAccesseeId(accesserId); + profileFourth.SetTrustDeviceId(trustDeviceId); + profileFourth.SetBindType(bindType); + profileFourth.SetAuthenticationType(authenticationType); + profileFourth.SetDeviceIdType(deviceIdType); + profileFourth.SetStatus(status); + profileFourth.SetBindLevel(bindLevel); + profileFourth.SetAccesser(accesser); + profileFourth.SetAccessee(accessee); + accessControlProfiles.push_back(profileFourth); +} + +void AddAccessControlprofileFourteenth(std::vector& accessControlProfiles) +{ + int32_t userId = 123456; + int32_t bindType = 1282; + int32_t deviceIdType = 1; + uint32_t bindLevel = 1; + uint32_t status = 1; + uint32_t authenticationType = 1; + uint32_t accesserId = 1; + uint32_t tokenId = 1001; + + std::string oldAccountId = "accountId"; + std::string newAccountId = "accountId"; + std::string deviceId = "deviceId"; + std::string trustDeviceId = "remoteDeviceId"; + + Accesser accesser; + accesser.SetAccesserId(0); + accesser.SetAccesserDeviceId(deviceId); + accesser.SetAccesserUserId(userId); + accesser.SetAccesserAccountId(""); + accesser.SetAccesserTokenId(tokenId); + accesser.SetAccesserBundleName("bundleName"); + accesser.SetAccesserHapSignature("uph1"); + accesser.SetAccesserBindLevel(bindLevel); + + Accessee accessee; + accessee.SetAccesseeId(accesserId); + accessee.SetAccesseeDeviceId(deviceId); + accessee.SetAccesseeUserId(userId); + accessee.SetAccesseeAccountId(newAccountId); + accessee.SetAccesseeTokenId(tokenId); + accessee.SetAccesseeBundleName(""); + accessee.SetAccesseeHapSignature("uph1"); + accessee.SetAccesseeBindLevel(bindLevel); + + AccessControlProfile profileFourth; + profileFourth.SetAccessControlId(accesserId); + profileFourth.SetAccesserId(accesserId); + profileFourth.SetAccesseeId(accesserId); + profileFourth.SetTrustDeviceId(trustDeviceId); + profileFourth.SetBindType(bindType); + profileFourth.SetAuthenticationType(authenticationType); + profileFourth.SetDeviceIdType(deviceIdType); + profileFourth.SetStatus(status); + profileFourth.SetBindLevel(bindLevel); + profileFourth.SetAccesser(accesser); + profileFourth.SetAccessee(accessee); + accessControlProfiles.push_back(profileFourth); +} + +void AddAccessControlprofileFifteenth(std::vector& accessControlProfiles) +{ + int32_t userId = 123456; + int32_t bindType = 1; + int32_t deviceIdType = 1; + uint32_t bindLevel = 1; + uint32_t status = 1; + uint32_t authenticationType = 1; + uint32_t accesserId = 1; + uint32_t tokenId = 1001; + + std::string oldAccountId = "accountId"; + std::string newAccountId = "accountId"; + std::string deviceId = "deviceId"; + std::string trustDeviceId = "123456"; + + Accesser accesser; + accesser.SetAccesserId(0); + accesser.SetAccesserDeviceId(deviceId); + accesser.SetAccesserUserId(userId); + accesser.SetAccesserAccountId(""); + accesser.SetAccesserTokenId(tokenId); + accesser.SetAccesserBundleName("bundleName"); + accesser.SetAccesserHapSignature("uph1"); + accesser.SetAccesserBindLevel(bindLevel); + + Accessee accessee; + accessee.SetAccesseeId(accesserId); + accessee.SetAccesseeDeviceId(deviceId); + accessee.SetAccesseeUserId(userId); + accessee.SetAccesseeAccountId(newAccountId); + accessee.SetAccesseeTokenId(tokenId); + accessee.SetAccesseeBundleName(""); + accessee.SetAccesseeHapSignature("uph1"); + accessee.SetAccesseeBindLevel(bindLevel); + + AccessControlProfile profileFourth; + profileFourth.SetAccessControlId(accesserId); + profileFourth.SetAccesserId(accesserId); + profileFourth.SetAccesseeId(accesserId); + profileFourth.SetTrustDeviceId(trustDeviceId); + profileFourth.SetBindType(bindType); + profileFourth.SetAuthenticationType(authenticationType); + profileFourth.SetDeviceIdType(deviceIdType); + profileFourth.SetStatus(status); + profileFourth.SetBindLevel(bindLevel); + profileFourth.SetAccesser(accesser); + profileFourth.SetAccessee(accessee); + accessControlProfiles.push_back(profileFourth); +} + int32_t DistributedDeviceProfileClient::PutAccessControlProfile(const AccessControlProfile& accessControlProfile) { return 0; @@ -620,6 +825,10 @@ int32_t DistributedDeviceProfileClient::GetAccessControlProfile(std::mapGetUuidByNetworkId(networkId, uuid); } + +int32_t SoftbusListener::GetTargetInfoFromCache(const std::string &deviceId, PeerTargetId &targetId, + ConnectionAddrType &addrType) +{ + return DmSoftbusListener::dmSoftbusListener->GetTargetInfoFromCache(deviceId, targetId, addrType); +} + +int32_t SoftbusListener::GetTrustedDeviceList(std::vector &deviceInfoList) +{ + return DmSoftbusListener::dmSoftbusListener->GetTrustedDeviceList(deviceInfoList); +} + +int32_t SoftbusListener::GetLocalDeviceInfo(DmDeviceInfo &deviceInfo) +{ + return DmSoftbusListener::dmSoftbusListener->GetLocalDeviceInfo(deviceInfo); +} } // 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 d99de2602..e2199f60e 100644 --- a/test/unittest/mock/softbus_listener_mock.h +++ b/test/unittest/mock/softbus_listener_mock.h @@ -31,6 +31,10 @@ public: virtual int32_t GetDeviceSecurityLevel(const char *networkId, int32_t &securityLevel) = 0; virtual int32_t GetDeviceInfo(const std::string &networkId, DmDeviceInfo &info) = 0; virtual int32_t GetUuidByNetworkId(const char *networkId, std::string &uuid) = 0; + virtual int32_t GetTargetInfoFromCache(const std::string &deviceId, PeerTargetId &targetId, + ConnectionAddrType &addrType) = 0; + virtual int32_t GetTrustedDeviceList(std::vector &deviceInfoList) = 0; + virtual int32_t GetLocalDeviceInfo(DmDeviceInfo &deviceInfo) = 0; public: static inline std::shared_ptr dmSoftbusListener = nullptr; }; @@ -42,6 +46,9 @@ public: MOCK_METHOD(int32_t, GetDeviceSecurityLevel, (const char *, int32_t &)); MOCK_METHOD(int32_t, GetDeviceInfo, (const std::string &, DmDeviceInfo &)); MOCK_METHOD(int32_t, GetUuidByNetworkId, (const char *, std::string &)); + MOCK_METHOD(int32_t, GetTargetInfoFromCache, (const std::string &, PeerTargetId &, ConnectionAddrType &addrType)); + MOCK_METHOD(int32_t, GetTrustedDeviceList, (std::vector &)); + MOCK_METHOD(int32_t, GetLocalDeviceInfo, (DmDeviceInfo &)); }; } } -- Gitee