jdk/test/com/sun/net/httpserver/Test9a.java
author chegar
Wed, 21 Jul 2010 13:29:26 +0100
changeset 6115 7c523cf2bc8a
parent 5506 202f599c92aa
child 7668 d4a77089c587
permissions -rw-r--r--
6969395: TEST_BUG: Tests in java/net sun/net problems Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3070
diff changeset
     2
 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3070
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3070
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3070
diff changeset
    21
 * questions.
2
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 6270015
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    27
 * @run main/othervm Test9a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary  Light weight HTTP server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/* Same as Test1 but requests run in parallel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class Test9a extends Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static SSLContext serverCtx, clientCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static boolean error = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        HttpsServer server = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        ExecutorService executor=null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            String root = System.getProperty ("test.src")+ "/docs";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            System.out.print ("Test9a: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            InetSocketAddress addr = new InetSocketAddress (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            server = HttpsServer.create (addr, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            HttpHandler h = new FileServerHandler (root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            HttpContext c1 = server.createContext ("/test1", h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            executor = Executors.newCachedThreadPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            server.setExecutor (executor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            serverCtx = new SimpleSSLContext(System.getProperty("test.src")).get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            clientCtx = new SimpleSSLContext(System.getProperty("test.src")).get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            server.setHttpsConfigurator(new HttpsConfigurator (serverCtx));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            int port = server.getAddress().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            error = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            Thread[] t = new Thread[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            t[0] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            t[1] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            t[2] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            t[3] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            t[4] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            t[5] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            t[6] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            t[7] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            t[8] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            t[9] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            t[10] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            t[11] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            t[12] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            t[13] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            t[14] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            t[15] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            for (int i=0; i<16; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                t[i].join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                throw new RuntimeException ("error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            System.out.println ("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            delay();
3070
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
    93
            if (server != null)
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
    94
                server.stop(2);
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
    95
            if (executor != null)
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
    96
                executor.shutdown();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    static int foo = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    static ClientThread test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        ClientThread t = new ClientThread (fixedLen, protocol, root, port, f, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    static Object fileLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    static class ClientThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        boolean fixedLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        String protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        String root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        String f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        ClientThread (boolean fixedLen, String protocol, String root, int port, String f, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            this.fixedLen = fixedLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            this.protocol = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            this.root = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            this.f =  f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                if (urlc instanceof HttpsURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    urlcs.setHostnameVerifier (new HostnameVerifier () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                        public boolean verify (String s, SSLSession s1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    urlcs.setSSLSocketFactory (clientCtx.getSocketFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                byte [] buf = new byte [4096];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                String s = "chunk";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                if (fixedLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    urlc.setRequestProperty ("XFixed", "yes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    s = "fixed";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                File temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                synchronized (fileLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    temp = File.createTempFile (s, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    temp.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                int c, count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                while ((c=is.read(buf)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    count += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    fout.write (buf, 0, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                fout.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                if (count != size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    System.out.println ("wrong amount of data returned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    System.out.println ("fixedLen = "+fixedLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    System.out.println ("protocol = "+protocol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    System.out.println ("root = "+root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    System.out.println ("port = "+port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    System.out.println ("f = "+f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    System.out.println ("size = "+size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    System.out.println ("temp = "+temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    System.out.println ("count = "+count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                String orig = root + "/" + f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                compare (new File(orig), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                temp.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /* compare the contents of the two files */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    static void compare (File f1, File f2) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        InputStream i1 = new BufferedInputStream (new FileInputStream(f1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        InputStream i2 = new BufferedInputStream (new FileInputStream(f2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        int c1,c2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            while ((c1=i1.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                c2 = i2.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                if (c1 != c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    throw new RuntimeException ("file compare failed 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (i2.read() != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                throw new RuntimeException ("file compare failed 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            i1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            i2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
}