33using System ;
44using System . Collections . Generic ;
55using System . IO ;
6+ using System . Net . Http ;
67using System . Security . Cryptography ;
78using System . Security . Cryptography . X509Certificates ;
89using System . Threading . Tasks ;
@@ -18,7 +19,10 @@ public class UnitTests
1819
1920 private static readonly string _targetFileAddress =
2021 "https://frendsfonts.blob.core.windows.net/images/frendsLogo.png" ;
21- private readonly string _certificatePath = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "TestFiles" , "certwithpk.pfx" ) ;
22+
23+ private readonly string _certificatePath =
24+ Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "TestFiles" , "certwithpk.pfx" ) ;
25+
2226 private readonly string _privateKeyPassword = "password" ;
2327
2428 [ TestInitialize ]
@@ -38,9 +42,21 @@ public void Cleanup()
3842 [ TestMethod ]
3943 public async Task TestFileDownload_WithoutHeaders_AllTrue ( )
4044 {
41- var auths = new List < Authentication > ( ) { Authentication . None , Authentication . Basic , Authentication . WindowsAuthentication , Authentication . WindowsIntegratedSecurity , Authentication . OAuth } ;
45+ var auths = new List < Authentication > ( )
46+ {
47+ Authentication . None ,
48+ Authentication . Basic ,
49+ Authentication . WindowsAuthentication ,
50+ Authentication . WindowsIntegratedSecurity ,
51+ Authentication . OAuth
52+ } ;
4253
43- var certSource = new List < CertificateSource > ( ) { CertificateSource . CertificateStore , CertificateSource . File , CertificateSource . String } ;
54+ var certSource = new List < CertificateSource > ( )
55+ {
56+ CertificateSource . CertificateStore ,
57+ CertificateSource . File ,
58+ CertificateSource . String
59+ } ;
4460
4561 var input = new Input
4662 {
@@ -89,9 +105,21 @@ public async Task TestFileDownload_WithoutHeaders_AllTrue()
89105 [ TestMethod ]
90106 public async Task TestFileDownload_WithoutHeaders_AllFalse ( )
91107 {
92- var auths = new List < Authentication > ( ) { Authentication . None , Authentication . Basic , Authentication . WindowsAuthentication , Authentication . WindowsIntegratedSecurity , Authentication . OAuth } ;
108+ var auths = new List < Authentication > ( )
109+ {
110+ Authentication . None ,
111+ Authentication . Basic ,
112+ Authentication . WindowsAuthentication ,
113+ Authentication . WindowsIntegratedSecurity ,
114+ Authentication . OAuth
115+ } ;
93116
94- var certSource = new List < CertificateSource > ( ) { CertificateSource . CertificateStore , CertificateSource . File , CertificateSource . String } ;
117+ var certSource = new List < CertificateSource > ( )
118+ {
119+ CertificateSource . CertificateStore ,
120+ CertificateSource . File ,
121+ CertificateSource . String
122+ } ;
95123
96124 var input = new Input
97125 {
@@ -139,7 +167,14 @@ public async Task TestFileDownload_WithoutHeaders_AllFalse()
139167 [ TestMethod ]
140168 public async Task TestFileDownload_WithHeaders ( )
141169 {
142- var headers = new [ ] { new Header { Name = "foo" , Value = "bar" } } ;
170+ var headers = new [ ]
171+ {
172+ new Header
173+ {
174+ Name = "foo" ,
175+ Value = "bar"
176+ }
177+ } ;
143178
144179 var auths = new List < Authentication >
145180 {
@@ -236,7 +271,8 @@ public async Task TestFileDownload_Certification()
236271 AutomaticCookieHandling = true ,
237272 CertificateThumbprint = tp ,
238273 ClientCertificateFilePath = _certificatePath ,
239- ClientCertificateInBase64 = cert is CertificateSource . String ? Convert . ToBase64String ( File . ReadAllBytes ( _certificatePath ) ) : "" ,
274+ ClientCertificateInBase64 =
275+ cert is CertificateSource . String ? Convert . ToBase64String ( File . ReadAllBytes ( _certificatePath ) ) : "" ,
240276 ClientCertificateKeyPhrase = _privateKeyPassword ,
241277 ClientCertificateSource = cert ,
242278 ConnectionTimeoutSeconds = 60 ,
@@ -259,7 +295,6 @@ public async Task TestFileDownload_Certification()
259295 Directory . CreateDirectory ( _directory ) ;
260296 CertificateHandler ( _certificatePath , _privateKeyPassword , true , tp ) ;
261297 }
262-
263298 }
264299
265300 private static string CertificateHandler ( string path , string password , bool cleanUp , string thumbPrint )
@@ -290,14 +325,16 @@ private static string CertificateHandler(string path, string password, bool clea
290325 }
291326
292327 File . WriteAllBytes ( path , certData ) ;
328+
293329 return cert . Thumbprint ;
294330 }
295331 else
296332 {
297333 using ( X509Store store = new ( StoreName . My , StoreLocation . CurrentUser ) )
298334 {
299335 store . Open ( OpenFlags . ReadWrite | OpenFlags . IncludeArchived ) ;
300- X509Certificate2Collection col = store . Certificates . Find ( X509FindType . FindByThumbprint , thumbPrint , false ) ;
336+ X509Certificate2Collection col =
337+ store . Certificates . Find ( X509FindType . FindByThumbprint , thumbPrint , false ) ;
301338
302339 foreach ( var cert in col )
303340 store . Remove ( cert ) ;
@@ -488,4 +525,25 @@ public async Task TestFileDownload_WindowsAuth_InvalidUsername_ShouldThrow()
488525
489526 await HTTP . DownloadFile ( input , options , default ) ;
490527 }
491- }
528+
529+ [ DataTestMethod ]
530+ [ DataRow ( CertificateStoreLocation . CurrentUser , "current user" ) ]
531+ [ DataRow ( CertificateStoreLocation . LocalMachine , "local machine" ) ]
532+ public void CorrectStoreSearched ( CertificateStoreLocation storeLocation , string storeLocationText )
533+ {
534+ var handler = new HttpClientHandler ( ) ;
535+ var options = new Options
536+ {
537+ Authentication = Authentication . ClientCertificate ,
538+ ClientCertificateSource = CertificateSource . CertificateStore ,
539+ CertificateStoreLocation = storeLocation ,
540+ CertificateThumbprint = "InvalidThumbprint" ,
541+ } ;
542+ var ex = Assert . ThrowsExactly < FileNotFoundException > ( ( ) =>
543+ handler . SetHandlerSettingsBasedOnOptions ( options ) ) ;
544+
545+ Assert . IsNotNull ( ex ) ;
546+ StringAssert . Contains ( ex . Message ,
547+ $ "Certificate with thumbprint: 'INVALIDTHUMBPRINT' not found in { storeLocationText } cert store.") ;
548+ }
549+ }
0 commit comments