|
| 1 | +/* |
| 2 | + * Copyright 2021-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "TestSuite.h" |
| 18 | +#include "mongoc-config.h" |
| 19 | +#include "test-conveniences.h" |
| 20 | + |
| 21 | +#ifdef MONGOC_ENABLE_SSL |
| 22 | +#include "mongoc-ssl-private.h" |
| 23 | + |
| 24 | +typedef struct { |
| 25 | + const char *description; |
| 26 | + const char *bson; |
| 27 | + const char *expect_error; |
| 28 | + const char *expect_pem_file; |
| 29 | + const char *expect_pem_pwd; |
| 30 | + const char *expect_ca_file; |
| 31 | + bool expect_weak_cert_validation; |
| 32 | + bool expect_allow_invalid_hostname; |
| 33 | + bool expect_disable_ocsp_endpoint_check; |
| 34 | + bool expect_disable_certificate_revocation_check; |
| 35 | +} testcase_t; |
| 36 | + |
| 37 | +/* |
| 38 | +The following are the only valid options for _mongoc_ssl_opts_from_bson: |
| 39 | +
|
| 40 | +MONGOC_URI_TLSCERTIFICATEKEYFILE "tlscertificatekeyfile" |
| 41 | +MONGOC_URI_TLSCERTIFICATEKEYFILEPASSWORD "tlscertificatekeyfilepassword" |
| 42 | +MONGOC_URI_TLSCAFILE "tlscafile" |
| 43 | +MONGOC_URI_TLSALLOWINVALIDCERTIFICATES "tlsallowinvalidcertificates" |
| 44 | +MONGOC_URI_TLSALLOWINVALIDHOSTNAMES "tlsallowinvalidhostnames" |
| 45 | +MONGOC_URI_TLSINSECURE "tlsinsecure" |
| 46 | +MONGOC_URI_TLSDISABLECERTIFICATEREVOCATIONCHECK |
| 47 | +"tlsdisablecertificaterevocationcheck" |
| 48 | +MONGOC_URI_TLSDISABLEOCSPENDPOINTCHECK "tlsdisableocspendpointcheck" |
| 49 | +*/ |
| 50 | + |
| 51 | +static void |
| 52 | +test_mongoc_ssl_opts_from_bson (void) |
| 53 | +{ |
| 54 | + testcase_t tests[] = { |
| 55 | + { |
| 56 | + "test all options set", |
| 57 | + "{'tlsCertificateKeyFile': 'test_pem_file', " |
| 58 | + "'tlsCertificateKeyFilePassword': 'test_pem_pwd', 'tlsCAFile': " |
| 59 | + "'test_ca_file', 'tlsAllowInvalidCertificates': true, " |
| 60 | + "'tlsAllowInvalidHostnames': true, 'tlsInsecure': true, " |
| 61 | + "'tlsDisableCertificateRevocationCheck': true, " |
| 62 | + "'tlsDisableOCSPEndpointCheck': true }", |
| 63 | + NULL /* expect_error */, |
| 64 | + "test_pem_file" /* pem_file */, |
| 65 | + "test_pem_pwd" /* pem_pwd */, |
| 66 | + "test_ca_file" /* ca_file */, |
| 67 | + true /* weak_cert_validation */, |
| 68 | + true /* allow_invalid_hostname */, |
| 69 | + true /* disable_ocsp_endpoint_check */, |
| 70 | + true /* disable_certificate_revocation_check */ |
| 71 | + }, |
| 72 | + { |
| 73 | + "test options are case insentive", |
| 74 | + "{'tlscertificatekeyfile': 'test_pem_file', " |
| 75 | + "'tlscertificatekeyfilepassword': 'test_pem_pwd', 'tlscafile': " |
| 76 | + "'test_ca_file', 'tlsallowinvalidcertificates': true, " |
| 77 | + "'tlsallowinvalidhostnames': true, 'tlsinsecure': true, " |
| 78 | + "'tlsdisablecertificaterevocationcheck': true, " |
| 79 | + "'tlsdisableocspendpointcheck': true }", |
| 80 | + NULL /* expect_error */, |
| 81 | + "test_pem_file" /* pem_file */, |
| 82 | + "test_pem_pwd" /* pem_pwd */, |
| 83 | + "test_ca_file" /* ca_file */, |
| 84 | + true /* weak_cert_validation */, |
| 85 | + true /* allow_invalid_hostname */, |
| 86 | + true /* disable_ocsp_endpoint_check */, |
| 87 | + true /* disable_certificate_revocation_check */ |
| 88 | + }, |
| 89 | + { |
| 90 | + "test no options set", |
| 91 | + "{}", |
| 92 | + NULL /* expect_error */, |
| 93 | + NULL /* pem_file */, |
| 94 | + NULL /* pem_pwd */, |
| 95 | + NULL /* ca_file */, |
| 96 | + false /* weak_cert_validation */, |
| 97 | + false /* allow_invalid_hostname */, |
| 98 | + false /* disable_ocsp_endpoint_check */, |
| 99 | + false /* disable_certificate_revocation_check */ |
| 100 | + }, |
| 101 | + { |
| 102 | + "test tlsInsecure overrides tlsAllowInvalidHostnames and " |
| 103 | + "tlsAllowInvalidCertificates set", |
| 104 | + "{'tlsInsecure': true, 'tlsAllowInvalidHostnames': false, " |
| 105 | + "'tlsAllowInvalidCertificates': false}", |
| 106 | + NULL /* expect_error */, |
| 107 | + NULL /* pem_file */, |
| 108 | + NULL /* pem_pwd */, |
| 109 | + NULL /* ca_file */, |
| 110 | + true /* weak_cert_validation */, |
| 111 | + true /* allow_invalid_hostname */, |
| 112 | + false /* disable_ocsp_endpoint_check */, |
| 113 | + false /* disable_certificate_revocation_check */ |
| 114 | + }, |
| 115 | + { |
| 116 | + "test unrecognized option", |
| 117 | + "{'foo': true }", |
| 118 | + "unexpected BOOL option: foo" /* expect_error */, |
| 119 | + NULL /* pem_file */, |
| 120 | + NULL /* pem_pwd */, |
| 121 | + NULL /* ca_file */, |
| 122 | + false /* weak_cert_validation */, |
| 123 | + false /* allow_invalid_hostname */, |
| 124 | + false /* disable_ocsp_endpoint_check */, |
| 125 | + false /* disable_certificate_revocation_check */ |
| 126 | + }, |
| 127 | + { |
| 128 | + "test wrong value type", |
| 129 | + "{'tlsCaFile': true }", |
| 130 | + "unexpected BOOL option: tlsCaFile" /* expect_error */, |
| 131 | + NULL /* pem_file */, |
| 132 | + NULL /* pem_pwd */, |
| 133 | + NULL /* ca_file */, |
| 134 | + false /* weak_cert_validation */, |
| 135 | + false /* allow_invalid_hostname */, |
| 136 | + false /* disable_ocsp_endpoint_check */, |
| 137 | + false /* disable_certificate_revocation_check */ |
| 138 | + }, |
| 139 | + {0}}; |
| 140 | + testcase_t *test; |
| 141 | + |
| 142 | + for (test = tests; test->bson != NULL; test++) { |
| 143 | + mongoc_ssl_opt_t ssl_opt = {0}; |
| 144 | + bson_string_t *errmsg = bson_string_new (NULL); |
| 145 | + bool ok = |
| 146 | + _mongoc_ssl_opts_from_bson (&ssl_opt, tmp_bson (test->bson), errmsg); |
| 147 | + |
| 148 | + MONGOC_DEBUG ("testcase: %s", test->bson); |
| 149 | + if (test->expect_error) { |
| 150 | + ASSERT_CONTAINS (errmsg->str, test->expect_error); |
| 151 | + ASSERT (!ok); |
| 152 | + } else { |
| 153 | + if (!ok) { |
| 154 | + test_error ("unexpected error parsing: %s", errmsg->str); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + if (!test->expect_pem_file) { |
| 159 | + ASSERT (!ssl_opt.pem_file); |
| 160 | + } else { |
| 161 | + ASSERT (ssl_opt.pem_file); |
| 162 | + ASSERT_CMPSTR (test->expect_pem_file, ssl_opt.pem_file); |
| 163 | + } |
| 164 | + |
| 165 | + if (!test->expect_pem_pwd) { |
| 166 | + ASSERT (!ssl_opt.pem_pwd); |
| 167 | + } else { |
| 168 | + ASSERT (ssl_opt.pem_pwd); |
| 169 | + ASSERT_CMPSTR (test->expect_pem_pwd, ssl_opt.pem_pwd); |
| 170 | + } |
| 171 | + |
| 172 | + if (!test->expect_ca_file) { |
| 173 | + ASSERT (!ssl_opt.ca_file); |
| 174 | + } else { |
| 175 | + ASSERT (ssl_opt.ca_file); |
| 176 | + ASSERT_CMPSTR (test->expect_ca_file, ssl_opt.ca_file); |
| 177 | + } |
| 178 | + |
| 179 | + ASSERT (test->expect_weak_cert_validation == |
| 180 | + ssl_opt.weak_cert_validation); |
| 181 | + ASSERT (test->expect_allow_invalid_hostname == |
| 182 | + ssl_opt.allow_invalid_hostname); |
| 183 | + ASSERT (test->expect_disable_ocsp_endpoint_check == |
| 184 | + _mongoc_ssl_opts_disable_ocsp_endpoint_check (&ssl_opt)); |
| 185 | + ASSERT (test->expect_disable_certificate_revocation_check == |
| 186 | + _mongoc_ssl_opts_disable_certificate_revocation_check (&ssl_opt)); |
| 187 | + |
| 188 | + /* It is not possible to set ca_dir or crl_file. */ |
| 189 | + ASSERT (!ssl_opt.ca_dir); |
| 190 | + ASSERT (!ssl_opt.crl_file); |
| 191 | + |
| 192 | + _mongoc_ssl_opts_cleanup (&ssl_opt, true /* free_internal */); |
| 193 | + bson_string_free (errmsg, true /* free_segment */); |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +#endif /* MONGOC_ENABLE_SSL */ |
| 198 | + |
| 199 | +void |
| 200 | +test_ssl_install (TestSuite *suite) |
| 201 | +{ |
| 202 | +#ifdef MONGOC_ENABLE_SSL |
| 203 | + TestSuite_Add (suite, "/ssl_opt/from_bson", test_mongoc_ssl_opts_from_bson); |
| 204 | +#endif /* MONGOC_ENABLE_SSL */ |
| 205 | +} |
0 commit comments