Path: blob/master/test/jdk/java/rmi/transport/readTimeout/ReadTimeoutTest.java
41155 views
/*1* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 420880425*26* @summary Incoming connections should be subject to timeout27* @author Adrian Colley28*29* @build TestIface TestImpl TestImpl_Stub30* @run main/othervm/policy=security.policy/timeout=6031* -Dsun.rmi.transport.tcp.readTimeout=5000 ReadTimeoutTest32*/3334/* This test sets a very short read timeout, exports an object, and then35* connects to the port and does nothing. The server should close the36* connection after the timeout. If that doesn't happen, the test fails.37*38* A background thread does the read. The foreground waits for DELAY*439* and then aborts. This should give sufficient margin for timing slop.40*/4142import java.rmi.*;43import java.rmi.server.RMISocketFactory;44import java.io.*;45import java.net.*;46import java.util.concurrent.CountDownLatch;47import java.util.concurrent.TimeUnit;4849public class ReadTimeoutTest50{51private static final int DELAY = 5000; // milliseconds5253public static void main(String[] args)54throws Exception55{56// Make trouble for ourselves57if (System.getSecurityManager() == null)58System.setSecurityManager(new RMISecurityManager());5960// Flaky code alert - hope this is executed before TCPTransport.<clinit>61System.setProperty("sun.rmi.transport.tcp.readTimeout", Integer.toString(DELAY));6263// Set the socket factory.64System.err.println("(installing socket factory)");65SomeFactory fac = new SomeFactory();66RMISocketFactory.setSocketFactory(fac);6768// Create remote object69TestImpl impl = new TestImpl();7071// Export and get which port.72System.err.println("(exporting remote object)");73TestIface stub = impl.export();74Socket DoS = null;75try {76int port = fac.whichPort();7778// Sanity79if (port == 0)80throw new Error("TEST FAILED: export didn't reserve a port(?)");8182// Now, connect to that port83//Thread.sleep(2000);84System.err.println("(connecting to listening port on localhost:" +85port + ")");86DoS = new Socket(InetAddress.getLoopbackAddress(), port);87InputStream stream = DoS.getInputStream();8889// Read on the socket in the background90CountDownLatch done = new CountDownLatch(1);91(new SomeReader(stream, done)).start();9293// Wait for completion94if (done.await(DELAY * 4, TimeUnit.SECONDS)) {95System.err.println("TEST PASSED.");96} else {97throw new Error("TEST FAILED.");98}99100} catch (InterruptedException ie) {101throw new Error("Unexpected interrupt", ie);102} finally {103try {104if (DoS != null)105DoS.close(); // aborts the reader if still blocked106impl.unexport();107} catch (Throwable unmatter) {108}109}110111// Should exit here112}113114private static class SomeFactory115extends RMISocketFactory116{117private int servport = 0;118119@Override120public Socket createSocket(String h, int p)121throws IOException122{123return (new Socket(h, p));124}125126/** Create a server socket and remember which port it's on.127* Aborts if createServerSocket(0) is called twice, because then128* it doesn't know whether to remember the first or second port.129*/130@Override131public ServerSocket createServerSocket(int p)132throws IOException133{134ServerSocket ss;135ss = new ServerSocket(p);136if (p == 0) {137if (servport != 0) {138System.err.println("TEST FAILED: " +139"Duplicate createServerSocket(0)");140throw new Error("Test aborted (createServerSocket)");141}142servport = ss.getLocalPort();143}144return (ss);145}146147/** Return which port was reserved by createServerSocket(0).148* If the return value was 0, createServerSocket(0) wasn't called.149*/150public int whichPort() {151return (servport);152}153} // end class SomeFactory154155protected static class SomeReader extends Thread {156private final InputStream readon;157private final CountDownLatch done;158159public SomeReader(InputStream s, CountDownLatch done) {160super();161this.setDaemon(true);162this.readon = s;163this.done = done;164}165166@Override167public void run() {168try {169int c = this.readon.read();170if (c != -1)171throw new Error ("Server returned " + c);172done.countDown();173174} catch (IOException e) {175e.printStackTrace();176}177}178} // end class SomeReader179}180181182