summaryrefslogtreecommitdiff
path: root/src/mongo/bson/bson_validate_test.cpp
diff options
context:
space:
mode:
authorMathias Stearn <mathias@10gen.com>2013-12-24 15:27:14 -0500
committerMathias Stearn <mathias@10gen.com>2014-01-03 12:50:26 -0500
commitab48b396e5bc48cb27ce3bc2dbd28bf3b6a96851 (patch)
tree309ae1ffe7b09e05f4a594d97cee9a599755c092 /src/mongo/bson/bson_validate_test.cpp
parent04221630900335fc6ee0d9922edae9d29f02d66d (diff)
downloadmongo-ab48b396e5bc48cb27ce3bc2dbd28bf3b6a96851.tar.gz
SERVER-11903 Remove BSONElement::validate()
It was mostly used for improving the error messages about BSON that already failed validation, but it did not cover all validation cases. To replace this use case validateBSON was enhanced to include the _id of the invalid object in the error message.
Diffstat (limited to 'src/mongo/bson/bson_validate_test.cpp')
-rw-r--r--src/mongo/bson/bson_validate_test.cpp60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/mongo/bson/bson_validate_test.cpp b/src/mongo/bson/bson_validate_test.cpp
index 61e67f33c46..2d381e31183 100644
--- a/src/mongo/bson/bson_validate_test.cpp
+++ b/src/mongo/bson/bson_validate_test.cpp
@@ -22,6 +22,14 @@ namespace {
using namespace mongo;
+ void appendInvalidStringElement(const char* fieldName, BufBuilder* bb) {
+ // like a BSONObj string, but without a NUL terminator.
+ bb->appendChar(String);
+ bb->appendStr("name", /*withNUL*/true);
+ bb->appendNum(4);
+ bb->appendStr("asdf", /*withNUL*/false);
+ }
+
TEST(BSONValidate, Basic) {
BSONObj x;
ASSERT_TRUE( x.valid() );
@@ -213,4 +221,56 @@ namespace {
ASSERT_NOT_OK(validateBSON(x.objdata(), x.objsize() / 2));
}
+ TEST(BSONValidateFast, ErrorWithId) {
+ BufBuilder bb;
+ BSONObjBuilder ob(bb);
+ ob.append("_id", 1);
+ appendInvalidStringElement("not_id", &bb);
+ const BSONObj x = ob.done();
+ const Status status = validateBSON(x.objdata(), x.objsize());
+ ASSERT_NOT_OK(status);
+ ASSERT_EQUALS(status.reason(), "not null terminated string in object with _id: 1");
+ }
+
+ TEST(BSONValidateFast, ErrorBeforeId) {
+ BufBuilder bb;
+ BSONObjBuilder ob(bb);
+ appendInvalidStringElement("not_id", &bb);
+ ob.append("_id", 1);
+ const BSONObj x = ob.done();
+ const Status status = validateBSON(x.objdata(), x.objsize());
+ ASSERT_NOT_OK(status);
+ ASSERT_EQUALS(status.reason(), "not null terminated string in object with unknown _id");
+ }
+
+ TEST(BSONValidateFast, ErrorNoId) {
+ BufBuilder bb;
+ BSONObjBuilder ob(bb);
+ appendInvalidStringElement("not_id", &bb);
+ const BSONObj x = ob.done();
+ const Status status = validateBSON(x.objdata(), x.objsize());
+ ASSERT_NOT_OK(status);
+ ASSERT_EQUALS(status.reason(), "not null terminated string in object with unknown _id");
+ }
+
+ TEST(BSONValidateFast, ErrorIsInId) {
+ BufBuilder bb;
+ BSONObjBuilder ob(bb);
+ appendInvalidStringElement("_id", &bb);
+ const BSONObj x = ob.done();
+ const Status status = validateBSON(x.objdata(), x.objsize());
+ ASSERT_NOT_OK(status);
+ ASSERT_EQUALS(status.reason(), "not null terminated string in object with unknown _id");
+ }
+
+ TEST(BSONValidateFast, NonTopLevelId) {
+ BufBuilder bb;
+ BSONObjBuilder ob(bb);
+ ob.append("not_id1", BSON("_id" << "not the real _id"));
+ appendInvalidStringElement("not_id2", &bb);
+ const BSONObj x = ob.done();
+ const Status status = validateBSON(x.objdata(), x.objsize());
+ ASSERT_NOT_OK(status);
+ ASSERT_EQUALS(status.reason(), "not null terminated string in object with unknown _id");
+ }
}