summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/port_options.js
blob: 8f4d4becc3e4b1af79af97a5f452edf61b494472 (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
// Check --port= edge behaviors.

(function() {
    'use strict';
    jsTest.log("Setting port=0 is okay unless binding to multiple IP interfaces.");

    function runTest(bindIP, expectOk) {
        jsTest.log("".concat("Testing with bindIP=[", bindIP, "], expectOk=[", expectOk, "]"));

        clearRawMongoProgramOutput();

        let pid = startMongoProgramNoConnect(
            "mongod", "--ipv6", "--dbpath", MongoRunner.dataDir, "--bind_ip", bindIP, "--port", 0);
        jsTest.log("".concat("pid=[", pid, "]"));

        if (expectOk) {
            let port;

            // We use assert.soonNoExcept() here because the mongod may not be logging yet.
            assert.soonNoExcept(() => {
                const logContents = rawMongoProgramOutput();
                const found = logContents.match(/waiting for connections on port (\d+)/);
                if (found !== null) {
                    print("Found message from mongod with port it is listening on: " + found[0]);
                    port = found[1];
                    return true;
                }
            });

            const connStr = `127.0.0.1:${port}`;
            print("Attempting to connect to " + connStr);

            let conn;
            assert.soonNoExcept(() => {
                conn = new Mongo(connStr);
                return true;
            });
            assert.commandWorked(conn.adminCommand({ping: 1}));

            stopMongoProgramByPid(pid);
        } else {
            const ec = waitProgram(pid);
            assert.eq(ec, MongoRunner.EXIT_NET_ERROR);
            assert.soonNoExcept(() => {
                const logContents = rawMongoProgramOutput();
                const found = logContents.match(
                    /Port 0 \(ephemeral port\) is not allowed when listening on multiple IP interfaces/);
                return (found !== null);
            }, "No warning issued for invalid port=0 usage");
        }
    }

    runTest("127.0.0.1", true);
    runTest("127.0.0.1,::1", false);
}());