1 /* This case tests basic DELETE 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_CLIENT_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_SERVER_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 #ifdef CTEST
test_application_define(void * first_unused_memory)76 VOID test_application_define(void *first_unused_memory)
77 #else
78 void netx_web_delete_basic_test_application_define(void *first_unused_memory)
79 #endif
80 {
81 CHAR *pointer;
82 UINT status;
83
84
85 error_counter = 0;
86
87 /* Setup the working pointer. */
88 pointer = (CHAR *) first_unused_memory;
89
90 /* Create a helper thread for the server. */
91 tx_thread_create(&server_thread, "HTTP Server thread", thread_server_entry, 0,
92 pointer, DEMO_STACK_SIZE,
93 NX_WEB_HTTP_SERVER_PRIORITY, NX_WEB_HTTP_SERVER_PRIORITY, TX_NO_TIME_SLICE, TX_AUTO_START);
94
95 pointer = pointer + DEMO_STACK_SIZE;
96
97 /* Initialize the NetX system. */
98 nx_system_initialize();
99
100 /* Create the server packet pool. */
101 status = nx_packet_pool_create(&server_pool, "HTTP Server Packet Pool", SERVER_PACKET_SIZE,
102 pointer, SERVER_PACKET_SIZE*8);
103 pointer = pointer + SERVER_PACKET_SIZE * 8;
104 if (status)
105 error_counter++;
106
107 /* Create an IP instance. */
108 status = nx_ip_create(&server_ip, "HTTP Server IP", HTTP_SERVER_ADDRESS,
109 0xFFFFFF00UL, &server_pool, _nx_ram_network_driver_1024,
110 pointer, 4096, 1);
111 pointer = pointer + 4096;
112 if (status)
113 error_counter++;
114
115 /* Enable ARP and supply ARP cache memory for the server IP instance. */
116 status = nx_arp_enable(&server_ip, (void *) pointer, 1024);
117 pointer = pointer + 1024;
118 if (status)
119 error_counter++;
120
121 /* Enable TCP traffic. */
122 status = nx_tcp_enable(&server_ip);
123 if (status)
124 error_counter++;
125
126 /* Create the HTTP Client thread. */
127 status = tx_thread_create(&client_thread, "HTTP Client", thread_client_entry, 0,
128 pointer, DEMO_STACK_SIZE,
129 NX_WEB_HTTP_SERVER_PRIORITY + 2, NX_WEB_HTTP_SERVER_PRIORITY + 2, TX_NO_TIME_SLICE, TX_AUTO_START);
130 pointer = pointer + DEMO_STACK_SIZE;
131 if (status)
132 error_counter++;
133
134 /* Create the Client packet pool. */
135 status = nx_packet_pool_create(&client_pool, "HTTP Client Packet Pool", CLIENT_PACKET_SIZE,
136 pointer, CLIENT_PACKET_SIZE*8);
137 pointer = pointer + CLIENT_PACKET_SIZE * 8;
138 if (status)
139 error_counter++;
140
141 /* Create an IP instance. */
142 status = nx_ip_create(&client_ip, "HTTP Client IP", HTTP_CLIENT_ADDRESS,
143 0xFFFFFF00UL, &client_pool, _nx_ram_network_driver_1024,
144 pointer, 2048, 1);
145 pointer = pointer + 2048;
146 if (status)
147 error_counter++;
148
149 status = nx_arp_enable(&client_ip, (void *) pointer, 1024);
150 pointer = pointer + 2048;
151 if (status)
152 error_counter++;
153
154 /* Enable TCP traffic. */
155 status = nx_tcp_enable(&client_ip);
156 if (status)
157 error_counter++;
158 }
159
160 #ifdef NX_WEB_HTTPS_ENABLE
161 /* Define the TLS setup callback function. */
tls_setup_callback(NX_WEB_HTTP_CLIENT * client_ptr,NX_SECURE_TLS_SESSION * tls_session)162 static UINT tls_setup_callback(NX_WEB_HTTP_CLIENT *client_ptr, NX_SECURE_TLS_SESSION *tls_session)
163 {
164 UINT status;
165
166
167 /* Initialize and create TLS session. */
168 status = nx_secure_tls_session_create(tls_session, &nx_crypto_tls_ciphers, crypto_metadata_client, sizeof(crypto_metadata_client));
169
170 /* Check status. */
171 if (status)
172 {
173 return(status);
174 }
175
176 /* Allocate space for packet reassembly. */
177 status = nx_secure_tls_session_packet_buffer_set(&(client_ptr -> nx_web_http_client_tls_session), tls_packet_buffer, sizeof(tls_packet_buffer));
178
179 /* Check status. */
180 if (status)
181 {
182 return(status);
183 }
184
185 /* Add a CA Certificate to our trusted store for verifying incoming server certificates. */
186 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);
187 nx_secure_tls_trusted_certificate_add(&(client_ptr -> nx_web_http_client_tls_session), &trusted_certificate);
188
189 /* Need to allocate space for the certificate coming in from the remote host. */
190 nx_secure_tls_remote_certificate_allocate(&(client_ptr -> nx_web_http_client_tls_session), &remote_certificate, remote_cert_buffer, sizeof(remote_cert_buffer));
191 nx_secure_tls_remote_certificate_allocate(&(client_ptr -> nx_web_http_client_tls_session), &remote_issuer, remote_issuer_buffer, sizeof(remote_issuer_buffer));
192
193 return(NX_SUCCESS);
194 }
195 #endif /* NX_WEB_HTTPS_ENABLE */
196
thread_client_entry(ULONG thread_input)197 void thread_client_entry(ULONG thread_input)
198 {
199 UINT i;
200 UINT status;
201 NX_PACKET *send_packet;
202 NX_PACKET *recv_packet;
203
204
205 /* Give IP task and driver a chance to initialize the system. */
206 tx_thread_sleep(NX_IP_PERIODIC_RATE);
207
208 /* Set server IP address. */
209 server_ip_address.nxd_ip_address.v4 = HTTP_SERVER_ADDRESS;
210 server_ip_address.nxd_ip_version = NX_IP_VERSION_V4;
211
212 /* First loop test HTTP, second loop test HTTPS. */
213 for (i = 0; i < loop ; i++)
214 {
215 if (i < 2)
216 {
217
218 /* Wait HTTP server started. */
219 while(!http_server_start)
220 {
221 tx_thread_sleep(NX_IP_PERIODIC_RATE);
222 }
223 }
224 #ifdef NX_WEB_HTTPS_ENABLE
225 else
226 {
227
228 /* Wait HTTPS server started. */
229 while(!https_server_start)
230 {
231 tx_thread_sleep(NX_IP_PERIODIC_RATE);
232 }
233 }
234 #endif /* NX_WEB_HTTPS_ENABLE */
235
236 /* Create an HTTP client instance. */
237 status = nx_web_http_client_create(&my_client, "HTTP Client", &client_ip, &client_pool, 1536);
238
239 /* Check status. */
240 if (status)
241 error_counter++;
242
243 /* Send PUT request. */
244 if (i < 2)
245 {
246
247 status = nx_web_http_client_put_start(&my_client, &server_ip_address,
248 NX_WEB_HTTP_SERVER_PORT, "/index.htm",
249 "www.abc.com", "name", "password", 103, NX_WAIT_FOREVER);
250 }
251 #ifdef NX_WEB_HTTPS_ENABLE
252 else
253 {
254
255 status = nx_web_http_client_put_secure_start(&my_client, &server_ip_address,
256 NX_WEB_HTTPS_SERVER_PORT, "/index.htm",
257 "www.abc.com", "name", "password", 103, tls_setup_callback, NX_WAIT_FOREVER);
258 }
259 #endif /* NX_WEB_HTTPS_ENABLE */
260
261 /* Check status. */
262 if (status)
263 error_counter++;
264
265 /* Allocate a packet. */
266 status = nx_web_http_client_request_packet_allocate(&my_client, &send_packet, NX_WAIT_FOREVER);
267
268 /* Check status. */
269 if (status)
270 error_counter++;
271
272 /* Build a simple 103-byte HTML page. */
273 nx_packet_data_append(send_packet, "<HTML>\r\n", 8,
274 &client_pool, NX_WAIT_FOREVER);
275 nx_packet_data_append(send_packet,
276 "<HEAD><TITLE>NetX HTTP Test</TITLE></HEAD>\r\n", 44,
277 &client_pool, NX_WAIT_FOREVER);
278 nx_packet_data_append(send_packet, "<BODY>\r\n", 8,
279 &client_pool, NX_WAIT_FOREVER);
280 nx_packet_data_append(send_packet, "<H1>Another NetX Test Page!</H1>\r\n", 25,
281 &client_pool, NX_WAIT_FOREVER);
282 nx_packet_data_append(send_packet, "</BODY>\r\n", 9,
283 &client_pool, NX_WAIT_FOREVER);
284 nx_packet_data_append(send_packet, "</HTML>\r\n", 9,
285 &client_pool, NX_WAIT_FOREVER);
286
287 /* Complete the PUT by writing the total length. */
288 status = nx_web_http_client_put_packet(&my_client, send_packet, 1 * NX_IP_PERIODIC_RATE);
289 if (status)
290 {
291 nx_packet_release(send_packet);
292 error_counter++;
293 }
294
295 /* Get response from server. */
296 while (1)
297 {
298 status = nx_web_http_client_response_body_get(&my_client, &recv_packet, 1 * NX_IP_PERIODIC_RATE);
299
300 if (status)
301 break;
302 else
303 nx_packet_release(recv_packet);
304 }
305
306 /* Check status. */
307 if (status != NX_WEB_HTTP_GET_DONE)
308 error_counter++;
309 else
310 nx_packet_release(recv_packet);
311
312 /* Send DELETE request. */
313 if (i == 0)
314 {
315 status = nx_web_http_client_delete_start(&my_client, &server_ip_address, NX_WEB_HTTP_SERVER_PORT,
316 "/index.htm", "www.abc.com", NX_NULL, NX_NULL,
317 NX_WAIT_FOREVER);
318 }
319 else if (i == 1)
320 {
321 status = nx_web_http_client_delete_start_extended(&my_client, &server_ip_address, NX_WEB_HTTP_SERVER_PORT,
322 "/index.htm", 10, "www.abc.com", 11, NX_NULL, 0, NX_NULL, 0,
323 NX_WAIT_FOREVER);
324 }
325 #ifdef NX_WEB_HTTPS_ENABLE
326 else if (i == 2)
327 {
328 status = nx_web_http_client_delete_secure_start(&my_client, &server_ip_address, NX_WEB_HTTPS_SERVER_PORT,
329 "/index.htm", "www.abc.com", NX_NULL, NX_NULL,
330 tls_setup_callback, NX_WAIT_FOREVER);
331 }
332 else
333 {
334 status = nx_web_http_client_delete_secure_start_extended(&my_client, &server_ip_address, NX_WEB_HTTPS_SERVER_PORT,
335 "/index.htm", 10, "www.abc.com", 11, NX_NULL, 0, NX_NULL, 0,
336 tls_setup_callback, NX_WAIT_FOREVER);
337 }
338 #endif /* NX_WEB_HTTPS_ENABLE */
339
340 /* Check status. */
341 if (status)
342 error_counter++;
343
344 /* Get response from server. */
345 while (1)
346 {
347 status = nx_web_http_client_response_body_get(&my_client, &recv_packet, 1 * NX_IP_PERIODIC_RATE);
348
349 if (status)
350 break;
351 else
352 nx_packet_release(recv_packet);
353 }
354
355 /* Check status. */
356 if (status != NX_WEB_HTTP_GET_DONE)
357 error_counter++;
358 else
359 nx_packet_release(recv_packet);
360
361 status = nx_web_http_client_delete(&my_client);
362 if (status)
363 error_counter++;
364
365 /* Set the flag. */
366 if (i == 1)
367 {
368 http_client_stop = 1;
369 }
370 #ifdef NX_WEB_HTTPS_ENABLE
371 else if (i == 3)
372 {
373 https_client_stop = 1;
374 }
375 #endif /* NX_WEB_HTTPS_ENABLE */
376 }
377 }
378
379
380 /* Define the helper HTTP server thread. */
thread_server_entry(ULONG thread_input)381 void thread_server_entry(ULONG thread_input)
382 {
383 UINT i;
384 UINT status;
385 UINT server_port = NX_WEB_HTTP_SERVER_PORT;
386
387
388 /* Print out test information banner. */
389 printf("NetX Test: Web Delete Basic Test.....................................");
390
391 /* Check for earlier error. */
392 if(error_counter)
393 {
394 printf("ERROR!\n");
395 test_control_return(1);
396 }
397
398 fx_media_format(&ram_disk,
399 _fx_ram_driver, // Driver entry
400 ram_disk_memory, // RAM disk memory pointer
401 media_memory, // Media buffer pointer
402 sizeof(media_memory), // Media buffer size
403 "MY_RAM_DISK", // Volume Name
404 1, // Number of FATs
405 32, // Directory Entries
406 0, // Hidden sectors
407 256, // Total sectors
408 512, // Sector size
409 8, // Sectors per cluster
410 1, // Heads
411 1); // Sectors per track
412
413 /* Open the RAM disk. */
414 status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, media_memory, sizeof(media_memory)) ;
415 status += fx_file_create(&ram_disk, "TEST.TXT");
416 if(status)
417 error_counter++;
418
419 /* Give NetX a chance to initialize the system. */
420 tx_thread_sleep(NX_IP_PERIODIC_RATE);
421
422 /* First loop test HTTP, second loop test HTTPS. */
423 for (i = 0; i < loop/2; i++)
424 {
425
426 if (i == 1)
427 {
428 server_port = NX_WEB_HTTPS_SERVER_PORT;
429 }
430
431 /* Create the HTTP Server. */
432 status = nx_web_http_server_create(&my_server, "My HTTP Server", &server_ip, server_port, &ram_disk,
433 &server_stack, sizeof(server_stack), &server_pool,
434 NX_NULL, NX_NULL);
435 if (status)
436 error_counter++;
437
438 #ifdef NX_WEB_HTTPS_ENABLE
439 /* Set TLS for HTTPS. */
440 if (i == 1)
441 {
442 /* Initialize device certificate (used for all sessions in HTTPS server). */
443 memset(&certificate, 0, sizeof(certificate));
444 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);
445
446 /* Setup TLS session data for the TCP server. */
447 status = nx_web_http_server_secure_configure(&my_server, &nx_crypto_tls_ciphers,
448 crypto_metadata_server, sizeof(crypto_metadata_server), tls_packet_buffer, sizeof(tls_packet_buffer),
449 &certificate, NX_NULL, 0, NX_NULL, 0, NX_NULL, 0);
450 if (status)
451 error_counter++;
452 }
453 #endif /* NX_WEB_HTTPS_ENABLE */
454
455 /* OK to start the HTTP Server. */
456 status = nx_web_http_server_start(&my_server);
457 if (status)
458 error_counter++;
459
460 /* Set the flag. */
461 if (i == 0)
462 {
463 http_server_start = 1;
464
465 /* Wait HTTP test finished. */
466 while(!http_client_stop)
467 {
468 tx_thread_sleep(NX_IP_PERIODIC_RATE);
469 }
470 }
471 #ifdef NX_WEB_HTTPS_ENABLE
472 else
473 {
474 https_server_start = 1;
475
476 /* Wait HTTPS test finished. */
477 while(!https_client_stop)
478 {
479 tx_thread_sleep(NX_IP_PERIODIC_RATE);
480 }
481 }
482 #endif /* NX_WEB_HTTPS_ENABLE */
483
484 status = nx_web_http_server_delete(&my_server);
485 if (status)
486 error_counter++;
487 }
488
489 /* Check packet pool. */
490 if (server_pool.nx_packet_pool_available != server_pool.nx_packet_pool_total)
491 {
492 error_counter++;
493 }
494
495 if (client_pool.nx_packet_pool_available != client_pool.nx_packet_pool_total)
496 {
497 error_counter++;
498 }
499
500 if(error_counter)
501 {
502 printf("ERROR!\n");
503 test_control_return(1);
504 }
505 else
506 {
507 printf("SUCCESS!\n");
508 test_control_return(0);
509 }
510 }
511 #else
512
513 #ifdef CTEST
test_application_define(void * first_unused_memory)514 VOID test_application_define(void *first_unused_memory)
515 #else
516 void netx_web_delete_basic_test_application_define(void *first_unused_memory)
517 #endif
518 {
519
520 /* Print out test information banner. */
521 printf("NetX Test: Web Delete Basic Test.....................................N/A\n");
522
523 test_control_return(3);
524 }
525 #endif
526