author | aefimov |
Wed, 10 Jul 2019 16:09:20 +0100 | |
changeset 55645 | 3203e857fa71 |
parent 47216 | 71c04702a3d5 |
child 57760 | ec948d19180a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55645
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
25 |
* @bug 4397096 |
|
55645
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
26 |
* @library /test/lib |
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
27 |
* @run main/othervm SetIfModifiedSince |
2 | 28 |
* @summary setIfModifiedSince() of HttpURLConnection sets invalid date of default locale |
29 |
*/ |
|
30 |
||
31 |
import java.net.*; |
|
32 |
import java.io.*; |
|
33 |
import java.util.*; |
|
34 |
||
55645
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
35 |
import jdk.test.lib.net.URIBuilder; |
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
36 |
|
2 | 37 |
public class SetIfModifiedSince { |
55645
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
38 |
static volatile boolean successfulHeaderCheck = false; |
2 | 39 |
|
40 |
static class XServer extends Thread { |
|
41 |
ServerSocket srv; |
|
42 |
Socket s; |
|
43 |
InputStream is; |
|
44 |
OutputStream os; |
|
45 |
||
46 |
XServer (ServerSocket s) { |
|
47 |
srv = s; |
|
48 |
} |
|
49 |
||
50 |
Socket getSocket () { |
|
51 |
return (s); |
|
52 |
} |
|
53 |
||
54 |
public void run() { |
|
55 |
try { |
|
56 |
String x; |
|
57 |
s = srv.accept (); |
|
58 |
is = s.getInputStream (); |
|
59 |
BufferedReader r = new BufferedReader(new InputStreamReader(is)); |
|
60 |
os = s.getOutputStream (); |
|
61 |
while ((x=r.readLine()) != null) { |
|
62 |
String header = "If-Modified-Since: "; |
|
63 |
if (x.startsWith(header)) { |
|
64 |
if (x.charAt(header.length()) == '?') { |
|
65 |
s.close (); |
|
66 |
srv.close (); // or else the HTTPURLConnection will retry |
|
67 |
throw new RuntimeException |
|
68 |
("Invalid HTTP date specification"); |
|
69 |
} |
|
70 |
break; |
|
71 |
} |
|
72 |
} |
|
55645
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
73 |
successfulHeaderCheck = true; |
2 | 74 |
s.close (); |
75 |
srv.close (); // or else the HTTPURLConnection will retry |
|
76 |
} catch (IOException e) {} |
|
77 |
} |
|
78 |
} |
|
79 |
||
55645
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
80 |
public static void main(String[] args) throws Exception { |
10138 | 81 |
Locale reservedLocale = Locale.getDefault(); |
2 | 82 |
try { |
83 |
Locale.setDefault(Locale.JAPAN); |
|
55645
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
84 |
ServerSocket serversocket = new ServerSocket(); |
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
85 |
serversocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); |
2 | 86 |
int port = serversocket.getLocalPort (); |
87 |
XServer server = new XServer (serversocket); |
|
88 |
server.start (); |
|
89 |
Thread.sleep (2000); |
|
55645
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
90 |
URL url = URIBuilder.newBuilder() |
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
91 |
.scheme("http") |
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
92 |
.loopback() |
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
93 |
.port(port) |
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
94 |
.path("/index.html") |
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
95 |
.toURLUnchecked(); |
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
96 |
URLConnection urlc = url.openConnection(Proxy.NO_PROXY); |
2 | 97 |
urlc.setIfModifiedSince (10000000); |
98 |
InputStream is = urlc.getInputStream (); |
|
55645
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
99 |
int i = 0, c; |
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
100 |
Thread.sleep(5000); |
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
101 |
if (!successfulHeaderCheck) { |
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
102 |
throw new RuntimeException("Header check was unsuccessful"); |
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
103 |
} |
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
104 |
} catch (SocketException ce) { |
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
105 |
if (!successfulHeaderCheck) { |
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
106 |
throw ce; |
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
107 |
} |
3203e857fa71
8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents:
47216
diff
changeset
|
108 |
System.out.println("ConnectionException expected on successful check of If-modified-since header"); |
10138 | 109 |
} finally { |
110 |
// restore the reserved locale |
|
111 |
Locale.setDefault(reservedLocale); |
|
2 | 112 |
} |
10138 | 113 |
|
2 | 114 |
} |
115 |
} |