1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
|
// jsontests.cpp - Tests for json.{h,cpp} code and BSONObj::jsonString()
//
/**
* Copyright (C) 2008 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../db/jsobj.h"
#include "../db/json.h"
#include "dbtests.h"
#include <limits>
namespace JsonTests {
namespace JsonStringTests {
class Empty {
public:
void run() {
ASSERT_EQUALS( "{}", BSONObj().jsonString( Strict ) );
}
};
class SingleStringMember {
public:
void run() {
ASSERT_EQUALS( "{ \"a\" : \"b\" }", BSON( "a" << "b" ).jsonString( Strict ) );
}
};
class EscapedCharacters {
public:
void run() {
BSONObjBuilder b;
b.append( "a", "\" \\ / \b \f \n \r \t" );
ASSERT_EQUALS( "{ \"a\" : \"\\\" \\\\ \\/ \\b \\f \\n \\r \\t\" }", b.done().jsonString( Strict ) );
}
};
// per http://www.ietf.org/rfc/rfc4627.txt, control characters are
// (U+0000 through U+001F). U+007F is not mentioned as a control character.
class AdditionalControlCharacters {
public:
void run() {
BSONObjBuilder b;
b.append( "a", "\x1 \x1f" );
ASSERT_EQUALS( "{ \"a\" : \"\\u0001 \\u001f\" }", b.done().jsonString( Strict ) );
}
};
class ExtendedAscii {
public:
void run() {
BSONObjBuilder b;
b.append( "a", "\x80" );
ASSERT_EQUALS( "{ \"a\" : \"\x80\" }", b.done().jsonString( Strict ) );
}
};
class EscapeFieldName {
public:
void run() {
BSONObjBuilder b;
b.append( "\t", "b" );
ASSERT_EQUALS( "{ \"\\t\" : \"b\" }", b.done().jsonString( Strict ) );
}
};
class SingleIntMember {
public:
void run() {
BSONObjBuilder b;
b.append( "a", 1 );
ASSERT_EQUALS( "{ \"a\" : 1 }", b.done().jsonString( Strict ) );
}
};
class SingleNumberMember {
public:
void run() {
BSONObjBuilder b;
b.append( "a", 1.5 );
ASSERT_EQUALS( "{ \"a\" : 1.5 }", b.done().jsonString( Strict ) );
}
};
class InvalidNumbers {
public:
void run() {
BSONObjBuilder b;
b.append( "a", numeric_limits< double >::infinity() );
ASSERT_EXCEPTION( b.done().jsonString( Strict ), AssertionException );
BSONObjBuilder c;
c.append( "a", numeric_limits< double >::quiet_NaN() );
ASSERT_EXCEPTION( c.done().jsonString( Strict ), AssertionException );
BSONObjBuilder d;
d.append( "a", numeric_limits< double >::signaling_NaN() );
ASSERT_EXCEPTION( d.done().jsonString( Strict ), AssertionException );
}
};
class NumberPrecision {
public:
void run() {
BSONObjBuilder b;
b.append( "a", 123456789 );
ASSERT_EQUALS( "{ \"a\" : 123456789 }", b.done().jsonString( Strict ) );
}
};
class NegativeNumber {
public:
void run() {
BSONObjBuilder b;
b.append( "a", -1 );
ASSERT_EQUALS( "{ \"a\" : -1 }", b.done().jsonString( Strict ) );
}
};
class SingleBoolMember {
public:
void run() {
BSONObjBuilder b;
b.appendBool( "a", true );
ASSERT_EQUALS( "{ \"a\" : true }", b.done().jsonString( Strict ) );
BSONObjBuilder c;
c.appendBool( "a", false );
ASSERT_EQUALS( "{ \"a\" : false }", c.done().jsonString( Strict ) );
}
};
class SingleNullMember {
public:
void run() {
BSONObjBuilder b;
b.appendNull( "a" );
ASSERT_EQUALS( "{ \"a\" : null }", b.done().jsonString( Strict ) );
}
};
class SingleObjectMember {
public:
void run() {
BSONObjBuilder b, c;
b.append( "a", c.done() );
ASSERT_EQUALS( "{ \"a\" : {} }", b.done().jsonString( Strict ) );
}
};
class TwoMembers {
public:
void run() {
BSONObjBuilder b;
b.append( "a", 1 );
b.append( "b", 2 );
ASSERT_EQUALS( "{ \"a\" : 1, \"b\" : 2 }", b.done().jsonString( Strict ) );
}
};
class EmptyArray {
public:
void run() {
vector< int > arr;
BSONObjBuilder b;
b.append( "a", arr );
ASSERT_EQUALS( "{ \"a\" : [] }", b.done().jsonString( Strict ) );
}
};
class Array {
public:
void run() {
vector< int > arr;
arr.push_back( 1 );
arr.push_back( 2 );
BSONObjBuilder b;
b.append( "a", arr );
ASSERT_EQUALS( "{ \"a\" : [ 1, 2 ] }", b.done().jsonString( Strict ) );
}
};
class DBRef {
public:
void run() {
OID oid;
memset( &oid, 0xff, 12 );
BSONObjBuilder b;
b.appendDBRef( "a", "namespace", oid );
BSONObj built = b.done();
ASSERT_EQUALS( "{ \"a\" : { \"$ns\" : \"namespace\", \"$id\" : \"ffffffffffffffffffffffff\" } }",
built.jsonString( Strict ) );
ASSERT_EQUALS( "{ \"a\" : { \"$ns\" : \"namespace\", \"$id\" : \"ffffffffffffffffffffffff\" } }",
built.jsonString( JS ) );
ASSERT_EQUALS( "{ \"a\" : Dbref( \"namespace\", \"ffffffffffffffffffffffff\" ) }",
built.jsonString( TenGen ) );
}
};
class DBRefZero {
public:
void run() {
OID oid;
memset( &oid, 0, 12 );
BSONObjBuilder b;
b.appendDBRef( "a", "namespace", oid );
ASSERT_EQUALS( "{ \"a\" : { \"$ns\" : \"namespace\", \"$id\" : \"000000000000000000000000\" } }",
b.done().jsonString( Strict ) );
}
};
class ObjectId {
public:
void run() {
OID oid;
memset( &oid, 0xff, 12 );
BSONObjBuilder b;
b.appendOID( "a", &oid );
BSONObj built = b.done();
ASSERT_EQUALS( "{ \"a\" : \"ffffffffffffffffffffffff\" }",
built.jsonString( Strict ) );
ASSERT_EQUALS( "{ \"a\" : ObjectId( \"ffffffffffffffffffffffff\" ) }",
built.jsonString( TenGen ) );
}
};
class BinData {
public:
void run() {
char z[ 3 ];
z[ 0 ] = 'a';
z[ 1 ] = 'b';
z[ 2 ] = 'c';
BSONObjBuilder b;
b.appendBinData( "a", 3, ByteArray, z );
ASSERT_EQUALS( "{ \"a\" : { \"$binary\" : \"YWJj\", \"$type\" : \"02\" } }",
b.done().jsonString( Strict ) );
BSONObjBuilder c;
c.appendBinData( "a", 2, ByteArray, z );
ASSERT_EQUALS( "{ \"a\" : { \"$binary\" : \"YWI=\", \"$type\" : \"02\" } }",
c.done().jsonString( Strict ) );
BSONObjBuilder d;
d.appendBinData( "a", 1, ByteArray, z );
ASSERT_EQUALS( "{ \"a\" : { \"$binary\" : \"YQ==\", \"$type\" : \"02\" } }",
d.done().jsonString( Strict ) );
}
};
class Symbol {
public:
void run() {
BSONObjBuilder b;
b.appendSymbol( "a", "b" );
ASSERT_EQUALS( "{ \"a\" : \"b\" }", b.done().jsonString( Strict ) );
}
};
class Date {
public:
void run() {
BSONObjBuilder b;
b.appendDate( "a", 0 );
BSONObj built = b.done();
ASSERT_EQUALS( "{ \"a\" : { \"$date\" : 0 } }", built.jsonString( Strict ) );
ASSERT_EQUALS( "{ \"a\" : Date( 0 ) }", built.jsonString( TenGen ) );
ASSERT_EQUALS( "{ \"a\" : Date( 0 ) }", built.jsonString( JS ) );
}
};
class Regex {
public:
void run() {
BSONObjBuilder b;
b.appendRegex( "a", "abc", "i" );
BSONObj built = b.done();
ASSERT_EQUALS( "{ \"a\" : { \"$regex\" : \"abc\", \"$options\" : \"i\" } }",
built.jsonString( Strict ) );
ASSERT_EQUALS( "{ \"a\" : /abc/i }", built.jsonString( TenGen ) );
ASSERT_EQUALS( "{ \"a\" : /abc/i }", built.jsonString( JS ) );
}
};
class RegexEscape {
public:
void run() {
BSONObjBuilder b;
b.appendRegex( "a", "/\"", "i" );
BSONObj built = b.done();
ASSERT_EQUALS( "{ \"a\" : { \"$regex\" : \"\\/\\\"\", \"$options\" : \"i\" } }",
built.jsonString( Strict ) );
ASSERT_EQUALS( "{ \"a\" : /\\/\\\"/i }", built.jsonString( TenGen ) );
ASSERT_EQUALS( "{ \"a\" : /\\/\\\"/i }", built.jsonString( JS ) );
}
};
class RegexManyOptions {
public:
void run() {
BSONObjBuilder b;
b.appendRegex( "a", "z", "abcgimx" );
BSONObj built = b.done();
ASSERT_EQUALS( "{ \"a\" : { \"$regex\" : \"z\", \"$options\" : \"abcgimx\" } }",
built.jsonString( Strict ) );
ASSERT_EQUALS( "{ \"a\" : /z/gim }", built.jsonString( TenGen ) );
ASSERT_EQUALS( "{ \"a\" : /z/gim }", built.jsonString( JS ) );
}
};
} // namespace JsonStringTests
namespace FromJsonTests {
class Base {
public:
virtual ~Base() {}
void run() {
ASSERT( fromjson( json() ).valid() );
assertEquals( bson(), fromjson( json() ) );
assertEquals( bson(), fromjson( bson().jsonString( Strict ) ) );
assertEquals( bson(), fromjson( bson().jsonString( TenGen ) ) );
assertEquals( bson(), fromjson( bson().jsonString( JS ) ) );
}
protected:
virtual BSONObj bson() const = 0;
virtual string json() const = 0;
private:
static void assertEquals( const BSONObj &expected, const BSONObj &actual ) {
if ( expected.woCompare( actual ) ) {
out() << "Expected: " << expected.toString()
<< ", got: " << actual.toString();
}
ASSERT( !expected.woCompare( actual ) );
}
};
class Bad {
public:
virtual ~Bad() {}
void run() {
ASSERT_EXCEPTION( fromjson( json() ), MsgAssertionException );
}
protected:
virtual string json() const = 0;
};
class Empty : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
return b.obj();
}
virtual string json() const {
return "{}";
}
};
class EmptyWithSpace : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
return b.obj();
}
virtual string json() const {
return "{ }";
}
};
class SingleString : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
b.append( "a", "b" );
return b.obj();
}
virtual string json() const {
return "{ \"a\" : \"b\" }";
}
};
class EmptyStrings : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
b.append( "", "" );
return b.obj();
}
virtual string json() const {
return "{ \"\" : \"\" }";
}
};
class ReservedFieldName : public Bad {
virtual string json() const {
return "{ \"$ns\" : \"b\" }";
}
};
class OkDollarFieldName : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
b.append( "$where", 1 );
return b.obj();
}
virtual string json() const {
return "{ \"$where\" : 1 }";
}
};
class SingleNumber : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
b.append( "a", 1 );
return b.obj();
}
virtual string json() const {
return "{ \"a\" : 1 }";
}
};
class FancyNumber {
public:
virtual ~FancyNumber() {}
void run() {
ASSERT_EQUALS( int( 1000000 * bson().firstElement().number() ),
int( 1000000 * fromjson( json() ).firstElement().number() ) );
}
virtual BSONObj bson() const {
BSONObjBuilder b;
b.append( "a", -4.4433e-2 );
return b.obj();
}
virtual string json() const {
return "{ \"a\" : -4.4433e-2 }";
}
};
class TwoElements : public Base {
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 Base {
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 ArrayEmpty : public Base {
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 Base {
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 Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
b.appendBool( "a", true );
return b.obj();
}
virtual string json() const {
return "{ \"a\" : true }";
}
};
class False : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
b.appendBool( "a", false );
return b.obj();
}
virtual string json() const {
return "{ \"a\" : false }";
}
};
class Null : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
b.appendNull( "a" );
return b.obj();
}
virtual string json() const {
return "{ \"a\" : null }";
}
};
class EscapedCharacters : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
b.append( "a", "\" \\ / \b \f \n \r \t" );
return b.obj();
}
virtual string json() const {
return "{ \"a\" : \"\\\" \\\\ \\/ \\b \\f \\n \\r \\t\" }";
}
};
class AllowedControlCharacter : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
b.append( "a", "\x7f" );
return b.obj();
}
virtual string json() const {
return "{ \"a\" : \"\x7f\" }";
}
};
class EscapeFieldName : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
b.append( "\n", "b" );
return b.obj();
}
virtual string json() const {
return "{ \"\\n\" : \"b\" }";
}
};
class EscapedUnicodeToUtf8 : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
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", u );
BSONObj built = b.obj();
ASSERT_EQUALS( string( u ), built.firstElement().valuestr() );
return built;
}
virtual string json() const {
return "{ \"a\" : \"\\ua000\\uA000\" }";
}
};
class Utf8AllOnes : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
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", u );
return b.obj();
}
virtual string json() const {
return "{ \"a\" : \"\\u0001\\u007f\\u07ff\\uffff\" }";
}
};
class Utf8FirstByteOnes : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
char u[ 6 ];
u[ 0 ] = 0xdc;
u[ 1 ] = 0x80;
u[ 2 ] = 0xef;
u[ 3 ] = 0xbc;
u[ 4 ] = 0x80;
u[ 5 ] = 0;
b.append( "a", u );
return b.obj();
}
virtual string json() const {
return "{ \"a\" : \"\\u0700\\uff00\" }";
}
};
class DBRef : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
OID o;
memset( &o, 0, 12 );
b.appendDBRef( "a", "foo", o );
return b.obj();
}
// NOTE Testing other formats handled by by Base class.
virtual string json() const {
return "{ \"a\" : { \"$ns\" : \"foo\", \"$id\" : \"000000000000000000000000\" } }";
}
};
class Oid : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
b.appendOID( "_id" );
return b.obj();
}
virtual string json() const {
return "{ \"_id\" : \"000000000000000000000000\" }";
}
};
class Oid2 : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
OID o;
memset( &o, 0x0f, 12 );
b.appendOID( "_id", &o );
return b.obj();
}
virtual string json() const {
return "{ \"_id\" : \"0f0f0f0f0f0f0f0f0f0f0f0f\" }";
}
};
class BinData : public Base {
virtual BSONObj bson() const {
char z[ 3 ];
z[ 0 ] = 'a';
z[ 1 ] = 'b';
z[ 2 ] = 'c';
BSONObjBuilder b;
b.appendBinData( "a", 3, ByteArray, z );
return b.obj();
}
virtual string json() const {
return "{ \"a\" : { \"$binary\" : \"YWJj\", \"$type\" : \"02\" } }";
}
};
class BinDataPaddedSingle : public Base {
virtual BSONObj bson() const {
char z[ 2 ];
z[ 0 ] = 'a';
z[ 1 ] = 'b';
BSONObjBuilder b;
b.appendBinData( "a", 2, ByteArray, z );
return b.obj();
}
virtual string json() const {
return "{ \"a\" : { \"$binary\" : \"YWI=\", \"$type\" : \"02\" } }";
}
};
class BinDataPaddedDouble : public Base {
virtual BSONObj bson() const {
char z[ 1 ];
z[ 0 ] = 'a';
BSONObjBuilder b;
b.appendBinData( "a", 1, ByteArray, z );
return b.obj();
}
virtual string json() const {
return "{ \"a\" : { \"$binary\" : \"YQ==\", \"$type\" : \"02\" } }";
}
};
class BinDataAllChars : public Base {
virtual BSONObj bson() const {
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, ByteArray, z );
return b.obj();
}
virtual string json() const {
return "{ \"a\" : { \"$binary\" : \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\", \"$type\" : \"02\" } }";
}
};
class Date : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
b.appendDate( "a", 0 );
return b.obj();
}
virtual string json() const {
return "{ \"a\" : { \"$date\" : 0 } }";
}
};
class DateNonzero : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
b.appendDate( "a", 100 );
return b.obj();
}
virtual string json() const {
return "{ \"a\" : { \"$date\" : 100 } }";
}
};
class DateTooLong : public Bad {
virtual string json() const {
stringstream ss;
ss << "{ \"a\" : { \"$date\" : " << ~(0LL) << "0" << " } }";
return ss.str();
}
};
class Regex : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
b.appendRegex( "a", "b", "i" );
return b.obj();
}
virtual string json() const {
return "{ \"a\" : { \"$regex\" : \"b\", \"$options\" : \"i\" } }";
}
};
class RegexEscape : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
b.appendRegex( "a", "\t", "i" );
return b.obj();
}
virtual string json() const {
return "{ \"a\" : { \"$regex\" : \"\\t\", \"$options\" : \"i\" } }";
}
};
class RegexWithQuotes : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
b.appendRegex( "a", "\"", "" );
return b.obj();
}
virtual string json() const {
return "{ \"a\" : /\"/ }";
}
};
class RegexInvalidOption : public Bad {
virtual string json() const {
return "{ \"a\" : { \"$regex\" : \"b\", \"$options\" : \"1\" } }";
}
};
class RegexInvalidOption2 : public Bad {
virtual string json() const {
return "{ \"a\" : /b/c }";
}
};
class Malformed : public Bad {
string json() const {
return "{";
}
};
class UnquotedFieldName : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
b.append( "a_b", 1 );
return b.obj();
}
virtual string json() const {
return "{ a_b : 1 }";
}
};
class UnquotedFieldNameDollar : public Base {
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 Base {
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 Base {
virtual BSONObj bson() const {
OID id;
id.init( "deadbeeff00ddeadbeeff00d" );
BSONObjBuilder b;
b.appendOID( "_id", &id );
return b.obj();
}
virtual string json() const {
return "{ \"_id\": ObjectId( \"deadbeeff00ddeadbeeff00d\" ) }";
}
};
} // namespace FromJsonTests
class All : public Suite {
public:
All() {
add< JsonStringTests::Empty >();
add< JsonStringTests::SingleStringMember >();
add< JsonStringTests::EscapedCharacters >();
add< JsonStringTests::AdditionalControlCharacters >();
add< JsonStringTests::ExtendedAscii >();
add< JsonStringTests::EscapeFieldName >();
add< JsonStringTests::SingleIntMember >();
add< JsonStringTests::SingleNumberMember >();
add< JsonStringTests::InvalidNumbers >();
add< JsonStringTests::NumberPrecision >();
add< JsonStringTests::NegativeNumber >();
add< JsonStringTests::SingleBoolMember >();
add< JsonStringTests::SingleNullMember >();
add< JsonStringTests::SingleObjectMember >();
add< JsonStringTests::TwoMembers >();
add< JsonStringTests::EmptyArray >();
add< JsonStringTests::Array >();
add< JsonStringTests::DBRef >();
add< JsonStringTests::DBRefZero >();
add< JsonStringTests::ObjectId >();
add< JsonStringTests::BinData >();
add< JsonStringTests::Symbol >();
add< JsonStringTests::Date >();
add< JsonStringTests::Regex >();
add< JsonStringTests::RegexEscape >();
add< JsonStringTests::RegexManyOptions >();
add< FromJsonTests::Empty >();
add< FromJsonTests::EmptyWithSpace >();
add< FromJsonTests::SingleString >();
add< FromJsonTests::EmptyStrings >();
add< FromJsonTests::ReservedFieldName >();
add< FromJsonTests::OkDollarFieldName >();
add< FromJsonTests::SingleNumber >();
add< FromJsonTests::FancyNumber >();
add< FromJsonTests::TwoElements >();
add< FromJsonTests::Subobject >();
add< FromJsonTests::ArrayEmpty >();
add< FromJsonTests::Array >();
add< FromJsonTests::True >();
add< FromJsonTests::False >();
add< FromJsonTests::Null >();
add< FromJsonTests::EscapedCharacters >();
add< FromJsonTests::AllowedControlCharacter >();
add< FromJsonTests::EscapeFieldName >();
add< FromJsonTests::EscapedUnicodeToUtf8 >();
add< FromJsonTests::Utf8AllOnes >();
add< FromJsonTests::Utf8FirstByteOnes >();
add< FromJsonTests::DBRef >();
add< FromJsonTests::Oid >();
add< FromJsonTests::Oid2 >();
add< FromJsonTests::BinData >();
add< FromJsonTests::BinDataPaddedSingle >();
add< FromJsonTests::BinDataPaddedDouble >();
add< FromJsonTests::BinDataAllChars >();
add< FromJsonTests::Date >();
add< FromJsonTests::DateNonzero >();
add< FromJsonTests::DateTooLong >();
add< FromJsonTests::Regex >();
add< FromJsonTests::RegexEscape >();
add< FromJsonTests::RegexWithQuotes >();
add< FromJsonTests::RegexInvalidOption >();
add< FromJsonTests::RegexInvalidOption2 >();
add< FromJsonTests::Malformed >();
add< FromJsonTests::UnquotedFieldName >();
add< FromJsonTests::UnquotedFieldNameDollar >();
add< FromJsonTests::SingleQuotes >();
add< FromJsonTests::ObjectId >();
}
};
} // namespace JsonTests
UnitTest::TestPtr jsonTests() {
return UnitTest::createSuite< JsonTests::All >();
}
|