summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/ipv6_connection_string_validation.js
blob: 9f544248ef1a1984dcc0e4491827c5bd70cc7ea5 (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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Test validation of IPv6 connection strings passed to the JavaScript "connect()" function.
// Related to SERVER-8030.

// This file runs in two modes: outer and inner.  This is to enable testing with --ipv6.
// The outer mode test starts a mongod with --ipv6 and then starts a mongo shell with --ipv6
// and a command line to run the test in inner_mode.  The inner mode test is the actual test.

if ("undefined" == typeof inner_mode) {
    // Start a mongod with --ipv6
    jsTest.log("Outer mode test starting mongod with --ipv6");
    // NOTE: bind_ip arg is present to test if it can parse ipv6 addresses (::1 in this case).
    // Unfortunately, having bind_ip = ::1 won't work in the test framework (But does work when
    // tested manually), so 127.0.0.1 is also present so the test mongo shell can connect
    // with that address.
    var mongod = MongoRunner.runMongod({ipv6: "", bind_ip: "::1,127.0.0.1"});
    var args = ["mongo",
                "--nodb",
                "--ipv6",
                "--host", "::1",
                "--port", mongod.port,
                "--eval", "inner_mode=true;port=" + mongod.port + ";",
                "jstests/noPassthroughWithMongod/ipv6_connection_string_validation.js" ];
    var exitCode = _runMongoProgram.apply(null, args);
    jsTest.log("Inner mode test finished, exit code was " + exitCode);

    // Stop the server we started
    jsTest.log("Outer mode test stopping server");
    MongoRunner.stopMongod(mongod.port, 15);

    // Pass the inner test's exit code back as the outer test's exit code
    quit(exitCode);
}

var goodStrings = [
        "localhost:27999/test",
        "[::1]:27999/test",
        "[0:0:0:0:0:0:0:1]:27999/test",
        "[0000:0000:0000:0000:0000:0000:0000:0001]:27999/test"
];

var badStrings = [
        { s: undefined,                 r: /^Missing connection string$/ },
        { s: 7,                         r: /^Incorrect type/ },
        { s: null,                      r: /^Incorrect type/ },
        { s: "",                        r: /^Empty connection string$/ },
        { s: "    ",                    r: /^Empty connection string$/ },
        { s: ":",                       r: /^Missing host name/ },
        { s: "/",                       r: /^Missing host name/ },
        { s: ":/",                      r: /^Missing host name/ },
        { s: ":/test",                  r: /^Missing host name/ },
        { s: ":27999/",                 r: /^Missing host name/ },
        { s: ":27999/test",             r: /^Missing host name/ },
        { s: "/test",                   r: /^Missing host name/ },
        { s: "localhost:/test",         r: /^Missing port number/ },
        { s: "::1:/test",               r: /^Missing port number/ },
        { s: "::1:cat/test",            r: /^Invalid port number/ },
        { s: "::1:1cat/test",           r: /^Invalid port number/ },
        { s: "::1:123456/test",         r: /^Invalid port number/ },
        { s: "::1:65536/test",          r: /^Invalid port number/ },
        { s: "127.0.0.1:65536/test",    r: /^Invalid port number/ },
        { s: "::1:27999/",              r: /^Missing database name/ },
        { s: "127.0.0.1:27999/",        r: /^Missing database name/ },
        { s: "::1:27999/test",          r: /^More than one ':'/ },
        { s: "0:0::0:0:1:27999/test",   r: /^More than one ':'/ },
        { s: "0000:0000:0000:0000:0000:0000:0000:0001:27999/test", r: /^More than one ':'/ },
        { s: "a[127.0.0.1]:27999/",     r: /^Missing database name/ },
        { s: "a[::1:]27999/",           r: /^Invalid port number/ },
        { s: "[::1:27999/",             r: /^Missing database name/ },
        { s: "[::1:]27999/",            r: /^Invalid port number/ },
        { s: "::1]:27999/",             r: /^Missing database name/ }
];

var substitutePort = function(connectionString) {
    // This will be called with non-strings as well as strings, so we need to catch exceptions
    try {
        return connectionString.replace("27999", "" + port);
    }
    catch (e) {
        return connectionString;
    }
};

var testGood = function(i, connectionString) {
    print("\n---\nTesting good connection string " + i + " (\"" + connectionString + "\") ...");
    var gotException = false;
    var exception;
    try {
        var connectDB = connect(connectionString);
        connectDB = null;
    }
    catch (e) {
        gotException = true;
        exception = e;
    }
    if (!gotException) {
        print("Good connection string " + i +
              " (\"" + connectionString + "\") correctly validated");
        return;
    }
    var message = "FAILED to correctly validate goodString " + i +
                  " (\"" + connectionString + "\"):  exception was \"" + tojson(exception) + "\"";
    doassert(message);
};

var testBad = function(i, connectionString, errorRegex) {
    print("\n---\nTesting bad connection string " + i + " (\"" + connectionString + "\") ...");
    var gotException = false;
    var gotCorrectErrorText = false;
    var exception;
    try {
        var connectDB = connect(connectionString);
        connectDB = null;
    }
    catch (e) {
        gotException = true;
        exception = e;
        if (errorRegex.test(e.message)) {
            gotCorrectErrorText = true;
        }
    }
    if (gotCorrectErrorText) {
        print("Bad connection string " + i + " (\"" + connectionString +
              "\") correctly rejected:\n" + tojson(exception));
        return;
    }
    var message = "FAILED to generate correct exception for badString " + i +
                  " (\"" + connectionString + "\"): ";
    if (gotException) {
        message += "exception was \"" + tojson(exception) +
                    "\", it should have matched \"" + errorRegex.toString() + "\"";
    }
    else {
        message += "no exception was thrown";
    }
    doassert(message);
};

var i;
jsTest.log("TESTING " + goodStrings.length + " good connection strings");
for (i = 0; i < goodStrings.length; ++i) {
    testGood(i, substitutePort(goodStrings[i]));
}

jsTest.log("TESTING " + badStrings.length + " bad connection strings");
for (i = 0; i < badStrings.length; ++i) {
    testBad(i, substitutePort(badStrings[i].s), badStrings[i].r);
}

jsTest.log("SUCCESSFUL test completion");