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