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 "mock/softbus_conn_br_connection_mock.h"
19 #include "softbus_conn_br_connection.h"
20 #include "softbus_conn_br_snapshot.h"
21 #include "softbus_conn_br_trans.h"
22 #include "softbus_conn_interface.h"
23 #include "softbus_feature_config.h"
24 #include "wrapper_br_interface.h"
25
26 using namespace testing;
27 using namespace testing::ext;
28
29 #define BR_READ_FAILED (-1)
30 #define BR_WRITE_FAILED (-2)
31
32 namespace OHOS {
33
34 ConnectFuncInterface *connectFuncInterface = nullptr;
35 ConnectFuncInterface *g_connectFuncInterface = nullptr;
36
Init(const struct tagSppSocketDriver * sppDriver)37 void Init(const struct tagSppSocketDriver *sppDriver)
38 {
39 (void)sppDriver;
40 return;
41 }
42
Read(int32_t clientFd,uint8_t * buf,const int32_t length)43 int32_t Read(int32_t clientFd, uint8_t *buf, const int32_t length)
44 {
45 (void)clientFd;
46 (void)buf;
47 if (length <= 0) {
48 return BR_READ_SOCKET_CLOSED;
49 }
50 return length;
51 }
52
Write(int32_t clientFd,const uint8_t * buf,const int32_t length)53 int32_t Write(int32_t clientFd, const uint8_t *buf, const int32_t length)
54 {
55 (void)clientFd;
56 (void)buf;
57 if (length <= 0) {
58 return BR_WRITE_FAILED;
59 }
60 return SOFTBUS_OK;
61 }
62
63 SppSocketDriver g_sppDriver = {
64 .Init = Init,
65 .Read = Read,
66 .Write = Write,
67 };
68
OnConnected(uint32_t connectionId,const ConnectionInfo * info)69 void OnConnected(uint32_t connectionId, const ConnectionInfo *info)
70 {
71 (void)connectionId;
72 (void)info;
73 return;
74 }
75
OnDisconnected(uint32_t connectionId,const ConnectionInfo * info)76 void OnDisconnected(uint32_t connectionId, const ConnectionInfo *info)
77 {
78 (void)connectionId;
79 (void)info;
80 return;
81 }
82
OnDataReceived(uint32_t connectionId,ConnModule moduleId,int64_t seq,char * data,int32_t len)83 void OnDataReceived(uint32_t connectionId, ConnModule moduleId, int64_t seq, char *data, int32_t len)
84 {
85 (void)connectionId;
86 (void)moduleId;
87 (void)seq;
88 (void)data;
89 (void)len;
90 return;
91 }
92
93 class SoftbusConnBrHiDumperTest : public testing::Test {
94 public:
SoftbusConnBrHiDumperTest()95 SoftbusConnBrHiDumperTest() { }
~SoftbusConnBrHiDumperTest()96 ~SoftbusConnBrHiDumperTest() { }
97 static void SetUpTestCase(void);
98 static void TearDownTestCase(void);
99 void SetUp();
100 void TearDown();
101 };
102
SetUpTestCase(void)103 void SoftbusConnBrHiDumperTest::SetUpTestCase(void)
104 {
105 LooperInit();
106 SoftbusConfigInit();
107 ConnectionBrInterfaceMock brMock;
108 EXPECT_CALL(brMock, InitSppSocketDriver).WillRepeatedly(Return(&g_sppDriver));
109 EXPECT_CALL(brMock, SoftbusGetConfig).WillRepeatedly(ConnectionBrInterfaceMock::ActionOfSoftbusGetConfig1);
110 ConnServerInit();
111 }
112
TearDownTestCase(void)113 void SoftbusConnBrHiDumperTest::TearDownTestCase(void)
114 {
115 LooperDeinit();
116 }
117
SetUp(void)118 void SoftbusConnBrHiDumperTest::SetUp(void) { }
119
TearDown(void)120 void SoftbusConnBrHiDumperTest::TearDown(void) { }
121
GetBrConnStateByConnectionId(uint32_t connectId)122 int32_t GetBrConnStateByConnectionId(uint32_t connectId)
123 {
124 (void)connectId;
125 return BR_CONNECTION_STATE_CLOSED;
126 }
127
ConnInit(void)128 ConnectFuncInterface *ConnInit(void)
129 {
130 LooperInit();
131
132 ConnectCallback callback = {
133 .OnConnected = OnConnected,
134 .OnDisconnected = OnDisconnected,
135 .OnDataReceived = OnDataReceived,
136 };
137 NiceMock<ConnectionBrInterfaceMock> brMock;
138
139 EXPECT_CALL(brMock, InitSppSocketDriver).WillOnce(Return(&g_sppDriver));
140 EXPECT_CALL(brMock, SoftbusGetConfig)
141 .WillOnce(ConnectionBrInterfaceMock::ActionOfSoftbusGetConfig1)
142 .WillOnce(ConnectionBrInterfaceMock::ActionOfSoftbusGetConfig2);
143 EXPECT_CALL(brMock, ConnBrInnerQueueInit).WillOnce(Return(SOFTBUS_OK));
144 EXPECT_CALL(brMock, SoftBusAddBtStateListener).WillOnce(Return(SOFTBUS_OK));
145 EXPECT_CALL(brMock, ConnBrInitBrPendingPacket).WillOnce(Return(SOFTBUS_OK));
146
147 connectFuncInterface = ConnInitBr(&callback);
148 return connectFuncInterface;
149 }
150
151 /*
152 * @tc.name: BrHiDumperTest
153 * @tc.desc: test dump method
154 * @tc.type: FUNC
155 * @tc.require:
156 */
157 HWTEST_F(SoftbusConnBrHiDumperTest, BrHiDumperTest, TestSize.Level1)
158 {
159 g_connectFuncInterface = ConnInit();
160 const char *addr1 = "11:22:33:44:55:66";
161 const char *addr2 = "22:33:44:55:66:77";
162 ConnBrConnection *connection1 = ConnBrCreateConnection(addr1, CONN_SIDE_CLIENT, INVALID_SOCKET_HANDLE);
163 ConnBrSaveConnection(connection1);
164 ConnBrConnection *connection2 = ConnBrCreateConnection(addr2, CONN_SIDE_CLIENT, INVALID_SOCKET_HANDLE);
165 ConnBrSaveConnection(connection2);
166 int fd = 1;
167 auto ret = BrHiDumper(fd);
168 ASSERT_EQ(SOFTBUS_OK, ret);
169 }
170 } // namespace OHOS