jdk/test/sun/net/www/http/ChunkedInputStream/ChunkedEncodingTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1339 5fb372f1d51f
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2004-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4333920
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @library ../../../../../sun/net/www/httptest/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @build HttpCallback HttpServer ClosedChannelList HttpTransaction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run main ChunkedEncodingTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @summary ChunkedEncodingTest unit test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class ChunkedEncodingTest implements HttpCallback {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static String FNPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private String[] respBody = new String[52];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private byte[][] bufs = new byte[52][8*1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static MessageDigest md5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static byte[] file1Mac, file2Mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public void request (HttpTransaction req) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            FileInputStream fis = new FileInputStream(FNPrefix+"test.txt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            DigestInputStream dis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            md5.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            dis = new DigestInputStream(fis, md5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            for (int i = 0; i < 52; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                int n = dis.read(bufs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                respBody[i] = new String(bufs[i], 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            file1Mac = dis.getMessageDigest().digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            dis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            req.setResponseEntityBody(respBody);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            req.sendResponse(200, "OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static void read (InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        System.out.println ("reading");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        DigestInputStream dis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        md5.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        dis = new DigestInputStream(is, md5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        while ((c=dis.read()) != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        file2Mac = dis.getMessageDigest().digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        dis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        System.out.println ("finished reading");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static void client (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        URLConnection urlc = url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        InputStream is = urlc.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        read (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    static HttpServer server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public static void test () throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            FNPrefix = System.getProperty("test.src", ".")+"/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            md5 = MessageDigest.getInstance("MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            server = new HttpServer (new ChunkedEncodingTest(), 1, 10, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            System.out.println ("Server: listening on port: " + server.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            client ("http://localhost:"+server.getLocalPort()+"/d1/foo.html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (server != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (!MessageDigest.isEqual(file1Mac, file2Mac)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            except ("The file sent by server is different from the original file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public static void except (String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        throw new RuntimeException (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
}