jdk/test/com/sun/net/httpserver/SelCacheTest.java
author chegar
Wed, 21 Jul 2010 13:29:26 +0100
changeset 6115 7c523cf2bc8a
parent 5506 202f599c92aa
child 26206 fb87c4051d65
permissions -rw-r--r--
6969395: TEST_BUG: Tests in java/net sun/net problems Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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 6270015
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @run main/othervm -Dsun.net.httpserver.selCacheTimeout=2 SelCacheTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary  Light weight HTTP server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/* basic http/s connectivity test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * (based on Test1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class SelCacheTest extends Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static SSLContext ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        HttpServer s1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        HttpsServer s2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        ExecutorService executor=null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            String root = System.getProperty ("test.src")+ "/docs";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            System.out.print ("Test1: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            InetSocketAddress addr = new InetSocketAddress (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            s1 = HttpServer.create (addr, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            if (s1 instanceof HttpsServer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                throw new RuntimeException ("should not be httpsserver");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            s2 = HttpsServer.create (addr, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            HttpHandler h = new FileServerHandler (root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            HttpContext c1 = s1.createContext ("/test1", h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            HttpContext c2 = s2.createContext ("/test1", h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            executor = Executors.newCachedThreadPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            s1.setExecutor (executor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            s2.setExecutor (executor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            ctx = new SimpleSSLContext(System.getProperty("test.src")).get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            s2.setHttpsConfigurator(new HttpsConfigurator (ctx));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            s1.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            s2.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            int port = s1.getAddress().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            int httpsport = s2.getAddress().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            test (true, "http", root+"/test1", port, "smallfile.txt", 23);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            test (true, "http", root+"/test1", port, "largefile.txt", 2730088);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            test (true, "https", root+"/test1", httpsport, "smallfile.txt", 23);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            test (true, "https", root+"/test1", httpsport, "largefile.txt", 2730088);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            test (false, "http", root+"/test1", port, "smallfile.txt", 23);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            test (false, "http", root+"/test1", port, "largefile.txt", 2730088);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            test (false, "https", root+"/test1", httpsport, "smallfile.txt", 23);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            test (false, "https", root+"/test1", httpsport, "largefile.txt", 2730088);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            System.out.println ("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            delay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            s1.stop(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            s2.stop(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            executor.shutdown ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    static void test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        Thread.sleep (2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (urlc instanceof HttpsURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            urlcs.setHostnameVerifier (new HostnameVerifier () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                public boolean verify (String s, SSLSession s1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            urlcs.setSSLSocketFactory (ctx.getSocketFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        byte [] buf = new byte [4096];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (fixedLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            urlc.setRequestProperty ("XFixed", "yes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        File temp = File.createTempFile ("Test1", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        temp.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        int c, count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        while ((c=is.read(buf)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            count += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            fout.write (buf, 0, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        fout.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (count != size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            throw new RuntimeException ("wrong amount of data returned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        String orig = root + "/" + f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        compare (new File(orig), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        temp.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /* compare the contents of the two files */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    static void compare (File f1, File f2) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        InputStream i1 = new BufferedInputStream (new FileInputStream(f1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        InputStream i2 = new BufferedInputStream (new FileInputStream(f2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        int c1,c2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            while ((c1=i1.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                c2 = i2.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                if (c1 != c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    throw new RuntimeException ("file compare failed 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (i2.read() != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                throw new RuntimeException ("file compare failed 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            i1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            i2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
}