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 Setting ServerSocket.setSoTimeout shouldn't cause
  27  *          the timeout to be inherited by accepted connections
  28  * @requires (os.family == "linux")
  29  * @library .. /test/lib
  30  * @build RsocketTest
  31  * @run main/othervm -Djava.net.preferIPv4Stack=true InheritTimeout
  32  */
  33 
  34 import java.net.*;
  35 import java.io.InputStream;
  36 import jdk.net.*;
  37 
  38 public class InheritTimeout implements Runnable {
  39 
  40     static Socket s1;
  41     static ServerSocket ss;
  42     static InetAddress addr;
  43 
  44     class Reaper extends Thread {
  45         Socket s;
  46         int timeout;
  47 
  48         Reaper(Socket s, int timeout) {
  49             this.s = s;
  50             this.timeout = timeout;
  51         }
  52 
  53         public void run() {
  54             try {
  55                 Thread.currentThread().sleep(timeout);
  56                 s.close();
  57             } catch (Exception e) {
  58             }
  59         }
  60     }
  61 
  62     class Client extends Thread {
  63         public void run() {
  64             InetSocketAddress isa = new InetSocketAddress(addr, ss.getLocalPort());
  65             try {
  66                 s1.connect(isa);
  67             } catch (Exception e) {
  68                 e.printStackTrace();
  69             }
  70         }
  71     }
  72 
  73     InheritTimeout() throws Exception {
  74         ss = Sockets.openRdmaServerSocket();
  75         ss.bind(new InetSocketAddress(addr, 0));
  76 
  77         ss.setSoTimeout(1000);
  78 
  79         s1 = Sockets.openRdmaSocket();
  80 
  81         Client c = new Client();
  82         c.start();
  83    
  84         Socket s2 = ss.accept();
  85 
  86         Reaper r = new Reaper(s2, 5000);
  87         r.start();
  88 
  89         boolean readTimedOut = false;
  90         try {
  91             s2.getInputStream().read();
  92         } catch (SocketTimeoutException te) {
  93             readTimedOut = true;
  94         } catch (SocketException e) {
  95             if (!s2.isClosed()) {
  96                 throw e;
  97             }
  98         }
  99         s1.close();
 100         ss.close();
 101 
 102         if (readTimedOut) {
 103             throw new Exception("Unexpected SocketTimeoutException throw!");
 104         }
 105     }
 106 
 107     public static void main(String args[]) throws Exception {
 108         if (!RsocketTest.isRsocketAvailable())
 109             return;
 110 
 111         new InheritTimeout();
 112    }
 113 
 114     public void run() {
 115         InetSocketAddress isa = new InetSocketAddress(addr, ss.getLocalPort());
 116         try {
 117             s1.connect(isa);
 118         } catch (Exception e) {
 119             e.printStackTrace();
 120             throw new RuntimeException("Test Failed!");
 121         }
 122     }
 123 }