@@ -5,8 +5,10 @@ namespace RemoteUnitTestExecutor
55{
66 using System ;
77 using System . Collections . Generic ;
8+ using System . Diagnostics . CodeAnalysis ;
89 using System . IO ;
910 using System . Linq ;
11+ using System . Runtime . Serialization ;
1012 using System . Runtime . Serialization . Formatters . Binary ;
1113
1214 /// <summary>
@@ -20,7 +22,7 @@ public TestResult()
2022 InvokedMethods = new List < MethodInvocationInfo > ( ) ;
2123 }
2224
23- public IList < MethodInvocationInfo > InvokedMethods { get ; set ; }
25+ public IList < MethodInvocationInfo > InvokedMethods { get ; }
2426
2527 public bool Succeeded { get ; set ; }
2628
@@ -36,16 +38,28 @@ public void Serialize(string filename)
3638 }
3739 }
3840
41+ /// CA2300 is set to Info level, which will not cause a build break. However, RoslynAnalyzer and PostAnalysis
42+ /// pipeline tasks will fail on any level reported above None. Recommendation is to disable CA2300 and enable
43+ /// CA2301 and CA2302 as mitigations. However, these are part of the SDL ruleset and we should not modify its
44+ /// behavior. Thus, mitigate CA2301 and CA2302 and then suppress CA2300.
45+ [ SuppressMessage ( "Security" , "CA2300" , Justification = "Mitigated with fixes for CA2301 and CA2302" ) ]
3946 public static ITestResult CreateFromFile ( string testOutputFileName )
4047 {
4148 using ( FileStream deserializationStream = File . OpenRead ( testOutputFileName ) )
4249 {
43- return ( ITestResult ) new BinaryFormatter ( ) . Deserialize ( deserializationStream ) ;
50+ BinaryFormatter formatter = new BinaryFormatter ( ) ;
51+ formatter . Binder = new TestResultSerializationBinder ( ) ;
52+ return ( ITestResult ) formatter . Deserialize ( deserializationStream ) ;
4453 }
4554 }
4655
4756 public void AddProfilerTraces ( IEnumerable < string > tracesIterator )
4857 {
58+ if ( null == tracesIterator )
59+ {
60+ throw new ArgumentNullException ( nameof ( tracesIterator ) ) ;
61+ }
62+
4963 foreach ( var trace in tracesIterator )
5064 {
5165 this . ProfilerTraces . Add ( trace ) ;
@@ -71,5 +85,27 @@ public string InvokedMethodsSequence
7185 return string . Empty ;
7286 }
7387 }
88+
89+ private class TestResultSerializationBinder : SerializationBinder
90+ {
91+ private static IEnumerable < string > s_supportedTypes = new List < string > ( )
92+ {
93+ typeof ( List < MethodInvocationInfo > ) . FullName ,
94+ typeof ( List < string > ) . FullName ,
95+ typeof ( MethodInvocationInfo ) . FullName ,
96+ typeof ( TestResult ) . FullName
97+ } ;
98+
99+ public override Type BindToType ( string assemblyName , string typeName )
100+ {
101+ if ( s_supportedTypes . Contains ( typeName , StringComparer . Ordinal ) )
102+ {
103+ // Tells serializer to use type from deserialized data.
104+ return null ;
105+ }
106+
107+ throw new NotSupportedException ( "Attempted to deserialize unexpected type: " + typeName ) ;
108+ }
109+ }
74110 }
75111}
0 commit comments