1/*
2 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * @test
26 * @bug 6270015
27 * @library /lib/testlibrary/
28 * @build jdk.testlibrary.SimpleSSLContext
29 * @run main/othervm Test1
30 * @run main/othervm -Dsun.net.httpserver.maxReqTime=10 Test1
31 * @run main/othervm -Dsun.net.httpserver.nodelay=true Test1
32 * @summary  Light weight HTTP server
33 */
34
35import com.sun.net.httpserver.*;
36
37import java.util.concurrent.*;
38import java.io.*;
39import java.net.*;
40import javax.net.ssl.*;
41import jdk.testlibrary.SimpleSSLContext;
42
43/* basic http/s connectivity test
44 * Tests:
45 *      - client/server
46 *      - send/receive large/small file
47 *      - chunked encoding
48 *      - via http and https
49 *
50 * The test is also run with sun.net.httpserver.nodelay simply to exercise
51 * this option. There is no specific pass or failure related to running with
52 * this option.
53 */
54
55public class Test1 extends Test {
56
57    static SSLContext ctx;
58
59    public static void main (String[] args) throws Exception {
60        HttpServer s1 = null;
61        HttpsServer s2 = null;
62        ExecutorService executor=null;
63        try {
64            String root = System.getProperty ("test.src")+ "/docs";
65            System.out.print ("Test1: ");
66            InetSocketAddress addr = new InetSocketAddress (0);
67            s1 = HttpServer.create (addr, 0);
68            if (s1 instanceof HttpsServer) {
69                throw new RuntimeException ("should not be httpsserver");
70            }
71            s2 = HttpsServer.create (addr, 0);
72            HttpHandler h = new FileServerHandler (root);
73            HttpContext c1 = s1.createContext ("/test1", h);
74            HttpContext c2 = s2.createContext ("/test1", h);
75            executor = Executors.newCachedThreadPool();
76            s1.setExecutor (executor);
77            s2.setExecutor (executor);
78            ctx = new SimpleSSLContext().get();
79            s2.setHttpsConfigurator(new HttpsConfigurator (ctx));
80            s1.start();
81            s2.start();
82
83            int port = s1.getAddress().getPort();
84            int httpsport = s2.getAddress().getPort();
85            test (true, "http", root+"/test1", port, "smallfile.txt", 23);
86            test (true, "http", root+"/test1", port, "largefile.txt", 2730088);
87            test (true, "https", root+"/test1", httpsport, "smallfile.txt", 23);
88            test (true, "https", root+"/test1", httpsport, "largefile.txt", 2730088);
89            test (false, "http", root+"/test1", port, "smallfile.txt", 23);
90            test (false, "http", root+"/test1", port, "largefile.txt", 2730088);
91            test (false, "https", root+"/test1", httpsport, "smallfile.txt", 23);
92            test (false, "https", root+"/test1", httpsport, "largefile.txt", 2730088);
93            System.out.println ("OK");
94        } finally {
95            delay();
96            if (s1 != null)
97                s1.stop(2);
98            if (s2 != null)
99                s2.stop(2);
100            if (executor != null)
101                executor.shutdown ();
102        }
103    }
104
105    static void test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception {
106        URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f);
107        HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
108        if (urlc instanceof HttpsURLConnection) {
109            HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
110            urlcs.setHostnameVerifier (new HostnameVerifier () {
111                public boolean verify (String s, SSLSession s1) {
112                    return true;
113                }
114            });
115            urlcs.setSSLSocketFactory (ctx.getSocketFactory());
116        }
117        byte [] buf = new byte [4096];
118
119        if (fixedLen) {
120            urlc.setRequestProperty ("XFixed", "yes");
121        }
122        InputStream is = urlc.getInputStream();
123        File temp = File.createTempFile ("Test1", null);
124        temp.deleteOnExit();
125        OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));
126        int c, count = 0;
127        while ((c=is.read(buf)) != -1) {
128            count += c;
129            fout.write (buf, 0, c);
130        }
131        is.close();
132        fout.close();
133
134        if (count != size) {
135            throw new RuntimeException ("wrong amount of data returned");
136        }
137        String orig = root + "/" + f;
138        compare (new File(orig), temp);
139        temp.delete();
140    }
141
142    /* compare the contents of the two files */
143
144    static void compare (File f1, File f2) throws IOException {
145        InputStream i1 = new BufferedInputStream (new FileInputStream(f1));
146        InputStream i2 = new BufferedInputStream (new FileInputStream(f2));
147
148        int c1,c2;
149        try {
150            while ((c1=i1.read()) != -1) {
151                c2 = i2.read();
152                if (c1 != c2) {
153                    throw new RuntimeException ("file compare failed 1");
154                }
155            }
156            if (i2.read() != -1) {
157                throw new RuntimeException ("file compare failed 2");
158            }
159        } finally {
160            i1.close();
161            i2.close();
162        }
163    }
164}
165