1 /*
   2  * Copyright (c) 2008, 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 6558853
  27  * @summary  getHostAddress() on connections using IPv6 link-local addrs should have zone id
  28  * @library /test/lib
  29  * @build jdk.test.lib.NetworkConfiguration
  30  *        jdk.test.lib.Utils
  31  *        jdk.test.lib.Asserts
  32  *        jdk.test.lib.JDKToolFinder
  33  *        jdk.test.lib.JDKToolLauncher
  34  *        jdk.test.lib.Platform
  35  *        jdk.test.lib.process.*
  36  * @run main B6558853
  37  */
  38 
  39 import java.io.IOException;
  40 import java.io.InputStream;
  41 import java.io.OutputStream;
  42 import java.net.*;
  43 import java.util.Optional;
  44 import jdk.test.lib.NetworkConfiguration;
  45 
  46 public class B6558853 implements Runnable {
  47     private InetAddress addr = null;
  48     private int port = 0;
  49 
  50     public static void main(String[] args) throws Exception {
  51         Optional<Inet6Address> oaddr = NetworkConfiguration.probe()
  52                 .ip6Addresses()
  53                 .filter(a -> a.isLinkLocalAddress())
  54                 .findFirst();
  55 
  56         if (!oaddr.isPresent()) {
  57             System.out.println("No suitable interface found. Exiting.");
  58             return;
  59         }
  60 
  61         Inet6Address dest = oaddr.get();
  62         System.out.println("Using " + dest);
  63 
  64         try (ServerSocket ss = new ServerSocket(0)) {
  65             int port = ss.getLocalPort();
  66             B6558853 test = new B6558853(dest, port);
  67             Thread thread = new Thread(test);
  68             thread.start();
  69             Socket s = ss.accept();
  70             InetAddress a = s.getInetAddress();
  71             OutputStream out = s.getOutputStream();
  72             out.write(1);
  73             out.close();
  74             if (!(a instanceof Inet6Address) || a.getHostAddress().indexOf("%") == -1) {
  75                 // No Scope found in the address String
  76                 throw new RuntimeException("Wrong address: " + a.getHostAddress());
  77             }
  78         }
  79     }
  80 
  81     public B6558853(InetAddress a, int port) {
  82         addr = a;
  83         this.port = port;
  84     }
  85 
  86     public void run() {
  87         try {
  88             Socket s = new Socket(addr, port);
  89             InputStream in = s.getInputStream();
  90             int i = in.read();
  91             in.close();
  92         } catch (IOException iOException) {
  93         }
  94     }
  95 }