jdk/test/sun/net/www/http/ChunkedInputStream/ChunkedEncodingTest.java
changeset 2 90ce3da70b43
child 1339 5fb372f1d51f
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2004-2007 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /**
       
    25  * @test
       
    26  * @bug 4333920
       
    27  * @library ../../../../../sun/net/www/httptest/
       
    28  * @build HttpCallback HttpServer ClosedChannelList HttpTransaction
       
    29  * @run main ChunkedEncodingTest
       
    30  * @summary ChunkedEncodingTest unit test
       
    31  */
       
    32 
       
    33 import java.io.*;
       
    34 import java.net.*;
       
    35 import java.security.*;
       
    36 
       
    37 public class ChunkedEncodingTest implements HttpCallback {
       
    38     private static String FNPrefix;
       
    39     private String[] respBody = new String[52];
       
    40     private byte[][] bufs = new byte[52][8*1024];
       
    41     private static MessageDigest md5;
       
    42     private static byte[] file1Mac, file2Mac;
       
    43     public void request (HttpTransaction req) {
       
    44         try {
       
    45             FileInputStream fis = new FileInputStream(FNPrefix+"test.txt");
       
    46             DigestInputStream dis = null;
       
    47             md5.reset();
       
    48             dis = new DigestInputStream(fis, md5);
       
    49             for (int i = 0; i < 52; i++) {
       
    50                 int n = dis.read(bufs[i]);
       
    51                 respBody[i] = new String(bufs[i], 0, n);
       
    52             }
       
    53             file1Mac = dis.getMessageDigest().digest();
       
    54             dis.close();
       
    55             req.setResponseEntityBody(respBody);
       
    56             req.sendResponse(200, "OK");
       
    57             req.orderlyClose();
       
    58         } catch (IOException e) {
       
    59             e.printStackTrace();
       
    60         }
       
    61     }
       
    62 
       
    63     static void read (InputStream is) throws IOException {
       
    64         int c;
       
    65         System.out.println ("reading");
       
    66 
       
    67         DigestInputStream dis = null;
       
    68         md5.reset();
       
    69         dis = new DigestInputStream(is, md5);
       
    70         while ((c=dis.read()) != -1);
       
    71         file2Mac = dis.getMessageDigest().digest();
       
    72         dis.close();
       
    73         System.out.println ("finished reading");
       
    74     }
       
    75 
       
    76     static void client (String u) throws Exception {
       
    77         URL url = new URL (u);
       
    78         System.out.println ("client opening connection to: " + u);
       
    79         URLConnection urlc = url.openConnection ();
       
    80         InputStream is = urlc.getInputStream ();
       
    81         read (is);
       
    82         is.close();
       
    83     }
       
    84 
       
    85     static HttpServer server;
       
    86 
       
    87     public static void test () throws Exception {
       
    88         try {
       
    89 
       
    90             FNPrefix = System.getProperty("test.src", ".")+"/";
       
    91             md5 = MessageDigest.getInstance("MD5");
       
    92             server = new HttpServer (new ChunkedEncodingTest(), 1, 10, 0);
       
    93             System.out.println ("Server: listening on port: " + server.getLocalPort());
       
    94             client ("http://localhost:"+server.getLocalPort()+"/d1/foo.html");
       
    95         } catch (Exception e) {
       
    96             if (server != null) {
       
    97                 server.terminate();
       
    98             }
       
    99             throw e;
       
   100         }
       
   101         if (!MessageDigest.isEqual(file1Mac, file2Mac)) {
       
   102             except ("The file sent by server is different from the original file");
       
   103         }
       
   104 
       
   105         server.terminate();
       
   106     }
       
   107 
       
   108     public static void main(String[] args) throws Exception {
       
   109         test();
       
   110     }
       
   111 
       
   112     public static void except (String s) {
       
   113         server.terminate();
       
   114         throw new RuntimeException (s);
       
   115     }
       
   116 }