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