From d9ce525a1c27f41ef66c39b3ec18e3a87bbd8dd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jeli=C5=84ski?= Date: Fri, 20 Oct 2023 06:18:18 +0000 Subject: [PATCH] 8318150: StaticProxySelector.select should not throw NullPointerExceptions Reviewed-by: jpai, dfuchs --- .../share/classes/java/net/ProxySelector.java | 19 ++-- .../java/net/ProxySelector/NullArguments.java | 88 ++++++++----------- 2 files changed, 50 insertions(+), 57 deletions(-) diff --git a/src/java.base/share/classes/java/net/ProxySelector.java b/src/java.base/share/classes/java/net/ProxySelector.java index f29c6738c52..40e11ef54e5 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 @@ package java.net; import java.io.IOException; import java.util.List; -import java.util.Locale; import sun.security.util.SecurityConstants; @@ -178,7 +177,8 @@ public abstract class ProxySelector { /** * 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 @@ public abstract class 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 dddaa15d7e1..6f0ce536156 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!"); } }