Path: blob/master/test/jdk/sun/management/jmxremote/bootstrap/RmiRegistrySslTest.java
41153 views
/*1* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import jdk.test.lib.process.OutputAnalyzer;24import jdk.test.lib.process.ProcessTools;25import jdk.test.lib.Utils;2627import java.io.BufferedReader;28import java.io.BufferedWriter;29import java.io.IOException;30import java.net.BindException;31import java.nio.charset.Charset;32import java.nio.file.FileSystem;33import java.nio.file.FileSystems;34import java.nio.file.Files;35import java.nio.file.Path;36import java.util.*;37import java.util.regex.Pattern;3839/**40* @test41* @bug 622823142* @summary Test that RMI registry uses SSL.43* @author Luis-Miguel Alventosa, Taras Ledkov44*45* @library /test/lib46*47* @build RmiRegistrySslTestApp48* @run main/timeout=300 RmiRegistrySslTest49*/50public class RmiRegistrySslTest {51private final String TEST_CLASS_PATH = System.getProperty("test.class.path");52private final String TEST_CLASSES = System.getProperty("test.classes");53private final String TEST_SRC = System.getProperty("test.src");54private final FileSystem FS = FileSystems.getDefault();5556private final Path libDir = FS.getPath(TEST_CLASSES, "lib");57private final Path rmiRegistryTemplate = FS.getPath(TEST_SRC, "rmiregistry.properties");58private final Path rmiRegistrySslTemplate = FS.getPath(TEST_SRC, "rmiregistryssl.properties");59private final Path rmiRegistryFile = libDir.resolve("rmiregistry.properties");60private final Path rmiRegistrySslFile = libDir.resolve("rmiregistryssl.properties");61private final String className = "RmiRegistrySslTestApp";62private int failures = 0;63private int port = 4444;64private static int MAX_GET_FREE_PORT_TRIES = 10;65private Map<String, Object> model = new HashMap<>();6667private RmiRegistrySslTest() {68try {69MAX_GET_FREE_PORT_TRIES = Integer.parseInt(System.getProperty("test.getfreeport.max.tries", "10"));70} catch (NumberFormatException ex) {71}72}7374private void initPort() {75try {76port = Utils.getFreePort();77} catch (Exception e) {78}79model.put("${getFreePort}", new Integer(port));80}8182private void initTestEnvironment() throws IOException {83initPort();8485Files.deleteIfExists(rmiRegistryFile);86Files.deleteIfExists(rmiRegistrySslFile);87libDir.toFile().mkdir();88createFileByTemplate(rmiRegistryTemplate, rmiRegistryFile, model);89createFileByTemplate(rmiRegistrySslTemplate, rmiRegistrySslFile, model);90}9192public static void createFileByTemplate(Path template, Path out, Map<String, Object> model) throws IOException {93if (Files.exists(out) && Files.isRegularFile(out)) {94try {95Files.delete(out);96} catch (Exception ex) {97System.out.println("WARNING: " + out.toFile().getAbsolutePath() + " already exists - unable to remove old copy");98ex.printStackTrace();99}100}101102try (BufferedReader br = Files.newBufferedReader(template, Charset.defaultCharset());103BufferedWriter bw = Files.newBufferedWriter(out, Charset.defaultCharset())) {104String line;105while ((line = br.readLine()) != null) {106if (model != null) {107for (Map.Entry<String, Object> macro : model.entrySet()) {108line = line.replaceAll(Pattern.quote(macro.getKey()), macro.getValue().toString());109}110}111112bw.write(line, 0, line.length());113bw.newLine();114}115}116}117118public void runTest(String[] args) throws Exception {119120test1();121test2();122test3();123124if (failures == 0) {125System.out.println("All test(s) passed");126} else {127throw new Error(String.format("%d test(s) failed", failures));128}129}130131private void test1() throws Exception {132System.out.println("-------------------------------------------------------------");133System.out.println(getClass().getName() + " : Non SSL RMIRegistry - Non SSL Lookup");134System.out.println("-------------------------------------------------------------");135136int res = doTest("-DtestID=Test1",137"-Dcom.sun.management.config.file=" + rmiRegistryFile.toFile().getAbsolutePath());138139if (res != 0) {140++failures;141}142}143144private void test2() throws Exception {145System.out.println("-------------------------------------------------------------");146System.out.println(getClass().getName() + " : SSL RMIRegistry - Non SSL Lookup");147System.out.println("-------------------------------------------------------------");148149int res = doTest("-DtestID=Test2",150"-Dcom.sun.management.config.file=" + rmiRegistrySslFile.toFile().getAbsolutePath());151152if (res != 0) {153++failures;154}155}156157private void test3() throws Exception {158159System.out.println("-------------------------------------------------------------");160System.out.println(getClass().getName() + " : SSL RMIRegistry - SSL Lookup");161System.out.println("-------------------------------------------------------------");162163int res = doTest("-DtestID=Test3",164"-Djavax.net.ssl.keyStore=" + FS.getPath(TEST_SRC, "ssl", "keystore").toFile().getAbsolutePath(),165"-Djavax.net.ssl.keyStorePassword=password",166"-Djavax.net.ssl.trustStore=" + FS.getPath(TEST_SRC, "ssl", "truststore").toFile().getAbsolutePath(),167"-Djavax.net.ssl.trustStorePassword=trustword",168"-Dcom.sun.management.config.file=" + rmiRegistrySslFile.toFile().getAbsolutePath());169170if (res != 0) {171++failures;172}173}174175private int doTest(String... args) throws Exception {176177for (int i = 0; i < MAX_GET_FREE_PORT_TRIES; ++i) {178179initTestEnvironment();180181List<String> command = new ArrayList<>();182Collections.addAll(command, Utils.getTestJavaOpts());183command.add("-Dtest.src=" + TEST_SRC);184command.add("-Dtest.rmi.port=" + port);185command.addAll(Arrays.asList(args));186command.add("-cp");187command.add(TEST_CLASS_PATH);188command.add(className);189190ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(command.toArray(new String[command.size()]));191192OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);193194System.out.println("test output:");195System.out.println(output.getOutput());196197if (!output.getOutput().contains("Exception thrown by the agent : " +198"java.rmi.server.ExportException: Port already in use")) {199return output.getExitValue();200}201}202throw new Error("Cannot find free port");203}204205public static void main(String[] args) throws Exception {206RmiRegistrySslTest test = new RmiRegistrySslTest();207208test.runTest(args);209}210}211212213