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