Path: blob/master/test/jdk/java/net/Socket/B6210227.java
41152 views
/*1* Copyright (c) 2012, 2019, 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*/2223/**24* @test25* @bug 621022726* @library /test/lib27* @summary REGRESSION: Socket.getLocalAddress() returns address of 0.0.0.0 on outbound TCP.28* This test requires binding to the wildcard address.29* @run main B621022730* @run main/othervm -Djava.net.preferIPv4Stack=true B621022731* @run main/othervm -Djava.net.preferIPv6Addresses=true B621022732*/3334import java.util.*;35import java.net.*;36import java.util.stream.IntStream;37import java.util.stream.Collectors;38import jdk.test.lib.net.IPSupport;3940public class B6210227 {41public static void main(String[] args) throws Exception42{43IPSupport.throwSkippedExceptionIfNonOperational();4445ServerSocket ss = new ServerSocket(0);46int port = ss.getLocalPort();4748try {49InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(), port);50Socket s = new Socket();51s.connect( isa, 1000 );52InetAddress iaLocal = s.getLocalAddress(); // if this comes back as 0.0.0.0 this would demonstrate issue53String sLocalHostname = iaLocal.getHostName();54byte[] address = iaLocal.getAddress();55if (isWildcard(address)) {56if (iaLocal instanceof Inet6Address) {57String msg = IntStream.range(0, address.length)58.mapToObj(i -> "0").collect(Collectors.joining(":"));59throw new RuntimeException(msg + " returned");60} else {61throw new RuntimeException ("0.0.0.0 returned");62}63}64System.out.println("local hostname is "+sLocalHostname );65} catch(Exception e) {66System.out.println("Exception happened");67throw e;68} finally {69ss.close();70}71}7273private static boolean isWildcard(byte[] bytes) {74for (int i = 0; i < bytes.length ; i++) {75if (bytes[i] != 0) return false;76}77return true;78}79}808182