|
| 1 | +/* |
| 2 | + * Copyright 2012-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.reactor.netty; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import reactor.netty.tcp.SslProvider; |
| 24 | + |
| 25 | +import org.springframework.boot.ssl.SslBundle; |
| 26 | +import org.springframework.boot.ssl.pem.PemSslStoreBundle; |
| 27 | +import org.springframework.boot.ssl.pem.PemSslStoreDetails; |
| 28 | +import org.springframework.boot.testsupport.classpath.resources.WithPackageResources; |
| 29 | +import org.springframework.boot.web.server.Ssl; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests for {@link SslServerCustomizer}. |
| 35 | + * |
| 36 | + * @author Daeho Kwon |
| 37 | + */ |
| 38 | +class SslServerCustomizerTests { |
| 39 | + |
| 40 | + @Test |
| 41 | + @WithPackageResources({ "1.key", "1.crt", "2.key", "2.crt" }) |
| 42 | + void getSslProviderReturnsMappedProviderForKnownServerName() { |
| 43 | + SslBundle defaultBundle = createBundle("1.key", "1.crt"); |
| 44 | + SslBundle mappedBundle = createBundle("2.key", "2.crt"); |
| 45 | + SslServerCustomizer customizer = new SslServerCustomizer(null, Ssl.ClientAuth.NONE, defaultBundle, |
| 46 | + Map.of("mapped.example", mappedBundle)); |
| 47 | + SslProvider mapped = customizer.getSslProvider("mapped.example"); |
| 48 | + assertThat(mapped).isNotNull().isNotSameAs(customizer.getSslProvider(null)); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + @WithPackageResources({ "1.key", "1.crt", "2.key", "2.crt" }) |
| 53 | + void getSslProviderFallsBackToDefaultWhenServerNameIsUnmapped() { |
| 54 | + SslBundle defaultBundle = createBundle("1.key", "1.crt"); |
| 55 | + SslBundle mappedBundle = createBundle("2.key", "2.crt"); |
| 56 | + SslServerCustomizer customizer = new SslServerCustomizer(null, Ssl.ClientAuth.NONE, defaultBundle, |
| 57 | + Map.of("mapped.example", mappedBundle)); |
| 58 | + assertThat(customizer.getSslProvider("unmapped.example")).isSameAs(customizer.getSslProvider(null)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + @WithPackageResources({ "1.key", "1.crt" }) |
| 63 | + @SuppressWarnings("NullAway") // Test null check |
| 64 | + void getSslProviderReturnsDefaultWhenServerNameIsNull() { |
| 65 | + SslBundle defaultBundle = createBundle("1.key", "1.crt"); |
| 66 | + SslServerCustomizer customizer = new SslServerCustomizer(null, Ssl.ClientAuth.NONE, defaultBundle, |
| 67 | + Collections.emptyMap()); |
| 68 | + assertThat(customizer.getSslProvider(null)).isNotNull(); |
| 69 | + } |
| 70 | + |
| 71 | + private static SslBundle createBundle(String key, String certificate) { |
| 72 | + return SslBundle.of(new PemSslStoreBundle( |
| 73 | + new PemSslStoreDetails(null, "classpath:" + certificate, "classpath:" + key), null)); |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments