1 /*
   2  * Copyright (c) 2011, 2013, 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  * @summary <rdar://problem/3887227> 1.4.2: Class NetworkInterface can not handle IPv6 addresses correctly
  27  * @summary com.apple.junit.java.net
  28  * @run junit IPv6NetworkInterfaceTest
  29  */
  30 
  31 import junit.framework.*;
  32 import java.net.*;
  33 import java.util.Enumeration;
  34 
  35 public class IPv6NetworkInterfaceTest extends TestCase {
  36 
  37     public static Test suite() {
  38         return new TestSuite( IPv6NetworkInterfaceTest.class);
  39     }
  40 
  41     public static void main (String[] args) throws RuntimeException {
  42         TestResult tr = junit.textui.TestRunner.run(suite());
  43         if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  44             throw new RuntimeException("### FAILED: unexpected JUnit errors or failures.");
  45         }
  46     }
  47 
  48     public void testNetworkInterface() throws Exception {
  49         String ipv6_string = null;
  50 
  51         try {
  52             // Get the local host and grab the IPv6 address string
  53             InetAddress localhost = InetAddress.getLocalHost();
  54             NetworkInterface ni = NetworkInterface.getByInetAddress(localhost);
  55             for (Enumeration e = ni.getInetAddresses() ; e.hasMoreElements() ;) {
  56                 InetAddress ia = (InetAddress) e.nextElement();
  57                 if (ia instanceof Inet6Address) {
  58                     ipv6_string = ia.getHostAddress();
  59                 }
  60             }
  61         } catch (Exception e) {
  62             //e.printStackTrace();
  63             //Swallow the exception, as the next assert checks that ipv6_string isn't null
  64         }
  65 
  66         assertTrue("Could not find IPv6 address string. Test cannot run.", (ipv6_string != null));
  67 
  68         InetAddress ipv6_addr = InetAddress.getByName(ipv6_string);
  69 
  70         assertTrue("Could not lookup IPv6 address from " + ipv6_string + ". Test cannot run.", (ipv6_addr != null));
  71 
  72         // Whew! Finally we get to the bug!
  73         NetworkInterface ipv6_ni = NetworkInterface.getByInetAddress(ipv6_addr);
  74 
  75         assertTrue("Could not lookup NetworkInterface from " + ipv6_addr, (ipv6_ni != null));
  76     }
  77 }