diff options
Diffstat (limited to 'src/mongo/bson/bsonobjbuilder_test.cpp')
-rw-r--r-- | src/mongo/bson/bsonobjbuilder_test.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/mongo/bson/bsonobjbuilder_test.cpp b/src/mongo/bson/bsonobjbuilder_test.cpp index 6debaa2fa3a..39ce08cc588 100644 --- a/src/mongo/bson/bsonobjbuilder_test.cpp +++ b/src/mongo/bson/bsonobjbuilder_test.cpp @@ -446,5 +446,68 @@ TEST(BSONObjBuilderTest, SeedingBSONObjBuilderWithNonrootedUnownedBsonWorks) { ASSERT_NE(static_cast<const void*>(obj.objdata()), static_cast<const void*>(origObjPtr)); } +TEST(BSONObjBuilderTest, SizeChecks) { + auto generateBuffer = [](std::int32_t size) { + std::vector<char> buffer(size); + DataRange bufferRange(&buffer.front(), &buffer.back()); + ASSERT_OK(bufferRange.write(LittleEndian<int32_t>(size))); + + return buffer; + }; + + { + // Implicitly assert that BSONObjBuilder does not throw + // with standard size buffers. + auto normalBuffer = generateBuffer(15 * 1024 * 1024); + BSONObj obj(normalBuffer.data()); + + BSONObjBuilder builder; + builder.append("a", obj); + BSONObj finalObj = builder.obj(); + } + + // Large buffers cause an exception to be thrown. + ASSERT_THROWS_CODE( + [&] { + auto largeBuffer = generateBuffer(17 * 1024 * 1024); + BSONObj obj(largeBuffer.data(), BSONObj::LargeSizeTrait{}); + + BSONObjBuilder builder; + builder.append("a", obj); + BSONObj finalObj = builder.obj(); + }(), + DBException, + ErrorCodes::BSONObjectTooLarge); + + + // Assert that the max size can be increased by passing BSONObj a tag type. + { + auto largeBuffer = generateBuffer(17 * 1024 * 1024); + BSONObj obj(largeBuffer.data(), BSONObj::LargeSizeTrait{}); + + BSONObjBuilder builder; + builder.append("a", obj); + BSONObj finalObj = builder.obj<BSONObj::LargeSizeTrait>(); + } + + // But a size is in fact being enforced. + { + auto largeBuffer = generateBuffer(40 * 1024 * 1024); + BSONObj obj(largeBuffer.data(), BSONObj::LargeSizeTrait{}); + BSONObjBuilder builder; + ASSERT_THROWS( + [&]() { + + for (StringData character : {"a", "b", "c"}) { + builder.append(character, obj); + } + BSONObj finalObj = builder.obj<BSONObj::LargeSizeTrait>(); + + }(), + DBException); + } +} + + } // namespace } // namespace mongo |