Skip to content

Commit caaf409

Browse files
author
SendaoYan
committedMar 5, 2025
8350546: Several java/net/InetAddress tests fails UnknownHostException
Reviewed-by: dfuchs, myankelevich
1 parent 062b7c7 commit caaf409

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed
 

‎test/jdk/java/net/InetAddress/IsReachableViaLoopbackTest.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,22 +21,24 @@
2121
* questions.
2222
*/
2323

24-
import java.io.*;
25-
import java.net.*;
26-
import java.util.*;
24+
import java.io.IOException;
25+
import java.net.InetAddress;
26+
import java.net.NetworkInterface;
2727

2828
/**
2929
* @test
3030
* @bug 8135305
3131
* @key intermittent
32+
* @library /test/lib
3233
* @summary ensure we can't ping external hosts via loopback if
34+
* @run main IsReachableViaLoopbackTest
3335
*/
3436

3537
public class IsReachableViaLoopbackTest {
3638
public static void main(String[] args) {
3739
try {
38-
InetAddress addr = InetAddress.getByName("localhost");
39-
InetAddress remoteAddr = InetAddress.getByName("bugs.openjdk.org");
40+
InetAddress addr = InetAddress.getLoopbackAddress();
41+
InetAddress remoteAddr = InetAddress.getByName("23.197.138.208"); // use literal address to avoid DNS checks
4042
if (!addr.isReachable(10000))
4143
throw new RuntimeException("Localhost should always be reachable");
4244
NetworkInterface inf = NetworkInterface.getByInetAddress(addr);
@@ -54,7 +56,6 @@ public static void main(String[] args) {
5456
} else {
5557
System.out.println("inf == null");
5658
}
57-
5859
} catch (IOException e) {
5960
throw new RuntimeException("Unexpected exception:" + e);
6061
}

‎test/jdk/java/net/InetAddress/getOriginalHostName.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -41,7 +41,9 @@ public class getOriginalHostName {
4141
public static void main(String[] args) throws Exception {
4242
final String HOST = "dummyserver.java.net";
4343
InetAddress ia = null;
44-
ia = InetAddress.getByName(HOST);
44+
ia = getInetAddress(HOST);
45+
if (ia != null) testInetAddress(ia, HOST);
46+
ia = InetAddress.getByAddress(HOST, new byte[] { 1, 2, 3, 4});
4547
testInetAddress(ia, HOST);
4648
ia = InetAddress.getByName("255.255.255.0");
4749
testInetAddress(ia, null);
@@ -53,6 +55,14 @@ public static void main(String[] args) throws Exception {
5355
testInetAddress(ia, ia.getHostName());
5456
}
5557

58+
private static InetAddress getInetAddress(String host) {
59+
try {
60+
return InetAddress.getByName(host);
61+
} catch (java.net.UnknownHostException uhe) {
62+
System.out.println("Skipping " + host + " due to " + uhe);
63+
return null;
64+
}
65+
}
5666

5767
private static void testInetAddress(InetAddress ia, String expected)
5868
throws Exception {

0 commit comments

Comments
 (0)