@@ -426,7 +426,9 @@ public static IEnumerable<object[]> ThrowsForInvalidPrimitiveType_Arguments()
426
426
{
427
427
foreach ( byte binaryType in new byte [ ] { ( byte ) 0 /* BinaryType.Primitive */ , ( byte ) 7 /* BinaryType.PrimitiveArray */ } )
428
428
{
429
+ yield return new object [ ] { recordType , binaryType , ( byte ) 0 } ; // value not used by the spec
429
430
yield return new object [ ] { recordType , binaryType , ( byte ) 4 } ; // value not used by the spec
431
+ yield return new object [ ] { recordType , binaryType , ( byte ) 17 } ; // used by the spec, but illegal in given context
430
432
yield return new object [ ] { recordType , binaryType , ( byte ) 19 } ;
431
433
}
432
434
}
@@ -478,4 +480,109 @@ public void ThrowsOnInvalidArrayType()
478
480
stream . Position = 0 ;
479
481
Assert . Throws < SerializationException > ( ( ) => NrbfDecoder . Decode ( stream ) ) ;
480
482
}
483
+
484
+ [ Theory ]
485
+ [ InlineData ( 18 , typeof ( NotSupportedException ) ) ] // not part of the spec, but still less than max allowed value (22)
486
+ [ InlineData ( 19 , typeof ( NotSupportedException ) ) ] // same as above
487
+ [ InlineData ( 20 , typeof ( NotSupportedException ) ) ] // same as above
488
+ [ InlineData ( 23 , typeof ( SerializationException ) ) ] // not part of the spec and more than max allowed value (22)
489
+ [ InlineData ( 64 , typeof ( SerializationException ) ) ] // same as above but also matches AllowedRecordTypes.SerializedStreamHeader
490
+ public void InvalidSerializationRecordType ( byte recordType , Type expectedException )
491
+ {
492
+ using MemoryStream stream = new ( ) ;
493
+ BinaryWriter writer = new ( stream , Encoding . UTF8 ) ;
494
+
495
+ WriteSerializedStreamHeader ( writer ) ;
496
+ writer . Write ( recordType ) ; // SerializationRecordType
497
+ writer . Write ( ( byte ) SerializationRecordType . MessageEnd ) ;
498
+
499
+ stream . Position = 0 ;
500
+
501
+ Assert . Throws ( expectedException , ( ) => NrbfDecoder . Decode ( stream ) ) ;
502
+ }
503
+
504
+ [ Fact ]
505
+ public void MissingRootRecord ( )
506
+ {
507
+ const int RootRecordId = 1 ;
508
+ using MemoryStream stream = new ( ) ;
509
+ BinaryWriter writer = new ( stream , Encoding . UTF8 ) ;
510
+
511
+ WriteSerializedStreamHeader ( writer , rootId : RootRecordId ) ;
512
+ writer . Write ( ( byte ) SerializationRecordType . BinaryObjectString ) ;
513
+ writer . Write ( RootRecordId + 1 ) ; // a different ID
514
+ writer . Write ( "theString" ) ;
515
+ writer . Write ( ( byte ) SerializationRecordType . MessageEnd ) ;
516
+
517
+ stream . Position = 0 ;
518
+
519
+ Assert . Throws < SerializationException > ( ( ) => NrbfDecoder . Decode ( stream ) ) ;
520
+ }
521
+
522
+ [ Fact ]
523
+ public void Invalid7BitEncodedStringLength ( )
524
+ {
525
+ // The highest bit of the last byte is set (so it's invalid).
526
+ byte [ ] invalidLength = [ byte . MaxValue , byte . MaxValue , byte . MaxValue , byte . MaxValue , byte . MaxValue ] ;
527
+
528
+ using MemoryStream stream = new ( ) ;
529
+ BinaryWriter writer = new ( stream , Encoding . UTF8 ) ;
530
+
531
+ WriteSerializedStreamHeader ( writer ) ;
532
+ writer . Write ( ( byte ) SerializationRecordType . BinaryObjectString ) ;
533
+ writer . Write ( 1 ) ; // root record Id
534
+ writer . Write ( invalidLength ) ; // the length prefix
535
+ writer . Write ( Encoding . UTF8 . GetBytes ( "theString" ) ) ;
536
+ writer . Write ( ( byte ) SerializationRecordType . MessageEnd ) ;
537
+
538
+ stream . Position = 0 ;
539
+
540
+ Assert . Throws < SerializationException > ( ( ) => NrbfDecoder . Decode ( stream ) ) ;
541
+ }
542
+
543
+ [ Theory ]
544
+ [ InlineData ( "79228162514264337593543950336" ) ] // invalid format (decimal.MaxValue + 1)
545
+ [ InlineData ( "1111111111111111111111111111111111111111111111111" ) ] // overflow
546
+ public void InvalidDecimal ( string textRepresentation )
547
+ {
548
+ using MemoryStream stream = new ( ) ;
549
+ BinaryWriter writer = new ( stream , Encoding . UTF8 ) ;
550
+
551
+ WriteSerializedStreamHeader ( writer ) ;
552
+ writer . Write ( ( byte ) SerializationRecordType . SystemClassWithMembersAndTypes ) ;
553
+ writer . Write ( 1 ) ; // root record Id
554
+ writer . Write ( "ClassWithDecimalField" ) ; // type name
555
+ writer . Write ( 1 ) ; // member count
556
+ writer . Write ( "memberName" ) ;
557
+ writer . Write ( ( byte ) BinaryType . Primitive ) ;
558
+ writer . Write ( ( byte ) PrimitiveType . Decimal ) ;
559
+ writer . Write ( textRepresentation ) ;
560
+ writer . Write ( ( byte ) SerializationRecordType . MessageEnd ) ;
561
+
562
+ stream . Position = 0 ;
563
+
564
+ Assert . Throws < SerializationException > ( ( ) => NrbfDecoder . Decode ( stream ) ) ;
565
+ }
566
+
567
+ [ Fact ]
568
+ public void SurrogateCharacter ( )
569
+ {
570
+ using MemoryStream stream = new ( ) ;
571
+ BinaryWriter writer = new ( stream , Encoding . UTF8 ) ;
572
+
573
+ WriteSerializedStreamHeader ( writer ) ;
574
+ writer . Write ( ( byte ) SerializationRecordType . SystemClassWithMembersAndTypes ) ;
575
+ writer . Write ( 1 ) ; // root record Id
576
+ writer . Write ( "ClassWithCharField" ) ; // type name
577
+ writer . Write ( 1 ) ; // member count
578
+ writer . Write ( "memberName" ) ;
579
+ writer . Write ( ( byte ) BinaryType . Primitive ) ;
580
+ writer . Write ( ( byte ) PrimitiveType . Char ) ;
581
+ writer . Write ( ( byte ) 0xC0 ) ; // a surrogate character
582
+ writer . Write ( ( byte ) SerializationRecordType . MessageEnd ) ;
583
+
584
+ stream . Position = 0 ;
585
+
586
+ Assert . Throws < SerializationException > ( ( ) => NrbfDecoder . Decode ( stream ) ) ;
587
+ }
481
588
}
0 commit comments