Add some more OCSP testing
[openssl.git] / test / sslapitest.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <string.h>
11
12 #include <openssl/opensslconf.h>
13 #include <openssl/bio.h>
14 #include <openssl/crypto.h>
15 #include <openssl/ssl.h>
16 #include <openssl/ocsp.h>
17
18 #include "ssltestlib.h"
19 #include "testutil.h"
20
21 static char *cert = NULL;
22 static char *privkey = NULL;
23
24 static const unsigned char orespder[] = "Dummy OCSP Response";
25 static int ocsp_server_called = 0;
26 static int ocsp_client_called = 0;
27
28 static int cdummyarg = 1;
29 static X509 *ocspcert = NULL;
30
31 static int ocsp_server_cb(SSL *s, void *arg)
32 {
33     int *argi = (int *)arg;
34     unsigned char *orespdercopy = NULL;
35     STACK_OF(OCSP_RESPID) *ids = NULL;
36     OCSP_RESPID *id = NULL;
37
38     if (*argi == 2) {
39         /* In this test we are expecting exactly 1 OCSP_RESPID */
40         SSL_get_tlsext_status_ids(s, &ids);
41         if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
42             return SSL_TLSEXT_ERR_ALERT_FATAL;
43
44         id = sk_OCSP_RESPID_value(ids, 0);
45         if (id == NULL || !OCSP_RESPID_match(id, ocspcert))
46             return SSL_TLSEXT_ERR_ALERT_FATAL;
47     } else if (*argi != 1) {
48         return SSL_TLSEXT_ERR_ALERT_FATAL;
49     }
50
51
52     orespdercopy = OPENSSL_memdup(orespder, sizeof(orespder));
53     if (orespdercopy == NULL)
54         return SSL_TLSEXT_ERR_ALERT_FATAL;
55
56     SSL_set_tlsext_status_ocsp_resp(s, orespdercopy, sizeof(orespder));
57
58     ocsp_server_called = 1;
59
60     return SSL_TLSEXT_ERR_OK;
61 }
62
63 static int ocsp_client_cb(SSL *s, void *arg)
64 {
65     int *argi = (int *)arg;
66     const unsigned char *respderin;
67     size_t len;
68
69     if (*argi != 1 && *argi != 2)
70         return 0;
71
72     len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
73
74     if (memcmp(orespder, respderin, len) != 0)
75         return 0;
76
77     ocsp_client_called = 1;
78
79     return 1;
80 }
81
82 static int test_tlsext_status_type(void)
83 {
84     SSL_CTX *cctx = NULL, *sctx = NULL;
85     SSL *clientssl = NULL, *serverssl = NULL;
86     int testresult = 0;
87     STACK_OF(OCSP_RESPID) *ids = NULL;
88     OCSP_RESPID *id = NULL;
89     BIO *certbio = NULL;
90
91     if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
92                              &cctx, cert, privkey)) {
93         printf("Unable to create SSL_CTX pair\n");
94         return 0;
95     }
96
97     if (SSL_CTX_get_tlsext_status_type(cctx) != -1) {
98         printf("Unexpected initial value for "
99                "SSL_CTX_get_tlsext_status_type()\n");
100         goto end;
101     }
102
103     /* First just do various checks getting and setting tlsext_status_type */
104
105     clientssl = SSL_new(cctx);
106     if (SSL_get_tlsext_status_type(clientssl) != -1) {
107         printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
108         goto end;
109     }
110
111     if (!SSL_set_tlsext_status_type(clientssl, TLSEXT_STATUSTYPE_ocsp)) {
112         printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
113         goto end;
114     }
115
116     if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
117         printf("Unexpected result for SSL_get_tlsext_status_type()\n");
118         goto end;
119     }
120
121     SSL_free(clientssl);
122     clientssl = NULL;
123
124     if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)) {
125         printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
126         goto end;
127     }
128
129     if (SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp) {
130         printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
131         goto end;
132     }
133
134     clientssl = SSL_new(cctx);
135
136     if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) {
137         printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
138         goto end;
139     }
140
141     SSL_free(clientssl);
142     clientssl = NULL;
143
144     /*
145      * Now actually do a handshake and check OCSP information is exchanged and
146      * the callbacks get called
147      */
148
149     SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
150     SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
151     SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
152     SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
153
154     if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
155         printf("Unable to create SSL objects\n");
156         goto end;
157     }
158
159     if (!create_ssl_connection(serverssl, clientssl)) {
160         printf("Unable to create SSL connection\n");
161         goto end;
162     }
163
164     if (!ocsp_client_called || !ocsp_server_called) {
165         printf("OCSP callbacks not called\n");
166         goto end;
167     }
168
169     SSL_free(serverssl);
170     SSL_free(clientssl);
171     serverssl = NULL;
172     clientssl = NULL;
173
174     /* Try again but this time force the server side callback to fail */
175     ocsp_client_called = 0;
176     ocsp_server_called = 0;
177     cdummyarg = 0;
178
179     if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
180         printf("Unable to create SSL objects\n");
181         goto end;
182     }
183
184     /* This should fail because the callback will fail */
185     if (create_ssl_connection(serverssl, clientssl)) {
186         printf("Unexpected success creating the connection\n");
187         goto end;
188     }
189
190     if (ocsp_client_called || ocsp_server_called) {
191         printf("OCSP callbacks successfully called unexpectedly\n");
192         goto end;
193     }
194
195     SSL_free(serverssl);
196     SSL_free(clientssl);
197     serverssl = NULL;
198     clientssl = NULL;
199
200     /*
201      * This time we'll get the client to send an OCSP_RESPID that it will
202      * accept.
203      */
204     ocsp_client_called = 0;
205     ocsp_server_called = 0;
206     cdummyarg = 2;
207
208     if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
209         printf("Unable to create SSL objects\n");
210         goto end;
211     }
212
213     /*
214      * We'll just use any old cert for this test - it doesn't have to be an OCSP
215      * specifc one. We'll use the server cert.
216      */
217     certbio = BIO_new_file(cert, "r");
218     if (certbio == NULL) {
219         printf("Can't load the certficate file\n");
220         goto end;
221     }
222     id = OCSP_RESPID_new();
223     ids = sk_OCSP_RESPID_new_null();
224     ocspcert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
225     if (id == NULL || ids == NULL || ocspcert == NULL
226             || !OCSP_RESPID_set_by_key(id, ocspcert)
227             || !sk_OCSP_RESPID_push(ids, id)) {
228         printf("Unable to set OCSP_RESPIDs\n");
229         goto end;
230     }
231     id = NULL;
232     SSL_set_tlsext_status_ids(clientssl, ids);
233     /* Control has been transferred */
234     ids = NULL;
235
236     BIO_free(certbio);
237     certbio = NULL;
238
239     if (!create_ssl_connection(serverssl, clientssl)) {
240         printf("Unable to create SSL connection\n");
241         goto end;
242     }
243
244     if (!ocsp_client_called || !ocsp_server_called) {
245         printf("OCSP callbacks not called\n");
246         goto end;
247     }
248
249     testresult = 1;
250
251  end:
252     SSL_free(serverssl);
253     SSL_free(clientssl);
254     SSL_CTX_free(sctx);
255     SSL_CTX_free(cctx);
256     sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
257     OCSP_RESPID_free(id);
258     BIO_free(certbio);
259     X509_free(ocspcert);
260     ocspcert = NULL;
261
262     return testresult;
263 }
264
265 typedef struct ssl_session_test_fixture {
266     const char *test_case_name;
267     int use_ext_cache;
268     int use_int_cache;
269 } SSL_SESSION_TEST_FIXTURE;
270
271 static int new_called = 0, remove_called = 0;
272
273 static SSL_SESSION_TEST_FIXTURE
274 ssl_session_set_up(const char *const test_case_name)
275 {
276     SSL_SESSION_TEST_FIXTURE fixture;
277
278     fixture.test_case_name = test_case_name;
279     fixture.use_ext_cache = 1;
280     fixture.use_int_cache = 1;
281
282     new_called = remove_called = 0;
283
284     return fixture;
285 }
286
287 static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
288 {
289 }
290
291 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
292 {
293     new_called++;
294
295     return 1;
296 }
297
298 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
299 {
300     remove_called++;
301 }
302
303 static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
304 {
305     SSL_CTX *sctx = NULL, *cctx = NULL;
306     SSL *serverssl1 = NULL, *clientssl1 = NULL;
307     SSL *serverssl2 = NULL, *clientssl2 = NULL;
308 #ifndef OPENSSL_NO_TLS1_1
309     SSL *serverssl3 = NULL, *clientssl3 = NULL;
310 #endif
311     SSL_SESSION *sess1 = NULL, *sess2 = NULL;
312     int testresult = 0;
313
314     if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
315                              &cctx, cert, privkey)) {
316         printf("Unable to create SSL_CTX pair\n");
317         return 0;
318     }
319
320 #ifndef OPENSSL_NO_TLS1_2
321     /* Only allow TLS1.2 so we can force a connection failure later */
322     SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
323 #endif
324
325     /* Set up session cache */
326     if (fix.use_ext_cache) {
327         SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
328         SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
329     }
330     if (fix.use_int_cache) {
331         /* Also covers instance where both are set */
332         SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
333     } else {
334         SSL_CTX_set_session_cache_mode(cctx,
335                                        SSL_SESS_CACHE_CLIENT
336                                        | SSL_SESS_CACHE_NO_INTERNAL_STORE);
337     }
338
339     if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
340                                NULL)) {
341         printf("Unable to create SSL objects\n");
342         goto end;
343     }
344
345     if (!create_ssl_connection(serverssl1, clientssl1)) {
346         printf("Unable to create SSL connection\n");
347         goto end;
348     }
349     sess1 = SSL_get1_session(clientssl1);
350     if (sess1 == NULL) {
351         printf("Unexpected NULL session\n");
352         goto end;
353     }
354
355     if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {
356         /* Should have failed because it should already be in the cache */
357         printf("Unexpected success adding session to cache\n");
358         goto end;
359     }
360
361     if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {
362         printf("Session not added to cache\n");
363         goto end;
364     }
365
366     if (!create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) {
367         printf("Unable to create second SSL objects\n");
368         goto end;
369     }
370
371     if (!create_ssl_connection(serverssl2, clientssl2)) {
372         printf("Unable to create second SSL connection\n");
373         goto end;
374     }
375
376     sess2 = SSL_get1_session(clientssl2);
377     if (sess2 == NULL) {
378         printf("Unexpected NULL session from clientssl2\n");
379         goto end;
380     }
381
382     if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {
383         printf("Remove session callback unexpectedly called\n");
384         goto end;
385     }
386
387     /*
388      * This should clear sess2 from the cache because it is a "bad" session. See
389      * SSL_set_session() documentation.
390      */
391     if (!SSL_set_session(clientssl2, sess1)) {
392         printf("Unexpected failure setting session\n");
393         goto end;
394     }
395
396     if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {
397         printf("Failed to call callback to remove session\n");
398         goto end;
399     }
400
401
402     if (SSL_get_session(clientssl2) != sess1) {
403         printf("Unexpected session found\n");
404         goto end;
405     }
406
407     if (fix.use_int_cache) {
408         if (!SSL_CTX_add_session(cctx, sess2)) {
409             /*
410              * Should have succeeded because it should not already be in the cache
411              */
412             printf("Unexpected failure adding session to cache\n");
413             goto end;
414         }
415
416         if (!SSL_CTX_remove_session(cctx, sess2)) {
417             printf("Unexpected failure removing session from cache\n");
418             goto end;
419         }
420
421         /* This is for the purposes of internal cache testing...ignore the
422          * counter for external cache
423          */
424         if (fix.use_ext_cache)
425             remove_called--;
426     }
427
428     /* This shouldn't be in the cache so should fail */
429     if (SSL_CTX_remove_session(cctx, sess2)) {
430         printf("Unexpected success removing session from cache\n");
431         goto end;
432     }
433
434     if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {
435         printf("Failed to call callback to remove session #2\n");
436         goto end;
437     }
438
439 #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
440     /* Force a connection failure */
441     SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
442
443     if (!create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) {
444         printf("Unable to create third SSL objects\n");
445         goto end;
446     }
447
448     if (!SSL_set_session(clientssl3, sess1)) {
449         printf("Unable to set session for third connection\n");
450         goto end;
451     }
452
453     /* This should fail because of the mismatched protocol versions */
454     if (create_ssl_connection(serverssl3, clientssl3)) {
455         printf("Unable to create third SSL connection\n");
456         goto end;
457     }
458
459
460     /* We should have automatically removed the session from the cache */
461     if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {
462         printf("Failed to call callback to remove session #2\n");
463         goto end;
464     }
465
466     if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {
467         /*
468          * Should have succeeded because it should not already be in the cache
469          */
470         printf("Unexpected failure adding session to cache #2\n");
471         goto end;
472     }
473 #endif
474
475     testresult = 1;
476
477  end:
478     SSL_free(serverssl1);
479     SSL_free(clientssl1);
480     SSL_free(serverssl2);
481     SSL_free(clientssl2);
482 #ifndef OPENSSL_NO_TLS1_1
483     SSL_free(serverssl3);
484     SSL_free(clientssl3);
485 #endif
486     SSL_SESSION_free(sess1);
487     SSL_SESSION_free(sess2);
488     /*
489      * Check if we need to remove any sessions up-refed for the external cache
490      */
491     if (new_called >= 1)
492         SSL_SESSION_free(sess1);
493     if (new_called >= 2)
494         SSL_SESSION_free(sess2);
495     SSL_CTX_free(sctx);
496     SSL_CTX_free(cctx);
497
498     return testresult;
499 }
500
501 static int test_session_with_only_int_cache(void)
502 {
503     SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
504
505     fixture.use_ext_cache = 0;
506
507     EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
508 }
509
510 static int test_session_with_only_ext_cache(void)
511 {
512     SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
513
514     fixture.use_int_cache = 0;
515
516     EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
517 }
518
519 static int test_session_with_both_cache(void)
520 {
521     SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
522
523     EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
524 }
525
526 #define USE_NULL    0
527 #define USE_BIO_1   1
528 #define USE_BIO_2   2
529
530 #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
531
532 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
533 {
534     switch (type) {
535     case USE_NULL:
536         *res = NULL;
537         break;
538     case USE_BIO_1:
539         *res = bio1;
540         break;
541     case USE_BIO_2:
542         *res = bio2;
543         break;
544     }
545 }
546
547 static int test_ssl_set_bio(int idx)
548 {
549     SSL_CTX *ctx = SSL_CTX_new(TLS_method());
550     BIO *bio1 = NULL;
551     BIO *bio2 = NULL;
552     BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
553     SSL *ssl = NULL;
554     int initrbio, initwbio, newrbio, newwbio;
555     int testresult = 0;
556
557     if (ctx == NULL) {
558         printf("Failed to allocate SSL_CTX\n");
559         goto end;
560     }
561
562     ssl = SSL_new(ctx);
563     if (ssl == NULL) {
564         printf("Failed to allocate SSL object\n");
565         goto end;
566     }
567
568     initrbio = idx % 3;
569     idx /= 3;
570     initwbio = idx % 3;
571     idx /= 3;
572     newrbio = idx % 3;
573     idx /= 3;
574     newwbio = idx;
575     OPENSSL_assert(newwbio <= 2);
576
577     if (initrbio == USE_BIO_1 || initwbio == USE_BIO_1 || newrbio == USE_BIO_1
578             || newwbio == USE_BIO_1) {
579         bio1 = BIO_new(BIO_s_mem());
580         if (bio1 == NULL) {
581             printf("Failed to allocate bio1\n");
582             goto end;
583         }
584     }
585
586     if (initrbio == USE_BIO_2 || initwbio == USE_BIO_2 || newrbio == USE_BIO_2
587             || newwbio == USE_BIO_2) {
588         bio2 = BIO_new(BIO_s_mem());
589         if (bio2 == NULL) {
590             printf("Failed to allocate bio2\n");
591             goto end;
592         }
593     }
594
595     setupbio(&irbio, bio1, bio2, initrbio);
596     setupbio(&iwbio, bio1, bio2, initwbio);
597
598     /*
599      * We want to maintain our own refs to these BIO, so do an up ref for each
600      * BIO that will have ownersip transferred in the SSL_set_bio() call
601      */
602     if (irbio != NULL)
603         BIO_up_ref(irbio);
604     if (iwbio != NULL && iwbio != irbio)
605         BIO_up_ref(iwbio);
606
607     SSL_set_bio(ssl, irbio, iwbio);
608
609     setupbio(&nrbio, bio1, bio2, newrbio);
610     setupbio(&nwbio, bio1, bio2, newwbio);
611
612     /*
613      * We will (maybe) transfer ownership again so do more up refs.
614      * SSL_set_bio() has some really complicated ownership rules where BIOs have
615      * already been set!
616      */
617     if (nrbio != NULL && nrbio != irbio && (nwbio != iwbio || nrbio != nwbio))
618         BIO_up_ref(nrbio);
619     if (nwbio != NULL && nwbio != nrbio && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
620         BIO_up_ref(nwbio);
621
622     SSL_set_bio(ssl, nrbio, nwbio);
623
624     testresult = 1;
625
626  end:
627     SSL_free(ssl);
628     BIO_free(bio1);
629     BIO_free(bio2);
630     /*
631      * This test is checking that the ref counting for SSL_set_bio is correct.
632      * If we get here and we did too many frees then we will fail in the above
633      * functions. If we haven't done enough then this will only be detected in
634      * a crypto-mdebug build
635      */
636     SSL_CTX_free(ctx);
637
638     return testresult;
639 }
640
641 typedef struct ssl_bio_test_fixture {
642     const char *test_case_name;
643     int pop_ssl;
644     enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } change_bio;
645 } SSL_BIO_TEST_FIXTURE;
646
647 static SSL_BIO_TEST_FIXTURE ssl_bio_set_up(const char *const test_case_name)
648 {
649     SSL_BIO_TEST_FIXTURE fixture;
650
651     fixture.test_case_name = test_case_name;
652     fixture.pop_ssl = 0;
653     fixture.change_bio = NO_BIO_CHANGE;
654
655     return fixture;
656 }
657
658 static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
659 {
660 }
661
662 static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix)
663 {
664     BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
665     SSL_CTX *ctx = SSL_CTX_new(TLS_method());
666     SSL *ssl = NULL;
667     int testresult = 0;
668
669     if (ctx == NULL) {
670         printf("Failed to allocate SSL_CTX\n");
671         return 0;
672     }
673
674     ssl = SSL_new(ctx);
675     if (ssl == NULL) {
676         printf("Failed to allocate SSL object\n");
677         goto end;
678     }
679
680     sslbio = BIO_new(BIO_f_ssl());
681     membio1 = BIO_new(BIO_s_mem());
682
683     if (sslbio == NULL || membio1 == NULL) {
684         printf("Malloc failure creating BIOs\n");
685         goto end;
686     }
687
688     BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
689
690     /*
691      * If anything goes wrong here then we could leak memory, so this will
692      * be caught in a crypto-mdebug build
693      */
694     BIO_push(sslbio, membio1);
695
696     /* Verify chaning the rbio/wbio directly does not cause leaks */
697     if (fix.change_bio != NO_BIO_CHANGE) {
698         membio2 = BIO_new(BIO_s_mem());
699         if (membio2 == NULL) {
700             printf("Malloc failure creating membio2\n");
701             goto end;
702         }
703         if (fix.change_bio == CHANGE_RBIO)
704             SSL_set0_rbio(ssl, membio2);
705         else
706             SSL_set0_wbio(ssl, membio2);
707     }
708     ssl = NULL;
709
710     if (fix.pop_ssl)
711         BIO_pop(sslbio);
712     else
713         BIO_pop(membio1);
714
715     testresult = 1;
716  end:
717     BIO_free(membio1);
718     BIO_free(sslbio);
719     SSL_free(ssl);
720     SSL_CTX_free(ctx);
721
722     return testresult;
723 }
724
725 static int test_ssl_bio_pop_next_bio(void)
726 {
727     SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
728
729     EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
730 }
731
732 static int test_ssl_bio_pop_ssl_bio(void)
733 {
734     SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
735
736     fixture.pop_ssl = 1;
737
738     EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
739 }
740
741 static int test_ssl_bio_change_rbio(void)
742 {
743     SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
744
745     fixture.change_bio = CHANGE_RBIO;
746
747     EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
748 }
749
750 static int test_ssl_bio_change_wbio(void)
751 {
752     SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
753
754     fixture.change_bio = CHANGE_WBIO;
755
756     EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
757 }
758
759 int main(int argc, char *argv[])
760 {
761     BIO *err = NULL;
762     int testresult = 1;
763
764     if (argc != 3) {
765         printf("Invalid argument count\n");
766         return 1;
767     }
768
769     cert = argv[1];
770     privkey = argv[2];
771
772     err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
773
774     CRYPTO_set_mem_debug(1);
775     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
776
777     ADD_TEST(test_tlsext_status_type);
778     ADD_TEST(test_session_with_only_int_cache);
779     ADD_TEST(test_session_with_only_ext_cache);
780     ADD_TEST(test_session_with_both_cache);
781     ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
782     ADD_TEST(test_ssl_bio_pop_next_bio);
783     ADD_TEST(test_ssl_bio_pop_ssl_bio);
784     ADD_TEST(test_ssl_bio_change_rbio);
785     ADD_TEST(test_ssl_bio_change_wbio);
786
787     testresult = run_tests(argv[0]);
788
789 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
790     if (CRYPTO_mem_leaks(err) <= 0)
791         testresult = 1;
792 #endif
793     BIO_free(err);
794
795     if (!testresult)
796         printf("PASS\n");
797
798     return testresult;
799 }