1 /* This case tests sending packet failure. */
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 = 2;
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 = 1;
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_invalid_release_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 == 0)
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 == 0)
245 {
246
247 status = nx_web_http_client_put_start_extended(&my_client, &server_ip_address,
248 NX_WEB_HTTP_SERVER_PORT, "http://1.2.3.4/http_put_test2.html", 34,
249 "www.abc.com", 11, "name", 4, "password", 8, 103, NX_WAIT_FOREVER);
250 }
251 #ifdef NX_WEB_HTTPS_ENABLE
252 else
253 {
254
255 status = nx_web_http_client_put_secure_start_extended(&my_client, &server_ip_address,
256 NX_WEB_HTTPS_SERVER_PORT, "https://1.2.3.4/https_put_test2.html", 36,
257 "www.abc.com", 11, "name", 4, "password", 8, 103, tls_setup_callback, NX_WAIT_FOREVER);
258 }
259 #endif /* NX_WEB_HTTPS_ENABLE */
260
261 /* Check status. */
262 if (status)
263 {
264 error_counter++;
265 break;
266 }
267
268 /* Allocate a packet. */
269 status = nx_web_http_client_request_packet_allocate(&my_client, &send_packet, 1 * NX_IP_PERIODIC_RATE);
270
271 /* Check status. */
272 if (status)
273 error_counter++;
274
275 /* Send the an empty packet. */
276 status = nx_web_http_client_put_packet(&my_client, send_packet, 1 * NX_IP_PERIODIC_RATE);
277 if (status)
278 {
279 nx_packet_release(send_packet);
280 }
281 else
282 {
283 error_counter++;
284 }
285
286 /* Connect to server. */
287 if (i == 0)
288 {
289 status = nx_web_http_client_connect(&my_client, &server_ip_address, NX_WEB_HTTP_SERVER_PORT, NX_WAIT_FOREVER);
290
291 /* Check status. */
292 if (status)
293 error_counter++;
294 }
295 #ifdef NX_WEB_HTTPS_ENABLE
296 else
297 {
298 status = nx_web_http_client_secure_connect(&my_client, &server_ip_address, NX_WEB_HTTPS_SERVER_PORT,
299 tls_setup_callback, NX_WAIT_FOREVER);
300
301 /* Check status. */
302 if (status)
303 error_counter++;
304 }
305 #endif /* NX_WEB_HTTPS_ENABLE */
306
307 /* Allocate a packet. */
308 status = nx_web_http_client_request_packet_allocate(&my_client, &send_packet, 1 * NX_IP_PERIODIC_RATE);
309
310 /* Check status. */
311 if (status)
312 error_counter++;
313
314 if (i == 1)
315 {
316 send_packet -> nx_packet_prepend_ptr = send_packet -> nx_packet_data_start;
317 send_packet -> nx_packet_append_ptr = send_packet -> nx_packet_prepend_ptr;
318 }
319
320 /* Send the an empty packet. */
321 status = nx_web_http_client_request_packet_send(&my_client, send_packet, NX_FALSE, 1 * NX_IP_PERIODIC_RATE);
322 if (status)
323 {
324 nx_packet_release(send_packet);
325 }
326 else
327 {
328 error_counter++;
329 }
330
331 status = nx_web_http_client_delete(&my_client);
332 if (status)
333 error_counter++;
334
335 /* Set the flag. */
336 if (i == 0)
337 {
338 http_client_stop = 1;
339 }
340 #ifdef NX_WEB_HTTPS_ENABLE
341 else if (i == 1)
342 {
343 https_client_stop = 1;
344 }
345 #endif /* NX_WEB_HTTPS_ENABLE */
346 }
347 }
348
349
350 /* Define the helper HTTP server thread. */
thread_server_entry(ULONG thread_input)351 void thread_server_entry(ULONG thread_input)
352 {
353 UINT i;
354 UINT status;
355 FX_FILE my_file;
356 UINT server_port = NX_WEB_HTTP_SERVER_PORT;
357
358
359 /* Print out test information banner. */
360 printf("NetX Test: Web Invalid Release Test..................................");
361
362 /* Check for earlier error. */
363 if(error_counter)
364 {
365 printf("ERROR!\n");
366 test_control_return(1);
367 }
368
369 fx_media_format(&ram_disk,
370 _fx_ram_driver, // Driver entry
371 ram_disk_memory, // RAM disk memory pointer
372 media_memory, // Media buffer pointer
373 sizeof(media_memory), // Media buffer size
374 "MY_RAM_DISK", // Volume Name
375 1, // Number of FATs
376 32, // Directory Entries
377 0, // Hidden sectors
378 256, // Total sectors
379 512, // Sector size
380 8, // Sectors per cluster
381 1, // Heads
382 1); // Sectors per track
383
384 /* Open the RAM disk. */
385 status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, media_memory, sizeof(media_memory)) ;
386 status += fx_file_create(&ram_disk, "TEST.TXT");
387 status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
388 status += fx_file_write(&my_file, "https server", 12);
389 status += fx_file_close(&my_file);
390 if(status)
391 error_counter++;
392
393 /* Give NetX a chance to initialize the system. */
394 tx_thread_sleep(NX_IP_PERIODIC_RATE);
395
396 /* First loop test HTTP, second loop test HTTPS. */
397 for (i = 0; i < loop; i++)
398 {
399
400 if (i == 1)
401 {
402 server_port = NX_WEB_HTTPS_SERVER_PORT;
403 }
404
405 /* Create the HTTP Server. */
406 status = nx_web_http_server_create(&my_server, "My HTTP Server", &server_ip, server_port, &ram_disk,
407 &server_stack, sizeof(server_stack), &server_pool,
408 NX_NULL, NX_NULL);
409 if (status)
410 error_counter++;
411
412 #ifdef NX_WEB_HTTPS_ENABLE
413 /* Set TLS for HTTPS. */
414 if (i == 1)
415 {
416 /* Initialize device certificate (used for all sessions in HTTPS server). */
417 memset(&certificate, 0, sizeof(certificate));
418 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);
419
420 /* Setup TLS session data for the TCP server. */
421 status = nx_web_http_server_secure_configure(&my_server, &nx_crypto_tls_ciphers,
422 crypto_metadata_server, sizeof(crypto_metadata_server), tls_packet_buffer, sizeof(tls_packet_buffer),
423 &certificate, NX_NULL, 0, NX_NULL, 0, NX_NULL, 0);
424 if (status)
425 error_counter++;
426 }
427 #endif /* NX_WEB_HTTPS_ENABLE */
428
429 /* OK to start the HTTP Server. */
430 status = nx_web_http_server_start(&my_server);
431 if (status)
432 error_counter++;
433
434 /* Set the flag. */
435 if (i == 0)
436 {
437 http_server_start = 1;
438
439 /* Wait HTTP test finished. */
440 while(!http_client_stop)
441 {
442 tx_thread_sleep(NX_IP_PERIODIC_RATE);
443 }
444 }
445 #ifdef NX_WEB_HTTPS_ENABLE
446 else
447 {
448 https_server_start = 1;
449
450 /* Wait HTTPS test finished. */
451 while(!https_client_stop)
452 {
453 tx_thread_sleep(NX_IP_PERIODIC_RATE);
454 }
455 }
456 #endif /* NX_WEB_HTTPS_ENABLE */
457
458 status = nx_web_http_server_delete(&my_server);
459 if (status)
460 error_counter++;
461 }
462
463 /* Check packet pool. */
464 if (server_pool.nx_packet_pool_available != server_pool.nx_packet_pool_total)
465 {
466 error_counter++;
467 }
468
469 if (client_pool.nx_packet_pool_available != client_pool.nx_packet_pool_total)
470 {
471 error_counter++;
472 }
473
474 if (client_pool.nx_packet_pool_invalid_releases || server_pool.nx_packet_pool_invalid_releases)
475 {
476 error_counter++;
477 }
478
479 if(error_counter)
480 {
481 printf("ERROR!\n");
482 test_control_return(1);
483 }
484 else
485 {
486 printf("SUCCESS!\n");
487 test_control_return(0);
488 }
489 }
490 #else
491
492 #ifdef CTEST
test_application_define(void * first_unused_memory)493 VOID test_application_define(void *first_unused_memory)
494 #else
495 void netx_web_invalid_release_test_application_define(void *first_unused_memory)
496 #endif
497 {
498
499 /* Print out test information banner. */
500 printf("NetX Test: Web Invalid Release Test..................................N/A\n");
501
502 test_control_return(3);
503 }
504 #endif
505