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 #include <gtest/gtest.h>
16 #include <gmock/gmock.h>
17 #include <securec.h>
18
19 #include "general_connection_server_proxy.h"
20 #include "softbus_connection.h"
21 #include "softbus_error_code.h"
22 #include "client_connection_mock_test.h"
23
24 using namespace testing::ext;
25 namespace OHOS {
26 namespace {
27 class ClientConnectionTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
30 static void TearDownTestCase();
31 void SetUp();
32 void TearDown();
33 };
34
SetUpTestCase()35 void ClientConnectionTest::SetUpTestCase()
36 {
37 }
38
TearDownTestCase()39 void ClientConnectionTest::TearDownTestCase() { }
40
SetUp()41 void ClientConnectionTest::SetUp() { }
42
TearDown()43 void ClientConnectionTest::TearDown() { }
44
OnAcceptConnect(const char * name,uint32_t handle)45 static int32_t OnAcceptConnect(const char *name, uint32_t handle)
46 {
47 printf("OnAcceptConnect called, name: %s, handle: %u\n", name, handle);
48 return 0;
49 }
50
OnConnectionStateChange(uint32_t handle,int32_t state,int32_t reason)51 static int32_t OnConnectionStateChange(uint32_t handle, int32_t state, int32_t reason)
52 {
53 printf("OnConnectionStateChange called, handle: %u, state: %d, reason: %d\n", handle, state, reason);
54 return 0;
55 }
56
OnDataRecevied(uint32_t handle,const uint8_t * data,uint32_t len)57 static void OnDataRecevied(uint32_t handle, const uint8_t *data, uint32_t len)
58 {
59 printf("OnDataRecevied called, handle: %u, data: %s, len: %u\n", handle, data, len);
60 }
61
OnServiceDied(void)62 static void OnServiceDied(void)
63 {
64 printf("OnServiceDied called\n");
65 }
66
67 static IGeneralListener g_listener = {
68 .OnAcceptConnect = OnAcceptConnect,
69 .OnConnectionStateChange = OnConnectionStateChange,
70 .OnDataReceived = OnDataRecevied,
71 .OnServiceDied = OnServiceDied,
72 };
73
74 /*
75 * @tc.name: RegisterListenerTest
76 * @tc.desc: register listener test
77 * @tc.type: FUNC
78 * @tc.require:
79 */
80 HWTEST_F(ClientConnectionTest, RegisterListenerTest, TestSize.Level0)
81 {
82 int32_t ret = GeneralRegisterListener(nullptr);
83 ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
84
85 testing::NiceMock<ClientConnectionInterfaceMock> mock;
86 EXPECT_CALL(mock, SoftBusMutexInit).WillOnce(testing::Return(-1));
87 ret = GeneralRegisterListener(&g_listener);
88 ASSERT_EQ(ret, SOFTBUS_LOCK_ERR);
89
90 EXPECT_CALL(mock, SoftBusMutexInit).WillOnce(testing::Return(SOFTBUS_OK));
91 ret = GeneralRegisterListener(&g_listener);
92 ASSERT_NE(ret, SOFTBUS_OK);
93 }
94
95 /*
96 * @tc.name: UnregisterListenerTest
97 * @tc.desc: unregister listener test
98 * @tc.type: FUNC
99 * @tc.require:
100 */
101 HWTEST_F(ClientConnectionTest, UnregisterListenerTest, TestSize.Level0)
102 {
103 int32_t ret = GeneralUnregisterListener();
104 ASSERT_EQ(ret, SOFTBUS_OK);
105 }
106
107 /*
108 * @tc.name: CreateServerTest
109 * @tc.desc: create server test
110 * @tc.type: FUNC
111 * @tc.require:
112 */
113 HWTEST_F(ClientConnectionTest, CreateServerTest, TestSize.Level0)
114 {
115 int32_t ret = GeneralCreateServer(nullptr, nullptr);
116 ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
117 ret = GeneralCreateServer("ohos.distributedschedule.dms", nullptr);
118 ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
119 ret = GeneralCreateServer("smd", "1234");
120 ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
121
122 testing::NiceMock<ClientConnectionInterfaceMock> mock;
123 EXPECT_CALL(mock, InitSoftBus).WillOnce(testing::Return(-1));
124 ret = GeneralCreateServer("ohos.distributedschedule.dms", "1234");
125 ASSERT_NE(ret, SOFTBUS_OK);
126
127 EXPECT_CALL(mock, InitSoftBus).WillOnce(testing::Return(SOFTBUS_OK));
128 EXPECT_CALL(mock, ServerIpcCreateServer).WillOnce(testing::Return(-1));
129 ret = GeneralCreateServer("ohos.distributedschedule.dms", "1234");
130 ASSERT_NE(ret, SOFTBUS_OK);
131
132 EXPECT_CALL(mock, InitSoftBus).WillOnce(testing::Return(SOFTBUS_OK));
133 EXPECT_CALL(mock, ServerIpcCreateServer).WillOnce(testing::Return(SOFTBUS_OK));
134 ret = GeneralCreateServer("ohos.distributedschedule.dms", "1234");
135 ASSERT_EQ(ret, SOFTBUS_OK);
136 }
137
138 /*
139 * @tc.name: RemoveServerTest
140 * @tc.desc: remove server test
141 * @tc.type: FUNC
142 * @tc.require:
143 */
144 HWTEST_F(ClientConnectionTest, RemoveServerTest, TestSize.Level0)
145 {
146 int32_t ret = GeneralRemoveServer(nullptr, nullptr);
147 ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
148
149 testing::NiceMock<ClientConnectionInterfaceMock> mock;
150 EXPECT_CALL(mock, ServerIpcRemoveServer).WillOnce(testing::Return(-1));
151 ret = GeneralRemoveServer("ohos.distributedschedule.dms", "1234");
152 ASSERT_NE(ret, SOFTBUS_OK);
153
154 EXPECT_CALL(mock, ServerIpcRemoveServer).WillOnce(testing::Return(SOFTBUS_OK));
155 ret = GeneralRemoveServer("ohos.distributedschedule.dms", "1234");
156 ASSERT_EQ(ret, SOFTBUS_OK);
157 }
158
159 /*
160 * @tc.name: ConnectTest
161 * @tc.desc: connect test
162 * @tc.type: FUNC
163 * @tc.require:
164 */
165 HWTEST_F(ClientConnectionTest, ConnectTest, TestSize.Level0)
166 {
167 int32_t ret = GeneralConnect(nullptr, nullptr, nullptr);
168 ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
169 ret = GeneralConnect("ohos.distributedschedule.dms", "1234", nullptr);
170 ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
171
172 testing::NiceMock<ClientConnectionInterfaceMock> mock;
173 EXPECT_CALL(mock, InitSoftBus).WillOnce(testing::Return(-1));
174 Address addr;
175 ret = GeneralConnect("ohos.distributedschedule.dms", "1234", &addr);
176 ASSERT_NE(ret, SOFTBUS_OK);
177
178 EXPECT_CALL(mock, InitSoftBus).WillOnce(testing::Return(SOFTBUS_OK));
179 EXPECT_CALL(mock, ServerIpcConnect).WillOnce(testing::Return(-1));
180 ret = GeneralConnect("ohos.distributedschedule.dms", "1234", &addr);
181 ASSERT_NE(ret, SOFTBUS_OK);
182
183 EXPECT_CALL(mock, InitSoftBus).WillOnce(testing::Return(SOFTBUS_OK));
184 EXPECT_CALL(mock, ServerIpcConnect).WillOnce(testing::Return(SOFTBUS_OK));
185 ret = GeneralConnect("ohos.distributedschedule.dms", "1234", &addr);
186 ASSERT_EQ(ret, SOFTBUS_OK);
187
188 GeneralDisconnect(-1);
189 EXPECT_CALL(mock, ServerIpcDisconnect).WillOnce(testing::Return(-1));
190 GeneralDisconnect(1);
191 EXPECT_CALL(mock, ServerIpcDisconnect).WillOnce(testing::Return(SOFTBUS_OK));
192 GeneralDisconnect(1);
193 }
194
195 /*
196 * @tc.name: SendTest
197 * @tc.desc: send test
198 * @tc.type: FUNC
199 * @tc.require:
200 */
201 HWTEST_F(ClientConnectionTest, SendTest, TestSize.Level0)
202 {
203 int32_t ret = GeneralSend(-1, nullptr, 0);
204 ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
205 ret = GeneralSend(1, nullptr, 0);
206 ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
207 ret = GeneralSend(1, (const uint8_t *)"1234", 0);
208 ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
209 ret = GeneralSend(1, (const uint8_t *)"1234", GENERAL_SEND_DATA_MAX_LEN + 1);
210 ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
211
212 testing::NiceMock<ClientConnectionInterfaceMock> mock;
213 EXPECT_CALL(mock, ServerIpcSend).WillOnce(testing::Return(-1));
214 ret = GeneralSend(1, (const uint8_t *)"1234", 4);
215 ASSERT_NE(ret, SOFTBUS_OK);
216
217 EXPECT_CALL(mock, ServerIpcSend).WillOnce(testing::Return(SOFTBUS_OK));
218 ret = GeneralSend(1, (const uint8_t *)"1234", 4);
219 ASSERT_EQ(ret, SOFTBUS_OK);
220 }
221
222 /*
223 * @tc.name: ConnGetPeerDeviceIdTest
224 * @tc.desc: conn get peer device id test
225 * @tc.type: FUNC
226 * @tc.require:
227 */
228 HWTEST_F(ClientConnectionTest, ConnGetPeerDeviceIdTest, TestSize.Level0)
229 {
230 int32_t ret = GeneralGetPeerDeviceId(-1, nullptr, 0);
231 ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
232 ret = GeneralGetPeerDeviceId(1, nullptr, 0);
233 ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
234 char deviceId[10] = { 0 };
235 ret = GeneralGetPeerDeviceId(1, deviceId, 0);
236 ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
237 ret = GeneralGetPeerDeviceId(1, deviceId, BT_MAC_LEN + 1);
238 ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
239
240 testing::NiceMock<ClientConnectionInterfaceMock> mock;
241 EXPECT_CALL(mock, ServerIpcGetPeerDeviceId).WillOnce(testing::Return(-1));
242 ret = GeneralGetPeerDeviceId(1, deviceId, 10);
243 ASSERT_NE(ret, SOFTBUS_OK);
244
245 EXPECT_CALL(mock, ServerIpcGetPeerDeviceId).WillOnce(testing::Return(SOFTBUS_OK));
246 ret = GeneralGetPeerDeviceId(1, deviceId, 10);
247 ASSERT_EQ(ret, SOFTBUS_OK);
248 }
249 }
250 } // namespace OHOS
251