• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "iservice_registry.h"
17 #include "softbus_common.h"
18 #include "softbus_error_code.h"
19 #include "softbus_server.h"
20 #include "softbus_server_test_mock.h"
21 #include "system_ability_definition.h"
22 #include <gtest/gtest.h>
23 #include "softbus_server.cpp"
24 
25 using namespace testing;
26 using namespace testing::ext;
27 
28 namespace OHOS {
29 
30 #define TEST_SESSION_NAME_SIZE_MAX 256
31 
32 using GetGCMFunc = GeneralConnectionManager* (*)(void);
33 auto g_realGetGCM = reinterpret_cast<GetGCMFunc>(dlsym(RTLD_NEXT, "GetGeneralConnectionManager"));
34 
35 class SoftbusServerTest : public testing::Test {
36 public:
SoftbusServerTest()37     SoftbusServerTest()
38     {}
~SoftbusServerTest()39     ~SoftbusServerTest()
40     {}
SetUpTestCase()41     static void SetUpTestCase()
42     {}
TearDownTestCase()43     static void TearDownTestCase()
44     {}
SetUp()45     void SetUp() override
46     {}
TearDown()47     void TearDown() override
48     {}
49 };
50 
GenerateRemoteObject(void)51 static sptr<IRemoteObject> GenerateRemoteObject(void)
52 {
53     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
54     if (samgr != nullptr) {
55         return samgr->GetSystemAbility(SOFTBUS_SERVER_SA_ID);
56     }
57     return nullptr;
58 }
59 
60 /**
61  * @tc.name: SoftbusServerTest001
62  * @tc.desc: Verify the SoftbusRegisterService function.
63  * @tc.type: FUNC
64  * @tc.require:
65  */
66 HWTEST_F(SoftbusServerTest, SoftbusServerTest001, TestSize.Level1)
67 {
68     sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
69     ASSERT_NE(nullptr, softBusServer);
70 
71     int32_t ret = softBusServer->SoftbusRegisterService("test", nullptr);
72     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
73 
74     sptr<IRemoteObject> obj = GenerateRemoteObject();
75     ret = softBusServer->SoftbusRegisterService("test", obj);
76     EXPECT_EQ(SOFTBUS_OK, ret);
77 }
78 
79 /**
80  * @tc.name: SoftbusServerTest002
81  * @tc.desc: Verify the OpenAuthSession function.
82  * @tc.type: FUNC
83  * @tc.require:
84  */
85 HWTEST_F(SoftbusServerTest, SoftbusServerTest002, TestSize.Level1)
86 {
87     sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
88     ASSERT_NE(nullptr, softBusServer);
89     ConnectionAddr addr;
90     addr.type = CONNECTION_ADDR_MAX;
91 
92     int32_t ret = softBusServer->OpenAuthSession("test", nullptr);
93     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
94 
95     ret = softBusServer->OpenAuthSession("test", &addr);
96     EXPECT_EQ(SOFTBUS_TRANS_INVALID_CONNECT_TYPE, ret);
97 }
98 
99 /**
100  * @tc.name: SoftbusServerTest003
101  * @tc.desc: Verify the Dump function.
102  * @tc.type: FUNC
103  * @tc.require:
104  */
105 HWTEST_F(SoftbusServerTest, SoftbusServerTest003, TestSize.Level1)
106 {
107     sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
108     ASSERT_NE(nullptr, softBusServer);
109     int32_t fd = -1;
110     std::vector<std::u16string> args;
111 
112     int32_t ret = softBusServer->Dump(fd, args);
113     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
114 
115     fd = 0;
116     ret = softBusServer->Dump(fd, args);
117     EXPECT_EQ(SOFTBUS_OK, ret);
118 }
119 
120 /**
121  * @tc.name: SoftbusServerTest004
122  * @tc.desc: Verify the GetSoftbusSpecObject function.
123  * @tc.type: FUNC
124  * @tc.require:
125  */
126 HWTEST_F(SoftbusServerTest, SoftbusServerTest004, TestSize.Level1)
127 {
128     sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
129     ASSERT_NE(nullptr, softBusServer);
130     sptr<IRemoteObject> object = nullptr;
131     int32_t ret = softBusServer->GetSoftbusSpecObject(object);
132     EXPECT_EQ(ret, SOFTBUS_OK);
133 }
134 
135 /**
136  * @tc.name: SoftbusServerTest005
137  * @tc.desc: Verify the GetBusCenterExObj function.
138  * @tc.type: FUNC
139  * @tc.require:
140  */
141 HWTEST_F(SoftbusServerTest, SoftbusServerTest005, TestSize.Level1)
142 {
143     sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
144     ASSERT_NE(nullptr, softBusServer);
145     sptr<IRemoteObject> object = nullptr;
146     int32_t ret = softBusServer->GetBusCenterExObj(object);
147     EXPECT_EQ(ret, SOFTBUS_OK);
148 }
149 
150 /**
151  * @tc.name: SoftbusServerTest006
152  * @tc.desc: Verify the EvaluateQos function.
153  * @tc.type: FUNC
154  * @tc.require:
155  */
156 HWTEST_F(SoftbusServerTest, SoftbusServerTest006, TestSize.Level1)
157 {
158     sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
159     ASSERT_NE(nullptr, softBusServer);
160     NiceMock<SoftbusServerTestInterfaceMock> softbusServerMock;
161     char networkId[NETWORK_ID_BUF_LEN] = "test";
162     TransDataType dataType = DATA_TYPE_BYTES;
163 
164     EXPECT_CALL(softbusServerMock, IsValidString(_, _))
165         .WillRepeatedly(Return(false));
166     int32_t ret = softBusServer->EvaluateQos(networkId, dataType, nullptr, 0);
167     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
168 
169     EXPECT_CALL(softbusServerMock, IsValidString(_, _))
170         .WillRepeatedly(Return(true));
171     ret = softBusServer->EvaluateQos(networkId, dataType, nullptr, 0);
172     EXPECT_EQ(SOFTBUS_NETWORK_NODE_OFFLINE, ret);
173 }
174 
175 /**
176  * @tc.name: SoftbusServerTest007
177  * @tc.desc: ConvertConnectType api test.
178  * @tc.type: FUNC
179  * @tc.require:
180  */
181 HWTEST_F(SoftbusServerTest, SoftbusServerTest007, TestSize.Level1)
182 {
183     int ret = ConvertConnectType(CONNECTION_ADDR_BR);
184     EXPECT_EQ(ret, CONNECT_BR);
185     ret = ConvertConnectType(CONNECTION_ADDR_BLE);
186     EXPECT_EQ(ret, CONNECT_BLE);
187     ret = ConvertConnectType(CONNECTION_ADDR_ETH);
188     EXPECT_EQ(ret, CONNECT_TCP);
189     ret = ConvertConnectType(CONNECTION_ADDR_WLAN);
190     EXPECT_EQ(ret, CONNECT_TCP);
191     ret = ConvertConnectType(CONNECTION_ADDR_NCM);
192     EXPECT_EQ(ret, CONNECT_TCP);
193 }
194 
195 /**
196  * @tc.name: SoftbusServerTest008
197  * @tc.desc: SoftbusRegisterService api test.
198  * @tc.type: FUNC
199  * @tc.require:
200  */
201 HWTEST_F(SoftbusServerTest, SoftbusServerTest008, TestSize.Level1)
202 {
203     sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
204     EXPECT_NE(softBusServer, nullptr);
205     sptr<IRemoteObject> obj = GenerateRemoteObject();
206     EXPECT_NE(obj, nullptr);
207 
208     int32_t ret = softBusServer->SoftbusRegisterService("test008", obj);
209     EXPECT_EQ(ret, SOFTBUS_OK);
210     ret = softBusServer->SoftbusRegisterService("test008", obj);
211     EXPECT_EQ(ret, SOFTBUS_OK);
212 }
213 
214 /**
215  * @tc.name: SoftbusServerTest009
216  * @tc.desc: OpenAuthSession api test.
217  * @tc.type: FUNC
218  * @tc.require:
219  */
220 HWTEST_F(SoftbusServerTest, SoftbusServerTest009, TestSize.Level1)
221 {
222     sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
223     EXPECT_NE(softBusServer, nullptr);
224 
225     NiceMock<SoftbusServerTestInterfaceMock> softbusServerMock;
226     EXPECT_CALL(softbusServerMock, IsValidString(_, _))
227         .WillRepeatedly(Return(true));
228 
229     ConnectionAddr addr1;
230     addr1.type = CONNECTION_ADDR_WLAN;
231     int32_t ret = softBusServer->OpenAuthSession("test", &addr1);
232     EXPECT_EQ(ret, -1);
233     addr1.type = CONNECTION_ADDR_BR;
234     ret = softBusServer->OpenAuthSession("test", &addr1);
235     EXPECT_EQ(ret, -1);
236     addr1.type = CONNECTION_ADDR_BLE;
237     ret = softBusServer->OpenAuthSession("test", &addr1);
238     EXPECT_EQ(ret, -1);
239     addr1.type = CONNECTION_ADDR_ETH;
240     ret = softBusServer->OpenAuthSession("test", &addr1);
241     EXPECT_EQ(ret, -1);
242 }
243 
244 /**
245  * @tc.name: SoftbusServerTest010
246  * @tc.desc: adapter func test.
247  * @tc.type: FUNC
248  * @tc.require:
249  */
250 HWTEST_F(SoftbusServerTest, SoftbusServerTest010, TestSize.Level1)
251 {
252     sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
253     EXPECT_NE(softBusServer, nullptr);
254 
255     int32_t ret = softBusServer->UnregDataLevelChangeCb(nullptr);
256     EXPECT_EQ(ret, SOFTBUS_OK);
257     ret = softBusServer->StopRangeForMsdp(nullptr, nullptr);
258     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
259     ret = softBusServer->SyncTrustedRelationShip(nullptr, nullptr, 0);
260     EXPECT_EQ(ret, SOFTBUS_OK);
261     ret = softBusServer->ProcessInnerEvent(0, nullptr, 0);
262     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
263     ret = softBusServer->PrivilegeCloseChannel(0, 0, nullptr);
264     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
265     ret = softBusServer->PrivilegeCloseChannel(0, 0, "test");
266     EXPECT_EQ(ret, SOFTBUS_OK);
267 }
268 
269 /**
270  * @tc.name: SoftbusServerTest011
271  * @tc.desc: adapter func test.
272  * @tc.type: FUNC
273  * @tc.require:
274  */
275 HWTEST_F(SoftbusServerTest, SoftbusServerTest011, TestSize.Level1)
276 {
277     sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
278     EXPECT_NE(softBusServer, nullptr);
279 
280     int32_t ret = softBusServer->OpenBrProxy(nullptr, nullptr);
281     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
282     ret = softBusServer->CloseBrProxy(0);
283     EXPECT_EQ(ret, SOFTBUS_TRANS_SESSION_SERVER_NOINIT);
284     ret = softBusServer->SendBrProxyData(0, nullptr, 0);
285     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
286     ret = softBusServer->SetListenerState(0, 0, false);
287     EXPECT_EQ(ret, SOFTBUS_TRANS_SESSION_SERVER_NOINIT);
288     ret = softBusServer->IsProxyChannelEnabled(0);
289     EXPECT_EQ(ret, 1);
290 }
291 
292 /**
293  * @tc.name: SoftbusServerTest012
294  * @tc.desc: ConvertTransType api test.
295  * @tc.type: FUNC
296  * @tc.require:
297  */
298 HWTEST_F(SoftbusServerTest, SoftbusServerTest012, TestSize.Level1)
299 {
300     LaneTransType ret;
301     TransDataType dataType = DATA_TYPE_MESSAGE;
302     ret = ConvertTransType(dataType);
303     EXPECT_EQ(ret, LANE_T_MSG);
304     dataType = DATA_TYPE_BYTES;
305     ret = ConvertTransType(dataType);
306     EXPECT_EQ(ret, LANE_T_BYTE);
307     dataType = DATA_TYPE_FILE;
308     ret = ConvertTransType(dataType);
309     EXPECT_EQ(ret, LANE_T_FILE);
310     dataType = DATA_TYPE_RAW_STREAM;
311     ret = ConvertTransType(dataType);
312     EXPECT_EQ(ret, LANE_T_RAW_STREAM);
313     dataType = DATA_TYPE_VIDEO_STREAM;
314     ret = ConvertTransType(dataType);
315     EXPECT_EQ(ret, LANE_T_COMMON_VIDEO);
316     dataType = DATA_TYPE_AUDIO_STREAM;
317     ret = ConvertTransType(dataType);
318     EXPECT_EQ(ret, LANE_T_COMMON_VOICE);
319     dataType = DATA_TYPE_SLICE_STREAM;
320     ret = ConvertTransType(dataType);
321     EXPECT_EQ(ret, LANE_T_RAW_STREAM);
322     dataType = DATA_TYPE_RAW_STREAM_ENCRYPED;
323     ret = ConvertTransType(dataType);
324     EXPECT_EQ(ret, LANE_T_BUTT);
325 }
326 
327 /**
328  * @tc.name: SoftbusServerTest013
329  * @tc.desc: ConnGetPeerDeviceId api test.
330  * @tc.type: FUNC
331  * @tc.require:
332  */
333 HWTEST_F(SoftbusServerTest, SoftbusServerTest013, TestSize.Level1)
334 {
335     sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
336     EXPECT_NE(softBusServer, nullptr);
337     NiceMock<SoftbusServerTestInterfaceMock> softbusServerMock;
338 
339     using GetGCMFunc = GeneralConnectionManager* (*)(void);
340     auto realGetGCM = reinterpret_cast<GetGCMFunc>(dlsym(RTLD_NEXT, "GetGeneralConnectionManager"));
341     EXPECT_NE(realGetGCM, nullptr);
342 
343     EXPECT_CALL(softbusServerMock, GetGeneralConnectionManager())
344         .WillOnce(Return(nullptr))
345         .WillRepeatedly(Invoke(realGetGCM));
346 
347     int32_t ret = softBusServer->ConnGetPeerDeviceId(0, nullptr, 0);
348     EXPECT_EQ(ret, SOFTBUS_NO_INIT);
349 
350     ret = softBusServer->ConnGetPeerDeviceId(0, nullptr, 0);
351     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
352 }
353 
354 /**
355  * @tc.name: SoftbusServerTest014
356  * @tc.desc: Connect api test.
357  * @tc.type: FUNC
358  * @tc.require:
359  */
360 HWTEST_F(SoftbusServerTest, SoftbusServerTest014, TestSize.Level1)
361 {
362     sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
363     EXPECT_NE(softBusServer, nullptr);
364     NiceMock<SoftbusServerTestInterfaceMock> softbusServerMock;
365 
366     EXPECT_CALL(softbusServerMock, GetGeneralConnectionManager())
367         .WillOnce(Return(nullptr))
368         .WillRepeatedly(Invoke(g_realGetGCM));
369 
370     int32_t ret = softBusServer->Connect(nullptr, nullptr, nullptr);
371     EXPECT_EQ(ret, SOFTBUS_NO_INIT);
372 
373     ret = softBusServer->Connect(nullptr, nullptr, nullptr);
374     EXPECT_EQ(ret, SOFTBUS_STRCPY_ERR);
375 
376     ret = softBusServer->Connect(nullptr, "test", nullptr);
377     EXPECT_EQ(ret, SOFTBUS_STRCPY_ERR);
378 
379     ret = softBusServer->Connect("test", "test", nullptr);
380     EXPECT_EQ(ret, SOFTBUS_CONN_GENERAL_CREATE_CLIENT_MAX);
381 }
382 
383 /**
384  * @tc.name: SoftbusServerTest015
385  * @tc.desc: Disconnect api test.
386  * @tc.type: FUNC
387  * @tc.require:
388  */
389 HWTEST_F(SoftbusServerTest, SoftbusServerTest015, TestSize.Level1)
390 {
391     sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
392     EXPECT_NE(softBusServer, nullptr);
393     NiceMock<SoftbusServerTestInterfaceMock> softbusServerMock;
394 
395     EXPECT_CALL(softbusServerMock, GetGeneralConnectionManager())
396         .WillOnce(Return(nullptr))
397         .WillRepeatedly(Invoke(g_realGetGCM));
398 
399     int32_t ret = softBusServer->Disconnect(0);
400     EXPECT_EQ(ret, SOFTBUS_NO_INIT);
401 
402     ret = softBusServer->Disconnect(0);
403     EXPECT_EQ(ret, SOFTBUS_OK);
404 }
405 
406 /**
407  * @tc.name: SoftbusServerTest016
408  * @tc.desc: Send api test.
409  * @tc.type: FUNC
410  * @tc.require:
411  */
412 HWTEST_F(SoftbusServerTest, SoftbusServerTest016, TestSize.Level1)
413 {
414     sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
415     EXPECT_NE(softBusServer, nullptr);
416     NiceMock<SoftbusServerTestInterfaceMock> softbusServerMock;
417 
418     EXPECT_CALL(softbusServerMock, GetGeneralConnectionManager())
419         .WillOnce(Return(nullptr))
420         .WillRepeatedly(Invoke(g_realGetGCM));
421 
422     int32_t ret = softBusServer->Send(0, nullptr, 0);
423     EXPECT_EQ(ret, SOFTBUS_NO_INIT);
424 
425     ret = softBusServer->Send(0, nullptr, 0);
426     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
427 }
428 
429 /**
430  * @tc.name: SoftbusServerTest017
431  * @tc.desc: Send api test.
432  * @tc.type: FUNC
433  * @tc.require:
434  */
435 HWTEST_F(SoftbusServerTest, SoftbusServerTest017, TestSize.Level1)
436 {
437     sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
438     EXPECT_NE(softBusServer, nullptr);
439     NiceMock<SoftbusServerTestInterfaceMock> softbusServerMock;
440 
441     EXPECT_CALL(softbusServerMock, GetGeneralConnectionManager())
442         .WillOnce(Return(nullptr))
443         .WillRepeatedly(Invoke(g_realGetGCM));
444 
445     int32_t ret = softBusServer->ConnGetPeerDeviceId(0, nullptr, 0);
446     EXPECT_EQ(ret, SOFTBUS_NO_INIT);
447 
448     ret = softBusServer->ConnGetPeerDeviceId(0, nullptr, 0);
449     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
450 }
451 }