From c86d8f86e9b4d3bf52d46a681e7d44dd86d0811b Mon Sep 17 00:00:00 2001 From: wufengshan Date: Tue, 27 May 2025 15:22:21 +0800 Subject: [PATCH 1/2] add general UT Signed-off-by: wufengshan --- tests/core/connection/general/BUILD.gn | 1 + .../general/general_connection_mock.cpp | 34 +- .../general/general_connection_mock.h | 5 +- .../softbus_conn_general_connection_test.cpp | 328 ++++++++++++++++++ 4 files changed, 361 insertions(+), 7 deletions(-) diff --git a/tests/core/connection/general/BUILD.gn b/tests/core/connection/general/BUILD.gn index 360d68dcc5..21ec96b2b0 100644 --- a/tests/core/connection/general/BUILD.gn +++ b/tests/core/connection/general/BUILD.gn @@ -40,6 +40,7 @@ ohos_unittest("GeneralConnectionTest") { "$dsoftbus_root_path/core/connection/tcp/include", "$dsoftbus_root_path/core/connection/sle/include", "$dsoftbus_root_path/core/frame/init/include", + "$dsoftbus_root_path/adapter/common/include", ] deps = [ diff --git a/tests/core/connection/general/general_connection_mock.cpp b/tests/core/connection/general/general_connection_mock.cpp index 2f55e4fa94..676baf05cc 100644 --- a/tests/core/connection/general/general_connection_mock.cpp +++ b/tests/core/connection/general/general_connection_mock.cpp @@ -20,6 +20,9 @@ using namespace testing::ext; namespace OHOS { void *g_connectionGeneralInterface; +static ConnectCallback g_connectCallback = {0}; +static ConnectResult g_connectResult = {0}; + GeneralConnectionInterfaceMock::GeneralConnectionInterfaceMock() { g_connectionGeneralInterface = reinterpret_cast(this); @@ -30,6 +33,16 @@ GeneralConnectionInterfaceMock::~GeneralConnectionInterfaceMock() g_connectionGeneralInterface = nullptr; } +ConnectCallback *GeneralConnectionInterfaceMock::GetConnectCallbackMock() +{ + return &g_connectCallback; +} + +ConnectResult *GeneralConnectionInterfaceMock::GetConnectResultMock() +{ + return &g_connectResult; +} + static GeneralConnectionInterface *GetGeneralConnectionInterface() { return reinterpret_cast(g_connectionGeneralInterface); @@ -78,6 +91,20 @@ int32_t ClientIpcOnDataReceived(const char *pkgName, int32_t pid, uint32_t handl return SOFTBUS_OK; } +ConnBleConnection *ConnBleGetConnectionById(uint32_t connectionId) +{ + static ConnBleConnection connection = { + .featureBitSet = 0, + .protocol = BLE_COC, + }; + + (void)memcpy_s(connection.networkId, UDID_BUF_LEN, "testNetworkId", UDID_BUF_LEN); + (void)memcpy_s(connection.udid, UDID_BUF_LEN, "testnetUdid", UDID_BUF_LEN); + const char *addr = "11:22:33:44:55:66"; + (void)memcpy_s(connection.addr, BT_MAC_LEN, addr, BT_MAC_LEN); + return &connection; +} + ConnectFuncInterface *ConnInitTcp(const ConnectCallback *callback) { (void)callback; @@ -96,11 +123,6 @@ ConnectFuncInterface *ConnSleInit(const ConnectCallback *callback) return NULL; } -ConnBleConnection *ConnBleGetConnectionById(uint32_t connectionId) -{ - return GetGeneralConnectionInterface()->ConnBleGetConnectionById(connectionId); -} - int32_t LnnGetLocalStrInfo(InfoKey key, char *info, uint32_t len) { return GetGeneralConnectionInterface()->LnnGetLocalStrInfo(key, info, len); @@ -108,6 +130,7 @@ int32_t LnnGetLocalStrInfo(InfoKey key, char *info, uint32_t len) int32_t BleConnectDeviceMock(const ConnectOption *option, uint32_t requestId, const ConnectResult *result) { + g_connectResult = *result; return GetGeneralConnectionInterface()->BleConnectDeviceMock(option, requestId, result); } @@ -123,6 +146,7 @@ ConnectFuncInterface *ConnInitBle(const ConnectCallback *callback) .ConnectDevice = BleConnectDeviceMock, .PostBytes = ConnBlePostBytesMock, }; + g_connectCallback= *callback; return &bleFuncInterface; } } diff --git a/tests/core/connection/general/general_connection_mock.h b/tests/core/connection/general/general_connection_mock.h index b464b5a6e3..25db281fdb 100644 --- a/tests/core/connection/general/general_connection_mock.h +++ b/tests/core/connection/general/general_connection_mock.h @@ -27,7 +27,6 @@ class GeneralConnectionInterface { public: GeneralConnectionInterface() {}; virtual ~GeneralConnectionInterface() {}; - virtual ConnBleConnection *ConnBleGetConnectionById(uint32_t connectionId) = 0; virtual int32_t LnnGetLocalStrInfo(InfoKey key, char *info, uint32_t len) = 0; virtual int32_t BleConnectDeviceMock(const ConnectOption *option, uint32_t requestId, const ConnectResult *result) = 0; @@ -39,12 +38,14 @@ class GeneralConnectionInterfaceMock : public GeneralConnectionInterface { public: GeneralConnectionInterfaceMock(); ~GeneralConnectionInterfaceMock() override; - MOCK_METHOD(ConnBleConnection *, ConnBleGetConnectionById, (uint32_t), (override)); MOCK_METHOD(int32_t, LnnGetLocalStrInfo, (InfoKey key, char *info, uint32_t len), (override)); MOCK_METHOD(int32_t, BleConnectDeviceMock, (const ConnectOption *option, uint32_t requestId, const ConnectResult *result), (override)); MOCK_METHOD(int32_t, ConnBlePostBytesMock, (uint32_t connectionId, uint8_t *data, uint32_t dataLen, int32_t pid, int32_t flag, int32_t module, int64_t seq), (override)); + + static ConnectCallback *GetConnectCallbackMock(); + static ConnectResult *GetConnectResultMock(); }; } // namespace OHOS #endif // GENERAL_CONNECTION_MOCK_H \ No newline at end of file diff --git a/tests/core/connection/general/softbus_conn_general_connection_test.cpp b/tests/core/connection/general/softbus_conn_general_connection_test.cpp index 962001627e..9b49a9b649 100644 --- a/tests/core/connection/general/softbus_conn_general_connection_test.cpp +++ b/tests/core/connection/general/softbus_conn_general_connection_test.cpp @@ -19,6 +19,7 @@ #include "softbus_conn_general_connection.h" #include "softbus_conn_ipc.h" #include "softbus_feature_config.h" +#include "softbus_adapter_mem.h" using namespace testing::ext; using namespace testing; @@ -26,6 +27,12 @@ using namespace std; #define GENERAL_PKGNAME_MAX_COUNT (10) +static ConnectCallback *g_ConnectCallback = nullptr; +static uint32_t g_handle = 0; +static uint32_t g_ConnectionId = 0; +static bool g_isRecvNewConnection = false; +static uint32_t g_isServerGeneralId = 0; + namespace OHOS { class GeneralConnectionTest : public testing::Test { public: @@ -45,6 +52,72 @@ void GeneralConnectionTest::SetUp(void) { } void GeneralConnectionTest::TearDown(void) { } +static OutData *PackReciveData(const uint8_t *data, uint32_t dataLen, uint32_t localId, uint32_t peerId) +{ + OutData *outData = (OutData *)SoftBusCalloc(sizeof(OutData)); + CONN_CHECK_AND_RETURN_RET_LOGE(outData != nullptr, nullptr, CONN_BLE, "malloc outData err"); + outData->dataLen = GENERAL_CONNECTION_HEADER_SIZE + dataLen; + outData->data = (uint8_t *)SoftBusCalloc(outData->dataLen); + if (outData->data == nullptr) { + SoftBusFree(outData); + return nullptr; + } + GeneralConnectionHead *header = (GeneralConnectionHead *)outData->data; + header->headLen = GENERAL_CONNECTION_HEADER_SIZE; + header->localId = localId; + header->peerId = peerId; + header->msgType = GENERAL_CONNECTION_MSG_TYPE_NORMAL; + + if (memcpy_s(outData->data + GENERAL_CONNECTION_HEADER_SIZE, + outData->dataLen - GENERAL_CONNECTION_HEADER_SIZE, data, dataLen) != EOK) { + FreeOutData(outData); + outData = nullptr; + } + + uint32_t size = ConnGetHeadSize(); + uint32_t totalLen = outData->dataLen + size; + + OutData *dataOut = (OutData *)SoftBusCalloc(sizeof(OutData)); + if (dataOut == nullptr) { + FreeOutData(outData); + return nullptr; + } + dataOut->dataLen = totalLen; + dataOut->data = (uint8_t *)SoftBusCalloc(totalLen); + int32_t ret = memcpy_s(dataOut->data + size, outData->dataLen, outData->data, outData->dataLen); + if (ret != EOK) { + FreeOutData(outData); + FreeOutData(dataOut); + return nullptr; + } + return dataOut; +} + +static ConnPostData *PackInnerMsg(GeneralConnectionInfo *info, GeneralConnectionMsgType msgType, int32_t module) +{ + OutData *data = GeneralConnectionPackMsg(info, msgType); + EXPECT_NE(data, nullptr); + + uint32_t size = ConnGetHeadSize(); + + static ConnPostData buff = {0}; + buff.seq = 0; + buff.flag = CONN_HIGH; + buff.pid = 0; + + buff.len = data->dataLen + size; + buff.buf = (char *)SoftBusCalloc(buff.len); + buff.module = module; + + if (buff.buf == NULL || memcpy_s(buff.buf + size, data->dataLen, data->data, data->dataLen) != EOK) { + SoftBusFree(buff.buf); + FreeOutData(data); + return nullptr; + } + return &buff; +} + + /* * @tc.name: TestInit * @tc.desc: test init general connection @@ -72,6 +145,8 @@ HWTEST_F(GeneralConnectionTest, TestInit, TestSize.Level1) EXPECT_NE(manager, nullptr); GeneralConnectionParam param = {0}; manager->closeServer(¶m); + g_ConnectCallback = GeneralConnectionInterfaceMock::GetConnectCallbackMock(); + EXPECT_NE(g_ConnectCallback, nullptr); CONN_LOGI(CONN_BLE, "test init end"); } @@ -171,4 +246,257 @@ HWTEST_F(GeneralConnectionTest, TestConnect, TestSize.Level1) EXPECT_EQ(ret, SOFTBUS_CONN_GENERAL_CONNECT_FAILED); CONN_LOGI(CONN_BLE, "test connect end"); } + +/* +* @tc.name: test send +* @tc.desc: test send +* @tc.type: FUNC +* @tc.require:AR000GIRGE +*/ +HWTEST_F(GeneralConnectionTest, TestSend, TestSize.Level1) +{ + CONN_LOGI(CONN_BLE, "test send start"); + GeneralConnectionManager *manager = GetGeneralConnectionManager(); + EXPECT_NE(manager, nullptr); + GeneralConnectionParam param = {0}; + const char *pkgName = "testPkgName"; + int32_t ret = strcpy_s(param.pkgName, PKG_NAME_SIZE_MAX, pkgName); + EXPECT_EQ(ret, EOK); + ret = strcpy_s(param.bundleName, BUNDLE_NAME_MAX, "testBundleName"); + EXPECT_EQ(ret, EOK); + ret = strcpy_s(param.name, GENERAL_NAME_LEN, "test"); + EXPECT_EQ(ret, EOK); + const char *addr = "11:22:33:44:55:66"; + param.pid = 0; + NiceMock mock; + EXPECT_CALL(mock, BleConnectDeviceMock).WillRepeatedly(Return(SOFTBUS_OK)); + g_handle = manager->connect(¶m, addr); + EXPECT_EQ(g_handle > 0, true); + uint8_t *data = (uint8_t *)malloc(sizeof(uint8_t)); + EXPECT_NE(data, nullptr); + ret = manager->send(g_handle, data, 0, 0); + EXPECT_EQ(ret, SOFTBUS_CONN_GENERAL_CONNECTION_NOT_READY); + GeneralConnectionInfo info = {{0}}; + info.ackStatus = 0; + info.localId = 0; + info.peerId = g_handle; + info.abilityBitSet = 0; + OutData *dataRecv = GeneralConnectionPackMsg(&info, GENERAL_CONNECTION_MSG_TYPE_HANDSHAKE_ACK); + EXPECT_NE(dataRecv, nullptr); + uint32_t size = ConnGetHeadSize(); + uint32_t dataLen = dataRecv->dataLen + size; + char *buff = (char *)SoftBusCalloc(dataLen); + EXPECT_NE(buff, nullptr); + ret = memcpy_s(buff + size, dataRecv->dataLen, dataRecv->data, dataRecv->dataLen); + EXPECT_EQ(ret, EOK); + g_ConnectCallback->OnDataReceived(0, MODULE_BLE_GENERAL, 0, buff, dataRecv->dataLen); + g_ConnectCallback->OnDataReceived(0, MODULE_BLE_GENERAL, 0, (char *)dataRecv->data, dataLen); + g_ConnectionId = (CONNECT_BLE << CONNECT_TYPE_SHIFT); + ConnectResult *connectResult = GeneralConnectionInterfaceMock::GetConnectResultMock(); + uint32_t requestId = 12; + ConnectionInfo infos = {0}; + connectResult->OnConnectSuccessed(requestId, g_ConnectionId, &infos); + g_ConnectCallback->OnDataReceived(g_ConnectionId, MODULE_BLE_GENERAL, 0, buff, dataLen); // state change to success + EXPECT_CALL(mock, ConnBlePostBytesMock).WillRepeatedly(Return(SOFTBUS_OK)); + ret = manager->send(g_handle, data, sizeof(uint8_t), 0); + EXPECT_EQ(ret, SOFTBUS_OK); + SoftBusFree(data); + CONN_LOGI(CONN_BLE, "test send end"); +} + +/* +* @tc.name: test recv +* @tc.desc: test recv normal message +* @tc.type: FUNC +* @tc.require:AR000GIRGE +*/ +HWTEST_F(GeneralConnectionTest, TestRecv, TestSize.Level1) +{ + CONN_LOGI(CONN_BLE, "test recv start"); + GeneralConnectionManager *manager = GetGeneralConnectionManager(); + EXPECT_NE(manager, nullptr); + uint8_t *data = (uint8_t *)malloc(sizeof(uint8_t)); + EXPECT_NE(data, nullptr); + g_ConnectCallback->OnDataReceived(0, MODULE_BLE_CONN, 0, (char *)data, GENERAL_CONNECTION_HEADER_SIZE + 1); + OutData *dataRecv = PackReciveData(data, sizeof(uint8_t), 0, g_handle); + EXPECT_NE(dataRecv, nullptr); + g_ConnectCallback->OnDataReceived(g_ConnectionId, MODULE_BLE_GENERAL, 0, (char *)dataRecv->data, dataRecv->dataLen); + CONN_LOGI(CONN_BLE, "test recv end"); +} + +/* +* @tc.name: Test GetPeerDeviceId +* @tc.desc: test recv normal message +* @tc.type: FUNC +* @tc.require:AR000GIRGE +*/ +HWTEST_F(GeneralConnectionTest, TestGetPeerDeviceId, TestSize.Level1) +{ + CONN_LOGI(CONN_BLE, "test get peer deviceId start"); + GeneralConnectionManager *manager = GetGeneralConnectionManager(); + EXPECT_NE(manager, nullptr); + + char addr[BT_MAC_LEN] = {0}; + int32_t ret = manager->getPeerDeviceId(g_handle, addr, BT_MAC_LEN - 1, 0); + EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); + + ret = manager->getPeerDeviceId(g_handle, addr, BT_MAC_LEN, 0); + EXPECT_NE(ret, SOFTBUS_OK); + CONN_LOGI(CONN_BLE, "test get peer deviceId end"); +} + +/* +* @tc.name: Test OnConnectDisconnected +* @tc.desc: test OnConnectDisconnected +* @tc.type: FUNC +* @tc.require:AR000GIRGE +*/ +HWTEST_F(GeneralConnectionTest, TestOnConnectDisconnected, TestSize.Level1) +{ + CONN_LOGI(CONN_BLE, "test on connect disconnect start"); + GeneralConnectionManager *manager = GetGeneralConnectionManager(); + EXPECT_NE(manager, nullptr); + + GeneralConnectionParam param = {0}; + const char *pkgName = "testPkgName1"; + int32_t ret = strcpy_s(param.pkgName, PKG_NAME_SIZE_MAX, pkgName); + EXPECT_EQ(ret, EOK); + ret = strcpy_s(param.bundleName, BUNDLE_NAME_MAX, "testBundleName1"); + EXPECT_EQ(ret, EOK); + ret = strcpy_s(param.name, GENERAL_NAME_LEN, "test1"); + EXPECT_EQ(ret, EOK); + const char *addr = "11:22:33:44:55:66"; + param.pid = 0; + NiceMock mock; + EXPECT_CALL(mock, BleConnectDeviceMock).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t handle = manager->connect(¶m, addr); + EXPECT_EQ(handle > 0, true); + + ConnectResult *connectResult = GeneralConnectionInterfaceMock::GetConnectResultMock(); + uint32_t requestId = 13; + ConnectionInfo infos = {0}; + uint32_t connectionId = (CONNECT_BLE << CONNECT_TYPE_SHIFT) + 1; + connectResult->OnConnectSuccessed(requestId, connectionId, &infos); + + GeneralConnectionInfo info = { + .peerId = handle, + }; + ConnPostData *data = PackInnerMsg(&info, GENERAL_CONNECTION_MSG_TYPE_RESET, MODULE_BLE_GENERAL); + EXPECT_NE(data, nullptr); + g_ConnectCallback->OnDataReceived(connectionId, MODULE_BLE_GENERAL, 0, data->buf, data->len); + + //g_ConnectCallback->OnDisconnected(g_ConnectionId, &infos); + //g_ConnectCallback->OnDisconnected(g_ConnectionId, &infos); 最后一个用例的时候再调用, 这个全局变量后面还要用, 两次是走不同的分支。 + CONN_LOGI(CONN_BLE, "test on connect disconnect end"); +} + +void ConnectSuccess(GeneralConnectionParam *info, uint32_t generalHandle) +{ + (void)info; + (void)generalHandle; +} + +void ConnectFailed(GeneralConnectionParam *info, uint32_t generalHandle, int32_t reason) +{ + (void)info; + (void)generalHandle; + (void)reason; +} + +void AcceptConnect(GeneralConnectionParam *info, uint32_t generalHandle) +{ + CONN_LOGI(CONN_BLE, "test AcceptConnect generalHandle=%{public}u", generalHandle); + (void)info; + (void)generalHandle; + g_isRecvNewConnection = true; + g_isServerGeneralId = generalHandle; +} + +void DataReceived(GeneralConnectionParam *info, uint32_t generalHandle, const uint8_t *data, uint32_t dataLen) +{ + (void)info; + (void)generalHandle; + (void)data; + (void)dataLen; +} + +void ConnectionDisconnected(GeneralConnectionParam *info, uint32_t generalHandle, int32_t reason) +{ + (void)info; + (void)generalHandle; + (void)reason; +} + +/* +* @tc.name: TestRegisterListener +* @tc.desc: test register listener +* @tc.type: FUNC +* @tc.require:AR000GIRGE +*/ +HWTEST_F(GeneralConnectionTest, TestRegisterListener, TestSize.Level1) +{ + CONN_LOGI(CONN_BLE, "test register listener start"); + + GeneralConnectionManager *manager = GetGeneralConnectionManager(); + EXPECT_NE(manager, nullptr); + + GeneralConnectionListener listener = { + .onConnectSuccess = ConnectSuccess, + .onConnectFailed = ConnectFailed, + .onAcceptConnect = AcceptConnect, + .onDataReceived = DataReceived, + .onConnectionDisconnected = ConnectionDisconnected, + }; + + int32_t ret = manager->registerListener(&listener); + EXPECT_EQ(ret, SOFTBUS_OK); + + CONN_LOGI(CONN_BLE, "test register listener end"); +} + +/* +* @tc.name: TestRecvNewConnection +* @tc.desc: test recv GENERAL_CONNECTION_MSG_TYPE_HANDSHAKE +* @tc.type: FUNC +* @tc.require:AR000GIRGE +*/ +HWTEST_F(GeneralConnectionTest, TestRecvNewConnection, TestSize.Level1) +{ + CONN_LOGI(CONN_BLE, "test recv new connection start "); + NiceMock mock; + EXPECT_CALL(mock, ConnBlePostBytesMock).WillRepeatedly(Return(SOFTBUS_OK)); + + // test recv peer connect and not create server + uint32_t handle = 199657; + uint32_t connectionId = (CONNECT_BLE << CONNECT_TYPE_SHIFT) + 1; + GeneralConnectionInfo info = { + .peerId = handle, + .name = "test", + .bundleName = "testApp", + }; + ConnPostData *data = PackInnerMsg(&info, GENERAL_CONNECTION_MSG_TYPE_HANDSHAKE, MODULE_BLE_GENERAL); + g_ConnectCallback->OnDataReceived(connectionId, MODULE_BLE_GENERAL, 0, data->buf, data->len); + EXPECT_EQ(g_isRecvNewConnection, false); + + // test recv peer connect and notify upperLayer success + GeneralConnectionParam param = { + .name = "test", + .bundleName = "testApp", + }; + GeneralConnectionManager *manager = GetGeneralConnectionManager(); + EXPECT_NE(manager, nullptr); + manager->createServer(¶m); + g_ConnectCallback->OnDataReceived(connectionId, MODULE_BLE_GENERAL, 0, data->buf, data->len); + EXPECT_EQ(g_isRecvNewConnection, true); + CONN_LOGI(CONN_BLE, "test recv new connection end"); + + // test recv merge message + GeneralConnectionInfo info = { + .peerId = g_isServerGeneralId, + .updateHandle = 222, + }; + data = PackInnerMsg(&info, GENERAL_CONNECTION_MSG_TYPE_MERGE, MODULE_BLE_GENERAL); + g_ConnectCallback->OnDataReceived(connectionId, MODULE_BLE_GENERAL, 0, data->buf, data->len); +} + } \ No newline at end of file -- Gitee From fb75d73db7c1395dab162e35cbac591f08e8eb58 Mon Sep 17 00:00:00 2001 From: wufengshan Date: Thu, 5 Jun 2025 21:50:20 +0800 Subject: [PATCH 2/2] fix dataLen to uint and add ut Signed-off-by: wufengshan --- .../general/softbus_conn_general_connection.c | 2 +- .../softbus_conn_general_negotiation.h | 2 +- tests/core/connection/general/BUILD.gn | 6 +- .../general/general_connection_mock.cpp | 10 +- .../general/general_connection_mock.h | 4 +- .../softbus_conn_general_connection_test.cpp | 202 ++++++++++-------- 6 files changed, 128 insertions(+), 98 deletions(-) diff --git a/core/connection/general/softbus_conn_general_connection.c b/core/connection/general/softbus_conn_general_connection.c index 85bca908d8..5bdbcab504 100644 --- a/core/connection/general/softbus_conn_general_connection.c +++ b/core/connection/general/softbus_conn_general_connection.c @@ -1028,7 +1028,7 @@ static void OnCommDataReceived(uint32_t connectionId, ConnModule moduleId, int64 GeneralConnectionHead *head = (GeneralConnectionHead *)data; GeneralConnectionMsgType msgType = head->msgType; if (msgType >= GENERAL_CONNECTION_MSG_TYPE_MAX || len < head->headLen) { - CONN_LOGE(CONN_BLE, "invalid msgType, msgType=%{public}d, msgType=%{public}d, msgType=%{public}d", + CONN_LOGE(CONN_BLE, "invalid msgType, msgType=%{public}d, len=%{public}d, headLen=%{public}u", msgType, len, head->headLen); return; } diff --git a/core/connection/general/softbus_conn_general_negotiation.h b/core/connection/general/softbus_conn_general_negotiation.h index 2c5f58f054..e6d91204b8 100644 --- a/core/connection/general/softbus_conn_general_negotiation.h +++ b/core/connection/general/softbus_conn_general_negotiation.h @@ -41,7 +41,7 @@ typedef struct { int32_t msgType; int32_t localId; int32_t peerId; - int32_t headLen; + uint32_t headLen; } GeneralConnectionHead; typedef uint32_t GeneralConnAbilityBitSet; diff --git a/tests/core/connection/general/BUILD.gn b/tests/core/connection/general/BUILD.gn index 21ec96b2b0..4a38aef921 100644 --- a/tests/core/connection/general/BUILD.gn +++ b/tests/core/connection/general/BUILD.gn @@ -61,10 +61,10 @@ ohos_unittest("GeneralConnectionTest") { "$dsoftbus_root_path/core/connection/general/softbus_conn_general_negotiation.c", "$dsoftbus_root_path/core/connection/manager/softbus_conn_ipc.c", "$dsoftbus_root_path/core/connection/manager/softbus_conn_manager.c", + "$dsoftbus_root_path/core/frame/init/src/g_enhance_conn_func.c", + "$dsoftbus_root_path/core/frame/init/src/g_enhance_conn_func_pack.c", "general_connection_mock.cpp", "softbus_conn_general_connection_test.cpp", - "$dsoftbus_root_path/core/frame/init/src/g_enhance_conn_func_pack.c", - "$dsoftbus_root_path/core/frame/init/src/g_enhance_conn_func.c", ] if (dsoftbus_feature_conn_br) { @@ -123,4 +123,4 @@ ohos_unittest("GeneralNegotiationTest") { } else { sources += [ "$dsoftbus_root_path/adapter/common/net/bluetooth/common/softbus_adapter_bt_common_virtual.c" ] } -} \ No newline at end of file +} diff --git a/tests/core/connection/general/general_connection_mock.cpp b/tests/core/connection/general/general_connection_mock.cpp index 676baf05cc..37c8eb4e43 100644 --- a/tests/core/connection/general/general_connection_mock.cpp +++ b/tests/core/connection/general/general_connection_mock.cpp @@ -105,6 +105,11 @@ ConnBleConnection *ConnBleGetConnectionById(uint32_t connectionId) return &connection; } +int32_t LnnGetLocalStrInfo(InfoKey key, char *info, uint32_t len) +{ + return SOFTBUS_INVALID_PARAM; +} + ConnectFuncInterface *ConnInitTcp(const ConnectCallback *callback) { (void)callback; @@ -123,11 +128,6 @@ ConnectFuncInterface *ConnSleInit(const ConnectCallback *callback) return NULL; } -int32_t LnnGetLocalStrInfo(InfoKey key, char *info, uint32_t len) -{ - return GetGeneralConnectionInterface()->LnnGetLocalStrInfo(key, info, len); -} - int32_t BleConnectDeviceMock(const ConnectOption *option, uint32_t requestId, const ConnectResult *result) { g_connectResult = *result; diff --git a/tests/core/connection/general/general_connection_mock.h b/tests/core/connection/general/general_connection_mock.h index 25db281fdb..194c04f57f 100644 --- a/tests/core/connection/general/general_connection_mock.h +++ b/tests/core/connection/general/general_connection_mock.h @@ -27,7 +27,6 @@ class GeneralConnectionInterface { public: GeneralConnectionInterface() {}; virtual ~GeneralConnectionInterface() {}; - virtual int32_t LnnGetLocalStrInfo(InfoKey key, char *info, uint32_t len) = 0; virtual int32_t BleConnectDeviceMock(const ConnectOption *option, uint32_t requestId, const ConnectResult *result) = 0; virtual int32_t ConnBlePostBytesMock(uint32_t connectionId, uint8_t *data, @@ -38,12 +37,11 @@ class GeneralConnectionInterfaceMock : public GeneralConnectionInterface { public: GeneralConnectionInterfaceMock(); ~GeneralConnectionInterfaceMock() override; - MOCK_METHOD(int32_t, LnnGetLocalStrInfo, (InfoKey key, char *info, uint32_t len), (override)); MOCK_METHOD(int32_t, BleConnectDeviceMock, (const ConnectOption *option, uint32_t requestId, const ConnectResult *result), (override)); MOCK_METHOD(int32_t, ConnBlePostBytesMock, (uint32_t connectionId, uint8_t *data, uint32_t dataLen, int32_t pid, int32_t flag, int32_t module, int64_t seq), (override)); - + static ConnectCallback *GetConnectCallbackMock(); static ConnectResult *GetConnectResultMock(); }; diff --git a/tests/core/connection/general/softbus_conn_general_connection_test.cpp b/tests/core/connection/general/softbus_conn_general_connection_test.cpp index 9b49a9b649..cf6badc164 100644 --- a/tests/core/connection/general/softbus_conn_general_connection_test.cpp +++ b/tests/core/connection/general/softbus_conn_general_connection_test.cpp @@ -30,9 +30,14 @@ using namespace std; static ConnectCallback *g_ConnectCallback = nullptr; static uint32_t g_handle = 0; static uint32_t g_ConnectionId = 0; -static bool g_isRecvNewConnection = false; + static uint32_t g_isServerGeneralId = 0; +static bool g_connectCallbackFlag = false; +static int32_t g_failCallbackFlag = false; +static bool g_isRecvNewConnection = false; +static bool g_recvDataFlag = false; + namespace OHOS { class GeneralConnectionTest : public testing::Test { public: @@ -117,6 +122,72 @@ static ConnPostData *PackInnerMsg(GeneralConnectionInfo *info, GeneralConnection return &buff; } +static bool GetConnectCallbackFlag() +{ + if (g_connectCallbackFlag == true) { + g_connectCallbackFlag = false; + return true; + } + return false; +} + +static int32_t GetFailCallbackReason() +{ + if (g_failCallbackFlag < 0) { + int32_t reason = g_failCallbackFlag; + g_failCallbackFlag = SOFTBUS_OK; + return reason; + } + return SOFTBUS_OK; +} + +static bool GetRecvDataFlag() +{ + if (g_recvDataFlag == true) { + g_recvDataFlag = false; + return true; + } + return false; +} + +static void ConnectSuccess(GeneralConnectionParam *info, uint32_t generalHandle) +{ + (void)info; + (void)generalHandle; + g_connectCallbackFlag = true; +} + +static void ConnectFailed(GeneralConnectionParam *info, uint32_t generalHandle, int32_t reason) +{ + (void)info; + (void)generalHandle; + (void)reason; + g_failCallbackFlag = reason; +} + +static void AcceptConnect(GeneralConnectionParam *info, uint32_t generalHandle) +{ + (void)info; + (void)generalHandle; + g_isRecvNewConnection = true; + g_isServerGeneralId = generalHandle; +} + +static void DataReceived(GeneralConnectionParam *info, uint32_t generalHandle, const uint8_t *data, uint32_t dataLen) +{ + (void)info; + (void)generalHandle; + (void)data; + (void)dataLen; + g_recvDataFlag = true; +} + +static void ConnectionDisconnected(GeneralConnectionParam *info, uint32_t generalHandle, int32_t reason) +{ + (void)info; + (void)generalHandle; + (void)reason; +} /* * @tc.name: TestInit @@ -146,10 +217,36 @@ HWTEST_F(GeneralConnectionTest, TestInit, TestSize.Level1) GeneralConnectionParam param = {0}; manager->closeServer(¶m); g_ConnectCallback = GeneralConnectionInterfaceMock::GetConnectCallbackMock(); - EXPECT_NE(g_ConnectCallback, nullptr); + ASSERT_NE(g_ConnectCallback, nullptr); CONN_LOGI(CONN_BLE, "test init end"); } +/* +* @tc.name: TestRegisterListener +* @tc.desc: test register listener +* @tc.type: FUNC +* @tc.require:AR000GIRGE +*/ +HWTEST_F(GeneralConnectionTest, TestRegisterListener, TestSize.Level1) +{ + CONN_LOGI(CONN_BLE, "test register listener start"); + + GeneralConnectionManager *manager = GetGeneralConnectionManager(); + EXPECT_NE(manager, nullptr); + + GeneralConnectionListener listener = { + .onConnectSuccess = ConnectSuccess, + .onConnectFailed = ConnectFailed, + .onAcceptConnect = AcceptConnect, + .onDataReceived = DataReceived, + .onConnectionDisconnected = ConnectionDisconnected, + }; + + int32_t ret = manager->registerListener(&listener); + EXPECT_EQ(ret, SOFTBUS_OK); + + CONN_LOGI(CONN_BLE, "test register listener end"); +} /* * @tc.name: TestCreateServerMax * @tc.desc: test create server include max count(10) and normal case @@ -249,7 +346,7 @@ HWTEST_F(GeneralConnectionTest, TestConnect, TestSize.Level1) /* * @tc.name: test send -* @tc.desc: test send +* @tc.desc: test send * @tc.type: FUNC * @tc.require:AR000GIRGE */ @@ -257,9 +354,9 @@ HWTEST_F(GeneralConnectionTest, TestSend, TestSize.Level1) { CONN_LOGI(CONN_BLE, "test send start"); GeneralConnectionManager *manager = GetGeneralConnectionManager(); - EXPECT_NE(manager, nullptr); + ASSERT_NE(manager, nullptr); GeneralConnectionParam param = {0}; - const char *pkgName = "testPkgName"; + const char *pkgName = "testPkgName1"; int32_t ret = strcpy_s(param.pkgName, PKG_NAME_SIZE_MAX, pkgName); EXPECT_EQ(ret, EOK); ret = strcpy_s(param.bundleName, BUNDLE_NAME_MAX, "testBundleName"); @@ -270,17 +367,14 @@ HWTEST_F(GeneralConnectionTest, TestSend, TestSize.Level1) param.pid = 0; NiceMock mock; EXPECT_CALL(mock, BleConnectDeviceMock).WillRepeatedly(Return(SOFTBUS_OK)); - g_handle = manager->connect(¶m, addr); + g_handle = manager->connect(¶m, addr); // to get handle EXPECT_EQ(g_handle > 0, true); - uint8_t *data = (uint8_t *)malloc(sizeof(uint8_t)); + uint8_t *data = (uint8_t *)SoftBusCalloc(sizeof(uint8_t)); EXPECT_NE(data, nullptr); - ret = manager->send(g_handle, data, 0, 0); + ret = manager->send(g_handle, data, 0, 0); // unexpect state EXPECT_EQ(ret, SOFTBUS_CONN_GENERAL_CONNECTION_NOT_READY); GeneralConnectionInfo info = {{0}}; - info.ackStatus = 0; - info.localId = 0; info.peerId = g_handle; - info.abilityBitSet = 0; OutData *dataRecv = GeneralConnectionPackMsg(&info, GENERAL_CONNECTION_MSG_TYPE_HANDSHAKE_ACK); EXPECT_NE(dataRecv, nullptr); uint32_t size = ConnGetHeadSize(); @@ -295,12 +389,14 @@ HWTEST_F(GeneralConnectionTest, TestSend, TestSize.Level1) ConnectResult *connectResult = GeneralConnectionInterfaceMock::GetConnectResultMock(); uint32_t requestId = 12; ConnectionInfo infos = {0}; - connectResult->OnConnectSuccessed(requestId, g_ConnectionId, &infos); + connectResult->OnConnectSuccessed(requestId, g_ConnectionId, &infos); // save connectionId g_ConnectCallback->OnDataReceived(g_ConnectionId, MODULE_BLE_GENERAL, 0, buff, dataLen); // state change to success + EXPECT_EQ(true, GetConnectCallbackFlag()); EXPECT_CALL(mock, ConnBlePostBytesMock).WillRepeatedly(Return(SOFTBUS_OK)); ret = manager->send(g_handle, data, sizeof(uint8_t), 0); EXPECT_EQ(ret, SOFTBUS_OK); SoftBusFree(data); + SoftBusFree(buff); CONN_LOGI(CONN_BLE, "test send end"); } @@ -318,9 +414,11 @@ HWTEST_F(GeneralConnectionTest, TestRecv, TestSize.Level1) uint8_t *data = (uint8_t *)malloc(sizeof(uint8_t)); EXPECT_NE(data, nullptr); g_ConnectCallback->OnDataReceived(0, MODULE_BLE_CONN, 0, (char *)data, GENERAL_CONNECTION_HEADER_SIZE + 1); + EXPECT_EQ(GetRecvDataFlag(), false); OutData *dataRecv = PackReciveData(data, sizeof(uint8_t), 0, g_handle); EXPECT_NE(dataRecv, nullptr); g_ConnectCallback->OnDataReceived(g_ConnectionId, MODULE_BLE_GENERAL, 0, (char *)dataRecv->data, dataRecv->dataLen); + EXPECT_EQ(GetRecvDataFlag(), true); CONN_LOGI(CONN_BLE, "test recv end"); } @@ -334,7 +432,7 @@ HWTEST_F(GeneralConnectionTest, TestGetPeerDeviceId, TestSize.Level1) { CONN_LOGI(CONN_BLE, "test get peer deviceId start"); GeneralConnectionManager *manager = GetGeneralConnectionManager(); - EXPECT_NE(manager, nullptr); + ASSERT_NE(manager, nullptr); char addr[BT_MAC_LEN] = {0}; int32_t ret = manager->getPeerDeviceId(g_handle, addr, BT_MAC_LEN - 1, 0); @@ -347,7 +445,7 @@ HWTEST_F(GeneralConnectionTest, TestGetPeerDeviceId, TestSize.Level1) /* * @tc.name: Test OnConnectDisconnected -* @tc.desc: test OnConnectDisconnected +* @tc.desc: test OnConnectDisconnected * @tc.type: FUNC * @tc.require:AR000GIRGE */ @@ -355,7 +453,7 @@ HWTEST_F(GeneralConnectionTest, TestOnConnectDisconnected, TestSize.Level1) { CONN_LOGI(CONN_BLE, "test on connect disconnect start"); GeneralConnectionManager *manager = GetGeneralConnectionManager(); - EXPECT_NE(manager, nullptr); + ASSERT_NE(manager, nullptr); GeneralConnectionParam param = {0}; const char *pkgName = "testPkgName1"; @@ -384,76 +482,11 @@ HWTEST_F(GeneralConnectionTest, TestOnConnectDisconnected, TestSize.Level1) ConnPostData *data = PackInnerMsg(&info, GENERAL_CONNECTION_MSG_TYPE_RESET, MODULE_BLE_GENERAL); EXPECT_NE(data, nullptr); g_ConnectCallback->OnDataReceived(connectionId, MODULE_BLE_GENERAL, 0, data->buf, data->len); - - //g_ConnectCallback->OnDisconnected(g_ConnectionId, &infos); - //g_ConnectCallback->OnDisconnected(g_ConnectionId, &infos); 最后一个用例的时候再调用, 这个全局变量后面还要用, 两次是走不同的分支。 + EXPECT_EQ(GetFailCallbackReason(), SOFTBUS_CONN_GENERAL_PEER_CONNECTION_CLOSE); + g_ConnectCallback->OnDisconnected(connectionId, &infos); CONN_LOGI(CONN_BLE, "test on connect disconnect end"); } -void ConnectSuccess(GeneralConnectionParam *info, uint32_t generalHandle) -{ - (void)info; - (void)generalHandle; -} - -void ConnectFailed(GeneralConnectionParam *info, uint32_t generalHandle, int32_t reason) -{ - (void)info; - (void)generalHandle; - (void)reason; -} - -void AcceptConnect(GeneralConnectionParam *info, uint32_t generalHandle) -{ - CONN_LOGI(CONN_BLE, "test AcceptConnect generalHandle=%{public}u", generalHandle); - (void)info; - (void)generalHandle; - g_isRecvNewConnection = true; - g_isServerGeneralId = generalHandle; -} - -void DataReceived(GeneralConnectionParam *info, uint32_t generalHandle, const uint8_t *data, uint32_t dataLen) -{ - (void)info; - (void)generalHandle; - (void)data; - (void)dataLen; -} - -void ConnectionDisconnected(GeneralConnectionParam *info, uint32_t generalHandle, int32_t reason) -{ - (void)info; - (void)generalHandle; - (void)reason; -} - -/* -* @tc.name: TestRegisterListener -* @tc.desc: test register listener -* @tc.type: FUNC -* @tc.require:AR000GIRGE -*/ -HWTEST_F(GeneralConnectionTest, TestRegisterListener, TestSize.Level1) -{ - CONN_LOGI(CONN_BLE, "test register listener start"); - - GeneralConnectionManager *manager = GetGeneralConnectionManager(); - EXPECT_NE(manager, nullptr); - - GeneralConnectionListener listener = { - .onConnectSuccess = ConnectSuccess, - .onConnectFailed = ConnectFailed, - .onAcceptConnect = AcceptConnect, - .onDataReceived = DataReceived, - .onConnectionDisconnected = ConnectionDisconnected, - }; - - int32_t ret = manager->registerListener(&listener); - EXPECT_EQ(ret, SOFTBUS_OK); - - CONN_LOGI(CONN_BLE, "test register listener end"); -} - /* * @tc.name: TestRecvNewConnection * @tc.desc: test recv GENERAL_CONNECTION_MSG_TYPE_HANDSHAKE @@ -491,12 +524,11 @@ HWTEST_F(GeneralConnectionTest, TestRecvNewConnection, TestSize.Level1) CONN_LOGI(CONN_BLE, "test recv new connection end"); // test recv merge message - GeneralConnectionInfo info = { + GeneralConnectionInfo info1 = { .peerId = g_isServerGeneralId, .updateHandle = 222, }; - data = PackInnerMsg(&info, GENERAL_CONNECTION_MSG_TYPE_MERGE, MODULE_BLE_GENERAL); + data = PackInnerMsg(&info1, GENERAL_CONNECTION_MSG_TYPE_MERGE, MODULE_BLE_GENERAL); g_ConnectCallback->OnDataReceived(connectionId, MODULE_BLE_GENERAL, 0, data->buf, data->len); } - } \ No newline at end of file -- Gitee