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