Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/ChunkedInputStream/ChunkedEncodingTest.java
38867 views
/*1* Copyright (c) 2004, 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 433392026* @run main ChunkedEncodingTest27* @summary ChunkedEncodingTest unit test28*/2930import java.io.*;31import java.net.*;32import java.security.*;33import com.sun.net.httpserver.HttpServer;34import com.sun.net.httpserver.HttpHandler;35import com.sun.net.httpserver.HttpExchange;36import static java.lang.System.out;3738public class ChunkedEncodingTest{39private static MessageDigest serverDigest, clientDigest;40private static volatile byte[] serverMac, clientMac;4142static void client(String u) throws Exception {43URL url = new URL(u);44out.println("client opening connection to: " + u);45URLConnection urlc = url.openConnection();46DigestInputStream dis =47new DigestInputStream(urlc.getInputStream(), clientDigest);48while (dis.read() != -1);49clientMac = dis.getMessageDigest().digest();50dis.close();51}5253public static void test() {54HttpServer server = null;55try {56serverDigest = MessageDigest.getInstance("MD5");57clientDigest = MessageDigest.getInstance("MD5");58server = startHttpServer();5960int port = server.getAddress().getPort();61out.println ("Server listening on port: " + port);62client("http://localhost:" + port + "/chunked/");6364if (!MessageDigest.isEqual(clientMac, serverMac)) {65throw new RuntimeException(66"Data received is NOT equal to the data sent");67}68} catch (Exception e) {69throw new RuntimeException(e);70} finally {71if (server != null)72server.stop(0);73}74}7576public static void main(String[] args) {77test();78}7980/**81* Http Server82*/83static HttpServer startHttpServer() throws IOException {84HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);85HttpHandler httpHandler = new SimpleHandler();86httpServer.createContext("/chunked/", httpHandler);87httpServer.start();88return httpServer;89}9091static class SimpleHandler implements HttpHandler {92static byte[] baMessage;93final static int CHUNK_SIZE = 8 * 1024;94final static int MESSAGE_LENGTH = 52 * CHUNK_SIZE;9596static {97baMessage = new byte[MESSAGE_LENGTH];98for (int i=0; i<MESSAGE_LENGTH; i++)99baMessage[i] = (byte)i;100}101102@Override103public void handle(HttpExchange t) throws IOException {104InputStream is = t.getRequestBody();105while (is.read() != -1);106is.close();107108t.sendResponseHeaders (200, 0);109OutputStream os = t.getResponseBody();110DigestOutputStream dos = new DigestOutputStream(os, serverDigest);111112int offset = 0;113for (int i=0; i<52; i++) {114dos.write(baMessage, offset, CHUNK_SIZE);115offset += CHUNK_SIZE;116}117serverMac = serverDigest.digest();118os.close();119t.close();120}121}122}123124125