Path: blob/master/test/jdk/java/net/Inet6Address/B6206527.java
41149 views
/*1* Copyright (c) 2005, 2021, 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* @test 1.1 05/01/0525* @bug 620652726* @summary "cannot assign address" when binding ServerSocket on Suse 927* @library /test/lib28* @build jdk.test.lib.NetworkConfiguration29* jdk.test.lib.Platform30* @run main B620652731*/3233import java.net.*;34import java.util.*;35import jdk.test.lib.NetworkConfiguration;3637public class B6206527 {3839public static void main (String[] args) throws Exception {40Inet6Address addr = getLocalAddr();41if (addr == null) {42System.out.println("Could not find a link-local address");43return;44}4546try (ServerSocket ss = new ServerSocket()) {47System.out.println("trying LL addr: " + addr);48ss.bind(new InetSocketAddress(addr, 0));49}5051// need to remove the %scope suffix52addr = (Inet6Address) InetAddress.getByAddress (53addr.getAddress()54);5556try (ServerSocket ss = new ServerSocket()) {57System.out.println("trying LL addr: " + addr);58ss.bind(new InetSocketAddress(addr, 0));59}60}6162public static Inet6Address getLocalAddr() throws Exception {63Optional<Inet6Address> oaddr = NetworkConfiguration.probe()64.ip6Addresses()65.filter(a -> a.isLinkLocalAddress())66.findFirst();6768return oaddr.orElseGet(() -> null);69}70}717273