1 /* This case tests basic POST method. */
2 #include    "tx_api.h"
3 #include    "nx_api.h"
4 #include    "fx_api.h"
5 #include    "nx_web_http_client.h"
6 #include    "nx_web_http_server.h"
7 
8 extern void test_control_return(UINT);
9 
10 #if !defined(NX_DISABLE_IPV4)
11 
12 #include "test_device_cert.c"
13 #include "test_ca_cert.c"
14 #define ca_cert_der test_ca_cert_der
15 #define ca_cert_der_len test_ca_cert_der_len
16 
17 #define     DEMO_STACK_SIZE         4096
18 
19 /* Set up FileX and file memory resources. */
20 static CHAR             ram_disk_memory[4096];
21 static FX_MEDIA         ram_disk;
22 static UCHAR            media_memory[4096];
23 
24 static UCHAR            server_stack[16000];
25 
26 /* Define device drivers.  */
27 extern void _fx_ram_driver(FX_MEDIA *media_ptr);
28 extern void _nx_ram_network_driver_1024(NX_IP_DRIVER *driver_req_ptr);
29 
30 /* Set up the HTTP client global variables. */
31 
32 #define         CLIENT_PACKET_SIZE  (NX_WEB_HTTP_SERVER_MIN_PACKET_SIZE * 2)
33 
34 static TX_THREAD           client_thread;
35 static NX_PACKET_POOL      client_pool;
36 static NX_WEB_HTTP_CLIENT  my_client;
37 static NX_IP               client_ip;
38 static UINT                error_counter;
39 
40 /* Set up the HTTP server global variables */
41 
42 #define         SERVER_PACKET_SIZE  (NX_WEB_HTTP_CLIENT_MIN_PACKET_SIZE * 2)
43 
44 static NX_WEB_HTTP_SERVER  my_server;
45 static NX_PACKET_POOL      server_pool;
46 static TX_THREAD           server_thread;
47 static NX_IP               server_ip;
48 static NXD_ADDRESS         server_ip_address;
49 static UINT                http_server_start = 0;
50 static UINT                http_client_stop = 0;
51 
52 static void thread_client_entry(ULONG thread_input);
53 static void thread_server_entry(ULONG thread_input);
54 
55 #define HTTP_SERVER_ADDRESS  IP_ADDRESS(1,2,3,4)
56 #define HTTP_CLIENT_ADDRESS  IP_ADDRESS(1,2,3,5)
57 
58 #ifdef NX_WEB_HTTPS_ENABLE
59 static UINT                https_server_start = 0;
60 static UINT                https_client_stop = 0;
61 static UINT loop = 4;
62 extern const NX_SECURE_TLS_CRYPTO nx_crypto_tls_ciphers;
63 static CHAR crypto_metadata_server[20000 * NX_WEB_HTTP_SERVER_SESSION_MAX];
64 static CHAR crypto_metadata_client[20000 * NX_WEB_HTTP_SERVER_SESSION_MAX];
65 static UCHAR tls_packet_buffer[18500];
66 static NX_SECURE_X509_CERT certificate;
67 static NX_SECURE_X509_CERT trusted_certificate;
68 static NX_SECURE_X509_CERT remote_certificate, remote_issuer;
69 static UCHAR remote_cert_buffer[2000];
70 static UCHAR remote_issuer_buffer[2000];
71 #else
72 static UINT loop = 2;
73 #endif /* NX_WEB_HTTPS_ENABLE  */
74 
75 /* POST AAAAAAAAAA*/
76 static char pkt[] = {
77     0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 /* AAAAAAAAAA */
78 };
79 
80 #ifdef CTEST
test_application_define(void * first_unused_memory)81 VOID test_application_define(void *first_unused_memory)
82 #else
83 void    netx_web_post_basic_test_application_define(void *first_unused_memory)
84 #endif
85 {
86 CHAR    *pointer;
87 UINT    status;
88 
89 
90     error_counter = 0;
91 
92     /* Setup the working pointer.  */
93     pointer =  (CHAR *) first_unused_memory;
94 
95     /* Create a helper thread for the server. */
96     tx_thread_create(&server_thread, "HTTP Server thread", thread_server_entry, 0,
97                      pointer, DEMO_STACK_SIZE,
98                      NX_WEB_HTTP_SERVER_PRIORITY, NX_WEB_HTTP_SERVER_PRIORITY, TX_NO_TIME_SLICE, TX_AUTO_START);
99 
100     pointer =  pointer + DEMO_STACK_SIZE;
101 
102     /* Initialize the NetX system.  */
103     nx_system_initialize();
104 
105     /* Create the server packet pool.  */
106     status =  nx_packet_pool_create(&server_pool, "HTTP Server Packet Pool", SERVER_PACKET_SIZE,
107                                     pointer, SERVER_PACKET_SIZE*8);
108     pointer = pointer + SERVER_PACKET_SIZE * 8;
109     if (status)
110         error_counter++;
111 
112     /* Create an IP instance.  */
113     status = nx_ip_create(&server_ip, "HTTP Server IP", HTTP_SERVER_ADDRESS,
114                           0xFFFFFF00UL, &server_pool, _nx_ram_network_driver_1024,
115                           pointer, 4096, 1);
116     pointer =  pointer + 4096;
117     if (status)
118         error_counter++;
119 
120     /* Enable ARP and supply ARP cache memory for the server IP instance.  */
121     status = nx_arp_enable(&server_ip, (void *) pointer, 1024);
122     pointer = pointer + 1024;
123     if (status)
124         error_counter++;
125 
126      /* Enable TCP traffic.  */
127     status = nx_tcp_enable(&server_ip);
128     if (status)
129         error_counter++;
130 
131     /* Create the HTTP Client thread. */
132     status = tx_thread_create(&client_thread, "HTTP Client", thread_client_entry, 0,
133                               pointer, DEMO_STACK_SIZE,
134                               NX_WEB_HTTP_SERVER_PRIORITY + 2, NX_WEB_HTTP_SERVER_PRIORITY + 2, TX_NO_TIME_SLICE, TX_AUTO_START);
135     pointer =  pointer + DEMO_STACK_SIZE;
136     if (status)
137         error_counter++;
138 
139     /* Create the Client packet pool.  */
140     status =  nx_packet_pool_create(&client_pool, "HTTP Client Packet Pool", CLIENT_PACKET_SIZE,
141                                     pointer, CLIENT_PACKET_SIZE*8);
142     pointer = pointer + CLIENT_PACKET_SIZE * 8;
143     if (status)
144         error_counter++;
145 
146     /* Create an IP instance.  */
147     status = nx_ip_create(&client_ip, "HTTP Client IP", HTTP_CLIENT_ADDRESS,
148                           0xFFFFFF00UL, &client_pool, _nx_ram_network_driver_1024,
149                           pointer, 2048, 1);
150     pointer =  pointer + 2048;
151     if (status)
152         error_counter++;
153 
154     status  = nx_arp_enable(&client_ip, (void *) pointer, 1024);
155     pointer =  pointer + 2048;
156     if (status)
157         error_counter++;
158 
159      /* Enable TCP traffic.  */
160     status = nx_tcp_enable(&client_ip);
161     if (status)
162         error_counter++;
163 }
164 
165 #ifdef NX_WEB_HTTPS_ENABLE
166 /* Define the TLS setup callback function.  */
tls_setup_callback(NX_WEB_HTTP_CLIENT * client_ptr,NX_SECURE_TLS_SESSION * tls_session)167 static UINT tls_setup_callback(NX_WEB_HTTP_CLIENT *client_ptr, NX_SECURE_TLS_SESSION *tls_session)
168 {
169 UINT status;
170 
171 
172     /* Initialize and create TLS session.  */
173     status = nx_secure_tls_session_create(tls_session, &nx_crypto_tls_ciphers, crypto_metadata_client, sizeof(crypto_metadata_client));
174 
175     /* Check status.  */
176     if (status)
177     {
178         return(status);
179     }
180 
181     /* Allocate space for packet reassembly.  */
182     status = nx_secure_tls_session_packet_buffer_set(&(client_ptr -> nx_web_http_client_tls_session), tls_packet_buffer, sizeof(tls_packet_buffer));
183 
184     /* Check status.  */
185     if (status)
186     {
187         return(status);
188     }
189 
190     /* Add a CA Certificate to our trusted store for verifying incoming server certificates.  */
191     nx_secure_x509_certificate_initialize(&trusted_certificate, ca_cert_der, ca_cert_der_len, NX_NULL, 0, NULL, 0, NX_SECURE_X509_KEY_TYPE_NONE);
192     nx_secure_tls_trusted_certificate_add(&(client_ptr -> nx_web_http_client_tls_session), &trusted_certificate);
193 
194     /* Need to allocate space for the certificate coming in from the remote host.  */
195     nx_secure_tls_remote_certificate_allocate(&(client_ptr -> nx_web_http_client_tls_session), &remote_certificate, remote_cert_buffer, sizeof(remote_cert_buffer));
196     nx_secure_tls_remote_certificate_allocate(&(client_ptr -> nx_web_http_client_tls_session), &remote_issuer, remote_issuer_buffer, sizeof(remote_issuer_buffer));
197 
198     return(NX_SUCCESS);
199 }
200 #endif /* NX_WEB_HTTPS_ENABLE  */
201 
thread_client_entry(ULONG thread_input)202 void thread_client_entry(ULONG thread_input)
203 {
204 UINT            i;
205 UINT            status;
206 NX_PACKET       *send_packet;
207 NX_PACKET       *recv_packet;
208 
209 
210     /* Give IP task and driver a chance to initialize the system. */
211     tx_thread_sleep(NX_IP_PERIODIC_RATE);
212 
213     /* Set server IP address.  */
214     server_ip_address.nxd_ip_address.v4 = HTTP_SERVER_ADDRESS;
215     server_ip_address.nxd_ip_version = NX_IP_VERSION_V4;
216 
217     /* First loop test HTTP, second loop test HTTPS.  */
218     for (i = 0; i < loop ; i++)
219     {
220         if (i < 2)
221         {
222 
223             /* Wait HTTP server started.  */
224             while(!http_server_start)
225             {
226                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
227             }
228         }
229 #ifdef NX_WEB_HTTPS_ENABLE
230         else
231         {
232 
233             /* Wait HTTPS server started.  */
234             while(!https_server_start)
235             {
236                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
237             }
238         }
239 #endif /* NX_WEB_HTTPS_ENABLE  */
240 
241         /* Create an HTTP client instance.  */
242         status = nx_web_http_client_create(&my_client, "HTTP Client", &client_ip, &client_pool, 1536);
243 
244         /* Check status.  */
245         if (status)
246             error_counter++;
247 
248         /* Send POST request.  */
249         if (i == 0)
250         {
251             status = nx_web_http_client_post_start(&my_client, &server_ip_address,
252                                                    NX_WEB_HTTP_SERVER_PORT,"/test.txt",
253                                                    "www.abc.com", "name", "password", sizeof(pkt), NX_WAIT_FOREVER);
254         }
255         else if (i == 1)
256         {
257             status = nx_web_http_client_post_start_extended(&my_client, &server_ip_address,
258                                                             NX_WEB_HTTP_SERVER_PORT, "/test.txt", 9,
259                                                             "www.abc.com", 11, "name", 4, "password", 8, sizeof(pkt), NX_WAIT_FOREVER);
260         }
261 #ifdef NX_WEB_HTTPS_ENABLE
262         else if (i == 2)
263         {
264             status = nx_web_http_client_post_secure_start(&my_client, &server_ip_address,
265                                                           NX_WEB_HTTPS_SERVER_PORT, "/test.txt",
266                                                           "www.abc.com", "name", "password", sizeof(pkt), tls_setup_callback, NX_WAIT_FOREVER);
267         }
268         else
269         {
270             status = nx_web_http_client_post_secure_start_extended(&my_client, &server_ip_address,
271                                                                    NX_WEB_HTTPS_SERVER_PORT, "/test.txt", 9,
272                                                                    "www.abc.com", 11, "name", 4, "password", 8, sizeof(pkt), tls_setup_callback, NX_WAIT_FOREVER);
273         }
274 #endif /* NX_WEB_HTTPS_ENABLE  */
275 
276         /* Check status.  */
277         if (status)
278             error_counter++;
279 
280         /* Allocate a packet.  */
281         status = nx_web_http_client_request_packet_allocate(&my_client, &send_packet, NX_WAIT_FOREVER);
282 
283         /* Check status.  */
284         if (status)
285             error_counter++;
286 
287         /* Write test data into the packet payload.  */
288         nx_packet_data_append(send_packet, pkt, sizeof(pkt), &client_pool, NX_WAIT_FOREVER);
289 
290         /* Send the POST request.  */
291         status = nx_web_http_client_put_packet(&my_client, send_packet, 1 * NX_IP_PERIODIC_RATE);
292         if (status)
293         {
294             nx_packet_release(send_packet);
295             error_counter++;
296         }
297 
298         /* Get response from server.  */
299         while (1)
300         {
301             status = nx_web_http_client_response_body_get(&my_client, &recv_packet, 1 * NX_IP_PERIODIC_RATE);
302 
303             if (status)
304                 break;
305             else
306                 nx_packet_release(recv_packet);
307         }
308 
309         /* Check status.  */
310         if (status != NX_WEB_HTTP_GET_DONE)
311             error_counter++;
312         else
313             nx_packet_release(recv_packet);
314 
315         status = nx_web_http_client_delete(&my_client);
316         if (status)
317             error_counter++;
318 
319         /* Set the flag.  */
320         if (i == 1)
321         {
322             http_client_stop = 1;
323         }
324 #ifdef NX_WEB_HTTPS_ENABLE
325         else if (i == 3)
326         {
327             https_client_stop = 1;
328         }
329 #endif /* NX_WEB_HTTPS_ENABLE  */
330     }
331 }
332 
333 
334 /* Define the helper HTTP server thread.  */
thread_server_entry(ULONG thread_input)335 void    thread_server_entry(ULONG thread_input)
336 {
337 UINT            i;
338 UINT            status;
339 FX_FILE         my_file;
340 UINT            server_port = NX_WEB_HTTP_SERVER_PORT;
341 
342 
343     /* Print out test information banner.  */
344     printf("NetX Test:   Web Post Basic Test.......................................");
345 
346     /* Check for earlier error. */
347     if(error_counter)
348     {
349         printf("ERROR!\n");
350         test_control_return(1);
351     }
352 
353     fx_media_format(&ram_disk,
354                     _fx_ram_driver,               // Driver entry
355                     ram_disk_memory,              // RAM disk memory pointer
356                     media_memory,                 // Media buffer pointer
357                     sizeof(media_memory),         // Media buffer size
358                     "MY_RAM_DISK",                // Volume Name
359                     1,                            // Number of FATs
360                     32,                           // Directory Entries
361                     0,                            // Hidden sectors
362                     256,                          // Total sectors
363                     512,                          // Sector size
364                     8,                            // Sectors per cluster
365                     1,                            // Heads
366                     1);                           // Sectors per track
367 
368     /* Open the RAM disk.  */
369     status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, media_memory, sizeof(media_memory)) ;
370     status += fx_file_create(&ram_disk, "TEST.TXT");
371     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
372     status += fx_file_write(&my_file, "https server", 12);
373     status += fx_file_close(&my_file);
374     if(status)
375         error_counter++;
376 
377     /* Give NetX a chance to initialize the system. */
378     tx_thread_sleep(NX_IP_PERIODIC_RATE);
379 
380     /* First loop test HTTP, second loop test HTTPS.  */
381     for (i = 0; i < loop/2; i++)
382     {
383 
384         if (i == 1)
385         {
386             server_port = NX_WEB_HTTPS_SERVER_PORT;
387         }
388 
389         /* Create the HTTP Server. */
390         status = nx_web_http_server_create(&my_server, "My HTTP Server", &server_ip, server_port, &ram_disk,
391                                            &server_stack, sizeof(server_stack), &server_pool,
392                                            NX_NULL, NX_NULL);
393         if (status)
394             error_counter++;
395 
396 #ifdef NX_WEB_HTTPS_ENABLE
397         /* Set TLS for HTTPS.  */
398         if (i == 1)
399         {
400             /* Initialize device certificate (used for all sessions in HTTPS server). */
401             memset(&certificate, 0, sizeof(certificate));
402             nx_secure_x509_certificate_initialize(&certificate, test_device_cert_der, test_device_cert_der_len, NX_NULL, 0, test_device_cert_key_der, test_device_cert_key_der_len, NX_SECURE_X509_KEY_TYPE_RSA_PKCS1_DER);
403 
404             /* Setup TLS session data for the TCP server. */
405             status = nx_web_http_server_secure_configure(&my_server, &nx_crypto_tls_ciphers,
406                                                          crypto_metadata_server, sizeof(crypto_metadata_server), tls_packet_buffer, sizeof(tls_packet_buffer),
407                                                          &certificate, NX_NULL, 0, NX_NULL, 0, NX_NULL, 0);
408             if (status)
409                 error_counter++;
410         }
411 #endif /* NX_WEB_HTTPS_ENABLE  */
412 
413         /* OK to start the HTTP Server.   */
414         status = nx_web_http_server_start(&my_server);
415         if (status)
416             error_counter++;
417 
418         /* Set the flag.  */
419         if (i == 0)
420         {
421             http_server_start = 1;
422 
423             /* Wait HTTP test finished.  */
424             while(!http_client_stop)
425             {
426                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
427             }
428         }
429 #ifdef NX_WEB_HTTPS_ENABLE
430         else
431         {
432             https_server_start = 1;
433 
434             /* Wait HTTPS test finished.  */
435             while(!https_client_stop)
436             {
437                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
438             }
439         }
440 #endif /* NX_WEB_HTTPS_ENABLE  */
441 
442         status = nx_web_http_server_delete(&my_server);
443         if (status)
444             error_counter++;
445     }
446 
447     /* Check packet pool.  */
448     if (server_pool.nx_packet_pool_available != server_pool.nx_packet_pool_total)
449     {
450         error_counter++;
451     }
452 
453     if (client_pool.nx_packet_pool_available != client_pool.nx_packet_pool_total)
454     {
455         error_counter++;
456     }
457 
458     if(error_counter)
459     {
460         printf("ERROR!\n");
461         test_control_return(1);
462     }
463     else
464     {
465         printf("SUCCESS!\n");
466         test_control_return(0);
467     }
468 }
469 
470 #else
471 
472 #ifdef CTEST
test_application_define(void * first_unused_memory)473 VOID test_application_define(void *first_unused_memory)
474 #else
475 void    netx_web_post_basic_test_application_define(void *first_unused_memory)
476 #endif
477 {
478 
479     /* Print out test information banner.  */
480     printf("NetX Test:   Web Post Basic Test.......................................N/A\n");
481 
482     test_control_return(3);
483 }
484 #endif
485