1/*
2 * Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * @test
26 * @bug 4333920
27 * @modules jdk.httpserver
28 * @run main ChunkedEncodingTest
29 * @summary ChunkedEncodingTest unit test
30 */
31
32import java.io.*;
33import java.net.*;
34import java.security.*;
35import com.sun.net.httpserver.HttpServer;
36import com.sun.net.httpserver.HttpHandler;
37import com.sun.net.httpserver.HttpExchange;
38import static java.lang.System.out;
39
40public class ChunkedEncodingTest{
41    private static MessageDigest serverDigest, clientDigest;
42    private static volatile byte[] serverMac, clientMac;
43
44    static void client(String u) throws Exception {
45        URL url = new URL(u);
46        out.println("client opening connection to: " + u);
47        URLConnection urlc = url.openConnection();
48        DigestInputStream dis =
49                new DigestInputStream(urlc.getInputStream(), clientDigest);
50        while (dis.read() != -1);
51        clientMac = dis.getMessageDigest().digest();
52        dis.close();
53    }
54
55    public static void test() {
56        HttpServer server = null;
57        try {
58            serverDigest = MessageDigest.getInstance("MD5");
59            clientDigest = MessageDigest.getInstance("MD5");
60            server = startHttpServer();
61
62            int port = server.getAddress().getPort();
63            out.println ("Server listening on port: " + port);
64            client("http://localhost:" + port + "/chunked/");
65
66            if (!MessageDigest.isEqual(clientMac, serverMac)) {
67                throw new RuntimeException(
68                 "Data received is NOT equal to the data sent");
69            }
70        } catch (Exception e) {
71            throw new RuntimeException(e);
72        } finally {
73            if (server != null)
74                server.stop(0);
75        }
76    }
77
78    public static void main(String[] args) {
79        test();
80    }
81
82    /**
83     * Http Server
84     */
85    static HttpServer startHttpServer() throws IOException {
86        HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
87        HttpHandler httpHandler = new SimpleHandler();
88        httpServer.createContext("/chunked/", httpHandler);
89        httpServer.start();
90        return httpServer;
91    }
92
93    static class SimpleHandler implements HttpHandler {
94        static byte[] baMessage;
95        final static int CHUNK_SIZE = 8 * 1024;
96        final static int MESSAGE_LENGTH = 52 * CHUNK_SIZE;
97
98        static {
99            baMessage = new byte[MESSAGE_LENGTH];
100            for (int i=0; i<MESSAGE_LENGTH; i++)
101                baMessage[i] = (byte)i;
102        }
103
104        @Override
105        public void handle(HttpExchange t) throws IOException {
106            InputStream is = t.getRequestBody();
107            while (is.read() != -1);
108            is.close();
109
110            t.sendResponseHeaders (200, 0);
111            OutputStream os = t.getResponseBody();
112            DigestOutputStream dos = new DigestOutputStream(os, serverDigest);
113
114            int offset = 0;
115            for (int i=0; i<52; i++) {
116                dos.write(baMessage, offset, CHUNK_SIZE);
117                offset += CHUNK_SIZE;
118            }
119            serverMac = serverDigest.digest();
120            os.close();
121            t.close();
122        }
123    }
124}
125