• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_log.h"
34 #include "softbus_utils.h"
35 
36 using namespace testing::ext;
37 
38 namespace OHOS {
39 enum TEST_WAY {
40     PASSIVE_OPENAUTHSESSION_WAY = 0,
41     ACTIVE_OPENAUTHSESSION_WAY
42 };
43 
44 const int CONN_SINGLE_WAIT_TIMEOUT = 5;
45 const int CONN_SLEEP_TIME = 1;
46 const int CLOSE_DELAY_TIME = 500;
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 SOFTBUS_ERR;
72     }
73     ret = SendBytes(sessionId, data, SEND_DATA_SIZE_4K);
74     if (ret != SOFTBUS_OK) {
75         printf("SendBytes 4K err.\n");
76         return SOFTBUS_ERR;
77     }
78     ret = SendBytes(sessionId, data, SEND_DATA_SIZE_40K);
79     if (ret != SOFTBUS_OK) {
80         printf("SendBytes 40000 err.\n");
81         return SOFTBUS_ERR;
82     }
83     ret = SendBytes(sessionId, data, SEND_DATA_SIZE_40K + 1);
84     if (ret == SOFTBUS_OK) {
85         printf("SendBytes 40001 err.\n");
86         return SOFTBUS_ERR;
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 SOFTBUS_ERR;
97     }
98     ret = SendMessage(sessionId, data, SEND_DATA_SIZE_4K);
99     if (ret != SOFTBUS_OK) {
100         printf("SendMessage 4K err.\n");
101         return SOFTBUS_ERR;
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 SOFTBUS_ERR;
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 SOFTBUS_ERR;
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_ERR;
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_ERR;
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_ERR;
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_ERR : 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         EXPECT_EQ(SOFTBUS_OK, SOFTBUS_OK);
293         return;
294     }
295     printf("test begin testPassiveOpenAuthSession001 \r\n");
296     int32_t ret = TestCreateSessionServer();
297     EXPECT_EQ(SOFTBUS_OK, ret);
298     int32_t count = 10;
299     printf("input the test count: \n");
300     if (scanf_s("%d", &count, sizeof(count)) < 0) {
301         printf("input error!\n");
302         EXPECT_EQ(SOFTBUS_OK, SOFTBUS_ERR);
303         return;
304     }
305     getchar();
306     ret = TestWaitOpenSession(count);
307     EXPECT_EQ(SOFTBUS_OK, ret);
308     sleep(CONN_SLEEP_TIME);
309     sleep(CONN_SLEEP_TIME);
310     TestCloseSession();
311 };
312 
313 /*
314 * @tc.name: testActiveOpenAuthSession001
315 * @tc.desc: test active open auth session
316 * @tc.type: FUNC
317 * @tc.require:AR000GIRGG
318 */
319 HWTEST_F(AuthSessionTest, testActiveOpenAuthSession001, TestSize.Level1)
320 {
321     if (g_testWay != ACTIVE_OPENAUTHSESSION_WAY) {
322         printf("skip testActiveOpenAuthSession001 test.");
323         EXPECT_EQ(SOFTBUS_OK, SOFTBUS_OK);
324         return;
325     }
326     printf("test begin testActiveOpenAuthSession001 \r\n");
327     int32_t ret = TestCreateSessionServer();
328     EXPECT_EQ(SOFTBUS_OK, ret);
329     g_addrInfo[0].type = CONNECTION_ADDR_BR;
330     printf("input macaddr: \n");
331     if (scanf_s("%s", g_addrInfo[0].info.br.brMac, BT_MAC_LEN) < 0) {
332         printf("input error!\n");
333         EXPECT_EQ(SOFTBUS_OK, SOFTBUS_ERR);
334         return;
335     }
336     printf("brMac: %s\n", g_addrInfo[0].info.br.brMac);
337     getchar();
338     int32_t count = 10;
339     printf("input the test count: \n");
340     if (scanf_s("%d", &count, sizeof(count)) < 0) {
341         printf("input error!\n");
342         EXPECT_EQ(SOFTBUS_OK, SOFTBUS_ERR);
343         return;
344     }
345     char *testData = (char *)SoftBusCalloc(SEND_DATA_SIZE_40K + 1);
346     if (testData == nullptr) {
347         printf("SoftBusCalloc error!\n");
348         EXPECT_EQ(SOFTBUS_OK, SOFTBUS_ERR);
349         return;
350     }
351     if (memcpy_s(testData, SEND_DATA_SIZE_40K + 1, g_testData, strlen(g_testData)) != EOK) {
352         printf("memcpy_s g_testData failed!\n");
353         SoftBusFree(testData);
354         return;
355     }
356     for (int32_t i = 0; i < count; i++) {
357         ret = TestOpenAuthSession(&(g_addrInfo[0]), false);
358         EXPECT_EQ(SOFTBUS_OK, ret);
359         ret = TestSendData(g_sessionId, testData, SEND_DATA_SIZE_40K + 1);
360         EXPECT_EQ(SOFTBUS_OK, ret);
361         sleep(CONN_SLEEP_TIME);
362         TestCloseSession();
363         SoftBusSleepMs(CLOSE_DELAY_TIME);
364     }
365     SoftBusFree(testData);
366     EXPECT_EQ(SOFTBUS_OK, SOFTBUS_OK);
367 };
368 
369 /*
370 * @tc.name: testActiveOpenAuthSession002
371 * @tc.desc: test active open 2 auth session
372 * @tc.type: FUNC
373 * @tc.require:AR000GIRGG
374 */
375 HWTEST_F(AuthSessionTest, testActiveOpenAuthSession002, TestSize.Level1)
376 {
377     if (g_testWay != ACTIVE_OPENAUTHSESSION_WAY) {
378         printf("skip testActiveOpenAuthSession002 test.");
379         EXPECT_EQ(SOFTBUS_OK, SOFTBUS_OK);
380         return;
381     }
382     printf("test begin testActiveOpenAuthSession002 \r\n");
383     int32_t ret = TestCreateSessionServer();
384     EXPECT_EQ(SOFTBUS_OK, ret);
385     char *testData = (char *)SoftBusCalloc(SEND_DATA_SIZE_1K);
386     if (testData == nullptr) {
387         printf("SoftBusCalloc error!\n");
388         EXPECT_EQ(SOFTBUS_OK, SOFTBUS_ERR);
389         return;
390     }
391     if (memcpy_s(testData, SEND_DATA_SIZE_1K, g_testData, strlen(g_testData)) != EOK) {
392         printf("memcpy_s g_testData failed!\n");
393         SoftBusFree(testData);
394         return;
395     }
396     ret = TestOpenAuthSession(&(g_addrInfo[0]), true);
397     EXPECT_EQ(SOFTBUS_OK, ret);
398     ret = TestSendData(g_sessionId, testData, SEND_DATA_SIZE_1K);
399     ret = TestSendData(g_sessionIdTwo, testData, SEND_DATA_SIZE_1K);
400     EXPECT_EQ(SOFTBUS_OK, ret);
401     sleep(CONN_SLEEP_TIME);
402     TestCloseSession();
403     TestCloseSessionTwo();
404     SoftBusSleepMs(CLOSE_DELAY_TIME);
405     SoftBusFree(testData);
406     EXPECT_EQ(SOFTBUS_OK, SOFTBUS_OK);
407 };
408 } // namespace OHOS