diff options
author | Max Hirschhorn <max.hirschhorn@mongodb.com> | 2016-04-14 18:18:51 -0400 |
---|---|---|
committer | Max Hirschhorn <max.hirschhorn@mongodb.com> | 2016-04-14 18:18:51 -0400 |
commit | 60681f21a65226d5a38ac632b7ec28d081c2671c (patch) | |
tree | 552af2bd02bb6242bb709ed6dfa05f3139d9bfbb | |
parent | 0f1f80e2f7cf0ca1fbc9354b5fa2fad705207677 (diff) | |
download | mongo-60681f21a65226d5a38ac632b7ec28d081c2671c.tar.gz |
SERVER-23591 Assert on return code of mongo shell in js_protection*.js.
The rawMongoProgramOutput() function isn't guaranteed to return the full
output of the process, even after it has exited.
(cherry picked from commit 4566c8ebffb803d5175e7f1b7aa5baeffcba795e)
-rw-r--r-- | jstests/noPassthrough/js_protection.js | 84 | ||||
-rw-r--r-- | jstests/noPassthrough/js_protection_roundtrip.js | 48 |
2 files changed, 77 insertions, 55 deletions
diff --git a/jstests/noPassthrough/js_protection.js b/jstests/noPassthrough/js_protection.js index 243bd67342e..1299131289d 100644 --- a/jstests/noPassthrough/js_protection.js +++ b/jstests/noPassthrough/js_protection.js @@ -13,63 +13,79 @@ (function() { "use strict"; - var testServer = MongoRunner.runMongod({setParameter: 'javascriptProtection=true'}); + var testServer = MongoRunner.runMongod({setParameter: "javascriptProtection=true"}); + assert.neq( + null, testServer, "failed to start mongod with --setParameter=javascriptProtection=true"); + var db = testServer.getDB("test"); - var t = db.foo; - var funcToStore = function(x) { - return x + 1; - }; + var t = db.js_protection; function assertMongoClientCorrect() { - var mongo = runMongoProgram("mongo", - "--port", - testServer.port, - "--enableJavaScriptProtection", - "--eval", - // stored functions in objects - "var x = db.foo.findOne({'_id' : 0});" + - "assert.neq(typeof x.foo, 'function');" + - // retain db.loadServerScripts() functionality - "db.loadServerScripts();" + - "assert.eq(stored_func(4), 5);" + - - "print(\"completed gracefully\");"); - - var mongoOutput = rawMongoProgramOutput(); - assert(!mongoOutput.match(/assert failed/)); - assert(mongoOutput.match(/completed gracefully/)); + var functionToEval = function() { + var doc = db.js_protection.findOne({_id: 0}); + assert.neq(null, doc); + assert(doc.hasOwnProperty("myFunc")); + assert.neq("function", + typeof doc.myFunc, + "value of BSON type Code shouldn't have been eval()ed automatically"); + + assert.eq("undefined", typeof addOne, "addOne function has already been defined"); + db.loadServerScripts(); + assert.neq( + "undefined", typeof addOne, "addOne function should have been eval()ed locally"); + assert.eq(5, addOne(4)); + }; + + var exitCode = runMongoProgram("mongo", + "--port", + testServer.port, + "--enableJavaScriptProtection", + "--eval", + "(" + functionToEval.toString() + ")();"); + assert.eq(0, exitCode); } function assertNoStoredWhere() { - t.insertOne({name: 'testdoc', val: 0, y: 0}); - t.update({$where: "stored_func(this.val) == 1"}, {$set: {y: 100}}, false, true); + t.insertOne({name: "testdoc", val: 0, y: 0}); + + var res = t.update({$where: "addOne(this.val) === 1"}, {$set: {y: 100}}, false, true); + assert.writeError(res); - var x = t.findOne({name: 'testdoc'}); - assert.eq(x.y, 0); + var doc = t.findOne({name: "testdoc"}); + assert.neq(null, doc); + assert.eq(0, doc.y, tojson(doc)); - t.update( + res = t.update( { $where: function() { - return this.val == 0; + return this.val === 0; } }, {$set: {y: 100}}, false, true); + assert.writeOK(res); - x = t.findOne({name: 'testdoc'}); - assert.eq(x.y, 100); + doc = t.findOne({name: "testdoc"}); + assert.neq(null, doc); + assert.eq(100, doc.y, tojson(doc)); } /** * ACTUAL TEST */ - db.system.js.save({_id: "stored_func", value: funcToStore}); + db.system.js.insertOne({ + _id: "addOne", + value: function(x) { + return x + 1; + } + }); + t.insertOne({ - '_id': 0, - 'myFunc': function() { - return 'tesval'; + _id: 0, + myFunc: function() { + return "testval"; } }); diff --git a/jstests/noPassthrough/js_protection_roundtrip.js b/jstests/noPassthrough/js_protection_roundtrip.js index b85e8cfac43..2fa42ad8430 100644 --- a/jstests/noPassthrough/js_protection_roundtrip.js +++ b/jstests/noPassthrough/js_protection_roundtrip.js @@ -10,22 +10,27 @@ (function() { "use strict"; - var testServer = MongoRunner.runMongod({setParameter: 'javascriptProtection=true'}), - db = testServer.getDB("test"), t = db.foo, x; + var testServer = MongoRunner.runMongod({setParameter: "javascriptProtection=true"}); + assert.neq( + null, testServer, "failed to start mongod with --setParameter=javascriptProtection=true"); + + var db = testServer.getDB("test"); + var t = db.js_protection_roundtrip; function makeRoundTrip() { - var mongo = runMongoProgram("mongo", - "--port", - testServer.port, - "--enableJavaScriptProtection", - "--eval", - "var x = db.foo.findOne({'_id' : 0});" + - "db.foo.insertOne({'_id': 1, myFunc: x.myFunc});" + - "print(\"completed gracefully\");"); - - var mongoOutput = rawMongoProgramOutput(); - assert(!mongoOutput.match(/assert failed/)); - assert(mongoOutput.match(/completed gracefully/)); + var functionToEval = function() { + var doc = db.js_protection_roundtrip.findOne({_id: 0}); + assert.neq(null, doc); + db.js_protection_roundtrip.insertOne({_id: 1, myFunc: doc.myFunc}); + }; + + var exitCode = runMongoProgram("mongo", + "--port", + testServer.port, + "--enableJavaScriptProtection", + "--eval", + "(" + functionToEval.toString() + ")();"); + assert.eq(0, exitCode); } /** @@ -33,19 +38,20 @@ */ t.insertOne({ - '_id': 0, - 'myFunc': function() { - return 'yes'; + _id: 0, + myFunc: function() { + return "yes"; } }); makeRoundTrip(); - x = t.findOne({'_id': 1}); + var doc = t.findOne({_id: 1}); + assert.neq(null, doc, "failed to find document inserted by other mongo shell"); - if (!x.myFunc() == 'yes') { - assert(0); - } + assert(doc.hasOwnProperty("myFunc"), tojson(doc)); + assert.eq("function", typeof doc.myFunc, tojson(doc)); + assert.eq("yes", doc.myFunc(), tojson(doc)); MongoRunner.stopMongod(testServer); })(); |