Skip to content

Commit 5dd1ead

Browse files
jaikirandfuch
authored andcommitted
8252767: URLConnection.setRequestProperty throws IllegalAccessError
Reviewed-by: chegar, michaelm, alanb
1 parent 2cceeed commit 5dd1ead

File tree

2 files changed

+129
-31
lines changed

2 files changed

+129
-31
lines changed

src/java.base/share/classes/sun/net/www/URLConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void setProperties(MessageHeader properties) {
7171

7272
public void setRequestProperty(String key, String value) {
7373
if(connected)
74-
throw new IllegalAccessError("Already connected");
74+
throw new IllegalStateException("Already connected");
7575
if (key == null)
7676
throw new NullPointerException ("key cannot be null");
7777
properties.set(key, value);

test/jdk/java/net/URLConnection/RequestProperties.java

Lines changed: 128 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 2020, 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
@@ -23,57 +23,155 @@
2323

2424
/**
2525
* @test
26-
* @bug 4485208
27-
* @summary file: and ftp: URL handlers need to throw NPE in setRequestProperty
26+
* @bug 4485208 8252767
27+
* @summary Validate various request property methods on java.net.URLConnection
28+
* throw NullPointerException and IllegalStateException when expected
29+
* @run testng RequestProperties
2830
*/
2931

30-
import java.net.*;
32+
import org.testng.Assert;
33+
import org.testng.annotations.DataProvider;
34+
import org.testng.annotations.Test;
35+
36+
import java.io.IOException;
37+
import java.net.URL;
38+
import java.net.URLConnection;
39+
import java.nio.file.Path;
3140
import java.util.ArrayList;
3241
import java.util.List;
3342

3443
public class RequestProperties {
35-
static int failed;
3644

37-
public static void main (String args[]) throws Exception {
38-
List<String> urls = new ArrayList<>();
45+
private static final Class NPE = NullPointerException.class;
46+
private static final Class ISE = IllegalStateException.class;
47+
48+
@DataProvider(name = "urls")
49+
private Object[][] urls() {
50+
final List<String> urls = new ArrayList<>();
3951
urls.add("http://foo.com/bar/");
4052
urls.add("jar:http://foo.com/bar.html!/foo/bar");
4153
urls.add("file:/etc/passwd");
42-
if (hasFtp())
54+
if (hasFtp()) {
4355
urls.add("ftp://foo:bar@foobar.com/etc/passwd");
56+
}
57+
final Object[][] data = new Object[urls.size()][1];
58+
for (int i = 0; i < urls.size(); i++) {
59+
data[i][0] = urls.get(i);
60+
}
61+
return data;
62+
}
63+
64+
65+
/**
66+
* Test that {@link java.net.URLConnection#setRequestProperty(String, String)} throws
67+
* a {@link NullPointerException} when passed null key
68+
*/
69+
@Test(dataProvider = "urls")
70+
public void testSetRequestPropertyNullPointerException(final String url) throws Exception {
71+
final URLConnection conn = new URL(url).openConnection();
72+
Assert.assertThrows(NPE, () -> conn.setRequestProperty(null, "bar"));
73+
// expected to pass
74+
conn.setRequestProperty("key", null);
75+
}
76+
77+
/**
78+
* Test that {@link java.net.URLConnection#addRequestProperty(String, String)} throws
79+
* a {@link NullPointerException} when passed null key
80+
*/
81+
@Test(dataProvider = "urls")
82+
public void testAddRequestPropertyNullPointerException(final String url) throws Exception {
83+
final URLConnection conn = new URL(url).openConnection();
84+
Assert.assertThrows(NPE, () -> conn.addRequestProperty(null, "hello"));
85+
// expected to pass
86+
conn.addRequestProperty("key", null);
87+
}
88+
89+
/**
90+
* Test that {@link java.net.URLConnection#getRequestProperty(String)} returns
91+
* null when the passed key is null
92+
*/
93+
@Test(dataProvider = "urls")
94+
public void testGetRequestPropertyReturnsNull(final String url) throws Exception {
95+
final URLConnection conn = new URL(url).openConnection();
96+
Assert.assertNull(conn.getRequestProperty(null),
97+
"getRequestProperty was expected to return null for null key");
98+
}
99+
100+
/**
101+
* Test that {@link java.net.URLConnection#setRequestProperty(String, String)} throws
102+
* an {@link IllegalStateException} when already connected
103+
*/
104+
@Test
105+
public void testSetRequestPropertyIllegalStateException() throws Exception {
106+
final URLConnection conn = createAndConnectURLConnection();
107+
try {
108+
Assert.assertThrows(ISE, () -> conn.setRequestProperty("foo", "bar"));
109+
} finally {
110+
safeClose(conn);
111+
}
112+
}
44113

45-
for (String urlStr : urls)
46-
test(new URL(urlStr));
114+
/**
115+
* Test that {@link java.net.URLConnection#addRequestProperty(String, String)} throws
116+
* an {@link IllegalStateException} when already connected
117+
*/
118+
@Test
119+
public void testAddRequestPropertyIllegalStateException() throws Exception {
120+
final URLConnection conn = createAndConnectURLConnection();
121+
try {
122+
Assert.assertThrows(ISE, () -> conn.addRequestProperty("foo", "bar"));
123+
} finally {
124+
safeClose(conn);
125+
}
126+
}
47127

48-
if (failed != 0)
49-
throw new RuntimeException(failed + " errors") ;
128+
/**
129+
* Test that {@link java.net.URLConnection#getRequestProperty(String)} throws
130+
* an {@link IllegalStateException} when already connected
131+
*/
132+
@Test
133+
public void testGetRequestPropertyIllegalStateException() throws Exception {
134+
final URLConnection conn = createAndConnectURLConnection();
135+
try {
136+
Assert.assertThrows(ISE, () -> conn.getRequestProperty("hello"));
137+
} finally {
138+
safeClose(conn);
139+
}
50140
}
51141

52-
static void test(URL url) throws Exception {
53-
URLConnection urlc = url.openConnection();
142+
/**
143+
* Test that {@link URLConnection#getRequestProperties()} throws
144+
* an {@link IllegalStateException} when already connected
145+
*/
146+
@Test
147+
public void testGetRequestPropertiesIllegalStateException() throws Exception {
148+
final URLConnection conn = createAndConnectURLConnection();
54149
try {
55-
urlc.setRequestProperty(null, null);
56-
System.out.println(url.getProtocol()
57-
+ ": setRequestProperty(null,) did not throw NPE");
58-
failed++;
59-
} catch (NullPointerException e) { /* Expected */ }
150+
Assert.assertThrows(ISE, () -> conn.getRequestProperties());
151+
} finally {
152+
safeClose(conn);
153+
}
154+
}
155+
156+
private static URLConnection createAndConnectURLConnection() throws IOException {
157+
final URL url = Path.of(System.getProperty("java.io.tmpdir")).toUri().toURL();
158+
final URLConnection conn = url.openConnection();
159+
conn.connect();
160+
return conn;
161+
}
162+
163+
private static void safeClose(final URLConnection conn) {
60164
try {
61-
urlc.addRequestProperty(null, null);
62-
System.out.println(url.getProtocol()
63-
+ ": addRequestProperty(null,) did not throw NPE");
64-
failed++;
65-
} catch (NullPointerException e) { /* Expected */ }
66-
67-
if (urlc.getRequestProperty(null) != null) {
68-
System.out.println(url.getProtocol()
69-
+ ": getRequestProperty(null,) did not return null");
70-
failed++;
165+
conn.getInputStream().close();
166+
} catch (Exception e) {
167+
// ignore
71168
}
72169
}
73170

74171
private static boolean hasFtp() {
75172
try {
76-
return new java.net.URL("ftp://") != null;
173+
new java.net.URL("ftp://");
174+
return true;
77175
} catch (java.net.MalformedURLException x) {
78176
System.out.println("FTP not supported by this runtime.");
79177
return false;

0 commit comments

Comments
 (0)