From 01d420199188f04a4da7b482b23d925e4f86ad37 Mon Sep 17 00:00:00 2001 From: BrainL Date: Wed, 23 Apr 2025 09:26:59 +0800 Subject: [PATCH 1/5] add UT Signed-off-by: BrainL --- test/unittest/UTTest_device_name_manager.cpp | 261 +++++++++++++++ test/unittest/UTTest_dm_comm_tool.cpp | 323 +++++++++++++++++++ test/unittest/UTTest_dm_comm_tool.h | 2 + test/unittest/mock/dm_comm_tool_mock.cpp | 5 + test/unittest/mock/dm_comm_tool_mock.h | 2 + test/unittest/mock/dm_transport_mock.cpp | 4 + test/unittest/mock/dm_transport_mock.h | 2 + 7 files changed, 599 insertions(+) diff --git a/test/unittest/UTTest_device_name_manager.cpp b/test/unittest/UTTest_device_name_manager.cpp index 409f64894..127cce257 100644 --- a/test/unittest/UTTest_device_name_manager.cpp +++ b/test/unittest/UTTest_device_name_manager.cpp @@ -324,5 +324,266 @@ 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); +} + } // DistributedHardware } // OHOS diff --git a/test/unittest/UTTest_dm_comm_tool.cpp b/test/unittest/UTTest_dm_comm_tool.cpp index 577923925..1fec5a40e 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 5fae452d0..a24fe4d26 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/mock/dm_comm_tool_mock.cpp b/test/unittest/mock/dm_comm_tool_mock.cpp index 2a4f65eb8..28afdc998 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 479247a2e..735f8179c 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 b54e80668..e9ce75fcc 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 360c37c6c..b65e4d045 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, ()); }; } } -- Gitee From 760c0a84a4a8507a89a06c2bfab36b07d4ce5b30 Mon Sep 17 00:00:00 2001 From: BrainL Date: Wed, 23 Apr 2025 10:34:02 +0800 Subject: [PATCH 2/5] add UT Signed-off-by: BrainL --- test/unittest/BUILD.gn | 24 ++++ test/unittest/UTTest_dm_auth_state.cpp | 74 +++++++++++ test/unittest/UTTest_dm_auth_state.h | 36 +++++ test/unittest/UTTest_dm_transport_msg.cpp | 155 ++++++++++++++++++++++ 4 files changed, 289 insertions(+) create mode 100644 test/unittest/UTTest_dm_auth_state.cpp create mode 100644 test/unittest/UTTest_dm_auth_state.h diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 51ab64aef..cc2546a1c 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_comm_tool", ":UTTest_dm_common_event_manager", ":UTTest_dm_credential_manager", @@ -1499,6 +1500,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_dm_auth_state.cpp b/test/unittest/UTTest_dm_auth_state.cpp new file mode 100644 index 000000000..2e35fba88 --- /dev/null +++ b/test/unittest/UTTest_dm_auth_state.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * 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_log.h" +#include "dm_anonymous.h" +#include "dm_constants.h" +#include "dm_auth_message_processor.h" +#include "dm_auth_context.h" +#include "dm_auth_state_machine.h" +#include "UTTest_dm_auth_state.h" + +using namespace testing; +namespace OHOS { +namespace DistributedHardware { + +void DmAuthMessageProcessorTest::SetUpTestCase() +{ +} + +void DmAuthMessageProcessorTest::TearDownTestCase() +{ +} + +void DmAuthMessageProcessorTest::SetUp() +{ +} + +void DmAuthMessageProcessorTest::TearDown() +{ +} + +HWTEST_F(DmAuthStateTest, GetTaskTimeout_001, testing::ext::TestSize.Level1) +{ + const char *taskName = "taskName"; + int32_t taskTimeOut = 6; + std::shared_ptr context = std::make_shared(); + context->authType = AUTH_TYPE_UNKNOW; + std::shared_ptr state = std::make_shared(); + EXPECT_EQ(state->GetTaskTimeout(context, taskName, taskTimeOut), taskTimeOut); +} + +HWTEST_F(DmAuthStateTest, GetTaskTimeout_002, testing::ext::TestSize.Level1) +{ + const char *taskName = "deviceManagerTimer:authenticate"; + int32_t taskTimeOut = 6; + std::shared_ptr context = std::make_shared(); + context->authType = AUTH_TYPE_NFC; + std::shared_ptr state = std::make_shared(); + EXPECT_EQ(state->GetTaskTimeout(context, taskName, taskTimeOut), 20); +} + +HWTEST_F(DmAuthStateTest, GetTaskTimeout_003, testing::ext::TestSize.Level1) +{ + const char *taskName = "test"; + int32_t taskTimeOut = 6; + std::shared_ptr context = std::make_shared(); + context->authType = AUTH_TYPE_NFC; + std::shared_ptr state = std::make_shared(); + EXPECT_EQ(state->GetTaskTimeout(context, taskName, taskTimeOut), taskTimeOut); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/UTTest_dm_auth_state.h b/test/unittest/UTTest_dm_auth_state.h new file mode 100644 index 000000000..c584458f7 --- /dev/null +++ b/test/unittest/UTTest_dm_auth_state.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*/ + +#ifndef OHOS_DM_AUTH_STATE_TEST_H +#define OHOS_DM_AUTH_STATE_TEST_H + +#include +#include +#include +#include "json_object.h" + +namespace OHOS { +namespace DistributedHardware { +class DmAuthStateTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_AUTH_STATE_TEST_H + \ No newline at end of file diff --git a/test/unittest/UTTest_dm_transport_msg.cpp b/test/unittest/UTTest_dm_transport_msg.cpp index 1e662ffd1..f1ded91c0 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,115 @@ 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 +330,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 -- Gitee From 3e8cf6a85ffa4ef9166ab9c280ae2c0a5a1e93b6 Mon Sep 17 00:00:00 2001 From: BrainL Date: Wed, 23 Apr 2025 10:48:29 +0800 Subject: [PATCH 3/5] modify format Signed-off-by: BrainL --- test/unittest/UTTest_dm_transport_msg.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unittest/UTTest_dm_transport_msg.cpp b/test/unittest/UTTest_dm_transport_msg.cpp index f1ded91c0..e5548295f 100644 --- a/test/unittest/UTTest_dm_transport_msg.cpp +++ b/test/unittest/UTTest_dm_transport_msg.cpp @@ -176,7 +176,6 @@ HWTEST_F(DMTransportMsgTest, ToJsonAndFromJson02_Invaild, testing::ext::TestSize EXPECT_EQ(jsonObject, nullptr); EXPECT_EQ(newNotifyUserIds.remoteUdid, ""); EXPECT_EQ(newNotifyUserIds.userIds.size(), 0); - } /** -- Gitee From 5cf48c2abd7a99a1580cfdda98abba63dd3087ba Mon Sep 17 00:00:00 2001 From: BrainL Date: Wed, 23 Apr 2025 11:44:07 +0800 Subject: [PATCH 4/5] add UT Signed-off-by: BrainL --- test/unittest/UTTest_device_name_manager.cpp | 82 ++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/test/unittest/UTTest_device_name_manager.cpp b/test/unittest/UTTest_device_name_manager.cpp index 127cce257..8dddb5787 100644 --- a/test/unittest/UTTest_device_name_manager.cpp +++ b/test/unittest/UTTest_device_name_manager.cpp @@ -585,5 +585,87 @@ HWTEST_F(DeviceNameManagerTest, SetUserDefinedDeviceName_001, testing::ext::Test 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 -- Gitee From d312e321f35ac96c223d2df4cfed9d4c41c99472 Mon Sep 17 00:00:00 2001 From: BrainL Date: Wed, 23 Apr 2025 14:28:38 +0800 Subject: [PATCH 5/5] modify Signed-off-by: BrainL --- test/unittest/UTTest_dm_auth_state.cpp | 74 -------------------------- test/unittest/UTTest_dm_auth_state.h | 36 ------------- 2 files changed, 110 deletions(-) delete mode 100644 test/unittest/UTTest_dm_auth_state.cpp delete mode 100644 test/unittest/UTTest_dm_auth_state.h diff --git a/test/unittest/UTTest_dm_auth_state.cpp b/test/unittest/UTTest_dm_auth_state.cpp deleted file mode 100644 index 2e35fba88..000000000 --- a/test/unittest/UTTest_dm_auth_state.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2022-2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * 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_log.h" -#include "dm_anonymous.h" -#include "dm_constants.h" -#include "dm_auth_message_processor.h" -#include "dm_auth_context.h" -#include "dm_auth_state_machine.h" -#include "UTTest_dm_auth_state.h" - -using namespace testing; -namespace OHOS { -namespace DistributedHardware { - -void DmAuthMessageProcessorTest::SetUpTestCase() -{ -} - -void DmAuthMessageProcessorTest::TearDownTestCase() -{ -} - -void DmAuthMessageProcessorTest::SetUp() -{ -} - -void DmAuthMessageProcessorTest::TearDown() -{ -} - -HWTEST_F(DmAuthStateTest, GetTaskTimeout_001, testing::ext::TestSize.Level1) -{ - const char *taskName = "taskName"; - int32_t taskTimeOut = 6; - std::shared_ptr context = std::make_shared(); - context->authType = AUTH_TYPE_UNKNOW; - std::shared_ptr state = std::make_shared(); - EXPECT_EQ(state->GetTaskTimeout(context, taskName, taskTimeOut), taskTimeOut); -} - -HWTEST_F(DmAuthStateTest, GetTaskTimeout_002, testing::ext::TestSize.Level1) -{ - const char *taskName = "deviceManagerTimer:authenticate"; - int32_t taskTimeOut = 6; - std::shared_ptr context = std::make_shared(); - context->authType = AUTH_TYPE_NFC; - std::shared_ptr state = std::make_shared(); - EXPECT_EQ(state->GetTaskTimeout(context, taskName, taskTimeOut), 20); -} - -HWTEST_F(DmAuthStateTest, GetTaskTimeout_003, testing::ext::TestSize.Level1) -{ - const char *taskName = "test"; - int32_t taskTimeOut = 6; - std::shared_ptr context = std::make_shared(); - context->authType = AUTH_TYPE_NFC; - std::shared_ptr state = std::make_shared(); - EXPECT_EQ(state->GetTaskTimeout(context, taskName, taskTimeOut), taskTimeOut); -} -} // namespace DistributedHardware -} // namespace OHOS diff --git a/test/unittest/UTTest_dm_auth_state.h b/test/unittest/UTTest_dm_auth_state.h deleted file mode 100644 index c584458f7..000000000 --- a/test/unittest/UTTest_dm_auth_state.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. -*/ - -#ifndef OHOS_DM_AUTH_STATE_TEST_H -#define OHOS_DM_AUTH_STATE_TEST_H - -#include -#include -#include -#include "json_object.h" - -namespace OHOS { -namespace DistributedHardware { -class DmAuthStateTest : public testing::Test { -public: - static void SetUpTestCase(); - static void TearDownTestCase(); - void SetUp(); - void TearDown(); -}; -} // namespace DistributedHardware -} // namespace OHOS -#endif // OHOS_DM_AUTH_STATE_TEST_H - \ No newline at end of file -- Gitee