diff options
author | Matt Cotter <matt.cotter@mongodb.com> | 2016-07-13 14:39:35 -0400 |
---|---|---|
committer | Matt Cotter <matt.cotter@mongodb.com> | 2016-08-18 14:13:01 -0400 |
commit | 88b540f0c14c7ab8af708aecc4cd6df83081b32e (patch) | |
tree | a6f01563ac2b8db7d34f98e5fab3d17f5416e315 /jstests | |
parent | ceeb17e4265c4c48765acbf9ad6a60b5f1889332 (diff) | |
download | mongo-88b540f0c14c7ab8af708aecc4cd6df83081b32e.tar.gz |
SERVER-22382 allow mongo --host to take uri
Diffstat (limited to 'jstests')
3 files changed, 227 insertions, 52 deletions
diff --git a/jstests/core/connection_string_validation.js b/jstests/core/connection_string_validation.js index 232650f230b..4f878593e6e 100644 --- a/jstests/core/connection_string_validation.js +++ b/jstests/core/connection_string_validation.js @@ -8,30 +8,42 @@ if (db.getMongo().host.indexOf(":") >= 0) { port = db.getMongo().host.substring(idx + 1); } -var goodStrings = ["localhost:" + port + "/test", "127.0.0.1:" + port + "/test"]; +var goodStrings = [ + "localhost:" + port + "/test", + "127.0.0.1:" + port + "/test", + "127.0.0.1:" + port + "/", +]; +var missingConnString = /^Missing connection string$/; +var incorrectType = /^Incorrect type/; +var emptyConnString = /^Empty connection string$/; +var badHost = /^Failed to parse mongodb/; +var emptyHost = /^Empty host component/; +var noPort = /^No digits/; +var badPort = /^Bad digit/; +var invalidPort = /^Port number \d+ out of range/; +var multipleColon = /^More than one ':' detected./; 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: ":" + port + "/", r: /^Missing host name/}, - {s: ":" + port + "/test", r: /^Missing host name/}, - {s: "/test", r: /^Missing host name/}, - {s: "localhost:/test", r: /^Missing port number/}, - {s: "127.0.0.1:/test", r: /^Missing port number/}, - {s: "127.0.0.1:cat/test", r: /^Invalid port number/}, - {s: "127.0.0.1:1cat/test", r: /^Invalid port number/}, - {s: "127.0.0.1:123456/test", r: /^Invalid port number/}, - {s: "127.0.0.1:65536/test", r: /^Invalid port number/}, - {s: "::1:65536/test", r: /^Invalid port number/}, - {s: "127.0.0.1:" + port + "/", r: /^Missing database name/}, - {s: "::1:" + port + "/", r: /^Missing database name/} + {s: undefined, r: missingConnString}, + {s: 7, r: incorrectType}, + {s: null, r: incorrectType}, + {s: "", r: emptyConnString}, + {s: " ", r: emptyConnString}, + {s: ":", r: emptyHost}, + {s: "/", r: badHost}, + {s: "/test", r: badHost}, + {s: ":/", r: emptyHost}, + {s: ":/test", r: emptyHost}, + {s: ":" + port + "/", r: emptyHost}, + {s: ":" + port + "/test", r: emptyHost}, + {s: "localhost:/test", r: noPort}, + {s: "127.0.0.1:/test", r: noPort}, + {s: "127.0.0.1:cat/test", r: badPort}, + {s: "127.0.0.1:1cat/test", r: badPort}, + {s: "127.0.0.1:123456/test", r: invalidPort}, + {s: "127.0.0.1:65536/test", r: invalidPort}, + {s: "::1:65536/test", r: multipleColon}, + {s: "::1:" + port + "/", r: multipleColon} ]; function testGood(i, connectionString) { @@ -55,6 +67,28 @@ function testGood(i, connectionString) { doassert(message); } +function testGoodAsURI(i, uri) { + uri = "mongodb://" + uri; + print("\nTesting good uri " + i + " (\"" + uri + "\") ..."); + var gotException = false; + var exception; + try { + var m_uri = MongoURI(uri); + var connectDB = connect(uri); + connectDB = null; + } catch (e) { + gotException = true; + exception = e; + } + if (!gotException) { + print("Good uri " + i + " (\"" + uri + "\") correctly validated"); + return; + } + var message = "FAILED to correctly validate goodString " + i + " (\"" + uri + + "\"): exception was \"" + tojson(exception) + "\""; + doassert(message); +} + function testBad(i, connectionString, errorRegex) { print("\nTesting bad connection string " + i + " (\"" + connectionString + "\") ..."); var gotException = false; @@ -90,6 +124,7 @@ var i; jsTest.log("TESTING " + goodStrings.length + " good connection strings"); for (i = 0; i < goodStrings.length; ++i) { testGood(i, goodStrings[i]); + testGoodAsURI(i, goodStrings[i]); } jsTest.log("TESTING " + badStrings.length + " bad connection strings"); diff --git a/jstests/noPassthroughWithMongod/host_connection_string_validation.js b/jstests/noPassthroughWithMongod/host_connection_string_validation.js new file mode 100644 index 00000000000..89d77e707f1 --- /dev/null +++ b/jstests/noPassthroughWithMongod/host_connection_string_validation.js @@ -0,0 +1,125 @@ +// Test --host. + +// This "inner_mode" method of spawning a mongod and re-running was copied from +// ipv6_connection_string_validation.js +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/host_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 testHost = function(host, shouldSucceed) { + var exitCode = runMongoProgram('mongo', '--ipv6', '--eval', ';', '--host', host); + if (shouldSucceed) { + if (exitCode !== 0) { + doassert("failed to connect with `--host " + host + + "`, but expected success. Exit code: " + exitCode); + } + } else { + if (exitCode === 0) { + doassert("successfully connected with `--host " + host + "`, but expected to fail."); + } + } +}; + +var goodStrings = [ + "[::1]:27999", + "[::1]:27999/test", + "localhost:27999", + "localhost:27999/test", + "127.0.0.1:27999", + "127.0.0.1:27999/test", + "[0:0:0:0:0:0:0:1]:27999", + "[0:0:0:0:0:0:0:1]:27999/test", + "[0000:0000:0000:0000:0000:0000:0000:0001]:27999", + "[0000:0000:0000:0000:0000:0000:0000:0001]:27999/test", +]; + +var goodSocketStrings = [ + "/tmp/mongodb-27999.sock", + "/tmp/mongodb-27999.sock/test", +]; + +var badStrings = [ + "/", + ":", + ":/", + "/test", + ":/test", + ":27999/", + ":27999/test", + "::1]:27999/", + "[::1:27999/", + "[::1]:/test", + "[::1:]27999/", + "a[::1:]27999/", + "::1:27999/test", + "::1:65536/test", + "[::1]:cat/test", + "[::1]:1cat/test", + "localhost:/test", + "127.0.0.1:/test", + "[::1]:65536/test", + "[::1]:123456/test", + "127.0.0.1:cat/test", + "a[127.0.0.1]:27999/", + "127.0.0.1:1cat/test", + "127.0.0.1:65536/test", + "0:0::0:0:1:27999/test", + "127.0.0.1:123456/test", + "0000:0000:0000:0000:0000:0000:0000:0001:27999/test", +]; + +function runUriTestFor(i, connectionString, isGood) { + connectionString = connectionString.replace("27999", "" + port); + print("Testing " + (isGood ? "good" : "bad") + " connection string " + i + "..."); + print(" * testing " + connectionString); + testHost(connectionString, isGood); + print(" * testing mongodb://" + connectionString); + testHost("mongodb://" + connectionString, isGood); +} + +var i; +jsTest.log("TESTING " + goodStrings.length + " good uri strings"); +for (i = 0; i < goodStrings.length; ++i) { + runUriTestFor(i, goodStrings[i], true); +} + +if (!_isWindows()) { + jsTest.log("TESTING " + goodSocketStrings.length + " good uri socket strings"); + for (i = 0; i < goodSocketStrings.length; ++i) { + runUriTestFor(i, goodSocketStrings[i], true); + } +} + +jsTest.log("TESTING " + badStrings.length + " bad uri strings"); +for (i = 0; i < badStrings.length; ++i) { + runUriTestFor(i, badStrings[i], false); +} + +jsTest.log("SUCCESSFUL test completion"); diff --git a/jstests/noPassthroughWithMongod/ipv6_connection_string_validation.js b/jstests/noPassthroughWithMongod/ipv6_connection_string_validation.js index e0a83397ee9..881c77c96d9 100644 --- a/jstests/noPassthroughWithMongod/ipv6_connection_string_validation.js +++ b/jstests/noPassthroughWithMongod/ipv6_connection_string_validation.js @@ -40,39 +40,54 @@ 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" + "[0000:0000:0000:0000:0000:0000:0000:0001]:27999/test", + "localhost:27999", + "[::1]:27999", + "[0:0:0:0:0:0:0:1]:27999", + "[0000:0000:0000:0000:0000:0000:0000:0001]:27999", ]; +var missingConnString = /^Missing connection string$/; +var incorrectType = /^Incorrect type/; +var emptyConnString = /^Empty connection string$/; +var badHost = /^Failed to parse mongodb/; +var emptyHost = /^Empty host component/; +var noPort = /^No digits/; +var badPort = /^Bad digit/; +var invalidPort = /^Port number \d+ out of range/; +var moreThanOneColon = /^More than one ':' detected/; +var charBeforeSquareBracket = /^'\[' present, but not first character/; +var noCloseBracket = /^ipv6 address is missing closing '\]'/; +var noOpenBracket = /^'\]' present without '\['/; +var noColonPrePort = /^missing colon after '\]' before the port/; 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/} + {s: undefined, r: missingConnString}, + {s: 7, r: incorrectType}, + {s: null, r: incorrectType}, + {s: "", r: emptyConnString}, + {s: " ", r: emptyConnString}, + {s: ":", r: emptyHost}, + {s: "/", r: badHost}, + {s: ":/", r: emptyHost}, + {s: ":/test", r: emptyHost}, + {s: ":27999/", r: emptyHost}, + {s: ":27999/test", r: emptyHost}, + {s: "/test", r: badHost}, + {s: "localhost:/test", r: noPort}, + {s: "[::1]:/test", r: noPort}, + {s: "[::1]:cat/test", r: badPort}, + {s: "[::1]:1cat/test", r: badPort}, + {s: "[::1]:123456/test", r: invalidPort}, + {s: "[::1]:65536/test", r: invalidPort}, + {s: "127.0.0.1:65536/test", r: invalidPort}, + {s: "::1:27999/test", r: moreThanOneColon}, + {s: "0:0::0:0:1:27999/test", r: moreThanOneColon}, + {s: "0000:0000:0000:0000:0000:0000:0000:0001:27999/test", r: moreThanOneColon}, + {s: "a[127.0.0.1]:27999/", r: charBeforeSquareBracket}, + {s: "a[::1:]27999/", r: charBeforeSquareBracket}, + {s: "[::1:27999/", r: noCloseBracket}, + {s: "[::1:]27999/", r: noColonPrePort}, + {s: "::1]:27999/", r: noOpenBracket}, ]; var substitutePort = function(connectionString) { |