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.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<Proxy> select(URI uri) {
String scheme = uri.getScheme().toLowerCase(Locale.ROOT);
if (scheme.equals("http") || scheme.equals("https")) {
public List<Proxy> 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;

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.
*
* 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<ProxySelector> 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!");
}
}