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