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