diff options
author | Tess Avitabile <tess.avitabile@mongodb.com> | 2016-08-22 14:49:31 -0400 |
---|---|---|
committer | Tess Avitabile <tess.avitabile@mongodb.com> | 2016-09-02 15:10:30 -0400 |
commit | f288ea86db58c6aefe4807ed7ac1815577da2752 (patch) | |
tree | dadff64105fe94dfeef57c00a38c1e4ff960ca07 /src/mongo/bson | |
parent | aa7411e6e2c4fd18bbff934c57567d89dc3efa0e (diff) | |
download | mongo-f288ea86db58c6aefe4807ed7ac1815577da2752.tar.gz |
SERVER-25159 Default BSON validation version should depend on admin.system.version
Diffstat (limited to 'src/mongo/bson')
-rw-r--r-- | src/mongo/bson/bson_validate.cpp | 7 | ||||
-rw-r--r-- | src/mongo/bson/bson_validate.h | 4 | ||||
-rw-r--r-- | src/mongo/bson/bson_validate_test.cpp | 54 | ||||
-rw-r--r-- | src/mongo/bson/bsonobj.cpp | 4 | ||||
-rw-r--r-- | src/mongo/bson/bsonobj.h | 7 | ||||
-rw-r--r-- | src/mongo/bson/bsontypes.cpp | 2 | ||||
-rw-r--r-- | src/mongo/bson/bsontypes.h | 6 |
7 files changed, 40 insertions, 44 deletions
diff --git a/src/mongo/bson/bson_validate.cpp b/src/mongo/bson/bson_validate.cpp index c578231e435..116dab6d12a 100644 --- a/src/mongo/bson/bson_validate.cpp +++ b/src/mongo/bson/bson_validate.cpp @@ -237,9 +237,10 @@ Status validateElementInfo(Buffer* buffer, return makeError("Invalid bson", idElem); return Status::OK(); } else { - return Status( - ErrorCodes::InvalidBSON, - "Attempt to use a decimal BSON type when support is not currently enabled."); + return Status(ErrorCodes::InvalidBSON, + "Cannot use decimal BSON type when the featureCompatibilityVersion " + "is 3.2. See " + "http://dochub.mongodb.org/core/3.4-feature-compatibility."); } case DBRef: diff --git a/src/mongo/bson/bson_validate.h b/src/mongo/bson/bson_validate.h index a40d9368ed1..e2aea3f47db 100644 --- a/src/mongo/bson/bson_validate.h +++ b/src/mongo/bson/bson_validate.h @@ -44,8 +44,6 @@ class Status; * this is NOT the bson size, but how far we know the buffer is valid * @param version - newest version to accept */ -Status validateBSON(const char* buf, - uint64_t maxLength, - BSONVersion version = BSONVersion::kLatest); +Status validateBSON(const char* buf, uint64_t maxLength, BSONVersion version); } // namespace mongo diff --git a/src/mongo/bson/bson_validate_test.cpp b/src/mongo/bson/bson_validate_test.cpp index 1d44b237d35..de4e55acb7b 100644 --- a/src/mongo/bson/bson_validate_test.cpp +++ b/src/mongo/bson/bson_validate_test.cpp @@ -52,10 +52,10 @@ void appendInvalidStringElement(const char* fieldName, BufBuilder* bb) { TEST(BSONValidate, Basic) { BSONObj x; - ASSERT_TRUE(x.valid()); + ASSERT_TRUE(x.valid(BSONVersion::kLatest)); x = BSON("x" << 1); - ASSERT_TRUE(x.valid()); + ASSERT_TRUE(x.valid(BSONVersion::kLatest)); } TEST(BSONValidate, RandomData) { @@ -81,12 +81,12 @@ TEST(BSONValidate, RandomData) { ASSERT_EQUALS(size, o.objsize()); - if (o.valid()) { + if (o.valid(BSONVersion::kLatest)) { numValid++; jsonSize += o.jsonString().size(); - ASSERT_OK(validateBSON(o.objdata(), o.objsize())); + ASSERT_OK(validateBSON(o.objdata(), o.objsize(), BSONVersion::kLatest)); } else { - ASSERT_NOT_OK(validateBSON(o.objdata(), o.objsize())); + ASSERT_NOT_OK(validateBSON(o.objdata(), o.objsize(), BSONVersion::kLatest)); } delete[] x; @@ -129,12 +129,12 @@ TEST(BSONValidate, MuckingData1) { data[i] = 200; numToRun++; - if (mine.valid()) { + if (mine.valid(BSONVersion::kLatest)) { numValid++; jsonSize += mine.jsonString().size(); - ASSERT_OK(validateBSON(mine.objdata(), mine.objsize())); + ASSERT_OK(validateBSON(mine.objdata(), mine.objsize(), BSONVersion::kLatest)); } else { - ASSERT_NOT_OK(validateBSON(mine.objdata(), mine.objsize())); + ASSERT_NOT_OK(validateBSON(mine.objdata(), mine.objsize(), BSONVersion::kLatest)); } } @@ -187,29 +187,31 @@ TEST(BSONValidate, Fuzz) { BSONObj fuzzed(buffer.get()); // Check that the two validation implementations agree (and neither crashes). - ASSERT_EQUALS(fuzzed.valid(), validateBSON(fuzzed.objdata(), fuzzed.objsize()).isOK()); + ASSERT_EQUALS( + fuzzed.valid(BSONVersion::kLatest), + validateBSON(fuzzed.objdata(), fuzzed.objsize(), BSONVersion::kLatest).isOK()); } } TEST(BSONValidateFast, Empty) { BSONObj x; - ASSERT_OK(validateBSON(x.objdata(), x.objsize())); + ASSERT_OK(validateBSON(x.objdata(), x.objsize(), BSONVersion::kLatest)); } TEST(BSONValidateFast, RegEx) { BSONObjBuilder b; b.appendRegex("foo", "i"); BSONObj x = b.obj(); - ASSERT_OK(validateBSON(x.objdata(), x.objsize())); + ASSERT_OK(validateBSON(x.objdata(), x.objsize(), BSONVersion::kLatest)); } TEST(BSONValidateFast, Simple0) { BSONObj x; - ASSERT_OK(validateBSON(x.objdata(), x.objsize())); + ASSERT_OK(validateBSON(x.objdata(), x.objsize(), BSONVersion::kLatest)); x = BSON("foo" << 17 << "bar" << "eliot"); - ASSERT_OK(validateBSON(x.objdata(), x.objsize())); + ASSERT_OK(validateBSON(x.objdata(), x.objsize(), BSONVersion::kLatest)); } TEST(BSONValidateFast, Simple2) { @@ -221,7 +223,7 @@ TEST(BSONValidateFast, Simple2) { sprintf(buf, "bar%d", i); b.appendMaxForType(buf, i); BSONObj x = b.obj(); - ASSERT_OK(validateBSON(x.objdata(), x.objsize())); + ASSERT_OK(validateBSON(x.objdata(), x.objsize(), BSONVersion::kLatest)); } } @@ -236,14 +238,14 @@ TEST(BSONValidateFast, Simple3) { b.appendMaxForType(buf, i); } BSONObj x = b.obj(); - ASSERT_OK(validateBSON(x.objdata(), x.objsize())); + ASSERT_OK(validateBSON(x.objdata(), x.objsize(), BSONVersion::kLatest)); } TEST(BSONValidateFast, NestedObject) { BSONObj x = BSON("a" << 1 << "b" << BSON("c" << 2 << "d" << BSONArrayBuilder().obj() << "e" << BSON_ARRAY("1" << 2 << 3))); - ASSERT_OK(validateBSON(x.objdata(), x.objsize())); - ASSERT_NOT_OK(validateBSON(x.objdata(), x.objsize() / 2)); + ASSERT_OK(validateBSON(x.objdata(), x.objsize(), BSONVersion::kLatest)); + ASSERT_NOT_OK(validateBSON(x.objdata(), x.objsize() / 2, BSONVersion::kLatest)); } TEST(BSONValidateFast, ErrorWithId) { @@ -252,7 +254,7 @@ TEST(BSONValidateFast, ErrorWithId) { ob.append("_id", 1); appendInvalidStringElement("not_id", &bb); const BSONObj x = ob.done(); - const Status status = validateBSON(x.objdata(), x.objsize()); + const Status status = validateBSON(x.objdata(), x.objsize(), BSONVersion::kLatest); ASSERT_NOT_OK(status); ASSERT_EQUALS(status.reason(), "not null terminated string in object with _id: 1"); } @@ -263,7 +265,7 @@ TEST(BSONValidateFast, ErrorBeforeId) { appendInvalidStringElement("not_id", &bb); ob.append("_id", 1); const BSONObj x = ob.done(); - const Status status = validateBSON(x.objdata(), x.objsize()); + const Status status = validateBSON(x.objdata(), x.objsize(), BSONVersion::kLatest); ASSERT_NOT_OK(status); ASSERT_EQUALS(status.reason(), "not null terminated string in object with unknown _id"); } @@ -273,7 +275,7 @@ TEST(BSONValidateFast, ErrorNoId) { BSONObjBuilder ob(bb); appendInvalidStringElement("not_id", &bb); const BSONObj x = ob.done(); - const Status status = validateBSON(x.objdata(), x.objsize()); + const Status status = validateBSON(x.objdata(), x.objsize(), BSONVersion::kLatest); ASSERT_NOT_OK(status); ASSERT_EQUALS(status.reason(), "not null terminated string in object with unknown _id"); } @@ -283,7 +285,7 @@ TEST(BSONValidateFast, ErrorIsInId) { BSONObjBuilder ob(bb); appendInvalidStringElement("_id", &bb); const BSONObj x = ob.done(); - const Status status = validateBSON(x.objdata(), x.objsize()); + const Status status = validateBSON(x.objdata(), x.objsize(), BSONVersion::kLatest); ASSERT_NOT_OK(status); ASSERT_EQUALS(status.reason(), "not null terminated string in object with unknown _id"); } @@ -296,7 +298,7 @@ TEST(BSONValidateFast, NonTopLevelId) { << "not the real _id")); appendInvalidStringElement("not_id2", &bb); const BSONObj x = ob.done(); - const Status status = validateBSON(x.objdata(), x.objsize()); + const Status status = validateBSON(x.objdata(), x.objsize(), BSONVersion::kLatest); ASSERT_NOT_OK(status); ASSERT_EQUALS(status.reason(), "not null terminated string in object with unknown _id"); } @@ -317,14 +319,14 @@ TEST(BSONValidateFast, StringHasSomething) { 4 // size , x.objsize()); - ASSERT_NOT_OK(validateBSON(x.objdata(), x.objsize())); + ASSERT_NOT_OK(validateBSON(x.objdata(), x.objsize(), BSONVersion::kLatest)); } TEST(BSONValidateBool, BoolValuesAreValidated) { BSONObjBuilder bob; bob.append("x", false); const BSONObj obj = bob.done(); - ASSERT_OK(validateBSON(obj.objdata(), obj.objsize())); + ASSERT_OK(validateBSON(obj.objdata(), obj.objsize(), BSONVersion::kLatest)); const BSONElement x = obj["x"]; // Legal, because we know that the BufBuilder gave // us back some heap memory, which isn't oringinally const. @@ -334,9 +336,9 @@ TEST(BSONValidateBool, BoolValuesAreValidated) { ++val) { *writable = static_cast<char>(val); if ((val == 0) || (val == 1)) { - ASSERT_OK(validateBSON(obj.objdata(), obj.objsize())); + ASSERT_OK(validateBSON(obj.objdata(), obj.objsize(), BSONVersion::kLatest)); } else { - ASSERT_NOT_OK(validateBSON(obj.objdata(), obj.objsize())); + ASSERT_NOT_OK(validateBSON(obj.objdata(), obj.objsize(), BSONVersion::kLatest)); } } } diff --git a/src/mongo/bson/bsonobj.cpp b/src/mongo/bson/bsonobj.cpp index 06d8a28c950..c3a561b0cdf 100644 --- a/src/mongo/bson/bsonobj.cpp +++ b/src/mongo/bson/bsonobj.cpp @@ -99,8 +99,8 @@ string BSONObj::jsonString(JsonStringFormat format, int pretty, bool isArray) co return s.str(); } -bool BSONObj::valid() const { - return validateBSON(objdata(), objsize()).isOK(); +bool BSONObj::valid(BSONVersion version) const { + return validateBSON(objdata(), objsize(), version).isOK(); } int BSONObj::woCompare(const BSONObj& r, diff --git a/src/mongo/bson/bsonobj.h b/src/mongo/bson/bsonobj.h index 77f13fd533b..a39bdfb64a6 100644 --- a/src/mongo/bson/bsonobj.h +++ b/src/mongo/bson/bsonobj.h @@ -532,8 +532,11 @@ public: passed object. */ BSONObj replaceFieldNames(const BSONObj& obj) const; - /** true unless corrupt */ - bool valid() const; + /** + * Returns true if this object is valid according to the specified BSON version, and returns + * false otherwise. + */ + bool valid(BSONVersion version) const; enum MatchType { Equality = 0, diff --git a/src/mongo/bson/bsontypes.cpp b/src/mongo/bson/bsontypes.cpp index 8ff370ec12f..b1f545ee180 100644 --- a/src/mongo/bson/bsontypes.cpp +++ b/src/mongo/bson/bsontypes.cpp @@ -34,8 +34,6 @@ namespace mongo { -bool enableBSON1_1 = true; - const char kMaxKeyData[] = {7, 0, 0, 0, static_cast<char>(MaxKey), 0, 0}; const BSONObj kMaxBSONKey(kMaxKeyData); diff --git a/src/mongo/bson/bsontypes.h b/src/mongo/bson/bsontypes.h index e3fde085ad9..ce3283ebb59 100644 --- a/src/mongo/bson/bsontypes.h +++ b/src/mongo/bson/bsontypes.h @@ -56,12 +56,6 @@ extern const BSONObj kMinBSONKey; enum class BSONVersion { kV1_0, kV1_1, kLatest = kV1_1 }; /** - Flag that determines whether we should accept decimal types in object validation, and default - to KeyString V1 indexes on non-MMAP storage engines. Set by enableBSON1_1 server parameter. -*/ -extern bool enableBSON1_1; - -/** the complete list of valid BSON types see also bsonspec.org */ |