Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/ChunkedOutputStream/checkError.java
38867 views
/*1* Copyright (c) 2004, 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 505401626* @run main/othervm/timeout=300 checkError27* @summary get the failure immediately when writing individual chunks over socket fail28*/2930import java.io.*;31import java.net.*;32import java.util.StringTokenizer;333435public class checkError {36static final int TEST_PASSED = 95;37static final int TEST_FAILED = 97;3839static int testStatus = TEST_PASSED;4041static String serverName = "localhost";42static int bufferSize = 8192; // 8k43static int totalBytes = 1048576; // 1M4445static int j = 0;4647static public Object threadStarting = new Object();48static public Object threadWaiting = new Object();495051public static void main(String[] args) throws Exception {52HttpURLConnection conn = null;53OutputStream toServer = null;54byte[] buffer = null;55HTTPServer server = null;56synchronized(threadWaiting) {57System.out.println("HTTP-client>Starting default Http-server");58synchronized(threadStarting) {59server = new HTTPServer();60server.start();61try {62System.out.println("waiting server to be start");63threadStarting.wait();64} catch (InterruptedException e) {65}66}67int port = server.getPort();68URL url = new URL("http://" + serverName + ":" + port);69conn = (HttpURLConnection )url.openConnection();70conn.setRequestMethod("POST");71conn.setDoOutput(true);7273System.out.println("assigning 1024 to the chunk length");74conn.setChunkedStreamingMode(1024);75conn.connect();7677toServer = conn.getOutputStream();78buffer = getThickBuffer(bufferSize);79System.out.println("sending " + totalBytes + " bytes");80}8182int byteAtOnce = 0;83int sendingBytes = totalBytes;84try {85while (sendingBytes > 0) {86if (sendingBytes > bufferSize) {87byteAtOnce = bufferSize;88} else {89byteAtOnce = sendingBytes;90}91toServer.write(buffer, 0, byteAtOnce);92sendingBytes -= byteAtOnce;93// System.out.println((totalBytes - sendingBytes) + " was sent");94toServer.flush();95}96} catch (OutOfMemoryError e) {97e.printStackTrace();98System.out.println("***ERR***> UNEXPECTED error: " + e);99testStatus = TEST_FAILED;100testExit();101} catch (IOException e) {102// e.printStackTrace();103// this is the expected IOException104// due to server.close()105testStatus = TEST_PASSED;106testExit();107} finally {108toServer.close();109}110111// we have not received the expected IOException112// test fail113testStatus = TEST_FAILED;114testExit();115116}117118static void testExit() {119if (testStatus == TEST_FAILED) {120throw new RuntimeException("Test Failed: haven't received the expected IOException");121} else {122System.out.println("TEST PASSED");123}124System.exit(testStatus);125}126127static byte[] getThickBuffer(int size) {128129byte[] buffer = new byte[size];130131for (int i = 0; i < size; i++) {132if (j > 9)133j = 0;134String s = Integer.toString(j);135buffer[i] = (byte )s.charAt(0);136j++;137}138139return buffer;140}141}142143144class HTTPServer extends Thread {145146static volatile boolean isCompleted;147148Socket client;149ServerSocket serverSocket;150151int getPort() {152return serverSocket.getLocalPort();153}154155public void run() {156157synchronized(checkError.threadStarting) {158159try {160serverSocket = new ServerSocket(0, 100);161} catch (Exception e) {162e.printStackTrace();163checkError.testStatus = checkError.TEST_FAILED;164return;165}166checkError.threadStarting.notify();167}168169try {170client = serverSocket.accept();171} catch (Exception e) {172e.printStackTrace();173checkError.testStatus = checkError.TEST_FAILED;174return;175}176177System.out.println("Server started");178179BufferedReader in = null;180PrintStream out = null;181InputStreamReader reader = null;182String version = null;183String line;184String method;185186synchronized(checkError.threadWaiting) {187try {188reader = new InputStreamReader(client.getInputStream());189in = new BufferedReader(reader);190line = in.readLine();191192} catch (Exception e) {193e.printStackTrace();194checkError.testStatus = checkError.TEST_FAILED;195return;196}197StringTokenizer st = new StringTokenizer(line);198method = st.nextToken();199String fileName = st.nextToken();200201// save version for replies202if (st.hasMoreTokens()) version = st.nextToken();203204System.out.println("HTTP version: " + version);205206}207208try {209210while (line != null && line.length() > 0) {211line = in.readLine();212System.out.println(line);213}214} catch (IOException e) {215e.printStackTrace();216checkError.testStatus = checkError.TEST_FAILED;217return;218}219220if (method.equals("POST")) {221System.out.println("receiving data");222byte[] buf = new byte[1024];223try {224//reading bytes until chunk whose size is zero,225// see 19.4.6 Introduction of Transfer-Encoding in RFC2616226int count = 0;227while (count <=5) {228count++;229in.readLine();230}231232System.out.println("Server socket is closed");233in.close();234client.close();235serverSocket.close();236237} catch (IOException e) {238e.printStackTrace();239checkError.testStatus = checkError.TEST_FAILED;240return;241} catch (OutOfMemoryError e) {242e.printStackTrace();243checkError.testStatus = checkError.TEST_FAILED;244return;245}246247}248}249250}251252253