1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17
18 #include "ble_protocol_interface_factory.h"
19 #include "softbus_conn_ble_manager_mock.h"
20 #include "softbus_adapter_ble_conflict.h"
21 #include "softbus_adapter_bt_common.h"
22 #include "softbus_adapter_crypto.h"
23 #include "softbus_adapter_mem.h"
24 #include "softbus_conn_ble_connection.h"
25 #include "softbus_conn_ble_manager.h"
26 #include "softbus_conn_ble_snapshot.h"
27 #include "softbus_conn_ble_trans.h"
28 #include "softbus_conn_interface.h"
29 #include "softbus_error_code.h"
30 #include "softbus_feature_config.h"
31 #include "softbus_utils.h"
32
33 using namespace testing;
34 using namespace testing::ext;
35
36 namespace OHOS {
37
38 static ConnectFuncInterface *g_bleInterface = nullptr;
39 #define SLEEP_TIME_MS 1000
40
OnConnected(uint32_t connectionId,const ConnectionInfo * info)41 void OnConnected(uint32_t connectionId, const ConnectionInfo *info)
42 {
43 (void)connectionId;
44 (void)info;
45 }
46
OnReusedConnected(uint32_t connectionId,const ConnectionInfo * info)47 void OnReusedConnected(uint32_t connectionId, const ConnectionInfo *info)
48 {
49 (void)connectionId;
50 (void)info;
51 }
52
OnDisconnected(uint32_t connectionId,const ConnectionInfo * info)53 void OnDisconnected(uint32_t connectionId, const ConnectionInfo *info)
54 {
55 (void)connectionId;
56 (void)info;
57 }
58
OnDataReceived(uint32_t connectionId,ConnModule moduleId,int64_t seq,char * data,int32_t len)59 void OnDataReceived(uint32_t connectionId, ConnModule moduleId, int64_t seq, char *data, int32_t len)
60 {
61 (void)connectionId;
62 (void)moduleId;
63 (void)seq;
64 (void)data;
65 (void)len;
66 }
67
OnConnectFailed(uint32_t requestId,int32_t reason)68 void OnConnectFailed(uint32_t requestId, int32_t reason)
69 {
70 (void)requestId;
71 (void)reason;
72 }
73
74 extern "C" {
ConnBleInitTransModule(ConnBleTransEventListener * listener)75 int32_t ConnBleInitTransModule(ConnBleTransEventListener *listener)
76 {
77 (void)listener;
78 return SOFTBUS_OK;
79 }
80
SoftBusAddBtStateListener(const SoftBusBtStateListener * listener,int32_t * listenerId)81 int32_t SoftBusAddBtStateListener(const SoftBusBtStateListener *listener, int32_t *listenerId)
82 {
83 (void)listener;
84 (void)listenerId;
85 return SOFTBUS_OK;
86 }
87
SoftbusBleConflictNotifyDateReceive(int32_t underlayerHandle,const uint8_t * data,uint32_t dataLen)88 void SoftbusBleConflictNotifyDateReceive(int32_t underlayerHandle, const uint8_t *data, uint32_t dataLen)
89 {
90 (void)underlayerHandle;
91 (void)data;
92 (void)dataLen;
93 }
94
SoftbusBleConflictNotifyDisconnect(const char * addr,const char * udid)95 void SoftbusBleConflictNotifyDisconnect(const char *addr, const char *udid)
96 {
97 (void)addr;
98 (void)udid;
99 }
100
SoftbusBleConflictNotifyConnectResult(uint32_t requestId,int32_t underlayerHandle,bool status)101 void SoftbusBleConflictNotifyConnectResult(uint32_t requestId, int32_t underlayerHandle, bool status)
102 {
103 (void)requestId;
104 (void)underlayerHandle;
105 (void)status;
106 }
107
SoftbusBleConflictRegisterListener(SoftBusBleConflictListener * listener)108 void SoftbusBleConflictRegisterListener(SoftBusBleConflictListener *listener) { }
109 }
110
111 class SoftbusConnBleHiDumperTest : public testing::Test {
112 public:
SetUpTestCase()113 static void SetUpTestCase() {};
114 static void TearDownTestCase();
SetUp()115 void SetUp() override
116 {
117 ConnectCallback connectCb = { 0 };
118 connectCb.OnConnected = OnConnected;
119 connectCb.OnReusedConnected = OnReusedConnected;
120 connectCb.OnDisconnected = OnDisconnected;
121 connectCb.OnDataReceived = OnDataReceived;
122
123 LooperInit();
124 SoftbusConfigInit();
125
126 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
127 EXPECT_CALL(bleMock, ConnGattInitClientModule).WillRepeatedly(Return(SOFTBUS_OK));
128 EXPECT_CALL(bleMock, ConnGattInitServerModule).WillRepeatedly(Return(SOFTBUS_OK));
129
130 g_bleInterface = ConnInitBle(&connectCb);
131 }
TearDown()132 void TearDown() override
133 {
134 LooperDeinit();
135 }
136 };
137
TearDownTestCase()138 void SoftbusConnBleHiDumperTest::TearDownTestCase()
139 {
140 SoftBusSleepMs(SLEEP_TIME_MS);
141 }
142
143 /*
144 * @tc.name: BleHiDumperTest
145 * @tc.desc: test dump method
146 * @tc.type: FUNC
147 * @tc.require:
148 */
149 HWTEST_F(SoftbusConnBleHiDumperTest, BleHiDumperTest, TestSize.Level1)
150 {
151 const char *addr1 = "11:22:33:44:55:66";
152 const char *addr2 = "22:33:44:55:66:77";
153
154 ConnBleConnection *connection1 =
155 ConnBleCreateConnection(addr1, BLE_GATT, CONN_SIDE_CLIENT, INVALID_UNDERLAY_HANDLE, true);
156 ASSERT_NE(connection1, nullptr);
157
158 ConnBleConnection *connection2 =
159 ConnBleCreateConnection(addr2, BLE_GATT, CONN_SIDE_CLIENT, INVALID_UNDERLAY_HANDLE, true);
160 ASSERT_NE(connection2, nullptr);
161
162 auto ret = ConnBleSaveConnection(connection1);
163 ASSERT_EQ(SOFTBUS_OK, ret);
164 ret = ConnBleSaveConnection(connection2);
165 ASSERT_EQ(SOFTBUS_OK, ret);
166 int fd = 1;
167 ret = BleHiDumper(fd);
168 ASSERT_EQ(SOFTBUS_OK, ret);
169 }
170 } // namespace OHOS