test/jdk/com/sun/net/httpserver/Test13.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: 52121
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
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 47216
diff changeset
    27
 * @library /test/lib
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 47216
diff changeset
    28
 * @build jdk.test.lib.net.SimpleSSLContext
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    29
 * @run main/othervm Test13
55330
1fef7d9309a9 8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents: 52121
diff changeset
    30
 * @run main/othervm -Djava.net.preferIPv6Addresses=true Test13
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 47216
diff changeset
    31
 * @summary Light weight HTTP server
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.concurrent.*;
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 6115
diff changeset
    37
import java.util.logging.*;
2
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 javax.net.ssl.*;
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 47216
diff changeset
    41
import jdk.test.lib.net.SimpleSSLContext;
55330
1fef7d9309a9 8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents: 52121
diff changeset
    42
import jdk.test.lib.net.URIBuilder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/* basic http/s connectivity test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Tests:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *      - same as Test12, but with 64 threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class Test13 extends Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static SSLContext ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 6115
diff changeset
    53
    final static int NUM = 32; // was 32
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 6115
diff changeset
    54
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static boolean fail = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        HttpServer s1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        HttpsServer s2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        ExecutorService executor=null;
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 6115
diff changeset
    61
        Logger l = Logger.getLogger ("com.sun.net.httpserver");
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 6115
diff changeset
    62
        Handler ha = new ConsoleHandler();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 6115
diff changeset
    63
        ha.setLevel(Level.ALL);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 6115
diff changeset
    64
        l.setLevel(Level.ALL);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 6115
diff changeset
    65
        l.addHandler(ha);
55330
1fef7d9309a9 8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents: 52121
diff changeset
    66
        InetAddress loopback = InetAddress.getLoopbackAddress();
1fef7d9309a9 8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents: 52121
diff changeset
    67
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            String root = System.getProperty ("test.src")+ "/docs";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            System.out.print ("Test13: ");
55330
1fef7d9309a9 8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents: 52121
diff changeset
    71
            InetSocketAddress addr = new InetSocketAddress(loopback, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            s1 = HttpServer.create (addr, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            s2 = HttpsServer.create (addr, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            HttpHandler h = new FileServerHandler (root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            HttpContext c1 = s1.createContext ("/test1", h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            HttpContext c2 = s2.createContext ("/test1", h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            executor = Executors.newCachedThreadPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            s1.setExecutor (executor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            s2.setExecutor (executor);
26206
fb87c4051d65 8055747: Move SimpleSSLContext to jdk/testlibrary
michaelm
parents: 7668
diff changeset
    80
            ctx = new SimpleSSLContext().get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            s2.setHttpsConfigurator(new HttpsConfigurator (ctx));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            s1.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            s2.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            int port = s1.getAddress().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            int httpsport = s2.getAddress().getPort();
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 6115
diff changeset
    87
            Runner r[] = new Runner[NUM*2];
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 6115
diff changeset
    88
            for (int i=0; i<NUM; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                r[i] = new Runner (true, "http", root+"/test1", port, "smallfile.txt", 23);
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 6115
diff changeset
    90
                r[i+NUM] = new Runner (true, "https", root+"/test1", httpsport, "smallfile.txt", 23);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            start (r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            join (r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            System.out.println ("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            delay();
3070
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
    97
            if (s1 != null)
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
    98
                s1.stop(2);
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
    99
            if (s2 != null)
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
   100
                s2.stop(2);
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
   101
            if (executor != null)
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
   102
                executor.shutdown ();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    static void start (Runner[] x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        for (int i=0; i<x.length; i++) {
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 6115
diff changeset
   108
            if (x[i] != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            x[i].start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static void join (Runner[] x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        for (int i=0; i<x.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            try {
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 6115
diff changeset
   116
                if (x[i] != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                x[i].join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            } catch (InterruptedException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    static class Runner extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        boolean fixedLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        String protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        String root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        String f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        Runner (boolean fixedLen, String protocol, String root, int port, String f, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            this.fixedLen=fixedLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            this.protocol=protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            this.root=root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            this.port=port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            this.f=f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            try {
55330
1fef7d9309a9 8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents: 52121
diff changeset
   143
                URL url = URIBuilder.newBuilder()
1fef7d9309a9 8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents: 52121
diff changeset
   144
                          .scheme(protocol)
1fef7d9309a9 8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents: 52121
diff changeset
   145
                          .loopback()
1fef7d9309a9 8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents: 52121
diff changeset
   146
                          .port(port)
1fef7d9309a9 8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents: 52121
diff changeset
   147
                          .path("/test1/"+f)
1fef7d9309a9 8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents: 52121
diff changeset
   148
                          .toURL();
1fef7d9309a9 8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents: 52121
diff changeset
   149
                HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                if (urlc instanceof HttpsURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    urlcs.setHostnameVerifier (new HostnameVerifier () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        public boolean verify (String s, SSLSession s1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    urlcs.setSSLSocketFactory (ctx.getSocketFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                byte [] buf = new byte [4096];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                if (fixedLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    urlc.setRequestProperty ("XFixed", "yes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                File temp = File.createTempFile ("Test1", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                temp.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                int c, count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                while ((c=is.read(buf)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    count += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    fout.write (buf, 0, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                fout.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                if (count != size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    throw new RuntimeException ("wrong amount of data returned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                String orig = root + "/" + f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                compare (new File(orig), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                temp.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                fail = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /* compare the contents of the two files */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    static void compare (File f1, File f2) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        InputStream i1 = new BufferedInputStream (new FileInputStream(f1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        InputStream i2 = new BufferedInputStream (new FileInputStream(f2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        int c1,c2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            while ((c1=i1.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                c2 = i2.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                if (c1 != c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    throw new RuntimeException ("file compare failed 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (i2.read() != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                throw new RuntimeException ("file compare failed 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            i1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            i2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
}