< prev index next >

test/java/net/Socket/LinkLocal.java

Print this page




   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()));


 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 }


   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()));


 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 }
< prev index next >