diff options
author | Jason Carey <jcarey@argv.me> | 2017-08-29 14:26:07 -0400 |
---|---|---|
committer | Jason Carey <jcarey@argv.me> | 2017-08-30 15:05:04 -0400 |
commit | 8ef456f89f63ab12941fe6b5352b20cff2522da3 (patch) | |
tree | 6c1e5d5bb2f71175297b69013dd00b199b9f053d /src/mongo/dbtests/jstests.cpp | |
parent | b39e21c774d0ff3afeac1730f08d8defed38044d (diff) | |
download | mongo-8ef456f89f63ab12941fe6b5352b20cff2522da3.tar.gz |
SERVER-30875 add requireOwnedObjects() to scoper3.4.8-rc1r3.4.8
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.
(cherry picked from commit 79b47945a6aae707d44e05669d991d86b157a14b)
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 13e71a0a74a..c0421910c2f 100644 --- a/src/mongo/dbtests/jstests.cpp +++ b/src/mongo/dbtests/jstests.cpp @@ -2388,6 +2388,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") {} @@ -2444,6 +2487,7 @@ public: add<RecursiveInvoke>(); add<ErrorCodeFromInvoke>(); + add<RequiresOwnedObjects>(); add<RoundTripTests::DBRefTest>(); add<RoundTripTests::DBPointerTest>(); |