]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/CyaSSL/testsuite/testsuite.c
ebcb0c28f0dfa446d673d31a0db3ed95e80c8c12
[freertos] / FreeRTOS-Plus / Source / CyaSSL / testsuite / testsuite.c
1 /* testsuite.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 #ifdef HAVE_CONFIG_H
23     #include <config.h>
24 #endif
25
26 #include <cyassl/openssl/ssl.h>
27 #include <cyassl/test.h>
28 #include <cyassl/ctaocrypt/md5.h>
29
30 #ifdef SINGLE_THREADED
31     #error testsuite needs threads to run, please run ctaocrypt/test, \
32            and the examples/ individually
33 #endif
34
35 void ctaocrypt_test(void*);
36
37 void client_test(void*);
38 void echoclient_test(void*);
39
40 THREAD_RETURN CYASSL_THREAD server_test(void*);
41 THREAD_RETURN CYASSL_THREAD echoserver_test(void*);
42
43 void file_test(char* file, byte* hash);
44
45 enum {
46     NUMARGS = 3
47 };
48
49
50 int myoptind = 0;
51 char* myoptarg = NULL;
52
53
54 int main(int argc, char** argv)
55 {
56     func_args args;
57     func_args server_args;
58
59     tcp_ready ready;
60     THREAD_TYPE serverThread;
61
62     StartTCP();
63
64     args.argc = server_args.argc = argc;
65     args.argv = server_args.argv = argv;
66
67     CyaSSL_Init();
68 #ifdef DEBUG_CYASSL
69     CyaSSL_Debugging_ON();
70 #endif
71
72     if (CurrentDir("testsuite"))
73         ChangeDirBack(1);
74     else if (CurrentDir("build"))  /* Xcode->Preferences->Locations->Build */
75         ChangeDirBack(2);          /* Location "Place build product in locations
76                                       specified by targets", uses build/Debug */
77     /* CTaoCrypt test */
78     ctaocrypt_test(&args);
79     if (args.return_code != 0) return args.return_code;
80  
81     /* Simple CyaSSL client server test */
82     InitTcpReady(&ready);
83     server_args.signal = &ready;
84     start_thread(server_test, &server_args, &serverThread);
85     wait_tcp_ready(&server_args);
86
87     client_test(&args);
88     if (args.return_code != 0) return args.return_code;
89     join_thread(serverThread);
90     if (server_args.return_code != 0) return server_args.return_code;
91
92     /* Echo input yaSSL client server test */
93     start_thread(echoserver_test, &server_args, &serverThread);
94     wait_tcp_ready(&server_args);
95     {
96         func_args echo_args;
97         char* myArgv[NUMARGS];
98
99         char argc0[32];
100         char argc1[32];
101         char argc2[32];
102
103         myArgv[0] = argc0;
104         myArgv[1] = argc1;
105         myArgv[2] = argc2;
106
107         echo_args.argc = NUMARGS;
108         echo_args.argv = myArgv;
109    
110         strcpy(echo_args.argv[0], "echoclient");
111         strcpy(echo_args.argv[1], "input");
112         strcpy(echo_args.argv[2], "output");
113         remove("output");
114
115         /* make sure OK */
116         echoclient_test(&echo_args);
117         if (echo_args.return_code != 0) return echo_args.return_code;  
118
119 #ifdef CYASSL_DTLS
120         wait_tcp_ready(&server_args);
121 #endif
122         /* send quit to echoserver */
123         echo_args.argc = 2;
124         strcpy(echo_args.argv[1], "quit");
125
126         echoclient_test(&echo_args);
127         if (echo_args.return_code != 0) return echo_args.return_code;
128         join_thread(serverThread);
129         if (server_args.return_code != 0) return server_args.return_code;
130     }
131
132     /* validate output equals input */
133     {
134         byte input[MD5_DIGEST_SIZE];
135         byte output[MD5_DIGEST_SIZE];
136
137         file_test("input",  input);
138         file_test("output", output);
139         if (memcmp(input, output, sizeof(input)) != 0)
140             return EXIT_FAILURE;
141     }
142
143     CyaSSL_Cleanup();
144     FreeTcpReady(&ready);
145
146     printf("\nAll tests passed!\n");
147     return EXIT_SUCCESS;
148 }
149
150
151
152 void wait_tcp_ready(func_args* args)
153 {
154 #ifdef _POSIX_THREADS
155     pthread_mutex_lock(&args->signal->mutex);
156     
157     if (!args->signal->ready)
158         pthread_cond_wait(&args->signal->cond, &args->signal->mutex);
159     args->signal->ready = 0; /* reset */
160
161     pthread_mutex_unlock(&args->signal->mutex);
162 #endif
163 }
164
165
166 void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread)
167 {
168 #ifdef _POSIX_THREADS
169     pthread_create(thread, 0, fun, args);
170     return;
171 #else
172     *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0);
173 #endif
174 }
175
176
177 void join_thread(THREAD_TYPE thread)
178 {
179 #ifdef _POSIX_THREADS
180     pthread_join(thread, 0);
181 #else
182     int res = WaitForSingleObject(thread, INFINITE);
183     assert(res == WAIT_OBJECT_0);
184     res = CloseHandle(thread);
185     assert(res);
186 #endif
187 }
188
189
190 void InitTcpReady(tcp_ready* ready)
191 {
192     ready->ready = 0;
193 #ifdef _POSIX_THREADS
194       pthread_mutex_init(&ready->mutex, 0);
195       pthread_cond_init(&ready->cond, 0);
196 #endif
197 }
198
199
200 void FreeTcpReady(tcp_ready* ready)
201 {
202 #ifdef _POSIX_THREADS
203     pthread_mutex_destroy(&ready->mutex);
204     pthread_cond_destroy(&ready->cond);
205 #endif
206 }
207
208
209 void file_test(char* file, byte* check)
210 {
211     FILE* f;
212     int   i = 0, j;
213     Md5   md5;
214     byte  buf[1024];
215     byte  md5sum[MD5_DIGEST_SIZE];
216    
217     InitMd5(&md5); 
218     if( !( f = fopen( file, "rb" ) )) {
219         printf("Can't open %s\n", file);
220         return;
221     }
222     while( ( i = (int)fread(buf, 1, sizeof(buf), f )) > 0 )
223         Md5Update(&md5, buf, i);
224     
225     Md5Final(&md5, md5sum);
226     memcpy(check, md5sum, sizeof(md5sum));
227
228     for(j = 0; j < MD5_DIGEST_SIZE; ++j ) 
229         printf( "%02x", md5sum[j] );
230    
231     printf("  %s\n", file);
232
233     fclose(f);
234 }
235
236