jdk/test/java/net/ResponseCache/Test2.java
author mli
Sun, 12 Jun 2016 21:55:20 -0700
changeset 38883 d5de564f8089
parent 27177 3717db2c3bfe
permissions -rw-r--r--
8151913: Fix module dependencies in java/net tests Summary: Module dependency declaration for java/net/* tests Reviewed-by: alanb Contributed-by: John Jiang <sha.jiang@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27177
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
     1
/*
38883
d5de564f8089 8151913: Fix module dependencies in java/net tests
mli
parents: 27177
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
27177
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
     4
 *
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
     8
 *
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    13
 * accompanied this code).
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    14
 *
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    18
 *
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    21
 * questions.
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    22
 */
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    23
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    24
/**
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    25
 * @test
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    26
 * @bug 8042622
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    27
 * @summary Check for CRL results in IllegalArgumentException "white space not allowed"
38883
d5de564f8089 8151913: Fix module dependencies in java/net tests
mli
parents: 27177
diff changeset
    28
 * @modules jdk.httpserver
27177
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    29
 * @run main/othervm Test2
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    30
 */
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    31
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    32
import com.sun.net.httpserver.*;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    33
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    34
import java.util.*;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    35
import java.util.concurrent.*;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    36
import java.io.*;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    37
import java.net.*;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    38
import java.security.*;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    39
import javax.security.auth.callback.*;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    40
import javax.net.ssl.*;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    41
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    42
public class Test2 {
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    43
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    44
    static volatile boolean failed = false;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    45
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    46
    static class Cache extends ResponseCache {
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    47
        public CacheResponse get(URI uri, String method, Map<String,List<String>> headers) {
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    48
            Set<String> keys = headers.keySet();
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    49
            for (String key : keys) {
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    50
                if (key.indexOf(' ') != -1 || key.indexOf('\t') != -1
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    51
                        || key.indexOf(':') != -1)
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    52
                {
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    53
                    failed = true;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    54
                }
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    55
            }
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    56
            return null;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    57
        }
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    58
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    59
        public CacheRequest put(URI uri, URLConnection c) throws IOException {
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    60
            return null;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    61
        }
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    62
    }
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    63
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    64
    static int port;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    65
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    66
    static String urlstring, redirstring;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    67
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    68
    public static void main (String[] args) throws Exception {
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    69
        Handler handler = new Handler();
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    70
        InetSocketAddress addr = new InetSocketAddress (0);
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    71
        HttpServer server = HttpServer.create (addr, 0);
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    72
        port = server.getAddress().getPort();
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    73
        HttpContext ctx = server.createContext ("/test", handler);
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    74
        System.out.println ("Server: " + server.getAddress().getPort());
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    75
        ResponseCache.setDefault(new Cache());
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    76
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    77
        ExecutorService executor = Executors.newCachedThreadPool();
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    78
        server.setExecutor (executor);
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    79
        server.start ();
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    80
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    81
        urlstring = "http://127.0.0.1:" + Integer.toString(port)+"/test/foo";
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    82
        redirstring = urlstring + "/redirect/bar";
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    83
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    84
        URL url = new URL (urlstring);
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    85
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    86
        urlc.addRequestProperty("X-Foo", "bar");
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    87
        urlc.setInstanceFollowRedirects(true);
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    88
        System.out.println(urlc.getResponseCode());
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    89
        InputStream i = urlc.getInputStream();
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    90
        int count=0;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    91
        for (int c=i.read(); c!=-1; c=i.read()) {
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    92
            //System.out.write(c);
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    93
            count++;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    94
        }
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    95
        System.out.println("Read " + count);
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    96
        System.out.println("FINISHED");
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    97
        server.stop(0);
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    98
        executor.shutdownNow();
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
    99
        if (failed) {
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   100
            throw new RuntimeException("Test failed");
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   101
        }
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   102
    }
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   103
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   104
    public static boolean error = false;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   105
    public static int count = 0;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   106
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   107
    static class Handler implements HttpHandler {
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   108
        int invocation = 0;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   109
        public void handle (HttpExchange t)
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   110
            throws IOException
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   111
        {
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   112
            InputStream is = t.getRequestBody();
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   113
            Headers map = t.getRequestHeaders();
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   114
            Headers rmap = t.getResponseHeaders();
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   115
            invocation ++;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   116
            if (invocation == 1) {
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   117
                rmap.add("Location", redirstring);
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   118
                while (is.read () != -1) ;
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   119
                is.close();
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   120
                System.out.println ("sending response");
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   121
                t.sendResponseHeaders (301, 0);
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   122
            } else {
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   123
                byte[] buf = "Hello world".getBytes();
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   124
                t.sendResponseHeaders (200, buf.length);
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   125
                OutputStream os = t.getResponseBody();
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   126
                try {
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   127
                        os.write(buf);
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   128
                } catch (IOException e) {
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   129
                        System.out.println ("EX 1 " + e);
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   130
                }
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   131
            }
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   132
            System.out.println ("Closing");
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   133
            t.close();
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   134
        }
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   135
    }
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents:
diff changeset
   136
}