diff options
author | Jason Carey <jcarey@argv.me> | 2017-08-29 14:26:07 -0400 |
---|---|---|
committer | Jason Carey <jcarey@argv.me> | 2017-08-30 14:48:37 -0400 |
commit | 79b47945a6aae707d44e05669d991d86b157a14b (patch) | |
tree | 3b5fde6583e50eb5a5ee1cce61b0a746bff811d0 /src/mongo/dbtests/jstests.cpp | |
parent | 697832f5474879c32713f78c5a9e27bbd2c5d19d (diff) | |
download | mongo-79b47945a6aae707d44e05669d991d86b157a14b.tar.gz |
SERVER-30875 add requireOwnedObjects() to scope
Add a flag to JS scopes that requires that bson objects bound to the
scope be owned. This should allow for more easy auditing of scopes that
don't explicitly manage lifetime with advanceGeneration.
Diffstat (limited to 'src/mongo/dbtests/jstests.cpp')
-rw-r--r-- | src/mongo/dbtests/jstests.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/mongo/dbtests/jstests.cpp b/src/mongo/dbtests/jstests.cpp index fa117f08b61..6b1d2d27dd1 100644 --- a/src/mongo/dbtests/jstests.cpp +++ b/src/mongo/dbtests/jstests.cpp @@ -2368,6 +2368,49 @@ public: } }; +class RequiresOwnedObjects { +public: + void run() { + char buf[] = {5, 0, 0, 0, 0}; + BSONObj unowned(buf); + BSONObj owned = unowned.getOwned(); + + ASSERT(!unowned.isOwned()); + ASSERT(owned.isOwned()); + + // Ensure that by default we can bind owned and unowned + { + unique_ptr<Scope> s(getGlobalScriptEngine()->newScope()); + s->setObject("unowned", unowned, true); + s->setObject("owned", owned, true); + } + + // After we set the flag, we should only be able to set owned + { + unique_ptr<Scope> s(getGlobalScriptEngine()->newScope()); + s->requireOwnedObjects(); + s->setObject("owned", owned, true); + + bool threwException = false; + try { + s->setObject("unowned", unowned, true); + } catch (...) { + threwException = true; + + auto status = exceptionToStatus(); + + ASSERT_EQUALS(status.code(), ErrorCodes::BadValue); + } + + ASSERT(threwException); + + // after resetting, we can set unowned's again + s->reset(); + s->setObject("unowned", unowned, true); + } + } +}; + class All : public Suite { public: All() : Suite("js") {} @@ -2424,6 +2467,7 @@ public: add<RecursiveInvoke>(); add<ErrorCodeFromInvoke>(); + add<RequiresOwnedObjects>(); add<RoundTripTests::DBRefTest>(); add<RoundTripTests::DBPointerTest>(); |