1 /*
2 * Copyright (c) 2021-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 <securec.h>
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <unistd.h>
20
21 #include "inner_session.h"
22 #include "session.h"
23 #include "softbus_adapter_mem.h"
24 #include "softbus_bus_center.h"
25 #include "softbus_common.h"
26 #include "softbus_errcode.h"
27 #include "softbus_utils.h"
28
29 #define TICK_TIME 1
30 #define CREATE_SESSION_CASE 0
31 #define OPEN_SESSION_CASE 1
32 #define SEND_DATA_TEST_CASE 2
33 #define WAIT_OPEN_SESSION_CASE 3
34
35 #define TEST_CASE_NUM 10
36 #define MAXT_WAIT_COUNT 6
37 #define WIFI_CONFIG_INTERVAL 10
38 #define TEST_COUNT_INTREVAL 5
39 #define WAIT_SERVER_READY 5
40 #define MAX_TEST_COUNT 8
41 #define NSTACKX_MAX_IP_STRING_LEN 20
42 #define DISC_TEST_PKG_NAME "com.plrdtest"
43 static const char *g_testModuleName = DISC_TEST_PKG_NAME;
44 static const char *g_testSessionName = "com.plrdtest.dsoftbus";
45 static ISessionListener g_sessionlistener;
46 static SessionAttribute g_sessionAttr;
47 static bool g_successFlag = false;
48
49 #define CONN_ADDR_INFO_COUNT 5
50 static int32_t g_sessionId = -1;
51 ConnectionAddr g_addrInfo[CONN_ADDR_INFO_COUNT];
52
53 static int32_t g_connectCnt = 0;
54
OnSessionOpened(int32_t sessionId,int32_t result)55 static int32_t OnSessionOpened(int32_t sessionId, int32_t result)
56 {
57 printf("############# session opened,sesison id[%d] result[%d]\n", sessionId, result);
58 if (result == SOFTBUS_OK) {
59 if (g_sessionId == -1 || sessionId == g_sessionId) {
60 if (g_sessionId == -1) {
61 g_connectCnt++;
62 g_sessionId = sessionId;
63 }
64 g_successFlag = true;
65 }
66 }
67
68 return result;
69 }
70
OnSessionClosed(int32_t sessionId)71 static void OnSessionClosed(int32_t sessionId)
72 {
73 printf("############# session closed, session id = %d\n", sessionId);
74 g_sessionId = -1;
75 g_successFlag = false;
76 }
77
OnBytesReceived(int32_t sessionId,const void * data,uint32_t len)78 static void OnBytesReceived(int32_t sessionId, const void *data, uint32_t len)
79 {
80 if (g_sessionId == -1 || sessionId == g_sessionId) {
81 printf("client bytes received, data[%s], dataLen[%u]\n", (char *)data, len);
82 } else {
83 printf("server bytes received, sessionid[%d], data[%s], dataLen[%u]\n", sessionId, (char *)data, len);
84 }
85 }
86
OnMessageReceived(int32_t sessionId,const void * data,uint32_t len)87 static void OnMessageReceived(int32_t sessionId, const void *data, uint32_t len)
88 {
89 if (g_sessionId == -1 || sessionId == g_sessionId) {
90 printf("client msg received, data[%s], dataLen[%u]\n", (char *)data, len);
91 } else {
92 printf("server msg received, sessionid[%d], data[%s], dataLen[%u]\n", sessionId, (char *)data, len);
93 }
94 }
95
TestSessionListenerInit(void)96 static void TestSessionListenerInit(void)
97 {
98 g_sessionlistener.OnSessionOpened = OnSessionOpened;
99 g_sessionlistener.OnSessionClosed = OnSessionClosed;
100 g_sessionlistener.OnBytesReceived = OnBytesReceived;
101 g_sessionlistener.OnMessageReceived = OnMessageReceived;
102 }
103
104 static const char *g_testData = "{\n \"data\":\"open auth session test!!!\"\n}";
105
TestSendBytesData(const char * data,int32_t len)106 static int32_t TestSendBytesData(const char *data, int32_t len)
107 {
108 printf("SendBytes start\n");
109 int32_t ret = SendBytes(g_sessionId, data, len);
110 if (ret != SOFTBUS_OK) {
111 printf("SendBytes failed ret[%d] len[%u]\n", ret, len);
112 }
113 printf("SendBytes end\n");
114 return ret;
115 }
116
TestSendMessageData(const char * data,int32_t len)117 static int32_t TestSendMessageData(const char *data, int32_t len)
118 {
119 printf("SendMessage start\n");
120 int32_t ret = SendMessage(g_sessionId, data, len);
121 if (ret != SOFTBUS_OK) {
122 printf("SendMessage failed ret[%d] len[%u]\n", ret, len);
123 }
124 printf("SendMessage end\n");
125 return ret;
126 }
127
TestCreateSessionServer(int32_t testWay)128 static int32_t TestCreateSessionServer(int32_t testWay)
129 {
130 int32_t state = -1;
131 int32_t ret = CreateSessionServer(g_testModuleName, g_testSessionName, &g_sessionlistener);
132 printf("CreateSessionServer ret: %d \n", ret);
133 if (ret != SOFTBUS_SERVER_NAME_REPEATED && ret != SOFTBUS_OK) {
134 printf("CreateSessionServer ret: %d \n", ret);
135 } else if (testWay == 1) {
136 state = OPEN_SESSION_CASE;
137 } else if (testWay == 0) {
138 state = WAIT_OPEN_SESSION_CASE;
139 }
140 return state;
141 }
142
TestCloseSession(void)143 static void TestCloseSession(void)
144 {
145 printf("TestCloseSession exit\n");
146 if (g_sessionId > 0) {
147 CloseSession(g_sessionId);
148 g_sessionId = -1;
149 }
150 }
151
TestOpenAuthSession(void)152 static int32_t TestOpenAuthSession(void)
153 {
154 printf("OpenAuthSession start\n");
155 int32_t state = -1;
156 g_addrInfo[0].type = CONNECTION_ADDR_BR;
157 printf("input macaddr: \n");
158 if (scanf_s("%s", g_addrInfo[0].info.br.brMac, BT_MAC_LEN) < 0) {
159 printf("input error!\n");
160 return OPEN_SESSION_CASE;
161 }
162 printf("brMac: %s\n", g_addrInfo[0].info.br.brMac);
163 g_sessionId = OpenAuthSession(g_testSessionName, &(g_addrInfo[0]), 1, NULL);
164 if (g_sessionId < 0) {
165 printf("OpenAuthSession ret[%d]", g_sessionId);
166 } else {
167 state = SEND_DATA_TEST_CASE;
168 }
169 printf("OpenAuthSession end\n");
170 return state;
171 }
172
173 #define SEND_DATA_SIZE_1K 1024
174 #define SEND_DATA_SIZE_4K (4 * 1024)
175 #define SEND_DATA_SIZE_40K (40 * 1000 - 8)
176 #define SEND_DATA_SIZE_64K (64 * 1024)
GetSize(char cSize)177 static int32_t GetSize(char cSize)
178 {
179 int32_t size = SEND_DATA_SIZE_64K + 1;
180 if (cSize == '0') {
181 size = SEND_DATA_SIZE_1K;
182 } else if (cSize == '1') {
183 size = SEND_DATA_SIZE_4K;
184 } else if (cSize == '2') {
185 size = SEND_DATA_SIZE_40K;
186 } else if (cSize == '3') {
187 size = SEND_DATA_SIZE_40K + 1;
188 } else if (cSize == '4') {
189 size = SEND_DATA_SIZE_64K;
190 }
191 return size;
192 }
TestAuthSessionSendData(const char * testData,int32_t count,char cSize)193 static int32_t TestAuthSessionSendData(const char *testData, int32_t count, char cSize)
194 {
195 int32_t waitCnt = 0;
196 while (!g_successFlag) {
197 printf("wait OpenAuthSession success Cnt: %d *******\n", waitCnt);
198 waitCnt++;
199 if (waitCnt > count) {
200 printf("wait OpenAuthSession success timeout!\n");
201 return SOFTBUS_TIMOUT;
202 }
203 sleep(TICK_TIME);
204 }
205 int32_t size = GetSize(cSize);
206 int32_t sendCnt = 0;
207 int32_t ret;
208 while (sendCnt < count) {
209 printf("******* sendCnt[%d] *******\n", sendCnt);
210 ret = TestSendBytesData(testData, size);
211 if (size <= SEND_DATA_SIZE_64K) {
212 if (ret != SOFTBUS_CONNECTION_ERR_SENDQUEUE_FULL && ret != SOFTBUS_OK) {
213 printf("******* TestSendBytesData %d failed *******\n", size);
214 return ret;
215 }
216 } else {
217 if (ret == SOFTBUS_OK) {
218 printf("******* TestSendBytesData %d failed *******\n", size);
219 return SOFTBUS_AUTH_SEND_FAIL;
220 }
221 }
222 sleep(TICK_TIME);
223 ret = TestSendMessageData(testData, size);
224 if (size <= SEND_DATA_SIZE_4K) {
225 if (ret != SOFTBUS_CONNECTION_ERR_SENDQUEUE_FULL && ret != SOFTBUS_OK) {
226 printf("******* TestSendMessageData %d failed *******\n", size);
227 return ret;
228 }
229 } else {
230 if (ret == SOFTBUS_OK) {
231 printf("******* TestSendMessageData %d failed *******\n", size);
232 return SOFTBUS_AUTH_SEND_FAIL;
233 }
234 }
235 sendCnt++;
236 }
237 return SOFTBUS_OK;
238 }
239
DiscoveryTestEntry(int32_t testWay,int32_t count)240 static void DiscoveryTestEntry(int32_t testWay, int32_t count)
241 {
242 TestSessionListenerInit();
243 g_sessionAttr.dataType = TYPE_BYTES;
244 int32_t stat = 0;
245 char *testData = (char *)SoftBusCalloc(SEND_DATA_SIZE_64K + 1);
246 if (testData == NULL) {
247 printf("DiscoveryTestEntry malloc failed!\n");
248 return;
249 }
250 if (memcpy_s(testData, SEND_DATA_SIZE_64K + 1, g_testData, strlen(g_testData)) != EOK) {
251 printf("memcpy_s g_testData failed!\n");
252 SoftBusFree(testData);
253 return;
254 }
255 int32_t ret = SOFTBUS_OK;
256 while (true) {
257 if (stat == CREATE_SESSION_CASE) {
258 stat = TestCreateSessionServer(testWay);
259 } else if (stat == OPEN_SESSION_CASE) {
260 stat = TestOpenAuthSession();
261 } else if (stat == SEND_DATA_TEST_CASE) {
262 getchar();
263 char cSize;
264 printf("data size(0:1K, 1:4K, 2:40000, 3:40001, 4:64K, 5:>64K, q:exit): \n");
265 if (scanf_s("%c", &cSize, 1) < 0) {
266 printf("input error!\n");
267 continue;
268 }
269 if (cSize == 'q') {
270 stat = -1;
271 continue;
272 }
273 ret = TestAuthSessionSendData(testData, count, cSize);
274 if (ret != SOFTBUS_OK) {
275 stat = -1;
276 }
277 } else if (stat == WAIT_OPEN_SESSION_CASE && g_connectCnt >= count) {
278 stat = -1;
279 } else if (stat == -1) {
280 TestCloseSession();
281 break;
282 }
283 sleep(TICK_TIME);
284 }
285 SoftBusFree(testData);
286 printf("Test Auth Channel %s!\n", ret == SOFTBUS_OK ? "OK" : "failed");
287 }
288
main(int32_t argc,char * argv[])289 int32_t main(int32_t argc, char *argv[])
290 {
291 #define ARGC_NUM 2
292 if (argc <= ARGC_NUM) {
293 printf("error argc <= 2\n");
294 return -1;
295 }
296 int32_t testWay = atoi(argv[1]);
297 int32_t count = atoi(argv[ARGC_NUM]);
298 DiscoveryTestEntry(testWay, count);
299 }
300