@@ -25,7 +25,7 @@ public class KestrelConfigurationLoaderTests
25
25
private KestrelServerOptions CreateServerOptions ( )
26
26
{
27
27
var serverOptions = new KestrelServerOptions ( ) ;
28
- var env = new MockHostingEnvironment { ApplicationName = "TestApplication" } ;
28
+ var env = new MockHostingEnvironment { ApplicationName = "TestApplication" , ContentRootPath = Directory . GetCurrentDirectory ( ) } ;
29
29
serverOptions . ApplicationServices = new ServiceCollection ( )
30
30
. AddLogging ( )
31
31
. AddSingleton < IHostEnvironment > ( env )
@@ -254,6 +254,141 @@ public void ConfigureEndpointDevelopmentCertificateGetsLoadedWhenPresent()
254
254
}
255
255
}
256
256
257
+ [ Fact ]
258
+ public void ConfigureEndpoint_CanLoadRsaPemCerts ( )
259
+ {
260
+ var serverOptions = CreateServerOptions ( ) ;
261
+ var certificate = new X509Certificate2 ( TestResources . GetCertPath ( "https-rsa.crt" ) ) ;
262
+
263
+ var ran1 = false ;
264
+ var config = new ConfigurationBuilder ( ) . AddInMemoryCollection ( new [ ]
265
+ {
266
+ new KeyValuePair < string , string > ( "Endpoints:End1:Url" , "https://*:5001" ) ,
267
+ new KeyValuePair < string , string > ( "Certificates:Default:Path" , Path . Combine ( "shared" , "TestCertificates" , "https-rsa.crt" ) ) ,
268
+ new KeyValuePair < string , string > ( "Certificates:Default:KeyPath" , Path . Combine ( "shared" , "TestCertificates" , "https-rsa.key" ) ) ,
269
+ } ) . Build ( ) ;
270
+
271
+ serverOptions
272
+ . Configure ( config )
273
+ . Endpoint ( "End1" , opt =>
274
+ {
275
+ ran1 = true ;
276
+ Assert . True ( opt . IsHttps ) ;
277
+ Assert . Equal ( opt . HttpsOptions . ServerCertificate . SerialNumber , certificate . SerialNumber ) ;
278
+ } ) . Load ( ) ;
279
+
280
+ Assert . True ( ran1 ) ;
281
+ Assert . NotNull ( serverOptions . DefaultCertificate ) ;
282
+ }
283
+
284
+ [ Fact ]
285
+ public void ConfigureEndpoint_CanLoadProtectedRsaPemCerts ( )
286
+ {
287
+ var serverOptions = CreateServerOptions ( ) ;
288
+ var certificate = new X509Certificate2 ( TestResources . GetCertPath ( "https-aspnet.crt" ) ) ;
289
+
290
+ var ran1 = false ;
291
+ var config = new ConfigurationBuilder ( ) . AddInMemoryCollection ( new [ ]
292
+ {
293
+ new KeyValuePair < string , string > ( "Endpoints:End1:Url" , "https://*:5001" ) ,
294
+ new KeyValuePair < string , string > ( "Certificates:Default:Path" , Path . Combine ( "shared" , "TestCertificates" , "https-aspnet.crt" ) ) ,
295
+ new KeyValuePair < string , string > ( "Certificates:Default:KeyPath" , Path . Combine ( "shared" , "TestCertificates" , "https-aspnet.key" ) ) ,
296
+ new KeyValuePair < string , string > ( "Certificates:Default:Password" , "aspnetcore" ) ,
297
+ } ) . Build ( ) ;
298
+
299
+ serverOptions
300
+ . Configure ( config )
301
+ . Endpoint ( "End1" , opt =>
302
+ {
303
+ ran1 = true ;
304
+ Assert . True ( opt . IsHttps ) ;
305
+ Assert . Equal ( opt . HttpsOptions . ServerCertificate . SerialNumber , certificate . SerialNumber ) ;
306
+ } ) . Load ( ) ;
307
+
308
+ Assert . True ( ran1 ) ;
309
+ Assert . NotNull ( serverOptions . DefaultCertificate ) ;
310
+ }
311
+
312
+ [ Fact ]
313
+ public void ConfigureEndpoint_ThrowsWhen_TheKeyCannotBeRead ( )
314
+ {
315
+ var serverOptions = CreateServerOptions ( ) ;
316
+ var certificate = new X509Certificate2 ( TestResources . GetCertPath ( "https-aspnet.crt" ) ) ;
317
+
318
+ var config = new ConfigurationBuilder ( ) . AddInMemoryCollection ( new [ ]
319
+ {
320
+ new KeyValuePair < string , string > ( "Endpoints:End1:Url" , "https://*:5001" ) ,
321
+ new KeyValuePair < string , string > ( "Certificates:Default:Path" , Path . Combine ( "shared" , "TestCertificates" , "https-aspnet.crt" ) ) ,
322
+ new KeyValuePair < string , string > ( "Certificates:Default:KeyPath" , Path . Combine ( "shared" , "TestCertificates" , "https-aspnet.key" ) )
323
+ } ) . Build ( ) ;
324
+
325
+ var ex = Assert . Throws < InvalidOperationException > ( ( ) =>
326
+ {
327
+ serverOptions
328
+ . Configure ( config )
329
+ . Endpoint ( "End1" , opt =>
330
+ {
331
+ Assert . True ( opt . IsHttps ) ;
332
+ } ) . Load ( ) ;
333
+ } ) ;
334
+ Assert . Equal ( CoreStrings . InvalidPemKey , ex . Message ) ;
335
+ }
336
+
337
+ [ Fact ]
338
+ public void ConfigureEndpoint_CanLoadDsaPemCerts ( )
339
+ {
340
+ var serverOptions = CreateServerOptions ( ) ;
341
+ var certificate = new X509Certificate2 ( TestResources . GetCertPath ( "https-dsa.crt" ) ) ;
342
+
343
+ var ran1 = false ;
344
+ var config = new ConfigurationBuilder ( ) . AddInMemoryCollection ( new [ ]
345
+ {
346
+ new KeyValuePair < string , string > ( "Endpoints:End1:Url" , "https://*:5001" ) ,
347
+ new KeyValuePair < string , string > ( "Certificates:Default:Path" , Path . Combine ( "shared" , "TestCertificates" , "https-dsa.crt" ) ) ,
348
+ new KeyValuePair < string , string > ( "Certificates:Default:KeyPath" , Path . Combine ( "shared" , "TestCertificates" , "https-dsa.key" ) ) ,
349
+ new KeyValuePair < string , string > ( "Certificates:Default:Password" , "asdf" ) ,
350
+ } ) . Build ( ) ;
351
+
352
+ serverOptions
353
+ . Configure ( config )
354
+ . Endpoint ( "End1" , opt =>
355
+ {
356
+ ran1 = true ;
357
+ Assert . True ( opt . IsHttps ) ;
358
+ Assert . Equal ( opt . HttpsOptions . ServerCertificate . SerialNumber , certificate . SerialNumber ) ;
359
+ } ) . Load ( ) ;
360
+
361
+ Assert . True ( ran1 ) ;
362
+ Assert . NotNull ( serverOptions . DefaultCertificate ) ;
363
+ }
364
+
365
+ [ Fact ]
366
+ public void ConfigureEndpoint_CanLoadUnprotectedDsaPemCerts ( )
367
+ {
368
+ var serverOptions = CreateServerOptions ( ) ;
369
+ var certificate = new X509Certificate2 ( TestResources . GetCertPath ( "https-dsa.crt" ) ) ;
370
+
371
+ var ran1 = false ;
372
+ var config = new ConfigurationBuilder ( ) . AddInMemoryCollection ( new [ ]
373
+ {
374
+ new KeyValuePair < string , string > ( "Endpoints:End1:Url" , "https://*:5001" ) ,
375
+ new KeyValuePair < string , string > ( "Certificates:Default:Path" , Path . Combine ( "shared" , "TestCertificates" , "https-dsa.crt" ) ) ,
376
+ new KeyValuePair < string , string > ( "Certificates:Default:KeyPath" , Path . Combine ( "shared" , "TestCertificates" , "https-dsa-no-pass.key" ) ) ,
377
+ } ) . Build ( ) ;
378
+
379
+ serverOptions
380
+ . Configure ( config )
381
+ . Endpoint ( "End1" , opt =>
382
+ {
383
+ ran1 = true ;
384
+ Assert . True ( opt . IsHttps ) ;
385
+ Assert . Equal ( opt . HttpsOptions . ServerCertificate . SerialNumber , certificate . SerialNumber ) ;
386
+ } ) . Load ( ) ;
387
+
388
+ Assert . True ( ran1 ) ;
389
+ Assert . NotNull ( serverOptions . DefaultCertificate ) ;
390
+ }
391
+
257
392
[ Fact ]
258
393
public void ConfigureEndpointDevelopmentCertificateGetsIgnoredIfPasswordIsNotCorrect ( )
259
394
{
0 commit comments