Skip to content

Commit 1043c48

Browse files
committed
8365086: CookieStore.getURIs() and get(URI) should return an immutable List
Backport-of: 022e29a77533aacabd56820d00ecffa9646a8362
1 parent 2a0b143 commit 1043c48

File tree

2 files changed

+123
-8
lines changed

2 files changed

+123
-8
lines changed

src/java.base/share/classes/java/net/InMemoryCookieStore.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,10 +25,6 @@
2525

2626
package java.net;
2727

28-
import java.net.URI;
29-
import java.net.CookieStore;
30-
import java.net.HttpCookie;
31-
import java.net.URISyntaxException;
3228
import java.util.List;
3329
import java.util.Map;
3430
import java.util.ArrayList;
@@ -72,6 +68,7 @@ public InMemoryCookieStore() {
7268
/**
7369
* Add one cookie into cookie store.
7470
*/
71+
@Override
7572
public void add(URI uri, HttpCookie cookie) {
7673
// pre-condition : argument can't be null
7774
if (cookie == null) {
@@ -109,6 +106,7 @@ public void add(URI uri, HttpCookie cookie) {
109106
* 3) not expired.
110107
* See RFC 2965 sec. 3.3.4 for more detail.
111108
*/
109+
@Override
112110
public List<HttpCookie> get(URI uri) {
113111
// argument can't be null
114112
if (uri == null) {
@@ -127,12 +125,13 @@ public List<HttpCookie> get(URI uri) {
127125
lock.unlock();
128126
}
129127

130-
return cookies;
128+
return Collections.unmodifiableList(cookies);
131129
}
132130

133131
/**
134132
* Get all cookies in cookie store, except those have expired
135133
*/
134+
@Override
136135
public List<HttpCookie> getCookies() {
137136
List<HttpCookie> rt;
138137

@@ -156,6 +155,7 @@ public List<HttpCookie> getCookies() {
156155
* Get all URIs, which are associated with at least one cookie
157156
* of this cookie store.
158157
*/
158+
@Override
159159
public List<URI> getURIs() {
160160
List<URI> uris = new ArrayList<>();
161161

@@ -165,7 +165,7 @@ public List<URI> getURIs() {
165165
while (it.hasNext()) {
166166
URI uri = it.next();
167167
List<HttpCookie> cookies = uriIndex.get(uri);
168-
if (cookies == null || cookies.size() == 0) {
168+
if (cookies == null || cookies.isEmpty()) {
169169
// no cookies list or an empty list associated with
170170
// this uri entry, delete it
171171
it.remove();
@@ -176,13 +176,14 @@ public List<URI> getURIs() {
176176
lock.unlock();
177177
}
178178

179-
return uris;
179+
return Collections.unmodifiableList(uris);
180180
}
181181

182182

183183
/**
184184
* Remove a cookie from store
185185
*/
186+
@Override
186187
public boolean remove(URI uri, HttpCookie ck) {
187188
// argument can't be null
188189
if (ck == null) {
@@ -204,6 +205,7 @@ public boolean remove(URI uri, HttpCookie ck) {
204205
/**
205206
* Remove all cookies in this cookie store.
206207
*/
208+
@Override
207209
public boolean removeAll() {
208210
lock.lock();
209211
try {
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright (c) 2025, 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+
import java.net.CookieManager;
25+
import java.net.CookieStore;
26+
import java.net.HttpCookie;
27+
import java.net.URI;
28+
import java.util.ArrayList;
29+
import java.util.List;
30+
31+
import org.junit.jupiter.params.ParameterizedTest;
32+
import org.junit.jupiter.params.provider.Arguments;
33+
import org.junit.jupiter.params.provider.MethodSource;
34+
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
import static org.junit.jupiter.api.Assertions.assertNotNull;
36+
import static org.junit.jupiter.api.Assertions.assertThrows;
37+
38+
/*
39+
* @test
40+
* @bug 8365086
41+
* @summary verify that the implementation of java.net.CookieStore works
42+
* as expected
43+
* @run junit CookieStoreTest
44+
*/
45+
class CookieStoreTest {
46+
47+
// neither the scheme, host nor the port matters in this test
48+
private static final URI COOKIE_TEST_URI = URI.create("https://127.0.0.1:12345");
49+
50+
static List<Arguments> cookieStores() {
51+
final List<Arguments> params = new ArrayList<>();
52+
// empty CookieStore
53+
params.add(Arguments.of(new CookieManager().getCookieStore(), true));
54+
55+
final CookieStore cookieStore = new CookieManager().getCookieStore();
56+
cookieStore.add(COOKIE_TEST_URI, new HttpCookie("foo", "bar"));
57+
// non-empty CookieStore
58+
params.add(Arguments.of(cookieStore, false));
59+
60+
return params;
61+
}
62+
63+
/*
64+
* Verify that the List returned by CookieStore.getURIs() is immutable.
65+
*/
66+
@ParameterizedTest
67+
@MethodSource("cookieStores")
68+
void testImmutableGetURIs(final CookieStore cookieStore, final boolean expectEmpty) {
69+
final List<URI> uris = cookieStore.getURIs();
70+
assertNotNull(uris, "CookieStore.getURIs() returned null");
71+
assertEquals(expectEmpty, uris.isEmpty(), "CookieStore.getURIs() returned: " + uris);
72+
assertImmutableList(uris, COOKIE_TEST_URI);
73+
}
74+
75+
/*
76+
* Verify that the List returned by CookieStore.getCookies() is immutable.
77+
*/
78+
@ParameterizedTest
79+
@MethodSource("cookieStores")
80+
void testImmutableGetCookies(final CookieStore cookieStore, final boolean expectEmpty) {
81+
final List<HttpCookie> cookies = cookieStore.getCookies();
82+
assertNotNull(cookies, "CookieStore.getCookies() returned null");
83+
assertEquals(expectEmpty, cookies.isEmpty(), "CookieStore.getCookies() returned: " + cookies);
84+
assertImmutableList(cookies, new HttpCookie("hello", "world"));
85+
}
86+
87+
/*
88+
* Verify that the List returned by CookieStore.get(URI) is immutable.
89+
*/
90+
@ParameterizedTest
91+
@MethodSource("cookieStores")
92+
void testImmutableGetCookiesForURI(final CookieStore cookieStore, final boolean expectEmpty) {
93+
final List<HttpCookie> cookies = cookieStore.get(COOKIE_TEST_URI);
94+
assertNotNull(cookies, "CookieStore.get(URI) returned null");
95+
assertEquals(expectEmpty, cookies.isEmpty(), "CookieStore.get(URI) returned: " + cookies);
96+
assertImmutableList(cookies, new HttpCookie("hello", "world"));
97+
}
98+
99+
/*
100+
* Verifies that the attempt to modify the contents of the list will fail
101+
* due to the list being immutable.
102+
*/
103+
private static <T> void assertImmutableList(final List<T> list, T elementToAddOrRemove) {
104+
// the list is expected to be immutable, so each of these operations must fail
105+
assertThrows(UnsupportedOperationException.class, () -> list.add(elementToAddOrRemove));
106+
assertThrows(UnsupportedOperationException.class, () -> list.remove(elementToAddOrRemove));
107+
assertThrows(UnsupportedOperationException.class, list::clear);
108+
// even try the replace operation when the list isn't empty
109+
if (!list.isEmpty()) {
110+
assertThrows(UnsupportedOperationException.class, () -> list.set(0, elementToAddOrRemove));
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)