77using System . Text . Json ;
88using Microsoft . Extensions . Configuration ;
99using Microsoft . Extensions . DependencyInjection ;
10- using Microsoft . Extensions . Logging ;
11- using Microsoft . Extensions . Logging . Abstractions ;
1210using Microsoft . Extensions . Options ;
1311using Steeltoe . Common . TestResources ;
1412using Steeltoe . Common . TestResources . IO ;
@@ -24,7 +22,8 @@ public sealed class ConfigureCertificateOptionsTest
2422 [ InlineData ( CertificateName ) ]
2523 public void ConfigureCertificateOptions_NoPath_NoCertificate ( string certificateName )
2624 {
27- var configureOptions = new ConfigureCertificateOptions ( new ConfigurationBuilder ( ) . Build ( ) , NullLogger < ConfigureCertificateOptions > . Instance ) ;
25+ IConfiguration configuration = new ConfigurationBuilder ( ) . Build ( ) ;
26+ var configureOptions = new ConfigureCertificateOptions ( configuration ) ;
2827 var options = new CertificateOptions ( ) ;
2928
3029 configureOptions . Configure ( certificateName , options ) ;
@@ -43,7 +42,7 @@ public void ConfigureCertificateOptions_BadPath_NoCertificate(string certificate
4342 } ;
4443
4544 IConfiguration configuration = new ConfigurationBuilder ( ) . AddInMemoryCollection ( appSettings ) . Build ( ) ;
46- var configureOptions = new ConfigureCertificateOptions ( configuration , NullLogger < ConfigureCertificateOptions > . Instance ) ;
45+ var configureOptions = new ConfigureCertificateOptions ( configuration ) ;
4746 var options = new CertificateOptions ( ) ;
4847
4948 configureOptions . Configure ( certificateName , options ) ;
@@ -54,65 +53,43 @@ public void ConfigureCertificateOptions_BadPath_NoCertificate(string certificate
5453 [ Theory ]
5554 [ InlineData ( "" ) ]
5655 [ InlineData ( CertificateName ) ]
57- public void ConfigureCertificateOptions_EmptyFile_Crash_logged ( string certificateName )
56+ public void ConfigureCertificateOptions_ThrowsOnEmptyFile ( string certificateName )
5857 {
59- CapturingLoggerProvider loggerProvider = new ( )
60- {
61- IncludeStackTraces = true
62- } ;
63-
64- using var loggerFactory = new LoggerFactory ( [ loggerProvider ] ) ;
65- ILogger < ConfigureCertificateOptions > logger = loggerFactory . CreateLogger < ConfigureCertificateOptions > ( ) ;
66-
6758 var appSettings = new Dictionary < string , string ? >
6859 {
6960 [ $ "{ GetConfigurationKey ( certificateName , "CertificateFilePath" ) } "] = "empty.crt"
7061 } ;
7162
7263 IConfiguration configuration = new ConfigurationBuilder ( ) . AddInMemoryCollection ( appSettings ) . Build ( ) ;
73- var configureOptions = new ConfigureCertificateOptions ( configuration , logger ) ;
64+
65+ var configureOptions = new ConfigureCertificateOptions ( configuration ) ;
7466 var options = new CertificateOptions ( ) ;
7567
76- configureOptions . Configure ( certificateName , options ) ;
68+ Action configureAction = ( ) => configureOptions . Configure ( certificateName , options ) ;
69+ configureAction . Should ( ) . Throw < CryptographicException > ( ) ;
7770
7871 options . Certificate . Should ( ) . BeNull ( ) ;
79-
80- loggerProvider . GetAll ( ) . Should ( ) . ContainSingle ( message =>
81- message . Contains ( typeof ( CryptographicException ) . FullName ! , StringComparison . OrdinalIgnoreCase ) && message . StartsWith (
82- $ "WARN { typeof ( ConfigureCertificateOptions ) . FullName } : Failed to parse file contents for '{ certificateName } ' from 'empty.crt'. Will retry on next reload.",
83- StringComparison . OrdinalIgnoreCase ) ) ;
8472 }
8573
8674 [ Theory ]
8775 [ InlineData ( "" ) ]
8876 [ InlineData ( CertificateName ) ]
8977 public void ConfigureCertificateOptions_ThrowsOnInvalidKey ( string certificateName )
9078 {
91- CapturingLoggerProvider loggerProvider = new ( )
92- {
93- IncludeStackTraces = true
94- } ;
95-
96- using var loggerFactory = new LoggerFactory ( [ loggerProvider ] ) ;
97- ILogger < ConfigureCertificateOptions > logger = loggerFactory . CreateLogger < ConfigureCertificateOptions > ( ) ;
98-
9979 var appSettings = new Dictionary < string , string ? >
10080 {
10181 [ $ "{ GetConfigurationKey ( certificateName , "CertificateFilePath" ) } "] = "instance.crt" ,
10282 [ $ "{ GetConfigurationKey ( certificateName , "PrivateKeyFilePath" ) } "] = "invalid.key"
10383 } ;
10484
10585 IConfiguration configuration = new ConfigurationBuilder ( ) . AddInMemoryCollection ( appSettings ) . Build ( ) ;
106- var configureOptions = new ConfigureCertificateOptions ( configuration , logger ) ;
86+ var configureOptions = new ConfigureCertificateOptions ( configuration ) ;
10787 var options = new CertificateOptions ( ) ;
10888
109- configureOptions . Configure ( certificateName , options ) ;
110- options . Certificate . Should ( ) . BeNull ( ) ;
89+ Action configureAction = ( ) => configureOptions . Configure ( certificateName , options ) ;
90+ configureAction . Should ( ) . Throw < CryptographicException > ( ) ;
11191
112- loggerProvider . GetAll ( ) . Should ( ) . ContainSingle ( message =>
113- message . Contains ( typeof ( CryptographicException ) . FullName ! , StringComparison . OrdinalIgnoreCase ) && message . StartsWith (
114- $ "WARN { typeof ( ConfigureCertificateOptions ) . FullName } : Failed to parse file contents for '{ certificateName } ' from 'instance.crt'. Will retry on next reload.",
115- StringComparison . OrdinalIgnoreCase ) ) ;
92+ options . Certificate . Should ( ) . BeNull ( ) ;
11693 }
11794
11895 [ Theory ]
@@ -127,7 +104,7 @@ public void ConfigureCertificateOptions_ReadsP12File_CreatesCertificate(string c
127104
128105 IConfiguration configuration = new ConfigurationBuilder ( ) . AddInMemoryCollection ( appSettings ) . Build ( ) ;
129106 configuration [ $ "{ GetConfigurationKey ( certificateName , "CertificateFilePath" ) } "] . Should ( ) . NotBeNull ( ) ;
130- var configureOptions = new ConfigureCertificateOptions ( configuration , NullLogger < ConfigureCertificateOptions > . Instance ) ;
107+ var configureOptions = new ConfigureCertificateOptions ( configuration ) ;
131108 var options = new CertificateOptions ( ) ;
132109
133110 configureOptions . Configure ( certificateName , options ) ;
@@ -148,7 +125,7 @@ public void ConfigureCertificateOptions_ReadsPemFiles_CreatesCertificate(string
148125 } ;
149126
150127 IConfiguration configuration = new ConfigurationBuilder ( ) . AddInMemoryCollection ( appSettings ) . Build ( ) ;
151- var configureOptions = new ConfigureCertificateOptions ( configuration , NullLogger < ConfigureCertificateOptions > . Instance ) ;
128+ var configureOptions = new ConfigureCertificateOptions ( configuration ) ;
152129 var options = new CertificateOptions ( ) ;
153130
154131 configureOptions . Configure ( certificateName , options ) ;
@@ -191,7 +168,7 @@ public async Task CertificateOptions_update_on_changed_contents(string certifica
191168 await File . WriteAllTextAsync ( privateKeyFilePath , secondPrivateKeyContent , TestContext . Current . CancellationToken ) ;
192169
193170 using Task pollTask = WaitUntilCertificateChangedToAsync ( secondX509 , optionsMonitor , certificateName , TestContext . Current . CancellationToken ) ;
194- await pollTask . WaitAsync ( TimeSpan . FromSeconds ( 4 ) , TestContext . Current . CancellationToken ) ;
171+ await pollTask . WaitAsync ( TimeSpan . FromSeconds ( 1 ) , TestContext . Current . CancellationToken ) ;
195172
196173 optionsMonitor . Get ( certificateName ) . Certificate . Should ( ) . Be ( secondX509 ) ;
197174 }
@@ -228,7 +205,7 @@ public async Task CertificateOptions_update_on_changed_path(string certificateNa
228205 await File . WriteAllTextAsync ( appSettingsPath , appSettings , TestContext . Current . CancellationToken ) ;
229206
230207 using Task pollTask = WaitUntilCertificateChangedToAsync ( secondX509 , optionsMonitor , certificateName , TestContext . Current . CancellationToken ) ;
231- await pollTask . WaitAsync ( TimeSpan . FromSeconds ( 4 ) , TestContext . Current . CancellationToken ) ;
208+ await pollTask . WaitAsync ( TimeSpan . FromSeconds ( 1 ) , TestContext . Current . CancellationToken ) ;
232209
233210 optionsMonitor . Get ( certificateName ) . Certificate . Should ( ) . Be ( secondX509 ) ;
234211 }
0 commit comments