Skip to content

Commit

Permalink
8129315: java/net/Socket/LingerTest.java and java/net/Socket/Shutdown…
Browse files Browse the repository at this point in the history
…Both.java timeout intermittently

Tests are updated to use the loopback address instead of the wildcard to avoid traffic being routed to a different server than what was intended by the test.

Backport-of: 72467d9
  • Loading branch information
GoeLin committed Feb 14, 2023
1 parent 3614fb2 commit c90bc80
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 23 deletions.
19 changes: 11 additions & 8 deletions test/jdk/java/net/Socket/LingerTest.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 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
Expand Down Expand Up @@ -71,11 +71,13 @@ public void run() {
}

static class Other implements Runnable {
int port;
long delay;
final InetAddress address;
final int port;
final long delay;
boolean connected = false;

public Other(int port, long delay) {
public Other(InetAddress address, int port, long delay) {
this.address = address;
this.port = port;
this.delay = delay;
}
Expand All @@ -85,7 +87,7 @@ public void run() {
try {
Thread.sleep(delay);
System.out.println ("Other opening socket");
Socket s = new Socket("localhost", port);
Socket s = new Socket(address, port);
synchronized (this) {
connected = true;
}
Expand All @@ -103,9 +105,10 @@ public synchronized boolean connected() {
}

public static void main(String args[]) throws Exception {
ServerSocket ss = new ServerSocket(0);
InetAddress loopback = InetAddress.getLoopbackAddress();
ServerSocket ss = new ServerSocket(0, 50, loopback);

Socket s1 = new Socket("localhost", ss.getLocalPort());
Socket s1 = new Socket(loopback, ss.getLocalPort());
Socket s2 = ss.accept();

// setup conditions for untransmitted data and lengthy
Expand All @@ -119,7 +122,7 @@ public static void main(String args[]) throws Exception {
senderThread.start();

// other thread that will connect after 5 seconds.
Other other = new Other(ss.getLocalPort(), 5000);
Other other = new Other(loopback, ss.getLocalPort(), 5000);
Thread otherThread = new Thread(other);
otherThread.start();

Expand Down
5 changes: 3 additions & 2 deletions test/jdk/java/net/Socket/ShutdownBoth.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 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
Expand Down Expand Up @@ -32,7 +32,8 @@
public class ShutdownBoth {

public static void main(String args[]) throws Exception {
ServerSocket ss = new ServerSocket(0);
InetAddress loopback = InetAddress.getLoopbackAddress();
ServerSocket ss = new ServerSocket(0, 50, loopback);
Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
Socket s2 = ss.accept();

Expand Down
40 changes: 31 additions & 9 deletions test/jdk/java/net/Socks/SocksIPv6Test.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 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
Expand Down Expand Up @@ -30,7 +30,6 @@

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Authenticator;
Expand All @@ -43,7 +42,6 @@
import java.net.ServerSocket;
import java.net.SocketException;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.List;
import com.sun.net.httpserver.*;
Expand All @@ -65,7 +63,7 @@ public class SocksIPv6Test {
public void setUp() throws Exception {
shouldRun = ensureInet6AddressFamily() && ensureIPv6OnLoopback();

server = HttpServer.create(new InetSocketAddress(0), 0);
server = HttpServer.create(new InetSocketAddress("::1", 0), 0);
server.createContext("/", ex -> {
ex.sendResponseHeaders(200, response.length());
try (BufferedWriter writer = new BufferedWriter(
Expand All @@ -76,7 +74,7 @@ public void setUp() throws Exception {
});
server.start();

socks = new SocksServer(0, false);
socks = new SocksServer(InetAddress.getByName("::1"), 0, false);
socks.addUser("user", "pass");
socks.start();

Expand Down Expand Up @@ -140,21 +138,45 @@ public void testSocksOverIPv6() throws Exception {
public void testSocksOverIPv6Hostname() throws Exception {
if (!shouldRun) return;

String ipv6Hostname = InetAddress.getByName("::1").getHostName();
String ipv4Hostname = InetAddress.getByName("127.0.0.1").getHostName();
InetAddress ipv6Loopback = InetAddress.getByName("::1");
String ipv6Hostname = ipv6Loopback.getHostName();
String ipv6HostAddress = ipv6Loopback.getHostAddress();
InetAddress ipv4Loopback;
String ipv4Hostname;
String ipv4HostAddress;
try {
ipv4Loopback = InetAddress.getByName("127.0.0.1");
ipv4Hostname = ipv4Loopback == null ? null : ipv4Loopback.getHostName();
ipv4HostAddress = ipv4Loopback == null ? null : ipv4Loopback.getHostAddress();
} catch (IOException io) {
ipv4Hostname = null;
ipv4HostAddress = null;
}

System.out.println("ipv6Hostname: " + ipv6Hostname + " / " + ipv6HostAddress);
System.out.println("ipv4Hostname: " + ipv4Hostname + " / " + ipv4HostAddress);

if (ipv6Hostname.equals(InetAddress.getByName("::1").getHostAddress())) {
if (ipv6Hostname.equals(ipv6HostAddress)) {
System.out.println("Unable to get the hostname of the IPv6 loopback "
+ "address. Skipping test case.");
return;
}

if (ipv6Hostname.equals(ipv4Hostname)) {
if (ipv4Hostname != null && ipv6Hostname.equals(ipv4Hostname)) {
System.out.println("IPv6 and IPv4 loopback addresses map to the"
+ " same hostname. Skipping test case.");
return;
}

if (!InetAddress.getByName(ipv6Hostname).getHostAddress()
.equals(ipv6HostAddress)) {
System.out.println(ipv6Hostname + " resolves to \""
+ InetAddress.getByName(ipv6Hostname).getHostAddress()
+ "\", not \"" + ipv6HostAddress +
"\". Skipping test case.");
return;
}

Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(ipv6Hostname,
socks.getPort()));
URL url = new URL("http://" + ipv6Hostname + ":" + server.getAddress().getPort());
Expand Down
21 changes: 20 additions & 1 deletion test/jdk/java/net/Socks/SocksServer.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 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
Expand Down Expand Up @@ -491,6 +491,25 @@ public SocksServer(int port) throws IOException {
}
}

public SocksServer(InetAddress addr, int port, boolean useV4) throws IOException {
this.port = port;
this.useV4 = useV4;
server = new ServerSocket();
if (port == 0 && addr == null) {
server.bind(null);
this.port = server.getLocalPort();
} else if (port == 0 && addr != null) {
server.bind(new InetSocketAddress(addr, 0));
this.port = server.getLocalPort();
} else if (addr == null) {
assert port != 0;
server.bind(new InetSocketAddress(port));
} else {
assert port != 0;
server.bind(new InetSocketAddress(addr, port));
}
}

public SocksServer() throws IOException {
this (DEFAULT_PORT);
}
Expand Down
13 changes: 10 additions & 3 deletions test/jdk/sun/net/www/http/HttpURLConnection/PostOnDelete.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 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
Expand Down Expand Up @@ -49,7 +49,7 @@ public void runTest() throws Exception {
try {
s = new Server();
s.startServer();
URL url = new URL("http://localhost:" + s.getPort());
URL url = new URL("http://" + s.getAuthority());
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("DELETE");
urlConnection.setDoOutput(true);
Expand All @@ -70,7 +70,8 @@ class Server {
HttpServer server;

public void startServer() {
InetSocketAddress addr = new InetSocketAddress(0);
InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress addr = new InetSocketAddress(loopback,0);
try {
server = HttpServer.create(addr, 0);
} catch (IOException ioe) {
Expand All @@ -81,6 +82,12 @@ public void startServer() {
server.start();
}

public String getAuthority() {
String address = server.getAddress().getHostString();
address = (address.indexOf(':') >= 0) ? ("[" + address + "]") : address;
return address + ":" + getPort();
}

public int getPort() {
return server.getAddress().getPort();
}
Expand Down

1 comment on commit c90bc80

@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.