|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import java.nio.file.Path; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | +import java.util.function.Predicate; |
| 29 | + |
| 30 | +import jdk.internal.util.OperatingSystem; |
| 31 | +import jdk.test.lib.net.IPSupport; |
| 32 | +import jdk.test.lib.process.ProcessTools; |
| 33 | +import jtreg.SkippedException; |
| 34 | + |
| 35 | +/* |
| 36 | + * @test |
| 37 | + * @bug 8332020 |
| 38 | + * @summary verifies that when jwebserver is launched with a IPv6 bind address |
| 39 | + * then the URL printed contains the correct host literal |
| 40 | + * @modules jdk.httpserver java.base/jdk.internal.util |
| 41 | + * @library /test/lib |
| 42 | + * @build jdk.test.lib.net.IPSupport |
| 43 | + * @run driver IPv6BoundHost |
| 44 | + */ |
| 45 | +public class IPv6BoundHost { |
| 46 | + |
| 47 | + private static final Path JDK_BIN_DIR = Path.of(System.getProperty("java.home")).resolve("bin"); |
| 48 | + private static final Path JWEBSERVER_BINARY = OperatingSystem.isWindows() |
| 49 | + ? JDK_BIN_DIR.resolve("jwebserver.exe") : JDK_BIN_DIR.resolve("jwebserver"); |
| 50 | + |
| 51 | + public static void main(final String[] args) throws Exception { |
| 52 | + IPSupport.printPlatformSupport(System.err); // for debug purposes |
| 53 | + if (!IPSupport.hasIPv6()) { |
| 54 | + throw new SkippedException("Skipping test - IPv6 is not supported"); |
| 55 | + } |
| 56 | + final String output = launchJwebserverAndExit(List.of("-b", "::1", "-p", "0")); |
| 57 | + if (output.contains("URL http://[::1]:") |
| 58 | + || output.contains("URL http://[0:0:0:0:0:0:0:1]:")) { |
| 59 | + // found expected content |
| 60 | + System.out.println("found expected URL in jwebserver output"); |
| 61 | + } else { |
| 62 | + throw new AssertionError("missing IPv6 address in jwebserver process output"); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static String launchJwebserverAndExit(final List<String> args) throws Exception { |
| 67 | + final Predicate<String> waitForLine = (s) -> s.startsWith("URL http://"); |
| 68 | + final StringBuilder sb = new StringBuilder(); // stdout & stderr |
| 69 | + final List<String> cmd = new ArrayList<>(); |
| 70 | + cmd.add(JWEBSERVER_BINARY.toString()); |
| 71 | + cmd.addAll(args); |
| 72 | + // start the process and await the waitForLine before returning |
| 73 | + final Process p = ProcessTools.startProcess("8332020-test", new ProcessBuilder(cmd), |
| 74 | + line -> sb.append(line).append("\n"), |
| 75 | + waitForLine, |
| 76 | + 30, // suitably high default timeout, not expected to timeout |
| 77 | + TimeUnit.SECONDS); |
| 78 | + System.out.println(sb.toString()); // print the process' stdout/stderr |
| 79 | + // the process has started and it is confirmed that the process output has the line |
| 80 | + // we were waiting for. now kill the process. |
| 81 | + p.destroy(); |
| 82 | + final int exitCode = p.waitFor(); |
| 83 | + if (!isNormalExitCode(exitCode)) { |
| 84 | + throw new AssertionError("jwebserver exited with unexpected exit code: " + exitCode); |
| 85 | + } |
| 86 | + return sb.toString(); |
| 87 | + } |
| 88 | + |
| 89 | + private static boolean isNormalExitCode(final int exitCode) { |
| 90 | + final int SIGTERM = 15; |
| 91 | + if (OperatingSystem.isWindows()) { |
| 92 | + return exitCode == 1; // we expect exit code == 1 on Windows for Process.destroy() |
| 93 | + } else { |
| 94 | + // signal terminated exit code on Unix is 128 + signal value |
| 95 | + return exitCode == (128 + SIGTERM); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments