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