test/jdk/sun/net/www/protocol/https/ChunkedOutputStream.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 30912 jdk/test/sun/net/www/protocol/https/ChunkedOutputStream.java@5c720b6875a2
child 55031 37a077319427
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 13788
diff changeset
     2
 * Copyright (c) 2004, 2012, 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: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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 5026745
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 23052
diff changeset
    27
 * @modules java.base/sun.net.www
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 10328
diff changeset
    28
 * @build TestHttpsServer HttpCallback
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14342
diff changeset
    29
 * @run main/othervm ChunkedOutputStream
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    30
 *
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    31
 *     SunJSSE does not support dynamic system properties, no way to re-use
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    32
 *     system properties in samevm/agentvm mode.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @summary Cannot flush output stream when writing to an HttpUrlConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14342
diff changeset
    40
public class ChunkedOutputStream implements HttpCallback {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * Where do we find the keystores for ssl?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     */
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14342
diff changeset
    44
    static String pathToStores = "../../../../../javax/net/ssl/etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static final String str1 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                                "1234567890abcdefkjsdlkjflkjsldkfjlsdkjflkj"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                                "1434567890abcdefkjsdlkjflkjsldkfjlsdkjflkj";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static final String str2 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                                "1234567890";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public void request (HttpTransaction req) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            // this is needed (count++ doesn't work), 'cause we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            // are doing concurrent tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            String path = req.getRequestURI().getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            if (path.equals("/d0")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            } else if (path.equals("/d01")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                count = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            } else if (path.equals("/d3")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                count = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            } else if (path.equals("/d4") || path.equals("/d5")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                count = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            } else if (path.equals("/d6")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                count = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            }  else if (path.equals("/d7")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                count = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            }  else if (path.equals("/d8")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                count = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            case 0: /* test1 -- keeps conn alive */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            case 1: /* test2 -- closes conn */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                String reqbody = req.getRequestEntityBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                if (!reqbody.equals(str1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    req.sendResponse (500, "Internal server error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                String chunk = req.getRequestHeader ("Transfer-encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                if (!"chunked".equals (chunk)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    req.sendResponse (501, "Internal server error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                req.setResponseEntityBody (reqbody);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                if (count == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    req.setResponseHeader ("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                req.sendResponse (200, "OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                if (count == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            case 2: /* test 3 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                reqbody = req.getRequestEntityBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if (!reqbody.equals(str2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    req.sendResponse (500, "Internal server error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                int clen = Integer.parseInt (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                        req.getRequestHeader ("Content-length"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                if (clen != str2.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    req.sendResponse (501, "Internal server error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                req.setResponseEntityBody (reqbody);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                req.setResponseHeader ("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                req.sendResponse (200, "OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            case 3: /* test 6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                req.setResponseHeader ("Location", "https://foo.bar/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                req.setResponseHeader ("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                req.sendResponse (307, "Temporary Redirect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            case 4: /* test 7 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            case 5: /* test 8 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                reqbody = req.getRequestEntityBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                if (reqbody != null && !"".equals (reqbody)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    req.sendResponse (501, "Internal server error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                req.setResponseHeader ("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                req.sendResponse (200, "OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    static void readAndCompare (InputStream is, String cmp) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        byte buf[] = new byte [1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        int len = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        while ((c=is.read(buf, off, len)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            off += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            len -= c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        String s1 = new String (buf, 0, off, "ISO8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (!cmp.equals(s1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throw new IOException ("strings not same");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /* basic chunked test (runs twice) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    static void test1 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        urlc.setChunkedStreamingMode (20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        os.write (str1.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        readAndCompare (is, str1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /* basic fixed length test */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    static void test3 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        urlc.setFixedLengthStreamingMode (str2.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        os.write (str2.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        readAndCompare (is, str2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /* write too few bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    static void test4 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        urlc.setFixedLengthStreamingMode (str2.length()+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        os.write (str2.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            throw new Exception ("should have thrown IOException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /* write too many bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    static void test5 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        urlc.setFixedLengthStreamingMode (str2.length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            os.write (str2.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            throw new Exception ("should have thrown IOException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /* check for HttpRetryException on redirection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    static void test6 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        urlc.setChunkedStreamingMode (20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        os.write (str1.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            throw new Exception ("should have gotten HttpRetryException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        } catch (HttpRetryException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            if (e.responseCode() != 307) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                throw new Exception ("Wrong response code " + e.responseCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if (!e.getLocation().equals ("https://foo.bar/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                throw new Exception ("Wrong location " + e.getLocation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /* next two tests send zero length posts */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    static void test7 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        urlc.setChunkedStreamingMode (20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        int ret = urlc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (ret != 200) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            throw new Exception ("Expected 200: got " + ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    static void test8 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        urlc.setFixedLengthStreamingMode (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        int ret = urlc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (ret != 200) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            throw new Exception ("Expected 200: got " + ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 10328
diff changeset
   279
    static TestHttpsServer server;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // setup properties to do ssl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   290
        HostnameVerifier reservedHV =
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   291
            HttpsURLConnection.getDefaultHostnameVerifier();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        try {
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   293
            System.setProperty("javax.net.ssl.keyStore", keyFilename);
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   294
            System.setProperty("javax.net.ssl.keyStorePassword", passwd);
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   295
            System.setProperty("javax.net.ssl.trustStore", trustFilename);
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   296
            System.setProperty("javax.net.ssl.trustStorePassword", passwd);
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   297
            HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   298
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   299
            try {
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14342
diff changeset
   300
                server = new TestHttpsServer(
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14342
diff changeset
   301
                        new ChunkedOutputStream(), 1, 10, 0);
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   302
                System.out.println ("Server started: listening on port: " + server.getLocalPort());
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   303
                // the test server doesn't support keep-alive yet
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   304
                // test1("http://localhost:"+server.getLocalPort()+"/d0");
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   305
                test1("https://localhost:"+server.getLocalPort()+"/d01");
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   306
                test3("https://localhost:"+server.getLocalPort()+"/d3");
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   307
                test4("https://localhost:"+server.getLocalPort()+"/d4");
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   308
                test5("https://localhost:"+server.getLocalPort()+"/d5");
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   309
                test6("https://localhost:"+server.getLocalPort()+"/d6");
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   310
                test7("https://localhost:"+server.getLocalPort()+"/d7");
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   311
                test8("https://localhost:"+server.getLocalPort()+"/d8");
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   312
            } catch (Exception e) {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   313
                if (server != null) {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   314
                    server.terminate();
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   315
                }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   316
                throw e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   318
            server.terminate();
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   319
        } finally {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   320
            HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    static class NameVerifier implements HostnameVerifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        public boolean verify(String hostname, SSLSession session) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public static void except (String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        throw new RuntimeException (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
}