summaryrefslogtreecommitdiff
path: root/src/mongo/shell
diff options
context:
space:
mode:
authorA. Jesse Jiryu Davis <jesse@mongodb.com>2019-04-23 17:27:07 -0400
committerA. Jesse Jiryu Davis <jesse@mongodb.com>2019-05-14 15:50:55 -0400
commitad9267a722e21268d1005c1428ccad85d5a98946 (patch)
tree4af25a065685aef29a20106b3f0f9e05a83c19e8 /src/mongo/shell
parenta566943aac06895581c16530b953a5724f4177e0 (diff)
downloadmongo-ad9267a722e21268d1005c1428ccad85d5a98946.tar.gz
SERVER-4999 Normalize all hostnames to lowercase
Hostnames passed to replSetInitiate, replSetReconfig, addShard, etc. are all normalized by replacing ASCII uppercase characters with lowercase characters, consistent with how MongoDB drivers treat hostnames. The shell's getHostName() function now returns the hostname lowercased. Fixes undefined behavior in mongo::str::toLower().
Diffstat (limited to 'src/mongo/shell')
-rw-r--r--src/mongo/shell/replsettest.js7
-rw-r--r--src/mongo/shell/shell_utils_extended.cpp6
2 files changed, 10 insertions, 3 deletions
diff --git a/src/mongo/shell/replsettest.js b/src/mongo/shell/replsettest.js
index df1009e0d66..565fd3fb7b5 100644
--- a/src/mongo/shell/replsettest.js
+++ b/src/mongo/shell/replsettest.js
@@ -279,7 +279,8 @@ var ReplSetTest = function(opts) {
"/" + node.name);
}
- if (status.members[i].name == node.host || status.members[i].name == node.name) {
+ if (status.members[i].name.toLowerCase() == node.host.toLowerCase() ||
+ status.members[i].name.toLowerCase() == node.name.toLowerCase()) {
for (var j = 0; j < states.length; j++) {
if (printStatus) {
print("Status -- " + " current state: " + status.members[i][ind] +
@@ -2597,7 +2598,7 @@ var ReplSetTest = function(opts) {
print('Starting new replica set ' + self.name);
self.useHostName = opts.useHostName == undefined ? true : opts.useHostName;
- self.host = self.useHostName ? (opts.host || getHostName()) : 'localhost';
+ self.host = (self.useHostName ? (opts.host || getHostName()) : 'localhost').toLowerCase();
self.oplogSize = opts.oplogSize || 40;
self.useSeedList = opts.useSeedList || false;
self.keyFile = opts.keyFile;
@@ -2693,7 +2694,7 @@ var ReplSetTest = function(opts) {
self.ports = existingNodes.map(node => node.split(':')[1]);
self.nodes = existingNodes.map(node => new Mongo(node));
self.waitForKeys = false;
- self.host = existingNodes[0].split(':')[0];
+ self.host = existingNodes[0].split(':')[0].toLowerCase();
self.name = conf._id;
}
diff --git a/src/mongo/shell/shell_utils_extended.cpp b/src/mongo/shell/shell_utils_extended.cpp
index 2db4d7713a3..dadf2300eae 100644
--- a/src/mongo/shell/shell_utils_extended.cpp
+++ b/src/mongo/shell/shell_utils_extended.cpp
@@ -38,6 +38,7 @@
#include <boost/filesystem.hpp>
#include <fstream>
+#include <string>
#include "mongo/scripting/engine.h"
#include "mongo/shell/shell_utils.h"
@@ -330,11 +331,16 @@ BSONObj writeFile(const BSONObj& args, void* data) {
return undefinedReturn;
}
+// Return hostname normalized to lowercase for ease of use in tests.
BSONObj getHostName(const BSONObj& a, void* data) {
uassert(13411, "getHostName accepts no arguments", a.nFields() == 0);
char buf[260]; // HOST_NAME_MAX is usually 255
verify(gethostname(buf, 260) == 0);
buf[259] = '\0';
+ for (char* c = buf; *c; c++) {
+ *c = static_cast<char>(tolower(static_cast<unsigned char>(*c)));
+ }
+
return BSON("" << buf);
}