summaryrefslogtreecommitdiff
path: root/test/regression/websocket_test/netx_websocket_multi_instance_test.c
blob: 430aa28ec921c30baaabc13553b04b1113bdb7c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/***************************************************************************/
/* Copyright (c) 2024 Microsoft Corporation                                */
/* Copyright (c) 2026 Eclipse ThreadX contributors                         */
/*                                                                         */
/* This program and the accompanying materials are made available under    */
/* the terms of the MIT License which is available at                      */
/* https://opensource.org/licenses/MIT.                                    */
/*                                                                         */
/* SPDX-License-Identifier: MIT                                            */
/***************************************************************************/

/* This case tests the condition when multi websocket instances are working at the same time.  */
#include    "tx_api.h"
#include    "nx_api.h"

extern void test_control_return(UINT);

#if !defined(NX_DISABLE_IPV4) && defined(__PRODUCT_NETXDUO__) && !defined(NX_DISABLE_PACKET_CHAIN)
#include    "nx_websocket_client.h"
#include    "netx_websocket_common_process.c"

#define     DEMO_STACK_SIZE         4096
#define     PACKET_SIZE             1536
#define     TOTAL_SIZE              DEMO_STACK_SIZE + (PACKET_SIZE * 8) + 2048 + 1024

/* Define device drivers.  */
extern void _nx_ram_network_driver_1024(NX_IP_DRIVER *driver_req_ptr);

/* ---- Variables for the first instance ---- */
static UINT                test_done = NX_FALSE;

static TX_THREAD           client_thread;
static NX_PACKET_POOL      client_pool;
static NX_TCP_SOCKET       test_client;
static NX_IP               client_ip;

static NX_TCP_SOCKET       test_server;
static NX_PACKET_POOL      server_pool;
static TX_THREAD           server_thread;
static NX_IP               server_ip;
static UINT                test_server_start = 0;
static UINT                test_client_stop = 0;

/* Set up the websocket global variables */
static NX_WEBSOCKET_CLIENT client_websocket;
static UCHAR               *client_websocket_host;
static UINT                client_websocket_host_length;
static UCHAR               *client_websocket_uri_path;
static UINT                client_websocket_uri_path_length;


static void thread_client_entry(ULONG thread_input);
static void thread_server_entry(ULONG thread_input);

#define TEST_SERVER_ADDRESS  IP_ADDRESS(1,2,3,4)
#define TEST_CLIENT_ADDRESS  IP_ADDRESS(1,2,3,5)
#define TEST_SERVER_PORT     80

#define TEST_HOST_NAME       "1.2.3.4"
#define TEST_URI_PATH        "/test"
#define TEST_PROTOCOL        "test"
#define TEST_BEARER          ""

static UCHAR server_switch_101[] =
{
0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x20,                                      // HTTP1.1/
0x31, 0x30, 0x31, 0x20, 0x53, 0x77, 0x69, 0x74, 0x63, 0x68, 0x69, 0x6e, 0x67, 0x20,        // 101 Switching
0x50, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x73, 0x0d, 0x0a,                          // Protocols\r\n
0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x3a, 0x20,                                      // Upgrade:
0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x0d, 0x0a,                          // WebSocket\r\n
0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20,                    // Connection:
0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x0d, 0x0a,                                      // Upgrade\r\n
0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65,                    // Sec-WebSocket-Protocol:
0x74, 0x2d, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x3a, 0x20,
0x74, 0x65, 0x73, 0x74, 0x0d, 0x0a,                                                        // test
0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b,                          // Sec-WebSocket-Accept:
0x65, 0x74, 0x2d, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x3a, 0x20,
0x35, 0x75, 0x31, 0x6c, 0x55, 0x72, 0x32, 0x57, 0x68, 0x70, 0x34, 0x64, 0x44, 0x57, 0x6e,  // 5u1lUr2Whp4dDWnskk9JcJZobO0=
0x73, 0x6b, 0x6b, 0x39, 0x4a, 0x63, 0x4a, 0x5a, 0x6f, 0x62, 0x4f, 0x30, 0x3d, 0x0d, 0x0a,
0x0d, 0x0a,
};

static UCHAR server_response[] =
{
0x82, 0x04, 0x01, 0x02, 0x03, 0x04,
0x82, 0x04, 0x01, 0x02, 0x03, 0x04, // The second frame will be on the processing packet list
};

static UCHAR client_test_data[] =
{
0x11, 0x22, 0x33, 0x44,
};

/* ---- Variables for the second instance ---- */
static UINT                test1_done = NX_FALSE;

static TX_THREAD           client1_thread;
static NX_TCP_SOCKET       test_client1;

static NX_TCP_SOCKET       test_server1;
static TX_THREAD           server1_thread;
static UINT                test_server1_start = 0;
static UINT                test_client1_stop = 0;

/* Set up the websocket global variables */
static NX_WEBSOCKET_CLIENT client1_websocket;
static UCHAR               *client1_websocket_host;
static UINT                client1_websocket_host_length;
static UCHAR               *client1_websocket_uri_path;
static UINT                client1_websocket_uri_path_length;

static void thread_client1_entry(ULONG thread_input);
static void thread_server1_entry(ULONG thread_input);

#define TEST_CLIENT1_ADDRESS  IP_ADDRESS(1,2,3,6)
#define TEST_SERVER1_PORT     81

#define TEST1_HOST_NAME       "1.2.3.5"
#define TEST1_URI_PATH        "/test"
#define TEST1_PROTOCOL        "test"

static UCHAR server1_switch_101[] =
{
0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x20,                                      // HTTP1.1/
0x31, 0x30, 0x31, 0x20, 0x53, 0x77, 0x69, 0x74, 0x63, 0x68, 0x69, 0x6e, 0x67, 0x20,        // 101 Switching
0x50, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x73, 0x0d, 0x0a,                          // Protocols\r\n
0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x3a, 0x20,                                      // Upgrade:
0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x0d, 0x0a,                          // WebSocket\r\n
0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20,                    // Connection:
0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x0d, 0x0a,                                      // Upgrade\r\n
0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65,                    // Sec-WebSocket-Protocol:
0x74, 0x2d, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x3a, 0x20,
0x74, 0x65, 0x73, 0x74, 0x0d, 0x0a,                                                        // test
0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b,                          // Sec-WebSocket-Accept:
0x65, 0x74, 0x2d, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x3a, 0x20,
0x35, 0x75, 0x31, 0x6c, 0x55, 0x72, 0x32, 0x57, 0x68, 0x70, 0x34, 0x64, 0x44, 0x57, 0x6e,  // 5u1lUr2Whp4dDWnskk9JcJZobO0=
0x73, 0x6b, 0x6b, 0x39, 0x4a, 0x63, 0x4a, 0x5a, 0x6f, 0x62, 0x4f, 0x30, 0x3d, 0x0d, 0x0a,
0x0d, 0x0a,
};

static UCHAR server1_response[] =
{
0x82, 0x04, 0x01, 0x02, 0x03, 0x04,
0x82, 0x04, 0x01, 0x02, 0x03, 0x04, // The second frame will be on the processing packet list
};

static UCHAR client1_test_data[] =
{
0x11, 0x22, 0x33, 0x44,
};

static ULONG                   error_counter;
static ULONG                   error1_counter;

extern void SET_ERROR_COUNTER(ULONG *error_counter, CHAR *filename, int line_number);

#define TEST_LOOP 2

#ifdef CTEST
VOID test_application_define(void *first_unused_memory)
#else
void    netx_websocket_multi_instance_test_application_define(void *first_unused_memory)
#endif
{
CHAR    *pointer;
UINT    status;


    error_counter = 0;
    error1_counter = 0;

    /* Setup the working pointer.  */
    pointer =  (CHAR *) first_unused_memory;

    /* Create a helper thread for the server. */
    tx_thread_create(&server_thread, "Test Server thread", thread_server_entry, 0,
                     pointer, DEMO_STACK_SIZE,
                     4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);

    pointer =  pointer + DEMO_STACK_SIZE;

    /* Create a helper thread for the server1. */
    tx_thread_create(&server1_thread, "Test Server1 thread", thread_server1_entry, 0,
                     pointer, DEMO_STACK_SIZE,
                     4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);

    pointer =  pointer + DEMO_STACK_SIZE;

    /* Initialize the NetX system.  */
    nx_system_initialize();

    /* Create the server packet pool.  */
    status =  nx_packet_pool_create(&server_pool, "Test Server Packet Pool", PACKET_SIZE,
                                    pointer, PACKET_SIZE * 10);
    pointer = pointer + PACKET_SIZE * 10;
    if (status)
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);

    /* Create an IP instance.  */
    status = nx_ip_create(&server_ip, "Test Server IP", TEST_SERVER_ADDRESS,
                          0xFFFFFF00UL, &server_pool, _nx_ram_network_driver_1024,
                          pointer, 2048, 1);
    pointer =  pointer + 2048;
    if (status)
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);

    /* Enable ARP and supply ARP cache memory for the server IP instance.  */
    status = nx_arp_enable(&server_ip, (void *) pointer, 1024);
    pointer = pointer + 1024;
    if (status)
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);

    /* Enable TCP traffic.  */
    status = nx_tcp_enable(&server_ip);
    if (status)
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);

    /* Create the Test Client thread. */
    status = tx_thread_create(&client_thread, "Test Client", thread_client_entry, 0,
                              pointer, DEMO_STACK_SIZE,
                              6, 6, TX_NO_TIME_SLICE, TX_AUTO_START);
    pointer =  pointer + DEMO_STACK_SIZE;
    if (status)
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);

    /* Create the Test Client1 thread. */
    status = tx_thread_create(&client1_thread, "Test Client1", thread_client1_entry, 0,
                              pointer, DEMO_STACK_SIZE,
                              6, 6, TX_NO_TIME_SLICE, TX_AUTO_START);
    pointer =  pointer + DEMO_STACK_SIZE;
    if (status)
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);

    /* Create the Client packet pool.  */
    status = nx_packet_pool_create(&client_pool, "Test Client Packet Pool", PACKET_SIZE,
                                    pointer, PACKET_SIZE * 10);
    pointer = pointer + PACKET_SIZE * 10;
    if (status)
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);

    /* Create an IP instance.  */
    status = nx_ip_create(&client_ip, "Test Client IP", TEST_CLIENT_ADDRESS,
                          0xFFFFFF00UL, &client_pool, _nx_ram_network_driver_1024,
                          pointer, 2048, 1);
    pointer =  pointer + 2048;
    if (status)
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);

    status  = nx_arp_enable(&client_ip, (void *) pointer, 1024);
    pointer =  pointer + 1024;
    if (status)
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);

    /* Enable TCP traffic.  */
    status = nx_tcp_enable(&client_ip);
    if (status)
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
}

void thread_client_entry(ULONG thread_input)
{
UINT            i, status;
NX_PACKET       *packet_ptr;
NXD_ADDRESS     server_ip_address;
UINT            code;

    /* Create client socket.  */
    status = nx_tcp_socket_create(&client_ip, &test_client, "Client Socket", NX_IP_NORMAL, NX_FRAGMENT_OKAY,
                                  NX_IP_TIME_TO_LIVE, 1000, NX_NULL, NX_NULL);
    if (status)
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);

    /* Create WebSocket.  */
    status = nx_websocket_client_create(&client_websocket, (UCHAR *)" ", &client_ip, &client_pool);

    /* Check status.  */
    if (status || client_websocket.nx_websocket_client_mutex.tx_mutex_ownership_count != 0)
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);

    /* Give IP task and driver a chance to initialize the system.  */
    tx_thread_sleep(NX_IP_PERIODIC_RATE);

    /* Bind and connect to server.  */
    status = nx_tcp_client_socket_bind(&test_client, TEST_SERVER_PORT, NX_IP_PERIODIC_RATE);
    if (status)
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);

    /* Wait test server started.  */
    while(!test_server_start)
    {
        tx_thread_sleep(NX_IP_PERIODIC_RATE);
    }

    /* Set server IP address.  */
    server_ip_address.nxd_ip_address.v4 = TEST_SERVER_ADDRESS;
    server_ip_address.nxd_ip_version = NX_IP_VERSION_V4;

    /* Connect to the server  */
    status = nxd_tcp_client_socket_connect(&test_client, &server_ip_address, TEST_SERVER_PORT, NX_WAIT_FOREVER);
    if (status)
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);

    /* Upgrade to websocket */
    status = nx_websocket_client_connect(&client_websocket, &test_client,
                                        TEST_HOST_NAME, sizeof(TEST_HOST_NAME) - 1,
                                        (UCHAR *)TEST_URI_PATH, sizeof(TEST_URI_PATH) - 1,
                                        (UCHAR *)TEST_PROTOCOL, sizeof(TEST_PROTOCOL) - 1,
                                        (UCHAR *)TEST_BEARER, sizeof(TEST_BEARER) -1,
                                        NX_WAIT_FOREVER);

    if (status || client_websocket.nx_websocket_client_mutex.tx_mutex_ownership_count != 0)
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
    else
    {
        status = nx_packet_allocate(&client_pool, &packet_ptr, NX_TCP_PACKET, NX_IP_PERIODIC_RATE);
        if (status)
            SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);

        /* Append and send data.  */
        status = nx_packet_data_append(packet_ptr, client_test_data, sizeof(client_test_data), &client_pool, NX_IP_PERIODIC_RATE);
        if (status)
            SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
        status = nx_websocket_client_send(&client_websocket, packet_ptr, NX_WEBSOCKET_OPCODE_BINARY_FRAME, NX_TRUE, NX_WAIT_FOREVER);
        if (status || client_websocket.nx_websocket_client_mutex.tx_mutex_ownership_count != 0)
            SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);

        /* Only receive and parse the first frame from server.  */
        status = nx_websocket_client_receive(&client_websocket, &packet_ptr, &code, 5 * NX_IP_PERIODIC_RATE);
        if (status || client_websocket.nx_websocket_client_mutex.tx_mutex_ownership_count != 0)
            SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
        if (packet_ptr -> nx_packet_length != 4)
            SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
        nx_packet_release(packet_ptr);
    }

    nx_tcp_client_socket_unbind(&test_client);
    nx_tcp_socket_delete(&test_client);

    nx_websocket_client_delete(&client_websocket);
    if (client_websocket.nx_websocket_client_mutex.tx_mutex_ownership_count != 0)
        SET_ERROR_COUNTER(&error1_counter, __FILE__, __LINE__);

    test_done = NX_TRUE;
}

/* Define the helper Test server thread.  */
void    thread_server_entry(ULONG thread_input)
{
UINT            i, status;
NX_PACKET       *packet_ptr;


    /* Print out test information banner.  */
    printf("NetX Test:   Websocket Multi-instance Test.....................................");

    /* Check for earlier error.  */
    if (error_counter)
    {
        printf("ERROR!\n");
        test_control_return(1);
    }

    /* Give NetX a chance to initialize the system.  */
    tx_thread_sleep(NX_IP_PERIODIC_RATE);

    status = nx_tcp_socket_create(&server_ip, &test_server, "Test Server Socket",
                                  NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE, 1000,
                                  NX_NULL, NX_NULL);

    status = nx_tcp_server_socket_listen(&server_ip, TEST_SERVER_PORT, &test_server, 5, NX_NULL);
    if (status)
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);

    /* Set the flag.  */
    test_server_start = 1;

    /* Accept a connection from test client.  */
    status = nx_tcp_server_socket_accept(&test_server, 5 * NX_IP_PERIODIC_RATE);
    if (status)
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);

    for (i = 0; i < TEST_LOOP; i++)
    {
        /* Receive client data.  */
        status = nx_tcp_socket_receive(&test_server, &packet_ptr, 5 * NX_IP_PERIODIC_RATE);
        if (status)
            SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
        else
        {
            /* Response data.  */
            switch (i)
            {
            case 0:
                /* Update the value in the field Sec-Protocol-Accept since it is calculated based on a random value */
                _server_connect_response_process(packet_ptr);
                memcpy(&server_switch_101[127], connect_key, 28);

                packet_ptr -> nx_packet_append_ptr = packet_ptr -> nx_packet_prepend_ptr;
                packet_ptr -> nx_packet_length = 0;
                nx_packet_data_append(packet_ptr, server_switch_101, sizeof(server_switch_101), &server_pool, NX_IP_PERIODIC_RATE);
                status = nx_tcp_socket_send(&test_server, packet_ptr, NX_IP_PERIODIC_RATE);
                if (status)
                    SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
                break;
            case 1:
                packet_ptr -> nx_packet_append_ptr = packet_ptr -> nx_packet_prepend_ptr;
                packet_ptr -> nx_packet_length = 0;
                nx_packet_data_append(packet_ptr, server_response, sizeof(server_response), &server_pool, NX_IP_PERIODIC_RATE);
                status = nx_tcp_socket_send(&test_server, packet_ptr, NX_IP_PERIODIC_RATE);
                if (status)
                    SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
                break;
            default:
                break;
            }
        }
    }

    /* Wait for test done.  */
    while (test_done == NX_FALSE || test1_done == NX_FALSE)
    {
        tx_thread_sleep(NX_IP_PERIODIC_RATE);
    }

    nx_tcp_server_socket_unlisten(&server_ip, TEST_SERVER_PORT);
    nx_tcp_socket_delete(&test_server);

    if (client_pool.nx_packet_pool_available != client_pool.nx_packet_pool_total)
    {
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
    }
    else if (client_pool.nx_packet_pool_invalid_releases)
    {
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
    }

    if (server_pool.nx_packet_pool_available != server_pool.nx_packet_pool_total)
    {
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
    }

    if (error_counter || error1_counter)
    {
        printf("ERROR!\n");
        test_control_return(1);
    }
    else
    {
        printf("SUCCESS!\n");
        test_control_return(0);
    }
}

void thread_client1_entry(ULONG thread_input)
{
UINT            i, status;
NX_PACKET       *packet_ptr;
NXD_ADDRESS     server_ip_address;
UINT            code;

    /* Create client socket.  */
    status = nx_tcp_socket_create(&client_ip, &test_client1, "Client1 Socket", NX_IP_NORMAL, NX_FRAGMENT_OKAY,
                                  NX_IP_TIME_TO_LIVE, 1000, NX_NULL, NX_NULL);
    if (status)
        SET_ERROR_COUNTER(&error1_counter, __FILE__, __LINE__);

    /* Create WebSocket.  */
    status = nx_websocket_client_create(&client1_websocket, (UCHAR *)" ", &client_ip, &client_pool);

    /* Check status.  */
    if (status || client1_websocket.nx_websocket_client_mutex.tx_mutex_ownership_count != 0)
        SET_ERROR_COUNTER(&error1_counter, __FILE__, __LINE__);

    /* Give IP task and driver a chance to initialize the system.  */
    tx_thread_sleep(NX_IP_PERIODIC_RATE);

    /* Bind and connect to server.  */
    status = nx_tcp_client_socket_bind(&test_client1, TEST_SERVER1_PORT, NX_IP_PERIODIC_RATE);
    if (status)
        SET_ERROR_COUNTER(&error1_counter, __FILE__, __LINE__);

    /* Wait test server started.  */
    while(!test_server1_start)
    {
        tx_thread_sleep(NX_IP_PERIODIC_RATE);
    }

    /* Set server IP address.  */
    server_ip_address.nxd_ip_address.v4 = TEST_SERVER_ADDRESS;
    server_ip_address.nxd_ip_version = NX_IP_VERSION_V4;

    /* Connect to the server  */
    status = nxd_tcp_client_socket_connect(&test_client1, &server_ip_address, TEST_SERVER1_PORT, NX_WAIT_FOREVER);
    if (status)
        SET_ERROR_COUNTER(&error1_counter, __FILE__, __LINE__);

    /* Upgrade to websocket */
    status = nx_websocket_client_connect(&client1_websocket, &test_client1,
                                        TEST1_HOST_NAME, sizeof(TEST1_HOST_NAME) - 1,
                                        (UCHAR *)TEST1_URI_PATH, sizeof(TEST1_URI_PATH) - 1,
                                        (UCHAR *)TEST1_PROTOCOL, sizeof(TEST1_PROTOCOL) - 1,
                                        (UCHAR *)TEST_BEARER, sizeof(TEST_BEARER) -1,
                                        NX_WAIT_FOREVER);

    if (status || client1_websocket.nx_websocket_client_mutex.tx_mutex_ownership_count != 0)
        SET_ERROR_COUNTER(&error1_counter, __FILE__, __LINE__);
    else
    {
        status = nx_packet_allocate(&client_pool, &packet_ptr, NX_TCP_PACKET, NX_IP_PERIODIC_RATE);
        if (status)
            SET_ERROR_COUNTER(&error1_counter, __FILE__, __LINE__);

        /* Append and send data.  */
        status = nx_packet_data_append(packet_ptr, client1_test_data, sizeof(client1_test_data), &client_pool, NX_IP_PERIODIC_RATE);
        if (status)
            SET_ERROR_COUNTER(&error1_counter, __FILE__, __LINE__);
        status = nx_websocket_client_send(&client1_websocket, packet_ptr, NX_WEBSOCKET_OPCODE_BINARY_FRAME, NX_TRUE, NX_WAIT_FOREVER);
        if (status || client1_websocket.nx_websocket_client_mutex.tx_mutex_ownership_count != 0)
            SET_ERROR_COUNTER(&error1_counter, __FILE__, __LINE__);

        /* Only receive and parse the first frame from server.  */
        status = nx_websocket_client_receive(&client1_websocket, &packet_ptr, &code, 5 * NX_IP_PERIODIC_RATE);
        if (status || client1_websocket.nx_websocket_client_mutex.tx_mutex_ownership_count != 0)
            SET_ERROR_COUNTER(&error1_counter, __FILE__, __LINE__);
        if (packet_ptr -> nx_packet_length != 4)
            SET_ERROR_COUNTER(&error1_counter, __FILE__, __LINE__);
        nx_packet_release(packet_ptr);
    }

    nx_tcp_client_socket_unbind(&test_client1);
    nx_tcp_socket_delete(&test_client1);

    nx_websocket_client_delete(&client1_websocket);
    if (client1_websocket.nx_websocket_client_mutex.tx_mutex_ownership_count != 0)
        SET_ERROR_COUNTER(&error1_counter, __FILE__, __LINE__);

    test1_done = NX_TRUE;
}

/* Define the helper Test server thread.  */
void    thread_server1_entry(ULONG thread_input)
{
UINT            i, status;
NX_PACKET       *packet_ptr;


    /* Check for earlier error.  */
    if (error_counter)
    {
        printf("ERROR!\n");
        test_control_return(1);
    }

    /* Give NetX a chance to initialize the system.  */
    tx_thread_sleep(NX_IP_PERIODIC_RATE);

    status = nx_tcp_socket_create(&server_ip, &test_server1, "Test Server1 Socket",
                                  NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE, 1000,
                                  NX_NULL, NX_NULL);

    status = nx_tcp_server_socket_listen(&server_ip, TEST_SERVER1_PORT, &test_server1, 5, NX_NULL);
    if (status)
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);

    /* Set the flag.  */
    test_server1_start = 1;

    /* Accept a connection from test client.  */
    status = nx_tcp_server_socket_accept(&test_server1, 5 * NX_IP_PERIODIC_RATE);
    if (status)
        SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);

    for (i = 0; i < TEST_LOOP; i++)
    {
        /* Receive client data.  */
        status = nx_tcp_socket_receive(&test_server1, &packet_ptr, 5 * NX_IP_PERIODIC_RATE);
        if (status)
            SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
        else
        {
            /* Response data.  */
            switch (i)
            {
            case 0:
                /* Update the value in the field Sec-Protocol-Accept since it is calculated based on a random value */
                _server_connect_response_process(packet_ptr);
                memcpy(&server1_switch_101[127], connect_key, 28);

                packet_ptr -> nx_packet_append_ptr = packet_ptr -> nx_packet_prepend_ptr;
                packet_ptr -> nx_packet_length = 0;
                nx_packet_data_append(packet_ptr, server1_switch_101, sizeof(server1_switch_101), &server_pool, NX_IP_PERIODIC_RATE);
                status = nx_tcp_socket_send(&test_server1, packet_ptr, NX_IP_PERIODIC_RATE);
                if (status)
                    SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
                break;
            case 1:
                packet_ptr -> nx_packet_append_ptr = packet_ptr -> nx_packet_prepend_ptr;
                packet_ptr -> nx_packet_length = 0;
                nx_packet_data_append(packet_ptr, server1_response, sizeof(server1_response), &server_pool, NX_IP_PERIODIC_RATE);
                status = nx_tcp_socket_send(&test_server1, packet_ptr, NX_IP_PERIODIC_RATE);
                if (status)
                    SET_ERROR_COUNTER(&error_counter, __FILE__, __LINE__);
                break;
            default:
                break;
            }
        }
    }

    /* Wait for test done.  */
    while (test1_done == NX_FALSE)
    {
        tx_thread_sleep(NX_IP_PERIODIC_RATE);
    }

    nx_tcp_server_socket_unlisten(&server_ip, TEST_SERVER1_PORT);
    nx_tcp_socket_delete(&test_server1);
}

#else

#ifdef CTEST
VOID test_application_define(void *first_unused_memory)
#else
void    netx_websocket_multi_instance_test_application_define(void *first_unused_memory)
#endif
{

    /* Print out test information banner.  */
    printf("NetX Test:   Websocket Multi-instance Test.....................................N/A\n");

    test_control_return(3);
}
#endif