summaryrefslogtreecommitdiff
path: root/jstests/ns_length.js
diff options
context:
space:
mode:
Diffstat (limited to 'jstests/ns_length.js')
-rw-r--r--jstests/ns_length.js85
1 files changed, 85 insertions, 0 deletions
diff --git a/jstests/ns_length.js b/jstests/ns_length.js
new file mode 100644
index 00000000000..2e3fb02b0af
--- /dev/null
+++ b/jstests/ns_length.js
@@ -0,0 +1,85 @@
+// SERVER-7282 Faulty logic when testing maximum collection name length.
+
+// constants from server
+var maxNsLength = 127;
+var maxNsCollectionLength = 120;
+
+var myDb = db.getSiblingDB("ns_length");
+myDb.dropDatabase(); // start empty
+
+function mkStr(length) {
+ s = "";
+ while (s.length < length) {
+ s += "x";
+ }
+ return s;
+}
+
+function canMakeCollectionWithName(name) {
+ assert.eq(myDb.stats().fileSize, 0, "initial conditions");
+
+ myDb[name].insert({});
+ var success = myDb.getLastError() == null;
+ if (!success) {
+ assert.eq(myDb.stats().fileSize, 0, "no files should be created on error");
+ return false;
+ }
+
+ myDb.dropDatabase();
+ return true;
+}
+
+function canMakeIndexWithName(collection, name) {
+ var success = (collection.ensureIndex({x:1}, {name: name}) == undefined);
+ if (success) {
+ assert.commandWorked(collection.dropIndex(name));
+ }
+ return success;
+}
+
+function canRenameCollection(from, to) {
+ var success = myDb[from].renameCollection(to).ok;
+ if (success) {
+ // put it back
+ assert.commandWorked(myDb[to].renameCollection(from));
+ }
+ return success;
+}
+
+// test making collections around the name limit
+var prefixOverhead = (myDb.getName() + ".").length;
+var maxCollectionNameLength = maxNsCollectionLength - prefixOverhead;
+for (var i = maxCollectionNameLength - 3; i <= maxCollectionNameLength + 3; i++) {
+ assert.eq(canMakeCollectionWithName(mkStr(i)),
+ i <= maxCollectionNameLength,
+ "ns name length = " + (prefixOverhead + i));
+}
+
+// test making indexes around the name limit
+var collection = myDb.collection;
+collection.insert({});
+var maxIndexNameLength = maxNsLength - (collection.getFullName() + ".$").length;
+for (var i = maxIndexNameLength - 3; i <= maxIndexNameLength + 3; i++) {
+ assert.eq(canMakeIndexWithName(collection, mkStr(i)),
+ i <= maxIndexNameLength,
+ "index ns name length = " + ((collection.getFullName() + ".$").length + i));
+}
+
+// test renaming collections with the destination around the name limit
+myDb.from.insert({});
+for (var i = maxCollectionNameLength - 3; i <= maxCollectionNameLength + 3; i++) {
+ assert.eq(canRenameCollection("from", mkStr(i)),
+ i <= maxCollectionNameLength,
+ "new ns name length = " + (prefixOverhead + i));
+}
+
+// test renaming collections with the destination around the name limit due to long indexe names
+myDb.from.ensureIndex({a:1}, {name: mkStr(100)});
+var indexNsNameOverhead = (myDb.getName() + "..$").length + 100; // index ns name - collection name
+var maxCollectionNameWithIndex = maxNsLength - indexNsNameOverhead;
+for (var i = maxCollectionNameWithIndex - 3; i <= maxCollectionNameWithIndex + 3; i++) {
+ assert.eq(canRenameCollection("from", mkStr(i)),
+ i <= maxCollectionNameWithIndex,
+ "index ns name length = " + (indexNsNameOverhead + i));
+}
+