jdk/test/java/net/Inet6Address/B6558853.java
author jccollet
Fri, 18 Apr 2008 15:23:27 +0200
changeset 482 9cb089744f91
child 5506 202f599c92aa
permissions -rw-r--r--
6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id Summary: Set the scope_id_set flag when necessary Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
482
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
     1
/*
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
     2
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
     4
 *
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
     8
 *
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    13
 * accompanied this code).
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    14
 *
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    18
 *
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    21
 * have any questions.
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    22
 */
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    23
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    24
/**
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    25
 * @test
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    26
 * @bug 6558853
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    27
 * @summary  getHostAddress() on connections using IPv6 link-local addrs should have zone id
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    28
 */
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    29
import java.io.IOException;
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    30
import java.io.InputStream;
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    31
import java.io.OutputStream;
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    32
import java.net.*;
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    33
import java.util.Enumeration;
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    34
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    35
public class B6558853 implements Runnable {
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    36
    private InetAddress addr = null;
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    37
    private int port = 0;
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    38
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    39
    public static void main(String[] args) throws Exception {
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    40
        ServerSocket ss = new ServerSocket(0);
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    41
        int port = ss.getLocalPort();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    42
        Enumeration<NetworkInterface> l = NetworkInterface.getNetworkInterfaces();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    43
        InetAddress dest = null;
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    44
        while (l.hasMoreElements() && dest == null) {
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    45
            NetworkInterface nif = l.nextElement();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    46
            for (InterfaceAddress a : nif.getInterfaceAddresses()) {
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    47
                if (a.getAddress() instanceof Inet6Address) {
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    48
                    Inet6Address a6 = (Inet6Address) a.getAddress();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    49
                    if (a6.isLinkLocalAddress()) {
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    50
                        dest = a6;
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    51
                    }
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    52
                    break;
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    53
                }
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    54
            }
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    55
        }
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    56
        if (dest != null) {
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    57
            B6558853 test = new B6558853(dest, port);
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    58
            Thread thread = new Thread(test);
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    59
            thread.start();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    60
            Socket s = ss.accept();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    61
            InetAddress a = s.getInetAddress();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    62
            OutputStream out = s.getOutputStream();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    63
            out.write(1);
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    64
            out.close();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    65
            if (!(a instanceof Inet6Address) || a.getHostAddress().indexOf("%") == -1) {
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    66
                // No Scope found in the address String
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    67
                throw new RuntimeException("Wrong address: " + a.getHostAddress());
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    68
            }
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    69
        }
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    70
    }
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    71
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    72
    public B6558853(InetAddress a, int port) {
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    73
        addr = a;
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    74
        this.port = port;
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    75
    }
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    76
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    77
    public void run() {
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    78
        try {
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    79
            Socket s = new Socket(addr, port);
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    80
            InputStream in = s.getInputStream();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    81
            int i = in.read();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    82
            in.close();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    83
        } catch (IOException iOException) {
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    84
        }
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    85
    }
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    86
}