1 /* 2 * Copyright (c) 2003, 2015, 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 4932074 26 * @summary Test that startListening(Map) method of the com.sun.jdi.SocketListen 27 * Connector returns an address that is usable for the address option on 28 * remove debuggees. 29 * @modules jdk.jdi 30 */ 31 import java.net.InetAddress; 32 import java.net.Inet4Address; 33 import java.net.NetworkInterface; 34 import java.io.IOException; 35 import java.util.*; 36 import com.sun.jdi.*; 37 import com.sun.jdi.connect.*; 38 39 public class ListenAddress { 40 41 /* 42 * Find Connector by name 43 */ 44 private static Connector findConnector(String name) { 45 List connectors = Bootstrap.virtualMachineManager().allConnectors(); 46 Iterator iter = connectors.iterator(); 47 while (iter.hasNext()) { 48 Connector connector = (Connector)iter.next(); 49 if (connector.name().equals(name)) { 50 return connector; 51 } 52 } 53 return null; 54 } 55 56 /* 57 * Failure count 58 */ 59 static int failures = 0; 60 61 /* 62 * Start listening with the specified Connector and check that the 63 * returned address includes a host component. If 'addr' is not null 64 * then set the localAddress argument to be the address. 65 */ 66 private static void check(ListeningConnector connector, InetAddress addr) 67 throws IOException, IllegalConnectorArgumentsException 68 { 69 Map args = connector.defaultArguments(); 70 if (addr != null) { 71 Connector.StringArgument addr_arg = 72 (Connector.StringArgument)args.get("localAddress"); 73 addr_arg.setValue(addr.getHostAddress()); 74 } 75 76 String address = connector.startListening(args); 77 if (address.indexOf(':') < 0) { 78 System.out.println(address + " => Failed - no host component!"); 79 failures++; 80 } else { 81 System.out.println(address); 82 } 83 connector.stopListening(args); 84 } 85 86 public static void main(String args[]) throws Exception { 87 ListeningConnector connector = (ListeningConnector)findConnector("com.sun.jdi.SocketListen"); 88 89 // check wildcard address 90 check(connector, (InetAddress)null); 91 92 // iterate over all IPv4 addresses and check that binding to 93 // that address results in the correct result from startListening(Map) 94 Enumeration nifs = NetworkInterface.getNetworkInterfaces(); 95 while (nifs.hasMoreElements()) { 96 NetworkInterface ni = (NetworkInterface)nifs.nextElement(); 97 Enumeration addrs = ni.getInetAddresses(); 98 while (addrs.hasMoreElements()) { 99 InetAddress addr = (InetAddress)addrs.nextElement(); 100 101 // JPDA implementation only currently supports IPv4 102 if (!(addr instanceof Inet4Address)) { 103 continue; 104 } 105 106 check(connector, addr); 107 } 108 } 109 110 if (failures > 0) { 111 throw new RuntimeException(failures + " test(s) failed - see output for details."); 112 } 113 } 114 }