]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/CyaSSL/tests/suites.c
6e59ed8afb7cb49815f89c8f87272c2d40111b24
[freertos] / FreeRTOS-Plus / Source / CyaSSL / tests / suites.c
1 /* suites.c
2  *
3  * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
4  *
5  * This file is part of CyaSSL.
6  *
7  * CyaSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * CyaSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20  */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <cyassl/ssl.h>
25 #include <tests/unit.h>
26
27
28 #define MAX_ARGS 40
29 #define MAX_COMMAND_SZ 240
30
31
32 void client_test(void*);
33 THREAD_RETURN CYASSL_THREAD server_test(void*);
34
35
36 static void execute_test_case(int svr_argc, char** svr_argv,
37                               int cli_argc, char** cli_argv)
38 {
39     func_args cliArgs = {cli_argc, cli_argv, 0, NULL};
40     func_args svrArgs = {svr_argc, svr_argv, 0, NULL};
41
42     tcp_ready   ready;
43     THREAD_TYPE serverThread;
44     char        commandLine[MAX_COMMAND_SZ];
45     int         i;
46     static      int tests = 1;
47
48     commandLine[0] = '\0';
49     for (i = 0; i < svr_argc; i++) {
50         strcat(commandLine, svr_argv[i]);
51         strcat(commandLine, " ");
52     }
53     printf("trying server command line[%d]: %s\n", tests, commandLine);
54
55     commandLine[0] = '\0';
56     for (i = 0; i < cli_argc; i++) {
57         strcat(commandLine, cli_argv[i]);
58         strcat(commandLine, " ");
59     }
60     printf("trying client command line[%d]: %s\n", tests++, commandLine);
61
62     InitTcpReady(&ready);
63
64     /* start server */
65     svrArgs.signal = &ready;
66     start_thread(server_test, &svrArgs, &serverThread);
67     wait_tcp_ready(&svrArgs);
68
69     /* start client */
70     client_test(&cliArgs);
71
72     /* verify results */ 
73     if (cliArgs.return_code != 0) {
74         printf("client_test failed\n");
75         exit(EXIT_FAILURE);
76     }
77
78     join_thread(serverThread);
79     if (svrArgs.return_code != 0) { 
80         printf("server_test failed\n");
81         exit(EXIT_FAILURE);
82     }
83
84     FreeTcpReady(&ready);
85
86 }
87
88 void test_harness(void* vargs)
89 {
90     func_args* args = (func_args*)vargs;
91     char* script;
92     long  sz, len;
93     int   cliMode = 0;   /* server or client command flag, server first */
94     FILE* file;
95     char* svrArgs[MAX_ARGS];
96     int   svrArgsSz;
97     char* cliArgs[MAX_ARGS];
98     int   cliArgsSz;
99     char* cursor;
100     char* comment;
101     char* fname = "tests/test.conf";
102
103
104     if (args->argc == 1) {
105         printf("notice: using default file %s\n", fname);
106     }
107     else if(args->argc != 2) {
108         printf("usage: harness [FILE]\n");
109         args->return_code = 1;
110         return;
111     }
112     else {
113         fname = args->argv[1];
114     }
115
116     file = fopen(fname, "r");
117     if (file == NULL) {
118         fprintf(stderr, "unable to open %s\n", fname);
119         args->return_code = 1;
120         return;
121     }
122     fseek(file, 0, SEEK_END);
123     sz = ftell(file);
124     rewind(file);
125     if (sz == 0) {
126         fprintf(stderr, "%s is empty\n", fname);
127         fclose(file);
128         args->return_code = 1;
129         return;
130     }
131
132     script = (char*)malloc(sz+1);
133     if (script == 0) {
134         fprintf(stderr, "unable to allocte script buffer\n");
135         fclose(file);
136         args->return_code = 1;
137         return;
138     }
139
140     len = fread(script, 1, sz, file);
141     if (len != sz) {
142         fprintf(stderr, "read error\n");
143         fclose(file);
144         args->return_code = 1;
145         return;
146     }
147     
148     fclose(file);
149     script[sz] = 0;
150
151     cursor = script;
152     svrArgsSz = 1;
153     svrArgs[0] = args->argv[0];
154     cliArgsSz = 1;
155     cliArgs[0] = args->argv[0];
156
157     while (*cursor != 0) {
158         int do_it = 0;
159
160         switch (*cursor) {
161             case '\n':
162                 /* A blank line triggers test case execution or switches
163                    to client mode if we don't have the client command yet */
164                 if (cliMode == 0)
165                     cliMode = 1;  /* switch to client mode processing */
166                 else
167                     do_it = 1;    /* Do It, we have server and client */
168                 cursor++;
169                 break;
170             case '#':
171                 /* Ignore lines that start with a #. */
172                 comment = strsep(&cursor, "\n");
173                 printf("%s\n", comment);
174                 break;
175             case '-':
176                 /* Parameters start with a -. They end in either a newline
177                  * or a space. Capture until either, save in Args list. */
178                 if (cliMode)
179                     cliArgs[cliArgsSz++] = strsep(&cursor, " \n");
180                 else
181                     svrArgs[svrArgsSz++] = strsep(&cursor, " \n");
182                 break;
183             default:
184                 /* Anything from cursor until end of line that isn't the above
185                  * is data for a paramter. Just up until the next newline in
186                  * the Args list. */
187                 if (cliMode)
188                     cliArgs[cliArgsSz++] = strsep(&cursor, "\n");
189                 else
190                     svrArgs[svrArgsSz++] = strsep(&cursor, "\n");
191                 if (*cursor == 0)  /* eof */
192                     do_it = 1; 
193         }
194
195         if (svrArgsSz == MAX_ARGS || cliArgsSz == MAX_ARGS) {
196             fprintf(stderr, "too many arguments, forcing test run\n");
197             do_it = 1;
198         }
199
200         if (do_it) {
201             execute_test_case(svrArgsSz, svrArgs, cliArgsSz, cliArgs);
202             svrArgsSz = 1;
203             cliArgsSz = 1;
204             cliMode   = 0;
205         }
206     }
207
208     free(script);
209     args->return_code = 0;
210 }
211
212
213 int SuiteTest(void)
214 {
215     func_args args;
216     char argv0[2][32];
217     char* myArgv[2];
218
219     printf(" Begin Cipher Suite Tests\n");
220
221     /* setup */
222     myArgv[0] = argv0[0];
223     myArgv[1] = argv0[1];
224     args.argv = myArgv;
225     strcpy(argv0[0], "SuiteTest");
226
227     /* default case */
228     args.argc = 1;
229     printf("starting default cipher suite tests\n");
230     test_harness(&args);
231     if (args.return_code != 0) {
232         printf("error from script %d\n", args.return_code);
233         exit(EXIT_FAILURE);  
234     }
235
236     /* any extra cases will need another argument */
237     args.argc = 2;
238
239 #ifdef OPENSSL_EXTRA
240     /* add openssl extra suites */
241     strcpy(argv0[1], "tests/test-openssl.conf");
242     printf("starting openssl extra cipher suite tests\n");
243     test_harness(&args);
244     if (args.return_code != 0) {
245         printf("error from script %d\n", args.return_code);
246         exit(EXIT_FAILURE);  
247     }
248 #endif
249
250 #ifdef HAVE_HC128 
251     /* add hc128 extra suites */
252     strcpy(argv0[1], "tests/test-hc128.conf");
253     printf("starting hc128 extra cipher suite tests\n");
254     test_harness(&args);
255     if (args.return_code != 0) {
256         printf("error from script %d\n", args.return_code);
257         exit(EXIT_FAILURE);  
258     }
259 #endif
260
261 #ifndef NO_PSK
262     /* add psk extra suites */
263     strcpy(argv0[1], "tests/test-psk.conf");
264     printf("starting psk extra cipher suite tests\n");
265     test_harness(&args);
266     if (args.return_code != 0) {
267         printf("error from script %d\n", args.return_code);
268         exit(EXIT_FAILURE);  
269     }
270 #endif
271
272 #ifdef HAVE_NTRU
273     /* add ntru extra suites */
274     strcpy(argv0[1], "tests/test-ntru.conf");
275     printf("starting ntru extra cipher suite tests\n");
276     test_harness(&args);
277     if (args.return_code != 0) {
278         printf("error from script %d\n", args.return_code);
279         exit(EXIT_FAILURE);  
280     }
281 #endif
282
283 #ifdef HAVE_ECC
284     /* add ecc extra suites */
285     strcpy(argv0[1], "tests/test-ecc.conf");
286     printf("starting ecc extra cipher suite tests\n");
287     test_harness(&args);
288     if (args.return_code != 0) {
289         printf("error from script %d\n", args.return_code);
290         exit(EXIT_FAILURE);  
291     }
292 #endif
293
294 #ifdef HAVE_AESGCM
295     /* add aesgcm extra suites */
296     strcpy(argv0[1], "tests/test-aesgcm.conf");
297     printf("starting aesgcm extra cipher suite tests\n");
298     test_harness(&args);
299     if (args.return_code != 0) {
300         printf("error from script %d\n", args.return_code);
301         exit(EXIT_FAILURE);  
302     }
303 #endif
304
305 #if defined(HAVE_AESGCM) && defined(OPENSSL_EXTRA)
306     /* add aesgcm openssl extra suites */
307     strcpy(argv0[1], "tests/test-aesgcm-openssl.conf");
308     printf("starting aesgcm openssl extra cipher suite tests\n");
309     test_harness(&args);
310     if (args.return_code != 0) {
311         printf("error from script %d\n", args.return_code);
312         exit(EXIT_FAILURE);  
313     }
314 #endif
315
316 #if defined(HAVE_AESGCM) && defined(HAVE_ECC)
317     /* add aesgcm ecc extra suites */
318     strcpy(argv0[1], "tests/test-aesgcm-ecc.conf");
319     printf("starting aesgcm ecc extra cipher suite tests\n");
320     test_harness(&args);
321     if (args.return_code != 0) {
322         printf("error from script %d\n", args.return_code);
323         exit(EXIT_FAILURE);  
324     }
325 #endif
326
327 #ifdef CYASSL_DTLS 
328     /* add dtls extra suites */
329     strcpy(argv0[1], "tests/test-dtls.conf");
330     printf("starting dtls extra cipher suite tests\n");
331     test_harness(&args);
332     if (args.return_code != 0) {
333         printf("error from script %d\n", args.return_code);
334         exit(EXIT_FAILURE);  
335     }
336 #endif
337
338     printf(" End Cipher Suite Tests\n");
339
340     return args.return_code;
341 }
342
343