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  */
  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         } finally {
  62             s.close();
  63             ss.close();
  64         }
  65     }
  66 
  67     static void UdpTest(InetAddress ia, boolean connected) throws Exception {
  68 
  69         System.out.println("**************************************");
  70         testCount++;
  71 
  72         if (connected) {
  73             System.out.println("Test " + testCount + ": UDP connect to " + ia);
  74         } else {
  75             System.out.println("Test " + testCount + ": UDP send to " + ia);
  76         }
  77 
  78         DatagramSocket ds1 = new DatagramSocket();
  79         DatagramSocket ds2 = new DatagramSocket();
  80 
  81         try {
  82             byte b[] = "Hello".getBytes();
  83             DatagramPacket p = new DatagramPacket(b, b.length);
  84 
  85             if (connected) {
  86                 ds1.connect( new InetSocketAddress(ia, ds2.getLocalPort()) );
  87                 System.out.println("DatagramSocket connected.");
  88             } else {
  89                 p.setAddress(ia);
  90                 p.setPort(ds2.getLocalPort());
  91             }
  92             ds1.send(p);
  93             System.out.println("Packet has been sent.");
  94 
  95             ds2.setSoTimeout(5000);
  96             ds2.receive(p);
  97             System.out.println("Test passed - packet received.");
  98         } catch (SocketException e) {
  99             failed++;
 100             System.out.println("Test failed: " + e);
 101         } finally {
 102             ds1.close();
 103             ds2.close();
 104         }
 105     }
 106 
 107     static void TestAddress(InetAddress ia) throws Exception {
 108         TcpTest(ia);
 109         UdpTest(ia, true);      /* unconnected */
 110         UdpTest(ia, false);     /* connected */
 111     }
 112 
 113     public static void main(String args[]) throws Exception {
 114 
 115         /*
 116          * If an argument is provided ensure that it's
 117          * a link-local IPv6 address.
 118          */
 119         if (args.length > 0) {
 120             InetAddress ia = InetAddress.getByName(args[0]);
 121 
 122             if ( !(ia instanceof Inet6Address) ||
 123                 !ia.isLinkLocalAddress()) {
 124                 throw new Exception(ia +
 125                         " is not a link-local IPv6 address");
 126             }
 127 
 128             TestAddress(ia);
 129         }
 130 
 131         /*
 132          * If no argument is provided then enumerate the
 133          * local addresses and run the test on each link-local
 134          * IPv6 address.
 135          */
 136         if (args.length == 0) {
 137             Enumeration nifs = NetworkInterface.getNetworkInterfaces();
 138             while (nifs.hasMoreElements()) {
 139                 NetworkInterface ni = (NetworkInterface)nifs.nextElement();
 140                 if (!ni.isUp())
 141                     continue;
 142 
 143                 Enumeration addrs = ni.getInetAddresses();
 144                 while (addrs.hasMoreElements()) {
 145                     InetAddress addr = (InetAddress)addrs.nextElement();
 146 
 147                     if (addr instanceof Inet6Address &&
 148                         addr.isLinkLocalAddress()) {
 149 
 150                         TestAddress(addr);
 151                     }
 152                 }
 153             }
 154         }
 155 
 156         /*
 157          * Print results
 158          */
 159         if (testCount == 0) {
 160             System.out.println("No link-local IPv6 addresses - test skipped!");
 161         } else {
 162             System.out.println("**************************************");
 163             System.out.println(testCount + " test(s) executed, " +
 164                 failed + " failed.");
 165             if (failed > 0) {
 166                 throw new Exception( failed + " test(s) failed.");
 167             }
 168         }
 169     }
 170 }