3 * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
5 * This file is part of CyaSSL.
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.
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.
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
26 #include <cyassl/openssl/ssl.h>
27 #include <cyassl/test.h>
28 #include <cyassl/ctaocrypt/md5.h>
30 #ifdef SINGLE_THREADED
31 #error testsuite needs threads to run, please run ctaocrypt/test, \
32 and the examples/ individually
35 void ctaocrypt_test(void*);
37 void client_test(void*);
38 void echoclient_test(void*);
40 THREAD_RETURN CYASSL_THREAD server_test(void*);
41 THREAD_RETURN CYASSL_THREAD echoserver_test(void*);
43 void file_test(char* file, byte* hash);
51 char* myoptarg = NULL;
54 int main(int argc, char** argv)
57 func_args server_args;
60 THREAD_TYPE serverThread;
64 args.argc = server_args.argc = argc;
65 args.argv = server_args.argv = argv;
69 CyaSSL_Debugging_ON();
72 if (CurrentDir("testsuite"))
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 */
78 ctaocrypt_test(&args);
79 if (args.return_code != 0) return args.return_code;
81 /* Simple CyaSSL client server test */
83 server_args.signal = &ready;
84 start_thread(server_test, &server_args, &serverThread);
85 wait_tcp_ready(&server_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;
92 /* Echo input yaSSL client server test */
93 start_thread(echoserver_test, &server_args, &serverThread);
94 wait_tcp_ready(&server_args);
97 char* myArgv[NUMARGS];
107 echo_args.argc = NUMARGS;
108 echo_args.argv = myArgv;
110 strcpy(echo_args.argv[0], "echoclient");
111 strcpy(echo_args.argv[1], "input");
112 strcpy(echo_args.argv[2], "output");
116 echoclient_test(&echo_args);
117 if (echo_args.return_code != 0) return echo_args.return_code;
120 wait_tcp_ready(&server_args);
122 /* send quit to echoserver */
124 strcpy(echo_args.argv[1], "quit");
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;
132 /* validate output equals input */
134 byte input[MD5_DIGEST_SIZE];
135 byte output[MD5_DIGEST_SIZE];
137 file_test("input", input);
138 file_test("output", output);
139 if (memcmp(input, output, sizeof(input)) != 0)
144 FreeTcpReady(&ready);
146 printf("\nAll tests passed!\n");
152 void wait_tcp_ready(func_args* args)
154 #ifdef _POSIX_THREADS
155 pthread_mutex_lock(&args->signal->mutex);
157 if (!args->signal->ready)
158 pthread_cond_wait(&args->signal->cond, &args->signal->mutex);
159 args->signal->ready = 0; /* reset */
161 pthread_mutex_unlock(&args->signal->mutex);
166 void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread)
168 #ifdef _POSIX_THREADS
169 pthread_create(thread, 0, fun, args);
172 *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0);
177 void join_thread(THREAD_TYPE thread)
179 #ifdef _POSIX_THREADS
180 pthread_join(thread, 0);
182 int res = WaitForSingleObject(thread, INFINITE);
183 assert(res == WAIT_OBJECT_0);
184 res = CloseHandle(thread);
190 void InitTcpReady(tcp_ready* ready)
193 #ifdef _POSIX_THREADS
194 pthread_mutex_init(&ready->mutex, 0);
195 pthread_cond_init(&ready->cond, 0);
200 void FreeTcpReady(tcp_ready* ready)
202 #ifdef _POSIX_THREADS
203 pthread_mutex_destroy(&ready->mutex);
204 pthread_cond_destroy(&ready->cond);
209 void file_test(char* file, byte* check)
215 byte md5sum[MD5_DIGEST_SIZE];
218 if( !( f = fopen( file, "rb" ) )) {
219 printf("Can't open %s\n", file);
222 while( ( i = (int)fread(buf, 1, sizeof(buf), f )) > 0 )
223 Md5Update(&md5, buf, i);
225 Md5Final(&md5, md5sum);
226 memcpy(check, md5sum, sizeof(md5sum));
228 for(j = 0; j < MD5_DIGEST_SIZE; ++j )
229 printf( "%02x", md5sum[j] );
231 printf(" %s\n", file);