diff options
Diffstat (limited to 'src/mongo/dbtests/jstests.cpp')
-rw-r--r-- | src/mongo/dbtests/jstests.cpp | 1193 |
1 files changed, 1 insertions, 1192 deletions
diff --git a/src/mongo/dbtests/jstests.cpp b/src/mongo/dbtests/jstests.cpp index c56b915e838..224ab0b4689 100644 --- a/src/mongo/dbtests/jstests.cpp +++ b/src/mongo/dbtests/jstests.cpp @@ -1031,22 +1031,7 @@ public: reset(); } void run() { - if (!getGlobalScriptEngine()->utf8Ok()) { - mongo::unittest::log() << "warning: utf8 not supported" << endl; - return; - } - string utf8ObjSpec = "{'_id':'\\u0001\\u007f\\u07ff\\uffff'}"; - BSONObj utf8Obj = fromjson(utf8ObjSpec); - - const ServiceContext::UniqueOperationContext opCtxPtr = cc().makeOperationContext(); - OperationContext& opCtx = *opCtxPtr; - DBDirectClient client(&opCtx); - - client.insert(ns(), utf8Obj); - client.eval("unittest", - "v = db.jstests.utf8check.findOne(); db.jstests.utf8check.remove( {} ); " - "db.jstests.utf8check.insert( v );"); - check(utf8Obj, client.findOne(ns(), BSONObj())); + ASSERT(getGlobalScriptEngine()->utf8Ok()); } private: @@ -1071,1071 +1056,6 @@ private: } }; -class LongUtf8String { -public: - LongUtf8String() { - reset(); - } - ~LongUtf8String() { - reset(); - } - void run() { - if (!getGlobalScriptEngine()->utf8Ok()) - return; - - const ServiceContext::UniqueOperationContext opCtxPtr = cc().makeOperationContext(); - OperationContext& opCtx = *opCtxPtr; - DBDirectClient client(&opCtx); - - client.eval("unittest", - "db.jstests.longutf8string.save( {_id:'\\uffff\\uffff\\uffff\\uffff'} )"); - } - -private: - void reset() { - const ServiceContext::UniqueOperationContext opCtxPtr = cc().makeOperationContext(); - OperationContext& opCtx = *opCtxPtr; - DBDirectClient client(&opCtx); - - client.dropCollection(ns()); - } - - static const char* ns() { - return "unittest.jstests.longutf8string"; - } -}; - -class InvalidUTF8Check { -public: - void run() { - if (!getGlobalScriptEngine()->utf8Ok()) - return; - - unique_ptr<Scope> s; - s.reset(getGlobalScriptEngine()->newScope()); - - BSONObj b; - { - char crap[5]; - - crap[0] = (char)128; - crap[1] = 17; - crap[2] = (char)128; - crap[3] = 17; - crap[4] = 0; - - BSONObjBuilder bb; - bb.append("x", crap); - b = bb.obj(); - } - - // cout << "ELIOT: " << b.jsonString() << endl; - // its ok if this is handled by js, just can't create a c++ exception - s->invoke("x=this.x.length;", 0, &b); - } -}; - -class CodeTests { -public: - void run() { - unique_ptr<Scope> s(getGlobalScriptEngine()->newScope()); - - { - BSONObjBuilder b; - b.append("a", 1); - b.appendCode("b", "function(){ out.b = 11; }"); - b.appendCodeWScope("c", "function(){ out.c = 12; }", BSONObj()); - b.appendCodeWScope("d", "function(){ out.d = 13 + bleh; }", BSON("bleh" << 5)); - s->setObject("foo", b.obj()); - } - - s->invokeSafe("out = {}; out.a = foo.a; foo.b(); foo.c();", 0, 0); - BSONObj out = s->getObject("out"); - - ASSERT_EQUALS(1, out["a"].number()); - ASSERT_EQUALS(11, out["b"].number()); - ASSERT_EQUALS(12, out["c"].number()); - - // Guess we don't care about this - // s->invokeSafe( "foo.d() " , BSONObj() ); - // out = s->getObject( "out" ); - // ASSERT_EQUALS( 18 , out["d"].number() ); - } -}; - -namespace RoundTripTests { - -// Inherit from this class to test round tripping of JSON objects -class TestRoundTrip { -public: - virtual ~TestRoundTrip() {} - void run() { - { - // Insert in Javascript -> Find using DBDirectClient - - // Drop the collection - const ServiceContext::UniqueOperationContext opCtxPtr = cc().makeOperationContext(); - OperationContext& opCtx = *opCtxPtr; - DBDirectClient client(&opCtx); - - client.dropCollection("unittest.testroundtrip"); - - // Insert in Javascript - stringstream jsInsert; - jsInsert << "db.testroundtrip.insert(" << jsonIn() << ")"; - ASSERT_TRUE(client.eval("unittest", jsInsert.str())); - - // Find using DBDirectClient - BSONObj excludeIdProjection = BSON("_id" << 0); - BSONObj directFind = client.findOne("unittest.testroundtrip", "", &excludeIdProjection); - bsonEquals(bson(), directFind); - } - - { - // Insert using DBDirectClient -> Find in Javascript - - const ServiceContext::UniqueOperationContext opCtxPtr = cc().makeOperationContext(); - OperationContext& opCtx = *opCtxPtr; - DBDirectClient client(&opCtx); - - // Drop the collection - client.dropCollection("unittest.testroundtrip"); - - // Insert using DBDirectClient - client.insert("unittest.testroundtrip", bson()); - - // Find in Javascript - stringstream jsFind; - jsFind << "dbref = db.testroundtrip.findOne( { } , { _id : 0 } )\n" - << "assert.eq(dbref, " << jsonOut() << ")"; - ASSERT_TRUE(client.eval("unittest", jsFind.str())); - } - } - -protected: - // Methods that must be defined by child classes - virtual BSONObj bson() const = 0; - virtual string json() const = 0; - - // This can be overriden if a different meaning of equality besides woCompare is needed - virtual void bsonEquals(const BSONObj& expected, const BSONObj& actual) { - if (expected.woCompare(actual)) { - ::mongo::log() << "want:" << expected.jsonString() << " size: " << expected.objsize() - << endl; - ::mongo::log() << "got :" << actual.jsonString() << " size: " << actual.objsize() - << endl; - ::mongo::log() << expected.hexDump() << endl; - ::mongo::log() << actual.hexDump() << endl; - } - ASSERT(!expected.woCompare(actual)); - } - - // This can be overriden if the JSON representation is altered on the round trip - virtual string jsonIn() const { - return json(); - } - virtual string jsonOut() const { - return json(); - } -}; - -class DBRefTest : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - OID o; - memset(&o, 0, 12); - BSONObjBuilder subBuilder(b.subobjStart("a")); - subBuilder.append("$ref", "ns"); - subBuilder.append("$id", o); - subBuilder.done(); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : DBRef( \"ns\", ObjectId( \"000000000000000000000000\" ) ) }"; - } - - // A "fetch" function is added to the DBRef object when it is inserted using the - // constructor, so we need to compare the fields individually - virtual void bsonEquals(const BSONObj& expected, const BSONObj& actual) { - ASSERT_EQUALS(expected["a"].type(), actual["a"].type()); - ASSERT_EQUALS(expected["a"]["$id"].OID(), actual["a"]["$id"].OID()); - ASSERT_EQUALS(expected["a"]["$ref"].String(), actual["a"]["$ref"].String()); - } -}; - -class DBPointerTest : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - OID o; - memset(&o, 0, 12); - b.appendDBRef("a", "ns", o); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : DBPointer( \"ns\", ObjectId( \"000000000000000000000000\" ) ) }"; - } -}; - -class InformalDBRefTest : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - BSONObjBuilder subBuilder(b.subobjStart("a")); - subBuilder.append("$ref", "ns"); - subBuilder.append("$id", "000000000000000000000000"); - subBuilder.done(); - return b.obj(); - } - - // Don't need to return anything because we are overriding both jsonOut and jsonIn - virtual string json() const { - return ""; - } - - // Need to override these because the JSON doesn't actually round trip. - // An object with "$ref" and "$id" fields is handled specially and different on the way out. - virtual string jsonOut() const { - return "{ \"a\" : DBRef( \"ns\", \"000000000000000000000000\" ) }"; - } - virtual string jsonIn() const { - stringstream ss; - ss << "{ \"a\" : { \"$ref\" : \"ns\" , " - << "\"$id\" : \"000000000000000000000000\" } }"; - return ss.str(); - } -}; - -class InformalDBRefOIDTest : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - OID o; - memset(&o, 0, 12); - BSONObjBuilder subBuilder(b.subobjStart("a")); - subBuilder.append("$ref", "ns"); - subBuilder.append("$id", o); - subBuilder.done(); - return b.obj(); - } - - // Don't need to return anything because we are overriding both jsonOut and jsonIn - virtual string json() const { - return ""; - } - - // Need to override these because the JSON doesn't actually round trip. - // An object with "$ref" and "$id" fields is handled specially and different on the way out. - virtual string jsonOut() const { - return "{ \"a\" : DBRef( \"ns\", ObjectId( \"000000000000000000000000\" ) ) }"; - } - virtual string jsonIn() const { - stringstream ss; - ss << "{ \"a\" : { \"$ref\" : \"ns\" , " - << "\"$id\" : ObjectId( \"000000000000000000000000\" ) } }"; - return ss.str(); - } -}; - -class InformalDBRefExtraFieldTest : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - OID o; - memset(&o, 0, 12); - BSONObjBuilder subBuilder(b.subobjStart("a")); - subBuilder.append("$ref", "ns"); - subBuilder.append("$id", o); - subBuilder.append("otherfield", "value"); - subBuilder.done(); - return b.obj(); - } - - // Don't need to return anything because we are overriding both jsonOut and jsonIn - virtual string json() const { - return ""; - } - - // Need to override these because the JSON doesn't actually round trip. - // An object with "$ref" and "$id" fields is handled specially and different on the way out. - virtual string jsonOut() const { - return "{ \"a\" : DBRef( \"ns\", ObjectId( \"000000000000000000000000\" ) ) }"; - } - virtual string jsonIn() const { - stringstream ss; - ss << "{ \"a\" : { \"$ref\" : \"ns\" , " - << "\"$id\" : ObjectId( \"000000000000000000000000\" ) , " - << "\"otherfield\" : \"value\" } }"; - return ss.str(); - } -}; - -class Empty : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - return b.obj(); - } - virtual string json() const { - return "{}"; - } -}; - -class EmptyWithSpace : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - return b.obj(); - } - virtual string json() const { - return "{ }"; - } -}; - -class SingleString : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.append("a", "b"); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : \"b\" }"; - } -}; - -class EmptyStrings : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.append("", ""); - return b.obj(); - } - virtual string json() const { - return "{ \"\" : \"\" }"; - } -}; - -class SingleNumber : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.append("a", 1); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : 1 }"; - } -}; - -class RealNumber : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - double d; - ASSERT_OK(parseNumberFromString("0.7", &d)); - b.append("a", d); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : 0.7 }"; - } -}; - -class FancyNumber : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - double d; - ASSERT_OK(parseNumberFromString("-4.4433e-2", &d)); - b.append("a", d); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : -4.4433e-2 }"; - } -}; - -class TwoElements : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.append("a", 1); - b.append("b", "foo"); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : 1, \"b\" : \"foo\" }"; - } -}; - -class Subobject : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.append("a", 1); - BSONObjBuilder c; - c.append("z", b.done()); - return c.obj(); - } - virtual string json() const { - return "{ \"z\" : { \"a\" : 1 } }"; - } -}; - -class DeeplyNestedObject : public TestRoundTrip { - virtual string buildJson(int depth) const { - if (depth == 0) { - return "{\"0\":true}"; - } else { - std::stringstream ss; - ss << "{\"" << depth << "\":" << buildJson(depth - 1) << "}"; - depth--; - return ss.str(); - } - } - virtual BSONObj buildBson(int depth) const { - BSONObjBuilder builder; - if (depth == 0) { - builder.append("0", true); - return builder.obj(); - } else { - std::stringstream ss; - ss << depth; - depth--; - builder.append(ss.str(), buildBson(depth)); - return builder.obj(); - } - } - virtual BSONObj bson() const { - return buildBson(35); - } - virtual string json() const { - return buildJson(35); - } -}; - -class ArrayEmpty : public TestRoundTrip { - virtual BSONObj bson() const { - vector<int> arr; - BSONObjBuilder b; - b.append("a", arr); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : [] }"; - } -}; - -class Array : public TestRoundTrip { - virtual BSONObj bson() const { - vector<int> arr; - arr.push_back(1); - arr.push_back(2); - arr.push_back(3); - BSONObjBuilder b; - b.append("a", arr); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : [ 1, 2, 3 ] }"; - } -}; - -class True : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.appendBool("a", true); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : true }"; - } -}; - -class False : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.appendBool("a", false); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : false }"; - } -}; - -class Null : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.appendNull("a"); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : null }"; - } -}; - -class Undefined : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.appendUndefined("a"); - return b.obj(); - } - - virtual string json() const { - return "{ \"a\" : undefined }"; - } -}; - -class EscapedCharacters : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.append("a", "\" \\ / \b \f \n \r \t \v"); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : \"\\\" \\\\ \\/ \\b \\f \\n \\r \\t \\v\" }"; - } -}; - -class NonEscapedCharacters : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.append("a", "% { a z $ # ' "); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : \"\\% \\{ \\a \\z \\$ \\# \\' \\ \" }"; - } -}; - -class AllowedControlCharacter : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.append("a", "\x7f"); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : \"\x7f\" }"; - } -}; - -class NumbersInFieldName : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.append("b1", "b"); - return b.obj(); - } - virtual string json() const { - return "{ b1 : \"b\" }"; - } -}; - -class EscapeFieldName : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.append("\n", "b"); - return b.obj(); - } - virtual string json() const { - return "{ \"\\n\" : \"b\" }"; - } -}; - -class EscapedUnicodeToUtf8 : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - unsigned char u[7]; - u[0] = 0xe0 | 0x0a; - u[1] = 0x80; - u[2] = 0x80; - u[3] = 0xe0 | 0x0a; - u[4] = 0x80; - u[5] = 0x80; - u[6] = 0; - b.append("a", (char*)u); - BSONObj built = b.obj(); - ASSERT_EQUALS(string((char*)u), built.firstElement().valuestr()); - return built; - } - virtual string json() const { - return "{ \"a\" : \"\\ua000\\uA000\" }"; - } -}; - -class Utf8AllOnes : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - unsigned char u[8]; - u[0] = 0x01; - - u[1] = 0x7f; - - u[2] = 0xdf; - u[3] = 0xbf; - - u[4] = 0xef; - u[5] = 0xbf; - u[6] = 0xbf; - - u[7] = 0; - - b.append("a", (char*)u); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : \"\\u0001\\u007f\\u07ff\\uffff\" }"; - } -}; - -class Utf8FirstByteOnes : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - unsigned char u[6]; - u[0] = 0xdc; - u[1] = 0x80; - - u[2] = 0xef; - u[3] = 0xbc; - u[4] = 0x80; - - u[5] = 0; - - b.append("a", (char*)u); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : \"\\u0700\\uff00\" }"; - } -}; - -class BinData : public TestRoundTrip { - virtual BSONObj bson() const { - char z[3]; - z[0] = 'a'; - z[1] = 'b'; - z[2] = 'c'; - BSONObjBuilder b; - b.appendBinData("a", 3, BinDataGeneral, z); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : BinData( 0 , \"YWJj\" ) }"; - } -}; - -class BinDataPaddedSingle : public TestRoundTrip { - virtual BSONObj bson() const { - char z[2]; - z[0] = 'a'; - z[1] = 'b'; - BSONObjBuilder b; - b.appendBinData("a", 2, BinDataGeneral, z); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : BinData( 0 , \"YWI=\" ) }"; - } -}; - -class BinDataPaddedDouble : public TestRoundTrip { - virtual BSONObj bson() const { - char z[1]; - z[0] = 'a'; - BSONObjBuilder b; - b.appendBinData("a", 1, BinDataGeneral, z); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : BinData( 0 , \"YQ==\" ) }"; - } -}; - -class BinDataAllChars : public TestRoundTrip { - virtual BSONObj bson() const { - unsigned char z[] = {0x00, 0x10, 0x83, 0x10, 0x51, 0x87, 0x20, 0x92, 0x8B, 0x30, - 0xD3, 0x8F, 0x41, 0x14, 0x93, 0x51, 0x55, 0x97, 0x61, 0x96, - 0x9B, 0x71, 0xD7, 0x9F, 0x82, 0x18, 0xA3, 0x92, 0x59, 0xA7, - 0xA2, 0x9A, 0xAB, 0xB2, 0xDB, 0xAF, 0xC3, 0x1C, 0xB3, 0xD3, - 0x5D, 0xB7, 0xE3, 0x9E, 0xBB, 0xF3, 0xDF, 0xBF}; - BSONObjBuilder b; - b.appendBinData("a", 48, BinDataGeneral, z); - return b.obj(); - } - virtual string json() const { - stringstream ss; - ss << "{ \"a\" : BinData( 0 , \"ABCDEFGHIJKLMNOPQRSTUVWXYZ" - << "abcdefghijklmnopqrstuvwxyz0123456789+/\" ) }"; - return ss.str(); - } -}; - -class Date : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.appendDate("a", Date_t()); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : new Date( 0 ) }"; - } -}; - -class DateNonzero : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.appendDate("a", Date_t::fromMillisSinceEpoch(100)); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : new Date( 100 ) }"; - } -}; - -class DateNegative : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.appendDate("a", Date_t::fromMillisSinceEpoch(-1)); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : new Date( -1 ) }"; - } -}; - -class JSTimestamp : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.append("a", Timestamp(20, 5)); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : Timestamp( 20, 5 ) }"; - } -}; - -class TimestampMax : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.appendMaxForType("a", mongo::bsonTimestamp); - BSONObj o = b.obj(); - return o; - } - virtual string json() const { - Timestamp opTime = Timestamp::max(); - stringstream ss; - ss << "{ \"a\" : Timestamp( " << opTime.getSecs() << ", " << opTime.getInc() << " ) }"; - return ss.str(); - } -}; - -class Regex : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.appendRegex("a", "b", ""); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : /b/ }"; - } -}; - -class RegexWithQuotes : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.appendRegex("a", "\"", ""); - return b.obj(); - } - virtual string json() const { - return "{ \"a\" : /\"/ }"; - } -}; - -class UnquotedFieldName : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.append("a_b", 1); - return b.obj(); - } - virtual string json() const { - return "{ a_b : 1 }"; - } -}; - -class SingleQuotes : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.append("ab'c\"", "bb\b '\""); - return b.obj(); - } - virtual string json() const { - return "{ 'ab\\'c\"' : 'bb\\b \\'\"' }"; - } -}; - -class ObjectId : public TestRoundTrip { - virtual BSONObj bson() const { - OID id; - id.init("deadbeeff00ddeadbeeff00d"); - BSONObjBuilder b; - b.appendOID("foo", &id); - return b.obj(); - } - virtual string json() const { - return "{ \"foo\": ObjectId( \"deadbeeff00ddeadbeeff00d\" ) }"; - } -}; - -class NumberLong : public TestRoundTrip { -public: - virtual BSONObj bson() const { - return BSON("long" << 4611686018427387904ll); // 2**62 - } - virtual string json() const { - return "{ \"long\": NumberLong(4611686018427387904) }"; - } -}; - -class NumberInt : public TestRoundTrip { -public: - virtual BSONObj bson() const { - return BSON("int" << static_cast<int>(100)); - } - virtual string json() const { - return "{ \"int\": NumberInt(100) }"; - } -}; - -class Number : public TestRoundTrip { -public: - virtual BSONObj bson() const { - return BSON("double" << 3.14); - } - virtual string json() const { - return "{ \"double\": Number(3.14) }"; - } -}; - -class NumberDecimal : public TestRoundTrip { -public: - virtual BSONObj bson() const { - return BSON("decimal" << Decimal128("2.010")); - } - virtual string json() const { - return "{ \"decimal\": NumberDecimal(\"+2.010\") }"; - } -}; - -class NumberDecimalNegative : public TestRoundTrip { -public: - virtual BSONObj bson() const { - return BSON("decimal" << Decimal128("-4.018")); - } - virtual string json() const { - return "{ \"decimal\": NumberDecimal(\"-4018E-3\") }"; - } -}; - -class NumberDecimalMax : public TestRoundTrip { -public: - virtual BSONObj bson() const { - return BSON("decimal" << Decimal128("+9.999999999999999999999999999999999E6144")); - } - virtual string json() const { - return "{ \"decimal\": NumberDecimal(\"+9999999999999999999999999999999999E6111\") }"; - } -}; - -class NumberDecimalMin : public TestRoundTrip { -public: - virtual BSONObj bson() const { - return BSON("decimal" << Decimal128("0.000000000000000000000000000000001E-6143")); - } - virtual string json() const { - return "{ \"decimal\": NumberDecimal(\"+1E-6176\") }"; - } -}; - -class NumberDecimalPositiveZero : public TestRoundTrip { -public: - virtual BSONObj bson() const { - return BSON("decimal" << Decimal128("0")); - } - virtual string json() const { - return "{ \"decimal\": NumberDecimal(\"0\") }"; - } -}; - -class NumberDecimalNegativeZero : public TestRoundTrip { -public: - virtual BSONObj bson() const { - return BSON("decimal" << Decimal128("-0")); - } - virtual string json() const { - return "{ \"decimal\": NumberDecimal(\"-0\") }"; - } -}; - -class NumberDecimalPositiveNaN : public TestRoundTrip { -public: - virtual BSONObj bson() const { - return BSON("decimal" << Decimal128("NaN")); - } - virtual string json() const { - return "{ \"decimal\": NumberDecimal(\"NaN\") }"; - } -}; - -class NumberDecimalNegativeNaN : public TestRoundTrip { -public: - virtual BSONObj bson() const { - return BSON("decimal" << Decimal128("-NaN")); - } - virtual string json() const { - return "{ \"decimal\": NumberDecimal(\"-NaN\") }"; - } -}; - -class NumberDecimalPositiveInfinity : public TestRoundTrip { -public: - virtual BSONObj bson() const { - return BSON("decimal" << Decimal128("1E999999")); - } - virtual string json() const { - return "{ \"decimal\": NumberDecimal(\"+Inf\") }"; - } -}; - -class NumberDecimalNegativeInfinity : public TestRoundTrip { -public: - virtual BSONObj bson() const { - return BSON("decimal" << Decimal128("-1E999999")); - } - virtual string json() const { - return "{ \"decimal\": NumberDecimal(\"-Inf\") }"; - } -}; - -class NumberDecimalPrecision : public TestRoundTrip { -public: - virtual BSONObj bson() const { - return BSON("decimal" << Decimal128("5.00")); - } - virtual string json() const { - return "{ \"decimal\": NumberDecimal(\"+500E-2\") }"; - } -}; - -class UUID : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - unsigned char z[] = {0xAB, - 0xCD, - 0xEF, - 0xAB, - 0xCD, - 0xEF, - 0xAB, - 0xCD, - 0xEF, - 0xAB, - 0xCD, - 0xEF, - 0x00, - 0x00, - 0x00, - 0x00}; - b.appendBinData("a", 16, bdtUUID, z); - return b.obj(); - } - - // Don't need to return anything because we are overriding both jsonOut and jsonIn - virtual string json() const { - return ""; - } - - // The UUID constructor corresponds to a special BinData type - virtual string jsonIn() const { - return "{ \"a\" : UUID(\"abcdefabcdefabcdefabcdef00000000\") }"; - } - virtual string jsonOut() const { - return "{ \"a\" : BinData(3,\"q83vq83vq83vq83vAAAAAA==\") }"; - } -}; - -class HexData : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - unsigned char z[] = {0xAB, - 0xCD, - 0xEF, - 0xAB, - 0xCD, - 0xEF, - 0xAB, - 0xCD, - 0xEF, - 0xAB, - 0xCD, - 0xEF, - 0x00, - 0x00, - 0x00, - 0x00}; - b.appendBinData("a", 16, BinDataGeneral, z); - return b.obj(); - } - - // Don't need to return anything because we are overriding both jsonOut and jsonIn - virtual string json() const { - return ""; - } - - // The HexData constructor creates a BinData type from a hex string - virtual string jsonIn() const { - return "{ \"a\" : HexData(0,\"abcdefabcdefabcdefabcdef00000000\") }"; - } - virtual string jsonOut() const { - return "{ \"a\" : BinData(0,\"q83vq83vq83vq83vAAAAAA==\") }"; - } -}; - -class MD5 : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - unsigned char z[] = {0xAB, - 0xCD, - 0xEF, - 0xAB, - 0xCD, - 0xEF, - 0xAB, - 0xCD, - 0xEF, - 0xAB, - 0xCD, - 0xEF, - 0x00, - 0x00, - 0x00, - 0x00}; - b.appendBinData("a", 16, MD5Type, z); - return b.obj(); - } - - // Don't need to return anything because we are overriding both jsonOut and jsonIn - virtual string json() const { - return ""; - } - - // The HexData constructor creates a BinData type from a hex string - virtual string jsonIn() const { - return "{ \"a\" : MD5(\"abcdefabcdefabcdefabcdef00000000\") }"; - } - virtual string jsonOut() const { - return "{ \"a\" : BinData(5,\"q83vq83vq83vq83vAAAAAA==\") }"; - } -}; - -class NullString : public TestRoundTrip { - virtual BSONObj bson() const { - BSONObjBuilder b; - b.append("x", "a\0b", 4); - return b.obj(); - } - virtual string json() const { - return "{ \"x\" : \"a\\u0000b\" }"; - } -}; - -} // namespace RoundTripTests class BinDataType { public: @@ -2277,44 +1197,6 @@ public: } }; - -class InvalidStoredJS { -public: - void run() { - BSONObjBuilder query; - query.append("_id", "invalidstoredjs1"); - - BSONObjBuilder update; - update.append("_id", "invalidstoredjs1"); - update.appendCode("value", - "function () { db.test.find().forEach(function(obj) { continue; }); }"); - { - const ServiceContext::UniqueOperationContext opCtxPtr = cc().makeOperationContext(); - OperationContext& opCtx = *opCtxPtr; - DBDirectClient client(&opCtx); - client.update("test.system.js", query.obj(), update.obj(), true /* upsert */); - } - - unique_ptr<Scope> s(getGlobalScriptEngine()->newScope()); - { - const ServiceContext::UniqueOperationContext opCtxPtr = cc().makeOperationContext(); - OperationContext& opCtx = *opCtxPtr; - DBDirectClient client(&opCtx); - client.eval("test", "invalidstoredjs1()"); - } - - BSONObj info; - BSONElement ret; - { - const ServiceContext::UniqueOperationContext opCtxPtr = cc().makeOperationContext(); - OperationContext& opCtx = *opCtxPtr; - DBDirectClient client(&opCtx); - ASSERT(client.eval("test", "return 5 + 12", info, ret)); - } - ASSERT_EQUALS(17, ret.number()); - } -}; - /** * This tests a bug discovered in SERVER-24054, where certain interesting nan patterns crash * spidermonkey by looking like non-double type puns. This verifies that we put that particular @@ -2619,22 +1501,13 @@ public: add<RenameTest>(); add<WeirdObjects>(); - add<CodeTests>(); add<BinDataType>(); add<VarTests>(); - add<Speed1>(); - - add<InvalidUTF8Check>(); add<Utf8Check>(); - add<LongUtf8String>(); - add<ScopeOut>(); - add<InvalidStoredJS>(); - add<NovelNaN>(); - add<NoReturnSpecified>(); add<RecursiveInvoke>(); @@ -2642,70 +1515,6 @@ public: add<ErrorWithSidecarFromInvoke>(); add<RequiresOwnedObjects>(); add<ConvertShardKeyToHashed>(); - - add<RoundTripTests::DBRefTest>(); - add<RoundTripTests::DBPointerTest>(); - add<RoundTripTests::InformalDBRefTest>(); - add<RoundTripTests::InformalDBRefOIDTest>(); - add<RoundTripTests::InformalDBRefExtraFieldTest>(); - add<RoundTripTests::Empty>(); - add<RoundTripTests::EmptyWithSpace>(); - add<RoundTripTests::SingleString>(); - add<RoundTripTests::EmptyStrings>(); - add<RoundTripTests::SingleNumber>(); - add<RoundTripTests::RealNumber>(); - add<RoundTripTests::FancyNumber>(); - add<RoundTripTests::TwoElements>(); - add<RoundTripTests::Subobject>(); - add<RoundTripTests::DeeplyNestedObject>(); - add<RoundTripTests::ArrayEmpty>(); - add<RoundTripTests::Array>(); - add<RoundTripTests::True>(); - add<RoundTripTests::False>(); - add<RoundTripTests::Null>(); - add<RoundTripTests::Undefined>(); - add<RoundTripTests::EscapedCharacters>(); - add<RoundTripTests::NonEscapedCharacters>(); - add<RoundTripTests::AllowedControlCharacter>(); - add<RoundTripTests::NumbersInFieldName>(); - add<RoundTripTests::EscapeFieldName>(); - add<RoundTripTests::EscapedUnicodeToUtf8>(); - add<RoundTripTests::Utf8AllOnes>(); - add<RoundTripTests::Utf8FirstByteOnes>(); - add<RoundTripTests::BinData>(); - add<RoundTripTests::BinDataPaddedSingle>(); - add<RoundTripTests::BinDataPaddedDouble>(); - add<RoundTripTests::BinDataAllChars>(); - add<RoundTripTests::Date>(); - add<RoundTripTests::DateNonzero>(); - add<RoundTripTests::DateNegative>(); - add<RoundTripTests::JSTimestamp>(); - add<RoundTripTests::TimestampMax>(); - add<RoundTripTests::Regex>(); - add<RoundTripTests::RegexWithQuotes>(); - add<RoundTripTests::UnquotedFieldName>(); - add<RoundTripTests::SingleQuotes>(); - add<RoundTripTests::ObjectId>(); - add<RoundTripTests::NumberLong>(); - add<RoundTripTests::NumberInt>(); - add<RoundTripTests::Number>(); - - add<RoundTripTests::NumberDecimal>(); - add<RoundTripTests::NumberDecimalNegative>(); - add<RoundTripTests::NumberDecimalMax>(); - add<RoundTripTests::NumberDecimalMin>(); - add<RoundTripTests::NumberDecimalPositiveZero>(); - add<RoundTripTests::NumberDecimalNegativeZero>(); - add<RoundTripTests::NumberDecimalPositiveNaN>(); - add<RoundTripTests::NumberDecimalNegativeNaN>(); - add<RoundTripTests::NumberDecimalPositiveInfinity>(); - add<RoundTripTests::NumberDecimalNegativeInfinity>(); - add<RoundTripTests::NumberDecimalPrecision>(); - - add<RoundTripTests::UUID>(); - add<RoundTripTests::HexData>(); - add<RoundTripTests::MD5>(); - add<RoundTripTests::NullString>(); } }; |