< prev index next >

test/java/net/Inet6Address/B6558853.java

Print this page




   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 6558853
  27  * @summary  getHostAddress() on connections using IPv6 link-local addrs should have zone id



  28  */

  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.io.OutputStream;
  32 import java.net.*;
  33 import java.util.Enumeration;

  34 
  35 public class B6558853 implements Runnable {
  36     private InetAddress addr = null;
  37     private int port = 0;
  38 
  39     public static void main(String[] args) throws Exception {
  40         ServerSocket ss = new ServerSocket(0);
  41         int port = ss.getLocalPort();
  42         Enumeration<NetworkInterface> l = NetworkInterface.getNetworkInterfaces();
  43         InetAddress dest = null;
  44         while (l.hasMoreElements() && dest == null) {
  45             NetworkInterface nif = l.nextElement();
  46             if (!nif.isUp())
  47                 continue;
  48 
  49             for (InterfaceAddress a : nif.getInterfaceAddresses()) {
  50                 if (a.getAddress() instanceof Inet6Address) {
  51                     Inet6Address a6 = (Inet6Address) a.getAddress();
  52                     if (a6.isLinkLocalAddress()) {
  53                         dest = a6;
  54                     }
  55                     break;
  56                 }
  57             }
  58         }
  59         System.out.println("Using " + dest);
  60         if (dest != null) {


  61             B6558853 test = new B6558853(dest, port);
  62             Thread thread = new Thread(test);
  63             thread.start();
  64             Socket s = ss.accept();
  65             InetAddress a = s.getInetAddress();
  66             OutputStream out = s.getOutputStream();
  67             out.write(1);
  68             out.close();
  69             if (!(a instanceof Inet6Address) || a.getHostAddress().indexOf("%") == -1) {
  70                 // No Scope found in the address String
  71                 throw new RuntimeException("Wrong address: " + a.getHostAddress());
  72             }
  73         }
  74     }

  75 
  76     public B6558853(InetAddress a, int port) {
  77         addr = a;
  78         this.port = port;
  79     }
  80 
  81     public void run() {
  82         try {
  83             Socket s = new Socket(addr, port);
  84             InputStream in = s.getInputStream();
  85             int i = in.read();
  86             in.close();
  87         } catch (IOException iOException) {
  88         }
  89     }
  90 }


   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 6558853
  27  * @summary  getHostAddress() on connections using IPv6 link-local addrs should have zone id
  28  *  * @library /lib/testlibrary
  29  * @build jdk.testlibrary.NetworkConfiguration
  30  * @run main B6558853
  31  */
  32 
  33 import java.io.IOException;
  34 import java.io.InputStream;
  35 import java.io.OutputStream;
  36 import java.net.*;
  37 import java.util.Optional;
  38 import jdk.testlibrary.NetworkConfiguration;
  39 
  40 public class B6558853 implements Runnable {
  41     private InetAddress addr = null;
  42     private int port = 0;
  43 
  44     public static void main(String[] args) throws Exception {
  45         Optional<Inet6Address> oaddr = NetworkConfiguration.probe()
  46                 .ip6Addresses()
  47                 .filter(a -> a.isLinkLocalAddress())
  48                 .findFirst();
  49 
  50         if (oaddr.isPresent()) {
  51             Inet6Address dest = oaddr.get();












  52             System.out.println("Using " + dest);
  53 
  54             try (ServerSocket ss = new ServerSocket(0)) {
  55                 int port = ss.getLocalPort();
  56                 B6558853 test = new B6558853(dest, port);
  57                 Thread thread = new Thread(test);
  58                 thread.start();
  59                 Socket s = ss.accept();
  60                 InetAddress a = s.getInetAddress();
  61                 OutputStream out = s.getOutputStream();
  62                 out.write(1);
  63                 out.close();
  64                 if (!(a instanceof Inet6Address) || a.getHostAddress().indexOf("%") == -1) {
  65                     // No Scope found in the address String
  66                     throw new RuntimeException("Wrong address: " + a.getHostAddress());
  67                 }
  68             }
  69         }
  70     }
  71 
  72     public B6558853(InetAddress a, int port) {
  73         addr = a;
  74         this.port = port;
  75     }
  76 
  77     public void run() {
  78         try {
  79             Socket s = new Socket(addr, port);
  80             InputStream in = s.getInputStream();
  81             int i = in.read();
  82             in.close();
  83         } catch (IOException iOException) {
  84         }
  85     }
  86 }
< prev index next >