1 #include "tls_test_frame.h"
2 
3 /* Declare the test entries of two test instances. */
4 INT demo_func_entry_0(TLS_TEST_INSTANCE* instance_ptr);
5 INT demo_func_entry_1(TLS_TEST_INSTANCE* instance_ptr);
6 
7 /* Declare global semaphore pointers. */
8 TLS_TEST_SEMAPHORE* semaphore_server_prepared;
9 TLS_TEST_SEMAPHORE* semaphore_client_terminated;
10 
main(INT argc,CHAR * argv[])11 INT main( INT argc, CHAR* argv[])
12 {
13 INT status;
14 TLS_TEST_INSTANCE* ins0;
15 TLS_TEST_INSTANCE* ins1;
16 TLS_TEST_DIRECTOR* director;
17 INT exit_status[2], i;
18 
19     /* Create two test instances. */
20     status = tls_test_instance_create(&ins0,              /* test instance ptr */
21                                       "icmp_server",      /* instance name */
22                                       demo_func_entry_0,  /* test entry */
23                                       0,                  /* delay(seconds) */
24                                       40000,                 /* timeout(seconds) */
25                                       1024,               /* shared buffer size */
26                                       NULL);              /* reserved */
27     return_value_if_fail(TLS_TEST_SUCCESS == status, status);
28 
29     status = tls_test_instance_create(&ins1,
30                                       "icmp_client",
31                                       demo_func_entry_1,
32                                       1,
33                                       40000,
34                                       1024,
35                                       NULL);
36     return_value_if_fail(TLS_TEST_SUCCESS == status, status);
37 
38     /* Create two semaphore and set the initial value as 0. */
39     status = tls_test_semaphore_create(&semaphore_server_prepared, 0/* initial value */);
40     return_value_if_fail(TLS_TEST_SUCCESS == status, status);
41     status = tls_test_semaphore_create(&semaphore_client_terminated, 0);
42     return_value_if_fail(TLS_TEST_SUCCESS == status, status);
43 
44     /* Create the test director. */
45     status = tls_test_director_create(&director, NULL/* description (reserved) */);
46     return_value_if_fail(TLS_TEST_SUCCESS == status, status);
47 
48     /* Register test instances to the test director. */
49     status = tls_test_director_register_test_instance(director, ins0);
50     status += tls_test_director_register_test_instance(director, ins1);
51     return_value_if_fail(TLS_TEST_SUCCESS == status, TLS_TEST_UNKNOWN_TYPE_ERROR);
52 
53     /* Launch test. */
54     status = tls_test_director_test_start(director);
55     return_value_if_fail(TLS_TEST_SUCCESS == status, status);
56 
57     /* Error checking. */
58     tls_test_instance_show_exit_status(ins0);
59     tls_test_instance_show_exit_status(ins1);
60 
61     /* Record exit codes. */
62     status = tls_test_instance_get_exit_status(ins0, &(exit_status[0]));
63     status += tls_test_instance_get_exit_status(ins1, &(exit_status[1]));
64     return_value_if_fail(TLS_TEST_SUCCESS == status, TLS_TEST_UNKNOWN_TYPE_ERROR);
65 
66     /* Destroy the director and the registered instances. */
67     status = tls_test_director_clean_all(director);
68     return_value_if_fail(TLS_TEST_SUCCESS == status, status);
69 
70     /* Destroy semaphores. */
71     tls_test_semaphore_destroy(semaphore_server_prepared);
72     tls_test_semaphore_destroy(semaphore_client_terminated);
73 
74     return exit_status[0] | exit_status[1];
75 }
76 
77 /* Call external program as test entry */
demo_func_entry_1(TLS_TEST_INSTANCE * instance_ptr)78 INT demo_func_entry_1(TLS_TEST_INSTANCE* instance_ptr)
79 {
80 INT exit_status;
81 INT status;
82 /* Define an array of strings as the arguments of external program. */
83 /* Note: the last element of the array must be NULL. */
84 CHAR* external_cmd[] = { "ping", TLS_TEST_IP_ADDRESS_STRING, "-c", "4", (CHAR*)NULL};
85 
86     /* Wait for server prepared. */
87     tls_test_semaphore_wait(semaphore_server_prepared);
88 
89     /* Call external program to ping the icmp server. */
90     /* The exit code of external program will be stored in the contorl block of current instance . */
91     tls_test_launch_external_test_process(&exit_status, external_cmd);
92 
93     /* Post another semaphore after the icmp test. */
94     tls_test_semaphore_post(semaphore_client_terminated);
95 
96     /* Check for the exit status of external program. */
97     return_value_if_fail(0 == exit_status, TLS_TEST_INSTANCE_EXTERNAL_PROGRAM_FAILED);
98     return TLS_TEST_SUCCESS;
99 }
100 
101 static TLS_TEST_INSTANCE* demo_instance;
102 
103 /* Create a threax device as an icmp echo server. */
demo_func_entry_0(TLS_TEST_INSTANCE * instance_ptr)104 INT demo_func_entry_0(TLS_TEST_INSTANCE* instance_ptr)
105 {
106 VOID* shm;
107 INT status;
108 
109     /* Store the address of current instance control block in static variable for we don't have the method passing parameters to ThreadX kernel. */
110     demo_instance = instance_ptr;
111 
112     /* Enter the ThreadX kernel.  */
113     tx_kernel_enter();
114 }
115 
116 /* Define the ThreadX and NetX object control blocks...  */
117 NX_PACKET_POOL    pool_0;
118 NX_IP             ip_0;
119 UCHAR tls_packet_buffer[4000];
120 
121 /* Define the IP thread's stack area.  */
122 ULONG             ip_thread_stack[3 * 1024 / sizeof(ULONG)];
123 
124 /* Define packet pool for the demonstration.  */
125 #define NX_PACKET_POOL_SIZE ((1536 + sizeof(NX_PACKET)) * 32)
126 ULONG             packet_pool_area[NX_PACKET_POOL_SIZE/sizeof(ULONG) + 64 / sizeof(ULONG)];
127 
128 /* Define the ARP cache area.  */
129 ULONG             arp_space_area[512 / sizeof(ULONG)];
130 
131 /* Define the demo thread.  */
132 ULONG             demo_thread_stack[6 * 1024 / sizeof(ULONG)];
133 TX_THREAD         demo_thread;
134 
135 /* Pcap network driver.  */
136 VOID    _nx_pcap_network_driver(NX_IP_DRIVER *driver_req_ptr);
137 
138 /* Declare a thread entry. */
139 VOID demo_thread_entry(ULONG thread_input);
140 
141 /* Define what the initial system looks like.  */
tx_application_define(void * first_unused_memory)142 void    tx_application_define(void *first_unused_memory)
143 {
144 ULONG gateway_ipv4_address;
145 UINT  status;
146 
147     /* Initialize the NetX system.  */
148     nx_system_initialize();
149 
150     /* Create a packet pool.  */
151     status =  nx_packet_pool_create(&pool_0, "NetX Main Packet Pool", 1536,  (ULONG*)(((int)packet_pool_area + 64) & ~63) , NX_PACKET_POOL_SIZE);
152     show_error_message_if_fail( status == NX_SUCCESS);
153 
154     /* Create an IP instance.  */
155     status = nx_ip_create(&ip_0, "NetX IP Instance 0", TLS_TEST_IP_ADDRESS_NUMBER, 0xFFFFFF00UL, &pool_0, _nx_pcap_network_driver, (UCHAR*)ip_thread_stack, sizeof(ip_thread_stack), 1);
156     show_error_message_if_fail( status == NX_SUCCESS);
157 
158     /* Enable ARP and supply ARP cache memory for IP Instance 0.  */
159     status =  nx_arp_enable(&ip_0, (void *)arp_space_area, sizeof(arp_space_area));
160     show_error_message_if_fail( status == NX_SUCCESS);
161 
162     /* Enable TCP traffic.  */
163     status =  nx_tcp_enable(&ip_0);
164     show_error_message_if_fail( status == NX_SUCCESS);
165 
166     /* Enable UDP traffic.  */
167     status =  nx_udp_enable(&ip_0);
168     show_error_message_if_fail( status == NX_SUCCESS);
169 
170     /* Enable ICMP.  */
171     status =  nx_icmp_enable(&ip_0);
172     show_error_message_if_fail( status == NX_SUCCESS);
173 
174     status =  nx_ip_fragment_enable(&ip_0);
175     show_error_message_if_fail( status == NX_SUCCESS);
176 
177     /* Post the semaphore to enable icmp test. */
178     tls_test_semaphore_post(semaphore_server_prepared);
179 
180     /* Create an new thread waiting for the termination of icmp test. */
181     tx_thread_create(&demo_thread, "demo thread", demo_thread_entry, 0, demo_thread_stack, sizeof(demo_thread_stack), 16, 16, 4, TX_AUTO_START);
182 }
183 
184 /* The function entry of the thread created by tx_thread_create. */
demo_thread_entry(ULONG thread_input)185 VOID demo_thread_entry(ULONG thread_input)
186 {
187     INT status;
188 
189     /* Wait fot the termination of icmp test. */
190     /* Wait until success to avoid system call being interrupted by SIGUSR1. */
191     tls_test_semaphore_wait(semaphore_client_terminated);
192 
193     exit(0);
194 }
195