|
| 1 | +// Copyright (c) Nate McMaster. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System.Security.Cryptography.X509Certificates; |
| 5 | +using McMaster.AspNetCore.Kestrel.Certificates; |
| 6 | +using Microsoft.AspNetCore.Connections; |
| 7 | +using Microsoft.AspNetCore.Hosting; |
| 8 | +using Microsoft.AspNetCore.Server.Kestrel.Https; |
| 9 | +using Moq; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace LettuceEncrypt.UnitTests; |
| 13 | + |
| 14 | +using SelectorFunc = Func<ConnectionContext, string, X509Certificate2>; |
| 15 | + |
| 16 | +public class KestrelHttpsOptionsExtensionsTests |
| 17 | +{ |
| 18 | + [Fact] |
| 19 | + public void UseServerCertificateSelectorFallsbackToOriginalSelector() |
| 20 | + { |
| 21 | + var injectedSelector = new Mock<IServerCertificateSelector>(); |
| 22 | + injectedSelector |
| 23 | + .Setup(c => c.Select(It.IsAny<ConnectionContext>(), It.IsAny<string>())) |
| 24 | + .Returns(() => null); |
| 25 | + |
| 26 | + var originalSelectorWasCalled = false; |
| 27 | + SelectorFunc originalSelector = (_, __) => { originalSelectorWasCalled = true; return null; }; |
| 28 | + |
| 29 | + var options = new HttpsConnectionAdapterOptions |
| 30 | + { |
| 31 | + ServerCertificateSelector = originalSelector |
| 32 | + }; |
| 33 | + |
| 34 | + KestrelHttpsOptionsExtensions.UseServerCertificateSelector(options, injectedSelector.Object); |
| 35 | + options.ServerCertificateSelector(null, null); |
| 36 | + |
| 37 | + Assert.NotSame(options.ServerCertificateSelector, originalSelector); |
| 38 | + Assert.True(originalSelectorWasCalled); |
| 39 | + injectedSelector.VerifyAll(); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void UseServerCertificateSelectorDoesNotCallFallback() |
| 44 | + { |
| 45 | + var injectedSelector = new Mock<IServerCertificateSelector>(); |
| 46 | + injectedSelector |
| 47 | + .Setup(c => c.Select(It.IsAny<ConnectionContext>(), It.IsAny<string>())) |
| 48 | + .Returns(() => TestUtils.CreateTestCert("foo.test")); |
| 49 | + |
| 50 | + var originalSelectorWasCalled = false; |
| 51 | + SelectorFunc originalSelector = (_, __) => { originalSelectorWasCalled = true; return null; }; |
| 52 | + |
| 53 | + var options = new HttpsConnectionAdapterOptions |
| 54 | + { |
| 55 | + ServerCertificateSelector = originalSelector |
| 56 | + }; |
| 57 | + |
| 58 | + KestrelHttpsOptionsExtensions.UseServerCertificateSelector(options, injectedSelector.Object); |
| 59 | + options.ServerCertificateSelector(null, null); |
| 60 | + |
| 61 | + Assert.NotSame(options.ServerCertificateSelector, originalSelector); |
| 62 | + Assert.False(originalSelectorWasCalled); |
| 63 | + injectedSelector.VerifyAll(); |
| 64 | + } |
| 65 | +} |
0 commit comments