summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/replset_host_connection_validation.js
blob: 29ca7436b92078afb6f71c2a633cf246f20ec70e (plain)
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
// Test --host with a replica set.
// @tags: [requires_replication]

(function() {
'use strict';

const replSetName = 'hostTestReplSetName';

// This "inner_mode" method of spawning a replset and re-running was copied from
// host_connection_string_validation.js
if ("undefined" == typeof inner_mode) {
    jsTest.log("Outer mode test starting a replica set");

    const replTest = new ReplSetTest({name: replSetName, nodes: 2});
    replTest.startSet();
    replTest.initiate();

    const primary = replTest.getPrimary();

    const args = [
        "mongo",
        "--nodb",
        "--eval",
        "inner_mode=true;port=" + primary.port + ";",
        "jstests/noPassthroughWithMongod/replset_host_connection_validation.js"
    ];
    const exitCode = _runMongoProgram(...args);
    jsTest.log("Inner mode test finished, exit code was " + exitCode);

    replTest.stopSet();
    // Pass the inner test's exit code back as the outer test's exit code
    if (exitCode != 0) {
        doassert("inner test failed with exit code " + exitCode);
    }
    return;
}

function testHost(host, uri, ok) {
    const exitCode = runMongoProgram('mongo', '--eval', ';', '--host', host, uri);
    if (ok) {
        assert.eq(exitCode, 0, "failed to connect with `--host " + host + "`");
    } else {
        assert.neq(exitCode, 0, "unexpectedly succeeded to connect with `--host " + host + "`");
    }
}

function runConnectionStringTestFor(connectionString, uri, ok) {
    print("* Testing: --host " + connectionString + " " + uri);
    if (!ok) {
        print("  This should fail");
    }
    testHost(connectionString, uri, ok);
}

function expSuccess(str) {
    runConnectionStringTestFor(str, '', true);
    if (!str.startsWith('mongodb://')) {
        runConnectionStringTestFor(str, 'dbname', true);
    }
}

function expFailure(str) {
    runConnectionStringTestFor(str, '', false);
}

expSuccess(`localhost:${port}`);
expSuccess(`${replSetName}/localhost:${port}`);
expSuccess(`${replSetName}/localhost:${port},[::1]:${port}`);
expSuccess(`${replSetName}/localhost:${port},`);
expSuccess(`${replSetName}/localhost:${port},,`);
expSuccess(`mongodb://localhost:${port}/admin?replicaSet=${replSetName}`);
expSuccess(`mongodb://localhost:${port}`);

expFailure(',');
expFailure(',,');
expFailure(`${replSetName}/`);
expFailure(`${replSetName}/,`);
expFailure(`${replSetName}/,,`);
expFailure(`${replSetName}//not/a/socket`);
expFailure(`mongodb://localhost:${port}/admin?replicaSet=`);
expFailure('mongodb://localhost:');
expFailure(`mongodb://:${port}`);

jsTest.log("SUCCESSFUL test completion");
})();