File: Scoping.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (196 lines) | stat: -rw-r--r-- 8,321 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8216417
 * @summary cleanup of IPv6 scope-id handling
 * @library /test/lib
 * @build jdk.test.lib.NetworkConfiguration
 * @run main/othervm Scoping
 */

import java.io.IOException;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

import jdk.test.lib.NetworkConfiguration;

public class Scoping {

    interface ThrowingConsumer<T> {
        public void accept(T t) throws Exception;
    }

    static List<ThrowingConsumer<InetSocketAddress>> targets = List.of(
        /* 0 */  (a) -> {Socket s = new Socket(); s.bind(a);s.close();},
        /* 1 */  (a) -> {ServerSocket s = new ServerSocket(); s.bind(a);s.close();},
        /* 2 */  (a) -> {DatagramSocket s = new DatagramSocket(null); s.bind(a); s.close();},
        /* 3 */  (a) -> {MulticastSocket s = new MulticastSocket(null); s.bind(a); s.close();},
        /* 4 */  (a) -> {SocketChannel s = SocketChannel.open(); s.bind(a);s.close();},
        /* 5 */  (a) -> {ServerSocketChannel s = ServerSocketChannel.open(); s.bind(a);s.close();},
        /* 6 */  (a) -> {DatagramChannel s = DatagramChannel.open(); s.bind(a); s.close();},
        /* 7 */  (a) -> {SocketChannel s = SocketChannel.open(); s.socket().bind(a);s.close();},
        /* 8 */  (a) -> {ServerSocketChannel s = ServerSocketChannel.open(); s.socket().bind(a);s.close();},
        /* 9 */  (a) -> {DatagramChannel s = DatagramChannel.open(); s.socket().bind(a); s.close();},
        /* 10 */ (a) -> {socketTest(a);},
        /* 11 */ (a) -> {dgSocketTest(a, false);},
        /* 12 */ (a) -> {dgSocketTest(a, true);},
        /* 13 */ (a) -> {dgChannelTest(a, false);},
        /* 14 */ (a) -> {dgChannelTest(a, true);}
    );

    static List<Inet6Address> getLinkLocalAddrs() throws IOException {
        return NetworkConfiguration.probe()
                   .ip6Addresses()
                   .filter(Inet6Address::isLinkLocalAddress)
                   .collect(Collectors.toList());
    }

    static void compare(InetSocketAddress a, InetSocketAddress b) {
        Inet6Address a1 = (Inet6Address)a.getAddress();
        Inet6Address b1 = (Inet6Address)b.getAddress();
        compare (a1, b1);
    }

    static void compare(InetAddress a, InetAddress b) {
        Inet6Address a1 = (Inet6Address)a;
        Inet6Address b1 = (Inet6Address)b;
        if (!a1.equals(b1)) {
            System.out.printf("%s != %s\n", a1, b1);
            throw new RuntimeException("Addresses not the same");
        }

        if (!a1.getHostAddress().equals(b1.getHostAddress())) {
            System.out.printf("%s != %s\n", a1, b1);
            if (a1.getScopeId() != b1.getScopeId())
                throw new RuntimeException("Scope ids not the same");
        }
    }

    static void socketTest(InetSocketAddress a) throws Exception {
        System.out.printf("socketTest: address %s\n", a);
        try (ServerSocket server = new ServerSocket(0, 5, a.getAddress())) {
            InetAddress saddr = server.getInetAddress();
            int port = server.getLocalPort();
            Socket client = new Socket(saddr, port);
            compare(client.getInetAddress(), saddr);
            try {
                client.close();
            } catch (IOException e) {}
        }
    }

    static void dgSocketTest(InetSocketAddress a, boolean connect) throws Exception {
        try (DatagramSocket s = new DatagramSocket(null)) {
            System.out.println("dgSocketTest: " + a);
            s.bind(a);
            String t = "Hello world";
            DatagramPacket rx = new DatagramPacket(new byte[128], 128);
            int port = s.getLocalPort();
            InetSocketAddress dest = new InetSocketAddress(a.getAddress(), port);
            DatagramPacket tx = new DatagramPacket(t.getBytes("ISO8859_1"), t.length(), dest);
            if (connect) {
                s.connect(dest);
                System.out.println("dgSocketTest: connect remote addr = " + s.getRemoteSocketAddress());
                compare(a, (InetSocketAddress)s.getRemoteSocketAddress());
            }
            s.send(tx);
            s.receive(rx);
            String t1 = new String(rx.getData(), rx.getOffset(), rx.getLength(), "ISO8859_1");
            if (!t1.equals(t))
                throw new RuntimeException("Packets not equal");
        }
    }

    static void dgChannelTest(InetSocketAddress a, boolean connect) throws Exception {
        try (DatagramChannel s = DatagramChannel.open()) {
            System.out.println("dgChannelTest: " + a);
            s.bind(a);
            String t = "Hello world";
            ByteBuffer rx = ByteBuffer.allocate(128);
            int port = ((InetSocketAddress)s.getLocalAddress()).getPort();
            InetSocketAddress dest = new InetSocketAddress(a.getAddress(), port);
            ByteBuffer tx = ByteBuffer.wrap(t.getBytes("ISO8859_1"));
            if (connect) {
                s.connect(dest);
                System.out.println("dgChannelTest: connect remote addr = " + s.getRemoteAddress());
                compare(a, (InetSocketAddress)s.getRemoteAddress());
            }
            s.send(tx, dest);
            s.receive(rx);
            rx.flip();
            String t1 = new String(rx.array(), 0, rx.limit(), "ISO8859_1");
            System.out.printf("rx : %s, data: %s\n", rx, t1);
            if (!t1.equals(t))
                throw new RuntimeException("Packets not equal");
        }
    }

    static Inet6Address stripScope(Inet6Address address) {
        byte[] bytes = address.getAddress();
        InetAddress result = null;
        try {
            result = InetAddress.getByAddress(bytes);
        } catch (UnknownHostException e) {
            assert false;
        }
        return (Inet6Address)result;
    }

    public static void main(String[] args) throws Exception {
        for (Inet6Address address : getLinkLocalAddrs()) {
            Inet6Address stripped = stripScope(address);
            InetSocketAddress s1 = new InetSocketAddress(address, 0);
            InetSocketAddress s2 = new InetSocketAddress(stripped, 0);
            System.out.println("Trying: " + address);
            int count = 0, success = 0;
            for (ThrowingConsumer<InetSocketAddress> target : targets) {
                try {
                    target.accept(s1);
                    System.out.println("target " + count + " OK");
                    // if that succeeds try s2 (the actual test)
                    try {
                        target.accept(s2);
                        success++;
                    } catch (IOException ee) {
                        String msg = "Failed: " + s2.toString() + "count: " + Integer.toString(count);
                        throw new RuntimeException (msg);
                    }
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                    // OK
                }
                count++;
            }
            System.out.println("Succeeded with " + success + " binds");
        }
    }
}