author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58108 | 5302477c8285 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55399
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
51724
diff
changeset
|
2 |
* Copyright (c) 2005, 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 |
/* |
|
25 |
* @test |
|
26 |
* @summary Unit test for java.net.CookieManager |
|
18148
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
27 |
* @bug 6244040 7150552 7051862 |
38780
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
28 |
* @modules jdk.httpserver |
58108
5302477c8285
8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents:
55399
diff
changeset
|
29 |
* java.logging |
5302477c8285
8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents:
55399
diff
changeset
|
30 |
* @run main/othervm -ea -esa CookieManagerTest |
2 | 31 |
* @author Edward Wang |
32 |
*/ |
|
33 |
||
17489 | 34 |
import com.sun.net.httpserver.*; |
51724
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
35 |
import java.util.Collections; |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
36 |
import java.util.LinkedList; |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
37 |
import java.util.List; |
17489 | 38 |
import java.io.IOException; |
18148
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
39 |
import java.net.*; |
58108
5302477c8285
8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents:
55399
diff
changeset
|
40 |
import java.util.logging.Level; |
5302477c8285
8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents:
55399
diff
changeset
|
41 |
import java.util.logging.Logger; |
38780
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
42 |
import static java.net.Proxy.NO_PROXY; |
2 | 43 |
|
44 |
public class CookieManagerTest { |
|
17489 | 45 |
|
46 |
static CookieTransactionHandler httpTrans; |
|
47 |
static HttpServer server; |
|
2 | 48 |
|
38780
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
49 |
static final String hostAddress = getAddr(); |
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
50 |
|
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
51 |
/** Returns an IP literal suitable for use by the test. */ |
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
52 |
static String getAddr() { |
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
53 |
try { |
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
54 |
InetAddress lh = InetAddress.getLocalHost(); |
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
55 |
System.out.println("Trying: " + lh); |
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
56 |
if (lh.isReachable(5_000)) { |
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
57 |
System.out.println("Using: " + lh); |
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
58 |
return lh.getHostAddress(); |
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
59 |
} |
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
60 |
} catch (IOException x) { |
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
61 |
System.out.println("Debug: caught:" + x); |
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
62 |
} |
55399
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
51724
diff
changeset
|
63 |
InetAddress loopback = InetAddress.getLoopbackAddress(); |
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
51724
diff
changeset
|
64 |
System.out.println("Using: \"" + loopback.getHostAddress() + "\""); |
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
51724
diff
changeset
|
65 |
return loopback.getHostAddress(); |
38780
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
66 |
} |
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
67 |
|
2 | 68 |
public static void main(String[] args) throws Exception { |
58108
5302477c8285
8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents:
55399
diff
changeset
|
69 |
// logs everything... |
5302477c8285
8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents:
55399
diff
changeset
|
70 |
Logger root = Logger.getLogger(""); |
5302477c8285
8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents:
55399
diff
changeset
|
71 |
root.setLevel(Level.ALL); |
5302477c8285
8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents:
55399
diff
changeset
|
72 |
root.getHandlers()[0].setLevel(Level.ALL); |
5302477c8285
8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents:
55399
diff
changeset
|
73 |
|
2 | 74 |
startHttpServer(); |
75 |
makeHttpCall(); |
|
76 |
||
77 |
if (httpTrans.badRequest) { |
|
78 |
throw new RuntimeException("Test failed : bad cookie header"); |
|
79 |
} |
|
18148
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
80 |
checkCookiePolicy(); |
2 | 81 |
} |
82 |
||
18148
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
83 |
public static void startHttpServer() throws IOException { |
17489 | 84 |
httpTrans = new CookieTransactionHandler(); |
55399
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
51724
diff
changeset
|
85 |
server = HttpServer.create(new InetSocketAddress(hostAddress, 0), 0); |
17489 | 86 |
server.createContext("/", httpTrans); |
87 |
server.start(); |
|
2 | 88 |
} |
89 |
||
18148
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
90 |
/* |
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
91 |
* Checks if CookiePolicy.ACCEPT_ORIGINAL_SERVER#shouldAccept() |
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
92 |
* returns false for null arguments |
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
93 |
*/ |
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
94 |
private static void checkCookiePolicy() throws Exception { |
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
95 |
CookiePolicy cp = CookiePolicy.ACCEPT_ORIGINAL_SERVER; |
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
96 |
boolean retVal; |
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
97 |
retVal = cp.shouldAccept(null, null); |
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
98 |
checkValue(retVal); |
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
99 |
retVal = cp.shouldAccept(null, new HttpCookie("CookieName", "CookieVal")); |
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
100 |
checkValue(retVal); |
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
101 |
retVal = cp.shouldAccept((new URL("http", "localhost", 2345, "/")).toURI(), |
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
102 |
null); |
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
103 |
checkValue(retVal); |
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
104 |
} |
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
105 |
|
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
106 |
private static void checkValue(boolean val) { |
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
107 |
if (val) |
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
108 |
throw new RuntimeException("Return value is not false!"); |
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
109 |
} |
d641a1b31205
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents:
17489
diff
changeset
|
110 |
|
17489 | 111 |
public static void makeHttpCall() throws IOException { |
2 | 112 |
try { |
38780
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
113 |
int port = server.getAddress().getPort(); |
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
114 |
System.out.println("http server listenining on: " + port); |
2 | 115 |
|
116 |
// install CookieManager to use |
|
117 |
CookieHandler.setDefault(new CookieManager()); |
|
118 |
||
17489 | 119 |
for (int i = 0; i < CookieTransactionHandler.testCount; i++) { |
120 |
System.out.println("====== CookieManager test " + (i+1) |
|
121 |
+ " ======"); |
|
122 |
((CookieManager)CookieHandler.getDefault()) |
|
123 |
.setCookiePolicy(CookieTransactionHandler.testPolicies[i]); |
|
124 |
((CookieManager)CookieHandler.getDefault()) |
|
125 |
.getCookieStore().removeAll(); |
|
126 |
URL url = new URL("http" , |
|
38780
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
127 |
hostAddress, |
17489 | 128 |
server.getAddress().getPort(), |
129 |
CookieTransactionHandler.testCases[i][0] |
|
130 |
.serverPath); |
|
38780
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
131 |
System.out.println("Requesting " + url); |
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
132 |
HttpURLConnection uc = (HttpURLConnection)url.openConnection(NO_PROXY); |
2 | 133 |
uc.getResponseCode(); |
134 |
uc.disconnect(); |
|
135 |
} |
|
136 |
} finally { |
|
17489 | 137 |
server.stop(0); |
2 | 138 |
} |
139 |
} |
|
140 |
} |
|
141 |
||
17489 | 142 |
class CookieTransactionHandler implements HttpHandler { |
143 |
||
144 |
private int testcaseDone = 0; |
|
145 |
private int testDone = 0; |
|
146 |
||
2 | 147 |
public static boolean badRequest = false; |
148 |
// the main test control logic will also loop exactly this number |
|
149 |
// to send http request |
|
150 |
public static final int testCount = 6; |
|
151 |
||
17489 | 152 |
@Override |
153 |
public void handle(HttpExchange exchange) throws IOException { |
|
154 |
if (testDone < testCases[testcaseDone].length) { |
|
155 |
// still have other tests to run, |
|
156 |
// check the Cookie header and then redirect it |
|
157 |
if (testDone > 0) checkRequest(exchange.getRequestHeaders()); |
|
158 |
exchange.getResponseHeaders().add("Location", |
|
159 |
testCases[testcaseDone][testDone].serverPath); |
|
160 |
exchange.getResponseHeaders() |
|
161 |
.add(testCases[testcaseDone][testDone].headerToken, |
|
162 |
testCases[testcaseDone][testDone].cookieToSend); |
|
163 |
exchange.sendResponseHeaders(302, -1); |
|
164 |
testDone++; |
|
165 |
} else { |
|
166 |
// the last test of this test case |
|
167 |
if (testDone > 0) checkRequest(exchange.getRequestHeaders()); |
|
168 |
testcaseDone++; |
|
169 |
testDone = 0; |
|
170 |
exchange.sendResponseHeaders(200, -1); |
|
171 |
} |
|
172 |
exchange.close(); |
|
173 |
} |
|
174 |
||
51724
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
175 |
private static String trim(String s) { |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
176 |
StringBuilder sb = new StringBuilder(); |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
177 |
for (int i=0; i<s.length(); i++) { |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
178 |
char c = s.charAt(i); |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
179 |
if (!Character.isWhitespace(c)) |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
180 |
sb.append(c); |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
181 |
} |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
182 |
return sb.toString(); |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
183 |
} |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
184 |
|
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
185 |
private static boolean cookieEquals(String s1, String s2) { |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
186 |
s1 = trim(s1); |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
187 |
s2 = trim(s2); |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
188 |
String[] s1a = s1.split(";"); |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
189 |
String[] s2a = s2.split(";"); |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
190 |
List<String> l1 = new LinkedList(List.of(s1a)); |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
191 |
List<String> l2 = new LinkedList(List.of(s2a)); |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
192 |
Collections.sort(l1); |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
193 |
Collections.sort(l2); |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
194 |
int i = 0; |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
195 |
for (String s : l1) { |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
196 |
if (!s.equals(l2.get(i++))) { |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
197 |
return false; |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
198 |
} |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
199 |
} |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
200 |
return true; |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
201 |
} |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
202 |
|
17489 | 203 |
private void checkRequest(Headers hdrs) { |
204 |
||
205 |
assert testDone > 0; |
|
206 |
String cookieHeader = hdrs.getFirst("Cookie"); |
|
51724
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
207 |
if (cookieHeader != null && cookieEquals( |
8abb0fa2c334
8210311: IllegalArgumentException in CookieManager - Comparison method violates its general contract
michaelm
parents:
47216
diff
changeset
|
208 |
cookieHeader, testCases[testcaseDone][testDone-1].cookieToRecv)) |
17489 | 209 |
{ |
210 |
System.out.printf("%15s %s\n", "PASSED:", cookieHeader); |
|
211 |
} else { |
|
212 |
System.out.printf("%15s %s\n", "FAILED:", cookieHeader); |
|
213 |
System.out.printf("%15s %s\n\n", "should be:", |
|
214 |
testCases[testcaseDone][testDone-1].cookieToRecv); |
|
215 |
badRequest = true; |
|
216 |
} |
|
217 |
} |
|
218 |
||
2 | 219 |
// test cases |
220 |
public static class CookieTestCase { |
|
221 |
public String headerToken; |
|
222 |
public String cookieToSend; |
|
223 |
public String cookieToRecv; |
|
224 |
public String serverPath; |
|
225 |
||
226 |
public CookieTestCase(String h, String cts, String ctr, String sp) { |
|
227 |
headerToken = h; |
|
228 |
cookieToSend = cts; |
|
229 |
cookieToRecv = ctr; |
|
230 |
serverPath = sp; |
|
231 |
} |
|
232 |
}; |
|
233 |
||
17489 | 234 |
/* |
235 |
* these two must match each other, |
|
236 |
* i.e. testCases.length == testPolicies.length |
|
237 |
*/ |
|
2 | 238 |
|
17489 | 239 |
// the test cases to run; each test case may contain multiple roundtrips |
240 |
public static CookieTestCase[][] testCases = null; |
|
241 |
// indicates what CookiePolicy to use with each test cases |
|
242 |
public static CookiePolicy[] testPolicies = null; |
|
243 |
||
244 |
CookieTransactionHandler() { |
|
2 | 245 |
testCases = new CookieTestCase[testCount][]; |
246 |
testPolicies = new CookiePolicy[testCount]; |
|
247 |
||
38780
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
248 |
String localHostAddr = CookieManagerTest.hostAddress; |
fa8bf2d62d36
8158525: Update a few java/net tests to use the loopback address instead of the host address
chegar
parents:
23010
diff
changeset
|
249 |
|
2 | 250 |
int count = 0; |
251 |
||
252 |
// an http session with Netscape cookies exchanged |
|
253 |
testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER; |
|
254 |
testCases[count++] = new CookieTestCase[]{ |
|
255 |
new CookieTestCase("Set-Cookie", |
|
17489 | 256 |
"CUSTOMER=WILE:BOB; " + |
257 |
"path=/; expires=Sat, 09-Nov-2030 23:12:40 GMT;" + "domain=." + |
|
258 |
localHostAddr, |
|
2 | 259 |
"CUSTOMER=WILE:BOB", |
260 |
"/" |
|
261 |
), |
|
262 |
new CookieTestCase("Set-Cookie", |
|
263 |
"PART_NUMBER=ROCKET_LAUNCHER_0001; path=/;" + "domain=." + localHostAddr, |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
264 |
"CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001", |
2 | 265 |
"/" |
266 |
), |
|
267 |
new CookieTestCase("Set-Cookie", |
|
268 |
"SHIPPING=FEDEX; path=/foo;" + "domain=." + localHostAddr, |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
269 |
"CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001", |
2 | 270 |
"/" |
271 |
), |
|
272 |
new CookieTestCase("Set-Cookie", |
|
273 |
"SHIPPING=FEDEX; path=/foo;" + "domain=." + localHostAddr, |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
274 |
"CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX", |
2 | 275 |
"/foo" |
276 |
) |
|
277 |
}; |
|
278 |
||
279 |
// check whether or not path rule is applied |
|
280 |
testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER; |
|
281 |
testCases[count++] = new CookieTestCase[]{ |
|
282 |
new CookieTestCase("Set-Cookie", |
|
283 |
"PART_NUMBER=ROCKET_LAUNCHER_0001; path=/;" + "domain=." + localHostAddr, |
|
284 |
"PART_NUMBER=ROCKET_LAUNCHER_0001", |
|
285 |
"/" |
|
286 |
), |
|
287 |
new CookieTestCase("Set-Cookie", |
|
288 |
"PART_NUMBER=RIDING_ROCKET_0023; path=/ammo;" + "domain=." + localHostAddr, |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
289 |
"PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001", |
2 | 290 |
"/ammo" |
291 |
) |
|
292 |
}; |
|
293 |
||
294 |
// an http session with rfc2965 cookies exchanged |
|
295 |
testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER; |
|
296 |
testCases[count++] = new CookieTestCase[]{ |
|
297 |
new CookieTestCase("Set-Cookie2", |
|
298 |
"Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr, |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
299 |
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"", |
2 | 300 |
"/acme/login" |
301 |
), |
|
302 |
new CookieTestCase("Set-Cookie2", |
|
303 |
"Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\";" + "domain=." + localHostAddr, |
|
17489 | 304 |
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";" + "$Domain=\"." + |
305 |
localHostAddr + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" |
|
306 |
+ "$Domain=\"." + localHostAddr + "\"", |
|
2 | 307 |
"/acme/pickitem" |
308 |
), |
|
309 |
new CookieTestCase("Set-Cookie2", |
|
310 |
"Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr, |
|
17489 | 311 |
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + |
312 |
"\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"." |
|
313 |
+ localHostAddr + "\"" + "; Shipping=\"FedEx\";$Path=\"/acme\";" + |
|
314 |
"$Domain=\"." + localHostAddr + "\"", |
|
2 | 315 |
"/acme/shipping" |
316 |
) |
|
317 |
}; |
|
318 |
||
319 |
// check whether or not the path rule is applied |
|
320 |
testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER; |
|
321 |
testCases[count++] = new CookieTestCase[]{ |
|
322 |
new CookieTestCase("Set-Cookie2", |
|
323 |
"Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr, |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
324 |
"$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"", |
2 | 325 |
"/acme/ammo" |
326 |
), |
|
327 |
new CookieTestCase("Set-Cookie2", |
|
17489 | 328 |
"Part_Number=\"Riding_Rocket_0023\"; Version=\"1\"; Path=\"/acme/ammo\";" + "domain=." |
329 |
+ localHostAddr, |
|
330 |
"$Version=\"1\"; Part_Number=\"Riding_Rocket_0023\";$Path=\"/acme/ammo\";$Domain=\"." |
|
331 |
+ localHostAddr + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" |
|
332 |
+ "$Domain=\"." + localHostAddr + "\"", |
|
2 | 333 |
"/acme/ammo" |
334 |
), |
|
335 |
new CookieTestCase("", |
|
336 |
"", |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
337 |
"$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"", |
2 | 338 |
"/acme/parts" |
339 |
) |
|
340 |
}; |
|
341 |
||
342 |
// new cookie should overwrite old cookie |
|
343 |
testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER; |
|
344 |
testCases[count++] = new CookieTestCase[]{ |
|
345 |
new CookieTestCase("Set-Cookie2", |
|
346 |
"Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr, |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
347 |
"$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"", |
2 | 348 |
"/acme" |
349 |
), |
|
350 |
new CookieTestCase("Set-Cookie2", |
|
351 |
"Part_Number=\"Rocket_Launcher_2000\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr, |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
352 |
"$Version=\"1\"; Part_Number=\"Rocket_Launcher_2000\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"", |
2 | 353 |
"/acme" |
354 |
) |
|
355 |
}; |
|
356 |
||
357 |
// cookies without domain attributes |
|
2924
a67f12d92bdd
6349566: java.net.CookieManager doesn't set default domain
jccollet
parents:
715
diff
changeset
|
358 |
// RFC 2965 states that domain should default to host |
2 | 359 |
testPolicies[count] = CookiePolicy.ACCEPT_ALL; |
360 |
testCases[count++] = new CookieTestCase[]{ |
|
361 |
new CookieTestCase("Set-Cookie2", |
|
362 |
"Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\"", |
|
2924
a67f12d92bdd
6349566: java.net.CookieManager doesn't set default domain
jccollet
parents:
715
diff
changeset
|
363 |
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"", |
2 | 364 |
"/acme/login" |
365 |
), |
|
366 |
new CookieTestCase("Set-Cookie2", |
|
367 |
"Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\"", |
|
17489 | 368 |
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" + |
369 |
"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"", |
|
2 | 370 |
"/acme/pickitem" |
371 |
), |
|
372 |
new CookieTestCase("Set-Cookie2", |
|
373 |
"Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\"", |
|
17489 | 374 |
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" + |
375 |
"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" + |
|
376 |
"; Shipping=\"FedEx\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"", |
|
2 | 377 |
"/acme/shipping" |
378 |
) |
|
379 |
}; |
|
380 |
||
381 |
assert count == testCount; |
|
382 |
} |
|
383 |
} |