|
| 1 | +/* |
| 2 | + * JBoss, Home of Professional Open Source. |
| 3 | + * Copyright 2024 Red Hat, Inc., and individual contributors |
| 4 | + * as indicated by the @author tags. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package io.undertow.server.ssl; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.nio.file.Paths; |
| 25 | +import java.util.concurrent.Executor; |
| 26 | +import javax.net.ssl.SSLContext; |
| 27 | + |
| 28 | +import io.undertow.server.handlers.accesslog.AccessLogFileTestCase; |
| 29 | +import io.undertow.server.handlers.accesslog.AccessLogHandler; |
| 30 | +import io.undertow.server.handlers.accesslog.DefaultAccessLogReceiver; |
| 31 | +import io.undertow.testutils.DefaultServer; |
| 32 | +import io.undertow.testutils.HttpClientUtils; |
| 33 | +import io.undertow.testutils.TestHttpClient; |
| 34 | +import io.undertow.util.CompletionLatchHandler; |
| 35 | +import io.undertow.util.StatusCodes; |
| 36 | +import org.apache.http.HttpResponse; |
| 37 | +import org.apache.http.client.methods.HttpGet; |
| 38 | +import org.junit.Assert; |
| 39 | +import org.junit.Test; |
| 40 | +import org.junit.runner.RunWith; |
| 41 | + |
| 42 | +@RunWith(DefaultServer.class) |
| 43 | +public class SslProtocolAttributeTestCase { |
| 44 | + private static final Path logDirectory = Paths.get(System.getProperty("java.io.tmpdir")); |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testTlsRequestViaLogging() throws IOException, InterruptedException { |
| 48 | + logDirectory.toFile().mkdirs(); |
| 49 | + Path logFileName = logDirectory.resolve("server1.log"); |
| 50 | + File logFile = logFileName.toFile(); |
| 51 | + logFile.createNewFile(); |
| 52 | + logFile.deleteOnExit(); |
| 53 | + |
| 54 | + AccessLogReceiver logReceiver = new AccessLogReceiver(DefaultServer.getWorker(), logDirectory, |
| 55 | + "server1.", "log"); |
| 56 | + |
| 57 | + String formatString = "SSL Protocol is %{SSL_PROTOCOL}."; |
| 58 | + CompletionLatchHandler latchHandler= new CompletionLatchHandler( |
| 59 | + new AccessLogHandler(exchange -> exchange.getResponseSender().send("ping"), |
| 60 | + logReceiver, formatString, |
| 61 | + AccessLogFileTestCase.class.getClassLoader())); |
| 62 | + DefaultServer.setRootHandler(latchHandler); |
| 63 | + |
| 64 | + try(TestHttpClient client = new TestHttpClient()) { |
| 65 | + DefaultServer.startSSLServer(); |
| 66 | + SSLContext sslContext = DefaultServer.getClientSSLContext(); |
| 67 | + client.setSSLContext(sslContext); |
| 68 | + |
| 69 | + HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress() + "/path"); |
| 70 | + HttpResponse result = client.execute(get); |
| 71 | + Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode()); |
| 72 | + Assert.assertEquals("ping", HttpClientUtils.readResponse(result)); |
| 73 | + latchHandler.await(); |
| 74 | + logReceiver.awaitWrittenForTest(); |
| 75 | + Assert.assertEquals(formatString.replaceAll("%\\{SSL_PROTOCOL}", sslContext.getProtocol()) + System.lineSeparator(), |
| 76 | + new String(Files.readAllBytes(logFileName))); |
| 77 | + } finally { |
| 78 | + DefaultServer.stopSSLServer(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private static class AccessLogReceiver extends DefaultAccessLogReceiver { |
| 83 | + AccessLogReceiver(final Executor logWriteExecutor, |
| 84 | + final Path outputDirectory, |
| 85 | + final String logBaseName, |
| 86 | + final String logNameSuffix) { |
| 87 | + super(logWriteExecutor, outputDirectory, logBaseName, logNameSuffix, true); |
| 88 | + } |
| 89 | + |
| 90 | + public void awaitWrittenForTest() throws InterruptedException { |
| 91 | + super.awaitWrittenForTest(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments