8322062: com/sun/jdi/JdwpAllowTest.java does not performs negative testing with prefix length

Backport-of: 459957f30a
This commit is contained in:
Andrew Lu
2024-06-25 05:43:24 +00:00
committed by Vitaly Provodin
parent b36aae659c
commit ccbcd8b97b

View File

@@ -57,7 +57,7 @@ public class JdwpAllowTest {
res = s.getInputStream().read(buffer);
}
catch (SocketException ex) {
ex.printStackTrace();
ex.printStackTrace(System.out);
// pass
} finally {
if (s != null) {
@@ -98,7 +98,7 @@ public class JdwpAllowTest {
public static void positiveTest(String testName, String allowOpt)
throws InterruptedException, IOException {
System.err.println("\nStarting " + testName);
System.out.println("\nStarting " + testName);
String[] cmd = prepareCmd(allowOpt);
LingeredApp a = LingeredApp.startApp(cmd);
@@ -111,12 +111,12 @@ public class JdwpAllowTest {
if (res < 0) {
throw new RuntimeException(testName + " FAILED");
}
System.err.println(testName + " PASSED");
System.out.println(testName + " PASSED");
}
public static void negativeTest(String testName, String allowOpt)
throws InterruptedException, IOException {
System.err.println("\nStarting " + testName);
System.out.println("\nStarting " + testName);
String[] cmd = prepareCmd(allowOpt);
LingeredApp a = LingeredApp.startApp(cmd);
@@ -127,24 +127,24 @@ public class JdwpAllowTest {
a.stopApp();
}
if (res > 0) {
System.err.println(testName + ": res=" + res);
System.out.println(testName + ": res=" + res);
throw new RuntimeException(testName + " FAILED");
}
System.err.println(testName + ": returned a negative code as expected: " + res);
System.err.println(testName + " PASSED");
System.out.println(testName + ": returned a negative code as expected: " + res);
System.out.println(testName + " PASSED");
}
public static void badAllowOptionTest(String testName, String allowOpt)
throws InterruptedException, IOException {
System.err.println("\nStarting " + testName);
System.out.println("\nStarting " + testName);
String[] cmd = prepareCmd(allowOpt);
LingeredApp a;
try {
a = LingeredApp.startApp(cmd);
} catch (IOException ex) {
System.err.println(testName + ": caught expected IOException");
System.err.println(testName + " PASSED");
System.out.println(testName + ": caught expected IOException");
System.out.println(testName + " PASSED");
return;
}
// LingeredApp.startApp is expected to fail, but if not, terminate the app
@@ -154,7 +154,7 @@ public class JdwpAllowTest {
/*
* Generate allow address by changing random bit in the local address
* and calculate 2 masks (prefix length) - one is matches original local address
* and calculate 2 masks (prefix length) - one matches original local address
* and another doesn't.
*/
private static class MaskTest {
@@ -167,8 +167,16 @@ public class JdwpAllowTest {
localAddress = addr.getHostAddress();
byte[] bytes = addr.getAddress();
Random r = new Random();
// prefix length must be >= 1, so bitToChange must be >= 2
int bitToChange = r.nextInt(bytes.length * 8 - 3) + 2;
// Prefix length is 1..32 for IPv4, 1..128 for IPv6.
// bitToChange is zero-based and must be >0 (for 0 "good" prefix length would be 0).
// Corner cases (for 127.0.0.1):
// bitToChange == 1 => allow address = 0.0.0.0
// - "good" allow mask is "0.0.0.0/1";
// - "bad" allow mask is "0.0.0.0/2";
// bitToChange == 31 => allow address = 127.0.0.0
// - "good" allow mask is "127.0.0.0/31";
// - "bad" allow mask is "127.0.0.0/32".
int bitToChange = r.nextInt(bytes.length * 8 - 2) + 1;
setBit(bytes, bitToChange, !getBit(bytes, bitToChange));
// clear rest of the bits for mask address
for (int i = bitToChange + 1; i < bytes.length * 8; i++) {
@@ -176,8 +184,8 @@ public class JdwpAllowTest {
}
allowAddress = InetAddress.getByAddress(bytes).getHostAddress();
prefixLengthBad = bitToChange;
prefixLengthGood = bitToChange - 1;
prefixLengthGood = bitToChange;
prefixLengthBad = bitToChange + 1;
}
private static boolean getBit(byte[] bytes, int pos) {
@@ -203,7 +211,7 @@ public class JdwpAllowTest {
throw new RuntimeException("No addresses is returned for 'localhost'");
}
localAddr = addrs[0].getHostAddress();
System.err.println("localhost address: " + localAddr);
System.out.println("localhost address: " + localAddr);
for (int i = 0; i < addrs.length; i++) {
maskTests.add(new MaskTest(addrs[i]));
@@ -243,11 +251,11 @@ public class JdwpAllowTest {
localAddr = test.localAddress;
positiveTest("PositiveMaskTest(" + test.localAddress + ")",
test.allowAddress + "/" + test.prefixLengthGood);
positiveTest("NegativeMaskTest(" + test.localAddress + ")",
negativeTest("NegativeMaskTest(" + test.localAddress + ")",
test.allowAddress + "/" + test.prefixLengthBad);
}
System.err.println("\nTest PASSED");
System.out.println("\nTest PASSED");
}
}