1 /*
2  * Copyright (c) 2001, 2020, 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 4485208 8252767
27  * @summary Validate various request property methods on java.net.URLConnection
28  * throw NullPointerException and IllegalStateException when expected
29  * @run testng RequestProperties
30  */
31 
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;
40 import java.util.ArrayList;
41 import java.util.List;
42 
43 public class RequestProperties {
44 
45     private static final Class NPE = NullPointerException.class;
46     private static final Class ISE = IllegalStateException.class;
47 
48     @DataProvider(name = "urls")
urls()49     private Object[][] urls() {
50         final List<String> urls = new ArrayList<>();
51         urls.add("http://foo.com/bar/");
52         urls.add("jar:http://foo.com/bar.html!/foo/bar");
53         urls.add("file:/etc/passwd");
54         if (hasFtp()) {
55             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")
testSetRequestPropertyNullPointerException(final String url)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")
testAddRequestPropertyNullPointerException(final String url)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")
testGetRequestPropertyReturnsNull(final String url)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
testSetRequestPropertyIllegalStateException()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     }
113 
114     /**
115      * Test that {@link java.net.URLConnection#addRequestProperty(String, String)} throws
116      * an {@link IllegalStateException} when already connected
117      */
118     @Test
testAddRequestPropertyIllegalStateException()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     }
127 
128     /**
129      * Test that {@link java.net.URLConnection#getRequestProperty(String)} throws
130      * an {@link IllegalStateException} when already connected
131      */
132     @Test
testGetRequestPropertyIllegalStateException()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         }
140     }
141 
142     /**
143      * Test that {@link URLConnection#getRequestProperties()} throws
144      * an {@link IllegalStateException} when already connected
145      */
146     @Test
testGetRequestPropertiesIllegalStateException()147     public void testGetRequestPropertiesIllegalStateException() throws Exception {
148         final URLConnection conn = createAndConnectURLConnection();
149         try {
150             Assert.assertThrows(ISE, () -> conn.getRequestProperties());
151         } finally {
152             safeClose(conn);
153         }
154     }
155 
createAndConnectURLConnection()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 
safeClose(final URLConnection conn)163     private static void safeClose(final URLConnection conn) {
164         try {
165             conn.getInputStream().close();
166         } catch (Exception e) {
167             // ignore
168         }
169     }
170 
hasFtp()171     private static boolean hasFtp() {
172         try {
173             new java.net.URL("ftp://");
174             return true;
175         } catch (java.net.MalformedURLException x) {
176             System.out.println("FTP not supported by this runtime.");
177             return false;
178         }
179     }
180 }
181