Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/Test1.java
38855 views
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
* @run main/othervm Test1
28
* @run main/othervm -Dsun.net.httpserver.maxReqTime=10 Test1
29
* @run main/othervm -Dsun.net.httpserver.nodelay=true Test1
30
* @summary Light weight HTTP server
31
*/
32
33
import com.sun.net.httpserver.*;
34
35
import java.util.concurrent.*;
36
import java.io.*;
37
import java.net.*;
38
import javax.net.ssl.*;
39
40
/* basic http/s connectivity test
41
* Tests:
42
* - client/server
43
* - send/receive large/small file
44
* - chunked encoding
45
* - via http and https
46
*
47
* The test is also run with sun.net.httpserver.nodelay simply to exercise
48
* this option. There is no specific pass or failure related to running with
49
* this option.
50
*/
51
52
public class Test1 extends Test {
53
54
static SSLContext ctx;
55
56
public static void main (String[] args) throws Exception {
57
HttpServer s1 = null;
58
HttpsServer s2 = null;
59
ExecutorService executor=null;
60
try {
61
String root = System.getProperty ("test.src")+ "/docs";
62
System.out.print ("Test1: ");
63
InetSocketAddress addr = new InetSocketAddress (0);
64
s1 = HttpServer.create (addr, 0);
65
if (s1 instanceof HttpsServer) {
66
throw new RuntimeException ("should not be httpsserver");
67
}
68
s2 = HttpsServer.create (addr, 0);
69
HttpHandler h = new FileServerHandler (root);
70
HttpContext c1 = s1.createContext ("/test1", h);
71
HttpContext c2 = s2.createContext ("/test1", h);
72
executor = Executors.newCachedThreadPool();
73
s1.setExecutor (executor);
74
s2.setExecutor (executor);
75
ctx = new SimpleSSLContext(System.getProperty("test.src")).get();
76
s2.setHttpsConfigurator(new HttpsConfigurator (ctx));
77
s1.start();
78
s2.start();
79
80
int port = s1.getAddress().getPort();
81
int httpsport = s2.getAddress().getPort();
82
test (true, "http", root+"/test1", port, "smallfile.txt", 23);
83
test (true, "http", root+"/test1", port, "largefile.txt", 2730088);
84
test (true, "https", root+"/test1", httpsport, "smallfile.txt", 23);
85
test (true, "https", root+"/test1", httpsport, "largefile.txt", 2730088);
86
test (false, "http", root+"/test1", port, "smallfile.txt", 23);
87
test (false, "http", root+"/test1", port, "largefile.txt", 2730088);
88
test (false, "https", root+"/test1", httpsport, "smallfile.txt", 23);
89
test (false, "https", root+"/test1", httpsport, "largefile.txt", 2730088);
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 test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception {
103
URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f);
104
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
105
if (urlc instanceof HttpsURLConnection) {
106
HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
107
urlcs.setHostnameVerifier (new HostnameVerifier () {
108
public boolean verify (String s, SSLSession s1) {
109
return true;
110
}
111
});
112
urlcs.setSSLSocketFactory (ctx.getSocketFactory());
113
}
114
byte [] buf = new byte [4096];
115
116
if (fixedLen) {
117
urlc.setRequestProperty ("XFixed", "yes");
118
}
119
InputStream is = urlc.getInputStream();
120
File temp = File.createTempFile ("Test1", null);
121
temp.deleteOnExit();
122
OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));
123
int c, count = 0;
124
while ((c=is.read(buf)) != -1) {
125
count += c;
126
fout.write (buf, 0, c);
127
}
128
is.close();
129
fout.close();
130
131
if (count != size) {
132
throw new RuntimeException ("wrong amount of data returned");
133
}
134
String orig = root + "/" + f;
135
compare (new File(orig), temp);
136
temp.delete();
137
}
138
139
/* compare the contents of the two files */
140
141
static void compare (File f1, File f2) throws IOException {
142
InputStream i1 = new BufferedInputStream (new FileInputStream(f1));
143
InputStream i2 = new BufferedInputStream (new FileInputStream(f2));
144
145
int c1,c2;
146
try {
147
while ((c1=i1.read()) != -1) {
148
c2 = i2.read();
149
if (c1 != c2) {
150
throw new RuntimeException ("file compare failed 1");
151
}
152
}
153
if (i2.read() != -1) {
154
throw new RuntimeException ("file compare failed 2");
155
}
156
} finally {
157
i1.close();
158
i2.close();
159
}
160
}
161
}
162
163