1 #ifndef __TLS_TEST_FRAME__
2 #define __TLS_TEST_FRAME__
3 
4 /* System headers. */
5 #include <fcntl.h>
6 #include <semaphore.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/mman.h>
11 #include <sys/wait.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14 #include <errno.h>
15 
16 /* The compile definition of TEST_FOR_FRAME means that the current test program is only for the interoperability itself. */
17 #ifndef TEST_FOR_FRAME
18 
19 /* Product headers. */
20 #include "tx_api.h"
21 #include "nx_api.h"
22 #include "nx_crypto.h"
23 #include "nx_secure_tls_api.h"
24 #include "nx_secure_dtls_api.h"
25 #include "nx_secure_x509.h"
26 
27 #else /* ! TEST_FOR_FRAME */
28 
29 typedef int INT;
30 typedef unsigned int UINT;
31 typedef void VOID;
32 typedef long LONG;
33 typedef unsigned long ULONG;
34 typedef char CHAR;
35 typedef unsigned char UCHAR;
36 
37 #endif /* TEST_FOR_FRAME */
38 
39 /* Type of test instance structure. */
40 typedef struct _TLS_TEST_INSTANCE TLS_TEST_INSTANCE;
41 
42 /* Test entry function. */
43 typedef INT ( *InstanceTestEntryFunc)( TLS_TEST_INSTANCE* instance_ptr);
44 
45 /* Test instance structure. */
46 struct _TLS_TEST_INSTANCE
47 {
48 
49     /* Members related to shared memory. */
50     UINT    tls_test_shared_buffer_size;                /* The size of shared buffer. */
51     UINT    tls_test_shared_buffer_offset;              /* The location of the variable of current shared buffer offset. */
52     VOID*   tls_test_shared_buffer;                     /* The location of user's shared buffer. */
53 
54     /* Other attributes. */
55     CHAR*   tls_test_instance_name;                     /* Instance name. */
56     UINT    tls_test_timeout;                           /* Timeout before reciving SIGRECV. */
57     UINT    tls_test_delay;                             /* Delay after last test process started. */
58     UINT    tls_test_instance_identify;                 /* The location in director's registry table. */
59     UINT    tls_test_instance_status;                   /* The indication of test instance status. */
60     InstanceTestEntryFunc tls_test_entry;               /* The test entry of this test instance. */
61     pid_t   tls_test_instance_current_pid;              /* The process id of test process. */
62     TLS_TEST_INSTANCE* tls_test_next_instance_ptr;      /* The pointer to the next test instance. */
63     INT     tls_test_instance_exit_status;              /* The return code of the test process.(A negative value -N indicate that the test process wat terminated by signal N). */
64 };
65 
66 /* Test director structure. */
67 typedef struct _TLS_TEST_DIRECTOR
68 {
69     UINT    tls_test_registered_test_instances;
70     TLS_TEST_INSTANCE* tls_test_first_instance_ptr;
71 } TLS_TEST_DIRECTOR;
72 
73 typedef sem_t TLS_TEST_SEMAPHORE;
74 
75 typedef struct _TLS_TEST_EXTERNAL_TEST_PROCESS
76 {
77     INT     tls_test_external_test_process_id;
78 } TLS_TEST_EXTERNAL_TEST_PROCESS;
79 
80 /* Test instance methods. */
81 INT tls_test_instance_append( TLS_TEST_INSTANCE* instance_ptr, TLS_TEST_INSTANCE* next_instance_ptr);
82 INT tls_test_instance_create( TLS_TEST_INSTANCE** instance_ptr_ptr, CHAR* instance_name, InstanceTestEntryFunc test_entry, UINT delay, UINT timeout, UINT shared_buffer_size, VOID* reserved);
83 INT tls_test_instance_destroy( TLS_TEST_INSTANCE* instance_ptr);
84 INT tls_test_instance_find_next( TLS_TEST_INSTANCE* instance_ptr, TLS_TEST_INSTANCE** next_instance_ptr_ptr);
85 INT tls_test_instance_get_exit_status( TLS_TEST_INSTANCE* instance_ptr, INT* exit_status_ptr);
86 INT tls_test_instance_get_name( TLS_TEST_INSTANCE* instance_ptr, CHAR** name_ptr);
87 INT tls_test_instance_show_exit_status(TLS_TEST_INSTANCE* instance_ptr);
88 INT tls_test_instance_set_exit_status( TLS_TEST_INSTANCE* instance_ptr, INT exit_status);
89 INT tls_test_instance_set_exit_status( TLS_TEST_INSTANCE* instance_ptr, INT exit_status);
90 
91 /* Test director methods. */
92 INT tls_test_director_create( TLS_TEST_DIRECTOR** director_ptr, VOID* description);
93 INT tls_test_director_register_test_instance( TLS_TEST_DIRECTOR* director_ptr, TLS_TEST_INSTANCE* instance_ptr);
94 INT tls_test_director_cleanup_registered_instances( TLS_TEST_DIRECTOR* director_ptr);
95 INT tls_test_director_destroy( TLS_TEST_DIRECTOR* director_ptr);
96 INT tls_test_director_clean_all( TLS_TEST_DIRECTOR* director_ptr);
97 INT tls_test_director_test_start( TLS_TEST_DIRECTOR* director_ptr);
98 
99 /* Shared buffer manipulation. */
100 INT tls_test_instance_get_shared_buffer( TLS_TEST_INSTANCE* instance_ptr, VOID** shared_buffer_ptr);
101 INT tls_test_instance_get_shared_buffer_offset( TLS_TEST_INSTANCE* instance_ptr, UINT* offset);
102 INT tls_test_instance_set_shared_buffer_offset( TLS_TEST_INSTANCE* instance_ptr, UINT offset);
103 INT tls_test_instance_append_data_to_shared_buffer( TLS_TEST_INSTANCE* instance_ptr, VOID* data, UINT* length);
104 
105 /* Semaphore methods. */
106 INT tls_test_semaphore_create( TLS_TEST_SEMAPHORE** semaphore_ptr_ptr, UINT initial_value);
107 INT tls_test_semaphore_post( TLS_TEST_SEMAPHORE* semaphore_ptr);
108 INT tls_test_semaphore_wait( TLS_TEST_SEMAPHORE* semaphore_ptr);
109 INT tls_test_semaphore_destroy( TLS_TEST_SEMAPHORE* semaphore_ptr);
110 
111 /* External programs calling. */
112 INT tls_test_get_external_test_process_output( INT* exit_status_ptr, CHAR* argv[], VOID* output_buffer, ULONG* length_ptr);
113 INT tls_test_launch_external_test_process( INT* exit_status_ptr, CHAR* argv[]);
114 INT tls_test_launch_external_test_process_in_background( TLS_TEST_EXTERNAL_TEST_PROCESS* test_process_ptr, CHAR* argv[]);
115 INT tls_test_kill_external_test_process( TLS_TEST_EXTERNAL_TEST_PROCESS* test_process_ptr);
116 INT tls_test_wait_all_child_process( void* reserved_ptr);
117 INT tls_test_wait_external_test_process( TLS_TEST_EXTERNAL_TEST_PROCESS* test_process_ptr, INT* exit_status_ptr);
118 INT tls_test_instance_append_external_program_output_to_shared_buffer( TLS_TEST_INSTANCE* instance_ptr, INT* exit_status_ptr, CHAR* argv[]);
119 INT tls_test_uninterruptable_wait( pid_t* pid_ptr, INT* exit_status_ptr);
120 #define tls_test_sleep( secs) sleep( secs)
121 
122 /* Return code macros. */
123 #define TLS_TEST_SUCCESS                                    0
124 #define TLS_TEST_UNABLE_TO_CREATE_SHARED_MEMORY             1
125 #define TLS_TEST_INVALID_POINTER                            2
126 #define TLS_TEST_TOO_MANY_TEST_INSTANCES                    3
127 #define TLS_TEST_UNKNOWN_TYPE_ERROR                         4
128 #define TLS_TEST_ALREADY_REGISTERED                         5
129 #define TLS_TEST_NO_REGISTERED_INSTANCE                     6
130 #define TLS_TEST_INSTANCE_UNINITIALIZED                     7
131 #define TLS_TEST_UNABLE_TO_CREATE_TEST_PROCESS              8
132 #define TLS_TEST_ILLEGAL_SHARED_BUFFER_ACCESS               9
133 #define TLS_TEST_UNABLE_TO_REDIRECT_EXTERNAL_PROGRAM_OUTPUT 10
134 #define TLS_TEST_SYSTEM_CALL_FAILED                         11
135 #define TLS_TEST_INSTANCE_EXTERNAL_PROGRAM_FAILED           12
136 #define TLS_TEST_INSTANTIATION_FAILED                       13
137 #define TLS_TEST_ENTRY_FUNCTION_FAILED                      14
138 #define TLS_TEST_INSTANCE_UNEXITED                          15
139 #define TLS_TEST_INSTANCE_FAILED                            16
140 #define TLS_TEST_INSTANCE_NO_TIME_LEFT                      17
141 #define TLS_TEST_NOT_AVAILABLE                              233
142 
143 /* Test instance status */
144 #define TLS_TEST_INSTANCE_STATUS_INITIALIZED                0x1
145 #define TLS_TEST_INSTANCE_STATUS_REGISTERED                 0x10
146 #define TLS_TEST_INSTANCE_STATUS_RUNNING                    0x100
147 #define TLS_TEST_INSTANCE_STATUS_EXITED                     0x1000
148 #define TLS_TEST_INSTANCE_STATUS_SIGNALED                   0x10000
149 
150 /* Default timeout for every test process. */
151 #define TLS_TEST_PROCESS_DEFAULT_TIMEOUT                    (60*15)
152 
153 /* Maximum of instances registered in a director instance. */
154 #define TLS_TEST_MAX_TEST_INSTANCE_NUMBER                   10
155 
156 /* The buffer size for pipe data. */
157 #define TLS_TEST_PIPE_BUFFER_SIZE                           1024
158 
159 /* The maximum of external program parameters. */
160 #define TLS_TEST_MAXIMUM_EXTERNAL_PROGRAM_PARAMETERS        1024
161 
162 /* Take use of two levels of macros to stringize the result of expansison of a macro argument. */
163 #define xstr(s) str(s)
164 #define str(s) #s
165 
166 /* Specify the ipv4 address of the test device. */
167 #if defined(TLS_TEST_IP_BYTE_0) || defined(TLS_TEST_IP_BYTE_1) || defined(TLS_TEST_IP_BYTE_2) || defined(TLS_TEST_IP_BYTE_3)
168 #define TLS_TEST_IP_ADDRESS_STRING xstr(TLS_TEST_IP_BYTE_0)"."xstr(TLS_TEST_IP_BYTE_1)"."xstr(TLS_TEST_IP_BYTE_2)"."xstr(TLS_TEST_IP_BYTE_3)
169 #define TLS_TEST_IP_ADDRESS_NUMBER  IP_ADDRESS( TLS_TEST_IP_BYTE_0, TLS_TEST_IP_BYTE_1, TLS_TEST_IP_BYTE_2, TLS_TEST_IP_BYTE_3)
170 #endif /* defined() && defined() && defined() && defined() */
171 
172 /* TLS_TEST_IP_ADDRESS_STRING */
173 #ifndef TLS_TEST_IP_ADDRESS_STRING
174 #define TLS_TEST_IP_ADDRESS_STRING                          "10.0.0.1"
175 #endif /* ifndef TLS_TEST_IP_ADDRESS_STRING */
176 
177 #ifndef TLS_TEST_IP_ADDRESS_NUMBER
178 #define TLS_TEST_IP_ADDRESS_NUMBER                          IP_ADDRESS( 10, 0, 0, 1)
179 #endif /* ifndef TLS_TEST_IP_ADDRESS_NUMBER */
180 
181 /* Specify the ipv4 address of the remote device. */
182 #if defined(REMOTE_IP_BYTE_0) || defined(REMOTE_IP_BYTE_1) || defined(REMOTE_IP_BYTE_2) || defined(REMOTE_IP_BYTE_3)
183 #define REMOTE_IP_ADDRESS_STRING xstr(REMOTE_IP_BYTE_0)"."xstr(REMOTE_IP_BYTE_1)"."xstr(REMOTE_IP_BYTE_2)"."xstr(REMOTE_IP_BYTE_3)
184 #define REMOTE_IP_ADDRESS_NUMBER  IP_ADDRESS( REMOTE_IP_BYTE_0, REMOTE_IP_BYTE_1, REMOTE_IP_BYTE_2, REMOTE_IP_BYTE_3)
185 #endif /* defined() && defined() && defined() && defined() */
186 
187 #ifndef REMOTE_IP_ADDRESS_STRING
188 #define REMOTE_IP_ADDRESS_STRING                            "10.0.0.2"
189 #endif /* ifndef REMOTE_IP_ADDRESS_STRING */
190 
191 #ifndef REMOTE_IP_ADDRESS_NUMBER
192 #define REMOTE_IP_ADDRESS_NUMBER                            IP_ADDRESS( 10, 0, 0, 2)
193 #endif /* ifndef REMOTE_IP_ADDRESS_NUMBER */
194 
195 
196 /* For parallel processing, all ports are using in arrangement. */
197 #ifdef INTEROPERABILITY_TEST_ENABLE_PARALLEL_PROCESSING
198 
199 #ifndef DEVICE_SERVER_PORT
200 #define DEVICE_SERVER_PORT 4433
201 #endif /* DEVICE_SERVER_PORT */
202 
203 /* the string for DEVICE_SERVER_PORT */
204 #define DEVICE_SERVER_PORT_STRING xstr(DEVICE_SERVER_PORT)
205 
206 #ifndef LOCAL_CLIENT_PORT
207 #define LOCAL_CLIENT_PORT 30024
208 #endif /* LOCAL_CLIENT_PORT */
209 
210 /* the string for LOCAL_CLIENT_PORT */
211 #define LOCAL_CLIENT_PORT_STRING xstr(LOCAL_CLIENT_PORT)
212 
213 #endif /* INTEROPERABILITY_TEST_ENABLE_PARALLEL_PROCESSING */
214 
215 
216 /* Output error message to stderr. */
217 #define print_error_message( format, ...) printf( format, ##__VA_ARGS__)
218 
219 #define return_value_if_fail( p, val) if(!(p)){print_error_message("Error! %s:%d, "#p" failed.\n", __func__, __LINE__);return(val);}
220 
221 #define exit_if_fail( p, val) if(!(p)){print_error_message("Error! %s:%d, "#p" failed.\n", __func__, __LINE__);exit(val);}
222 
223 #define show_error_message_if_fail( p) if(!(p)){print_error_message("Error! %s:%d, "#p" failed.\n", __func__, __LINE__);}
224 
225 #define add_error_counter_if_fail( p, counter) if(!(p)){print_error_message("Error! %s:%d, "#p" failed.\n", __func__, __LINE__);counter++;}
226 
227 #endif /* __TLS_TEST_FRAME__ */
228