Path: blob/master/test/jdk/java/net/Inet6Address/B6558853.java
41149 views
/*1* Copyright (c) 2008, 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* @key intermittent26* @bug 655885327* @summary getHostAddress() on connections using IPv6 link-local addrs should have zone id28* This test needs to bind to the wildcard address and as such is succeptible to29* fail intermittently because of port reuse issues.30* @library /test/lib31* @build jdk.test.lib.NetworkConfiguration32* jdk.test.lib.Platform33* @run main B655885334*/3536import java.io.IOException;37import java.io.InputStream;38import java.io.OutputStream;39import java.net.*;40import java.util.Optional;41import jdk.test.lib.NetworkConfiguration;4243public class B6558853 implements Runnable {44private InetAddress addr = null;45private int port = 0;4647public static void main(String[] args) throws Exception {48Optional<Inet6Address> oaddr = NetworkConfiguration.probe()49.ip6Addresses()50.filter(a -> a.isLinkLocalAddress())51.findFirst();5253if (!oaddr.isPresent()) {54System.out.println("No suitable interface found. Exiting.");55return;56}5758Inet6Address dest = oaddr.get();59System.out.println("Using " + dest);6061try (ServerSocket ss = new ServerSocket(0)) {62int port = ss.getLocalPort();63B6558853 test = new B6558853(dest, port);64Thread thread = new Thread(test);65thread.start();66Socket s = ss.accept();67InetAddress a = s.getInetAddress();68OutputStream out = s.getOutputStream();69out.write(1);70out.close();71if (!(a instanceof Inet6Address) || a.getHostAddress().indexOf("%") == -1) {72// No Scope found in the address String73throw new RuntimeException("Wrong address: " + a.getHostAddress());74}75}76}7778public B6558853(InetAddress a, int port) {79addr = a;80this.port = port;81}8283public void run() {84try {85Socket s = new Socket(addr, port);86InputStream in = s.getInputStream();87int i = in.read();88in.close();89} catch (IOException iOException) {90}91}92}939495