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 8195160 26 * @summary Test the java.net.socket.GetLocalAddress method 27 * @requires (os.family == "linux") 28 * @library .. /test/lib 29 * @build RsocketTest 30 * @run main/othervm GetLocalAddress 31 */ 32 33 import java.net.InetAddress; 34 import java.net.InetSocketAddress; 35 import java.net.StandardProtocolFamily; 36 import java.net.ServerSocket; 37 import java.net.Socket; 38 import jdk.net.RdmaSockets; 39 40 import jtreg.SkippedException; 41 42 public class GetLocalAddress implements Runnable { 43 static Socket s; 44 static ServerSocket ss; 45 static InetAddress addr; 46 static int port; 47 48 public static void main(String args[]) throws Exception { 49 if (!RsocketTest.isRsocketAvailable()) 50 throw new SkippedException("rsocket is not available"); 51 52 testBindNull(); 53 54 boolean error = true; 55 int value = 0; 56 addr = InetAddress.getLocalHost(); 57 58 ss = RdmaSockets.openServerSocket(StandardProtocolFamily.INET); 59 s = RdmaSockets.openSocket(StandardProtocolFamily.INET); 60 addr = InetAddress.getLocalHost(); 61 ss.bind(new InetSocketAddress(addr, 0)); 62 63 port = ss.getLocalPort(); 64 65 Thread t = new Thread(new GetLocalAddress()); 66 t.start(); 67 68 Socket soc = ss.accept(); 69 70 if(addr.equals(soc.getLocalAddress())) { 71 error = false; 72 } 73 if (error) 74 throw new RuntimeException("Socket.GetLocalAddress failed."); 75 soc.close(); 76 } 77 78 public void run() { 79 try { 80 s.connect(new InetSocketAddress(addr, port)); 81 } catch (Exception e) { 82 e.printStackTrace(); 83 throw new RuntimeException("Socket.GetLocalAddress failed."); 84 } 85 } 86 87 static void testBindNull() throws Exception { 88 try { 89 Socket soc = RdmaSockets.openSocket(StandardProtocolFamily.INET); 90 soc.bind(null); 91 if (!soc.isBound()) 92 throw new RuntimeException( 93 "should be bound after bind(null)"); 94 if (soc.getLocalPort() <= 0) 95 throw new RuntimeException( 96 "bind(null) failed, local port: " + soc.getLocalPort()); 97 if (soc.getLocalAddress() == null) 98 throw new RuntimeException( 99 "bind(null) failed, local address is null"); 100 } catch (Exception e) { 101 e.printStackTrace(); 102 throw new RuntimeException("Socket.GetLocalAddress failed."); 103 } 104 } 105 }