Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/bugs/6725892/Test.java
38889 views
/*1* Copyright (c) 2005, 2010, 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/**24* @test25* @bug 672589226* @run main/othervm -Dsun.net.httpserver.maxReqTime=2 Test27* @summary28*/2930import com.sun.net.httpserver.*;3132import java.util.concurrent.*;33import java.util.logging.*;34import java.io.*;35import java.net.*;36import javax.net.ssl.*;3738public class Test {3940static HttpServer s1;41static int port;42static URL url;43static final String RESPONSE_BODY = "response";44static boolean failed = false;4546static class Handler implements HttpHandler {4748public void handle (HttpExchange t)49throws IOException50{51InputStream is = t.getRequestBody();52InetSocketAddress rem = t.getRemoteAddress();53System.out.println ("Request from: " + rem);54while (is.read () != -1) ;55is.close();56String requrl = t.getRequestURI().toString();57OutputStream os = t.getResponseBody();58t.sendResponseHeaders (200, RESPONSE_BODY.length());59os.write (RESPONSE_BODY.getBytes());60t.close();61}62}6364public static void main (String[] args) throws Exception {6566ExecutorService exec = Executors.newCachedThreadPool();6768try {69InetSocketAddress addr = new InetSocketAddress (0);70s1 = HttpServer.create (addr, 100);71HttpHandler h = new Handler ();72HttpContext c1 = s1.createContext ("/", h);73s1.setExecutor(exec);74s1.start();7576port = s1.getAddress().getPort();77System.out.println ("Server on port " + port);78url = new URL ("http://127.0.0.1:"+port+"/foo");79test1();80test2();81test3();82Thread.sleep (2000);83} catch (Exception e) {84e.printStackTrace();85System.out.println ("FAIL");86throw new RuntimeException ();87} finally {88s1.stop(0);89System.out.println ("After Shutdown");90exec.shutdown();91}92}9394// open TCP connection without sending anything. Check server closes it.9596static void test1() throws IOException {97failed = false;98Socket s = new Socket ("127.0.0.1", port);99InputStream is = s.getInputStream();100// server should close connection after 2 seconds. We wait up to 10101s.setSoTimeout (10000);102try {103is.read();104} catch (SocketTimeoutException e) {105failed = true;106}107s.close();108if (failed) {109System.out.println ("test1: FAIL");110throw new RuntimeException ();111} else {112System.out.println ("test1: OK");113}114}115116// send request and don't read response. Check server closes connection117118static void test2() throws IOException {119HttpURLConnection urlc = (HttpURLConnection) url.openConnection();120urlc.setReadTimeout (20 * 1000);121InputStream is = urlc.getInputStream();122// we won't read response and check if it times out123// on server. If it timesout at client then there is a problem124try {125Thread.sleep (10 * 1000);126while (is.read() != -1) ;127} catch (InterruptedException e) {128System.out.println (e);129System.out.println ("test2: FAIL");130throw new RuntimeException ("unexpected error");131} catch (SocketTimeoutException e1) {132System.out.println (e1);133System.out.println ("test2: FAIL");134throw new RuntimeException ("client timedout");135} finally {136is.close();137}138System.out.println ("test2: OK");139}140141// same as test2, but repeated with multiple connections142// including a number of valid request/responses143144// Worker: a thread opens a connection to the server in one of three modes.145// NORMAL - sends a request, waits for response, and checks valid response146// REQUEST - sends a partial request, and blocks, to see if147// server closes the connection.148// RESPONSE - sends a request, partially reads response and blocks,149// to see if server closes the connection.150151static class Worker extends Thread {152CountDownLatch latch;153Mode mode;154155enum Mode {156REQUEST, // block during sending of request157RESPONSE, // block during reading of response158NORMAL // don't block159};160161Worker (CountDownLatch latch, Mode mode) {162this.latch = latch;163this.mode = mode;164}165166void fail(String msg) {167System.out.println (msg);168failed = true;169}170171public void run () {172HttpURLConnection urlc;173InputStream is = null;174175try {176urlc = (HttpURLConnection) url.openConnection();177urlc.setReadTimeout (20 * 1000);178urlc.setDoOutput(true);179} catch (IOException e) {180fail("Worker: failed to connect to server");181latch.countDown();182return;183}184try {185OutputStream os = urlc.getOutputStream();186os.write ("foo".getBytes());187if (mode == Mode.REQUEST) {188Thread.sleep (3000);189}190os.close();191is = urlc.getInputStream();192if (mode == Mode.RESPONSE) {193Thread.sleep (3000);194}195if (!checkResponse (is, RESPONSE_BODY)) {196fail ("Worker: response");197}198is.close();199return;200} catch (InterruptedException e0) {201fail("Worker: timedout");202} catch (SocketTimeoutException e1) {203fail("Worker: timedout");204} catch (IOException e2) {205switch (mode) {206case NORMAL:207fail ("Worker: " + e2.getMessage());208break;209case RESPONSE:210if (is == null) {211fail ("Worker: " + e2.getMessage());212break;213}214// default: is ok215}216} finally {217latch.countDown();218}219}220}221222static final int NUM = 20;223224static void test3() throws Exception {225failed = false;226CountDownLatch l = new CountDownLatch (NUM*3);227Worker[] workers = new Worker[NUM*3];228for (int i=0; i<NUM; i++) {229workers[i*3] = new Worker (l, Worker.Mode.NORMAL);230workers[i*3+1] = new Worker (l, Worker.Mode.REQUEST);231workers[i*3+2] = new Worker (l, Worker.Mode.RESPONSE);232workers[i*3].start();233workers[i*3+1].start();234workers[i*3+2].start();235}236l.await();237for (int i=0; i<NUM*3; i++) {238workers[i].join();239}240if (failed) {241throw new RuntimeException ("test3: failed");242}243System.out.println ("test3: OK");244}245246static boolean checkResponse (InputStream is, String resp) {247try {248ByteArrayOutputStream bos = new ByteArrayOutputStream();249byte[] buf = new byte [64];250int c;251while ((c=is.read(buf)) != -1) {252bos.write (buf, 0, c);253}254bos.close();255if (!bos.toString().equals(resp)) {256System.out.println ("Wrong response: " + bos.toString());257return false;258}259} catch (IOException e) {260System.out.println (e);261return false;262}263return true;264}265}266267268