diff --git a/src/java.base/share/classes/java/net/ProxySelector.java b/src/java.base/share/classes/java/net/ProxySelector.java index f29c6738c52d4..40e11ef54e533 100644 --- a/src/java.base/share/classes/java/net/ProxySelector.java +++ b/src/java.base/share/classes/java/net/ProxySelector.java @@ -27,7 +27,6 @@ import java.io.IOException; import java.util.List; -import java.util.Locale; import sun.security.util.SecurityConstants; @@ -178,7 +177,8 @@ public static void setDefault(ProxySelector ps) { /** * Returns a ProxySelector which uses the given proxy address for all HTTP - * and HTTPS requests. If proxy is {@code null} then proxying is disabled. + * and HTTPS requests. If {@code proxyAddress} is {@code null} + * then proxying is disabled. * * @param proxyAddress * The address of the proxy @@ -207,13 +207,22 @@ static class StaticProxySelector extends ProxySelector { @Override public void connectFailed(URI uri, SocketAddress sa, IOException e) { + if (uri == null || sa == null || e == null) { + throw new IllegalArgumentException("Arguments can't be null."); + } /* ignore */ } @Override - public synchronized List select(URI uri) { - String scheme = uri.getScheme().toLowerCase(Locale.ROOT); - if (scheme.equals("http") || scheme.equals("https")) { + public List select(URI uri) { + if (uri == null) { + throw new IllegalArgumentException("URI can't be null"); + } + String scheme = uri.getScheme(); + if (scheme == null) { + throw new IllegalArgumentException("protocol can't be null"); + } + if (scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https")) { return list; } else { return NO_PROXY_LIST; diff --git a/test/jdk/java/net/ProxySelector/NullArguments.java b/test/jdk/java/net/ProxySelector/NullArguments.java index dddaa15d7e16e..6f0ce5361565a 100644 --- a/test/jdk/java/net/ProxySelector/NullArguments.java +++ b/test/jdk/java/net/ProxySelector/NullArguments.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -22,62 +22,46 @@ */ /* @test - * @bug 4937962 + * @bug 4937962 8318150 * @summary ProxySelector.connectFailed and .select never throw IllegalArgumentException + * @run junit NullArguments */ import java.net.*; -import java.util.List; import java.io.IOException; +import java.util.stream.Stream; + +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import static org.junit.jupiter.api.Assertions.*; public class NullArguments { - public static void main(String[] args) { - ProxySelector ps = ProxySelector.getDefault(); - List p = null; - boolean ok = false; - if (ps != null) { - try { - p = ps.select(null); - } catch (IllegalArgumentException iae) { - System.out.println("OK"); - ok = true; - } - if (!ok) - throw new RuntimeException("Expected IllegalArgumentException!"); - URI uri = null; - try { - uri = new URI("http://java.sun.com"); - } catch (java.net.URISyntaxException use) { - // can't happen - } - SocketAddress sa = new InetSocketAddress("localhost", 80); - IOException ioe = new IOException("dummy IOE"); - ok = false; - try { - ps.connectFailed(uri, sa, null); - } catch (IllegalArgumentException iae) { - System.out.println("OK"); - ok = true; - } - if (!ok) - throw new RuntimeException("Expected IllegalArgumentException!"); - ok = false; - try { - ps.connectFailed(uri, null, ioe); - } catch (IllegalArgumentException iae) { - System.out.println("OK"); - ok = true; - } - if (!ok) - throw new RuntimeException("Expected IllegalArgumentException!"); - ok = false; - try { - ps.connectFailed(null, sa, ioe); - } catch (IllegalArgumentException iae) { - System.out.println("OK"); - ok = true; - } - if (!ok) - throw new RuntimeException("Expected IllegalArgumentException!"); - } + + public static Stream testProxies() { + return Stream.of( + ProxySelector.getDefault(), + ProxySelector.of(new InetSocketAddress(1234))); + } + + @ParameterizedTest + @MethodSource("testProxies") + void testNullArguments(ProxySelector ps) throws URISyntaxException { + Assumptions.assumeTrue(ps != null, "Skipping null selector"); + assertThrows(IllegalArgumentException.class, + () -> ps.select(null), + "Expected IllegalArgumentException!"); + URI uri = new URI("http://java.sun.com"); + SocketAddress sa = new InetSocketAddress("localhost", 80); + IOException ioe = new IOException("dummy IOE"); + assertThrows(IllegalArgumentException.class, + () -> ps.connectFailed(uri, sa, null), + "Expected IllegalArgumentException!"); + assertThrows(IllegalArgumentException.class, + () -> ps.connectFailed(uri, null, ioe), + "Expected IllegalArgumentException!"); + assertThrows(IllegalArgumentException.class, + () -> ps.connectFailed(null, sa, ioe), + "Expected IllegalArgumentException!"); } }