jdk/test/java/net/Inet6Address/B6558853.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 6115 7c523cf2bc8a
child 44671 d1d011d4654d
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
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
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6115
diff changeset
     2
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
482
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 482
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 482
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 482
diff changeset
    21
 * questions.
482
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();
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    46
            if (!nif.isUp())
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    47
                continue;
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    48
482
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    49
            for (InterfaceAddress a : nif.getInterfaceAddresses()) {
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    50
                if (a.getAddress() instanceof Inet6Address) {
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    51
                    Inet6Address a6 = (Inet6Address) a.getAddress();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    52
                    if (a6.isLinkLocalAddress()) {
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    53
                        dest = a6;
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
                    break;
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    56
                }
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    57
            }
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    58
        }
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    59
        System.out.println("Using " + dest);
482
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    60
        if (dest != null) {
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    61
            B6558853 test = new B6558853(dest, port);
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    62
            Thread thread = new Thread(test);
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    63
            thread.start();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    64
            Socket s = ss.accept();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    65
            InetAddress a = s.getInetAddress();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    66
            OutputStream out = s.getOutputStream();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    67
            out.write(1);
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    68
            out.close();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    69
            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
    70
                // 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
    71
                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
    72
            }
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    73
        }
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    74
    }
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
    public B6558853(InetAddress a, int port) {
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    77
        addr = a;
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    78
        this.port = port;
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    79
    }
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    80
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    81
    public void run() {
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    82
        try {
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    83
            Socket s = new Socket(addr, port);
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    84
            InputStream in = s.getInputStream();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    85
            int i = in.read();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    86
            in.close();
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    87
        } catch (IOException iOException) {
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    88
        }
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    89
    }
9cb089744f91 6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
diff changeset
    90
}