Test1.java revision 3105
0N/A/*
4028N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2362N/A */
2362N/A
0N/A/**
0N/A * @test
0N/A * @bug 6270015
0N/A * @run main/othervm Test1
0N/A * @run main/othervm -Dsun.net.httpserver.maxReqTime=10 Test1
0N/A * @summary Light weight HTTP server
0N/A */
0N/A
0N/Aimport com.sun.net.httpserver.*;
0N/A
0N/Aimport java.util.concurrent.*;
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport javax.net.ssl.*;
0N/A
0N/A/* basic http/s connectivity test
0N/A * Tests:
0N/A * - client/server
0N/A * - send/receive large/small file
0N/A * - chunked encoding
0N/A * - via http and https
0N/A */
0N/A
0N/Apublic class Test1 extends Test {
0N/A
0N/A static SSLContext ctx;
0N/A
0N/A public static void main (String[] args) throws Exception {
0N/A HttpServer s1 = null;
0N/A HttpsServer s2 = null;
0N/A ExecutorService executor=null;
0N/A try {
0N/A String root = System.getProperty ("test.src")+ "/docs";
0N/A System.out.print ("Test1: ");
0N/A InetSocketAddress addr = new InetSocketAddress (0);
0N/A s1 = HttpServer.create (addr, 0);
0N/A if (s1 instanceof HttpsServer) {
0N/A throw new RuntimeException ("should not be httpsserver");
0N/A }
0N/A s2 = HttpsServer.create (addr, 0);
0N/A HttpHandler h = new FileServerHandler (root);
0N/A HttpContext c1 = s1.createContext ("/test1", h);
0N/A HttpContext c2 = s2.createContext ("/test1", h);
0N/A executor = Executors.newCachedThreadPool();
0N/A s1.setExecutor (executor);
0N/A s2.setExecutor (executor);
0N/A ctx = new SimpleSSLContext(System.getProperty("test.src")).get();
0N/A s2.setHttpsConfigurator(new HttpsConfigurator (ctx));
0N/A s1.start();
0N/A s2.start();
0N/A
0N/A int port = s1.getAddress().getPort();
0N/A int httpsport = s2.getAddress().getPort();
0N/A test (true, "http", root+"/test1", port, "smallfile.txt", 23);
0N/A test (true, "http", root+"/test1", port, "largefile.txt", 2730088);
0N/A test (true, "https", root+"/test1", httpsport, "smallfile.txt", 23);
0N/A test (true, "https", root+"/test1", httpsport, "largefile.txt", 2730088);
0N/A test (false, "http", root+"/test1", port, "smallfile.txt", 23);
0N/A test (false, "http", root+"/test1", port, "largefile.txt", 2730088);
0N/A test (false, "https", root+"/test1", httpsport, "smallfile.txt", 23);
0N/A test (false, "https", root+"/test1", httpsport, "largefile.txt", 2730088);
0N/A System.out.println ("OK");
0N/A } finally {
0N/A delay();
0N/A if (s1 != null)
0N/A s1.stop(2);
0N/A if (s2 != null)
0N/A s2.stop(2);
0N/A if (executor != null)
0N/A executor.shutdown ();
0N/A }
0N/A }
0N/A
0N/A static void test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception {
0N/A URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f);
0N/A HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
0N/A if (urlc instanceof HttpsURLConnection) {
0N/A HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
0N/A urlcs.setHostnameVerifier (new HostnameVerifier () {
0N/A public boolean verify (String s, SSLSession s1) {
0N/A return true;
0N/A }
0N/A });
0N/A urlcs.setSSLSocketFactory (ctx.getSocketFactory());
0N/A }
0N/A byte [] buf = new byte [4096];
0N/A
0N/A if (fixedLen) {
0N/A urlc.setRequestProperty ("XFixed", "yes");
0N/A }
0N/A InputStream is = urlc.getInputStream();
0N/A File temp = File.createTempFile ("Test1", null);
0N/A temp.deleteOnExit();
0N/A OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));
0N/A int c, count = 0;
0N/A while ((c=is.read(buf)) != -1) {
0N/A count += c;
0N/A fout.write (buf, 0, c);
0N/A }
0N/A is.close();
0N/A fout.close();
0N/A
0N/A if (count != size) {
0N/A throw new RuntimeException ("wrong amount of data returned");
0N/A }
0N/A String orig = root + "/" + f;
0N/A compare (new File(orig), temp);
0N/A temp.delete();
0N/A }
0N/A
0N/A /* compare the contents of the two files */
0N/A
0N/A static void compare (File f1, File f2) throws IOException {
0N/A InputStream i1 = new BufferedInputStream (new FileInputStream(f1));
0N/A InputStream i2 = new BufferedInputStream (new FileInputStream(f2));
0N/A
0N/A int c1,c2;
0N/A try {
0N/A while ((c1=i1.read()) != -1) {
0N/A c2 = i2.read();
0N/A if (c1 != c2) {
0N/A throw new RuntimeException ("file compare failed 1");
0N/A }
0N/A }
0N/A if (i2.read() != -1) {
0N/A throw new RuntimeException ("file compare failed 2");
0N/A }
0N/A } finally {
0N/A i1.close();
0N/A i2.close();
0N/A }
0N/A }
0N/A}
0N/A