test/jdk/com/sun/net/httpserver/Test4.java
author dholmes
Wed, 06 Nov 2019 21:18:42 -0500
changeset 58956 9a0a5e70eeb2
parent 55330 1fef7d9309a9
permissions -rw-r--r--
8233454: Test fails with assert(!is_init_completed(), "should only happen during init") after JDK-8229516 Reviewed-by: jiefu, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55330
1fef7d9309a9 8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents: 47216
diff changeset
     2
 * Copyright (c) 2005, 2019, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary  Light weight HTTP server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @run main/othervm -Dsun.net.httpserver.idleInterval=4 Test4
55330
1fef7d9309a9 8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents: 47216
diff changeset
    29
 * @run main/othervm -Djava.net.preferIPv6Addresses=true
1fef7d9309a9 8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents: 47216
diff changeset
    30
 *                   -Dsun.net.httpserver.idleInterval=4 Test4
2
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 com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.regex.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Test pipe-lining (block after read)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class Test4 extends Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static int count = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        System.out.print ("Test4: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        Handler handler = new Handler();
55330
1fef7d9309a9 8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents: 47216
diff changeset
    51
        InetAddress loopback = InetAddress.getLoopbackAddress();
1fef7d9309a9 8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents: 47216
diff changeset
    52
        InetSocketAddress addr = new InetSocketAddress(loopback, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        HttpServer server = HttpServer.create (addr, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        int port = server.getAddress().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        HttpContext c2 = server.createContext ("/test", handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        c2.getAttributes().put ("name", "This is the http handler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        ExecutorService exec = Executors.newCachedThreadPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        server.setExecutor (exec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            server.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            doClient(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            System.out.println ("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            delay();
3070
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
    66
            if (server != null)
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
    67
                server.stop(2);
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
    68
            if (exec != null)
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
    69
                exec.shutdown();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static class Handler implements HttpHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        volatile int invocation = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        public void handle (HttpExchange t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            InputStream is = t.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            Headers map = t.getRequestHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            Headers rmap = t.getResponseHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            int x = invocation ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            rmap.set ("XTest", Integer.toString (x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            switch (x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                checkBody (is, body1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                try {Thread.sleep (2000); } catch (Exception e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                checkBody (is, body2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                try {Thread.sleep (1000); } catch (Exception e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                checkBody (is, body3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                checkBody (is, body4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            t.sendResponseHeaders (200, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            t.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    static void checkBody (InputStream is, String cmp) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        byte [] b = new byte [1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        int count = 0, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        while ((c=is.read(b, count, b.length-count)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            count+=c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        String s = new String (b, 0, count, "ISO8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (!s.equals (cmp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throw new RuntimeException ("strings not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    static String body1 = "1234567890abcdefghij";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    static String body2 = "2234567890abcdefghij0123456789";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static String body3 = "3wertyuiop";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    static String body4 = "4234567890";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    static String result =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        "HTTP/1.1 200 OK.*Xtest: 0.*"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        "HTTP/1.1 200 OK.*Xtest: 1.*"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        "HTTP/1.1 200 OK.*Xtest: 2.*"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        "HTTP/1.1 200 OK.*Xtest: 3.*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public static void doClient (int port) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        String s = "GET /test/1.html HTTP/1.1\r\nContent-length: 20\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        "\r\n" +body1 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        "GET /test/2.html HTTP/1.1\r\nContent-length: 30\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        "\r\n"+ body2 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        "GET /test/3.html HTTP/1.1\r\nContent-length: 10\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        "\r\n"+ body3 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        "GET /test/4.html HTTP/1.1\r\nContent-length: 10\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        "\r\n"+body4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
55330
1fef7d9309a9 8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents: 47216
diff changeset
   139
        Socket socket = new Socket (InetAddress.getLoopbackAddress(), port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        OutputStream os = socket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        os.write (s.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        InputStream is = socket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        int c, count=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        byte[] b = new byte [1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        while ((c=is.read(b, count, b.length-count)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            count +=c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        s = new String (b,0,count, "ISO8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (!compare (s, result)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throw new RuntimeException ("wrong string result");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    static boolean compare (String s, String result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        Pattern pattern = Pattern.compile (result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                Pattern.DOTALL|Pattern.CASE_INSENSITIVE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        Matcher matcher = pattern.matcher (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return matcher.matches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
}