1 /*
   2  * Copyright (c) 2001, 2018, 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 /* @test
  25  * @bug 8195160
  26  * @summary Unit test for socket channels
  27  * @summary This tests whether it's possible to get some informations
  28  *          out of a closed socket. This is for backward compatibility
  29  *          purposes.
  30  * @requires (os.family == "linux")
  31  * @library .. /test/lib
  32  * @build RsocketTest
  33  * @run main/othervm -Djava.net.preferIPv4Stack=true TestClose
  34  */
  35 import java.net.*;
  36 import jdk.net.Sockets;
  37 
  38 public class TestClose implements Runnable {
  39 
  40     static ServerSocket ss;
  41     static Socket s;
  42     static InetAddress ia;
  43     static int serverport;
  44 
  45     public static void main(String[] args) throws Exception {
  46         if (!RsocketTest.isRsocketAvailable())
  47             return;
  48 
  49         InetAddress ad1, ad2;
  50         int port1, port2;
  51 
  52         ia = InetAddress.getLocalHost();
  53         ss = Sockets.openRdmaServerSocket();
  54         ss.bind(new InetSocketAddress(ia, 0));
  55         serverport = ss.getLocalPort();
  56 
  57         s = Sockets.openRdmaSocket();
  58         Thread t = new Thread(new TestClose());
  59         t.start();
  60 
  61         Socket s2 = ss.accept();
  62         s.close();
  63         s2.close();
  64         ss.close();
  65 
  66         ad1 = ss.getInetAddress();
  67         if (ad1 == null)
  68             throw new RuntimeException("ServerSocket.getInetAddress() returned null");
  69         port1 = ss.getLocalPort();
  70         if (port1 != serverport)
  71             throw new RuntimeException("ServerSocket.getLocalPort() returned the wrong value");
  72         ad2 = s.getInetAddress();
  73         if (ad2 == null)
  74             throw new RuntimeException("Socket.getInetAddress() returned null");
  75         port2 = s.getPort();
  76         if (port2 != serverport)
  77             throw new RuntimeException("Socket.getPort() returned wrong value");
  78         ad2 = s.getLocalAddress();
  79         if (ad2 == null)
  80             throw new RuntimeException("Socket.getLocalAddress() returned null");
  81         port2 = s.getLocalPort();
  82         if (port2 == -1)
  83             throw new RuntimeException("Socket.getLocalPort returned -1");
  84     }
  85 
  86     public void run() {
  87         try {
  88             s.connect(new InetSocketAddress(ia, serverport));
  89         } catch (Exception e) {
  90             e.printStackTrace();
  91             throw new RuntimeException("Test Failed");
  92         }
  93     }
  94 }