1140
1141
BSONType type_;
1143
// Randomized BSON parsing test. See if we seg fault.
1144
// NOTE This test is disabled (below), see SERVER-4948.
1147
Fuzz( double frequency ) : frequency_( frequency ) {}
1149
BSONObj a = fromjson( "{\"a\": 1, \"b\": \"c\"}" );
1153
BSONObj b = fromjson( "{\"one\":2, \"two\":5, \"three\": {},"
1154
"\"four\": { \"five\": { \"six\" : 11 } },"
1155
"\"seven\": [ \"a\", \"bb\", \"ccc\", 5 ],"
1156
"\"eight\": Dbref( \"rrr\", \"01234567890123456789aaaa\" ),"
1157
"\"_id\": ObjectId( \"deadbeefdeadbeefdeadbeef\" ),"
1158
"\"nine\": { \"$binary\": \"abc=\", \"$type\": \"00\" },"
1159
"\"ten\": Date( 44 ), \"eleven\": /foooooo/i }" );
1164
void fuzz( BSONObj &o ) const {
1165
for( int i = 4; i < o.objsize(); ++i )
1166
for( unsigned char j = 1; j; j <<= 1 )
1167
if ( rand() < int( RAND_MAX * frequency_ ) ) {
1168
char *c = const_cast< char * >( o.objdata() ) + i;
1178
1144
} // namespace Validation
1180
1146
} // namespace BSONObjTests
1382
// These are listed in order of BSONType
1384
ASSERT_EQUALS(objTypeOf(MINKEY), MinKey);
1385
ASSERT_EQUALS(arrTypeOf(MINKEY), MinKey);
1387
// EOO not valid in middle of BSONObj
1389
ASSERT_EQUALS(objTypeOf(1.0), NumberDouble);
1390
ASSERT_EQUALS(arrTypeOf(1.0), NumberDouble);
1392
ASSERT_EQUALS(objTypeOf(""), String);
1393
ASSERT_EQUALS(arrTypeOf(""), String);
1394
ASSERT_EQUALS(objTypeOf(string()), String);
1395
ASSERT_EQUALS(arrTypeOf(string()), String);
1396
ASSERT_EQUALS(objTypeOf(StringData("")), String);
1397
ASSERT_EQUALS(arrTypeOf(StringData("")), String);
1399
ASSERT_EQUALS(objTypeOf(BSONObj()), Object);
1400
ASSERT_EQUALS(arrTypeOf(BSONObj()), Object);
1402
ASSERT_EQUALS(objTypeOf(BSONArray()), Array);
1403
ASSERT_EQUALS(arrTypeOf(BSONArray()), Array);
1405
ASSERT_EQUALS(objTypeOf(BSONBinData("", 0, BinDataGeneral)), BinData);
1406
ASSERT_EQUALS(arrTypeOf(BSONBinData("", 0, BinDataGeneral)), BinData);
1408
ASSERT_EQUALS(objTypeOf(BSONUndefined), Undefined);
1409
ASSERT_EQUALS(arrTypeOf(BSONUndefined), Undefined);
1411
ASSERT_EQUALS(objTypeOf(OID()), jstOID);
1412
ASSERT_EQUALS(arrTypeOf(OID()), jstOID);
1414
ASSERT_EQUALS(objTypeOf(true), Bool);
1415
ASSERT_EQUALS(arrTypeOf(true), Bool);
1417
ASSERT_EQUALS(objTypeOf(Date_t()), Date);
1418
ASSERT_EQUALS(arrTypeOf(Date_t()), Date);
1420
ASSERT_EQUALS(objTypeOf(BSONNULL), jstNULL);
1421
ASSERT_EQUALS(arrTypeOf(BSONNULL), jstNULL);
1423
ASSERT_EQUALS(objTypeOf(BSONRegEx("", "")), RegEx);
1424
ASSERT_EQUALS(arrTypeOf(BSONRegEx("", "")), RegEx);
1426
ASSERT_EQUALS(objTypeOf(BSONDBRef("", OID())), DBRef);
1427
ASSERT_EQUALS(arrTypeOf(BSONDBRef("", OID())), DBRef);
1429
ASSERT_EQUALS(objTypeOf(BSONCode("")), Code);
1430
ASSERT_EQUALS(arrTypeOf(BSONCode("")), Code);
1432
ASSERT_EQUALS(objTypeOf(BSONSymbol("")), Symbol);
1433
ASSERT_EQUALS(arrTypeOf(BSONSymbol("")), Symbol);
1435
ASSERT_EQUALS(objTypeOf(BSONCodeWScope("", BSONObj())), CodeWScope);
1436
ASSERT_EQUALS(arrTypeOf(BSONCodeWScope("", BSONObj())), CodeWScope);
1438
ASSERT_EQUALS(objTypeOf(1), NumberInt);
1439
ASSERT_EQUALS(arrTypeOf(1), NumberInt);
1441
ASSERT_EQUALS(objTypeOf(OpTime()), Timestamp);
1442
ASSERT_EQUALS(arrTypeOf(OpTime()), Timestamp);
1444
ASSERT_EQUALS(objTypeOf(1LL), NumberLong);
1445
ASSERT_EQUALS(arrTypeOf(1LL), NumberLong);
1447
ASSERT_EQUALS(objTypeOf(MAXKEY), MaxKey);
1448
ASSERT_EQUALS(arrTypeOf(MAXKEY), MaxKey);
1451
template<typename T>
1452
BSONType objTypeOf(const T& thing) {
1453
return BSON("" << thing).firstElement().type();
1456
template<typename T>
1457
BSONType arrTypeOf(const T& thing) {
1458
return BSON_ARRAY(thing).firstElement().type();
1413
1461
} // namespace ValueStreamTests
1415
1463
class SubObjectBuilder {
1581
namespace external_sort {
1585
BSONObjExternalSorter sorter(indexInterfaceForTheseTests);
1587
sorter.add( BSON( "x" << 10 ) , 5 , 1);
1588
sorter.add( BSON( "x" << 2 ) , 3 , 1 );
1589
sorter.add( BSON( "x" << 5 ) , 6 , 1 );
1590
sorter.add( BSON( "x" << 5 ) , 7 , 1 );
1594
auto_ptr<BSONObjExternalSorter::Iterator> i = sorter.iterator();
1596
while ( i->more() ) {
1597
pair<BSONObj,DiskLoc> p = i->next();
1599
verify( p.first["x"].number() == 2 );
1600
else if ( num <= 2 ) {
1601
verify( p.first["x"].number() == 5 );
1603
else if ( num == 3 )
1604
verify( p.first["x"].number() == 10 );
1611
ASSERT_EQUALS( 0 , sorter.numFiles() );
1618
BSONObjExternalSorter sorter( indexInterfaceForTheseTests, BSONObj() , 10 );
1619
sorter.add( BSON( "x" << 10 ) , 5 , 11 );
1620
sorter.add( BSON( "x" << 2 ) , 3 , 1 );
1621
sorter.add( BSON( "x" << 5 ) , 6 , 1 );
1622
sorter.add( BSON( "x" << 5 ) , 7 , 1 );
1626
auto_ptr<BSONObjExternalSorter::Iterator> i = sorter.iterator();
1628
while ( i->more() ) {
1629
pair<BSONObj,DiskLoc> p = i->next();
1631
verify( p.first["x"].number() == 2 );
1632
ASSERT_EQUALS( p.second.toString() , "3:1" );
1634
else if ( num <= 2 )
1635
verify( p.first["x"].number() == 5 );
1636
else if ( num == 3 ) {
1637
verify( p.first["x"].number() == 10 );
1638
ASSERT_EQUALS( p.second.toString() , "5:b" );
1651
BSONObjExternalSorter sorter( indexInterfaceForTheseTests, BSONObj() , 10 );
1654
auto_ptr<BSONObjExternalSorter::Iterator> i = sorter.iterator();
1655
verify( ! i->more() );
1664
BSONObjExternalSorter sorter(indexInterfaceForTheseTests);
1665
sorter.add( BSON( "x" << 10 ) , 5 , 4);
1666
sorter.add( BSON( "x" << 2 ) , 3 , 0 );
1667
sorter.add( BSON( "x" << 5 ) , 6 , 2 );
1668
sorter.add( BSON( "x" << 5 ) , 7 , 3 );
1669
sorter.add( BSON( "x" << 5 ) , 2 , 1 );
1673
auto_ptr<BSONObjExternalSorter::Iterator> i = sorter.iterator();
1675
while ( i->more() ) {
1676
pair<BSONObj,DiskLoc> p = i->next();
1678
verify( p.first["x"].number() == 2 );
1679
else if ( num <= 3 ) {
1680
verify( p.first["x"].number() == 5 );
1682
else if ( num == 4 )
1683
verify( p.first["x"].number() == 10 );
1686
ASSERT_EQUALS( num , p.second.getOfs() );
1698
BSONObjExternalSorter sorter( indexInterfaceForTheseTests, BSONObj() , 2000 );
1699
for ( int i=0; i<10000; i++ ) {
1700
sorter.add( BSON( "x" << rand() % 10000 ) , 5 , i );
1705
auto_ptr<BSONObjExternalSorter::Iterator> i = sorter.iterator();
1708
while ( i->more() ) {
1709
pair<BSONObj,DiskLoc> p = i->next();
1711
double cur = p.first["x"].number();
1712
verify( cur >= prev );
1715
verify( num == 10000 );
1722
const int total = 100000;
1723
BSONObjExternalSorter sorter( indexInterfaceForTheseTests, BSONObj() , total * 2 );
1724
for ( int i=0; i<total; i++ ) {
1725
sorter.add( BSON( "a" << "b" ) , 5 , i );
1730
auto_ptr<BSONObjExternalSorter::Iterator> i = sorter.iterator();
1733
while ( i->more() ) {
1734
pair<BSONObj,DiskLoc> p = i->next();
1736
double cur = p.first["x"].number();
1737
verify( cur >= prev );
1740
verify( num == total );
1741
ASSERT( sorter.numFiles() > 2 );
1749
const int total = 1000 * 1000;
1750
BSONObjExternalSorter sorter( indexInterfaceForTheseTests, BSONObj() , total * 2 );
1751
for ( int i=0; i<total; i++ ) {
1752
sorter.add( BSON( "abcabcabcabd" << "basdasdasdasdasdasdadasdasd" << "x" << i ) , 5 , i );
1757
auto_ptr<BSONObjExternalSorter::Iterator> i = sorter.iterator();
1760
while ( i->more() ) {
1761
pair<BSONObj,DiskLoc> p = i->next();
1763
double cur = p.first["x"].number();
1764
verify( cur >= prev );
1767
verify( num == total );
1768
ASSERT( sorter.numFiles() > 2 );
1779
BSONObj x = b.obj();
1781
BSONObjExternalSorter sorter(indexInterfaceForTheseTests);
1782
sorter.add(x, DiskLoc(3,7));
1783
sorter.add(x, DiskLoc(4,7));
1784
sorter.add(x, DiskLoc(2,7));
1785
sorter.add(x, DiskLoc(1,7));
1786
sorter.add(x, DiskLoc(3,77));
1790
auto_ptr<BSONObjExternalSorter::Iterator> i = sorter.iterator();
1791
while( i->more() ) {
1792
BSONObjExternalSorter::Data d = i->next();
1793
/*cout << d.second.toString() << endl;
1794
cout << d.first.objsize() << endl;
1795
cout<<"SORTER next:" << d.first.toString() << endl;*/
1801
1629
class CompatBSON {