summaryrefslogtreecommitdiff
path: root/src/mongo/idl/idl_test.cpp
diff options
context:
space:
mode:
authorMark Benvenuto <mark.benvenuto@mongodb.com>2018-08-29 14:29:42 -0400
committerMark Benvenuto <mark.benvenuto@mongodb.com>2018-08-29 17:17:45 -0400
commit1e83ae5db6a9be04fa714d57b2068f79f519af32 (patch)
tree0eaa0963710ffe4afbe2132c6a9f4612df720a1e /src/mongo/idl/idl_test.cpp
parent57b4b2216ffc7986440b87f9659e970e061b9f33 (diff)
downloadmongo-1e83ae5db6a9be04fa714d57b2068f79f519af32.tar.gz
SERVER-35247 Undefined is allowed for required fields in IDL
Diffstat (limited to 'src/mongo/idl/idl_test.cpp')
-rw-r--r--src/mongo/idl/idl_test.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/mongo/idl/idl_test.cpp b/src/mongo/idl/idl_test.cpp
index 8818132c65e..ed5ea3dc6ab 100644
--- a/src/mongo/idl/idl_test.cpp
+++ b/src/mongo/idl/idl_test.cpp
@@ -274,6 +274,31 @@ TEST(IDLOneTypeTests, TestNegativeWrongTypes) {
TestParsers<One_timestamp, bsonTimestamp>();
}
+// Negative: document with wrong types for required field
+TEST(IDLOneTypeTests, TestNegativeRequiredNullTypes) {
+ TestParse<One_string, String, NullLabeler, jstNULL>(BSONNULL);
+ TestParse<One_int, NumberInt, NullLabeler, jstNULL>(BSONNULL);
+ TestParse<One_long, NumberLong, NullLabeler, jstNULL>(BSONNULL);
+ TestParse<One_double, NumberDouble, NullLabeler, jstNULL>(BSONNULL);
+ TestParse<One_bool, Bool, NullLabeler, jstNULL>(BSONNULL);
+ TestParse<One_objectid, jstOID, NullLabeler, jstNULL>(BSONNULL);
+ TestParse<One_date, Date, NullLabeler, jstNULL>(BSONNULL);
+ TestParse<One_timestamp, bsonTimestamp, NullLabeler, jstNULL>(BSONNULL);
+}
+
+// Negative: document with wrong types for required field
+TEST(IDLOneTypeTests, TestNegativeRequiredUndefinedTypes) {
+ TestParse<One_string, String, UndefinedLabeler, Undefined>(BSONUndefined);
+ TestParse<One_int, NumberInt, UndefinedLabeler, Undefined>(BSONUndefined);
+ TestParse<One_long, NumberLong, UndefinedLabeler, Undefined>(BSONUndefined);
+ TestParse<One_double, NumberDouble, UndefinedLabeler, Undefined>(BSONUndefined);
+ TestParse<One_bool, Bool, UndefinedLabeler, Undefined>(BSONUndefined);
+ TestParse<One_objectid, jstOID, UndefinedLabeler, Undefined>(BSONUndefined);
+ TestParse<One_date, Date, UndefinedLabeler, Undefined>(BSONUndefined);
+ TestParse<One_timestamp, bsonTimestamp, UndefinedLabeler, Undefined>(BSONUndefined);
+}
+
+
// Mixed: test a type that accepts multiple bson types
TEST(IDLOneTypeTests, TestSafeInt32) {
TestParse<One_safeint32, NumberInt, StringData, String>("test_value");
@@ -600,6 +625,19 @@ TEST(IDLFieldTests, TestStrictStructIgnoredField) {
}
}
+// Negative: check duplicate ignored fields fail
+TEST(IDLFieldTests, TestStrictDuplicateIgnoredFields) {
+ IDLParserErrorContext ctxt("root");
+
+ // Negative: Test duplicate ignored fields fail
+ {
+ auto testDoc =
+ BSON("required_field" << 12 << "ignored_field" << 123 << "ignored_field" << 456);
+ ASSERT_THROWS(IgnoredField::parse(ctxt, testDoc), AssertionException);
+ }
+}
+
+
// First test: test an empty document and the default value
// Second test: test a non-empty document and that we do not get the default value
#define TEST_DEFAULT_VALUES(field_name, default_value, new_value) \
@@ -685,6 +723,31 @@ TEST(IDLFieldTests, TestOptionalFields) {
}
}
+template <typename TestT>
+void TestWeakType(TestT test_value) {
+ IDLParserErrorContext ctxt("root");
+ auto testDoc =
+ BSON("field1" << test_value << "field2" << test_value << "field3" << test_value << "field4"
+ << test_value
+ << "field5"
+ << test_value);
+ auto testStruct = Optional_field::parse(ctxt, testDoc);
+
+ ASSERT_FALSE(testStruct.getField1().is_initialized());
+ ASSERT_FALSE(testStruct.getField2().is_initialized());
+ ASSERT_FALSE(testStruct.getField3().is_initialized());
+ ASSERT_FALSE(testStruct.getField4().is_initialized());
+ ASSERT_FALSE(testStruct.getField5().is_initialized());
+}
+
+// Positive: struct strict, and optional field works
+TEST(IDLFieldTests, TestOptionalFieldsWithNullAndUndefined) {
+
+ TestWeakType<NullLabeler>(BSONNULL);
+
+ TestWeakType<UndefinedLabeler>(BSONUndefined);
+}
+
// Positive: Test a nested struct
TEST(IDLNestedStruct, TestDuplicateTypes) {
IDLParserErrorContext ctxt("root");