1 /*
   2  * Copyright (c) 1999, 2008, 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 4208804
  26  *
  27  * @summary Incoming connections should be subject to timeout
  28  * @author Adrian Colley
  29  *
  30  * @build TestIface TestImpl TestImpl_Stub
  31  * @run main/othervm/policy=security.policy/timeout=60
  32  *     -Dsun.rmi.transport.tcp.readTimeout=5000 ReadTimeoutTest
  33  */
  34 
  35 /* This test sets a very short read timeout, exports an object, and then
  36  * connects to the port and does nothing.  The server should close the
  37  * connection after the timeout.  If that doesn't happen, the test fails.
  38  *
  39  * A background thread does the read.  The foreground waits for DELAY*4
  40  * and then aborts.  This should give sufficient margin for timing slop.
  41  */
  42 
  43 import java.rmi.*;
  44 import java.rmi.server.RMISocketFactory;
  45 import java.io.*;
  46 import java.net.*;
  47 
  48 public class ReadTimeoutTest
  49 {
  50     private static final int DELAY = 5000;      // milliseconds
  51 
  52     public static void main(String[] args)
  53         throws Exception
  54     {
  55         // Make trouble for ourselves
  56         if (System.getSecurityManager() == null)
  57             System.setSecurityManager(new RMISecurityManager());
  58 
  59         // Flaky code alert - hope this is executed before TCPTransport.<clinit>
  60         System.setProperty("sun.rmi.transport.tcp.readTimeout", Integer.toString(DELAY));
  61 
  62         // Set the socket factory.
  63         System.err.println("(installing socket factory)");
  64         SomeFactory fac = new SomeFactory();
  65         RMISocketFactory.setSocketFactory(fac);
  66 
  67         // Create remote object
  68         TestImpl impl = new TestImpl();
  69 
  70         // Export and get which port.
  71         System.err.println("(exporting remote object)");
  72         TestIface stub = impl.export();
  73         Socket DoS = null;
  74         try {
  75             int port = fac.whichPort();
  76 
  77             // Sanity
  78             if (port == 0)
  79                 throw new Error("TEST FAILED: export didn't reserve a port(?)");
  80 
  81             // Now, connect to that port
  82             //Thread.sleep(2000);
  83             System.err.println("(connecting to listening port on 127.0.0.1:" +
  84                                port + ")");
  85             DoS = new Socket("127.0.0.1", port);
  86             InputStream stream = DoS.getInputStream();
  87 
  88             // Read on the socket in the background
  89             boolean[] successful = new boolean[] { false };
  90             (new SomeReader(stream, successful)).start();
  91 
  92             // Wait for completion
  93             int nretries = 4;
  94             while (nretries-- > 0) {
  95                 if (successful[0])
  96                     break;
  97                 Thread.sleep(DELAY);
  98             }
  99 
 100             if (successful[0]) {
 101                 System.err.println("TEST PASSED.");
 102             } else {
 103                 throw new Error("TEST FAILED.");
 104             }
 105 
 106         } finally {
 107             try {
 108                 if (DoS != null)
 109                     DoS.close();        // aborts the reader if still blocked
 110                 impl.unexport();
 111             } catch (Throwable unmatter) {
 112             }
 113         }
 114 
 115         // Should exit here
 116     }
 117 
 118     private static class SomeFactory
 119         extends RMISocketFactory
 120     {
 121         private int servport = 0;
 122 
 123         public Socket createSocket(String h, int p)
 124             throws IOException
 125         {
 126             return (new Socket(h, p));
 127         }
 128 
 129         /** Create a server socket and remember which port it's on.
 130          * Aborts if createServerSocket(0) is called twice, because then
 131          * it doesn't know whether to remember the first or second port.
 132          */
 133         public ServerSocket createServerSocket(int p)
 134             throws IOException
 135         {
 136             ServerSocket ss;
 137             ss = new ServerSocket(p);
 138             if (p == 0) {
 139                 if (servport != 0) {
 140                     System.err.println("TEST FAILED: " +
 141                                        "Duplicate createServerSocket(0)");
 142                     throw new Error("Test aborted (createServerSocket)");
 143                 }
 144                 servport = ss.getLocalPort();
 145             }
 146             return (ss);
 147         }
 148 
 149         /** Return which port was reserved by createServerSocket(0).
 150          * If the return value was 0, createServerSocket(0) wasn't called.
 151          */
 152         public int whichPort() {
 153             return (servport);
 154         }
 155     } // end class SomeFactory
 156 
 157     protected static class SomeReader extends Thread {
 158         private InputStream readon;
 159         private boolean[] vec;
 160 
 161         public SomeReader(InputStream s, boolean[] successvec) {
 162             super();
 163             this.setDaemon(true);
 164             this.readon = s;
 165             this.vec = successvec;
 166         }
 167 
 168         public void run() {
 169             try {
 170                 int c = this.readon.read();
 171                 if (c != -1)
 172                     throw new Error ("Server returned " + c);
 173                 this.vec[0] = true;
 174 
 175             } catch (IOException e) {
 176                 e.printStackTrace();
 177             }
 178         }
 179     } // end class SomeReader
 180 }