8318150: StaticProxySelector.select should not throw NullPointerExceptions

Reviewed-by: jpai, dfuchs
This commit is contained in:
Daniel Jeliński 2023-10-20 06:18:18 +00:00
parent c46a54e018
commit d9ce525a1c
2 changed files with 50 additions and 57 deletions

View File

@ -27,7 +27,6 @@ package java.net;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Locale;
import sun.security.util.SecurityConstants; 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 * 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 * @param proxyAddress
* The address of the proxy * The address of the proxy
@ -207,13 +207,22 @@ public abstract class ProxySelector {
@Override @Override
public void connectFailed(URI uri, SocketAddress sa, IOException e) { 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 */ /* ignore */
} }
@Override @Override
public synchronized List<Proxy> select(URI uri) { public List<Proxy> select(URI uri) {
String scheme = uri.getScheme().toLowerCase(Locale.ROOT); if (uri == null) {
if (scheme.equals("http") || scheme.equals("https")) { 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; return list;
} else { } else {
return NO_PROXY_LIST; return NO_PROXY_LIST;

View File

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -22,62 +22,46 @@
*/ */
/* @test /* @test
* @bug 4937962 * @bug 4937962 8318150
* @summary ProxySelector.connectFailed and .select never throw IllegalArgumentException * @summary ProxySelector.connectFailed and .select never throw IllegalArgumentException
* @run junit NullArguments
*/ */
import java.net.*; import java.net.*;
import java.util.List;
import java.io.IOException; 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 class NullArguments {
public static void main(String[] args) {
ProxySelector ps = ProxySelector.getDefault(); public static Stream<ProxySelector> testProxies() {
List p = null; return Stream.of(
boolean ok = false; ProxySelector.getDefault(),
if (ps != null) { ProxySelector.of(new InetSocketAddress(1234)));
try { }
p = ps.select(null);
} catch (IllegalArgumentException iae) { @ParameterizedTest
System.out.println("OK"); @MethodSource("testProxies")
ok = true; void testNullArguments(ProxySelector ps) throws URISyntaxException {
} Assumptions.assumeTrue(ps != null, "Skipping null selector");
if (!ok) assertThrows(IllegalArgumentException.class,
throw new RuntimeException("Expected IllegalArgumentException!"); () -> ps.select(null),
URI uri = null; "Expected IllegalArgumentException!");
try { URI uri = new URI("http://java.sun.com");
uri = new URI("http://java.sun.com"); SocketAddress sa = new InetSocketAddress("localhost", 80);
} catch (java.net.URISyntaxException use) { IOException ioe = new IOException("dummy IOE");
// can't happen assertThrows(IllegalArgumentException.class,
} () -> ps.connectFailed(uri, sa, null),
SocketAddress sa = new InetSocketAddress("localhost", 80); "Expected IllegalArgumentException!");
IOException ioe = new IOException("dummy IOE"); assertThrows(IllegalArgumentException.class,
ok = false; () -> ps.connectFailed(uri, null, ioe),
try { "Expected IllegalArgumentException!");
ps.connectFailed(uri, sa, null); assertThrows(IllegalArgumentException.class,
} catch (IllegalArgumentException iae) { () -> ps.connectFailed(null, sa, ioe),
System.out.println("OK"); "Expected IllegalArgumentException!");
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!");
}
} }
} }