diff options
author | Spencer Jackson <spencer.jackson@mongodb.com> | 2018-08-20 14:18:30 -0400 |
---|---|---|
committer | Spencer Jackson <spencer.jackson@mongodb.com> | 2018-09-17 17:21:40 -0400 |
commit | f99914d14b76718f1fef879cfaabe23c0c8f0857 (patch) | |
tree | e1da0b70c4d958cd59e671166bec0dc9ce9f3a57 /src/mongo/bson/bson_obj_test.cpp | |
parent | d246e38f3dad15b9919773ffe6a2fa59288034f2 (diff) | |
download | mongo-f99914d14b76718f1fef879cfaabe23c0c8f0857.tar.gz |
SERVER-36606: Allow construction of large BSON objects
Diffstat (limited to 'src/mongo/bson/bson_obj_test.cpp')
-rw-r--r-- | src/mongo/bson/bson_obj_test.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/mongo/bson/bson_obj_test.cpp b/src/mongo/bson/bson_obj_test.cpp index 82f95be072a..461e97eb636 100644 --- a/src/mongo/bson/bson_obj_test.cpp +++ b/src/mongo/bson/bson_obj_test.cpp @@ -675,4 +675,46 @@ TEST(BSONObj, addField) { ASSERT_BSONOBJ_EQ(obj, BSON("a" << 1 << "b" << 2)); } +TEST(BSONObj, 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 BSONObj constructor does not throw + // with standard size buffers. + auto normalBuffer = generateBuffer(15 * 1024 * 1024); + BSONObj obj(normalBuffer.data()); + } + + // Large buffers cause an exception to be thrown. + ASSERT_THROWS_CODE( + [&] { + auto largeBuffer = generateBuffer(17 * 1024 * 1024); + BSONObj obj(largeBuffer.data()); + }(), + 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{}); + } + + // But a size is in fact being enforced. + ASSERT_THROWS_CODE( + [&]() { + auto hugeBuffer = generateBuffer(70 * 1024 * 1024); + BSONObj obj(hugeBuffer.data(), BSONObj::LargeSizeTrait{}); + }(), + DBException, + ErrorCodes::BSONObjectTooLarge); +} + } // unnamed namespace |