@@ -27,23 +27,29 @@ public class AuthenticodeDownloadValidatorTest
2727 [ CancelAfter ( 30_000 ) ]
2828 public void Unsigned ( CancellationToken ct )
2929 {
30- // TODO: this
30+ var testBinaryPath = Path . Combine ( TestContext . CurrentContext . TestDirectory , "testdata" , "hello.exe" ) ;
31+ var ex = Assert . ThrowsAsync < Exception > ( ( ) =>
32+ AuthenticodeDownloadValidator . Coder . ValidateAsync ( testBinaryPath , ct ) ) ;
33+ Assert . That ( ex . Message , Does . Contain ( "File is not signed and trusted with an Authenticode signature: State=Unsigned, StateReason=None" ) ) ;
3134 }
3235
3336 [ Test ( Description = "Test an untrusted binary" ) ]
3437 [ CancelAfter ( 30_000 ) ]
3538 public void Untrusted ( CancellationToken ct )
3639 {
37- // TODO: this
40+ var testBinaryPath = Path . Combine ( TestContext . CurrentContext . TestDirectory , "testdata" , "hello-self-signed.exe" ) ;
41+ var ex = Assert . ThrowsAsync < Exception > ( ( ) =>
42+ AuthenticodeDownloadValidator . Coder . ValidateAsync ( testBinaryPath , ct ) ) ;
43+ Assert . That ( ex . Message , Does . Contain ( "File is not signed and trusted with an Authenticode signature: State=Unsigned, StateReason=UntrustedRoot" ) ) ;
3844 }
3945
4046 [ Test ( Description = "Test an binary with a detached signature (catalog file)" ) ]
4147 [ CancelAfter ( 30_000 ) ]
4248 public void DifferentCertTrusted ( CancellationToken ct )
4349 {
44- // notepad .exe uses a catalog file for its signature.
50+ // rundll32 .exe uses a catalog file for its signature.
4551 var ex = Assert . ThrowsAsync < Exception > ( ( ) =>
46- AuthenticodeDownloadValidator . Coder . ValidateAsync ( @"C:\Windows\System32\notepad .exe" , ct ) ) ;
52+ AuthenticodeDownloadValidator . Coder . ValidateAsync ( @"C:\Windows\System32\rundll32 .exe" , ct ) ) ;
4753 Assert . That ( ex . Message ,
4854 Does . Contain ( "File is not signed with an embedded Authenticode signature: Kind=Catalog" ) ) ;
4955 }
@@ -52,15 +58,19 @@ public void DifferentCertTrusted(CancellationToken ct)
5258 [ CancelAfter ( 30_000 ) ]
5359 public void DifferentCertUntrusted ( CancellationToken ct )
5460 {
55- // TODO: this
61+ // dotnet.exe is signed by .NET. During tests we can be pretty sure
62+ // this is installed.
63+ var ex = Assert . ThrowsAsync < Exception > ( ( ) =>
64+ AuthenticodeDownloadValidator . Coder . ValidateAsync ( @"C:\Program Files\dotnet\dotnet.exe" , ct ) ) ;
65+ Assert . That ( ex . Message , Does . Contain ( "File is signed by an unexpected certificate: ExpectedName='Coder Technologies Inc.', ActualName='.NET" ) ) ;
5666 }
5767
5868 [ Test ( Description = "Test a binary signed by Coder's certificate" ) ]
5969 [ CancelAfter ( 30_000 ) ]
6070 public async Task CoderSigned ( CancellationToken ct )
6171 {
62- // TODO: this
63- await Task . CompletedTask ;
72+ var testBinaryPath = Path . Combine ( TestContext . CurrentContext . TestDirectory , "testdata" , "hello-versioned-signed.exe" ) ;
73+ await AuthenticodeDownloadValidator . Coder . ValidateAsync ( testBinaryPath , ct ) ;
6474 }
6575}
6676
@@ -71,22 +81,57 @@ public class AssemblyVersionDownloadValidatorTest
7181 [ CancelAfter ( 30_000 ) ]
7282 public void NoVersion ( CancellationToken ct )
7383 {
74- // TODO: this
84+ var testBinaryPath = Path . Combine ( TestContext . CurrentContext . TestDirectory , "testdata" , "hello.exe" ) ;
85+ var ex = Assert . ThrowsAsync < Exception > ( ( ) =>
86+ new AssemblyVersionDownloadValidator ( 1 , 2 , 3 , 4 ) . ValidateAsync ( testBinaryPath , ct ) ) ;
87+ Assert . That ( ex . Message , Does . Contain ( "File ProductVersion is empty or null" ) ) ;
7588 }
7689
77- [ Test ( Description = "Version mismatch " ) ]
90+ [ Test ( Description = "Invalid version on binary " ) ]
7891 [ CancelAfter ( 30_000 ) ]
79- public void VersionMismatch ( CancellationToken ct )
92+ public void InvalidVersion ( CancellationToken ct )
8093 {
81- // TODO: this
94+ var testBinaryPath = Path . Combine ( TestContext . CurrentContext . TestDirectory , "testdata" , "hello-invalid-version.exe" ) ;
95+ var ex = Assert . ThrowsAsync < Exception > ( ( ) =>
96+ new AssemblyVersionDownloadValidator ( 1 , 2 , 3 , 4 ) . ValidateAsync ( testBinaryPath , ct ) ) ;
97+ Assert . That ( ex . Message , Does . Contain ( "File ProductVersion '1-2-3-4' is not a valid version string" ) ) ;
98+ }
99+
100+ [ Test ( Description = "Version mismatch with full version check" ) ]
101+ [ CancelAfter ( 30_000 ) ]
102+ public void VersionMismatchFull ( CancellationToken ct )
103+ {
104+ var testBinaryPath = Path . Combine ( TestContext . CurrentContext . TestDirectory , "testdata" , "hello-versioned-signed.exe" ) ;
105+
106+ // Try changing each version component one at a time
107+ var expectedVersions = new [ ] { 1 , 2 , 3 , 4 } ;
108+ for ( var i = 0 ; i < 4 ; i ++ )
109+ {
110+ var testVersions = ( int [ ] ) expectedVersions . Clone ( ) ;
111+ testVersions [ i ] ++ ; // Increment this component to make it wrong
112+
113+ var ex = Assert . ThrowsAsync < Exception > ( ( ) =>
114+ new AssemblyVersionDownloadValidator (
115+ testVersions [ 0 ] , testVersions [ 1 ] , testVersions [ 2 ] , testVersions [ 3 ]
116+ ) . ValidateAsync ( testBinaryPath , ct ) ) ;
117+
118+ Assert . That ( ex . Message , Does . Contain (
119+ $ "File ProductVersion does not match expected version: Actual='1.2.3.4', Expected='{ string . Join ( "." , testVersions ) } '") ) ;
120+ }
82121 }
83122
84- [ Test ( Description = "Version match" ) ]
123+ [ Test ( Description = "Version match with and without partial version check " ) ]
85124 [ CancelAfter ( 30_000 ) ]
86125 public async Task VersionMatch ( CancellationToken ct )
87126 {
88- // TODO: this
89- await Task . CompletedTask ;
127+ var testBinaryPath = Path . Combine ( TestContext . CurrentContext . TestDirectory , "testdata" , "hello-versioned-signed.exe" ) ;
128+
129+ // Test with just major.minor
130+ await new AssemblyVersionDownloadValidator ( 1 , 2 ) . ValidateAsync ( testBinaryPath , ct ) ;
131+ // Test with major.minor.patch
132+ await new AssemblyVersionDownloadValidator ( 1 , 2 , 3 ) . ValidateAsync ( testBinaryPath , ct ) ;
133+ // Test with major.minor.patch.build
134+ await new AssemblyVersionDownloadValidator ( 1 , 2 , 3 , 4 ) . ValidateAsync ( testBinaryPath , ct ) ;
90135 }
91136}
92137
0 commit comments