1 /*
   2  * Copyright (c) 2018, 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 /* @test
  25  * @bug 8163083
  26  * @summary verifies that multiple listeners could be started using wildcard port number
  27  */
  28 
  29 
  30 import com.sun.jdi.*;
  31 import com.sun.jdi.connect.*;
  32 
  33 import java.io.IOException;
  34 import java.io.PrintStream;
  35 
  36 import java.util.Map;
  37 
  38 public class WildcardPortSupport {
  39 
  40     private static final String PORT_ARG = "port";
  41 
  42     public static void main(String argv[]) throws Exception {
  43         WildcardPortSupport test = new WildcardPortSupport();
  44         test.runAllTests();
  45     }
  46 
  47     public void runAllTests() throws Exception {
  48         ListeningConnector connector =
  49                 Bootstrap.virtualMachineManager().listeningConnectors().stream().
  50                         filter(c -> c.name().equals("com.sun.jdi.SocketListen")).findFirst().get();
  51 
  52         if (connector == null) {
  53             throw new RuntimeException("FAILURE: no com.sun.jdi.SocketListen connectors found!");
  54         }
  55 
  56         testWithDefaultArgs1(connector);
  57         testWithDefaultArgs2(connector);
  58         testWithWildcardPort1(connector);
  59         testWithWildcardPort2(connector);
  60         testWithDefaultArgsNegative(connector);
  61     }
  62 
  63 
  64     // Start listeners with unspecified port number and use their bound port numbers to stop them
  65     private void testWithDefaultArgs1(ListeningConnector connector) throws IOException,
  66             IllegalConnectorArgumentsException {
  67         int port1 = startListening(connector, connector.defaultArguments());
  68         int port2 = startListening(connector, connector.defaultArguments());
  69         connector.stopListening(getArgumentsMap(connector, port1));
  70         connector.stopListening(getArgumentsMap(connector, port2));
  71     }
  72 
  73     // Start listeners with unspecified port number and use the original argument map instances to stop them
  74     private void testWithDefaultArgs2(ListeningConnector connector) throws IOException,
  75             IllegalConnectorArgumentsException {
  76         Map<String, Connector.Argument> args1 = connector.defaultArguments();
  77         startListening(connector, args1);
  78         Map<String, Connector.Argument> args2 = connector.defaultArguments();
  79         startListening(connector, args2);
  80         connector.stopListening(args1);
  81         connector.stopListening(args2);
  82     }
  83 
  84     // Start listeners with wildcard  port number ("0") and use their bound port numbers to stop them
  85     private void testWithWildcardPort1(ListeningConnector connector) throws IOException,
  86             IllegalConnectorArgumentsException {
  87         int port1 = startListening(connector, getArgumentsMap(connector, 0));
  88         int port2 = startListening(connector, getArgumentsMap(connector, 0));
  89         connector.stopListening(getArgumentsMap(connector, port1));
  90         connector.stopListening(getArgumentsMap(connector, port2));
  91     }
  92 
  93     // Start listeners with wildcard port number ("0") and use the original argument map instances to stop them
  94     private void testWithWildcardPort2(ListeningConnector connector) throws IOException,
  95             IllegalConnectorArgumentsException {
  96         Map<String, Connector.Argument> args1 = getArgumentsMap(connector, 0);
  97         startListening(connector, args1);
  98         Map<String, Connector.Argument> args2 = getArgumentsMap(connector, 0);
  99         startListening(connector, args2);
 100         connector.stopListening(args1);
 101         connector.stopListening(args2);
 102     }
 103 
 104     // Tries to start two listeners using the same instance of default argument map
 105     private void testWithDefaultArgsNegative(ListeningConnector connector) throws IOException,
 106             IllegalConnectorArgumentsException {
 107         Map<String, Connector.Argument> args = connector.defaultArguments();
 108         connector.startListening(args);
 109         String port = args.get(PORT_ARG).value();
 110         if (port.isEmpty() || "0".equals(port)) {
 111             throw new RuntimeException("Test testWithDefaultArgsNegative failed." +
 112                     " The argument map was not updated with the bound port number.");
 113         }
 114         try {
 115             // This call should fail since the previous the argument map is
 116             // already updated with the port number of the started listener
 117             connector.startListening(args);
 118         } catch (IllegalConnectorArgumentsException ex) {
 119             System.out.println("Expected exception caught" + ex.getMessage());
 120             return;
 121         } finally {
 122             connector.stopListening(args);
 123         }
 124         throw new RuntimeException("Test testWithDefaultArgsNegative failed. No expected " +
 125                 "com.sun.jdi.IllegalConnectorArgumentsException exception was thrown.");
 126     }
 127 
 128     private int startListening(ListeningConnector connector, Map<String, Connector.Argument> args)
 129             throws IOException, IllegalConnectorArgumentsException {
 130         String address = connector.startListening(args);
 131         return Integer.valueOf(address.split(":")[1]);
 132     }
 133 
 134 
 135     private Map<String, Connector.Argument> getArgumentsMap(ListeningConnector connector, int port) {
 136         Map<String, Connector.Argument> args = connector.defaultArguments();
 137         Connector.Argument arg = args.get(PORT_ARG);
 138         arg.setValue(String.valueOf(port));
 139         return args;
 140     }
 141 }