Skip to content
Permalink
Browse files
8226514: Replace wildcard address with loopback or local host in test…
…s - part 17

Improve test stabilty by getting rid of the wildcard address whenever possible.

Reviewed-by: stuefe, mdoerr
Backport-of: 2ad9f11
  • Loading branch information
RealCLanger committed Oct 25, 2021
1 parent 80e371c commit e216ee64c242b754c1de373d190edec8e823399c
Showing 14 changed files with 144 additions and 86 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2019, 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
@@ -23,25 +23,35 @@

/* @test
* @bug 7183292
* @library /test/lib
* @modules jdk.httpserver
* @run main IllegalCookieNameTest
* @run main/othervm -Djava.net.preferIPv6Addresses=true IllegalCookieNameTest
*/
import java.net.*;
import java.util.*;
import java.io.*;
import com.sun.net.httpserver.*;
import jdk.test.lib.net.URIBuilder;

public class IllegalCookieNameTest {
public static void main(String[] args) throws IOException {
public static void main(String[] args) throws Exception {
HttpServer s = null;
try {
InetSocketAddress addr = new InetSocketAddress(0);
InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress addr = new InetSocketAddress(loopback, 0);
s = HttpServer.create(addr, 10);
s.createContext("/", new HHandler());
s.start();
String u = "http://127.0.0.1:" + s.getAddress().getPort() + "/";
String u = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(s.getAddress().getPort())
.path("/")
.build().toString();
CookieHandler.setDefault(new TestCookieHandler());
URL url = new URL(u);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
HttpURLConnection c = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
c.getHeaderFields();
System.out.println ("OK");
} finally {
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2019, 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
@@ -265,14 +265,27 @@ public static String protocol(HttpProtocolType type) {
public static URL url(HttpProtocolType protocol, InetSocketAddress address,
String path) throws MalformedURLException {
return new URL(protocol(protocol),
address.getHostString(),
address.getAddress().getHostAddress(),
address.getPort(), path);
}

public static Proxy proxy(HTTPTestServer server, HttpAuthType authType) {
return (authType == HttpAuthType.PROXY)
? new Proxy(Proxy.Type.HTTP, server.getAddress())
: null;
if (authType != HttpAuthType.PROXY) return null;

InetSocketAddress proxyAddress = server.getProxyAddress();
if (!proxyAddress.isUnresolved()) {
// Forces the proxy to use an unresolved address created
// from the actual IP address to avoid using the proxy
// address hostname which would result in resolving to
// a posibly different address. For instance we want to
// avoid cases such as:
// ::1 => "localhost" => 127.0.0.1
proxyAddress = InetSocketAddress.
createUnresolved(proxyAddress.getAddress().getHostAddress(),
proxyAddress.getPort());
}

return new Proxy(Proxy.Type.HTTP, proxyAddress);
}

public static HttpURLConnection openConnection(URL url,
@@ -399,6 +399,10 @@ public InetSocketAddress getAddress() {
return serverImpl.getAddress();
}

public InetSocketAddress getProxyAddress() {
return serverImpl.getAddress();
}

public void stop() {
serverImpl.stop(0);
if (redirect != null) {
@@ -1027,7 +1031,7 @@ public void run() {
}

@Override
public InetSocketAddress getAddress() {
public InetSocketAddress getProxyAddress() {
return new InetSocketAddress(ss.getInetAddress(), ss.getLocalPort());
}

@@ -1055,7 +1059,7 @@ public void run() {
Socket clientConnection = null;
try {
while (true) {
System.out.println("Tunnel: Waiting for client");
System.out.println("Tunnel: Waiting for client at: " + ss);
Socket previous = clientConnection;
try {
clientConnection = ss.accept();
@@ -27,7 +27,7 @@
* @summary FtpURLConnection doesn't close FTP connection when FileNotFoundException is thrown
* @library ../www/ftptest/
* @build FtpServer FtpCommandHandler FtpAuthHandler FtpFileSystemHandler
* @run main FtpURLConnectionLeak
* @run main/othervm FtpURLConnectionLeak
*/
import java.io.FileNotFoundException;
import java.io.IOException;
@@ -51,23 +51,19 @@
MyHandler httpHandler;
ExecutorService executorService;

public static void main(String[] args) {
public static void main(String[] args) throws Exception {
if (args.length == 1 && args[0].equals("noRetry"))
shouldRetry = false;

new RetryPost();
}

public RetryPost() {
try {
startHttpServer(shouldRetry);
doClient();
} catch (IOException ioe) {
System.err.println(ioe);
}
public RetryPost() throws Exception {
startHttpServer(shouldRetry);
doClient();
}

void doClient() {
void doClient() throws Exception {
try {
InetSocketAddress address = httpServer.getAddress();
URL url = new URL("http://localhost:" + address.getPort() + "/test/");
@@ -87,8 +83,6 @@ void doClient() {
else if (!shouldRetry && httpHandler.getCallCount() != 1)
throw new RuntimeException("Failed: Handler should have only been called once" +
"It was called "+ httpHandler.getCallCount() + " times");
} catch (IOException e) {
e.printStackTrace();
} finally {
httpServer.stop(1);
executorService.shutdown();
@@ -110,8 +104,8 @@ public void startHttpServer(boolean shouldRetry) throws IOException {
}

class MyHandler implements HttpHandler {
int callCount = 0;
boolean shouldRetry;
volatile int callCount = 0;
final boolean shouldRetry;

public MyHandler(boolean shouldRetry) {
this.shouldRetry = shouldRetry;
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2019, 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
@@ -25,7 +25,9 @@
* @test
* @bug 5017051 6360774
* @modules jdk.httpserver
* @library /test/lib
* @run main/othervm B5017051
* @run main/othervm -Djava.net.preferIPv6Addresses=true B5017051
* @summary Tests CR 5017051 & 6360774
*/

@@ -35,6 +37,7 @@
import com.sun.net.httpserver.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import jdk.test.lib.net.URIBuilder;

/*
* Part 1:
@@ -55,42 +58,47 @@

public class B5017051
{
com.sun.net.httpserver.HttpServer httpServer;
HttpServer httpServer;
ExecutorService executorService;

public static void main(String[] args)
{
public static void main(String[] args) throws Exception {
new B5017051();
}

public B5017051()
{
try {
startHttpServer();
doClient();
} catch (IOException ioe) {
System.err.println(ioe);
}
public B5017051() throws Exception {
startHttpServer();
doClient();
}

void doClient() {
void doClient() throws Exception {
java.net.Authenticator.setDefault(new MyAuthenticator());
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
ProxySelector.setDefault(ProxySelector.of(null));

try {
InetSocketAddress address = httpServer.getAddress();

// Part 1
URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/");
URL url = URIBuilder.newBuilder()
.scheme("http")
.host(address.getAddress())
.port(address.getPort())
.path("/test/")
.toURL();
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
int resp = uc.getResponseCode();
if (resp != 200)
throw new RuntimeException("Failed: Part 1, Response code is not 200");
throw new RuntimeException("Failed: Part 1, Response code is not 200: " + resp);

System.out.println("Response code from Part 1 = 200 OK");

// Part 2
URL url2 = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test2/");
URL url2 = URIBuilder.newBuilder()
.scheme("http")
.host(address.getAddress())
.port(address.getPort())
.path("/test2/")
.toURL();

// can use the global CookieHandler used for the first test as the URL's are different
CookieHandler ch = CookieHandler.getDefault();
@@ -106,15 +114,10 @@ void doClient() {
uc = (HttpURLConnection)url2.openConnection();
resp = uc.getResponseCode();
if (resp != 200)
throw new RuntimeException("Failed: Part 2, Response code is not 200");
throw new RuntimeException("Failed: Part 2, Response code is not 200: " + resp);

System.out.println("Response code from Part 2 = 200 OK");


} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException ue) {
ue.printStackTrace();
} finally {
httpServer.stop(1);
executorService.shutdown();
@@ -125,7 +128,8 @@ void doClient() {
* Http Server
*/
public void startHttpServer() throws IOException {
httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
InetAddress loopback = InetAddress.getLoopbackAddress();
httpServer = HttpServer.create(new InetSocketAddress(loopback, 0), 0);

// create HttpServer context for Part 1.
HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2019, 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
@@ -28,6 +28,7 @@
* @library ../../httptest/
* @build HttpCallback TestHttpServer HttpTransaction
* @run main/othervm B6296310
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6296310
* @summary REGRESSION: AppletClassLoader.getResourceAsStream() behaviour is wrong in some cases
*/

@@ -45,32 +46,26 @@
static SimpleHttpTransaction httpTrans;
static TestHttpServer server;

public static void main(String[] args)
public static void main(String[] args) throws Exception
{
ResponseCache.setDefault(new MyCacheHandler());
startHttpServer();

makeHttpCall();
}

public static void startHttpServer() {
try {
httpTrans = new SimpleHttpTransaction();
server = new TestHttpServer(httpTrans, 1, 10, 0);
} catch (IOException e) {
e.printStackTrace();
}
public static void startHttpServer() throws IOException {
httpTrans = new SimpleHttpTransaction();
InetAddress loopback = InetAddress.getLoopbackAddress();
server = new TestHttpServer(httpTrans, 1, 10, loopback, 0);
}

public static void makeHttpCall() {
public static void makeHttpCall() throws IOException {
try {
System.out.println("http server listen on: " + server.getLocalPort());
URL url = new URL("http" , InetAddress.getLocalHost().getHostAddress(),
URL url = new URL("http" , InetAddress.getLoopbackAddress().getHostAddress(),
server.getLocalPort(), "/");
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
System.out.println(uc.getResponseCode());
} catch (IOException e) {
e.printStackTrace();
} finally {
server.terminate();
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2019, 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
@@ -26,6 +26,7 @@
* @bug 6299712 7150552
* @modules jdk.httpserver
* @run main/othervm B6299712
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6299712
* @summary NullPointerException in sun.net.www.protocol.http.HttpURLConnection.followRedirect
*/

@@ -54,13 +55,15 @@ public class B6299712 {

public static void main(String[] args) throws Exception {
ResponseCache.setDefault(new DeployCacheHandler());
ProxySelector.setDefault(ProxySelector.of(null)); // no proxy
startHttpServer();

makeHttpCall();
}

public static void startHttpServer() throws IOException {
server = HttpServer.create(new InetSocketAddress(0), 0);
InetAddress address = InetAddress.getLocalHost();
server = HttpServer.create(new InetSocketAddress(address, 0), 0);
server.createContext("/", new DefaultHandler());
server.createContext("/redirect", new RedirectHandler());
server.start();

1 comment on commit e216ee6

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on e216ee6 Oct 25, 2021

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.