Path: blob/master/test/jdk/java/net/Inet6Address/Scoping.java
41149 views
/*1* Copyright (c) 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 821641726* @summary cleanup of IPv6 scope-id handling27* @library /test/lib28* @build jdk.test.lib.NetworkConfiguration29* @run main/othervm Scoping30*/3132import java.io.IOException;33import java.net.*;34import java.nio.ByteBuffer;35import java.nio.channels.DatagramChannel;36import java.nio.channels.ServerSocketChannel;37import java.nio.channels.SocketChannel;38import java.util.Enumeration;39import java.util.LinkedList;40import java.util.List;41import java.util.stream.Collectors;4243import jdk.test.lib.NetworkConfiguration;4445public class Scoping {4647interface ThrowingConsumer<T> {48public void accept(T t) throws Exception;49}5051static List<ThrowingConsumer<InetSocketAddress>> targets = List.of(52/* 0 */ (a) -> {Socket s = new Socket(); s.bind(a);s.close();},53/* 1 */ (a) -> {ServerSocket s = new ServerSocket(); s.bind(a);s.close();},54/* 2 */ (a) -> {DatagramSocket s = new DatagramSocket(null); s.bind(a); s.close();},55/* 3 */ (a) -> {MulticastSocket s = new MulticastSocket(null); s.bind(a); s.close();},56/* 4 */ (a) -> {SocketChannel s = SocketChannel.open(); s.bind(a);s.close();},57/* 5 */ (a) -> {ServerSocketChannel s = ServerSocketChannel.open(); s.bind(a);s.close();},58/* 6 */ (a) -> {DatagramChannel s = DatagramChannel.open(); s.bind(a); s.close();},59/* 7 */ (a) -> {SocketChannel s = SocketChannel.open(); s.socket().bind(a);s.close();},60/* 8 */ (a) -> {ServerSocketChannel s = ServerSocketChannel.open(); s.socket().bind(a);s.close();},61/* 9 */ (a) -> {DatagramChannel s = DatagramChannel.open(); s.socket().bind(a); s.close();},62/* 10 */ (a) -> {socketTest(a);},63/* 11 */ (a) -> {dgSocketTest(a, false);},64/* 12 */ (a) -> {dgSocketTest(a, true);},65/* 13 */ (a) -> {dgChannelTest(a, false);},66/* 14 */ (a) -> {dgChannelTest(a, true);}67);6869static List<Inet6Address> getLinkLocalAddrs() throws IOException {70return NetworkConfiguration.probe()71.ip6Addresses()72.filter(Inet6Address::isLinkLocalAddress)73.collect(Collectors.toList());74}7576static void compare(InetSocketAddress a, InetSocketAddress b) {77Inet6Address a1 = (Inet6Address)a.getAddress();78Inet6Address b1 = (Inet6Address)b.getAddress();79compare (a1, b1);80}8182static void compare(InetAddress a, InetAddress b) {83Inet6Address a1 = (Inet6Address)a;84Inet6Address b1 = (Inet6Address)b;85if (!a1.equals(b1)) {86System.out.printf("%s != %s\n", a1, b1);87throw new RuntimeException("Addresses not the same");88}8990if (!a1.getHostAddress().equals(b1.getHostAddress())) {91System.out.printf("%s != %s\n", a1, b1);92if (a1.getScopeId() != b1.getScopeId())93throw new RuntimeException("Scope ids not the same");94}95}9697static void socketTest(InetSocketAddress a) throws Exception {98System.out.printf("socketTest: address %s\n", a);99try (ServerSocket server = new ServerSocket(0, 5, a.getAddress())) {100InetAddress saddr = server.getInetAddress();101int port = server.getLocalPort();102Socket client = new Socket(saddr, port);103compare(client.getInetAddress(), saddr);104try {105client.close();106} catch (IOException e) {}107}108}109110static void dgSocketTest(InetSocketAddress a, boolean connect) throws Exception {111try (DatagramSocket s = new DatagramSocket(null)) {112System.out.println("dgSocketTest: " + a);113s.bind(a);114String t = "Hello world";115DatagramPacket rx = new DatagramPacket(new byte[128], 128);116int port = s.getLocalPort();117InetSocketAddress dest = new InetSocketAddress(a.getAddress(), port);118DatagramPacket tx = new DatagramPacket(t.getBytes("ISO8859_1"), t.length(), dest);119if (connect) {120s.connect(dest);121System.out.println("dgSocketTest: connect remote addr = " + s.getRemoteSocketAddress());122compare(a, (InetSocketAddress)s.getRemoteSocketAddress());123}124s.send(tx);125s.receive(rx);126String t1 = new String(rx.getData(), rx.getOffset(), rx.getLength(), "ISO8859_1");127if (!t1.equals(t))128throw new RuntimeException("Packets not equal");129}130}131132static void dgChannelTest(InetSocketAddress a, boolean connect) throws Exception {133try (DatagramChannel s = DatagramChannel.open()) {134System.out.println("dgChannelTest: " + a);135s.bind(a);136String t = "Hello world";137ByteBuffer rx = ByteBuffer.allocate(128);138int port = ((InetSocketAddress)s.getLocalAddress()).getPort();139InetSocketAddress dest = new InetSocketAddress(a.getAddress(), port);140ByteBuffer tx = ByteBuffer.wrap(t.getBytes("ISO8859_1"));141if (connect) {142s.connect(dest);143System.out.println("dgChannelTest: connect remote addr = " + s.getRemoteAddress());144compare(a, (InetSocketAddress)s.getRemoteAddress());145}146s.send(tx, dest);147s.receive(rx);148rx.flip();149String t1 = new String(rx.array(), 0, rx.limit(), "ISO8859_1");150System.out.printf("rx : %s, data: %s\n", rx, t1);151if (!t1.equals(t))152throw new RuntimeException("Packets not equal");153}154}155156static Inet6Address stripScope(Inet6Address address) {157byte[] bytes = address.getAddress();158InetAddress result = null;159try {160result = InetAddress.getByAddress(bytes);161} catch (UnknownHostException e) {162assert false;163}164return (Inet6Address)result;165}166167public static void main(String[] args) throws Exception {168for (Inet6Address address : getLinkLocalAddrs()) {169Inet6Address stripped = stripScope(address);170InetSocketAddress s1 = new InetSocketAddress(address, 0);171InetSocketAddress s2 = new InetSocketAddress(stripped, 0);172System.out.println("Trying: " + address);173int count = 0, success = 0;174for (ThrowingConsumer<InetSocketAddress> target : targets) {175try {176target.accept(s1);177System.out.println("target " + count + " OK");178// if that succeeds try s2 (the actual test)179try {180target.accept(s2);181success++;182} catch (IOException ee) {183String msg = "Failed: " + s2.toString() + "count: " + Integer.toString(count);184throw new RuntimeException (msg);185}186} catch (IOException e) {187System.out.println(e.getMessage());188// OK189}190count++;191}192System.out.println("Succeeded with " + success + " binds");193}194}195}196197198