diff options
author | Kyle Suarez <kyle.suarez@mongodb.com> | 2017-03-21 15:49:49 -0400 |
---|---|---|
committer | Kyle Suarez <kyle.suarez@mongodb.com> | 2017-03-21 16:02:49 -0400 |
commit | ba8c4a2cb599861bcd92446926b72bb17eb5df6b (patch) | |
tree | 5ff4826e0b5d581530579d93a416c6f365c9b2f8 /src/mongo/db/pipeline/document_source_project_test.cpp | |
parent | 19abe0c2dacef784aad78b89d6c6111109fbca88 (diff) | |
download | mongo-ba8c4a2cb599861bcd92446926b72bb17eb5df6b.tar.gz |
SERVER-8433 prohibit constructing FieldPaths that exceed the depth limit
Diffstat (limited to 'src/mongo/db/pipeline/document_source_project_test.cpp')
-rw-r--r-- | src/mongo/db/pipeline/document_source_project_test.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/mongo/db/pipeline/document_source_project_test.cpp b/src/mongo/db/pipeline/document_source_project_test.cpp index 60fe7a5988f..7512600ae73 100644 --- a/src/mongo/db/pipeline/document_source_project_test.cpp +++ b/src/mongo/db/pipeline/document_source_project_test.cpp @@ -30,6 +30,7 @@ #include <vector> +#include "mongo/bson/bson_depth.h" #include "mongo/bson/bsonelement.h" #include "mongo/bson/bsonmisc.h" #include "mongo/bson/bsonobj.h" @@ -261,5 +262,36 @@ TEST_F(ProjectStageTest, CanUseRemoveSystemVariableToConditionallyExcludeProject ASSERT(project->getNext().isEOF()); } +/** + * Creates BSON for a DocumentSourceProject that represents projecting a new computed field nested + * 'depth' levels deep. + */ +BSONObj makeProjectForNestedDocument(size_t depth) { + ASSERT_GTE(depth, 2U); + StringBuilder builder; + builder << "a"; + for (size_t i = 0; i < depth - 1; ++i) { + builder << ".a"; + } + return BSON(builder.str() << BSON("$literal" << 1)); +} + +TEST_F(ProjectStageTest, CanAddNestedDocumentExactlyAtDepthLimit) { + auto project = DocumentSourceProject::create( + makeProjectForNestedDocument(BSONDepth::getMaxAllowableDepth()), getExpCtx()); + auto mock = DocumentSourceMock::create(Document{{"_id", 1}}); + project->setSource(mock.get()); + + auto next = project->getNext(); + ASSERT_TRUE(next.isAdvanced()); +} + +TEST_F(ProjectStageTest, CannotAddNestedDocumentExceedingDepthLimit) { + ASSERT_THROWS_CODE( + DocumentSourceProject::create( + makeProjectForNestedDocument(BSONDepth::getMaxAllowableDepth() + 1), getExpCtx()), + UserException, + ErrorCodes::Overflow); +} } // namespace } // namespace mongo |