8
8
#endif
9
9
10
10
11
+ #define IS_NAN (dec ) (dec).high == 0x7c00000000000000ull
12
+
13
+
11
14
typedef struct {
12
15
const char * scenario ;
13
16
const char * test ;
@@ -54,6 +57,22 @@ compare_data (const uint8_t *a,
54
57
}
55
58
56
59
60
+ static bool
61
+ is_test_skipped (const char * scenario , const char * description )
62
+ {
63
+ skipped_corpus_test_t * skip ;
64
+
65
+ for (skip = SKIPPED_CORPUS_TESTS ; skip -> scenario != NULL ; skip ++ ) {
66
+ if (!strcmp (skip -> scenario , scenario ) &&
67
+ !strcmp (skip -> test , description )) {
68
+ return true;
69
+ }
70
+ }
71
+
72
+ return false;
73
+ }
74
+
75
+
57
76
/*
58
77
See:
59
78
github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.rst
@@ -78,9 +97,8 @@ github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.rst
78
97
79
98
*/
80
99
static void
81
- test_bson_corpus ( test_bson_type_t * test )
100
+ test_bson_corpus_valid ( test_bson_valid_type_t * test )
82
101
{
83
- skipped_corpus_test_t * skip ;
84
102
bson_t cB ;
85
103
bson_t dB ;
86
104
bson_t * decode_cE ;
@@ -91,16 +109,13 @@ test_bson_corpus (test_bson_type_t *test)
91
109
BSON_ASSERT (test -> cB );
92
110
BSON_ASSERT (test -> cE );
93
111
94
- for (skip = SKIPPED_CORPUS_TESTS ; skip -> scenario != NULL ; skip ++ ) {
95
- if (!strcmp (skip -> scenario , test -> scenario_description ) &&
96
- !strcmp (skip -> test , test -> test_description )) {
97
- if (test_suite_debug_output ()) {
98
- printf (" SKIP\n" );
99
- fflush (stdout );
100
- }
101
-
102
- return ;
112
+ if (is_test_skipped (test -> scenario_description , test -> test_description )) {
113
+ if (test_suite_debug_output ()) {
114
+ printf (" SKIP\n" );
115
+ fflush (stdout );
103
116
}
117
+
118
+ return ;
104
119
}
105
120
106
121
BSON_ASSERT (bson_init_static (& cB , test -> cB , test -> cB_len ));
@@ -158,71 +173,77 @@ test_bson_corpus (test_bson_type_t *test)
158
173
}
159
174
160
175
176
+ /*
177
+ See:
178
+ github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.rst
179
+ #testing-decode-errors
180
+ */
161
181
static void
162
- test_bson_corpus_cb ( bson_t * scenario )
182
+ test_bson_corpus_decode_error ( test_bson_decode_error_type_t * test )
163
183
{
164
- bson_iter_t iter ;
165
- bson_iter_t inner_iter ;
166
184
bson_t invalid_bson ;
167
185
168
- /* test valid BSON and Extended JSON */
169
- corpus_test (scenario , test_bson_corpus );
170
-
171
- /* test invalid BSON */
172
- if (bson_iter_init_find (& iter , scenario , "decodeErrors" )) {
173
- bson_iter_recurse (& iter , & inner_iter );
174
- while (bson_iter_next (& inner_iter )) {
175
- bson_iter_t test ;
176
- const char * description ;
177
- uint8_t * bson_str = NULL ;
178
- uint32_t bson_str_len = 0 ;
179
-
180
- bson_iter_recurse (& inner_iter , & test );
181
- while (bson_iter_next (& test )) {
182
- if (!strcmp (bson_iter_key (& test ), "description" )) {
183
- description = bson_iter_utf8 (& test , NULL );
184
- corpus_test_print_description (description );
185
- }
186
-
187
- if (!strcmp (bson_iter_key (& test ), "bson" )) {
188
- bson_str = corpus_test_unhexlify (& test , & bson_str_len );
189
- }
190
- }
191
-
192
- ASSERT (bson_str );
193
- ASSERT (!bson_init_static (& invalid_bson , bson_str , bson_str_len ) ||
194
- bson_empty (& invalid_bson ) ||
195
- !bson_as_canonical_json (& invalid_bson , NULL ));
196
-
197
- bson_free (bson_str );
186
+ BSON_ASSERT (test -> bson );
187
+
188
+ if (is_test_skipped (test -> scenario_description , test -> test_description )) {
189
+ if (test_suite_debug_output ()) {
190
+ printf (" SKIP\n" );
191
+ fflush (stdout );
198
192
}
193
+
194
+ return ;
199
195
}
200
196
201
- /* test invalid JSON */
202
- if (bson_iter_init_find (& iter , scenario , "parseErrors" )) {
203
- bson_iter_recurse (& iter , & inner_iter );
204
- while (bson_iter_next (& inner_iter )) {
205
- bson_iter_t test ;
206
- const char * description = NULL ;
207
- const char * input = NULL ;
208
- uint32_t tmp = 0 ;
209
-
210
- bson_iter_recurse (& inner_iter , & test );
211
- while (bson_iter_next (& test )) {
212
- if (!strcmp (bson_iter_key (& test ), "description" )) {
213
- description = bson_iter_utf8 (& test , NULL );
214
- corpus_test_print_description (description );
215
- }
216
-
217
- if (!strcmp (bson_iter_key (& test ), "string" )) {
218
- input = bson_iter_utf8 (& test , & tmp );
219
- }
220
- }
221
-
222
- ASSERT (input );
223
- ASSERT (!bson_new_from_json ((uint8_t * ) input , tmp , NULL ));
197
+ ASSERT (test -> bson );
198
+ ASSERT (!bson_init_static (& invalid_bson , test -> bson , test -> bson_len ) ||
199
+ bson_empty (& invalid_bson ) ||
200
+ !bson_as_canonical_json (& invalid_bson , NULL ));
201
+ }
202
+
203
+
204
+ /*
205
+ See:
206
+ github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.rst
207
+ #testing-parsing-errors
208
+ */
209
+ static void
210
+ test_bson_corpus_parse_error (test_bson_parse_error_type_t * test )
211
+ {
212
+ BSON_ASSERT (test -> str );
213
+
214
+ if (is_test_skipped (test -> scenario_description , test -> test_description )) {
215
+ if (test_suite_debug_output ()) {
216
+ printf (" SKIP\n" );
217
+ fflush (stdout );
224
218
}
219
+
220
+ return ;
225
221
}
222
+
223
+ switch (test -> bson_type ) {
224
+ case 0x00 : /* top-level document to be parsed as JSON */
225
+ ASSERT (!bson_new_from_json ((uint8_t * ) test -> str , test -> str_len , NULL ));
226
+ break ;
227
+ case BSON_TYPE_DECIMAL128 : {
228
+ bson_decimal128_t dec ;
229
+ ASSERT (!bson_decimal128_from_string (test -> str , & dec ));
230
+ ASSERT (IS_NAN (dec ));
231
+ break ;
232
+ }
233
+ default :
234
+ fprintf (stderr , "Unsupported parseError type: %#x\n" , test -> bson_type );
235
+ abort ();
236
+ }
237
+ }
238
+
239
+
240
+ static void
241
+ test_bson_corpus_cb (bson_t * scenario )
242
+ {
243
+ corpus_test (scenario ,
244
+ test_bson_corpus_valid ,
245
+ test_bson_corpus_decode_error ,
246
+ test_bson_corpus_parse_error );
226
247
}
227
248
228
249
void
0 commit comments