diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 803b2863fdd90c9806faf1da1feee5a929afb753..9bed4a1860fcf99e02d6ecaf93d409603974cceb 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -45,6 +45,7 @@ group("unittest") { ":UTTest_dm_auth_manager_first", ":UTTest_dm_auth_manager_second", ":UTTest_dm_auth_manager_third", + ":UTTest_dm_auth_state", ":UTTest_dm_auth_message_processor", ":UTTest_dm_comm_tool", ":UTTest_dm_common_event_manager", @@ -1500,6 +1501,29 @@ ohos_unittest("UTTest_dm_auth_manager_third") { ## UTTest_dm_auth_manager_third }}} +## UnitTest UTTest_dm_auth_state {{{ +ohos_unittest("UTTest_dm_auth_state") { + module_out_path = module_out_path + + include_dirs = [ "${devicemanager_path}/test/unittest" ] + + sources = [ "${devicemanager_path}/test/unittest/UTTest_dm_auth_state.cpp" ] + + deps = [ ":device_manager_test_common" ] + + external_deps = [ + "device_auth:deviceauth_sdk", + "device_info_manager:distributed_device_profile_common", + "device_info_manager:distributed_device_profile_sdk", + "dsoftbus:softbus_client", + "ffrt:libffrt", + "googletest:gmock", + "hilog:libhilog", + ] +} + +## UTTest_dm_auth_state }}} + ############################### ## UnitTest UTTest_dm_radar_helper_test {{{ ohos_unittest("UTTest_dm_radar_helper_test") { diff --git a/test/unittest/UTTest_device_name_manager.cpp b/test/unittest/UTTest_device_name_manager.cpp index 409f6489435df5d466f59bf919687f948aef4acf..8dddb5787d0463d6f61a33da663375e9dc8b9a32 100644 --- a/test/unittest/UTTest_device_name_manager.cpp +++ b/test/unittest/UTTest_device_name_manager.cpp @@ -324,5 +324,348 @@ HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_002, testing::ext::Tes result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(maxNamelength, output); EXPECT_EQ(result, ERR_DM_INPUT_PARA_INVALID); } + +HWTEST_F(DeviceNameManagerTest, ReleaseDataShareHelper_001, testing::ext::TestSize.Level1) +{ + std::shared_ptr helper = nullptr; + bool ret = DeviceNameManager::GetInstance().ReleaseDataShareHelper(helper); + EXPECT_EQ(ret, false); +} + +HWTEST_F(DeviceNameManagerTest, ReleaseDataShareHelper_002, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(helper_ != nullptr); + EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(false)); + bool ret = DeviceNameManager::GetInstance().ReleaseDataShareHelper(helper_); + EXPECT_EQ(ret, false); +} + +HWTEST_F(DeviceNameManagerTest, ReleaseDataShareHelper_003, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(helper_ != nullptr); + EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true)); + bool ret = DeviceNameManager::GetInstance().ReleaseDataShareHelper(helper_); + EXPECT_EQ(ret, true); +} + +HWTEST_F(DeviceNameManagerTest, MakeUri_001, testing::ext::TestSize.Level1) +{ + std::string proxyUri = ""; + std::string key = "key"; + auto ret = DeviceNameManager::GetInstance().MakeUri(proxyUri, key); + EXPECT_EQ(ret, Uri(proxyUri + "&key=" + key)); +} + +HWTEST_F(DeviceNameManagerTest, GetProxyUriStr_001, testing::ext::TestSize.Level1) +{ + std::string tableName = "SETTINGSDATA"; + int32_t userId = 12; + auto ret = DeviceNameManager::GetInstance().GetProxyUriStr(tableName, userId); + EXPECT_EQ(ret, "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true"); +} + +HWTEST_F(DeviceNameManagerTest, GetProxyUriStr_002, testing::ext::TestSize.Level1) +{ + std::string tableName = "tableName"; + int32_t userId = 12; + auto ret = DeviceNameManager::GetInstance().GetProxyUriStr(tableName, userId); + EXPECT_EQ(ret, "datashare:///com.ohos.settingsdata/entry/settingsdata/tableName100?Proxy=true"); +} + +HWTEST_F(DeviceNameManagerTest, SetValue_001, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(helper_ != nullptr); + + std::string tableName = "tableName"; + int32_t userId = 12; + std::string key = "key"; + std::string value = "value"; + int32_t ret = -1; + EXPECT_CALL(*helper_, Update(_, _, _)).WillRepeatedly(Return(ret)); + EXPECT_CALL(*helper_, Insert(_, _)).WillRepeatedly(Return(ret)); + EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true)); + + auto result = DeviceNameManager::GetInstance().SetValue(tableName, userId, key, value); + EXPECT_EQ(result, -1); +} + +HWTEST_F(DeviceNameManagerTest, SetValue_002, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(helper_ != nullptr); + + std::string tableName = "tableName"; + int32_t userId = 12; + std::string key = "key"; + std::string value = "value"; + int32_t ret = 1; + EXPECT_CALL(*helper_, Update(_, _, _)).WillRepeatedly(Return(ret)); + EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true)); + + auto result = DeviceNameManager::GetInstance().SetValue(tableName, userId, key, value); + EXPECT_EQ(result, 1); +} + +HWTEST_F(DeviceNameManagerTest, GetValue_001, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(helper_ != nullptr); + + std::string tableName = "tableName"; + int32_t userId = 12; + std::string key = "key"; + std::string value = "value"; + std::shared_ptr resultSet = nullptr; + EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet)); + EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true)); + + auto result = DeviceNameManager::GetInstance().GetValue(tableName, userId, key, value); + EXPECT_EQ(result, ERR_DM_POINT_NULL); +} + +HWTEST_F(DeviceNameManagerTest, GetValue_002, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(helper_ != nullptr); + + std::string tableName = "tableName"; + int32_t userId = 12; + std::string key = "key"; + std::string value = "value"; + auto resultSet = std::make_shared(nullptr); + EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet)); + EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true)); + + EXPECT_CALL(*resultSet, GetRowCount(_)).WillRepeatedly(DoAll(SetArgReferee<0>(1), Return(DataShare::E_OK))); + EXPECT_CALL(*resultSet, GoToRow(_)).Times(AtLeast(1)); + EXPECT_CALL(*resultSet, GetString(_, _)).WillRepeatedly(Return(DataShare::E_OK)); + EXPECT_CALL(*resultSet, Close()).Times(AtLeast(1)); + auto result = DeviceNameManager::GetInstance().GetValue(tableName, userId, key, value); + EXPECT_EQ(result, DM_OK); +} + +HWTEST_F(DeviceNameManagerTest, GetValue_003, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(helper_ != nullptr); + + std::string tableName = "tableName"; + int32_t userId = 12; + std::string key = "key"; + std::string value = "value"; + auto resultSet = std::make_shared(nullptr); + EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet)); + EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true)); + + EXPECT_CALL(*resultSet, GetRowCount(_)).WillRepeatedly(DoAll(SetArgReferee<0>(1), Return(DataShare::E_OK))); + EXPECT_CALL(*resultSet, GoToRow(_)).Times(AtLeast(1)); + EXPECT_CALL(*resultSet, GetString(_, _)).WillRepeatedly(Return(6666)); + EXPECT_CALL(*resultSet, Close()).Times(AtLeast(1)); + auto result = DeviceNameManager::GetInstance().GetValue(tableName, userId, key, value); + EXPECT_EQ(result, 6666); +} + +HWTEST_F(DeviceNameManagerTest, GetValue_004, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(helper_ != nullptr); + + std::string tableName = "tableName"; + int32_t userId = 12; + std::string key = "key"; + std::string value = "value"; + auto resultSet = std::make_shared(nullptr); + EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet)); + EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true)); + + EXPECT_CALL(*resultSet, GetRowCount(_)).WillRepeatedly(DoAll(SetArgReferee<0>(0), Return(DataShare::E_OK))); + EXPECT_CALL(*resultSet, Close()).Times(AtLeast(1)); + auto result = DeviceNameManager::GetInstance().GetValue(tableName, userId, key, value); + EXPECT_EQ(result, DM_OK); +} + +HWTEST_F(DeviceNameManagerTest, GetRemoteObj_001, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(client_ != nullptr); + auto bundleMgr = sptr(new (std::nothrow) BundleMgrMock()); + auto systemAbilityManager = sptr(new (std::nothrow) SystemAbilityManagerMock()); + EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_)) + .WillRepeatedly(Return(bundleMgr)); + EXPECT_CALL(*client_, GetSystemAbilityManager()) + .WillRepeatedly(Return(systemAbilityManager)); + auto ret = DeviceNameManager::GetInstance().GetRemoteObj(); + EXPECT_NE(ret, nullptr); +} + +HWTEST_F(DeviceNameManagerTest, SetDeviceName_001, testing::ext::TestSize.Level1) +{ + std::string deviceName = ""; + auto ret = DeviceNameManager::GetInstance().SetDeviceName(deviceName); + EXPECT_EQ(ret, ERR_DM_NAME_EMPTY); +} + +HWTEST_F(DeviceNameManagerTest, SetDeviceName_002, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(helper_ != nullptr); + std::string deviceName = "deviceName"; + int32_t ret = 1; + EXPECT_CALL(*helper_, Update(_, _, _)).WillRepeatedly(Return(ret)); + EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true)); + auto result = DeviceNameManager::GetInstance().SetDeviceName(deviceName); + EXPECT_EQ(result, 1); +} + +HWTEST_F(DeviceNameManagerTest, GetDeviceName_001, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(helper_ != nullptr); + + std::string deviceName = "deviceName"; + std::shared_ptr resultSet = nullptr; + EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet)); + EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true)); + + auto result = DeviceNameManager::GetInstance().GetDeviceName(deviceName); + EXPECT_EQ(result, ERR_DM_POINT_NULL); +} + +HWTEST_F(DeviceNameManagerTest, SetDisplayDeviceName_001, testing::ext::TestSize.Level1) +{ + std::string deviceName = ""; + int32_t userId = 12; + auto result = DeviceNameManager::GetInstance().SetDisplayDeviceName(deviceName, userId); + EXPECT_EQ(result, ERR_DM_NAME_EMPTY); +} + +HWTEST_F(DeviceNameManagerTest, SetDisplayDeviceName_002, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(helper_ != nullptr); + std::string deviceName = "deviceName"; + int32_t userId = 12; + int32_t ret = 1; + EXPECT_CALL(*helper_, Update(_, _, _)).WillRepeatedly(Return(ret)); + EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true)); + + auto result = DeviceNameManager::GetInstance().SetDisplayDeviceName(deviceName, userId); + EXPECT_EQ(result, 1); +} + +HWTEST_F(DeviceNameManagerTest, SetDisplayDeviceNameState_001, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(helper_ != nullptr); + std::string state = "state"; + int32_t userId = 12; + int32_t ret = 1; + EXPECT_CALL(*helper_, Update(_, _, _)).WillRepeatedly(Return(ret)); + EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true)); + + auto result = DeviceNameManager::GetInstance().SetDisplayDeviceNameState(state, userId); + EXPECT_EQ(result, 1); +} + +HWTEST_F(DeviceNameManagerTest, GetDisplayDeviceName_001, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(helper_ != nullptr); + std::string deviceName = "deviceName"; + int32_t userId = 12; + + std::shared_ptr resultSet = nullptr; + EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet)); + EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true)); + + auto result = DeviceNameManager::GetInstance().GetDisplayDeviceName(userId, deviceName); + EXPECT_EQ(result, ERR_DM_POINT_NULL); +} + +HWTEST_F(DeviceNameManagerTest, SetUserDefinedDeviceName_001, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(helper_ != nullptr); + std::string deviceName = "deviceName"; + int32_t userId = 12; + + int32_t ret = 1; + EXPECT_CALL(*helper_, Update(_, _, _)).WillRepeatedly(Return(ret)); + EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true)); + + auto result = DeviceNameManager::GetInstance().SetUserDefinedDeviceName(deviceName, userId); + EXPECT_EQ(result, 1); +} + +HWTEST_F(DeviceNameManagerTest, SubstrByBytes_001, testing::ext::TestSize.Level1) +{ + std::string str = "str"; + int32_t maxNumBytes = 12; + auto result = DeviceNameManager::GetInstance().SubstrByBytes(str, maxNumBytes); + EXPECT_EQ(result, str); +} + +HWTEST_F(DeviceNameManagerTest, SubstrByBytes_002, testing::ext::TestSize.Level1) +{ + std::string str = "HelloWorld"; + int32_t maxNumBytes = 5; + auto result = DeviceNameManager::GetInstance().SubstrByBytes(str, maxNumBytes); + EXPECT_EQ(result, "Hello"); +} + +HWTEST_F(DeviceNameManagerTest, GetUserDefinedDeviceName_001, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(helper_ != nullptr); + std::string deviceName = "deviceName"; + int32_t userId = 12; + std::shared_ptr resultSet = nullptr; + EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet)); + EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true)); + auto result = DeviceNameManager::GetInstance().GetUserDefinedDeviceName(userId, deviceName); + EXPECT_EQ(result, ERR_DM_POINT_NULL); +} + +HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_003, testing::ext::TestSize.Level1) +{ + std::string prefixName = "My"; + std::string subffixName = "Device"; + int32_t maxNameLength = 20; + std::string result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(prefixName, subffixName, maxNameLength); + EXPECT_EQ(result, "My的Device"); +} + +HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_004, testing::ext::TestSize.Level1) +{ + std::string prefixName = "MyVeryLong"; + std::string subffixName = "DeviceName"; + int32_t maxNameLength = 15; + std::string result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(prefixName, subffixName, maxNameLength); + EXPECT_EQ(result, "My...DeviceName"); +} + +HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_005, testing::ext::TestSize.Level1) +{ + std::string prefixName = "My"; + std::string subffixName = "Device"; + int32_t maxNameLength = 0; + std::string result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(prefixName, subffixName, maxNameLength); + EXPECT_EQ(result, "My的Device"); +} + +HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_006, testing::ext::TestSize.Level1) +{ + std::string prefixName = "My"; + std::string subffixName = "VeryLongDeviceNameThatExceedsLimit"; + int32_t maxNameLength = 30; + std::string result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(prefixName, subffixName, maxNameLength); + EXPECT_EQ(result, "My的VeryLongDevi..."); +} + +HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_007, testing::ext::TestSize.Level1) +{ + std::string prefixName = ""; + std::string subffixName = "Device"; + int32_t maxNameLength = 10; + std::string result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(prefixName, subffixName, maxNameLength); + EXPECT_EQ(result, "Device"); +} + +HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_008, testing::ext::TestSize.Level1) +{ + std::string prefixName = ""; + std::string subffixName = "VeryLongDeviceName"; + int32_t maxNameLength = 10; + std::string result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(prefixName, subffixName, maxNameLength); + EXPECT_EQ(result, "VeryLon..."); +} + } // DistributedHardware } // OHOS diff --git a/test/unittest/UTTest_dm_comm_tool.cpp b/test/unittest/UTTest_dm_comm_tool.cpp index 5779239256f750b7157b5038b2495539694ab510..1fec5a40ef64b63877caf38093ad8c882cc1a9e5 100644 --- a/test/unittest/UTTest_dm_comm_tool.cpp +++ b/test/unittest/UTTest_dm_comm_tool.cpp @@ -34,6 +34,7 @@ void DMCommToolTest::SetUpTestCase() { DmDMTransport::dMTransport_ = dmTransportMock_; DmSoftbusCache::dmSoftbusCache = softbusCacheMock_; + DMCommToolMock::dmDMCommTool = dmCommToolMock_; } void DMCommToolTest::TearDownTestCase() { @@ -41,6 +42,7 @@ void DMCommToolTest::TearDownTestCase() dmTransportMock_ = nullptr; DmSoftbusCache::dmSoftbusCache = nullptr; softbusCacheMock_ = nullptr; + dmCommToolMock_ = nullptr; } /** @@ -146,5 +148,326 @@ HWTEST_F(DMCommToolTest, ProcessResponseUserIdsEvent_001, testing::ext::TestSize int32_t ret = dmCommTool->SendUserIds(rmtNetworkId, foregroundUserIds, backgroundUserIds); EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); } + +HWTEST_F(DMCommToolTest, UnInit_001, testing::ext::TestSize.Level1) +{ + dmCommTool->dmTransportPtr_ = nullptr; + + EXPECT_NO_THROW(dmCommTool->UnInit()); +} + +HWTEST_F(DMCommToolTest, UnInit_002, testing::ext::TestSize.Level1) +{ + EXPECT_CALL(*dmTransportMock_, UnInit()).Times(1); + + EXPECT_NO_THROW(dmCommTool->UnInit()); +} + +HWTEST_F(DMCommToolTest, SendMsg_001, testing::ext::TestSize.Level1) +{ + std::string invalidNetworkId = ""; + int32_t msgType = 1; + std::string msg = "test message"; + + int32_t ret = dmCommTool->SendMsg(invalidNetworkId, msgType, msg); + EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); +} + +HWTEST_F(DMCommToolTest, SendMsg_002, testing::ext::TestSize.Level1) +{ + dmCommTool->dmTransportPtr_ = nullptr; + std::string rmtNetworkId = "validNetworkId"; + int32_t msgType = 1; + std::string msg = "test message"; + + int32_t ret = dmCommTool->SendMsg(rmtNetworkId, msgType, msg); + EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); +} + +HWTEST_F(DMCommToolTest, SendMsg_003, testing::ext::TestSize.Level1) +{ + std::string rmtNetworkId = "validNetworkId"; + int32_t msgType = 1; + std::string msg = "test message"; + + EXPECT_CALL(*dmTransportMock_, StartSocket(rmtNetworkId, _)) + .WillOnce(Return(ERR_DM_FAILED)); + + int32_t ret = dmCommTool->SendMsg(rmtNetworkId, msgType, msg); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DMCommToolTest, SendMsg_004, testing::ext::TestSize.Level1) +{ + std::string rmtNetworkId = "validNetworkId"; + int32_t msgType = 1; + std::string msg = "test message"; + + EXPECT_CALL(*dmTransportMock_, StartSocket(rmtNetworkId, _)) + .WillOnce(DoAll(SetArgReferee<1>(-1), Return(DM_OK))); + + int32_t ret = dmCommTool->SendMsg(rmtNetworkId, msgType, msg); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DMCommToolTest, SendMsg_005, testing::ext::TestSize.Level1) +{ + std::string rmtNetworkId = "validNetworkId"; + int32_t msgType = 1; + std::string msg = "test message"; + + EXPECT_CALL(*dmTransportMock_, StartSocket(rmtNetworkId, _)) + .WillOnce(DoAll(SetArgReferee<1>(1), Return(DM_OK))); + EXPECT_CALL(*dmTransportMock_, Send(rmtNetworkId, _, 1)) + .WillOnce(Return(ERR_DM_FAILED)); + + int32_t ret = dmCommTool->SendMsg(rmtNetworkId, msgType, msg); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DMCommToolTest, SendMsg_006, testing::ext::TestSize.Level1) +{ + std::string rmtNetworkId = "validNetworkId"; + int32_t msgType = 1; + std::string msg = "test message"; + + EXPECT_CALL(*dmTransportMock_, StartSocket(rmtNetworkId, _)) + .WillOnce(DoAll(SetArgReferee<1>(1), Return(DM_OK))); + EXPECT_CALL(*dmTransportMock_, Send(rmtNetworkId, _, 1)) + .WillOnce(Return(DM_OK)); + + int32_t ret = dmCommTool->SendMsg(rmtNetworkId, msgType, msg); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DMCommToolTest, SendUserStop_001, testing::ext::TestSize.Level1) +{ + std::string rmtNetworkId = "validNetworkId"; + int32_t stopUserId = 12345; + + EXPECT_CALL(*dmTransportMock_, StartSocket(rmtNetworkId, _)) + .Times(1).WillOnce(Return(DM_OK)); + // 模拟 CreateUserStopMessage 返回错误 + EXPECT_CALL(*dmTransportMock_, Send(_, _, _)).Times(0); + + int32_t ret = dmCommTool->SendUserStop(rmtNetworkId, stopUserId); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DMCommToolTest, SendUserStop_002, testing::ext::TestSize.Level1) +{ + std::string invalidNetworkId = ""; + int32_t stopUserId = 12345; + + int32_t ret = dmCommTool->SendUserStop(invalidNetworkId, stopUserId); + EXPECT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); +} + +HWTEST_F(DMCommToolTest, SendUserStop_003, testing::ext::TestSize.Level1) +{ + std::string rmtNetworkId = "validNetworkId"; + int32_t stopUserId = 12345; + + EXPECT_CALL(*dmTransportMock_, StartSocket(rmtNetworkId, _)) + .WillOnce(DoAll(SetArgReferee<1>(1), Return(DM_OK))); + EXPECT_CALL(*dmTransportMock_, Send(rmtNetworkId, _, 1)) + .WillOnce(Return(ERR_DM_FAILED)); + + int32_t ret = dmCommTool->SendUserStop(rmtNetworkId, stopUserId); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DMCommToolTest, SendUserStop_004, testing::ext::TestSize.Level1) +{ + std::string rmtNetworkId = "validNetworkId"; + int32_t stopUserId = 12345; + + EXPECT_CALL(*dmTransportMock_, StartSocket(rmtNetworkId, _)) + .WillOnce(Return(ERR_DM_FAILED)); + + int32_t ret = dmCommTool->SendUserStop(rmtNetworkId, stopUserId); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(DMCommToolTest, SendUserStop_005, testing::ext::TestSize.Level1) +{ + std::string rmtNetworkId = "validNetworkId"; + int32_t stopUserId = 12345; + + EXPECT_CALL(*dmTransportMock_, StartSocket(rmtNetworkId, _)) + .WillOnce(DoAll(SetArgReferee<1>(1), Return(DM_OK))); + EXPECT_CALL(*dmTransportMock_, Send(rmtNetworkId, _, 1)) + .WillOnce(Return(DM_OK)); + + int32_t ret = dmCommTool->SendUserStop(rmtNetworkId, stopUserId); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(DMCommToolTest, ParseUserStopMessage_001, testing::ext::TestSize.Level1) +{ + std::string invalidJson = "invalid_json"; + int32_t stopUserId = -1; + + int32_t result = DMCommTool::GetInstance()->ParseUserStopMessage(invalidJson, stopUserId); + EXPECT_EQ(result, ERR_DM_FAILED); +} + +HWTEST_F(DMCommToolTest, ParseUserStopMessage_002, testing::ext::TestSize.Level1) +{ + std::string jsonWithoutKey = R"({ "otherKey": 12345 })"; + int32_t stopUserId = -1; + + int32_t result = DMCommTool::GetInstance()->ParseUserStopMessage(jsonWithoutKey, stopUserId); + EXPECT_EQ(result, ERR_DM_FAILED); +} + +HWTEST_F(DMCommToolTest, ParseUserStopMessage_003, testing::ext::TestSize.Level1) +{ + std::string jsonWithInvalidValue = R"({ "stopUserId": "not_a_number" })"; + int32_t stopUserId = -1; + + int32_t result = DMCommTool::GetInstance()->ParseUserStopMessage(jsonWithInvalidValue, stopUserId); + EXPECT_EQ(result, ERR_DM_FAILED); +} + +HWTEST_F(DMCommToolTest, ParseUserStopMessage_004, testing::ext::TestSize.Level1) +{ + std::string validJson = R"({ "stopUserId": 12345 })"; + int32_t stopUserId = -1; + + int32_t result = DMCommTool::GetInstance()->ParseUserStopMessage(validJson, stopUserId); + EXPECT_EQ(result, DM_OK); + EXPECT_EQ(stopUserId, 12345); +} + +HWTEST_F(DMCommToolTest, ProcessReceiveUserStopEvent_001, testing::ext::TestSize.Level1) +{ + std::shared_ptr commMsg = nullptr; + + EXPECT_NO_THROW(dmCommTool->ProcessReceiveUserStopEvent(commMsg)); +} + +HWTEST_F(DMCommToolTest, ProcessReceiveUserStopEvent_002, testing::ext::TestSize.Level1) +{ + std::shared_ptr commMsg_ = std::make_shared(1, "{}"); + std::shared_ptr commMsg = std::make_shared("networkId", commMsg_, 0); + + EXPECT_CALL(*softbusCacheMock_, GetUdidFromCache(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(""), Return(ERR_DM_FAILED))); + + EXPECT_NO_THROW(dmCommTool->ProcessReceiveUserStopEvent(commMsg)); +} + +HWTEST_F(DMCommToolTest, ProcessReceiveUserStopEvent_003, testing::ext::TestSize.Level1) +{ + std::shared_ptr commMsg_ = std::make_shared(1, "invalid_json"); + std::shared_ptr commMsg = std::make_shared("networkId", commMsg_, 0); + + EXPECT_CALL(*softbusCacheMock_, GetUdidFromCache(_, _)) + .WillOnce(DoAll(SetArgReferee<1>("validUdid"), Return(DM_OK))); + + EXPECT_NO_THROW(dmCommTool->ProcessReceiveUserStopEvent(commMsg)); +} + +HWTEST_F(DMCommToolTest, ProcessReceiveUserStopEvent_004, testing::ext::TestSize.Level1) +{ + std::string validJson = R"({ "stopUserId": 12345 })"; + std::shared_ptr commMsg_ = std::make_shared(1, validJson); + std::shared_ptr commMsg = std::make_shared("networkId", commMsg_, 0); + + EXPECT_CALL(*softbusCacheMock_, GetUdidFromCache(_, _)) + .WillOnce(DoAll(SetArgReferee<1>("validUdid"), Return(DM_OK))); + EXPECT_CALL(*dmTransportMock_, Send(_, _, _)).Times(1); // 限制 Send 函数不被调用 + + EXPECT_NO_THROW(dmCommTool->ProcessReceiveUserStopEvent(commMsg)); +} + +HWTEST_F(DMCommToolTest, RspUserStop_001, testing::ext::TestSize.Level1) +{ + dmCommTool->dmTransportPtr_ = nullptr; + std::string rmtNetworkId = "validNetworkId"; + int32_t socketId = 1; + int32_t stopUserId = 12345; + + EXPECT_NO_THROW(dmCommTool->RspUserStop(rmtNetworkId, socketId, stopUserId)); +} + +HWTEST_F(DMCommToolTest, RspUserStop_002, testing::ext::TestSize.Level1) +{ + std::string rmtNetworkId = "validNetworkId"; + int32_t invalidSocketId = -1; + int32_t stopUserId = 12345; + + EXPECT_CALL(*dmTransportMock_, Send(rmtNetworkId, _, invalidSocketId)) + .WillOnce(Return(ERR_DM_FAILED)); + + EXPECT_NO_THROW(dmCommTool->RspUserStop(rmtNetworkId, invalidSocketId, stopUserId)); +} + +HWTEST_F(DMCommToolTest, RspUserStop_003, testing::ext::TestSize.Level1) +{ + std::string rmtNetworkId = "validNetworkId"; + int32_t socketId = 1; + int32_t stopUserId = 12345; + + EXPECT_CALL(*dmTransportMock_, Send(rmtNetworkId, _, socketId)) + .WillOnce(Return(ERR_DM_FAILED)); + + EXPECT_NO_THROW(dmCommTool->RspUserStop(rmtNetworkId, socketId, stopUserId)); +} + +HWTEST_F(DMCommToolTest, RspUserStop_004, testing::ext::TestSize.Level1) +{ + std::string rmtNetworkId = "validNetworkId"; + int32_t socketId = 1; + int32_t stopUserId = 12345; + + EXPECT_CALL(*dmTransportMock_, Send(rmtNetworkId, _, socketId)) + .WillOnce(Return(DM_OK)); + + EXPECT_NO_THROW(dmCommTool->RspUserStop(rmtNetworkId, socketId, stopUserId)); +} + +HWTEST_F(DMCommToolTest, ProcessResponseUserStopEvent_001, testing::ext::TestSize.Level1) +{ + std::shared_ptr commMsg = nullptr; + + EXPECT_NO_THROW(dmCommTool->ProcessResponseUserStopEvent(commMsg)); +} + +HWTEST_F(DMCommToolTest, ProcessResponseUserStopEvent_002, testing::ext::TestSize.Level1) +{ + std::shared_ptr commMsg_ = std::make_shared(1, "{}"); + std::shared_ptr commMsg = std::make_shared("networkId", commMsg_, 0); + + EXPECT_CALL(*softbusCacheMock_, GetUdidFromCache(_, _)) + .WillOnce(DoAll(SetArgReferee<1>(""), Return(ERR_DM_FAILED))); + + EXPECT_NO_THROW(dmCommTool->ProcessResponseUserStopEvent(commMsg)); +} + +HWTEST_F(DMCommToolTest, ProcessResponseUserStopEvent_003, testing::ext::TestSize.Level1) +{ + std::shared_ptr commMsg_ = std::make_shared(1, "invalid_json"); + std::shared_ptr commMsg = std::make_shared("networkId", commMsg_, 0); + + EXPECT_CALL(*softbusCacheMock_, GetUdidFromCache(_, _)) + .WillOnce(DoAll(SetArgReferee<1>("validUdid"), Return(DM_OK))); + + EXPECT_NO_THROW(dmCommTool->ProcessResponseUserStopEvent(commMsg)); +} + +HWTEST_F(DMCommToolTest, ProcessResponseUserStopEvent_004, testing::ext::TestSize.Level1) +{ + std::string validJson = R"({ "stopUserId": 12345 })"; + std::shared_ptr commMsg_ = std::make_shared(1, validJson); + std::shared_ptr commMsg = std::make_shared("networkId", commMsg_, 0); + + EXPECT_CALL(*softbusCacheMock_, GetUdidFromCache(_, _)) + .WillOnce(DoAll(SetArgReferee<1>("validUdid"), Return(DM_OK))); + + EXPECT_NO_THROW(dmCommTool->ProcessResponseUserStopEvent(commMsg)); +} + } // DistributedHardware } // OHOS \ No newline at end of file diff --git a/test/unittest/UTTest_dm_comm_tool.h b/test/unittest/UTTest_dm_comm_tool.h index 5fae452d0d8e62830fd4778a1de40c72723df427..a24fe4d260487a1a3b01dd1caf479c81c9a2102f 100644 --- a/test/unittest/UTTest_dm_comm_tool.h +++ b/test/unittest/UTTest_dm_comm_tool.h @@ -21,6 +21,7 @@ #include "dm_comm_tool.h" #include "dm_transport_mock.h" #include "dm_softbus_cache_mock.h" +#include "dm_comm_tool_mock.h" namespace OHOS { namespace DistributedHardware { @@ -32,6 +33,7 @@ public: void TearDown(); static inline std::shared_ptr dmTransportMock_ = std::make_shared(); static inline std::shared_ptr softbusCacheMock_ = std::make_shared(); + static inline std::shared_ptr dmCommToolMock_ = std::make_shared(); protected: std::shared_ptr dmCommTool; diff --git a/test/unittest/UTTest_dm_transport_msg.cpp b/test/unittest/UTTest_dm_transport_msg.cpp index 1e662ffd16984bd51a408cf1085d28a855798050..e5548295f736fb935195833fecf49299b5a64f9c 100644 --- a/test/unittest/UTTest_dm_transport_msg.cpp +++ b/test/unittest/UTTest_dm_transport_msg.cpp @@ -62,6 +62,24 @@ HWTEST_F(DMTransportMsgTest, ToJsonAndFromJson, testing::ext::TestSize.Level1) cJSON_Delete(emptyJsonObject); } +/** + * @tc.name: ToJsonAndFromJson_Invaild + * @tc.type: FUNC + */ +HWTEST_F(DMTransportMsgTest, ToJsonAndFromJson_Invaild, testing::ext::TestSize.Level1) +{ + UserIdsMsg userIdsMsg; + userIdsMsg.foregroundUserIds = {1, 2, 3}; + + cJSON *jsonObject = nullptr; + ToJson(jsonObject, userIdsMsg); + + UserIdsMsg newUserIdsMsg; + FromJson(jsonObject, newUserIdsMsg); + EXPECT_EQ(jsonObject, nullptr); + EXPECT_TRUE(newUserIdsMsg.foregroundUserIds.empty()); +} + /** * @tc.name: ToJsonAndFromJson01 * @tc.type: FUNC @@ -92,6 +110,114 @@ HWTEST_F(DMTransportMsgTest, ToJsonAndFromJson01, testing::ext::TestSize.Level1) cJSON_Delete(emptyCommJsonObject); } +/** + * @tc.name: ToJsonAndFromJson01_Invaild + * @tc.type: FUNC + */ +HWTEST_F(DMTransportMsgTest, ToJsonAndFromJson01_Invaild, testing::ext::TestSize.Level1) +{ + CommMsg commMsg; + commMsg.code = 200; + commMsg.msg = "Success"; + + cJSON *jsonObject = nullptr; + ToJson(jsonObject, commMsg); + + CommMsg newCommMsg; + FromJson(jsonObject, newCommMsg); + + EXPECT_EQ(jsonObject, nullptr); + EXPECT_TRUE(newCommMsg.msg.empty()); +} + +/** + * @tc.name: ToJsonAndFromJson02 + * @tc.type: FUNC + */ + +HWTEST_F(DMTransportMsgTest, ToJsonAndFromJson02, testing::ext::TestSize.Level1) +{ + std::string remoteUdid = "test_udid"; + std::vector userIds = {10, 20, 30}; + NotifyUserIds notifyUserIds(remoteUdid, userIds); + cJSON *jsonObject = cJSON_CreateObject(); + ToJson(jsonObject, notifyUserIds); + + NotifyUserIds newNotifyUserIds; + FromJson(jsonObject, newNotifyUserIds); + EXPECT_EQ(newNotifyUserIds.remoteUdid, remoteUdid); + EXPECT_EQ(newNotifyUserIds.userIds.size(), 3); + EXPECT_EQ(newNotifyUserIds.userIds[0], 10); + EXPECT_EQ(newNotifyUserIds.userIds[1], 20); + EXPECT_EQ(newNotifyUserIds.userIds[2], 30); + cJSON_Delete(jsonObject); + + cJSON *emptyCommJsonObject = cJSON_CreateObject(); + NotifyUserIds emptyNotifyUserIds; + FromJson(emptyCommJsonObject, emptyNotifyUserIds); + EXPECT_EQ(emptyNotifyUserIds.userIds.size(), 0); + EXPECT_EQ(emptyNotifyUserIds.remoteUdid, ""); + cJSON_Delete(emptyCommJsonObject); +} + +/** + * @tc.name: ToJsonAndFromJson02_Invaild + * @tc.type: FUNC + */ +HWTEST_F(DMTransportMsgTest, ToJsonAndFromJson02_Invaild, testing::ext::TestSize.Level1) +{ + std::string remoteUdid = "test_udid"; + std::vector userIds = {10, 20, 30}; + NotifyUserIds notifyUserIds(remoteUdid, userIds); + cJSON *jsonObject = nullptr; + ToJson(jsonObject, notifyUserIds); + NotifyUserIds newNotifyUserIds; + FromJson(jsonObject, newNotifyUserIds); + EXPECT_EQ(jsonObject, nullptr); + EXPECT_EQ(newNotifyUserIds.remoteUdid, ""); + EXPECT_EQ(newNotifyUserIds.userIds.size(), 0); +} + +/** + * @tc.name: ToJsonAndFromJson03 + * @tc.type: FUNC + */ +HWTEST_F(DMTransportMsgTest, ToJsonAndFromJson03, testing::ext::TestSize.Level1) +{ + LogoutAccountMsg logoutAccountMsg; + logoutAccountMsg.accountId = "test_account_id"; + logoutAccountMsg.userId = 123; + cJSON *jsonObject = cJSON_CreateObject(); + ToJson(jsonObject, logoutAccountMsg); + LogoutAccountMsg newLogoutAccountMsg; + FromJson(jsonObject, newLogoutAccountMsg); + EXPECT_EQ(newLogoutAccountMsg.accountId, "test_account_id"); + EXPECT_EQ(newLogoutAccountMsg.userId, 123); + cJSON_Delete(jsonObject); + + cJSON *emptyJsonObject = cJSON_CreateObject(); + LogoutAccountMsg emptyLogoutAccountMsg; + FromJson(emptyJsonObject, emptyLogoutAccountMsg); + EXPECT_EQ(emptyLogoutAccountMsg.userId, -1); +} + +/** + * @tc.name: ToJsonAndFromJson03_Invaild + * @tc.type: FUNC + */ +HWTEST_F(DMTransportMsgTest, ToJsonAndFromJson03_Invaild, testing::ext::TestSize.Level1) +{ + LogoutAccountMsg logoutAccountMsg; + logoutAccountMsg.accountId = "test_account_id"; + logoutAccountMsg.userId = 123; + cJSON *jsonObject = nullptr; + ToJson(jsonObject, logoutAccountMsg); + LogoutAccountMsg newLogoutAccountMsg; + FromJson(jsonObject, newLogoutAccountMsg); + EXPECT_EQ(jsonObject, nullptr); + EXPECT_EQ(newLogoutAccountMsg.userId, -1); +} + /** * @tc.name: PerformanceTest * @tc.type: FUNC @@ -203,5 +329,33 @@ HWTEST_F(DMTransportMsgTest, ToJson_UserIdsMsg, testing::ext::TestSize.Level1) cJSON_Delete(jsonObject); EXPECT_FALSE(userIdsMsg.foregroundUserIds.empty()); } + +/** + * @tc.name: GetCommMsgString_01 + * @tc.type: FUNC + */ +HWTEST_F(DMTransportMsgTest, GetCommMsgString_01, testing::ext::TestSize.Level1) +{ + const char* jsonstr = R"({"code":123,"msg":"messageinfo"})"; + std::string jsonObj(jsonstr); + CommMsg commMsg(123, "messageinfo"); + auto CommMsgString = GetCommMsgString(commMsg); + EXPECT_EQ(CommMsgString, jsonObj); +} + +/** + * @tc.name: ToString_01 + * @tc.type: FUNC + */ +HWTEST_F(DMTransportMsgTest, ToString_01, testing::ext::TestSize.Level1) +{ + const char* jsonstr = R"({"remoteUdid":"test_udid","foregroundUserIds":[10,20,30]})"; + std::string jsonObj(jsonstr); + std::string remoteUdid = "test_udid"; + std::vector userIds = {10, 20, 30}; + NotifyUserIds notifyUserIds(remoteUdid, userIds); + auto notifyUserIdsString = notifyUserIds.ToString(); + EXPECT_EQ(notifyUserIdsString, jsonObj); +} } // DistributedHardware } // OHOS \ No newline at end of file diff --git a/test/unittest/mock/dm_comm_tool_mock.cpp b/test/unittest/mock/dm_comm_tool_mock.cpp index 2a4f65eb896b37d0abd2c3d1f002953b301f00ed..28afdc998e2ac95d06f460877fd9509f404fe18b 100644 --- a/test/unittest/mock/dm_comm_tool_mock.cpp +++ b/test/unittest/mock/dm_comm_tool_mock.cpp @@ -29,5 +29,10 @@ int32_t DMCommTool::SendUserStop(const std::string rmtNetworkId, int32_t stopUse { return DmDMCommTool::dmDMCommTool->SendUserStop(rmtNetworkId, stopUserId); } + +int32_t DMCommTool::CreateUserStopMessage(int32_t stopUserId, std::string &msgStr) +{ + return DmDMCommTool::dmDMCommTool->CreateUserStopMessage(stopUserId, msgStr); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/dm_comm_tool_mock.h b/test/unittest/mock/dm_comm_tool_mock.h index 479247a2e901c67f7a853df4965392aaf337cd9c..735f8179c9dd4b1032e6c2543f8f3eac9116b0ea 100644 --- a/test/unittest/mock/dm_comm_tool_mock.h +++ b/test/unittest/mock/dm_comm_tool_mock.h @@ -29,6 +29,7 @@ public: virtual int32_t SendUserIds(const std::string rmtNetworkId, const std::vector &foregroundUserIds, const std::vector &backgroundUserIds) = 0; virtual int32_t SendUserStop(const std::string rmtNetworkId, int32_t stopUserId) = 0; + virtual int32_t CreateUserStopMessage(int32_t stopUserId, std::string &msgStr) = 0; public: static inline std::shared_ptr dmDMCommTool = nullptr; }; @@ -38,6 +39,7 @@ public: MOCK_METHOD(int32_t, SendUserIds, (const std::string, const std::vector &, const std::vector &)); MOCK_METHOD(int32_t, SendUserStop, (const std::string, int32_t)); + MOCK_METHOD(int32_t, CreateUserStopMessage, (int32_t stopUserId, std::string &msgStr)); }; } } diff --git a/test/unittest/mock/dm_transport_mock.cpp b/test/unittest/mock/dm_transport_mock.cpp index b54e806685cfbe5d33eebd0627539392411c185c..e9ce75fcc3628d53ee60eaf96ab4141f8dc7cfef 100644 --- a/test/unittest/mock/dm_transport_mock.cpp +++ b/test/unittest/mock/dm_transport_mock.cpp @@ -28,5 +28,9 @@ int32_t DMTransport::Send(const std::string &rmtNetworkId, const std::string &pa { return DmDMTransport::dMTransport_->Send(rmtNetworkId, payload, socketId); } +int32_t DMTransport::UnInit() +{ + return DmDMTransport::dMTransport_->UnInit(); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/dm_transport_mock.h b/test/unittest/mock/dm_transport_mock.h index 360c37c6c1cdef3333190404e393d19f1e75cc88..b65e4d045ffa2bc84226b78dc3300e1f564227c9 100644 --- a/test/unittest/mock/dm_transport_mock.h +++ b/test/unittest/mock/dm_transport_mock.h @@ -28,6 +28,7 @@ public: public: virtual int32_t StartSocket(const std::string &rmtNetworkId, int32_t &socketId) = 0; virtual int32_t Send(const std::string &rmtNetworkId, const std::string &payload, int32_t socketId) = 0; + virtual int32_t UnInit() = 0; public: static inline std::shared_ptr dMTransport_ = nullptr; }; @@ -36,6 +37,7 @@ class DMTransportMock : public DmDMTransport { public: MOCK_METHOD(int32_t, StartSocket, (const std::string &, int32_t &)); MOCK_METHOD(int32_t, Send, (const std::string &, const std::string &, int32_t)); + MOCK_METHOD(int32_t, UnInit, ()); }; } }