summaryrefslogtreecommitdiff
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:52:31 -0400
commitce097958162bfb977c39c07fc213f444b56244fa (patch)
tree4916402316962ffef36d183517b8b215cb477fd8
parente8dc6b98c1c91727f7def84f2fb4b57bf67ccc88 (diff)
downloadmongo-ce097958162bfb977c39c07fc213f444b56244fa.tar.gz
SERVER-23126 Check result of connect when constructing DBClient from MongoURI
(cherry picked from commit 85e60b54d73f185113575bdcb82f691471e12478)
-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 fabc96d0712..d112f34fe14 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 aaee6d2bcc2..ff9c048b610 100644
--- a/src/mongo/client/mongo_uri_connect.cpp
+++ b/src/mongo/client/mongo_uri_connect.cpp
@@ -163,6 +163,10 @@ BSONObj MongoURI::_makeAuthObjFromOptions(int maxWireVersion) const {
DBClientBase* MongoURI::connect(std::string& errmsg, double socketTimeout) 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 baa24dd2f57..89cf855ffed 100644
--- a/src/mongo/client/mongo_uri_test.cpp
+++ b/src/mongo/client/mongo_uri_test.cpp
@@ -311,4 +311,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