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 /* 25 * If the platform has IPv6 we spawn a child process simulating the 26 * effect of being launched from node.js. We check that IPv6 is available in the child 27 * and report back as appropriate. 28 */ 29 30 import jdk.test.lib.Utils; 31 import java.io.*; 32 import java.net.InetAddress; 33 import java.net.Inet6Address; 34 import java.net.NetworkInterface; 35 import java.util.Collections; 36 import java.util.Enumeration; 37 38 public class UnixSocketTest { 39 40 static boolean hasIPv6() throws Exception { 41 Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); 42 for (NetworkInterface netint : Collections.list(nets)) { 43 Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); 44 for (InetAddress inetAddress : Collections.list(inetAddresses)) { 45 if (inetAddress instanceof Inet6Address) { 46 return true; 47 } 48 } 49 } 50 return false; 51 } 52 53 public static class Child { 54 public static void main(String[] args) throws Exception { 55 System.out.write('X'); 56 System.out.flush(); 57 if (hasIPv6()) { 58 System.out.println("Y"); // GOOD 59 } else 60 System.out.println("N"); // BAD 61 } 62 } 63 64 public static void main(String args[]) throws Exception { 65 66 if (!hasIPv6()) { 67 return; // can only test if IPv6 is present 68 } 69 UnixDomainSocket sock = Launcher.launchWithUnixDomainSocket("UnixSocketTest$Child"); 70 if (sock.read() != 'X') { 71 System.exit(-2); 72 } 73 if (sock.read() != 'Y') { 74 System.exit(-2); 75 } 76 } 77 } | 1 /* 2 * Copyright (c) 2018, 2020, 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.net.InetAddress; 25 import java.net.Inet6Address; 26 import java.net.NetworkInterface; 27 import java.net.UnixDomainSocketAddress; 28 import java.nio.ByteBuffer; 29 import java.nio.channels.SocketChannel; 30 import java.nio.channels.ServerSocketChannel; 31 import java.nio.file.Files; 32 import java.util.Collections; 33 import java.util.Enumeration; 34 35 import static java.net.StandardProtocolFamily.UNIX; 36 37 public class UnixSocketTest { 38 39 public static class Child1 { 40 public static void main(String[] args) throws Exception { 41 SocketChannel chan = (SocketChannel)System.inheritedChannel(); 42 ByteBuffer bb = ByteBuffer.allocate(2); 43 bb.put((byte)'X'); 44 bb.put((byte)'Y'); 45 bb.flip(); 46 chan.write(bb); 47 chan.close(); 48 } 49 } 50 51 public static class Child2 { 52 public static void main(String[] args) throws Exception { 53 ServerSocketChannel server = (ServerSocketChannel)System.inheritedChannel(); 54 SocketChannel chan = server.accept(); 55 UnixDomainSocketAddress sa = (UnixDomainSocketAddress)server.getLocalAddress(); 56 Files.delete(sa.getPath()); 57 server.close(); 58 ByteBuffer bb = ByteBuffer.allocate(2); 59 bb.put((byte)'X'); 60 bb.put((byte)'Y'); 61 bb.flip(); 62 chan.write(bb); 63 chan.close(); 64 } 65 } 66 67 public static void main(String args[]) throws Exception { 68 SocketChannel sc = Launcher.launchWithUnixSocketChannel("UnixSocketTest$Child1"); 69 ByteBuffer bb = ByteBuffer.allocate(10); 70 sc.read(bb); 71 if (bb.get(0) != 'X') { 72 System.exit(-2); 73 } 74 if (bb.get(1) != 'Y') { 75 System.exit(-2); 76 } 77 sc.close(); 78 79 sc = Launcher.launchWithUnixServerSocketChannel("UnixSocketTest$Child2"); 80 bb.clear(); 81 sc.read(bb); 82 if (bb.get(0) != 'X') { 83 System.exit(-2); 84 } 85 if (bb.get(1) != 'Y') { 86 System.exit(-2); 87 } 88 } 89 } |