1 /* This case tests the cleanup function.
2  */
3 #include    "tx_api.h"
4 #include    "nx_api.h"
5 #include    "fx_api.h"
6 #include    "nx_web_http_client.h"
7 #include    "nx_web_http_server.h"
8 
9 extern void test_control_return(UINT);
10 
11 #if !defined(NX_DISABLE_IPV4)
12 
13 #include "test_device_cert.c"
14 #include "test_ca_cert.c"
15 #define ca_cert_der test_ca_cert_der
16 #define ca_cert_der_len test_ca_cert_der_len
17 
18 #define     DEMO_STACK_SIZE         4096
19 
20 /* Set up FileX and file memory resources. */
21 static CHAR             ram_disk_memory[4096];
22 static FX_MEDIA         ram_disk;
23 static UCHAR            media_memory[4096];
24 
25 static UCHAR            server_stack[16000];
26 
27 /* Define device drivers.  */
28 extern void _fx_ram_driver(FX_MEDIA *media_ptr);
29 extern void _nx_ram_network_driver_1024(NX_IP_DRIVER *driver_req_ptr);
30 
31 /* Set up the HTTP client global variables. */
32 
33 #define         CLIENT_PACKET_SIZE  (NX_WEB_HTTP_CLIENT_MIN_PACKET_SIZE * 2)
34 
35 static TX_THREAD           client_thread;
36 static NX_PACKET_POOL      client_pool;
37 static NX_WEB_HTTP_CLIENT  my_client;
38 static NX_IP               client_ip;
39 static UINT                error_counter;
40 
41 /* Set up the HTTP server global variables */
42 
43 #define         SERVER_PACKET_SIZE  (NX_WEB_HTTP_SERVER_MIN_PACKET_SIZE * 2)
44 
45 static NX_WEB_HTTP_SERVER  my_server;
46 static NX_PACKET_POOL      server_pool;
47 static TX_THREAD           server_thread;
48 static NX_IP               server_ip;
49 static NXD_ADDRESS         server_ip_address;
50 static UINT                http_client_get = 0;
51 static UINT                http_client_stop = 0;
52 static UINT                http_server_rst = 0;
53 
54 static void thread_client_entry(ULONG thread_input);
55 static void thread_server_entry(ULONG thread_input);
56 
57 #define HTTP_SERVER_ADDRESS  IP_ADDRESS(1,2,3,4)
58 #define HTTP_CLIENT_ADDRESS  IP_ADDRESS(1,2,3,5)
59 
60 extern VOID _nx_web_http_client_error_exit(NX_WEB_HTTP_CLIENT *client_ptr, UINT wait_option);
61 
62 #ifdef CTEST
test_application_define(void * first_unused_memory)63 VOID test_application_define(void *first_unused_memory)
64 #else
65 void    netx_web_client_rst_test_application_define(void *first_unused_memory)
66 #endif
67 {
68 CHAR    *pointer;
69 UINT    status;
70 
71 
72     error_counter = 0;
73 
74     /* Setup the working pointer.  */
75     pointer =  (CHAR *) first_unused_memory;
76 
77     /* Create a helper thread for the server. */
78     tx_thread_create(&server_thread, "HTTP Server thread", thread_server_entry, 0,
79                      pointer, DEMO_STACK_SIZE,
80                      NX_WEB_HTTP_SERVER_PRIORITY, NX_WEB_HTTP_SERVER_PRIORITY, TX_NO_TIME_SLICE, TX_AUTO_START);
81 
82     pointer =  pointer + DEMO_STACK_SIZE;
83 
84     /* Initialize the NetX system.  */
85     nx_system_initialize();
86 
87     /* Create the server packet pool.  */
88     status =  nx_packet_pool_create(&server_pool, "HTTP Server Packet Pool", SERVER_PACKET_SIZE,
89                                     pointer, SERVER_PACKET_SIZE*8);
90     pointer = pointer + SERVER_PACKET_SIZE * 8;
91     if (status)
92         error_counter++;
93 
94     /* Create an IP instance.  */
95     status = nx_ip_create(&server_ip, "HTTP Server IP", HTTP_SERVER_ADDRESS,
96                           0xFFFFFF00UL, &server_pool, _nx_ram_network_driver_1024,
97                           pointer, 4096, 1);
98     pointer =  pointer + 4096;
99     if (status)
100         error_counter++;
101 
102     /* Enable ARP and supply ARP cache memory for the server IP instance.  */
103     status = nx_arp_enable(&server_ip, (void *) pointer, 1024);
104     pointer = pointer + 1024;
105     if (status)
106         error_counter++;
107 
108      /* Enable TCP traffic.  */
109     status = nx_tcp_enable(&server_ip);
110     if (status)
111         error_counter++;
112 
113     /* Create the HTTP Client thread. */
114     status = tx_thread_create(&client_thread, "HTTP Client", thread_client_entry, 0,
115                               pointer, DEMO_STACK_SIZE,
116                               NX_WEB_HTTP_SERVER_PRIORITY + 2, NX_WEB_HTTP_SERVER_PRIORITY + 2, TX_NO_TIME_SLICE, TX_AUTO_START);
117     pointer =  pointer + DEMO_STACK_SIZE;
118     if (status)
119         error_counter++;
120 
121     /* Create the Client packet pool.  */
122     status =  nx_packet_pool_create(&client_pool, "HTTP Client Packet Pool", CLIENT_PACKET_SIZE,
123                                     pointer, CLIENT_PACKET_SIZE*8);
124     pointer = pointer + CLIENT_PACKET_SIZE * 8;
125     if (status)
126         error_counter++;
127 
128     /* Create an IP instance.  */
129     status = nx_ip_create(&client_ip, "HTTP Client IP", HTTP_CLIENT_ADDRESS,
130                           0xFFFFFF00UL, &client_pool, _nx_ram_network_driver_1024,
131                           pointer, 2048, 1);
132     pointer =  pointer + 2048;
133     if (status)
134         error_counter++;
135 
136     status  = nx_arp_enable(&client_ip, (void *) pointer, 1024);
137     pointer =  pointer + 2048;
138     if (status)
139         error_counter++;
140 
141      /* Enable TCP traffic.  */
142     status = nx_tcp_enable(&client_ip);
143     if (status)
144         error_counter++;
145 }
146 
thread_client_entry(ULONG thread_input)147 void thread_client_entry(ULONG thread_input)
148 {
149 UINT            status;
150 NX_PACKET       *send_packet;
151 NX_PACKET       *recv_packet;
152 UINT            recv_length = 0;
153 
154 
155     /* Give IP task and driver a chance to initialize the system. */
156     tx_thread_sleep(NX_IP_PERIODIC_RATE);
157 
158     /* Set server IP address.  */
159     server_ip_address.nxd_ip_address.v4 = HTTP_SERVER_ADDRESS;
160     server_ip_address.nxd_ip_version = NX_IP_VERSION_V4;
161 
162     /* Create an HTTP client instance.  */
163     status = nx_web_http_client_create(&my_client, "HTTP Client", &client_ip, &client_pool, 1536);
164 
165     /* Check status.  */
166     if (status)
167         error_counter++;
168 
169     /* Reconnect with server, the client should clean up the temporary variables and reset the state.  */
170     /* Send a GET request.  */
171     status = nx_web_http_client_get_start(&my_client, &server_ip_address,
172                                           NX_WEB_HTTP_SERVER_PORT, "TEST.TXT",
173                                           "www.abc.com", "name", "password", NX_WAIT_FOREVER);
174 
175     /* Check status.  */
176     if (status)
177         error_counter++;
178 
179     http_client_get = NX_TRUE;
180 
181     /* Reset the receive length.  */
182     recv_length = 0;
183 
184     /* Wait HTTP Server RST.  */
185     while(http_server_rst != NX_TRUE)
186     {
187         tx_thread_sleep(1);
188     }
189 
190     /* Get response from server.  */
191     while (1)
192     {
193         status = nx_web_http_client_response_body_get(&my_client, &recv_packet, 1 * NX_IP_PERIODIC_RATE);
194 
195         if (status)
196             break;
197         else
198         {
199             recv_length += recv_packet -> nx_packet_length;
200             nx_packet_release(recv_packet);
201         }
202     }
203 
204     /* Check status.  */
205     if (status != NX_WEB_HTTP_GET_DONE)
206         error_counter++;
207     else
208     {
209         recv_length += recv_packet -> nx_packet_length;
210         nx_packet_release(recv_packet);
211     }
212 
213     if (recv_length != 25)
214         error_counter++;
215 
216     status = nx_web_http_client_delete(&my_client);
217     if (status)
218         error_counter++;
219 
220     http_client_stop = NX_TRUE;
221 }
222 
223 
224 /* Define the helper HTTP server thread.  */
thread_server_entry(ULONG thread_input)225 void    thread_server_entry(ULONG thread_input)
226 {
227 UINT            status;
228 FX_FILE         my_file;
229 UINT            server_port = NX_WEB_HTTP_SERVER_PORT;
230 
231 
232     /* Print out test information banner.  */
233     printf("NetX Test:   Web Client RST Test.......................................");
234 
235     /* Check for earlier error. */
236     if(error_counter)
237     {
238         printf("ERROR!\n");
239         test_control_return(1);
240     }
241 
242     fx_media_format(&ram_disk,
243                     _fx_ram_driver,               // Driver entry
244                     ram_disk_memory,              // RAM disk memory pointer
245                     media_memory,                 // Media buffer pointer
246                     sizeof(media_memory),         // Media buffer size
247                     "MY_RAM_DISK",                // Volume Name
248                     1,                            // Number of FATs
249                     32,                           // Directory Entries
250                     0,                            // Hidden sectors
251                     256,                          // Total sectors
252                     512,                          // Sector size
253                     8,                            // Sectors per cluster
254                     1,                            // Heads
255                     1);                           // Sectors per track
256 
257     /* Open the RAM disk.  */
258     status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, media_memory, sizeof(media_memory)) ;
259     status += fx_file_create(&ram_disk, "TEST.TXT");
260     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
261     status += fx_file_write(&my_file, "HTTP Server Data Content", sizeof("HTTP Server Data Content"));
262     status += fx_file_close(&my_file);
263     if(status)
264         error_counter++;
265 
266     /* Give NetX a chance to initialize the system. */
267     tx_thread_sleep(NX_IP_PERIODIC_RATE);
268 
269     /* Create the HTTP Server. */
270     status = nx_web_http_server_create(&my_server, "My HTTP Server", &server_ip, server_port, &ram_disk,
271                                         &server_stack, sizeof(server_stack), &server_pool,
272                                         NX_NULL, NX_NULL);
273     if (status)
274         error_counter++;
275 
276     /* OK to start the HTTP Server.   */
277     status = nx_web_http_server_start(&my_server);
278     if (status)
279         error_counter++;
280 
281     /* Wait HTTP test finished.  */
282     while(http_client_get != NX_TRUE)
283     {
284         tx_thread_sleep(1);
285     }
286 
287     /* Send RST.  */
288     nx_tcp_socket_disconnect(&(my_server.nx_web_http_server_current_session_ptr -> nx_tcp_session_socket), NX_NO_WAIT);
289 
290     http_server_rst = NX_TRUE;
291 
292     /* Wait HTTP test finished.  */
293     while(http_client_stop != NX_TRUE)
294     {
295         tx_thread_sleep(1);
296     }
297 
298     if(error_counter)
299     {
300         printf("ERROR!\n");
301         test_control_return(1);
302     }
303     else
304     {
305         printf("SUCCESS!\n");
306         test_control_return(0);
307     }
308 }
309 #else
310 
311 #ifdef CTEST
test_application_define(void * first_unused_memory)312 VOID test_application_define(void *first_unused_memory)
313 #else
314 void    netx_web_client_rst_test_application_define(void *first_unused_memory)
315 #endif
316 {
317 
318     /* Print out test information banner.  */
319     printf("NetX Test:   Web Client RST Test.......................................N/A\n");
320 
321     test_control_return(3);
322 }
323 #endif
324