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

Reviewed-by: mdoerr
Backport-of: 77d09f0
  • Loading branch information
GoeLin committed Mar 18, 2022
1 parent dbd14f7 commit 306d863343d9cecf9d810ab1f793c83929f708bb
Showing 11 changed files with 231 additions and 87 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 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
@@ -30,22 +30,23 @@
*/
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.ServerSocket;
import java.net.Socket;
import jdk.net.ExtendedSocketOptions;

public class TcpKeepAliveTest {

private static final String LOCAL_HOST = "127.0.0.1";
private static final int DEFAULT_KEEP_ALIVE_PROBES = 7;
private static final int DEFAULT_KEEP_ALIVE_TIME = 1973;
private static final int DEFAULT_KEEP_ALIVE_INTVL = 53;

public static void main(String args[]) throws IOException {

try (ServerSocket ss = new ServerSocket(0);
Socket s = new Socket(LOCAL_HOST, ss.getLocalPort());
var loopback = InetAddress.getLoopbackAddress();
try (ServerSocket ss = boundServer(loopback);
Socket s = new Socket(loopback, ss.getLocalPort());
DatagramSocket ds = new DatagramSocket(0);
MulticastSocket mc = new MulticastSocket(0)) {
if (ss.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) {
@@ -110,4 +111,11 @@ public static void main(String args[]) throws IOException {
}
}
}

private static ServerSocket boundServer(InetAddress address) throws IOException {
var socketAddress = new InetSocketAddress(address, 0);
var server = new ServerSocket();
server.bind(socketAddress);
return server;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 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,6 +23,7 @@

/* @test
* @bug 4397096
* @library /test/lib
* @run main/othervm SetIfModifiedSince
* @summary setIfModifiedSince() of HttpURLConnection sets invalid date of default locale
*/
@@ -31,7 +32,11 @@
import java.io.*;
import java.util.*;

import jdk.test.lib.net.URIBuilder;

public class SetIfModifiedSince {
static volatile boolean successfulHeaderCheck = false;
static final String MARKER = "A-test-name";

static class XServer extends Thread {
ServerSocket srv;
@@ -48,46 +53,82 @@ Socket getSocket () {
}

public void run() {
try {
boolean foundMarker = false;
while (!foundMarker) {
String x;
s = srv.accept ();
is = s.getInputStream ();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
os = s.getOutputStream ();
while ((x=r.readLine()) != null) {
String header = "If-Modified-Since: ";
if (x.startsWith(header)) {
if (x.charAt(header.length()) == '?') {
s.close ();
srv.close (); // or else the HTTPURLConnection will retry
throw new RuntimeException
("Invalid HTTP date specification");
try {
s = srv.accept();
System.out.println("Server: accepting connection from: " + s);
is = s.getInputStream ();
} catch (IOException io) {
System.err.println("Server: Failed to accept connection: " + io);
io.printStackTrace();
try { srv.close(); } catch (IOException ioc) { }
break;
}
try {
BufferedReader r = new BufferedReader(new InputStreamReader(is));
os = s.getOutputStream ();
boolean foundHeader;
while ((x=r.readLine()) != null) {
String testname = MARKER + ": ";
String header = "If-Modified-Since: ";
if (x.startsWith(header)) {
foundHeader = true;
System.out.println("Server: found header: " + x);
if (x.charAt(header.length()) == '?') {
s.close ();
srv.close (); // or else the HTTPURLConnection will retry
throw new RuntimeException
("Invalid HTTP date specification");
}
if (foundMarker) break;
} else if (x.startsWith(testname)) {
foundMarker = true;
System.out.println("Server: found marker: " + x);
}
break;
}
}
s.close ();
srv.close (); // or else the HTTPURLConnection will retry
} catch (IOException e) {}
successfulHeaderCheck = true;
s.close ();
// only close server if connected from this test.
if (foundMarker) {
srv.close (); // or else the HTTPURLConnection will retry
}
} catch (IOException e) {}
}
}
}

public static void main (String[] args) {
public static void main(String[] args) throws Exception {
Locale reservedLocale = Locale.getDefault();
try {
Locale.setDefault(Locale.JAPAN);
ServerSocket serversocket = new ServerSocket (0);
ServerSocket serversocket = new ServerSocket();
serversocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
int port = serversocket.getLocalPort ();
XServer server = new XServer (serversocket);
server.start ();
Thread.sleep (2000);
URL url = new URL ("http://localhost:"+port+"/index.html");
URLConnection urlc = url.openConnection ();
URL url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(port)
.path("/index.html")
.toURLUnchecked();
URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
urlc.setRequestProperty(MARKER, "SetIfModifiedSince");
urlc.setIfModifiedSince (10000000);
InputStream is = urlc.getInputStream ();
int i=0, c;
Thread.sleep (5000);
} catch (Exception e) {
int i = 0, c;
Thread.sleep(5000);
if (!successfulHeaderCheck) {
throw new RuntimeException("Header check was unsuccessful");
}
} catch (SocketException ce) {
if (!successfulHeaderCheck) {
throw ce;
}
System.out.println("ConnectionException expected on successful check of If-modified-since header");
} finally {
// restore the reserved locale
Locale.setDefault(reservedLocale);
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 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
@@ -27,15 +27,25 @@
* @summary REGRESSION: Sun implementation for HttpURLConnection could throw NPE
* @modules java.base/sun.net
* java.base/sun.net.www.http
* @library /test/lib
*/
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.URL;
import sun.net.www.http.HttpClient;
import jdk.test.lib.net.URIBuilder;

public class GetProxyPort {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(0);
URL myURL = new URL("http://localhost:" + ss.getLocalPort());
ServerSocket ss = new ServerSocket();
InetAddress loopback = InetAddress.getLoopbackAddress();
ss.bind(new InetSocketAddress(loopback, 0));
URL myURL = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(ss.getLocalPort())
.toURL();
HttpClient httpC = new HttpClient(myURL, null, -1);
int port = httpC.getProxyPortUsed();
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2001, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 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
@@ -27,19 +27,29 @@
* @summary Make sure that implicit filenames will be returned as "/"
* @modules java.base/sun.net
* java.base/sun.net.www.http
* @library /test/lib
*/

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URL;
import java.net.ServerSocket;
import sun.net.www.http.HttpClient;
import jdk.test.lib.net.URIBuilder;

public class ImplicitFileName {

public static void main(String[] args) throws Exception {

ServerSocket ss = new ServerSocket(0);
ServerSocket ss = new ServerSocket();
InetAddress loopback = InetAddress.getLoopbackAddress();
ss.bind(new InetSocketAddress(loopback, 0));

URL url = new URL("http://localhost:" + ss.getLocalPort());
URL url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(ss.getLocalPort())
.toURL();

HttpClient c = HttpClient.New(url);

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 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
@@ -28,43 +28,55 @@
* has been closed
* @modules java.base/sun.net
* java.base/sun.net.www.http:+open
* @library /test/lib
*/

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URL;
import java.net.ServerSocket;
import sun.net.www.http.HttpClient;
import java.security.*;
import java.lang.reflect.Method;
import jdk.test.lib.net.URIBuilder;

public class IsAvailable {

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

URL url1 = new URL("http://localhost:" + ss.getLocalPort());
HttpClient c1 = HttpClient.New(url1);
try (ServerSocket toclose = ss) {

Method available = HttpClient.class.
getDeclaredMethod("available", null);
available.setAccessible(true);
URL url1 = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(ss.getLocalPort())
.toURL();

c1.setReadTimeout(readTimeout);
boolean a = (boolean) available.invoke(c1);
if (!a) {
throw new RuntimeException("connection should be available");
}
if (c1.getReadTimeout() != readTimeout) {
throw new RuntimeException("read timeout has been altered");
}
HttpClient c1 = HttpClient.New(url1);

c1.closeServer();
Method available = HttpClient.class.
getDeclaredMethod("available", null);
available.setAccessible(true);

a = (boolean) available.invoke(c1);
if (a) {
throw new RuntimeException("connection shouldn't be available");
}
c1.setReadTimeout(readTimeout);
boolean a = (boolean) available.invoke(c1);
if (!a) {
throw new RuntimeException("connection should be available");
}
if (c1.getReadTimeout() != readTimeout) {
throw new RuntimeException("read timeout has been altered");
}

ss.close();
c1.closeServer();

a = (boolean) available.invoke(c1);
if (a) {
throw new RuntimeException("connection shouldn't be available");
}
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2001, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 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,32 +28,42 @@
* doPrivileged() call at appropriate places.
* @modules java.base/sun.net
* java.base/sun.net.www.http
* @library /test/lib
* @run main/othervm/policy=IsKeepingAlive.policy IsKeepingAlive
*/

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URL;
import java.net.ServerSocket;
import sun.net.www.http.HttpClient;
import java.security.*;
import jdk.test.lib.net.URIBuilder;

public class IsKeepingAlive {

public static void main(String[] args) throws Exception {

ServerSocket ss = new ServerSocket(0);
ServerSocket ss = new ServerSocket();
InetAddress loopback = InetAddress.getLoopbackAddress();
ss.bind(new InetSocketAddress(loopback, 0));

SecurityManager security = System.getSecurityManager();
if (security == null) {
security = new SecurityManager();
System.setSecurityManager(security);
}

URL url1 = new URL("http://localhost:" + ss.getLocalPort());
try (ServerSocket toClose = ss) {
SecurityManager security = System.getSecurityManager();
if (security == null) {
security = new SecurityManager();
System.setSecurityManager(security);
}

HttpClient c1 = HttpClient.New(url1);
URL url1 = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(ss.getLocalPort())
.toURL();

boolean keepAlive = c1.isKeepingAlive();
HttpClient c1 = HttpClient.New(url1);

ss.close();
boolean keepAlive = c1.isKeepingAlive();
}
}
}

1 comment on commit 306d863

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 306d863 Mar 18, 2022

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.