test/jdk/java/net/Inet6Address/Scoping.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 55375 96c7427456f9
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55375
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
     1
/*
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
     4
 *
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
     8
 *
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    13
 * accompanied this code).
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    14
 *
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    18
 *
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    21
 * questions.
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    22
 */
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    23
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    24
/*
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    25
 * @test
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    26
 * @bug 8216417
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    27
 * @summary cleanup of IPv6 scope-id handling
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    28
 * @library /test/lib
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    29
 * @build jdk.test.lib.NetworkConfiguration
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    30
 * @run main/othervm Scoping
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    31
 */
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    32
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    33
import java.io.IOException;
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    34
import java.net.*;
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    35
import java.nio.ByteBuffer;
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    36
import java.nio.channels.DatagramChannel;
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    37
import java.nio.channels.ServerSocketChannel;
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    38
import java.nio.channels.SocketChannel;
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    39
import java.util.Enumeration;
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    40
import java.util.LinkedList;
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    41
import java.util.List;
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    42
import java.util.stream.Collectors;
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    43
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    44
import jdk.test.lib.NetworkConfiguration;
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    45
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    46
public class Scoping {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    47
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    48
    interface ThrowingConsumer<T> {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    49
        public void accept(T t) throws Exception;
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    50
    }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    51
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    52
    static List<ThrowingConsumer<InetSocketAddress>> targets = List.of(
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    53
        /* 0 */  (a) -> {Socket s = new Socket(); s.bind(a);s.close();},
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    54
        /* 1 */  (a) -> {ServerSocket s = new ServerSocket(); s.bind(a);s.close();},
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    55
        /* 2 */  (a) -> {DatagramSocket s = new DatagramSocket(null); s.bind(a); s.close();},
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    56
        /* 3 */  (a) -> {MulticastSocket s = new MulticastSocket(null); s.bind(a); s.close();},
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    57
        /* 4 */  (a) -> {SocketChannel s = SocketChannel.open(); s.bind(a);s.close();},
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    58
        /* 5 */  (a) -> {ServerSocketChannel s = ServerSocketChannel.open(); s.bind(a);s.close();},
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    59
        /* 6 */  (a) -> {DatagramChannel s = DatagramChannel.open(); s.bind(a); s.close();},
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    60
        /* 7 */  (a) -> {SocketChannel s = SocketChannel.open(); s.socket().bind(a);s.close();},
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    61
        /* 8 */  (a) -> {ServerSocketChannel s = ServerSocketChannel.open(); s.socket().bind(a);s.close();},
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    62
        /* 9 */  (a) -> {DatagramChannel s = DatagramChannel.open(); s.socket().bind(a); s.close();},
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    63
        /* 10 */ (a) -> {socketTest(a);},
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    64
        /* 11 */ (a) -> {dgSocketTest(a, false);},
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    65
        /* 12 */ (a) -> {dgSocketTest(a, true);},
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    66
        /* 13 */ (a) -> {dgChannelTest(a, false);},
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    67
        /* 14 */ (a) -> {dgChannelTest(a, true);}
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    68
    );
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    69
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    70
    static List<Inet6Address> getLinkLocalAddrs() throws IOException {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    71
        return NetworkConfiguration.probe()
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    72
                   .ip6Addresses()
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    73
                   .filter(Inet6Address::isLinkLocalAddress)
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    74
                   .collect(Collectors.toList());
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    75
    }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    76
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    77
    static void compare(InetSocketAddress a, InetSocketAddress b) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    78
        Inet6Address a1 = (Inet6Address)a.getAddress();
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    79
        Inet6Address b1 = (Inet6Address)b.getAddress();
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    80
        compare (a1, b1);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    81
    }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    82
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    83
    static void compare(InetAddress a, InetAddress b) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    84
        Inet6Address a1 = (Inet6Address)a;
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    85
        Inet6Address b1 = (Inet6Address)b;
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    86
        if (!a1.equals(b1)) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    87
            System.out.printf("%s != %s\n", a1, b1);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    88
            throw new RuntimeException("Addresses not the same");
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    89
        }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    90
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    91
        if (!a1.getHostAddress().equals(b1.getHostAddress())) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    92
            System.out.printf("%s != %s\n", a1, b1);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    93
            if (a1.getScopeId() != b1.getScopeId())
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    94
                throw new RuntimeException("Scope ids not the same");
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    95
        }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    96
    }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    97
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    98
    static void socketTest(InetSocketAddress a) throws Exception {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
    99
        System.out.printf("socketTest: address %s\n", a);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   100
        try (ServerSocket server = new ServerSocket(0, 5, a.getAddress())) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   101
            InetAddress saddr = server.getInetAddress();
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   102
            int port = server.getLocalPort();
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   103
            Socket client = new Socket(saddr, port);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   104
            compare(client.getInetAddress(), saddr);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   105
            try {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   106
                client.close();
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   107
            } catch (IOException e) {}
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   108
        }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   109
    }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   110
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   111
    static void dgSocketTest(InetSocketAddress a, boolean connect) throws Exception {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   112
        try (DatagramSocket s = new DatagramSocket(null)) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   113
            System.out.println("dgSocketTest: " + a);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   114
            s.bind(a);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   115
            String t = "Hello world";
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   116
            DatagramPacket rx = new DatagramPacket(new byte[128], 128);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   117
            int port = s.getLocalPort();
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   118
            InetSocketAddress dest = new InetSocketAddress(a.getAddress(), port);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   119
            DatagramPacket tx = new DatagramPacket(t.getBytes("ISO8859_1"), t.length(), dest);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   120
            if (connect) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   121
                s.connect(dest);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   122
                System.out.println("dgSocketTest: connect remote addr = " + s.getRemoteSocketAddress());
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   123
                compare(a, (InetSocketAddress)s.getRemoteSocketAddress());
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   124
            }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   125
            s.send(tx);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   126
            s.receive(rx);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   127
            String t1 = new String(rx.getData(), rx.getOffset(), rx.getLength(), "ISO8859_1");
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   128
            if (!t1.equals(t))
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   129
                throw new RuntimeException("Packets not equal");
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   130
        }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   131
    }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   132
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   133
    static void dgChannelTest(InetSocketAddress a, boolean connect) throws Exception {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   134
        try (DatagramChannel s = DatagramChannel.open()) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   135
            System.out.println("dgChannelTest: " + a);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   136
            s.bind(a);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   137
            String t = "Hello world";
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   138
            ByteBuffer rx = ByteBuffer.allocate(128);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   139
            int port = ((InetSocketAddress)s.getLocalAddress()).getPort();
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   140
            InetSocketAddress dest = new InetSocketAddress(a.getAddress(), port);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   141
            ByteBuffer tx = ByteBuffer.wrap(t.getBytes("ISO8859_1"));
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   142
            if (connect) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   143
                s.connect(dest);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   144
                System.out.println("dgChannelTest: connect remote addr = " + s.getRemoteAddress());
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   145
                compare(a, (InetSocketAddress)s.getRemoteAddress());
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   146
            }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   147
            s.send(tx, dest);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   148
            s.receive(rx);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   149
            rx.flip();
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   150
            String t1 = new String(rx.array(), 0, rx.limit(), "ISO8859_1");
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   151
            System.out.printf("rx : %s, data: %s\n", rx, t1);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   152
            if (!t1.equals(t))
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   153
                throw new RuntimeException("Packets not equal");
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   154
        }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   155
    }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   156
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   157
    static Inet6Address stripScope(Inet6Address address) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   158
        byte[] bytes = address.getAddress();
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   159
        InetAddress result = null;
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   160
        try {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   161
            result = InetAddress.getByAddress(bytes);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   162
        } catch (UnknownHostException e) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   163
            assert false;
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   164
        }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   165
        return (Inet6Address)result;
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   166
    }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   167
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   168
    public static void main(String[] args) throws Exception {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   169
        for (Inet6Address address : getLinkLocalAddrs()) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   170
            Inet6Address stripped = stripScope(address);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   171
            InetSocketAddress s1 = new InetSocketAddress(address, 0);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   172
            InetSocketAddress s2 = new InetSocketAddress(stripped, 0);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   173
            System.out.println("Trying: " + address);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   174
            int count = 0, success = 0;
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   175
            for (ThrowingConsumer<InetSocketAddress> target : targets) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   176
                try {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   177
                    target.accept(s1);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   178
                    System.out.println("target " + count + " OK");
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   179
                    // if that succeeds try s2 (the actual test)
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   180
                    try {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   181
                        target.accept(s2);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   182
                        success++;
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   183
                    } catch (IOException ee) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   184
                        String msg = "Failed: " + s2.toString() + "count: " + Integer.toString(count);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   185
                        throw new RuntimeException (msg);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   186
                    }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   187
                } catch (IOException e) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   188
                    System.out.println(e.getMessage());
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   189
                    // OK
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   190
                }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   191
                count++;
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   192
            }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   193
            System.out.println("Succeeded with " + success + " binds");
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   194
        }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   195
    }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents:
diff changeset
   196
}