1 /* 2 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * 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 * @library /test/lib 30 * @build jdk.test.lib.NetworkConfiguration 31 * jdk.test.lib.Utils 32 * jdk.test.lib.Asserts 33 * jdk.test.lib.JDKToolFinder 34 * jdk.test.lib.JDKToolLauncher 35 * jdk.test.lib.Platform 36 * jdk.test.lib.process.* 37 * @run main LinkLocal 38 */ 39 40 import jdk.test.lib.NetworkConfiguration; 41 42 import java.net.*; 43 import java.util.List; 44 import java.util.stream.Collectors; 45 46 public class LinkLocal { 47 48 static int testCount = 0; 49 static int failed = 0; 50 51 static void TcpTest(InetAddress ia) throws Exception { 52 System.out.println("**************************************"); 53 testCount++; 54 System.out.println("Test " + testCount + ": TCP connect to " + ia); 55 56 /* 57 * Create ServerSocket on wildcard address and then 58 * try to connect Socket to link-local address. 59 */ 60 ServerSocket ss = new ServerSocket(0); 61 62 Socket s = new Socket(); 63 try { 64 s.connect(new InetSocketAddress(ia, ss.getLocalPort())); 65 66 System.out.println("Test passed - connection established."); 67 68 // connection was established so accept it 69 Socket s2 = ss.accept(); 70 s2.close(); 71 } catch (SocketException e) { 72 failed++; 73 System.out.println("Test failed: " + e); 74 } finally { 75 s.close(); 76 ss.close(); 77 } 78 } 79 80 static void UdpTest(InetAddress ia, boolean connected) throws Exception { 81 82 System.out.println("**************************************"); 83 testCount++; 84 85 if (connected) { 86 System.out.println("Test " + testCount + ": UDP connect to " + ia); 87 } else { 88 System.out.println("Test " + testCount + ": UDP send to " + ia); 89 } 90 91 DatagramSocket ds1 = new DatagramSocket(); 92 DatagramSocket ds2 = new DatagramSocket(); 93 94 try { 95 byte b[] = "Hello".getBytes(); 96 DatagramPacket p = new DatagramPacket(b, b.length); 97 98 if (connected) { 99 ds1.connect( new InetSocketAddress(ia, ds2.getLocalPort()) ); 100 System.out.println("DatagramSocket connected."); 101 } else { 102 p.setAddress(ia); 103 p.setPort(ds2.getLocalPort()); 104 } 105 ds1.send(p); 106 System.out.println("Packet has been sent."); 107 108 ds2.setSoTimeout(5000); 109 ds2.receive(p); 110 System.out.println("Test passed - packet received."); 111 } catch (SocketException e) { 112 failed++; 113 System.out.println("Test failed: " + e); 114 } finally { 115 ds1.close(); 116 ds2.close(); 117 } 118 } 119 120 static void TestAddress(InetAddress ia) throws Exception { 121 TcpTest(ia); 122 UdpTest(ia, true); /* unconnected */ 123 UdpTest(ia, false); /* connected */ 124 } 125 126 public static void main(String args[]) throws Exception { 127 128 /* 129 * If an argument is provided ensure that it's 130 * a link-local IPv6 address. 131 */ 132 if (args.length > 0) { 133 InetAddress ia = InetAddress.getByName(args[0]); 134 135 if ( !(ia instanceof Inet6Address) || 136 !ia.isLinkLocalAddress()) { 137 throw new Exception(ia + 138 " is not a link-local IPv6 address"); 139 } 140 141 TestAddress(ia); 142 } 143 144 /* 145 * If no argument is provided then enumerate the 146 * local addresses and run the test on each link-local 147 * IPv6 address. 148 */ 149 if (args.length == 0) { 150 List<Inet6Address> addrs = NetworkConfiguration.probe() 151 .ip6Addresses() 152 .filter(Inet6Address::isLinkLocalAddress) 153 .collect(Collectors.toList()); 154 155 for (Inet6Address addr : addrs) { 156 TestAddress(addr); 157 } 158 } 159 160 /* 161 * Print results 162 */ 163 if (testCount == 0) { 164 System.out.println("No link-local IPv6 addresses - test skipped!"); 165 } else { 166 System.out.println("**************************************"); 167 System.out.println(testCount + " test(s) executed, " + 168 failed + " failed."); 169 if (failed > 0) { 170 throw new Exception( failed + " test(s) failed."); 171 } 172 } 173 } 174 }