Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/http/ChunkedInputStream/ChunkedEncodingTest.java
41154 views
1
/*
2
* Copyright (c) 2004, 2019, 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 4333920
27
* @modules jdk.httpserver
28
* @library /test/lib
29
* @run main ChunkedEncodingTest
30
* @summary ChunkedEncodingTest unit test
31
*/
32
33
import java.io.*;
34
import java.net.*;
35
import java.security.*;
36
import com.sun.net.httpserver.HttpServer;
37
import com.sun.net.httpserver.HttpHandler;
38
import com.sun.net.httpserver.HttpExchange;
39
import static java.lang.System.out;
40
import jdk.test.lib.net.URIBuilder;
41
42
public class ChunkedEncodingTest{
43
private static MessageDigest serverDigest, clientDigest;
44
private static volatile byte[] serverMac, clientMac;
45
46
static void client(String u) throws Exception {
47
URL url = new URL(u);
48
out.println("client opening connection to: " + u);
49
URLConnection urlc = url.openConnection();
50
DigestInputStream dis =
51
new DigestInputStream(urlc.getInputStream(), clientDigest);
52
while (dis.read() != -1);
53
clientMac = dis.getMessageDigest().digest();
54
dis.close();
55
}
56
57
public static void test() {
58
HttpServer server = null;
59
try {
60
serverDigest = MessageDigest.getInstance("MD5");
61
clientDigest = MessageDigest.getInstance("MD5");
62
server = startHttpServer();
63
64
int port = server.getAddress().getPort();
65
out.println ("Server listening on port: " + port);
66
String url = URIBuilder.newBuilder()
67
.scheme("http")
68
.host(server.getAddress().getAddress())
69
.port(port)
70
.path("/chunked/")
71
.build().toString();
72
client(url);
73
74
if (!MessageDigest.isEqual(clientMac, serverMac)) {
75
throw new RuntimeException(
76
"Data received is NOT equal to the data sent");
77
}
78
} catch (Exception e) {
79
throw new RuntimeException(e);
80
} finally {
81
if (server != null)
82
server.stop(0);
83
}
84
}
85
86
public static void main(String[] args) {
87
test();
88
}
89
90
/**
91
* Http Server
92
*/
93
static HttpServer startHttpServer() throws IOException {
94
InetAddress loopback = InetAddress.getLoopbackAddress();
95
HttpServer httpServer = HttpServer.create(new InetSocketAddress(loopback, 0), 0);
96
HttpHandler httpHandler = new SimpleHandler();
97
httpServer.createContext("/chunked/", httpHandler);
98
httpServer.start();
99
return httpServer;
100
}
101
102
static class SimpleHandler implements HttpHandler {
103
static byte[] baMessage;
104
final static int CHUNK_SIZE = 8 * 1024;
105
final static int MESSAGE_LENGTH = 52 * CHUNK_SIZE;
106
107
static {
108
baMessage = new byte[MESSAGE_LENGTH];
109
for (int i=0; i<MESSAGE_LENGTH; i++)
110
baMessage[i] = (byte)i;
111
}
112
113
@Override
114
public void handle(HttpExchange t) throws IOException {
115
InputStream is = t.getRequestBody();
116
while (is.read() != -1);
117
is.close();
118
119
t.sendResponseHeaders (200, 0);
120
OutputStream os = t.getResponseBody();
121
DigestOutputStream dos = new DigestOutputStream(os, serverDigest);
122
123
int offset = 0;
124
for (int i=0; i<52; i++) {
125
dos.write(baMessage, offset, CHUNK_SIZE);
126
offset += CHUNK_SIZE;
127
}
128
serverMac = serverDigest.digest();
129
os.close();
130
t.close();
131
}
132
}
133
}
134
135