Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/https/ChunkedOutputStream.java
38867 views
/*1* Copyright (c) 2004, 2012, 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 502674526* @build TestHttpsServer HttpCallback27* @run main/othervm ChunkedOutputStream28*29* SunJSSE does not support dynamic system properties, no way to re-use30* system properties in samevm/agentvm mode.31* @summary Cannot flush output stream when writing to an HttpUrlConnection32*/3334import java.io.*;35import java.net.*;36import javax.net.ssl.*;3738public class ChunkedOutputStream implements HttpCallback {39/*40* Where do we find the keystores for ssl?41*/42static String pathToStores = "../../../../../javax/net/ssl/etc";43static String keyStoreFile = "keystore";44static String trustStoreFile = "truststore";45static String passwd = "passphrase";46static int count = 0;4748static final String str1 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+49"1234567890abcdefkjsdlkjflkjsldkfjlsdkjflkj"+50"1434567890abcdefkjsdlkjflkjsldkfjlsdkjflkj";5152static final String str2 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+53"1234567890";5455public void request (HttpTransaction req) {56try {57// this is needed (count++ doesn't work), 'cause we58// are doing concurrent tests59String path = req.getRequestURI().getPath();60if (path.equals("/d0")) {61count = 0;62} else if (path.equals("/d01")) {63count = 1;64} else if (path.equals("/d3")) {65count = 2;66} else if (path.equals("/d4") || path.equals("/d5")) {67count = 3;68} else if (path.equals("/d6")) {69count = 3;70} else if (path.equals("/d7")) {71count = 4;72} else if (path.equals("/d8")) {73count = 5;74}7576switch (count) {77case 0: /* test1 -- keeps conn alive */78case 1: /* test2 -- closes conn */79String reqbody = req.getRequestEntityBody();80if (!reqbody.equals(str1)) {81req.sendResponse (500, "Internal server error");82req.orderlyClose();83}84String chunk = req.getRequestHeader ("Transfer-encoding");85if (!"chunked".equals (chunk)) {86req.sendResponse (501, "Internal server error");87req.orderlyClose();88}89req.setResponseEntityBody (reqbody);90if (count == 1) {91req.setResponseHeader ("Connection", "close");92}93req.sendResponse (200, "OK");94if (count == 1) {95req.orderlyClose();96}97break;98case 2: /* test 3 */99reqbody = req.getRequestEntityBody();100if (!reqbody.equals(str2)) {101req.sendResponse (500, "Internal server error");102req.orderlyClose();103}104int clen = Integer.parseInt (105req.getRequestHeader ("Content-length"));106if (clen != str2.length()) {107req.sendResponse (501, "Internal server error");108req.orderlyClose();109}110req.setResponseEntityBody (reqbody);111req.setResponseHeader ("Connection", "close");112req.sendResponse (200, "OK");113req.orderlyClose();114break;115case 3: /* test 6 */116req.setResponseHeader ("Location", "https://foo.bar/");117req.setResponseHeader ("Connection", "close");118req.sendResponse (307, "Temporary Redirect");119req.orderlyClose();120break;121case 4: /* test 7 */122case 5: /* test 8 */123reqbody = req.getRequestEntityBody();124if (reqbody != null && !"".equals (reqbody)) {125req.sendResponse (501, "Internal server error");126req.orderlyClose();127}128req.setResponseHeader ("Connection", "close");129req.sendResponse (200, "OK");130req.orderlyClose();131break;132}133} catch (IOException e) {134e.printStackTrace();135}136}137138static void readAndCompare (InputStream is, String cmp) throws IOException {139int c;140byte buf[] = new byte [1024];141int off = 0;142int len = 1024;143while ((c=is.read(buf, off, len)) != -1) {144off += c;145len -= c;146}147String s1 = new String (buf, 0, off, "ISO8859_1");148if (!cmp.equals(s1)) {149throw new IOException ("strings not same");150}151}152153/* basic chunked test (runs twice) */154155static void test1 (String u) throws Exception {156URL url = new URL (u);157System.out.println ("client opening connection to: " + u);158HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();159urlc.setChunkedStreamingMode (20);160urlc.setDoOutput(true);161urlc.setRequestMethod ("POST");162OutputStream os = urlc.getOutputStream ();163os.write (str1.getBytes());164os.close();165InputStream is = urlc.getInputStream();166readAndCompare (is, str1);167is.close();168}169170/* basic fixed length test */171172static void test3 (String u) throws Exception {173URL url = new URL (u);174System.out.println ("client opening connection to: " + u);175HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();176urlc.setFixedLengthStreamingMode (str2.length());177urlc.setDoOutput(true);178urlc.setRequestMethod ("POST");179OutputStream os = urlc.getOutputStream ();180os.write (str2.getBytes());181os.close();182InputStream is = urlc.getInputStream();183readAndCompare (is, str2);184is.close();185}186187/* write too few bytes */188189static void test4 (String u) throws Exception {190URL url = new URL (u);191System.out.println ("client opening connection to: " + u);192HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();193urlc.setFixedLengthStreamingMode (str2.length()+1);194urlc.setDoOutput(true);195urlc.setRequestMethod ("POST");196OutputStream os = urlc.getOutputStream ();197os.write (str2.getBytes());198try {199os.close();200throw new Exception ("should have thrown IOException");201} catch (IOException e) {}202}203204/* write too many bytes */205206static void test5 (String u) throws Exception {207URL url = new URL (u);208System.out.println ("client opening connection to: " + u);209HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();210urlc.setFixedLengthStreamingMode (str2.length()-1);211urlc.setDoOutput(true);212urlc.setRequestMethod ("POST");213OutputStream os = urlc.getOutputStream ();214try {215os.write (str2.getBytes());216throw new Exception ("should have thrown IOException");217} catch (IOException e) {}218}219220/* check for HttpRetryException on redirection */221222static void test6 (String u) throws Exception {223URL url = new URL (u);224System.out.println ("client opening connection to: " + u);225HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();226urlc.setChunkedStreamingMode (20);227urlc.setDoOutput(true);228urlc.setRequestMethod ("POST");229OutputStream os = urlc.getOutputStream ();230os.write (str1.getBytes());231os.close();232try {233InputStream is = urlc.getInputStream();234throw new Exception ("should have gotten HttpRetryException");235} catch (HttpRetryException e) {236if (e.responseCode() != 307) {237throw new Exception ("Wrong response code " + e.responseCode());238}239if (!e.getLocation().equals ("https://foo.bar/")) {240throw new Exception ("Wrong location " + e.getLocation());241}242}243}244245/* next two tests send zero length posts */246247static void test7 (String u) throws Exception {248URL url = new URL (u);249System.out.println ("client opening connection to: " + u);250HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();251urlc.setChunkedStreamingMode (20);252urlc.setDoOutput(true);253urlc.setRequestMethod ("POST");254OutputStream os = urlc.getOutputStream ();255os.close();256int ret = urlc.getResponseCode();257if (ret != 200) {258throw new Exception ("Expected 200: got " + ret);259}260}261262static void test8 (String u) throws Exception {263URL url = new URL (u);264System.out.println ("client opening connection to: " + u);265HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();266urlc.setFixedLengthStreamingMode (0);267urlc.setDoOutput(true);268urlc.setRequestMethod ("POST");269OutputStream os = urlc.getOutputStream ();270os.close();271int ret = urlc.getResponseCode();272if (ret != 200) {273throw new Exception ("Expected 200: got " + ret);274}275}276277static TestHttpsServer server;278279public static void main (String[] args) throws Exception {280// setup properties to do ssl281String keyFilename =282System.getProperty("test.src", "./") + "/" + pathToStores +283"/" + keyStoreFile;284String trustFilename =285System.getProperty("test.src", "./") + "/" + pathToStores +286"/" + trustStoreFile;287288HostnameVerifier reservedHV =289HttpsURLConnection.getDefaultHostnameVerifier();290try {291System.setProperty("javax.net.ssl.keyStore", keyFilename);292System.setProperty("javax.net.ssl.keyStorePassword", passwd);293System.setProperty("javax.net.ssl.trustStore", trustFilename);294System.setProperty("javax.net.ssl.trustStorePassword", passwd);295HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());296297try {298server = new TestHttpsServer(299new ChunkedOutputStream(), 1, 10, 0);300System.out.println ("Server started: listening on port: " + server.getLocalPort());301// the test server doesn't support keep-alive yet302// test1("http://localhost:"+server.getLocalPort()+"/d0");303test1("https://localhost:"+server.getLocalPort()+"/d01");304test3("https://localhost:"+server.getLocalPort()+"/d3");305test4("https://localhost:"+server.getLocalPort()+"/d4");306test5("https://localhost:"+server.getLocalPort()+"/d5");307test6("https://localhost:"+server.getLocalPort()+"/d6");308test7("https://localhost:"+server.getLocalPort()+"/d7");309test8("https://localhost:"+server.getLocalPort()+"/d8");310} catch (Exception e) {311if (server != null) {312server.terminate();313}314throw e;315}316server.terminate();317} finally {318HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);319}320}321322static class NameVerifier implements HostnameVerifier {323public boolean verify(String hostname, SSLSession session) {324return true;325}326}327328public static void except (String s) {329server.terminate();330throw new RuntimeException (s);331}332}333334335