Update sslapitest to use the test framework
[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 <openssl/opensslconf.h>
11 #include <openssl/bio.h>
12 #include <openssl/crypto.h>
13 #include <openssl/ssl.h>
14
15 #include "ssltestlib.h"
16 #include "testutil.h"
17
18 static char *cert = NULL;
19 static char *privkey = NULL;
20
21 static int test_tlsext_status_type(void)
22 {
23     SSL_CTX *ctx = NULL;
24     SSL *con = NULL;
25     int testresult = 0;
26
27     /* Test tlsext_status_type */
28     ctx = SSL_CTX_new(TLS_method());
29
30     if (SSL_CTX_get_tlsext_status_type(ctx) != -1) {
31         printf("Unexpected initial value for "
32                "SSL_CTX_get_tlsext_status_type()\n");
33         goto end;
34     }
35
36     con = SSL_new(ctx);
37
38     if (SSL_get_tlsext_status_type(con) != -1) {
39         printf("Unexpected initial value for SSL_get_tlsext_status_type()\n");
40         goto end;
41     }
42
43     if (!SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp)) {
44         printf("Unexpected fail for SSL_set_tlsext_status_type()\n");
45         goto end;
46     }
47
48     if (SSL_get_tlsext_status_type(con) != TLSEXT_STATUSTYPE_ocsp) {
49         printf("Unexpected result for SSL_get_tlsext_status_type()\n");
50         goto end;
51     }
52
53     SSL_free(con);
54     con = NULL;
55
56     if (!SSL_CTX_set_tlsext_status_type(ctx, TLSEXT_STATUSTYPE_ocsp)) {
57         printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n");
58         goto end;
59     }
60
61     if (SSL_CTX_get_tlsext_status_type(ctx) != TLSEXT_STATUSTYPE_ocsp) {
62         printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n");
63         goto end;
64     }
65
66     con = SSL_new(ctx);
67
68     if (SSL_get_tlsext_status_type(con) != TLSEXT_STATUSTYPE_ocsp) {
69         printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n");
70         goto end;
71     }
72
73     testresult = 1;
74
75  end:
76     SSL_free(con);
77     SSL_CTX_free(ctx);
78
79     return testresult;
80 }
81
82 static int test_session(void)
83 {
84     SSL_CTX *sctx = NULL, *cctx = NULL;
85     SSL *serverssl1 = NULL, *clientssl1 = NULL;
86     SSL *serverssl2 = NULL, *clientssl2 = NULL;
87     SSL_SESSION *sess1 = NULL, *sess2 = NULL;
88     int testresult = 0;
89
90     if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
91                              &cctx, cert, privkey)) {
92         printf("Unable to create SSL_CTX pair\n");
93         return 0;
94     }
95
96     /* Turn on client session cache */
97     SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
98
99     if (!create_ssl_connection(sctx, cctx, &serverssl1, &clientssl1, NULL,
100                                NULL)) {
101         printf("Unable to create SSL connection\n");
102         goto end;
103     }
104
105     sess1 = SSL_get1_session(clientssl1);
106     if (sess1 == NULL) {
107         printf("Unexpected NULL session\n");
108         goto end;
109     }
110
111     if (SSL_CTX_add_session(cctx, sess1)) {
112         /* Should have failed because it should already be in the cache */
113         printf("Unexpected success adding session to cache\n");
114         goto end;
115     }
116
117     if (!create_ssl_connection(sctx, cctx, &serverssl2, &clientssl2, NULL,
118                                NULL)) {
119         printf("Unable to create second SSL connection\n");
120         goto end;
121     }
122
123     sess2 = SSL_get1_session(clientssl2);
124     if (sess2 == NULL) {
125         printf("Unexpected NULL session from clientssl2\n");
126         goto end;
127     }
128
129     /*
130      * This should clear sess2 from the cache because it is a "bad" session. See
131      * SSL_set_session() documentation.
132      */
133     if (!SSL_set_session(clientssl2, sess1)) {
134         printf("Unexpected failure setting session\n");
135         goto end;
136     }
137
138     if (SSL_get_session(clientssl2) != sess1) {
139         printf("Unexpected session found\n");
140         goto end;
141     }
142
143     if (!SSL_CTX_add_session(cctx, sess2)) {
144         /*
145          * Should have succeeded because it should not already be in the cache
146          */
147         printf("Unexpected failure adding session to cache\n");
148         goto end;
149     }
150
151     if (!SSL_CTX_remove_session(cctx, sess2)) {
152         printf("Unexpected failure removing session from cache\n");
153         goto end;
154     }
155
156     if (SSL_CTX_remove_session(cctx, sess2)) {
157         printf("Unexpected success removing session from cache\n");
158         goto end;
159     }
160
161     testresult = 1;
162  end:
163     SSL_free(serverssl1);
164     SSL_free(clientssl1);
165     SSL_free(serverssl2);
166     SSL_free(clientssl2);
167     SSL_SESSION_free(sess1);
168     SSL_SESSION_free(sess2);
169     SSL_CTX_free(sctx);
170     SSL_CTX_free(cctx);
171
172     return testresult;
173 }
174
175 int main(int argc, char *argv[])
176 {
177     BIO *err = NULL;
178     int testresult = 1;
179
180     if (argc != 3) {
181         printf("Invalid argument count\n");
182         return 1;
183     }
184
185     cert = argv[1];
186     privkey = argv[2];
187
188     err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
189
190     CRYPTO_set_mem_debug(1);
191     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
192
193     ADD_TEST(test_tlsext_status_type);
194     ADD_TEST(test_session);
195
196     testresult = run_tests(argv[0]);
197
198 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
199     if (CRYPTO_mem_leaks(err) <= 0)
200         testresult = 1;
201 #endif
202     BIO_free(err);
203
204     if (!testresult)
205         printf("PASS\n");
206
207     return testresult;
208 }