xref: /freebsd/crypto/openssl/test/ssl_ctx_test.c (revision d37eb51047221dc3322b34db1038ff3aa533883f)
1 /*
2  * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 "testutil.h"
11 #include <openssl/ssl.h>
12 
13 typedef struct {
14     int min_version;
15     int max_version;
16     int min_ok;
17     int max_ok;
18     int expected_min;
19     int expected_max;
20 } version_test;
21 
22 static const version_test version_testdata[] = {
23     /* min           max             ok    expected min    expected max */
24     {0,              0,              1, 1, 0,              0},
25     {TLS1_VERSION,   TLS1_2_VERSION, 1, 1, TLS1_VERSION,   TLS1_2_VERSION},
26     {TLS1_2_VERSION, TLS1_2_VERSION, 1, 1, TLS1_2_VERSION, TLS1_2_VERSION},
27     {TLS1_2_VERSION, TLS1_1_VERSION, 1, 1, TLS1_2_VERSION, TLS1_1_VERSION},
28     {7,              42,             0, 0, 0,              0},
29 };
30 
31 static int test_set_min_max_version(int idx_tst)
32 {
33     SSL_CTX *ctx = NULL;
34     SSL *ssl = NULL;
35     int testresult = 0;
36     version_test t = version_testdata[idx_tst];
37 
38     ctx = SSL_CTX_new(TLS_server_method());
39     if (ctx == NULL)
40         goto end;
41 
42     ssl = SSL_new(ctx);
43     if (ssl == NULL)
44         goto end;
45 
46     if (!TEST_int_eq(SSL_CTX_set_min_proto_version(ctx, t.min_version), t.min_ok))
47         goto end;
48     if (!TEST_int_eq(SSL_CTX_set_max_proto_version(ctx, t.max_version), t.max_ok))
49         goto end;
50     if (!TEST_int_eq(SSL_CTX_get_min_proto_version(ctx), t.expected_min))
51         goto end;
52     if (!TEST_int_eq(SSL_CTX_get_max_proto_version(ctx), t.expected_max))
53         goto end;
54 
55     if (!TEST_int_eq(SSL_set_min_proto_version(ssl, t.min_version), t.min_ok))
56         goto end;
57     if (!TEST_int_eq(SSL_set_max_proto_version(ssl, t.max_version), t.max_ok))
58         goto end;
59     if (!TEST_int_eq(SSL_get_min_proto_version(ssl), t.expected_min))
60         goto end;
61     if (!TEST_int_eq(SSL_get_max_proto_version(ssl), t.expected_max))
62         goto end;
63 
64     testresult = 1;
65 
66   end:
67     SSL_free(ssl);
68     SSL_CTX_free(ctx);
69     return testresult;
70 }
71 
72 int setup_tests(void)
73 {
74     ADD_ALL_TESTS(test_set_min_max_version, sizeof(version_testdata) / sizeof(version_test));
75     return 1;
76 }
77