1 /*
2 * Copyright (c) 2021 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 <cstdio>
17 #include <cstdlib>
18 #include <cstring>
19 #include <gtest/gtest.h>
20 #include <securec.h>
21 #include <unistd.h>
22
23 #include "common_list.h"
24 #include "inner_session.h"
25 #include "session.h"
26 #include "softbus_adapter_mem.h"
27 #include "softbus_adapter_timer.h"
28 #include "softbus_bus_center.h"
29 #include "softbus_common.h"
30 #include "softbus_def.h"
31 #include "softbus_errcode.h"
32 #include "softbus_feature_config.h"
33 #include "softbus_utils.h"
34
35 using namespace testing::ext;
36
37 namespace OHOS {
38 enum TEST_WAY {
39 PASSIVE_OPENAUTHSESSION_WAY = 0,
40 ACTIVE_OPENAUTHSESSION_WAY
41 };
42
43 const int CONN_SINGLE_WAIT_TIMEOUT = 5;
44 const int CONN_SLEEP_TIME = 1;
45 const int CLOSE_DELAY_TIME = 500;
46
47 const int SEND_DATA_SIZE_1K = 1024;
48 const int SEND_DATA_SIZE_4K = 4 * 1024;
49 const int SEND_DATA_SIZE_40K = 40 * 1000 - 8;
50
51 const int CONN_ADDR_INFO_COUNT = 5;
52 ConnectionAddr g_addrInfo[CONN_ADDR_INFO_COUNT];
53
54 ISessionListener g_sessionlistener;
55 int32_t g_openCount = 0;
56 const char *g_testModuleName = "com.plrdtest";
57 const char *g_testSessionName = "com.plrdtest.dsoftbus";
58 const char *g_testData = "{\"data\":\"open auth session test!!!\"}";
59
60 int32_t g_sessionId = -1;
61 int32_t g_sessionIdTwo = -1;
62 bool g_successFlag = false;
63 int32_t g_testWay = -1;
64
TestSendBytesData(int32_t sessionId,const char * data)65 int32_t TestSendBytesData(int32_t sessionId, const char *data)
66 {
67 int32_t ret = SendBytes(sessionId, data, SEND_DATA_SIZE_1K);
68 if (ret != SOFTBUS_OK) {
69 printf("SendBytes 1K err.\n");
70 return SOFTBUS_ERR;
71 }
72 ret = SendBytes(sessionId, data, SEND_DATA_SIZE_4K);
73 if (ret != SOFTBUS_OK) {
74 printf("SendBytes 4K err.\n");
75 return SOFTBUS_ERR;
76 }
77 ret = SendBytes(sessionId, data, SEND_DATA_SIZE_40K);
78 if (ret != SOFTBUS_OK) {
79 printf("SendBytes 40000 err.\n");
80 return SOFTBUS_ERR;
81 }
82 ret = SendBytes(sessionId, data, SEND_DATA_SIZE_40K + 1);
83 if (ret == SOFTBUS_OK) {
84 printf("SendBytes 40001 err.\n");
85 return SOFTBUS_ERR;
86 }
87 return SOFTBUS_OK;
88 }
89
TestSendMessageData(int32_t sessionId,const char * data)90 int32_t TestSendMessageData(int32_t sessionId, const char *data)
91 {
92 int32_t ret = SendMessage(sessionId, data, SEND_DATA_SIZE_1K);
93 if (ret != SOFTBUS_OK) {
94 printf("SendMessage 1K err.\n");
95 return SOFTBUS_ERR;
96 }
97 ret = SendMessage(sessionId, data, SEND_DATA_SIZE_4K);
98 if (ret != SOFTBUS_OK) {
99 printf("SendMessage 4K err.\n");
100 return SOFTBUS_ERR;
101 }
102 ret = SendMessage(sessionId, data, SEND_DATA_SIZE_4K + 1);
103 if (ret == SOFTBUS_OK) {
104 printf("SendMessage 4K + 1 err.\n");
105 return SOFTBUS_ERR;
106 }
107 return SOFTBUS_OK;
108 }
109
TestSendData(int32_t sessionId,const char * data,int32_t len)110 int32_t TestSendData(int32_t sessionId, const char *data, int32_t len)
111 {
112 int32_t ret;
113 if (len <= SEND_DATA_SIZE_40K) {
114 ret = SendBytes(sessionId, data, len);
115 if (ret != SOFTBUS_OK) {
116 return ret;
117 }
118 } else {
119 ret = TestSendBytesData(sessionId, data);
120 if (ret != SOFTBUS_OK) {
121 return ret;
122 }
123 ret = TestSendMessageData(sessionId, data);
124 if (ret != SOFTBUS_OK) {
125 return ret;
126 }
127 }
128 return SOFTBUS_OK;
129 }
130
OnSessionOpened(int sessionId,int result)131 int OnSessionOpened(int sessionId, int result)
132 {
133 printf("############# session opened,sesison id[%d] result[%d]\n", sessionId, result);
134 if (result == SOFTBUS_OK) {
135 if (g_sessionId == -1) {
136 g_sessionId = sessionId;
137 }
138 g_successFlag = true;
139 g_openCount++;
140 }
141 return result;
142 }
143
OnSessionClosed(int sessionId)144 void OnSessionClosed(int sessionId)
145 {
146 printf("############# session closed, session id = %d\n", sessionId);
147 g_sessionId = -1;
148 g_successFlag = false;
149 }
150
OnBytesReceived(int sessionId,const void * data,unsigned int len)151 void OnBytesReceived(int sessionId, const void *data, unsigned int len)
152 {
153 if (g_testWay == PASSIVE_OPENAUTHSESSION_WAY) {
154 SendBytes(sessionId, "{\"received ok\"}", strlen("{\"received ok\"}"));
155 }
156 printf("bytes received, sessionid[%d], data[%s], dataLen[%u]\n", sessionId, data, len);
157 }
158
OnMessageReceived(int sessionId,const void * data,unsigned int len)159 void OnMessageReceived(int sessionId, const void *data, unsigned int len)
160 {
161 printf("msg received, sessionid[%d], data[%s], dataLen[%u]\n", sessionId, data, len);
162 }
163
TestSessionListenerInit(void)164 void TestSessionListenerInit(void)
165 {
166 g_sessionlistener.OnSessionOpened = OnSessionOpened;
167 g_sessionlistener.OnSessionClosed = OnSessionClosed;
168 g_sessionlistener.OnBytesReceived = OnBytesReceived;
169 g_sessionlistener.OnMessageReceived = OnMessageReceived;
170 }
171
TestCreateSessionServer(void)172 int32_t TestCreateSessionServer(void)
173 {
174 int32_t ret = CreateSessionServer(g_testModuleName, g_testSessionName, &g_sessionlistener);
175 if (ret != SOFTBUS_SERVER_NAME_REPEATED && ret != SOFTBUS_OK) { // -986: SOFTBUS_SERVER_NAME_REPEATED
176 printf("CreateSessionServer ret: %d \n", ret);
177 return SOFTBUS_ERR;
178 }
179 printf("CreateSessionServer ret: %d \n", ret);
180 return SOFTBUS_OK;
181 }
182
TestOpenAuthSession(const ConnectionAddr * addrInfo,bool two)183 int32_t TestOpenAuthSession(const ConnectionAddr *addrInfo, bool two)
184 {
185 g_sessionId = OpenAuthSession(g_testSessionName, addrInfo, 1, NULL);
186 if (g_sessionId < 0) {
187 printf("OpenAuthSession ret[%d]", g_sessionId);
188 return SOFTBUS_ERR;
189 }
190 if (two) {
191 g_sessionIdTwo = OpenAuthSession(g_testSessionName, addrInfo, 1, NULL);
192 if (g_sessionIdTwo < 0) {
193 printf("OpenAuthSession ret[%d]", g_sessionIdTwo);
194 return SOFTBUS_ERR;
195 }
196 }
197 int32_t timeout = 0;
198 while (!g_successFlag) {
199 timeout++;
200 if (timeout > CONN_SINGLE_WAIT_TIMEOUT) {
201 printf("wait [%ds] timeout!!\n", CONN_SINGLE_WAIT_TIMEOUT);
202 return SOFTBUS_ERR;
203 }
204 sleep(CONN_SLEEP_TIME);
205 }
206 return SOFTBUS_OK;
207 }
208
TestCloseSession(void)209 void TestCloseSession(void)
210 {
211 printf("TestCloseSession exit\n");
212 if (g_sessionId > 0) {
213 CloseSession(g_sessionId);
214 g_sessionId = -1;
215 g_successFlag = false;
216 }
217 }
218
TestCloseSessionTwo(void)219 void TestCloseSessionTwo(void)
220 {
221 printf("TestCloseSessionTwo exit\n");
222 if (g_sessionIdTwo > 0) {
223 CloseSession(g_sessionIdTwo);
224 g_sessionIdTwo = -1;
225 }
226 }
227
228 class AuthSessionTest : public testing::Test {
229 public:
AuthSessionTest()230 AuthSessionTest()
231 {}
~AuthSessionTest()232 ~AuthSessionTest()
233 {}
234 static void SetUpTestCase(void);
235 static void TearDownTestCase(void);
236 void SetUp();
237 void TearDown();
238 int32_t TestWaitOpenSession(int32_t count);
239 };
240
SetUpTestCase(void)241 void AuthSessionTest::SetUpTestCase(void)
242 {
243 SoftbusConfigInit();
244 TestSessionListenerInit();
245 printf("********Disc Test Begin*********\r\n");
246 printf("* 0.passive openAuthSession *\r\n");
247 printf("* 1.active openAuthSession *\r\n");
248 printf("********************************\r\n");
249 printf("input the num:");
250 if (scanf_s("%d", &g_testWay, sizeof(g_testWay)) < 0) {
251 printf("input error!\n");
252 }
253 getchar();
254 }
255
TearDownTestCase(void)256 void AuthSessionTest::TearDownTestCase(void)
257 {}
258
SetUp(void)259 void AuthSessionTest::SetUp(void)
260 {}
261
TearDown(void)262 void AuthSessionTest::TearDown(void)
263 {
264 TestCloseSession();
265 }
266
TestWaitOpenSession(int32_t count)267 int32_t AuthSessionTest::TestWaitOpenSession(int32_t count)
268 {
269 int32_t timeout = count * CONN_SINGLE_WAIT_TIMEOUT;
270 while (g_openCount < count) {
271 --timeout;
272 if (!timeout) {
273 printf("wait [%d] timeout!!\n", count);
274 break;
275 }
276 sleep(CONN_SLEEP_TIME);
277 }
278 return (g_openCount < count) ? SOFTBUS_ERR : SOFTBUS_OK;
279 }
280
281 /*
282 * @tc.name: testPassiveOpenAuthSession001
283 * @tc.desc: test passive open auth session
284 * @tc.type: FUNC
285 * @tc.require:AR000GIRGG
286 */
287 HWTEST_F(AuthSessionTest, testPassiveOpenAuthSession001, TestSize.Level1)
288 {
289 if (g_testWay != PASSIVE_OPENAUTHSESSION_WAY) {
290 printf("skip testPassiveOpenAuthSession001 test.");
291 return;
292 }
293 printf("test begin testPassiveOpenAuthSession001 \r\n");
294 int32_t ret = TestCreateSessionServer();
295 EXPECT_EQ(SOFTBUS_OK, ret);
296 int32_t count = 10;
297 printf("input the test count: \n");
298 if (scanf_s("%d", &count, sizeof(count)) < 0) {
299 printf("input error!\n");
300 EXPECT_EQ(SOFTBUS_OK, SOFTBUS_ERR);
301 return;
302 }
303 getchar();
304 ret = TestWaitOpenSession(count);
305 EXPECT_EQ(SOFTBUS_OK, ret);
306 sleep(CONN_SLEEP_TIME);
307 sleep(CONN_SLEEP_TIME);
308 TestCloseSession();
309 };
310
311 /*
312 * @tc.name: testActiveOpenAuthSession001
313 * @tc.desc: test active open auth session
314 * @tc.type: FUNC
315 * @tc.require:AR000GIRGG
316 */
317 HWTEST_F(AuthSessionTest, testActiveOpenAuthSession001, TestSize.Level1)
318 {
319 if (g_testWay != ACTIVE_OPENAUTHSESSION_WAY) {
320 printf("skip testActiveOpenAuthSession001 test.");
321 return;
322 }
323 printf("test begin testActiveOpenAuthSession001 \r\n");
324 int32_t ret = TestCreateSessionServer();
325 EXPECT_EQ(SOFTBUS_OK, ret);
326 g_addrInfo[0].type = CONNECTION_ADDR_BR;
327 printf("input macaddr: \n");
328 if (scanf_s("%s", g_addrInfo[0].info.br.brMac, BT_MAC_LEN) < 0) {
329 printf("input error!\n");
330 EXPECT_EQ(SOFTBUS_OK, SOFTBUS_ERR);
331 return;
332 }
333 printf("brMac: %s\n", g_addrInfo[0].info.br.brMac);
334 getchar();
335 int32_t count = 10;
336 printf("input the test count: \n");
337 if (scanf_s("%d", &count, sizeof(count)) < 0) {
338 printf("input error!\n");
339 EXPECT_EQ(SOFTBUS_OK, SOFTBUS_ERR);
340 return;
341 }
342 char *testData = (char *)SoftBusCalloc(SEND_DATA_SIZE_40K + 1);
343 if (testData == nullptr) {
344 printf("SoftBusCalloc error!\n");
345 EXPECT_EQ(SOFTBUS_OK, SOFTBUS_ERR);
346 return;
347 }
348 if (memcpy_s(testData, SEND_DATA_SIZE_40K + 1, g_testData, strlen(g_testData)) != EOK) {
349 printf("memcpy_s g_testData failed!\n");
350 SoftBusFree(testData);
351 return;
352 }
353 for (int32_t i = 0; i < count; i++) {
354 ret = TestOpenAuthSession(&(g_addrInfo[0]), false);
355 EXPECT_EQ(SOFTBUS_OK, ret);
356 ret = TestSendData(g_sessionId, testData, SEND_DATA_SIZE_40K + 1);
357 EXPECT_EQ(SOFTBUS_OK, ret);
358 sleep(CONN_SLEEP_TIME);
359 TestCloseSession();
360 SoftBusSleepMs(CLOSE_DELAY_TIME);
361 }
362 SoftBusFree(testData);
363 };
364
365 /*
366 * @tc.name: testActiveOpenAuthSession002
367 * @tc.desc: test active open 2 auth session
368 * @tc.type: FUNC
369 * @tc.require:AR000GIRGG
370 */
371 HWTEST_F(AuthSessionTest, testActiveOpenAuthSession002, TestSize.Level1)
372 {
373 if (g_testWay != ACTIVE_OPENAUTHSESSION_WAY) {
374 printf("skip testActiveOpenAuthSession002 test.");
375 return;
376 }
377 printf("test begin testActiveOpenAuthSession002 \r\n");
378 int32_t ret = TestCreateSessionServer();
379 EXPECT_EQ(SOFTBUS_OK, ret);
380 char *testData = (char *)SoftBusCalloc(SEND_DATA_SIZE_1K);
381 if (testData == nullptr) {
382 printf("SoftBusCalloc error!\n");
383 EXPECT_EQ(SOFTBUS_OK, SOFTBUS_ERR);
384 return;
385 }
386 if (memcpy_s(testData, SEND_DATA_SIZE_1K, g_testData, strlen(g_testData)) != EOK) {
387 printf("memcpy_s g_testData failed!\n");
388 SoftBusFree(testData);
389 return;
390 }
391 ret = TestOpenAuthSession(&(g_addrInfo[0]), true);
392 EXPECT_EQ(SOFTBUS_OK, ret);
393 ret = TestSendData(g_sessionId, testData, SEND_DATA_SIZE_1K);
394 ret = TestSendData(g_sessionIdTwo, testData, SEND_DATA_SIZE_1K);
395 EXPECT_EQ(SOFTBUS_OK, ret);
396 sleep(CONN_SLEEP_TIME);
397 TestCloseSession();
398 TestCloseSessionTwo();
399 SoftBusSleepMs(CLOSE_DELAY_TIME);
400 SoftBusFree(testData);
401 };
402 } // namespace OHOS