summaryrefslogtreecommitdiff
path: root/src/mongo
diff options
context:
space:
mode:
authorJonathan Reams <jbreams@mongodb.com>2016-06-28 15:39:08 -0400
committerJonathan Reams <jbreams@mongodb.com>2016-06-30 10:50:39 -0400
commit85e60b54d73f185113575bdcb82f691471e12478 (patch)
tree8080b9ba26a8bd579af02549382c42732ae400d3 /src/mongo
parent515b33fed131084cbef6da9be499642715bc12f1 (diff)
downloadmongo-85e60b54d73f185113575bdcb82f691471e12478.tar.gz
SERVER-23126 Check result of connect when constructing DBClient from MongoURI
Diffstat (limited to 'src/mongo')
-rw-r--r--src/mongo/client/SConscript2
-rw-r--r--src/mongo/client/mongo_uri_connect.cpp4
-rw-r--r--src/mongo/client/mongo_uri_test.cpp13
3 files changed, 18 insertions, 1 deletions
diff --git a/src/mongo/client/SConscript b/src/mongo/client/SConscript
index a01d60ffe87..78601d0b1b6 100644
--- a/src/mongo/client/SConscript
+++ b/src/mongo/client/SConscript
@@ -22,7 +22,7 @@ env.CppUnitTest(
'mongo_uri_test.cpp',
],
LIBDEPS=[
- 'connection_string',
+ 'clientdriver',
]
)
diff --git a/src/mongo/client/mongo_uri_connect.cpp b/src/mongo/client/mongo_uri_connect.cpp
index 7918cacb93f..d5b1bb0d0df 100644
--- a/src/mongo/client/mongo_uri_connect.cpp
+++ b/src/mongo/client/mongo_uri_connect.cpp
@@ -178,6 +178,10 @@ DBClientBase* MongoURI::connect(std::string& errmsg) const {
}
auto ret = _connectString.connect(errmsg, socketTimeout);
+ if (!ret) {
+ return ret;
+ }
+
if (!_user.empty()) {
ret->auth(_makeAuthObjFromOptions(ret->getMaxWireVersion()));
}
diff --git a/src/mongo/client/mongo_uri_test.cpp b/src/mongo/client/mongo_uri_test.cpp
index f67aad7b60f..71e43989a3f 100644
--- a/src/mongo/client/mongo_uri_test.cpp
+++ b/src/mongo/client/mongo_uri_test.cpp
@@ -308,4 +308,17 @@ TEST(MongoURI, InvalidURIs) {
}
}
+TEST(MongoURI, ValidButBadURIsFailToConnect) {
+ // "invalid" is a TLD that cannot exit on the public internet (see rfc2606). It should always
+ // parse as a valid URI, but connecting should always fail.
+ auto sw_uri = MongoURI::parse("mongodb://user:pass@hostname.invalid:12345");
+ ASSERT_OK(sw_uri.getStatus());
+ auto uri = sw_uri.getValue();
+ ASSERT_TRUE(uri.isValid());
+
+ std::string errmsg;
+ auto dbclient = uri.connect(errmsg);
+ ASSERT_EQ(dbclient, static_cast<decltype(dbclient)>(nullptr));
+}
+
} // namespace