@@ -23,8 +23,8 @@ import (
23
23
"github.com/stretchr/testify/require"
24
24
"github.com/tidwall/pretty"
25
25
"go.mongodb.org/mongo-driver/bson/bsoncodec"
26
- "go.mongodb.org/mongo-driver/bson/bsonrw"
27
26
"go.mongodb.org/mongo-driver/bson/primitive"
27
+ "go.mongodb.org/mongo-driver/internal/testutil/assert"
28
28
)
29
29
30
30
type testCase struct {
@@ -59,7 +59,7 @@ type parseErrorTestCase struct {
59
59
String string `json:"string"`
60
60
}
61
61
62
- const dataDir = "../data"
62
+ const dataDir = "../data/bson-corpus/ "
63
63
64
64
var dvd bsoncodec.DefaultValueDecoders
65
65
var dve bsoncodec.DefaultValueEncoders
@@ -354,19 +354,14 @@ func runTest(t *testing.T, file string) {
354
354
355
355
s := unescapeUnicode (p .String , test .BsonType )
356
356
if test .BsonType == "0x13" {
357
- s = fmt .Sprintf (`{"$numberDecimal": "%s"}` , s )
357
+ s = fmt .Sprintf (`{"decimal128": {" $numberDecimal": "%s"} }` , s )
358
358
}
359
359
360
360
switch test .BsonType {
361
- case "0x00" :
361
+ case "0x00" , "0x05" , "0x13" :
362
362
var doc D
363
363
err := UnmarshalExtJSON ([]byte (s ), true , & doc )
364
364
expectError (t , err , fmt .Sprintf ("%s: expected parse error" , p .Description ))
365
- case "0x13" :
366
- ejvr , err := bsonrw .NewExtJSONValueReader (strings .NewReader (s ), true )
367
- expectNoError (t , err , fmt .Sprintf ("error creating value reader: %s" , err ))
368
- _ , err = ejvr .ReadDecimal128 ()
369
- expectError (t , err , fmt .Sprintf ("%s: expected parse error" , p .Description ))
370
365
default :
371
366
t .Errorf ("Update test to check for parse errors for type %s" , test .BsonType )
372
367
t .Fail ()
@@ -398,3 +393,61 @@ func expectError(t *testing.T, err error, desc string) {
398
393
t .FailNow ()
399
394
}
400
395
}
396
+
397
+ func TestRelaxedUUIDValidation (t * testing.T ) {
398
+ testCases := []struct {
399
+ description string
400
+ canonicalExtJSON string
401
+ degenerateExtJSON string
402
+ expectedErr string
403
+ }{
404
+ {
405
+ "valid uuid" ,
406
+ "{\" x\" : { \" $binary\" : {\" base64\" : \" c//SZESzTGmQ6OfR38A11A==\" , \" subType\" : \" 04\" }}}" ,
407
+ "{\" x\" : { \" $uuid\" : \" 73ffd264-44b3-4c69-90e8-e7d1dfc035d4\" }}" ,
408
+ "" ,
409
+ },
410
+ {
411
+ "invalid uuid--no hyphens" ,
412
+ "" ,
413
+ "{\" x\" : { \" $uuid\" : \" 73ffd26444b34c6990e8e7d1dfc035d4\" }}" ,
414
+ "$uuid value does not follow RFC 4122 format regarding length and hyphens" ,
415
+ },
416
+ {
417
+ "invalid uuid--trailing hyphens" ,
418
+ "" ,
419
+ "{\" x\" : { \" $uuid\" : \" 73ffd264-44b3-4c69-90e8-e7d1dfc035--\" }}" ,
420
+ "$uuid value does not follow RFC 4122 format regarding length and hyphens" ,
421
+ },
422
+ {
423
+ "invalid uuid--malformed hex" ,
424
+ "" ,
425
+ "{\" x\" : { \" $uuid\" : \" q3@fd26l-44b3-4c69-90e8-e7d1dfc035d4\" }}" ,
426
+ "$uuid value does not follow RFC 4122 format regarding hex bytes: encoding/hex: invalid byte: U+0071 'q'" ,
427
+ },
428
+ }
429
+
430
+ for _ , tc := range testCases {
431
+ t .Run (tc .description , func (t * testing.T ) {
432
+ // get canonical extended JSON
433
+ cEJ := unescapeUnicode (string (pretty .Ugly ([]byte (tc .canonicalExtJSON ))), "0x05" )
434
+
435
+ // get degenerate extended JSON
436
+ dEJ := unescapeUnicode (string (pretty .Ugly ([]byte (tc .degenerateExtJSON ))), "0x05" )
437
+
438
+ // convert dEJ to native doc
439
+ var doc D
440
+ err := UnmarshalExtJSON ([]byte (dEJ ), true , & doc )
441
+
442
+ if tc .expectedErr != "" {
443
+ assert .Equal (t , tc .expectedErr , err .Error (), "expected error %v, got %v" , tc .expectedErr , err )
444
+ } else {
445
+ assert .Nil (t , err , "expected no error, got error: %v" , err )
446
+
447
+ // Marshal doc into extended JSON and compare with cEJ
448
+ nativeToJSON (t , cEJ , doc , tc .description , "degenerate canonical" , "cEJ" , "json_to_native(dEJ)" )
449
+ }
450
+ })
451
+ }
452
+
453
+ }
0 commit comments