File: test-mongocrypt-endpoint.c

package info (click to toggle)
libmongocrypt 1.17.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,572 kB
  • sloc: ansic: 70,067; python: 4,547; cpp: 615; sh: 460; makefile: 44; awk: 8
file content (125 lines) | stat: -rw-r--r-- 5,136 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright 2020-present MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <mongocrypt-endpoint-private.h>

#include "test-mongocrypt.h"

static void _test_mongocrypt_endpoint(_mongocrypt_tester_t *tester) {
    _mongocrypt_endpoint_t *endpoint;
    mongocrypt_status_t *status;
    _mongocrypt_endpoint_parse_opts_t opts;

    status = mongocrypt_status_new();

    endpoint = _mongocrypt_endpoint_new("https://kevin.keyvault.azure.net:443/some/path/?query=value",
                                        -1,
                                        NULL /* opts */,
                                        status);
    ASSERT_STREQUAL(endpoint->host, "kevin.keyvault.azure.net");
    ASSERT_STREQUAL(endpoint->domain, "keyvault.azure.net");
    ASSERT_STREQUAL(endpoint->subdomain, "kevin");
    ASSERT_STREQUAL(endpoint->protocol, "https");
    ASSERT_STREQUAL(endpoint->port, "443");
    ASSERT_STREQUAL(endpoint->path, "some/path");
    ASSERT_STREQUAL(endpoint->query, "query=value");
    BSON_ASSERT(mongocrypt_status_ok(status));
    _mongocrypt_endpoint_destroy(endpoint);

    endpoint = _mongocrypt_endpoint_new("kevin.keyvault.azure.net:443", -1, NULL /* opts */, status);
    ASSERT_STREQUAL(endpoint->host, "kevin.keyvault.azure.net");
    ASSERT_STREQUAL(endpoint->domain, "keyvault.azure.net");
    ASSERT_STREQUAL(endpoint->subdomain, "kevin");
    BSON_ASSERT(!endpoint->protocol);
    ASSERT_STREQUAL(endpoint->port, "443");
    BSON_ASSERT(!endpoint->path);
    BSON_ASSERT(!endpoint->query);
    BSON_ASSERT(mongocrypt_status_ok(status));
    _mongocrypt_endpoint_destroy(endpoint);

    endpoint = _mongocrypt_endpoint_new("kevin.keyvault.azure.net", -1, NULL /* opts */, status);
    ASSERT_STREQUAL(endpoint->host, "kevin.keyvault.azure.net");
    ASSERT_STREQUAL(endpoint->domain, "keyvault.azure.net");
    ASSERT_STREQUAL(endpoint->subdomain, "kevin");
    BSON_ASSERT(!endpoint->protocol);
    BSON_ASSERT(!endpoint->port);
    BSON_ASSERT(!endpoint->path);
    BSON_ASSERT(!endpoint->query);
    BSON_ASSERT(mongocrypt_status_ok(status));
    _mongocrypt_endpoint_destroy(endpoint);

    endpoint = _mongocrypt_endpoint_new("malformed", -1, NULL /* opts */, status);
    BSON_ASSERT(!endpoint);
    ASSERT_STATUS_CONTAINS(status, "Invalid endpoint, expected dot separator in host, but got: malformed");
    _mongocrypt_endpoint_destroy(endpoint);

    /* A host without a dot separator is valid if the "allow_empty_subdomain"
     * option is true. */
    memset(&opts, 0, sizeof(opts));
    opts.allow_empty_subdomain = true;
    endpoint = _mongocrypt_endpoint_new("localhost", -1, &opts, status);
    ASSERT_STREQUAL(endpoint->host, "localhost");
    ASSERT_STREQUAL(endpoint->domain, "localhost");
    BSON_ASSERT(!endpoint->subdomain);
    BSON_ASSERT(!endpoint->protocol);
    BSON_ASSERT(!endpoint->port);
    BSON_ASSERT(!endpoint->path);
    BSON_ASSERT(!endpoint->query);
    BSON_ASSERT(mongocrypt_status_ok(status));
    _mongocrypt_endpoint_destroy(endpoint);

    memset(&opts, 0, sizeof(opts));
    opts.allow_empty_subdomain = true;
    endpoint = _mongocrypt_endpoint_new("localhost:1234", -1, &opts, status);
    ASSERT_STREQUAL(endpoint->host, "localhost");
    ASSERT_STREQUAL(endpoint->domain, "localhost");
    BSON_ASSERT(!endpoint->subdomain);
    BSON_ASSERT(!endpoint->protocol);
    ASSERT_STREQUAL(endpoint->port, "1234");
    BSON_ASSERT(!endpoint->path);
    BSON_ASSERT(!endpoint->query);
    BSON_ASSERT(mongocrypt_status_ok(status));
    _mongocrypt_endpoint_destroy(endpoint);

    /* A host without a dot separator is invalid if the "allow_empty_subdomain"
     * option is false. */
    memset(&opts, 0, sizeof(opts));
    opts.allow_empty_subdomain = false;
    endpoint = _mongocrypt_endpoint_new("localhost", -1, &opts, status);
    ASSERT_STATUS_CONTAINS(status, "Invalid endpoint, expected dot separator in host, but got: localhost");
    BSON_ASSERT(!endpoint);

    mongocrypt_status_destroy(status);
}

static void _test_mongocrypt_apply_default_port(_mongocrypt_tester_t *tester) {
    char *endpoint;

    endpoint = bson_strdup("example.com");
    _mongocrypt_apply_default_port(&endpoint, "12");
    ASSERT_STREQUAL(endpoint, "example.com:12");
    bson_free(endpoint);

    endpoint = bson_strdup("example.com:34");
    _mongocrypt_apply_default_port(&endpoint, "12");
    ASSERT_STREQUAL(endpoint, "example.com:34");
    bson_free(endpoint);
}

void _mongocrypt_tester_install_endpoint(_mongocrypt_tester_t *tester) {
    INSTALL_TEST(_test_mongocrypt_endpoint);
    INSTALL_TEST(_test_mongocrypt_apply_default_port);
}