]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/WolfSSL/tests/api.c
commit 9f316c246baafa15c542a5aea81a94f26e3d6507
[freertos] / FreeRTOS-Plus / Source / WolfSSL / tests / api.c
1 /* api.c API unit tests
2  *
3  * Copyright (C) 2006-2015 wolfSSL Inc.
4  *
5  * This file is part of wolfSSL. (formerly known as CyaSSL)
6  *
7  * wolfSSL 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  * wolfSSL 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 /*----------------------------------------------------------------------------*
23  | Includes
24  *----------------------------------------------------------------------------*/
25
26 #ifdef HAVE_CONFIG_H
27     #include <config.h>
28 #endif
29
30 #include <wolfssl/wolfcrypt/settings.h>
31 #ifdef HAVE_ECC
32     #include <wolfssl/wolfcrypt/ecc.h>   /* wc_ecc_fp_free */
33 #endif
34 #include <wolfssl/error-ssl.h>
35
36 #include <stdlib.h>
37 #include <wolfssl/ssl.h>  /* compatibility layer */
38 #include <wolfssl/test.h>
39 #include <tests/unit.h>
40
41 /*----------------------------------------------------------------------------*
42  | Constants
43  *----------------------------------------------------------------------------*/
44
45 #define TEST_SUCCESS    (1)
46 #define TEST_FAIL       (0)
47
48 #define testingFmt "   %s:"
49 #define resultFmt  " %s\n"
50 static const char* passed = "passed";
51 static const char* failed = "failed";
52
53 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
54 static const char* bogusFile  = "/dev/null";
55 #endif
56
57 /*----------------------------------------------------------------------------*
58  | Setup
59  *----------------------------------------------------------------------------*/
60
61 static int test_wolfSSL_Init(void)
62 {
63     int result;
64
65     printf(testingFmt, "wolfSSL_Init()");
66     result = wolfSSL_Init();
67     printf(resultFmt, result == SSL_SUCCESS ? passed : failed);
68
69     return result;
70 }
71
72
73 static int test_wolfSSL_Cleanup(void)
74 {
75     int result;
76
77     printf(testingFmt, "wolfSSL_Cleanup()");
78     result = wolfSSL_Cleanup();
79     printf(resultFmt, result == SSL_SUCCESS ? passed : failed);
80
81     return result;
82 }
83
84 /*----------------------------------------------------------------------------*
85  | Method Allocators
86  *----------------------------------------------------------------------------*/
87
88 static void test_wolfSSL_Method_Allocators(void)
89 {
90     #define TEST_METHOD_ALLOCATOR(allocator, condition) \
91         do {                                            \
92             WOLFSSL_METHOD *method;                      \
93             condition(method = allocator());            \
94             XFREE(method, 0, DYNAMIC_TYPE_METHOD);      \
95         } while(0)
96
97     #define TEST_VALID_METHOD_ALLOCATOR(a) \
98             TEST_METHOD_ALLOCATOR(a, AssertNotNull)
99
100     #define TEST_INVALID_METHOD_ALLOCATOR(a) \
101             TEST_METHOD_ALLOCATOR(a, AssertNull)
102
103 #ifndef NO_OLD_TLS
104     TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
105     TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
106     TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
107     TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
108     TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
109     TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
110 #endif
111     TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
112     TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
113     TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
114
115 #ifdef WOLFSSL_DTLS
116     #ifndef NO_OLD_TLS
117         TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
118         TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
119     #endif
120     TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
121     TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
122 #endif
123
124 #ifdef OPENSSL_EXTRA
125     TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
126     TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
127 #endif
128 }
129
130 /*----------------------------------------------------------------------------*
131  | Context
132  *----------------------------------------------------------------------------*/
133
134 static void test_wolfSSL_CTX_new(WOLFSSL_METHOD *method)
135 {
136     WOLFSSL_CTX *ctx;
137     
138     AssertNull(ctx = wolfSSL_CTX_new(NULL));
139     
140     AssertNotNull(method);
141     AssertNotNull(ctx = wolfSSL_CTX_new(method));
142
143     wolfSSL_CTX_free(ctx);
144 }
145
146
147 static void test_wolfSSL_CTX_use_certificate_file(void)
148 {
149 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
150     WOLFSSL_CTX *ctx;
151
152     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
153
154     /* invalid context */
155     AssertFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCert, 
156                                                              SSL_FILETYPE_PEM));
157     /* invalid cert file */
158     AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile, 
159                                                              SSL_FILETYPE_PEM));
160     /* invalid cert type */
161     AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCert, 9999));
162
163 #ifdef NO_RSA
164     /* rsa needed */
165     AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCert,SSL_FILETYPE_PEM));
166 #else
167     /* success */
168     AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM));
169 #endif
170
171     wolfSSL_CTX_free(ctx);
172 #endif
173 }
174
175
176 static void test_wolfSSL_CTX_use_PrivateKey_file(void)
177 {
178 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
179     WOLFSSL_CTX *ctx;
180
181     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
182
183     /* invalid context */
184     AssertFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKey, 
185                                                              SSL_FILETYPE_PEM));
186     /* invalid key file */
187     AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile, 
188                                                              SSL_FILETYPE_PEM));
189     /* invalid key type */
190     AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKey, 9999));
191
192     /* success */
193 #ifdef NO_RSA
194     /* rsa needed */
195     AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM));
196 #else
197     /* success */
198     AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM));
199 #endif
200
201     wolfSSL_CTX_free(ctx);
202 #endif
203 }
204
205
206 static void test_wolfSSL_CTX_load_verify_locations(void)
207 {
208 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
209     WOLFSSL_CTX *ctx;
210
211     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
212     
213     /* invalid context */
214     AssertFalse(wolfSSL_CTX_load_verify_locations(NULL, caCert, 0));
215
216     /* invalid ca file */
217     AssertFalse(wolfSSL_CTX_load_verify_locations(ctx, NULL,      0));
218     AssertFalse(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, 0));
219
220 #ifndef WOLFSSL_TIRTOS
221     /* invalid path */
222     /* not working... investigate! */
223     /* AssertFalse(wolfSSL_CTX_load_verify_locations(ctx, caCert, bogusFile)); */
224 #endif
225
226     /* success */
227     AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCert, 0));
228
229     wolfSSL_CTX_free(ctx);
230 #endif
231 }
232
233 /*----------------------------------------------------------------------------*
234  | SSL
235  *----------------------------------------------------------------------------*/
236
237 static void test_server_wolfSSL_new(void)
238 {
239 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
240     WOLFSSL_CTX *ctx;
241     WOLFSSL_CTX *ctx_nocert;
242     WOLFSSL *ssl;
243
244     AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
245     AssertNotNull(ctx        = wolfSSL_CTX_new(wolfSSLv23_server_method()));
246
247     AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM));
248     AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM));
249
250     /* invalid context */
251     AssertNull(ssl = wolfSSL_new(NULL));
252     AssertNull(ssl = wolfSSL_new(ctx_nocert));
253
254     /* success */
255     AssertNotNull(ssl = wolfSSL_new(ctx));
256
257     wolfSSL_free(ssl);
258     wolfSSL_CTX_free(ctx);
259     wolfSSL_CTX_free(ctx_nocert);
260 #endif
261 }
262
263
264 static void test_client_wolfSSL_new(void)
265 {
266 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
267     WOLFSSL_CTX *ctx;
268     WOLFSSL_CTX *ctx_nocert;
269     WOLFSSL *ssl;
270
271     AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
272     AssertNotNull(ctx        = wolfSSL_CTX_new(wolfSSLv23_client_method()));
273
274     AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCert, 0));
275     
276     /* invalid context */
277     AssertNull(ssl = wolfSSL_new(NULL));
278
279     /* success */
280     AssertNotNull(ssl = wolfSSL_new(ctx_nocert));
281     wolfSSL_free(ssl);
282     
283     /* success */
284     AssertNotNull(ssl = wolfSSL_new(ctx));
285     wolfSSL_free(ssl);
286     
287     wolfSSL_CTX_free(ctx);
288     wolfSSL_CTX_free(ctx_nocert);
289 #endif
290 }
291
292 /*----------------------------------------------------------------------------*
293  | IO
294  *----------------------------------------------------------------------------*/
295 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
296     !defined(NO_RSA)        && !defined(SINGLE_THREADED)
297 #define HAVE_IO_TESTS_DEPENDENCIES
298 #endif
299
300 /* helper functions */
301 #ifdef HAVE_IO_TESTS_DEPENDENCIES
302 static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
303 {
304     SOCKET_T sockfd = 0;
305     SOCKET_T clientfd = 0;
306     word16 port = wolfSSLPort;
307
308     WOLFSSL_METHOD* method = 0;
309     WOLFSSL_CTX* ctx = 0;
310     WOLFSSL* ssl = 0;
311
312     char msg[] = "I hear you fa shizzle!";
313     char input[1024];
314     int idx;
315
316 #ifdef WOLFSSL_TIRTOS
317     fdOpenSession(Task_self());
318 #endif
319
320     ((func_args*)args)->return_code = TEST_FAIL;
321     method = wolfSSLv23_server_method();
322     ctx = wolfSSL_CTX_new(method);
323
324 #if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API) && \
325    !defined(WOLFSSL_SNIFFER) && !defined(WOLFSSL_MDK_SHELL) && \
326    !defined(WOLFSSL_TIRTOS)
327     port = 0;
328 #endif
329
330     wolfSSL_CTX_set_verify(ctx,
331                           SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
332
333 #ifdef OPENSSL_EXTRA
334     wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
335 #endif
336
337     if (wolfSSL_CTX_load_verify_locations(ctx, cliCert, 0) != SSL_SUCCESS)
338     {
339         /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
340         goto done;
341     }
342     if (wolfSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM)
343             != SSL_SUCCESS)
344     {
345         /*err_sys("can't load server cert chain file, "
346                 "Please run from wolfSSL home dir");*/
347         goto done;
348     }
349     if (wolfSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM)
350             != SSL_SUCCESS)
351     {
352         /*err_sys("can't load server key file, "
353                 "Please run from wolfSSL home dir");*/
354         goto done;
355     }
356     
357     ssl = wolfSSL_new(ctx);
358     tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0);
359     CloseSocket(sockfd);
360
361     wolfSSL_set_fd(ssl, clientfd);
362
363 #ifdef NO_PSK
364     #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
365         wolfSSL_SetTmpDH_file(ssl, dhParam, SSL_FILETYPE_PEM);
366     #elif !defined(NO_DH)
367         SetDH(ssl);  /* will repick suites with DHE, higher priority than PSK */
368     #endif
369 #endif
370
371     if (wolfSSL_accept(ssl) != SSL_SUCCESS)
372     {
373         int err = wolfSSL_get_error(ssl, 0);
374         char buffer[WOLFSSL_MAX_ERROR_SZ];
375         printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer));
376         /*err_sys("SSL_accept failed");*/
377         goto done;
378     }
379
380     idx = wolfSSL_read(ssl, input, sizeof(input)-1);
381     if (idx > 0) {
382         input[idx] = 0;
383         printf("Client message: %s\n", input);
384     }
385     
386     if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg))
387     {
388         /*err_sys("SSL_write failed");*/
389 #ifdef WOLFSSL_TIRTOS
390         return;
391 #else
392         return 0;
393 #endif
394     }
395
396 #ifdef WOLFSSL_TIRTOS
397     Task_yield();
398 #endif
399
400 done:
401     wolfSSL_shutdown(ssl);
402     wolfSSL_free(ssl);
403     wolfSSL_CTX_free(ctx);
404     
405     CloseSocket(clientfd);
406     ((func_args*)args)->return_code = TEST_SUCCESS;
407
408 #ifdef WOLFSSL_TIRTOS
409     fdCloseSession(Task_self());
410 #endif
411
412 #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
413                             && defined(HAVE_THREAD_LS)
414     wc_ecc_fp_free();  /* free per thread cache */
415 #endif
416
417 #ifndef WOLFSSL_TIRTOS
418     return 0;
419 #endif
420 }
421
422
423 static void test_client_nofail(void* args)
424 {
425     SOCKET_T sockfd = 0;
426
427     WOLFSSL_METHOD*  method  = 0;
428     WOLFSSL_CTX*     ctx     = 0;
429     WOLFSSL*         ssl     = 0;
430
431     char msg[64] = "hello wolfssl!";
432     char reply[1024];
433     int  input;
434     int  msgSz = (int)strlen(msg);
435
436 #ifdef WOLFSSL_TIRTOS
437     fdOpenSession(Task_self());
438 #endif
439
440     ((func_args*)args)->return_code = TEST_FAIL;
441     method = wolfSSLv23_client_method();
442     ctx = wolfSSL_CTX_new(method);
443
444 #ifdef OPENSSL_EXTRA
445     wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
446 #endif
447
448     if (wolfSSL_CTX_load_verify_locations(ctx, caCert, 0) != SSL_SUCCESS)
449     {
450         /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
451         goto done2;
452     }
453     if (wolfSSL_CTX_use_certificate_file(ctx, cliCert, SSL_FILETYPE_PEM)
454             != SSL_SUCCESS)
455     {
456         /*err_sys("can't load client cert file, "
457                 "Please run from wolfSSL home dir");*/
458         goto done2;
459     }
460     if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKey, SSL_FILETYPE_PEM)
461             != SSL_SUCCESS)
462     {
463         /*err_sys("can't load client key file, "
464                 "Please run from wolfSSL home dir");*/
465         goto done2;
466     }
467
468     tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port, 0);
469
470     ssl = wolfSSL_new(ctx);
471     wolfSSL_set_fd(ssl, sockfd);
472     if (wolfSSL_connect(ssl) != SSL_SUCCESS)
473     {
474         int  err = wolfSSL_get_error(ssl, 0);
475         char buffer[WOLFSSL_MAX_ERROR_SZ];
476         printf("err = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer));
477         /*printf("SSL_connect failed");*/
478         goto done2;
479     }
480
481     if (wolfSSL_write(ssl, msg, msgSz) != msgSz)
482     {
483         /*err_sys("SSL_write failed");*/
484         goto done2;
485     }
486
487     input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
488     if (input > 0)
489     {
490         reply[input] = 0;
491         printf("Server response: %s\n", reply);
492     }
493
494 done2:
495     wolfSSL_free(ssl);
496     wolfSSL_CTX_free(ctx);
497     
498     CloseSocket(sockfd);
499     ((func_args*)args)->return_code = TEST_SUCCESS;
500
501 #ifdef WOLFSSL_TIRTOS
502     fdCloseSession(Task_self());
503 #endif
504
505     return;
506 }
507
508 /* SNI helper functions */
509 #ifdef HAVE_SNI
510
511 static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
512 {
513     callback_functions* callbacks = ((func_args*)args)->callbacks;
514
515     WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
516     WOLFSSL*     ssl = NULL;
517     SOCKET_T    sfd = 0;
518     SOCKET_T    cfd = 0;
519     word16      port = wolfSSLPort;
520
521     char msg[] = "I hear you fa shizzle!";
522     int  len   = (int) XSTRLEN(msg);
523     char input[1024];
524     int  idx;
525
526 #ifdef WOLFSSL_TIRTOS
527     fdOpenSession(Task_self());
528 #endif
529     ((func_args*)args)->return_code = TEST_FAIL;
530
531 #if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API) && \
532    !defined(WOLFSSL_SNIFFER) && !defined(WOLFSSL_MDK_SHELL) && \
533    !defined(WOLFSSL_TIRTOS)
534     port = 0;
535 #endif
536
537     wolfSSL_CTX_set_verify(ctx,
538                           SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
539
540 #ifdef OPENSSL_EXTRA
541     wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
542 #endif
543
544
545     AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx, cliCert, 0));
546
547     AssertIntEQ(SSL_SUCCESS,
548                wolfSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM));
549
550     AssertIntEQ(SSL_SUCCESS,
551                  wolfSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM));
552
553     if (callbacks->ctx_ready)
554         callbacks->ctx_ready(ctx);
555
556     ssl = wolfSSL_new(ctx);
557
558     tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0);
559     CloseSocket(sfd);
560
561     wolfSSL_set_fd(ssl, cfd);
562
563 #ifdef NO_PSK
564     #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
565         wolfSSL_SetTmpDH_file(ssl, dhParam, SSL_FILETYPE_PEM);
566     #elif !defined(NO_DH)
567         SetDH(ssl);  /* will repick suites with DHE, higher priority than PSK */
568     #endif
569 #endif
570
571     if (callbacks->ssl_ready)
572         callbacks->ssl_ready(ssl);
573
574     /* AssertIntEQ(SSL_SUCCESS, wolfSSL_accept(ssl)); */
575     if (wolfSSL_accept(ssl) != SSL_SUCCESS) {
576         int err = wolfSSL_get_error(ssl, 0);
577         char buffer[WOLFSSL_MAX_ERROR_SZ];
578         printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer));
579
580     } else {
581         if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
582             input[idx] = 0;
583             printf("Client message: %s\n", input);
584         }
585
586         AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
587 #ifdef WOLFSSL_TIRTOS
588         Task_yield();
589 #endif
590         wolfSSL_shutdown(ssl);
591     }
592
593     if (callbacks->on_result)
594         callbacks->on_result(ssl);
595
596     wolfSSL_free(ssl);
597     wolfSSL_CTX_free(ctx);
598     CloseSocket(cfd);
599
600     ((func_args*)args)->return_code = TEST_SUCCESS;
601
602 #ifdef WOLFSSL_TIRTOS
603     fdCloseSession(Task_self());
604 #endif
605
606 #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
607                             && defined(HAVE_THREAD_LS)
608     wc_ecc_fp_free();  /* free per thread cache */
609 #endif
610
611 #ifndef WOLFSSL_TIRTOS
612     return 0;
613 #endif
614 }
615
616
617 static void run_wolfssl_client(void* args)
618 {
619     callback_functions* callbacks = ((func_args*)args)->callbacks;
620
621     WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
622     WOLFSSL*     ssl = NULL;
623     SOCKET_T    sfd = 0;
624
625     char msg[] = "hello wolfssl server!";
626     int  len   = (int) XSTRLEN(msg);
627     char input[1024];
628     int  idx;
629
630 #ifdef WOLFSSL_TIRTOS
631     fdOpenSession(Task_self());
632 #endif
633
634     ((func_args*)args)->return_code = TEST_FAIL;
635
636 #ifdef OPENSSL_EXTRA
637     wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
638 #endif
639
640     AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx, caCert, 0));
641
642     AssertIntEQ(SSL_SUCCESS,
643                wolfSSL_CTX_use_certificate_file(ctx, cliCert, SSL_FILETYPE_PEM));
644
645     AssertIntEQ(SSL_SUCCESS,
646                  wolfSSL_CTX_use_PrivateKey_file(ctx, cliKey, SSL_FILETYPE_PEM));
647
648     if (callbacks->ctx_ready)
649         callbacks->ctx_ready(ctx);
650
651     tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0);
652
653     ssl = wolfSSL_new(ctx);
654     wolfSSL_set_fd(ssl, sfd);
655
656     if (callbacks->ssl_ready)
657         callbacks->ssl_ready(ssl);
658
659     if (wolfSSL_connect(ssl) != SSL_SUCCESS) {
660         int err = wolfSSL_get_error(ssl, 0);
661         char buffer[WOLFSSL_MAX_ERROR_SZ];
662         printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer));
663
664     } else {
665         AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
666
667         if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
668             input[idx] = 0;
669             printf("Server response: %s\n", input);
670         }
671     }
672
673     if (callbacks->on_result)
674         callbacks->on_result(ssl);
675
676     wolfSSL_free(ssl);
677     wolfSSL_CTX_free(ctx);
678     CloseSocket(sfd);
679     ((func_args*)args)->return_code = TEST_SUCCESS;
680
681 #ifdef WOLFSSL_TIRTOS
682     fdCloseSession(Task_self());
683 #endif
684 }
685
686 #endif /* HAVE_SNI */
687 #endif /* io tests dependencies */
688
689
690 static void test_wolfSSL_read_write(void)
691 {
692 #ifdef HAVE_IO_TESTS_DEPENDENCIES
693     /* The unit testing for read and write shall happen simutaneously, since
694      * one can't do anything with one without the other. (Except for a failure
695      * test case.) This function will call all the others that will set up,
696      * execute, and report their test findings.
697      *
698      * Set up the success case first. This function will become the template
699      * for the other tests. This should eventually be renamed
700      *
701      * The success case isn't interesting, how can this fail?
702      * - Do not give the client context a CA certificate. The connect should
703      *   fail. Do not need server for this?
704      * - Using NULL for the ssl object on server. Do not need client for this.
705      * - Using NULL for the ssl object on client. Do not need server for this.
706      * - Good ssl objects for client and server. Client write() without server
707      *   read().
708      * - Good ssl objects for client and server. Server write() without client
709      *   read().
710      * - Forgetting the password callback?
711     */
712     tcp_ready ready;
713     func_args client_args;
714     func_args server_args;
715     THREAD_TYPE serverThread;
716
717 #ifdef WOLFSSL_TIRTOS
718     fdOpenSession(Task_self());
719 #endif
720
721     StartTCP();
722     InitTcpReady(&ready);
723     
724     server_args.signal = &ready;
725     client_args.signal = &ready;
726     
727     start_thread(test_server_nofail, &server_args, &serverThread);
728     wait_tcp_ready(&server_args);
729     test_client_nofail(&client_args);
730     join_thread(serverThread);
731
732     AssertTrue(client_args.return_code);
733     AssertTrue(server_args.return_code);
734
735     FreeTcpReady(&ready);
736
737 #ifdef WOLFSSL_TIRTOS
738     fdOpenSession(Task_self());
739 #endif
740
741 #endif
742 }
743
744 /*----------------------------------------------------------------------------*
745  | TLS extensions tests
746  *----------------------------------------------------------------------------*/
747
748 #ifdef HAVE_SNI
749 static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
750 {
751     byte type = WOLFSSL_SNI_HOST_NAME;
752     char name[] = "www.yassl.com";
753
754     AssertIntEQ(SSL_SUCCESS,
755                     wolfSSL_CTX_UseSNI(ctx, type, (void *) name, XSTRLEN(name)));
756 }
757
758 static void use_SNI_at_ssl(WOLFSSL* ssl)
759 {
760     byte type = WOLFSSL_SNI_HOST_NAME;
761     char name[] = "www.yassl.com";
762
763     AssertIntEQ(SSL_SUCCESS,
764                         wolfSSL_UseSNI(ssl, type, (void *) name, XSTRLEN(name)));
765 }
766
767 static void different_SNI_at_ssl(WOLFSSL* ssl)
768 {
769     byte type = WOLFSSL_SNI_HOST_NAME;
770     char name[] = "ww2.yassl.com";
771
772     AssertIntEQ(SSL_SUCCESS,
773                         wolfSSL_UseSNI(ssl, type, (void *) name, XSTRLEN(name)));
774 }
775
776 static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
777 {
778     byte type = WOLFSSL_SNI_HOST_NAME;
779
780     use_SNI_at_ssl(ssl);
781
782     wolfSSL_SNI_SetOptions(ssl, type, WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
783 }
784
785 static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
786 {
787     byte type = WOLFSSL_SNI_HOST_NAME;
788
789     use_SNI_at_ssl(ssl);
790
791     wolfSSL_SNI_SetOptions(ssl, type, WOLFSSL_SNI_ANSWER_ON_MISMATCH);
792 }
793
794 static void verify_SNI_abort_on_client(WOLFSSL* ssl)
795 {
796     AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
797 }
798
799 static void verify_SNI_abort_on_server(WOLFSSL* ssl)
800 {
801     AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
802 }
803
804 static void verify_SNI_no_matching(WOLFSSL* ssl)
805 {
806     byte  type    = WOLFSSL_SNI_HOST_NAME;
807     char* request = (char*) &type; /* to be overwriten */
808
809     AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
810
811     AssertNotNull(request);
812     AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
813     AssertNull(request);
814 }
815
816 static void verify_SNI_real_matching(WOLFSSL* ssl)
817 {
818     byte   type    = WOLFSSL_SNI_HOST_NAME;
819     char*  request = NULL;
820     char   name[]  = "www.yassl.com";
821     word16 length  = XSTRLEN(name);
822
823     AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
824
825     AssertIntEQ(length, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
826     AssertNotNull(request);
827     AssertStrEQ(name, request);
828 }
829
830 static void verify_SNI_fake_matching(WOLFSSL* ssl)
831 {
832     byte   type    = WOLFSSL_SNI_HOST_NAME;
833     char*  request = NULL;
834     char   name[]  = "ww2.yassl.com";
835     word16 length  = XSTRLEN(name);
836
837     AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
838
839     AssertIntEQ(length, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
840     AssertNotNull(request);
841     AssertStrEQ(name, request);
842 }
843
844 static void test_wolfSSL_SNI_GetFromBuffer(void)
845 {
846     byte buffer[] = { /* www.paypal.com */
847         0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
848         0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
849         0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
850         0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
851         0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
852         0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
853         0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
854         0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
855         0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
856     };
857
858     byte buffer2[] = { /* api.textmate.org */
859         0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
860         0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
861         0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
862         0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
863         0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
864         0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
865         0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
866         0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
867         0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
868         0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
869         0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
870         0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
871         0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
872         0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
873         0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
874         0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
875         0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
876     };
877
878     byte buffer3[] = { /* no sni extension */
879         0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
880         0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
881         0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
882         0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
883         0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
884         0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
885         0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
886     };
887
888     byte buffer4[] = { /* last extension has zero size */
889         0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
890         0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
891         0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
892         0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
893         0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
894         0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
895         0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
896         0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
897         0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
898         0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
899         0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
900         0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
901         0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
902         0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
903         0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
904         0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
905         0x12, 0x00, 0x00
906     };
907
908     byte result[32] = {0};
909     word32 length   = 32;
910
911     AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buffer4, sizeof(buffer4),
912                                                            0, result, &length));
913
914     AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buffer3, sizeof(buffer3),
915                                                            0, result, &length));
916
917     AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buffer2, sizeof(buffer2),
918                                                            1, result, &length));
919
920     AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer, sizeof(buffer),
921                                                            0, result, &length));
922     buffer[0] = 0x16;
923
924     AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer, sizeof(buffer),
925                                                            0, result, &length));
926     buffer[1] = 0x03;
927
928     AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buffer, 
929                                            sizeof(buffer), 0, result, &length));
930     buffer[2] = 0x03;
931
932     AssertIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buffer,
933                                            sizeof(buffer), 0, result, &length));
934     buffer[4] = 0x64;
935
936     AssertIntEQ(SSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buffer, sizeof(buffer),
937                                                            0, result, &length));
938     result[length] = 0;
939     AssertStrEQ("www.paypal.com", (const char*) result);
940
941     length = 32;
942
943     AssertIntEQ(SSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buffer2, sizeof(buffer2),
944                                                            0, result, &length));
945     result[length] = 0;
946     AssertStrEQ("api.textmate.org", (const char*) result);
947 }
948
949 static void test_wolfSSL_client_server(callback_functions* client_callbacks,
950                                       callback_functions* server_callbacks)
951 {
952 #ifdef HAVE_IO_TESTS_DEPENDENCIES
953     tcp_ready ready;
954     func_args client_args;
955     func_args server_args;
956     THREAD_TYPE serverThread;
957
958     StartTCP();
959
960     client_args.callbacks = client_callbacks;
961     server_args.callbacks = server_callbacks;
962
963 #ifdef WOLFSSL_TIRTOS
964     fdOpenSession(Task_self());
965 #endif
966
967     /* RUN Server side */
968     InitTcpReady(&ready);
969     server_args.signal = &ready;
970     client_args.signal = &ready;
971     start_thread(run_wolfssl_server, &server_args, &serverThread);
972     wait_tcp_ready(&server_args);
973
974     /* RUN Client side */
975     run_wolfssl_client(&client_args);
976     join_thread(serverThread);
977
978     FreeTcpReady(&ready);
979 #ifdef WOLFSSL_TIRTOS
980     fdCloseSession(Task_self());
981 #endif
982     
983 #else
984     (void)client_callbacks;
985     (void)server_callbacks;
986 #endif
987 }
988
989 #endif /* HAVE_SNI */
990
991 static void test_wolfSSL_UseSNI(void)
992 {
993 #ifdef HAVE_SNI
994     callback_functions client_callbacks = {wolfSSLv23_client_method, 0, 0, 0};
995     callback_functions server_callbacks = {wolfSSLv23_server_method, 0, 0, 0};
996
997     WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
998     WOLFSSL     *ssl = wolfSSL_new(ctx);
999
1000     AssertNotNull(ctx);
1001     AssertNotNull(ssl);
1002
1003     /* error cases */
1004     AssertIntNE(SSL_SUCCESS,
1005                     wolfSSL_CTX_UseSNI(NULL, 0, (void *) "ctx", XSTRLEN("ctx")));
1006     AssertIntNE(SSL_SUCCESS,
1007                     wolfSSL_UseSNI(    NULL, 0, (void *) "ssl", XSTRLEN("ssl")));
1008     AssertIntNE(SSL_SUCCESS,
1009                     wolfSSL_CTX_UseSNI(ctx, -1, (void *) "ctx", XSTRLEN("ctx")));
1010     AssertIntNE(SSL_SUCCESS,
1011                     wolfSSL_UseSNI(    ssl, -1, (void *) "ssl", XSTRLEN("ssl")));
1012     AssertIntNE(SSL_SUCCESS,
1013                     wolfSSL_CTX_UseSNI(ctx,  0, (void *) NULL,  XSTRLEN("ctx")));
1014     AssertIntNE(SSL_SUCCESS,
1015                     wolfSSL_UseSNI(    ssl,  0, (void *) NULL,  XSTRLEN("ssl")));
1016
1017     /* success case */
1018     AssertIntEQ(SSL_SUCCESS,
1019                     wolfSSL_CTX_UseSNI(ctx,  0, (void *) "ctx", XSTRLEN("ctx")));
1020     AssertIntEQ(SSL_SUCCESS,
1021                     wolfSSL_UseSNI(    ssl,  0, (void *) "ssl", XSTRLEN("ssl")));
1022
1023     wolfSSL_free(ssl);
1024     wolfSSL_CTX_free(ctx);
1025
1026     /* Testing success case at ctx */
1027     client_callbacks.ctx_ready = server_callbacks.ctx_ready = use_SNI_at_ctx;
1028     server_callbacks.on_result = verify_SNI_real_matching;
1029
1030     test_wolfSSL_client_server(&client_callbacks, &server_callbacks);
1031
1032     /* Testing success case at ssl */
1033     client_callbacks.ctx_ready = server_callbacks.ctx_ready = NULL;
1034     client_callbacks.ssl_ready = server_callbacks.ssl_ready = use_SNI_at_ssl;
1035
1036     test_wolfSSL_client_server(&client_callbacks, &server_callbacks);
1037
1038     /* Testing default mismatch behaviour */
1039     client_callbacks.ssl_ready = different_SNI_at_ssl;
1040     client_callbacks.on_result = verify_SNI_abort_on_client;
1041     server_callbacks.on_result = verify_SNI_abort_on_server;
1042
1043     test_wolfSSL_client_server(&client_callbacks, &server_callbacks);
1044     client_callbacks.on_result = NULL;
1045
1046     /* Testing continue on mismatch */
1047     client_callbacks.ssl_ready = different_SNI_at_ssl;
1048     server_callbacks.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl;
1049     server_callbacks.on_result = verify_SNI_no_matching;
1050
1051     test_wolfSSL_client_server(&client_callbacks, &server_callbacks);
1052
1053     /* Testing fake answer on mismatch */
1054     server_callbacks.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl;
1055     server_callbacks.on_result = verify_SNI_fake_matching;
1056
1057     test_wolfSSL_client_server(&client_callbacks, &server_callbacks);
1058
1059     test_wolfSSL_SNI_GetFromBuffer();
1060 #endif
1061 }
1062
1063 static void test_wolfSSL_UseMaxFragment(void)
1064 {
1065 #ifdef HAVE_MAX_FRAGMENT
1066     WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
1067     WOLFSSL     *ssl = wolfSSL_new(ctx);
1068
1069     AssertNotNull(ctx);
1070     AssertNotNull(ssl);
1071
1072     /* error cases */
1073     AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
1074     AssertIntNE(SSL_SUCCESS, wolfSSL_UseMaxFragment(    NULL, WOLFSSL_MFL_2_9));
1075     AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, 0));
1076     AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, 6));
1077     AssertIntNE(SSL_SUCCESS, wolfSSL_UseMaxFragment(ssl, 0));
1078     AssertIntNE(SSL_SUCCESS, wolfSSL_UseMaxFragment(ssl, 6));
1079
1080     /* success case */
1081     AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx,  WOLFSSL_MFL_2_9));
1082     AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx,  WOLFSSL_MFL_2_10));
1083     AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx,  WOLFSSL_MFL_2_11));
1084     AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx,  WOLFSSL_MFL_2_12));
1085     AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx,  WOLFSSL_MFL_2_13));
1086     AssertIntEQ(SSL_SUCCESS, wolfSSL_UseMaxFragment(    ssl,  WOLFSSL_MFL_2_9));
1087     AssertIntEQ(SSL_SUCCESS, wolfSSL_UseMaxFragment(    ssl,  WOLFSSL_MFL_2_10));
1088     AssertIntEQ(SSL_SUCCESS, wolfSSL_UseMaxFragment(    ssl,  WOLFSSL_MFL_2_11));
1089     AssertIntEQ(SSL_SUCCESS, wolfSSL_UseMaxFragment(    ssl,  WOLFSSL_MFL_2_12));
1090     AssertIntEQ(SSL_SUCCESS, wolfSSL_UseMaxFragment(    ssl,  WOLFSSL_MFL_2_13));
1091
1092     wolfSSL_free(ssl);
1093     wolfSSL_CTX_free(ctx);
1094 #endif
1095 }
1096
1097 static void test_wolfSSL_UseTruncatedHMAC(void)
1098 {
1099 #ifdef HAVE_TRUNCATED_HMAC
1100     WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
1101     WOLFSSL     *ssl = wolfSSL_new(ctx);
1102
1103     AssertNotNull(ctx);
1104     AssertNotNull(ssl);
1105
1106     /* error cases */
1107     AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
1108     AssertIntNE(SSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
1109
1110     /* success case */
1111     AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
1112     AssertIntEQ(SSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
1113
1114     wolfSSL_free(ssl);
1115     wolfSSL_CTX_free(ctx);
1116 #endif
1117 }
1118
1119 static void test_wolfSSL_UseSupportedCurve(void)
1120 {
1121 #ifdef HAVE_SUPPORTED_CURVES
1122     WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
1123     WOLFSSL     *ssl = wolfSSL_new(ctx);
1124
1125     AssertNotNull(ctx);
1126     AssertNotNull(ssl);
1127
1128 #ifndef NO_WOLFSSL_CLIENT
1129     /* error cases */
1130     AssertIntNE(SSL_SUCCESS,
1131                       wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
1132     AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx,  0));
1133
1134     AssertIntNE(SSL_SUCCESS,
1135                           wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
1136     AssertIntNE(SSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl,  0));
1137
1138     /* success case */
1139     AssertIntEQ(SSL_SUCCESS,
1140                        wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
1141     AssertIntEQ(SSL_SUCCESS,
1142                            wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
1143 #endif
1144
1145     wolfSSL_free(ssl);
1146     wolfSSL_CTX_free(ctx);
1147 #endif
1148 }
1149
1150 /*----------------------------------------------------------------------------*
1151  | Main
1152  *----------------------------------------------------------------------------*/
1153
1154 void ApiTest(void)
1155 {
1156     printf(" Begin API Tests\n");
1157     test_wolfSSL_Init();
1158
1159     test_wolfSSL_Method_Allocators();
1160     test_wolfSSL_CTX_new(wolfSSLv23_server_method());
1161     test_wolfSSL_CTX_use_certificate_file();
1162     test_wolfSSL_CTX_use_PrivateKey_file();
1163     test_wolfSSL_CTX_load_verify_locations();
1164     test_server_wolfSSL_new();
1165     test_client_wolfSSL_new();
1166     test_wolfSSL_read_write();
1167
1168     /* TLS extensions tests */
1169     test_wolfSSL_UseSNI();
1170     test_wolfSSL_UseMaxFragment();
1171     test_wolfSSL_UseTruncatedHMAC();
1172     test_wolfSSL_UseSupportedCurve();
1173
1174     test_wolfSSL_Cleanup();
1175     printf(" End API Tests\n");
1176 }