1 /* This case tests websocket connect corresponding logic, especially focusing on testing secure connect feature */
2 #include "tx_api.h"
3 #include "nx_api.h"
4
5 extern void test_control_return(UINT);
6
7 #if !defined(NX_DISABLE_IPV4) && defined(__PRODUCT_NETXDUO__) && !defined(NX_DISABLE_PACKET_CHAIN)
8 #include "nx_websocket_client.h"
9 #include "netx_websocket_common_process.c"
10
11 #define DEMO_STACK_SIZE 4096
12 #define PACKET_SIZE 1536
13 #define TOTAL_SIZE DEMO_STACK_SIZE + (PACKET_SIZE * 8) + 2048 + 1024
14
15 /* Define device drivers. */
16 extern void _nx_ram_network_driver_1024(NX_IP_DRIVER *driver_req_ptr);
17
18 static UINT test_done = NX_FALSE;
19
20 static TX_THREAD client_thread;
21 static NX_PACKET_POOL client_pool;
22 static NX_TCP_SOCKET test_client;
23 static NX_IP client_ip;
24
25 static NX_TCP_SOCKET test_server;
26 static NX_PACKET_POOL server_pool;
27 static TX_THREAD server_thread;
28 static NX_IP server_ip;
29 static UINT test_server_start = 0;
30 static UINT test_client_stop = 0;
31
32 /* Set up the websocket global variables */
33 static NX_WEBSOCKET_CLIENT client_websocket;
34 static UCHAR *client_websocket_host;
35 static UINT client_websocket_host_length;
36 static UCHAR *client_websocket_uri_path;
37 static UINT client_websocket_uri_path_length;
38
39 #ifdef NX_SECURE_ENABLE
40
41 #include "../web_test/test_device_cert.c"
42 #include "../web_test/test_ca_cert.c"
43
44 /* Declare external cryptosuites. */
45 extern const NX_SECURE_TLS_CRYPTO nx_crypto_tls_ciphers;
46
47 static NX_SECURE_TLS_SESSION tls_client_session;
48 static NX_SECURE_TLS_SESSION tls_server_session;
49 static NX_SECURE_X509_CERT server_local_certificate;
50
51 /* Define crypto metadata buffer. */
52 static UCHAR client_metadata[5*4096];
53 static UCHAR server_metadata[5*4096];
54
55 /* For remote certificate. */
56 static NX_SECURE_X509_CERT remote_certificate, remote_issuer, ca_certificate;
57 static UCHAR remote_cert_buffer[2000];
58 static UCHAR remote_issuer_buffer[2000];
59 static UCHAR tls_packet_buffer[2][4096];
60
61 #endif /* NX_SECURE_ENABLE */
62
63
64 static void thread_client_entry(ULONG thread_input);
65 static void thread_server_entry(ULONG thread_input);
66
67 #define TEST_SERVER_ADDRESS IP_ADDRESS(1,2,3,4)
68 #define TEST_CLIENT_ADDRESS IP_ADDRESS(1,2,3,5)
69 #define TEST_SERVER_PORT 80
70
71 #define TEST_HOST_NAME "1.2.3.4"
72 #define TEST_URI_PATH "/test"
73 #define TEST_PROTOCOL "test"
74
75 static UCHAR bad_server_switch_101[] =
76 {
77 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x20, // HTTP1.1/
78 0x31, 0x30, 0x31, 0x20, 0x53, 0x77, 0x69, 0x74, 0x63, 0x68, 0x69, 0x6e, 0x67, 0x20, // 101 Switching
79 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x73, //0x0d, 0x0a, // Protocols\r\n
80 0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x3a, 0x20, // Upgrade:
81 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x0d, 0x0a, // WebSocket\r\n
82 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, // Connection:
83 0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x0d, 0x0a, // Upgrade\r\n
84 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65, // Sec-WebSocket-Protocol:
85 0x74, 0x2d, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x3a, 0x20,
86 0x74, 0x65, 0x73, 0x74, 0x0d, 0x0a, // test
87 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, // Sec-WebSocket-Accept:
88 0x65, 0x74, 0x2d, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x3a, 0x20,
89 0x35, 0x75, 0x31, 0x6c, 0x55, 0x72, 0x32, 0x57, 0x68, 0x70, 0x34, 0x64, 0x44, 0x57, 0x6e, // 5u1lUr2Whp4dDWnskk9JcJZobO0=
90 0x73, 0x6b, 0x6b, 0x39, 0x4a, 0x63, 0x4a, 0x5a, 0x6f, 0x62, 0x4f, 0x30, 0x3d, 0x0d, 0x0a,
91 0x0d, 0x0a,
92 };
93
94 static UCHAR server_switch_101[] =
95 {
96 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x20, // HTTP1.1/
97 0x31, 0x30, 0x31, 0x20, 0x53, 0x77, 0x69, 0x74, 0x63, 0x68, 0x69, 0x6e, 0x67, 0x20, // 101 Switching
98 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x73, 0x0d, 0x0a, // Protocols\r\n
99 0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x3a, 0x20, // Upgrade:
100 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x0d, 0x0a, // WebSocket\r\n
101 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, // Connection:
102 0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x0d, 0x0a, // Upgrade\r\n
103 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65, // Sec-WebSocket-Protocol:
104 0x74, 0x2d, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x3a, 0x20,
105 0x74, 0x65, 0x73, 0x74, 0x0d, 0x0a, // test
106 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, // Sec-WebSocket-Accept:
107 0x65, 0x74, 0x2d, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x3a, 0x20,
108 0x35, 0x75, 0x31, 0x6c, 0x55, 0x72, 0x32, 0x57, 0x68, 0x70, 0x34, 0x64, 0x44, 0x57, 0x6e, // 5u1lUr2Whp4dDWnskk9JcJZobO0=
109 0x73, 0x6b, 0x6b, 0x39, 0x4a, 0x63, 0x4a, 0x5a, 0x6f, 0x62, 0x4f, 0x30, 0x3d, 0x0d, 0x0a,
110 0x0d, 0x0a,
111 };
112
113 static UCHAR server_response_1[] =
114 {
115 0x82, 0x04, 0x01, 0x02, 0x03, 0x04,
116 };
117
118 static UCHAR client_test_data[] =
119 {
120 0x11, 0x22, 0x33, 0x44,
121 };
122
123 static ULONG error_counter;
124
125 extern void SET_ERROR_COUNTER(ULONG *error_counter, CHAR *filename, int line_number);
126
127 #define TEST_LOOP 3
128
129 #ifdef CTEST
test_application_define(void * first_unused_memory)130 VOID test_application_define(void *first_unused_memory)
131 #else
132 void netx_websocket_connect_test_application_define(void *first_unused_memory)
133 #endif
134 {
135 CHAR *pointer;
136 UINT status;
137
138
139 error_counter = 0;
140
141 /* Setup the working pointer. */
142 pointer = (CHAR *) first_unused_memory;
143
144 /* Create a helper thread for the server. */
145 tx_thread_create(&server_thread, "Test Server thread", thread_server_entry, 0,
146 pointer, DEMO_STACK_SIZE,
147 4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
148
149 pointer = pointer + DEMO_STACK_SIZE;
150
151 /* Initialize the NetX system. */
152 nx_system_initialize();
153
154 /* Create the server packet pool. */
155 status = nx_packet_pool_create(&server_pool, "Test Server Packet Pool", PACKET_SIZE,
156 pointer, PACKET_SIZE * 8);
157 pointer = pointer + PACKET_SIZE * 8;
158 if (status)
159 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
160
161 /* Create an IP instance. */
162 status = nx_ip_create(&server_ip, "Test Server IP", TEST_SERVER_ADDRESS,
163 0xFFFFFF00UL, &server_pool, _nx_ram_network_driver_1024,
164 pointer, 2048, 1);
165 pointer = pointer + 2048;
166 if (status)
167 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
168
169 /* Enable ARP and supply ARP cache memory for the server IP instance. */
170 status = nx_arp_enable(&server_ip, (void *) pointer, 1024);
171 pointer = pointer + 1024;
172 if (status)
173 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
174
175
176 /* Enable TCP traffic. */
177 status = nx_tcp_enable(&server_ip);
178 if (status)
179 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
180
181 /* Create the Test Client thread. */
182 status = tx_thread_create(&client_thread, "Test Client", thread_client_entry, 0,
183 pointer, DEMO_STACK_SIZE,
184 6, 6, TX_NO_TIME_SLICE, TX_AUTO_START);
185 pointer = pointer + DEMO_STACK_SIZE;
186 if (status)
187 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
188
189 /* Create the Client packet pool. */
190 status = nx_packet_pool_create(&client_pool, "Test Client Packet Pool", PACKET_SIZE,
191 pointer, PACKET_SIZE * 8);
192 pointer = pointer + PACKET_SIZE * 8;
193 if (status)
194 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
195
196 /* Create an IP instance. */
197 status = nx_ip_create(&client_ip, "Test Client IP", TEST_CLIENT_ADDRESS,
198 0xFFFFFF00UL, &client_pool, _nx_ram_network_driver_1024,
199 pointer, 2048, 1);
200 pointer = pointer + 2048;
201 if (status)
202 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
203
204 status = nx_arp_enable(&client_ip, (void *) pointer, 1024);
205 pointer = pointer + 1024;
206 if (status)
207 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
208
209 /* Enable TCP traffic. */
210 status = nx_tcp_enable(&client_ip);
211 if (status)
212 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
213 }
214
215 #ifdef NX_SECURE_ENABLE
216
217 /* Define the callback function for tls connection. */
client_tls_setup(NX_SECURE_TLS_SESSION * tls_session)218 static UINT client_tls_setup(NX_SECURE_TLS_SESSION* tls_session)
219 {
220 UINT status;
221
222 /* Create a tls session. */
223 status = nx_secure_tls_session_create(tls_session,
224 &nx_crypto_tls_ciphers,
225 client_metadata,
226 sizeof(client_metadata));
227
228 if (status)
229 {
230 return status;
231 }
232
233 nx_secure_tls_session_packet_buffer_set(tls_session, tls_packet_buffer[0], sizeof(tls_packet_buffer[0]));
234 nx_secure_tls_remote_certificate_allocate(tls_session, &remote_certificate, remote_cert_buffer, sizeof(remote_cert_buffer));
235 nx_secure_tls_remote_certificate_allocate(tls_session, &remote_issuer, remote_issuer_buffer, sizeof(remote_issuer_buffer));
236
237 nx_secure_x509_certificate_initialize(&ca_certificate, test_ca_cert_der, test_ca_cert_der_len,
238 NX_NULL, 0, NX_NULL, 0, NX_SECURE_X509_KEY_TYPE_NONE);
239 nx_secure_tls_trusted_certificate_add(tls_session, &ca_certificate);
240
241 return(NX_SUCCESS);
242 }
243
server_tls_setup(NX_SECURE_TLS_SESSION * tls_session)244 static UINT server_tls_setup(NX_SECURE_TLS_SESSION *tls_session)
245 {
246 UINT status;
247
248 status = nx_secure_tls_session_create(tls_session,
249 &nx_crypto_tls_ciphers,
250 server_metadata,
251 sizeof(server_metadata));
252 if (status)
253 {
254 return status;
255 }
256
257 memset(&server_local_certificate, 0, sizeof(server_local_certificate));
258 nx_secure_x509_certificate_initialize(&server_local_certificate,
259 test_device_cert_der, test_device_cert_der_len,
260 NX_NULL, 0, test_device_cert_key_der,
261 test_device_cert_key_der_len, NX_SECURE_X509_KEY_TYPE_RSA_PKCS1_DER);
262
263 nx_secure_tls_local_certificate_add(tls_session, &server_local_certificate);
264
265 nx_secure_tls_session_packet_buffer_set(tls_session, tls_packet_buffer[1], sizeof(tls_packet_buffer[1]));
266
267 return(NX_SUCCESS);
268 }
269 #endif /* NX_SECURE_ENABLE */
270
thread_client_entry(ULONG thread_input)271 void thread_client_entry(ULONG thread_input)
272 {
273 UINT i, status;
274 NX_PACKET *packet_ptr;
275 NX_PACKET *packet_ptr1;
276 NXD_ADDRESS server_ip_address;
277 UINT code;
278
279 /* Create client socket. */
280 status = nx_tcp_socket_create(&client_ip, &test_client, "Client Socket", NX_IP_NORMAL, NX_FRAGMENT_OKAY,
281 NX_IP_TIME_TO_LIVE, 1000, NX_NULL, NX_NULL);
282 if (status)
283 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
284
285 /* Create WebSocket. */
286 status = nx_websocket_client_create(&client_websocket, (UCHAR *)" ", &client_ip, &client_pool);
287 if (status || client_websocket.nx_websocket_client_mutex.tx_mutex_ownership_count != 0)
288 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
289
290 /* Give IP task and driver a chance to initialize the system. */
291 tx_thread_sleep(NX_IP_PERIODIC_RATE);
292
293 /* Bind and connect to server. */
294 status = nx_tcp_client_socket_bind(&test_client, TEST_SERVER_PORT, NX_IP_PERIODIC_RATE);
295 if (status)
296 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
297
298 /* Wait test server started. */
299 while(!test_server_start)
300 {
301 tx_thread_sleep(NX_IP_PERIODIC_RATE);
302 }
303
304 /* Set server IP address. */
305 server_ip_address.nxd_ip_address.v4 = TEST_SERVER_ADDRESS;
306 server_ip_address.nxd_ip_version = NX_IP_VERSION_V4;
307
308 status = nxd_tcp_client_socket_connect(&test_client, &server_ip_address, TEST_SERVER_PORT, NX_IP_PERIODIC_RATE);
309 if (status)
310 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
311
312 /* Upgrade to websocket */
313 status = nx_websocket_client_connect(&client_websocket, &test_client,
314 TEST_HOST_NAME, sizeof(TEST_HOST_NAME) - 1,
315 (UCHAR *)TEST_URI_PATH, sizeof(TEST_URI_PATH) - 1,
316 (UCHAR *)TEST_PROTOCOL, sizeof(TEST_PROTOCOL) - 1,
317 NX_WAIT_FOREVER);
318
319 /* The first time is to test whether the bad response from server will be checked and found */
320 if (status != NX_WEBSOCKET_INVALID_PACKET || client_websocket.nx_websocket_client_mutex.tx_mutex_ownership_count != 0)
321 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
322
323 /* Upgrade to websocket */
324 status = nx_websocket_client_connect(&client_websocket, &test_client,
325 TEST_HOST_NAME, sizeof(TEST_HOST_NAME) - 1,
326 (UCHAR *)TEST_URI_PATH, sizeof(TEST_URI_PATH) - 1,
327 (UCHAR *)TEST_PROTOCOL, sizeof(TEST_PROTOCOL) - 1,
328 NX_WAIT_FOREVER);
329 if (status || client_websocket.nx_websocket_client_mutex.tx_mutex_ownership_count != 0)
330 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
331
332 status = nx_websocket_client_delete(&client_websocket);
333 if (status || client_websocket.nx_websocket_client_mutex.tx_mutex_ownership_count != 0)
334 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
335
336 #ifdef NX_SECURE_ENABLE
337 /* Re-create WebSocket for secure test. */
338 status = nx_websocket_client_create(&client_websocket, (UCHAR *)" ", &client_ip, &client_pool);
339 if (status || client_websocket.nx_websocket_client_mutex.tx_mutex_ownership_count != 0)
340 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
341
342 client_tls_setup(&tls_client_session);
343
344 status = nx_secure_tls_session_start(&tls_client_session, &test_client, NX_WAIT_FOREVER);
345 if (status)
346 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
347 status = nx_websocket_client_secure_connect(&client_websocket, &tls_client_session,
348 TEST_HOST_NAME, sizeof(TEST_HOST_NAME) - 1,
349 (UCHAR *)TEST_URI_PATH, sizeof(TEST_URI_PATH) - 1,
350 (UCHAR *)TEST_PROTOCOL, sizeof(TEST_PROTOCOL) - 1,
351 NX_WAIT_FOREVER);
352 if (status || client_websocket.nx_websocket_client_mutex.tx_mutex_ownership_count != 0)
353 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
354 else
355 {
356 status = nx_secure_tls_packet_allocate(&tls_client_session, &client_pool, &packet_ptr, NX_NO_WAIT);
357 if (status)
358 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
359 /* Append client test data. */
360 status = nx_packet_data_append(packet_ptr, client_test_data, sizeof(client_test_data), &client_pool, NX_NO_WAIT);
361 if (status)
362 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
363 status = nx_secure_tls_session_send(&tls_client_session, packet_ptr, NX_WAIT_FOREVER);
364 if (status)
365 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
366 status = nx_secure_tls_session_receive(&tls_client_session, &packet_ptr, NX_WAIT_FOREVER);
367 if (status)
368 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
369 nx_packet_release(packet_ptr);
370 }
371
372 nx_websocket_client_delete(&client_websocket);
373 if (client_websocket.nx_websocket_client_mutex.tx_mutex_ownership_count != 0)
374 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
375
376 /* End session. */
377 nx_secure_tls_session_delete(&tls_client_session);
378
379 #endif
380
381 /* TCP Disconnect. */
382 status = nx_tcp_socket_disconnect(&test_client, NX_IP_PERIODIC_RATE);
383 if (status)
384 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
385
386 nx_tcp_client_socket_unbind(&test_client);
387 nx_tcp_socket_delete(&test_client);
388
389 test_done = NX_TRUE;
390 }
391
392 /* Define the helper Test server thread. */
thread_server_entry(ULONG thread_input)393 void thread_server_entry(ULONG thread_input)
394 {
395 UINT i, status;
396 NX_PACKET *packet_ptr;
397
398
399 /* Print out test information banner. */
400 printf("NetX Test: Websocket Connect Test.....................................");
401
402 /* Check for earlier error. */
403 if (error_counter)
404 {
405 printf("ERROR!\n");
406 test_control_return(1);
407 }
408
409 /* Give NetX a chance to initialize the system. */
410 tx_thread_sleep(NX_IP_PERIODIC_RATE);
411
412 status = nx_tcp_socket_create(&server_ip, &test_server, "Test Server Socket",
413 NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE, 1000,
414 NX_NULL, NX_NULL);
415
416 status = nx_tcp_server_socket_listen(&server_ip, TEST_SERVER_PORT, &test_server, 5, NX_NULL);
417 if (status)
418 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
419
420 #ifdef NX_SECURE_ENABLE
421 /* Session setup. */
422 server_tls_setup(&tls_server_session);
423 #endif
424
425 /* Set the flag. */
426 test_server_start = 1;
427
428 /* Accept a connection from test client. */
429 status = nx_tcp_server_socket_accept(&test_server, 5 * NX_IP_PERIODIC_RATE);
430 if (status)
431 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
432
433 /* Bad server response test */
434 status = nx_tcp_socket_receive(&test_server, &packet_ptr, 5 * NX_IP_PERIODIC_RATE);
435 if (status)
436 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
437 else
438 {
439 packet_ptr -> nx_packet_append_ptr = packet_ptr -> nx_packet_prepend_ptr;
440 packet_ptr -> nx_packet_length = 0;
441 nx_packet_data_append(packet_ptr, bad_server_switch_101, sizeof(bad_server_switch_101), &server_pool, NX_IP_PERIODIC_RATE);
442 status = nx_tcp_socket_send(&test_server, packet_ptr, NX_IP_PERIODIC_RATE);
443 if (status)
444 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
445 }
446
447 /* ---- Insecure websocket connect test ---- */
448 status = nx_tcp_socket_receive(&test_server, &packet_ptr, 5 * NX_IP_PERIODIC_RATE);
449 if (status)
450 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
451 else
452 {
453 /* Update the value in the field Sec-Protocol-Accept since it is calculated based on a random value */
454 _server_connect_response_process(packet_ptr);
455 memcpy(&server_switch_101[127], connect_key, 28);
456
457 packet_ptr -> nx_packet_append_ptr = packet_ptr -> nx_packet_prepend_ptr;
458 packet_ptr -> nx_packet_length = 0;
459 nx_packet_data_append(packet_ptr, server_switch_101, sizeof(server_switch_101), &server_pool, NX_IP_PERIODIC_RATE);
460 status = nx_tcp_socket_send(&test_server, packet_ptr, NX_IP_PERIODIC_RATE);
461 if (status)
462 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
463 }
464
465 /* ---- Secure websocket connect test ---- */
466
467 #ifdef NX_SECURE_ENABLE
468
469 status = nx_secure_tls_session_start(&tls_server_session, &test_server, NX_WAIT_FOREVER);
470 if (status)
471 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
472 tx_thread_sleep(1);
473
474 status = nx_secure_tls_session_receive(&tls_server_session, &packet_ptr, NX_WAIT_FOREVER);
475 if (status)
476 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
477 _server_connect_response_process(packet_ptr);
478 memcpy(&server_switch_101[127], connect_key, 28);
479 nx_packet_release(packet_ptr);
480
481 status = nx_secure_tls_packet_allocate(&tls_server_session, &server_pool, &packet_ptr, NX_NO_WAIT);
482 if (status)
483 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
484 /* Append response 101. */
485 status = nx_packet_data_append(packet_ptr, server_switch_101, sizeof(server_switch_101), &server_pool, NX_NO_WAIT);
486 if (status)
487 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
488 status = nx_secure_tls_session_send(&tls_server_session, packet_ptr, NX_WAIT_FOREVER);
489 if (status)
490 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
491
492 /* Receive client data. */
493 status = nx_secure_tls_session_receive(&tls_server_session, &packet_ptr, NX_IP_PERIODIC_RATE);
494 if (status)
495 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
496 nx_packet_release(packet_ptr);
497 /* Send a response */
498 status = nx_secure_tls_packet_allocate(&tls_server_session, &server_pool, &packet_ptr, NX_NO_WAIT);
499 if (status)
500 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
501 nx_packet_data_append(packet_ptr, server_response_1, sizeof(server_response_1), &server_pool, NX_IP_PERIODIC_RATE);
502 status = nx_secure_tls_session_send(&tls_server_session, packet_ptr, NX_WAIT_FOREVER);
503 if (status)
504 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
505 nx_packet_release(packet_ptr);
506
507 /* End session. */
508 nx_secure_tls_session_end(&tls_server_session, NX_NO_WAIT);
509 #endif
510
511 /* Disconnect. */
512 status = nx_tcp_socket_disconnect(&test_server, NX_IP_PERIODIC_RATE);
513 if (status)
514 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
515 /* Unaccept the server socket. */
516 status = nx_tcp_server_socket_unaccept(&test_server);
517 if (status)
518 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
519
520 /* Wait for test done. */
521 while (test_done == NX_FALSE)
522 {
523 tx_thread_sleep(NX_IP_PERIODIC_RATE);
524 }
525
526 nx_tcp_server_socket_unlisten(&server_ip, TEST_SERVER_PORT);
527 nx_tcp_socket_delete(&test_server);
528
529 if (client_pool.nx_packet_pool_available != client_pool.nx_packet_pool_total)
530 {
531 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
532 }
533 else if (client_pool.nx_packet_pool_invalid_releases)
534 {
535 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
536 }
537
538 if (server_pool.nx_packet_pool_available != server_pool.nx_packet_pool_total)
539 {
540 SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
541 }
542
543 if (error_counter)
544 {
545 printf("ERROR!\n");
546 test_control_return(1);
547 }
548 else
549 {
550 printf("SUCCESS!\n");
551 test_control_return(0);
552 }
553 }
554
555 #else
556
557 #ifdef CTEST
test_application_define(void * first_unused_memory)558 VOID test_application_define(void *first_unused_memory)
559 #else
560 void netx_websocket_connect_test_application_define(void *first_unused_memory)
561 #endif
562 {
563
564 /* Print out test information banner. */
565 printf("NetX Test: Websocket Connect Test.....................................N/A\n");
566
567 test_control_return(3);
568 }
569 #endif
570
571