Skip to content

Commit

Permalink
8318150: StaticProxySelector.select should not throw NullPointerExcep…
Browse files Browse the repository at this point in the history
…tions

Reviewed-by: jpai, dfuchs
  • Loading branch information
djelinski committed Oct 20, 2023
1 parent c46a54e commit d9ce525
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 57 deletions.
19 changes: 14 additions & 5 deletions src/java.base/share/classes/java/net/ProxySelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import sun.security.util.SecurityConstants;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<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;
Expand Down
88 changes: 36 additions & 52 deletions test/jdk/java/net/ProxySelector/NullArguments.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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!");
}
}

1 comment on commit d9ce525

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.