xref: /openssl/test/quicapitest.c (revision 26dad42e)
1 /*
2  * Copyright 2022 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 <stdio.h>
11 #include <string.h>
12 
13 #include <openssl/opensslconf.h>
14 #include <openssl/quic.h>
15 
16 #include "helpers/ssltestlib.h"
17 #include "testutil.h"
18 #include "testutil/output.h"
19 
20 static OSSL_LIB_CTX *libctx = NULL;
21 static OSSL_PROVIDER *defctxnull = NULL;
22 
23 static int is_fips = 0;
24 
25 /*
26  * Test that we read what we've written.
27  */
test_quic_write_read(void)28 static int test_quic_write_read(void)
29 {
30     SSL_CTX *cctx = NULL, *sctx = NULL;
31     SSL *clientquic = NULL, *serverquic = NULL;
32     int j, ret = 0;
33     char buf[20];
34     static char *msg = "A test message";
35     size_t msglen = strlen(msg);
36     size_t numbytes = 0;
37 
38     if (!TEST_true(create_ssl_ctx_pair(libctx, OSSL_QUIC_server_method(),
39                                        OSSL_QUIC_client_method(),
40                                        0,
41                                        0,
42                                        &sctx, &cctx, NULL, NULL))
43             || !TEST_true(create_ssl_objects(sctx, cctx, &serverquic, &clientquic,
44                                              NULL, NULL))
45             || !TEST_true(create_bare_ssl_connection(serverquic, clientquic,
46                                                      SSL_ERROR_NONE, 0, 0)))
47         goto end;
48 
49     for (j = 0; j < 2; j++) {
50         /* Check that sending and receiving app data is ok */
51         if (!TEST_true(SSL_write_ex(clientquic, msg, msglen, &numbytes))
52                 || !TEST_true(SSL_read_ex(serverquic, buf, sizeof(buf),
53                                           &numbytes))
54                 || !TEST_mem_eq(buf, numbytes, msg, msglen))
55             goto end;
56 
57         if (!TEST_true(SSL_write_ex(serverquic, msg, msglen, &numbytes))
58                 || !TEST_true(SSL_read_ex(clientquic, buf, sizeof(buf),
59                                           &numbytes))
60                 || !TEST_mem_eq(buf, numbytes, msg, msglen))
61             goto end;
62     }
63 
64     ret = 1;
65 
66  end:
67     SSL_free(serverquic);
68     SSL_free(clientquic);
69     SSL_CTX_free(sctx);
70     SSL_CTX_free(cctx);
71 
72     return ret;
73 }
74 
75 OPT_TEST_DECLARE_USAGE("provider config\n")
76 
setup_tests(void)77 int setup_tests(void)
78 {
79     char *modulename;
80     char *configfile;
81 
82     libctx = OSSL_LIB_CTX_new();
83     if (!TEST_ptr(libctx))
84         return 0;
85 
86     defctxnull = OSSL_PROVIDER_load(NULL, "null");
87 
88     /*
89      * Verify that the default and fips providers in the default libctx are not
90      * available
91      */
92     if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
93             || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
94         return 0;
95 
96     if (!test_skip_common_options()) {
97         TEST_error("Error parsing test options\n");
98         return 0;
99     }
100 
101     if (!TEST_ptr(modulename = test_get_argument(0))
102             || !TEST_ptr(configfile = test_get_argument(1)))
103         return 0;
104 
105     if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile)))
106         return 0;
107 
108     /* Check we have the expected provider available */
109     if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename)))
110         return 0;
111 
112     /* Check the default provider is not available */
113     if (strcmp(modulename, "default") != 0
114             && !TEST_false(OSSL_PROVIDER_available(libctx, "default")))
115         return 0;
116 
117     if (strcmp(modulename, "fips") == 0)
118         is_fips = 1;
119 
120     ADD_TEST(test_quic_write_read);
121     return 1;
122 }
123 
cleanup_tests(void)124 void cleanup_tests(void)
125 {
126     OSSL_PROVIDER_unload(defctxnull);
127     OSSL_LIB_CTX_free(libctx);
128 }
129