@@ -27,23 +27,29 @@ public class AuthenticodeDownloadValidatorTest
27
27
[ CancelAfter ( 30_000 ) ]
28
28
public void Unsigned ( CancellationToken ct )
29
29
{
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" ) ) ;
31
34
}
32
35
33
36
[ Test ( Description = "Test an untrusted binary" ) ]
34
37
[ CancelAfter ( 30_000 ) ]
35
38
public void Untrusted ( CancellationToken ct )
36
39
{
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" ) ) ;
38
44
}
39
45
40
46
[ Test ( Description = "Test an binary with a detached signature (catalog file)" ) ]
41
47
[ CancelAfter ( 30_000 ) ]
42
48
public void DifferentCertTrusted ( CancellationToken ct )
43
49
{
44
- // notepad .exe uses a catalog file for its signature.
50
+ // rundll32 .exe uses a catalog file for its signature.
45
51
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 ) ) ;
47
53
Assert . That ( ex . Message ,
48
54
Does . Contain ( "File is not signed with an embedded Authenticode signature: Kind=Catalog" ) ) ;
49
55
}
@@ -52,15 +58,19 @@ public void DifferentCertTrusted(CancellationToken ct)
52
58
[ CancelAfter ( 30_000 ) ]
53
59
public void DifferentCertUntrusted ( CancellationToken ct )
54
60
{
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" ) ) ;
56
66
}
57
67
58
68
[ Test ( Description = "Test a binary signed by Coder's certificate" ) ]
59
69
[ CancelAfter ( 30_000 ) ]
60
70
public async Task CoderSigned ( CancellationToken ct )
61
71
{
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 ) ;
64
74
}
65
75
}
66
76
@@ -71,22 +81,57 @@ public class AssemblyVersionDownloadValidatorTest
71
81
[ CancelAfter ( 30_000 ) ]
72
82
public void NoVersion ( CancellationToken ct )
73
83
{
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" ) ) ;
75
88
}
76
89
77
- [ Test ( Description = "Version mismatch " ) ]
90
+ [ Test ( Description = "Invalid version on binary " ) ]
78
91
[ CancelAfter ( 30_000 ) ]
79
- public void VersionMismatch ( CancellationToken ct )
92
+ public void InvalidVersion ( CancellationToken ct )
80
93
{
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
+ }
82
121
}
83
122
84
- [ Test ( Description = "Version match" ) ]
123
+ [ Test ( Description = "Version match with and without partial version check " ) ]
85
124
[ CancelAfter ( 30_000 ) ]
86
125
public async Task VersionMatch ( CancellationToken ct )
87
126
{
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 ) ;
90
135
}
91
136
}
92
137
0 commit comments