@@ -22,16 +22,36 @@ public partial class BinaryFormatUtilitiesTests : IDisposable
22
22
private void WriteObjectToStream ( object value , bool restrictSerialization = false ) =>
23
23
Utilities . WriteObjectToStream ( _stream , value , restrictSerialization ) ;
24
24
25
- private object ? ReadObjectFromStream ( bool restrictDeserialization = false )
25
+ private object ? ReadObjectFromStream ( )
26
26
{
27
27
_stream . Position = 0 ;
28
- return Utilities . ReadObjectFromStream < object > ( _stream , restrictDeserialization , resolver : null , legacyMode : true ) ;
28
+ return Utilities . ReadObjectFromStream < object > ( _stream , resolver : null , legacyMode : true ) ;
29
+ }
30
+
31
+ private object ? ReadRestrictedObjectFromStream ( )
32
+ {
33
+ _stream . Position = 0 ;
34
+ return Utilities . ReadRestrictedObjectFromStream < object > ( _stream , resolver : null , legacyMode : true ) ;
29
35
}
30
36
31
37
private object ? ReadObjectFromStream < T > ( bool restrictDeserialization , Func < TypeName , Type > ? resolver )
32
38
{
33
39
_stream . Position = 0 ;
34
- return Utilities . ReadObjectFromStream < T > ( _stream , restrictDeserialization , resolver , legacyMode : false ) ;
40
+ return restrictDeserialization
41
+ ? Utilities . ReadRestrictedObjectFromStream < T > ( _stream , resolver , legacyMode : false )
42
+ : Utilities . ReadObjectFromStream < T > ( _stream , resolver , legacyMode : false ) ;
43
+ }
44
+
45
+ private object ? ReadObjectFromStream < T > ( Func < TypeName , Type > ? resolver )
46
+ {
47
+ _stream . Position = 0 ;
48
+ return Utilities . ReadObjectFromStream < T > ( _stream , resolver , legacyMode : false ) ;
49
+ }
50
+
51
+ private object ? ReadRestrictedObjectFromStream < T > ( Func < TypeName , Type > ? resolver )
52
+ {
53
+ _stream . Position = 0 ;
54
+ return Utilities . ReadRestrictedObjectFromStream < T > ( _stream , resolver , legacyMode : false ) ;
35
55
}
36
56
37
57
private object ? RoundTripObject ( object value )
@@ -46,31 +66,31 @@ private void WriteObjectToStream(object value, bool restrictSerialization = fals
46
66
{
47
67
// This is equivalent to SetData/GetData methods using registered OLE formats, resolves only the known types
48
68
WriteObjectToStream ( value , restrictSerialization : true ) ;
49
- return ReadObjectFromStream ( restrictDeserialization : true ) ;
69
+ return ReadRestrictedObjectFromStream ( ) ;
50
70
}
51
71
52
72
private object ? RoundTripOfType < T > ( object value )
53
73
{
54
74
// This is equivalent to SetData/TryGetData<T> methods using unbounded OLE formats,
55
75
// and works with the BinaryFormat AppContext switches.
56
76
WriteObjectToStream ( value ) ;
57
- return ReadObjectFromStream < T > ( restrictDeserialization : false , NotSupportedResolver ) ;
77
+ return ReadObjectFromStream < T > ( NotSupportedResolver ) ;
58
78
}
59
79
60
80
private object ? RoundTripOfType_RestrictedFormat < T > ( object value )
61
81
{
62
82
// This is equivalent to SetData/TryGetData<T> methods using OLE formats. Deserialization is restricted
63
83
// to known types.
64
84
WriteObjectToStream ( value , restrictSerialization : true ) ;
65
- return ReadObjectFromStream < T > ( restrictDeserialization : true , NotSupportedResolver ) ;
85
+ return ReadRestrictedObjectFromStream < T > ( NotSupportedResolver ) ;
66
86
}
67
87
68
88
private object ? RoundTripOfType < T > ( object value , Func < TypeName , Type > ? resolver )
69
89
{
70
90
// This is equivalent to SetData/TryGetData<T> methods using unbounded formats,
71
91
// serialization is restricted by the resolver and BinaryFormat AppContext switches.
72
92
WriteObjectToStream ( value ) ;
73
- return ReadObjectFromStream < T > ( restrictDeserialization : false , resolver ) ;
93
+ return ReadObjectFromStream < T > ( resolver ) ;
74
94
}
75
95
76
96
// Primitive types as defined by the NRBF spec.
@@ -311,14 +331,14 @@ public void RoundTrip_RestrictedFormat_ImageList()
311
331
public void RoundTrip_Bitmap ( )
312
332
{
313
333
using Bitmap value = new ( 10 , 10 ) ;
314
- RoundTripObject ( value ) . Should ( ) . BeOfType < Bitmap > ( ) . Subject . Size . Should ( ) . Be ( value . Size ) ;
334
+ RoundTripObject ( value ) . Should ( ) . BeOfType < Bitmap > ( ) . Which . Size . Should ( ) . Be ( value . Size ) ;
315
335
}
316
336
317
337
[ Fact ]
318
338
public void RoundTrip_RestrictedFormat_Bitmap ( )
319
339
{
320
340
using Bitmap value = new ( 10 , 10 ) ;
321
- RoundTripObject_RestrictedFormat ( value ) . Should ( ) . BeOfType < Bitmap > ( ) . Subject . Size . Should ( ) . Be ( value . Size ) ;
341
+ RoundTripObject_RestrictedFormat ( value ) . Should ( ) . BeOfType < Bitmap > ( ) . Which . Size . Should ( ) . Be ( value . Size ) ;
322
342
}
323
343
324
344
[ Theory ]
@@ -402,12 +422,12 @@ public void RoundTripOfType_Unsupported()
402
422
ReadAndValidate ( ) ;
403
423
}
404
424
405
- Action read = ( ) => ReadObjectFromStream < List < object > > ( restrictDeserialization : false , ObjectListResolver ) ;
425
+ Action read = ( ) => ReadObjectFromStream < List < object > > ( ObjectListResolver ) ;
406
426
read . Should ( ) . Throw < NotSupportedException > ( ) ;
407
427
408
428
void ReadAndValidate ( )
409
429
{
410
- var result = ReadObjectFromStream < List < object > > ( restrictDeserialization : false , ObjectListResolver )
430
+ var result = ReadObjectFromStream < List < object > > ( ObjectListResolver )
411
431
. Should ( ) . BeOfType < List < object > > ( ) . Subject ;
412
432
result . Count . Should ( ) . Be ( 1 ) ;
413
433
result [ 0 ] . Should ( ) . Be ( "text" ) ;
@@ -450,10 +470,10 @@ public void RoundTripOfType_RestrictedFormat_AsUnmatchingType_Simple()
450
470
// but in this case requested type will not match the payload type.
451
471
WriteObjectToStream ( value ) ;
452
472
453
- ReadObjectFromStream < Control > ( restrictDeserialization : true , NotSupportedResolver ) . Should ( ) . BeNull ( ) ;
473
+ ReadRestrictedObjectFromStream < Control > ( NotSupportedResolver ) . Should ( ) . BeNull ( ) ;
454
474
455
475
using BinaryFormatterFullCompatScope scope = new ( ) ;
456
- ReadObjectFromStream < Control > ( restrictDeserialization : true , NotSupportedResolver ) . Should ( ) . BeNull ( ) ;
476
+ ReadRestrictedObjectFromStream < Control > ( NotSupportedResolver ) . Should ( ) . BeNull ( ) ;
457
477
}
458
478
459
479
[ Fact ]
@@ -471,7 +491,7 @@ public void RoundTripOfType_RestrictedFormat_intNullableArray_NotSupportedResolv
471
491
472
492
using BinaryFormatterFullCompatScope scope = new ( ) ;
473
493
WriteObjectToStream ( value ) ;
474
- Action read = ( ) => ReadObjectFromStream < int ? [ ] > ( restrictDeserialization : true , NotSupportedResolver ) ;
494
+ Action read = ( ) => ReadRestrictedObjectFromStream < int ? [ ] > ( NotSupportedResolver ) ;
475
495
476
496
// nullable struct requires a custom resolver.
477
497
// RestrictedTypeDeserializationException
@@ -485,10 +505,11 @@ public void RoundTripOfType_intNullableArray_NotSupportedResolver()
485
505
486
506
using BinaryFormatterFullCompatScope scope = new ( ) ;
487
507
WriteObjectToStream ( value ) ;
488
- Action read = ( ) => ReadObjectFromStream < int ? [ ] > ( restrictDeserialization : false , NotSupportedResolver ) ;
508
+ Action read = ( ) => ReadObjectFromStream < int ? [ ] > ( NotSupportedResolver ) ;
489
509
490
510
// nullable struct requires a custom resolver.
491
- read . Should ( ) . Throw < SerializationException > ( ) ;
511
+ // This is either NotSupportedException of RestrictedTypeDeserializationException, depending on format.
512
+ read . Should ( ) . Throw < Exception > ( ) ;
492
513
}
493
514
494
515
[ Theory ]
@@ -507,7 +528,7 @@ public void RoundTripOfType_OffsetArray_NotSupportedResolver(bool restrictDeseri
507
528
WriteObjectToStream ( value ) ;
508
529
Action read = ( ) => ReadObjectFromStream < uint [ , ] > ( restrictDeserialization , NotSupportedResolver ) ;
509
530
510
- read . Should ( ) . Throw < NotSupportedException > ( ) ;
531
+ read . Should ( ) . Throw < Exception > ( ) ;
511
532
}
512
533
513
534
[ Fact ]
@@ -583,7 +604,7 @@ public void RoundTripOfType_TestData_InvalidResolver()
583
604
WriteObjectToStream ( value ) ;
584
605
585
606
// Resolver that returns a null is blocked in our SerializationBinder wrapper.
586
- Action read = ( ) => ReadObjectFromStream < TestData > ( restrictDeserialization : false , InvalidResolver ) ;
607
+ Action read = ( ) => ReadObjectFromStream < TestData > ( InvalidResolver ) ;
587
608
588
609
read . Should ( ) . Throw < SerializationException > ( ) ;
589
610
@@ -649,7 +670,6 @@ public void ReadFontSerializedOnNet481()
649
670
stream . Position = 0 ;
650
671
Action getData = ( ) => Utilities . ReadObjectFromStream < object > (
651
672
stream ,
652
- restrictDeserialization : false ,
653
673
resolver : null ,
654
674
legacyMode : true ) ;
655
675
@@ -664,7 +684,6 @@ public void ReadFontSerializedOnNet481()
664
684
stream . Position = 0 ;
665
685
var result = Utilities . ReadObjectFromStream < object > (
666
686
stream ,
667
- restrictDeserialization : false ,
668
687
resolver : null ,
669
688
legacyMode : true ) . Should ( ) . BeOfType < Font > ( ) . Subject ;
670
689
@@ -679,7 +698,6 @@ static void TryGetData(MemoryStream stream)
679
698
stream . Position = 0 ;
680
699
var result = Utilities . ReadObjectFromStream < Font > (
681
700
stream ,
682
- restrictDeserialization : false ,
683
701
resolver : FontResolver ,
684
702
legacyMode : false ) . Should ( ) . BeOfType < Font > ( ) . Subject ;
685
703
@@ -721,7 +739,7 @@ public void Sample_GetData_UseBinaryFormatter()
721
739
722
740
// legacyMode == true follows the GetData path.
723
741
_stream . Position = 0 ;
724
- Utilities . ReadObjectFromStream < MyClass1 > ( _stream , restrictDeserialization : false , resolver : null , legacyMode : true )
742
+ Utilities . ReadObjectFromStream < MyClass1 > ( _stream , resolver : null , legacyMode : true )
725
743
. Should ( ) . BeEquivalentTo ( value ) ;
726
744
}
727
745
@@ -736,7 +754,7 @@ public void Sample_GetData_UseNrbfDeserialize()
736
754
737
755
// This works because GetData falls back to the BinaryFormatter deserializer, NRBF deserializer fails because it requires a resolver.
738
756
_stream . Position = 0 ;
739
- Utilities . ReadObjectFromStream < MyClass1 > ( _stream , restrictDeserialization : false , resolver : null , legacyMode : true )
757
+ Utilities . ReadObjectFromStream < MyClass1 > ( _stream , resolver : null , legacyMode : true )
740
758
. Should ( ) . BeEquivalentTo ( value ) ;
741
759
}
742
760
0 commit comments