xref: /netbsd-src/crypto/external/bsd/openssl/dist/test/ssl_ctx_test.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
17d004720Schristos /*
2*b0d17251Schristos  * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
37d004720Schristos  *
4*b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
57d004720Schristos  * this file except in compliance with the License.  You can obtain a copy
67d004720Schristos  * in the file LICENSE in the source distribution or at
77d004720Schristos  * https://www.openssl.org/source/license.html
87d004720Schristos  */
97d004720Schristos 
107d004720Schristos #include "testutil.h"
117d004720Schristos #include <openssl/ssl.h>
127d004720Schristos 
137d004720Schristos typedef struct {
147d004720Schristos     int min_version;
157d004720Schristos     int max_version;
167d004720Schristos     int min_ok;
177d004720Schristos     int max_ok;
187d004720Schristos     int expected_min;
197d004720Schristos     int expected_max;
207d004720Schristos } version_test;
217d004720Schristos 
227d004720Schristos static const version_test version_testdata[] = {
237d004720Schristos     /* min           max             ok    expected min    expected max */
247d004720Schristos     {0,              0,              1, 1, 0,              0},
257d004720Schristos     {TLS1_VERSION,   TLS1_2_VERSION, 1, 1, TLS1_VERSION,   TLS1_2_VERSION},
267d004720Schristos     {TLS1_2_VERSION, TLS1_2_VERSION, 1, 1, TLS1_2_VERSION, TLS1_2_VERSION},
27*b0d17251Schristos     {TLS1_2_VERSION, TLS1_1_VERSION, 1, 1, TLS1_2_VERSION, TLS1_1_VERSION},
287d004720Schristos     {7,              42,             0, 0, 0,              0},
297d004720Schristos };
307d004720Schristos 
test_set_min_max_version(int idx_tst)317d004720Schristos static int test_set_min_max_version(int idx_tst)
327d004720Schristos {
337d004720Schristos     SSL_CTX *ctx = NULL;
347d004720Schristos     SSL *ssl = NULL;
357d004720Schristos     int testresult = 0;
367d004720Schristos     version_test t = version_testdata[idx_tst];
377d004720Schristos 
387d004720Schristos     ctx = SSL_CTX_new(TLS_server_method());
397d004720Schristos     if (ctx == NULL)
407d004720Schristos         goto end;
417d004720Schristos 
427d004720Schristos     ssl = SSL_new(ctx);
437d004720Schristos     if (ssl == NULL)
447d004720Schristos         goto end;
457d004720Schristos 
467d004720Schristos     if (!TEST_int_eq(SSL_CTX_set_min_proto_version(ctx, t.min_version), t.min_ok))
477d004720Schristos         goto end;
487d004720Schristos     if (!TEST_int_eq(SSL_CTX_set_max_proto_version(ctx, t.max_version), t.max_ok))
497d004720Schristos         goto end;
507d004720Schristos     if (!TEST_int_eq(SSL_CTX_get_min_proto_version(ctx), t.expected_min))
517d004720Schristos         goto end;
527d004720Schristos     if (!TEST_int_eq(SSL_CTX_get_max_proto_version(ctx), t.expected_max))
537d004720Schristos         goto end;
547d004720Schristos 
557d004720Schristos     if (!TEST_int_eq(SSL_set_min_proto_version(ssl, t.min_version), t.min_ok))
567d004720Schristos         goto end;
577d004720Schristos     if (!TEST_int_eq(SSL_set_max_proto_version(ssl, t.max_version), t.max_ok))
587d004720Schristos         goto end;
597d004720Schristos     if (!TEST_int_eq(SSL_get_min_proto_version(ssl), t.expected_min))
607d004720Schristos         goto end;
617d004720Schristos     if (!TEST_int_eq(SSL_get_max_proto_version(ssl), t.expected_max))
627d004720Schristos         goto end;
637d004720Schristos 
647d004720Schristos     testresult = 1;
657d004720Schristos 
667d004720Schristos   end:
677d004720Schristos     SSL_free(ssl);
687d004720Schristos     SSL_CTX_free(ctx);
697d004720Schristos     return testresult;
707d004720Schristos }
717d004720Schristos 
setup_tests(void)727d004720Schristos int setup_tests(void)
737d004720Schristos {
747d004720Schristos     ADD_ALL_TESTS(test_set_min_max_version, sizeof(version_testdata) / sizeof(version_test));
757d004720Schristos     return 1;
767d004720Schristos }
77