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