1 /* api.c API unit tests
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
23 #include <cyassl/ssl.h>
24 #include <cyassl/test.h>
25 #include <tests/unit.h>
27 #define TEST_FAIL (-1)
28 #define TEST_SUCCESS (0)
30 static int test_CyaSSL_Init(void);
31 static int test_CyaSSL_Cleanup(void);
32 static int test_CyaSSL_Method_Allocators(void);
33 static int test_CyaSSL_CTX_new(CYASSL_METHOD *method);
35 static int test_CyaSSL_CTX_use_certificate_file(void);
36 static int test_CyaSSL_CTX_use_PrivateKey_file(void);
37 static int test_CyaSSL_CTX_load_verify_locations(void);
38 static int test_server_CyaSSL_new(void);
39 static int test_client_CyaSSL_new(void);
40 static int test_CyaSSL_read_write(void);
43 /* test function helpers */
44 static int test_method(CYASSL_METHOD *method, const char *name);
45 static int test_method2(CYASSL_METHOD *method, const char *name);
47 static int test_ucf(CYASSL_CTX *ctx, const char* file, int type,
48 int cond, const char* name);
49 static int test_upkf(CYASSL_CTX *ctx, const char* file, int type,
50 int cond, const char* name);
51 static int test_lvl(CYASSL_CTX *ctx, const char* file, const char* path,
52 int cond, const char* name);
54 THREAD_RETURN CYASSL_THREAD test_server_nofail(void*);
55 void test_client_nofail(void*);
58 static const char* bogusFile = "/dev/null";
59 static const char* testingFmt = " %s:";
60 static const char* resultFmt = " %s\n";
61 static const char* passed = "passed";
62 static const char* failed = "failed";
64 /* List of methods found in echoserver.c that I'm skipping for the moment:
65 * - CyaSSL_CTX_set_session_cache_mode()
70 printf(" Begin API Tests\n");
72 test_CyaSSL_Method_Allocators();
73 test_CyaSSL_CTX_new(CyaSSLv23_server_method());
75 test_CyaSSL_CTX_use_certificate_file();
76 test_CyaSSL_CTX_use_PrivateKey_file();
77 test_CyaSSL_CTX_load_verify_locations();
78 test_server_CyaSSL_new();
79 test_client_CyaSSL_new();
80 test_CyaSSL_read_write();
82 test_CyaSSL_Cleanup();
83 printf(" End API Tests\n");
88 int test_CyaSSL_Init(void)
92 printf(testingFmt, "CyaSSL_Init()");
93 result = CyaSSL_Init();
94 printf(resultFmt, result ? failed : passed);
99 static int test_CyaSSL_Cleanup(void)
103 printf(testingFmt, "CyaSSL_Cleanup()");
104 result = CyaSSL_Cleanup();
105 printf(resultFmt, result ? failed : passed);
110 int test_method(CYASSL_METHOD *method, const char *name)
112 printf(testingFmt, name);
115 printf(resultFmt, failed);
118 XFREE(method, 0, DYNAMIC_TYPE_METHOD);
119 printf(resultFmt, passed);
123 int test_method2(CYASSL_METHOD *method, const char *name)
125 printf(testingFmt, name);
128 XFREE(method, 0, DYNAMIC_TYPE_METHOD);
129 printf(resultFmt, failed);
132 printf(resultFmt, passed);
136 int test_CyaSSL_Method_Allocators(void)
138 test_method(CyaSSLv3_server_method(), "CyaSSLv3_server_method()");
139 test_method(CyaSSLv3_client_method(), "CyaSSLv3_client_method()");
140 test_method(CyaTLSv1_server_method(), "CyaTLSv1_server_method()");
141 test_method(CyaTLSv1_client_method(), "CyaTLSv1_client_method()");
142 test_method(CyaTLSv1_1_server_method(), "CyaTLSv1_1_server_method()");
143 test_method(CyaTLSv1_1_client_method(), "CyaTLSv1_1_client_method()");
144 test_method(CyaTLSv1_2_server_method(), "CyaTLSv1_2_server_method()");
145 test_method(CyaTLSv1_2_client_method(), "CyaTLSv1_2_client_method()");
146 test_method(CyaSSLv23_client_method(), "CyaSSLv23_client_method()");
149 test_method(CyaDTLSv1_server_method(), "CyaDTLSv1_server_method()");
150 test_method(CyaDTLSv1_client_method(), "CyaDTLSv1_client_method()");
151 #endif /* CYASSL_DTLS */
154 test_method2(CyaSSLv2_server_method(), "CyaSSLv2_server_method()");
155 test_method2(CyaSSLv2_client_method(), "CyaSSLv2_client_method()");
156 #endif /* OPENSSL_EXTRA */
161 int test_CyaSSL_CTX_new(CYASSL_METHOD *method)
167 printf(testingFmt, "CyaSSL_CTX_new(NULL)");
168 ctx = CyaSSL_CTX_new(NULL);
171 CyaSSL_CTX_free(ctx);
172 printf(resultFmt, failed);
175 printf(resultFmt, passed);
177 printf(testingFmt, "CyaSSL_CTX_new(method)");
178 ctx = CyaSSL_CTX_new(method);
181 printf(resultFmt, failed);
182 XFREE(method, 0, DYNAMIC_TYPE_METHOD);
183 /* free the method data. if this was successful, freeing
184 the CTX frees the method. */
188 CyaSSL_CTX_free(ctx);
189 printf(resultFmt, passed);
193 printf("test_CyaSSL_CTX_new() called without method\n");
198 #ifndef NO_FILESYSTEM
199 /* Helper for testing CyaSSL_CTX_use_certificate_file() */
200 int test_ucf(CYASSL_CTX *ctx, const char* file, int type, int cond,
205 printf(testingFmt, name);
206 result = CyaSSL_CTX_use_certificate_file(ctx, file, type);
209 printf(resultFmt, failed);
212 printf(resultFmt, passed);
216 int test_CyaSSL_CTX_use_certificate_file(void)
218 CYASSL_METHOD *method;
221 method = CyaSSLv23_server_method();
224 printf("test_CyaSSL_CTX_use_certificate_file() cannot create method\n");
228 ctx = CyaSSL_CTX_new(method);
231 printf("test_CyaSSL_CTX_use_certificate_file() cannot create context\n");
232 XFREE(method, 0, DYNAMIC_TYPE_METHOD);
236 /* setting all parameters to garbage. this should succeed with
238 /* Then set the parameters to legit values but set each item to
239 bogus and call again. Finish with a successful success. */
241 test_ucf(NULL, NULL, 9999, SSL_FAILURE,
242 "CyaSSL_CTX_use_certificate_file(NULL, NULL, 9999)");
243 /* test_ucf(NULL, svrCert, SSL_FILETYPE_PEM, SSL_FAILURE,
244 "CyaSSL_CTX_use_certificate_file(NULL, svrCert, SSL_FILETYPE_PEM)");*/
245 test_ucf(ctx, bogusFile, SSL_FILETYPE_PEM, SSL_FAILURE,
246 "CyaSSL_CTX_use_certificate_file(ctx, bogusFile, SSL_FILETYPE_PEM)");
247 test_ucf(ctx, svrCert, 9999, SSL_FAILURE,
248 "CyaSSL_CTX_use_certificate_file(ctx, svrCert, 9999)");
249 test_ucf(ctx, svrCert, SSL_FILETYPE_PEM, SSL_SUCCESS,
250 "CyaSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM)");
252 CyaSSL_CTX_free(ctx);
256 /* Helper for testing CyaSSL_CTX_use_PrivateKey_file() */
257 int test_upkf(CYASSL_CTX *ctx, const char* file, int type, int cond,
262 printf(testingFmt, name);
263 result = CyaSSL_CTX_use_PrivateKey_file(ctx, file, type);
266 printf(resultFmt, failed);
269 printf(resultFmt, passed);
273 int test_CyaSSL_CTX_use_PrivateKey_file(void)
275 CYASSL_METHOD *method;
278 method = CyaSSLv23_server_method();
281 printf("test_CyaSSL_CTX_use_PrivateKey_file() cannot create method\n");
285 ctx = CyaSSL_CTX_new(method);
288 printf("test_CyaSSL_CTX_use_PrivateKey_file() cannot create context\n");
289 XFREE(method, 0, DYNAMIC_TYPE_METHOD);
293 test_upkf(NULL, NULL, 9999, SSL_FAILURE,
294 "CyaSSL_CTX_use_PrivateKey_file(NULL, NULL, 9999)");
295 /* test_upkf(NULL, svrKey, SSL_FILETYPE_PEM, SSL_FAILURE,
296 "CyaSSL_CTX_use_PrivateKey_file(NULL, svrKey, SSL_FILETYPE_PEM)");*/
297 test_upkf(ctx, bogusFile, SSL_FILETYPE_PEM, SSL_FAILURE,
298 "CyaSSL_CTX_use_PrivateKey_file(ctx, bogusFile, SSL_FILETYPE_PEM)");
299 test_upkf(ctx, svrKey, 9999, SSL_FAILURE,
300 "CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, 9999)");
301 test_upkf(ctx, svrKey, SSL_FILETYPE_PEM, SSL_SUCCESS,
302 "CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM)");
304 CyaSSL_CTX_free(ctx);
308 /* Helper for testing CyaSSL_CTX_load_verify_locations() */
309 int test_lvl(CYASSL_CTX *ctx, const char* file, const char* path, int cond,
314 printf(testingFmt, name);
316 * CyaSSL_CTX_load_verify_locations() returns SSL_SUCCESS (1) for
317 * success, SSL_FAILURE (0) for a non-specific failure, or a specific
318 * failure code (<0). Need to normalize the return code to 1 or 0.
320 result = CyaSSL_CTX_load_verify_locations(ctx, file, path) >= SSL_SUCCESS;
323 printf(resultFmt, failed);
326 printf(resultFmt, passed);
330 int test_CyaSSL_CTX_load_verify_locations(void)
332 CYASSL_METHOD *method;
335 method = CyaSSLv23_client_method();
338 printf("test_CyaSSL_CTX_load_verify_locations() cannot create method\n");
342 ctx = CyaSSL_CTX_new(method);
345 printf("test_CyaSSL_CTX_load_verify_locations() cannot create context\n");
350 test_lvl(NULL, NULL, NULL, SSL_FAILURE,
351 "CyaSSL_CTX_load_verify_locations(NULL, NULL, NULL)");
352 test_lvl(ctx, NULL, NULL, SSL_FAILURE,
353 "CyaSSL_CTX_load_verify_locations(ctx, NULL, NULL)");
354 test_lvl(NULL, caCert, NULL, SSL_FAILURE,
355 "CyaSSL_CTX_load_verify_locations(ctx, NULL, NULL)");
356 test_lvl(ctx, caCert, bogusFile, SSL_FAILURE,
357 "CyaSSL_CTX_load_verify_locations(ctx, caCert, bogusFile)");
358 /* Add a test for the certs directory path loading. */
359 /* There is a leak here. If you load a second cert, the first one
361 test_lvl(ctx, caCert, 0, SSL_SUCCESS,
362 "CyaSSL_CTX_load_verify_locations(ctx, caCert, 0)");
364 CyaSSL_CTX_free(ctx);
368 int test_server_CyaSSL_new(void)
372 CYASSL_CTX *ctx_nocert;
375 ctx = CyaSSL_CTX_new(CyaSSLv23_server_method());
378 printf("test_server_CyaSSL_new() cannot create context\n");
382 result = CyaSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM);
383 if (result == SSL_FAILURE)
385 printf("test_server_CyaSSL_new() cannot obtain certificate\n");
386 CyaSSL_CTX_free(ctx);
390 result = CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM);
391 if (result == SSL_FAILURE)
393 printf("test_server_CyaSSL_new() cannot obtain key\n");
394 CyaSSL_CTX_free(ctx);
398 ctx_nocert = CyaSSL_CTX_new(CyaSSLv23_server_method());
399 if (ctx_nocert == NULL)
401 printf("test_server_CyaSSL_new() cannot create bogus context\n");
402 CyaSSL_CTX_free(ctx);
406 printf(testingFmt, "CyaSSL_new(NULL) server");
407 ssl = CyaSSL_new(NULL);
410 printf(resultFmt, failed);
414 printf(resultFmt, passed);
416 printf(testingFmt, "CyaSSL_new(ctx_nocert) server");
417 ssl = CyaSSL_new(ctx_nocert);
420 printf(resultFmt, failed);
424 printf(resultFmt, passed);
426 printf(testingFmt, "CyaSSL_new(ctx) server");
427 ssl = CyaSSL_new(ctx);
429 printf(resultFmt, failed);
432 printf(resultFmt, passed);
436 CyaSSL_CTX_free(ctx_nocert);
437 CyaSSL_CTX_free(ctx);
441 int test_client_CyaSSL_new(void)
445 CYASSL_CTX *ctx_nocert;
448 ctx = CyaSSL_CTX_new(CyaSSLv23_client_method());
451 printf("test_client_CyaSSL_new() cannot create context\n");
455 result = CyaSSL_CTX_load_verify_locations(ctx, caCert, 0);
456 if (result == SSL_FAILURE)
458 printf("test_client_CyaSSL_new() cannot obtain certificate\n");
459 CyaSSL_CTX_free(ctx);
463 ctx_nocert = CyaSSL_CTX_new(CyaSSLv23_client_method());
464 if (ctx_nocert == NULL)
466 printf("test_client_CyaSSL_new() cannot create bogus context\n");
467 CyaSSL_CTX_free(ctx);
471 printf(testingFmt, "CyaSSL_new(NULL) client");
472 ssl = CyaSSL_new(NULL);
475 printf(resultFmt, failed);
479 printf(resultFmt, passed);
481 printf(testingFmt, "CyaSSL_new(ctx_nocert) client");
482 ssl = CyaSSL_new(ctx_nocert);
484 printf(resultFmt, failed);
487 printf(resultFmt, passed);
491 printf(testingFmt, "CyaSSL_new(ctx) client");
492 ssl = CyaSSL_new(ctx);
494 printf(resultFmt, failed);
497 printf(resultFmt, passed);
501 CyaSSL_CTX_free(ctx_nocert);
502 CyaSSL_CTX_free(ctx);
507 static int test_CyaSSL_read_write(void)
509 /* The unit testing for read and write shall happen simutaneously, since
510 * one can't do anything with one without the other. (Except for a failure
511 * test case.) This function will call all the others that will set up,
512 * execute, and report their test findings.
514 * Set up the success case first. This function will become the template
515 * for the other tests. This should eventually be renamed
517 * The success case isn't interesting, how can this fail?
518 * - Do not give the client context a CA certificate. The connect should
519 * fail. Do not need server for this?
520 * - Using NULL for the ssl object on server. Do not need client for this.
521 * - Using NULL for the ssl object on client. Do not need server for this.
522 * - Good ssl objects for client and server. Client write() without server
524 * - Good ssl objects for client and server. Server write() without client
526 * - Forgetting the password callback?
528 int test_result = TEST_SUCCESS;
530 func_args client_args;
531 func_args server_args;
532 THREAD_TYPE serverThread;
536 InitTcpReady(&ready);
537 server_args.signal = &ready;
538 start_thread(test_server_nofail, &server_args, &serverThread);
539 wait_tcp_ready(&server_args);
540 test_client_nofail(&client_args);
541 join_thread(serverThread);
543 if (client_args.return_code != TEST_SUCCESS)
545 printf(resultFmt, "client failure");
546 test_result = TEST_FAIL;
548 if (server_args.return_code != TEST_SUCCESS)
550 printf(resultFmt, "server failure");
551 test_result = TEST_FAIL;
554 FreeTcpReady(&ready);
560 THREAD_RETURN CYASSL_THREAD test_server_nofail(void* args)
565 CYASSL_METHOD* method = 0;
569 char msg[] = "I hear you fa shizzle!";
573 ((func_args*)args)->return_code = TEST_FAIL;
574 method = CyaSSLv23_server_method();
575 ctx = CyaSSL_CTX_new(method);
577 CyaSSL_CTX_set_verify(ctx,
578 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
581 CyaSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
584 if (CyaSSL_CTX_load_verify_locations(ctx, cliCert, 0) != SSL_SUCCESS)
586 /*err_sys("can't load ca file, Please run from CyaSSL home dir");*/
589 if (CyaSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM)
592 /*err_sys("can't load server cert chain file, "
593 "Please run from CyaSSL home dir");*/
596 if (CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM)
599 /*err_sys("can't load server key file, "
600 "Please run from CyaSSL home dir");*/
603 ssl = CyaSSL_new(ctx);
604 tcp_accept(&sockfd, &clientfd, (func_args*)args, yasslPort, 0, 0);
607 CyaSSL_set_fd(ssl, clientfd);
610 #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
611 CyaSSL_SetTmpDH_file(ssl, dhParam, SSL_FILETYPE_PEM);
613 SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
616 if (CyaSSL_accept(ssl) != SSL_SUCCESS)
618 int err = CyaSSL_get_error(ssl, 0);
620 printf("error = %d, %s\n", err, CyaSSL_ERR_error_string(err, buffer));
621 /*err_sys("SSL_accept failed");*/
625 idx = CyaSSL_read(ssl, input, sizeof(input));
628 printf("Client message: %s\n", input);
631 if (CyaSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg))
633 /*err_sys("SSL_write failed");*/
637 CyaSSL_shutdown(ssl);
639 CyaSSL_CTX_free(ctx);
641 CloseSocket(clientfd);
642 ((func_args*)args)->return_code = TEST_SUCCESS;
646 void test_client_nofail(void* args)
650 CYASSL_METHOD* method = 0;
654 char msg[64] = "hello cyassl!";
657 int msgSz = strlen(msg);
659 int argc = ((func_args*)args)->argc;
660 char** argv = ((func_args*)args)->argv;
662 ((func_args*)args)->return_code = TEST_FAIL;
663 method = CyaSSLv23_client_method();
664 ctx = CyaSSL_CTX_new(method);
667 CyaSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
670 if (CyaSSL_CTX_load_verify_locations(ctx, caCert, 0) != SSL_SUCCESS)
672 /* err_sys("can't load ca file, Please run from CyaSSL home dir");*/
675 if (CyaSSL_CTX_use_certificate_file(ctx, cliCert, SSL_FILETYPE_PEM)
678 /*err_sys("can't load client cert file, "
679 "Please run from CyaSSL home dir");*/
682 if (CyaSSL_CTX_use_PrivateKey_file(ctx, cliKey, SSL_FILETYPE_PEM)
685 /*err_sys("can't load client key file, "
686 "Please run from CyaSSL home dir");*/
690 tcp_connect(&sockfd, yasslIP, yasslPort, 0);
692 ssl = CyaSSL_new(ctx);
693 CyaSSL_set_fd(ssl, sockfd);
694 if (CyaSSL_connect(ssl) != SSL_SUCCESS)
696 int err = CyaSSL_get_error(ssl, 0);
698 printf("err = %d, %s\n", err, CyaSSL_ERR_error_string(err, buffer));
699 /*printf("SSL_connect failed");*/
703 if (CyaSSL_write(ssl, msg, msgSz) != msgSz)
705 /*err_sys("SSL_write failed");*/
709 input = CyaSSL_read(ssl, reply, sizeof(reply));
713 printf("Server response: %s\n", reply);
716 ((func_args*)args)->return_code = TEST_SUCCESS;
723 #endif /* NO_FILESYSTEM */