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 } | 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 import java.util.concurrent.CountDownLatch; 48 import java.util.concurrent.TimeUnit; 49 50 public class ReadTimeoutTest 51 { 52 private static final int DELAY = 5000; // milliseconds 53 54 public static void main(String[] args) 55 throws Exception 56 { 57 // Make trouble for ourselves 58 if (System.getSecurityManager() == null) 59 System.setSecurityManager(new RMISecurityManager()); 60 61 // Flaky code alert - hope this is executed before TCPTransport.<clinit> 62 System.setProperty("sun.rmi.transport.tcp.readTimeout", Integer.toString(DELAY)); 63 64 // Set the socket factory. 65 System.err.println("(installing socket factory)"); 66 SomeFactory fac = new SomeFactory(); 67 RMISocketFactory.setSocketFactory(fac); 68 69 // Create remote object 70 TestImpl impl = new TestImpl(); 71 72 // Export and get which port. 73 System.err.println("(exporting remote object)"); 74 int port = fac.whichPort(); 75 76 // Sanity 77 if (port == 0) 78 throw new Error("TEST FAILED: export didn't reserve a port(?)"); 79 try (Socket DoS = new Socket("127.0.0.1", port)) { 80 // Now, connect to that port 81 //Thread.sleep(2000); 82 System.err.println("(connecting to listening port on 127.0.0.1:" + 83 port + ")"); 84 InputStream stream = DoS.getInputStream(); 85 86 // Read on the socket in the background 87 CountDownLatch done = new CountDownLatch(1); 88 (new SomeReader(stream, done)).start(); 89 90 // Wait for completion 91 if (done.await(DELAY * 4, TimeUnit.SECONDS)) { 92 System.err.println("TEST PASSED."); 93 } else { 94 throw new Error("TEST FAILED."); 95 } 96 } catch (InterruptedException ie) { 97 throw new Error("Unexpected error happen in reader:" + ie); 98 } finally { 99 impl.unexport(); 100 } 101 // Should exit here 102 } 103 104 private static class SomeFactory 105 extends RMISocketFactory 106 { 107 private int servport = 0; 108 109 @Override 110 public Socket createSocket(String h, int p) 111 throws IOException 112 { 113 return (new Socket(h, p)); 114 } 115 116 /** Create a server socket and remember which port it's on. 117 * Aborts if createServerSocket(0) is called twice, because then 118 * it doesn't know whether to remember the first or second port. 119 */ 120 @Override 121 public ServerSocket createServerSocket(int p) 122 throws IOException 123 { 124 ServerSocket ss; 125 ss = new ServerSocket(p); 126 if (p == 0) { 127 if (servport != 0) { 128 System.err.println("TEST FAILED: " + 129 "Duplicate createServerSocket(0)"); 130 throw new Error("Test aborted (createServerSocket)"); 131 } 132 servport = ss.getLocalPort(); 133 } 134 return (ss); 135 } 136 137 /** Return which port was reserved by createServerSocket(0). 138 * If the return value was 0, createServerSocket(0) wasn't called. 139 */ 140 public int whichPort() { 141 return (servport); 142 } 143 } // end class SomeFactory 144 145 protected static class SomeReader extends Thread { 146 private final InputStream readon; 147 private final CountDownLatch done; 148 149 public SomeReader(InputStream s, CountDownLatch done) { 150 super(); 151 this.setDaemon(true); 152 this.readon = s; 153 this.done = done; 154 } 155 156 @Override 157 public void run() { 158 try { 159 int c = this.readon.read(); 160 if (c != -1) 161 throw new Error ("Server returned " + c); 162 done.countDown(); 163 } catch (IOException e) { 164 e.printStackTrace(); 165 } 166 } 167 } // end class SomeReader 168 } |