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 Test for <rdar://problem/4659174>. getLocalAddress() on a connected socket was returning 0.0.0.0 instead of the actual local address.
  27  * @summary com.apple.junit.java.net
  28  */
  29 
  30 import junit.framework.*;
  31 import java.net.*;
  32 
  33 public class GetLocalAddressTest extends TestCase {
  34     public static Test suite() {
  35         return new TestSuite(GetLocalAddressTest.class);
  36     }
  37 
  38     public static void main (String[] args) throws RuntimeException {
  39         TestResult tr = junit.textui.TestRunner.run(suite());
  40         if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  41             throw new RuntimeException("### FAILED: unexpected JUnit errors or failures.");
  42         }
  43     }
  44 
  45     public void testGetLocalAddress() throws Exception
  46     {
  47         String sHostname = "apple.com";
  48         InetAddress ina = InetAddress.getByName( sHostname );
  49         InetSocketAddress isa = new InetSocketAddress( ina, 80 );
  50         Socket s = new Socket();
  51         s.connect( isa, 1000 );
  52         InetAddress iaLocal = s.getLocalAddress(); // if this comes back as 0.0.0.0 this would demonstrate issue
  53         String hostname = iaLocal.getHostName();
  54         String preferIPv6addresses = System.getProperty("java.net.preferIPv6Addresses");
  55         if (preferIPv6addresses != null && preferIPv6addresses.equals("true")) {
  56             if (hostname.equals("::")) {
  57                 assertTrue("Invalid anyLocalAddress returned from getLocalAddress (IPv6)", false);
  58             }
  59         } else {
  60             if (hostname.equals("0.0.0.0")) {
  61                 assertTrue("Invalid anyLocalAddress returned from getLocalAddress (IPv4)", false);
  62             }
  63         }
  64     }
  65 }