duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2001 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 4469866 |
| 27 | * @summary Connecting to a link-local IPv6 address should not |
| 28 | * causes a SocketException to be thrown. |
| 29 | */ |
| 30 | import java.net.*; |
| 31 | import java.util.Enumeration; |
| 32 | |
| 33 | public class LinkLocal { |
| 34 | |
| 35 | static int testCount = 0; |
| 36 | static int failed = 0; |
| 37 | |
| 38 | static void TcpTest(InetAddress ia) throws Exception { |
| 39 | System.out.println("**************************************"); |
| 40 | testCount++; |
| 41 | System.out.println("Test " + testCount + ": TCP connect to " + ia); |
| 42 | |
| 43 | /* |
| 44 | * Create ServerSocket on wildcard address and then |
| 45 | * try to connect Socket to link-local address. |
| 46 | */ |
| 47 | ServerSocket ss = new ServerSocket(0); |
| 48 | |
| 49 | Socket s = new Socket(); |
| 50 | try { |
| 51 | s.connect(new InetSocketAddress(ia, ss.getLocalPort())); |
| 52 | |
| 53 | System.out.println("Test passed - connection established."); |
| 54 | |
| 55 | // connection was established so accept it |
| 56 | Socket s2 = ss.accept(); |
| 57 | s2.close(); |
| 58 | } catch (SocketException e) { |
| 59 | failed++; |
| 60 | System.out.println("Test failed: " + e); |
| 61 | } |
| 62 | |
| 63 | // clean up |
| 64 | s.close(); |
| 65 | ss.close(); |
| 66 | } |
| 67 | |
| 68 | static void UdpTest(InetAddress ia, boolean connected) throws Exception { |
| 69 | |
| 70 | System.out.println("**************************************"); |
| 71 | testCount++; |
| 72 | |
| 73 | if (connected) { |
| 74 | System.out.println("Test " + testCount + ": UDP connect to " + ia); |
| 75 | } else { |
| 76 | System.out.println("Test " + testCount + ": UDP send to " + ia); |
| 77 | } |
| 78 | |
| 79 | DatagramSocket ds1 = new DatagramSocket(); |
| 80 | DatagramSocket ds2 = new DatagramSocket(); |
| 81 | |
| 82 | try { |
| 83 | byte b[] = "Hello".getBytes(); |
| 84 | DatagramPacket p = new DatagramPacket(b, b.length); |
| 85 | |
| 86 | if (connected) { |
| 87 | ds1.connect( new InetSocketAddress(ia, ds2.getLocalPort()) ); |
| 88 | System.out.println("DatagramSocket connected."); |
| 89 | } else { |
| 90 | p.setAddress(ia); |
| 91 | p.setPort(ds2.getLocalPort()); |
| 92 | } |
| 93 | ds1.send(p); |
| 94 | System.out.println("Packet has been sent."); |
| 95 | |
| 96 | ds2.setSoTimeout(1000); |
| 97 | ds2.receive(p); |
| 98 | System.out.println("Test passed - packet received."); |
| 99 | } catch (SocketException e) { |
| 100 | failed++; |
| 101 | System.out.println("Test failed: " + e); |
| 102 | } |
| 103 | |
| 104 | ds1.close(); |
| 105 | ds2.close(); |
| 106 | } |
| 107 | |
| 108 | static void TestAddress(InetAddress ia) throws Exception { |
| 109 | TcpTest(ia); |
| 110 | UdpTest(ia, true); /* unconnected */ |
| 111 | UdpTest(ia, false); /* connected */ |
| 112 | } |
| 113 | |
| 114 | public static void main(String args[]) throws Exception { |
| 115 | |
| 116 | /* |
| 117 | * If an argument is provided ensure that it's |
| 118 | * a link-local IPv6 address. |
| 119 | */ |
| 120 | if (args.length > 0) { |
| 121 | InetAddress ia = InetAddress.getByName(args[0]); |
| 122 | |
| 123 | if ( !(ia instanceof Inet6Address) || |
| 124 | !ia.isLinkLocalAddress()) { |
| 125 | throw new Exception(ia + |
| 126 | " is not a link-local IPv6 address"); |
| 127 | } |
| 128 | |
| 129 | TestAddress(ia); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * If no argument is provided then enumerate the |
| 134 | * local addresses and run the test on each link-local |
| 135 | * IPv6 address. |
| 136 | */ |
| 137 | if (args.length == 0) { |
| 138 | Enumeration nifs = NetworkInterface.getNetworkInterfaces(); |
| 139 | while (nifs.hasMoreElements()) { |
| 140 | NetworkInterface ni = (NetworkInterface)nifs.nextElement(); |
| 141 | Enumeration addrs = ni.getInetAddresses(); |
| 142 | while (addrs.hasMoreElements()) { |
| 143 | InetAddress addr = (InetAddress)addrs.nextElement(); |
| 144 | |
| 145 | if (addr instanceof Inet6Address && |
| 146 | addr.isLinkLocalAddress()) { |
| 147 | |
| 148 | TestAddress(addr); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Print results |
| 156 | */ |
| 157 | if (testCount == 0) { |
| 158 | System.out.println("No link-local IPv6 addresses - test skipped!"); |
| 159 | } else { |
| 160 | System.out.println("**************************************"); |
| 161 | System.out.println(testCount + " test(s) executed, " + |
| 162 | failed + " failed."); |
| 163 | if (failed > 0) { |
| 164 | throw new Exception( failed + " test(s) failed."); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |