Skip to content

Commit 459957f

Browse files
author
Alex Menkov
committedDec 18, 2023
8322062: com/sun/jdi/JdwpAllowTest.java does not performs negative testing with prefix length
Reviewed-by: cjplummer, sspitsyn
1 parent b98d13f commit 459957f

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed
 

‎test/jdk/com/sun/jdi/JdwpAllowTest.java

+26-18
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static int handshake(int port) throws IOException {
5757
res = s.getInputStream().read(buffer);
5858
}
5959
catch (SocketException ex) {
60-
ex.printStackTrace();
60+
ex.printStackTrace(System.out);
6161
// pass
6262
} finally {
6363
if (s != null) {
@@ -98,7 +98,7 @@ private static int detectPort(LingeredApp app) {
9898

9999
public static void positiveTest(String testName, String allowOpt)
100100
throws InterruptedException, IOException {
101-
System.err.println("\nStarting " + testName);
101+
System.out.println("\nStarting " + testName);
102102
String[] cmd = prepareCmd(allowOpt);
103103

104104
LingeredApp a = LingeredApp.startApp(cmd);
@@ -111,12 +111,12 @@ public static void positiveTest(String testName, String allowOpt)
111111
if (res < 0) {
112112
throw new RuntimeException(testName + " FAILED");
113113
}
114-
System.err.println(testName + " PASSED");
114+
System.out.println(testName + " PASSED");
115115
}
116116

117117
public static void negativeTest(String testName, String allowOpt)
118118
throws InterruptedException, IOException {
119-
System.err.println("\nStarting " + testName);
119+
System.out.println("\nStarting " + testName);
120120
String[] cmd = prepareCmd(allowOpt);
121121

122122
LingeredApp a = LingeredApp.startApp(cmd);
@@ -127,24 +127,24 @@ public static void negativeTest(String testName, String allowOpt)
127127
a.stopApp();
128128
}
129129
if (res > 0) {
130-
System.err.println(testName + ": res=" + res);
130+
System.out.println(testName + ": res=" + res);
131131
throw new RuntimeException(testName + " FAILED");
132132
}
133-
System.err.println(testName + ": returned a negative code as expected: " + res);
134-
System.err.println(testName + " PASSED");
133+
System.out.println(testName + ": returned a negative code as expected: " + res);
134+
System.out.println(testName + " PASSED");
135135
}
136136

137137
public static void badAllowOptionTest(String testName, String allowOpt)
138138
throws InterruptedException, IOException {
139-
System.err.println("\nStarting " + testName);
139+
System.out.println("\nStarting " + testName);
140140
String[] cmd = prepareCmd(allowOpt);
141141

142142
LingeredApp a;
143143
try {
144144
a = LingeredApp.startApp(cmd);
145145
} catch (IOException ex) {
146-
System.err.println(testName + ": caught expected IOException");
147-
System.err.println(testName + " PASSED");
146+
System.out.println(testName + ": caught expected IOException");
147+
System.out.println(testName + " PASSED");
148148
return;
149149
}
150150
// LingeredApp.startApp is expected to fail, but if not, terminate the app
@@ -154,7 +154,7 @@ public static void badAllowOptionTest(String testName, String allowOpt)
154154

155155
/*
156156
* Generate allow address by changing random bit in the local address
157-
* and calculate 2 masks (prefix length) - one is matches original local address
157+
* and calculate 2 masks (prefix length) - one matches original local address
158158
* and another doesn't.
159159
*/
160160
private static class MaskTest {
@@ -167,17 +167,25 @@ public MaskTest(InetAddress addr) throws Exception {
167167
localAddress = addr.getHostAddress();
168168
byte[] bytes = addr.getAddress();
169169
Random r = new Random();
170-
// prefix length must be >= 1, so bitToChange must be >= 2
171-
int bitToChange = r.nextInt(bytes.length * 8 - 3) + 2;
170+
// Prefix length is 1..32 for IPv4, 1..128 for IPv6.
171+
// bitToChange is zero-based and must be >0 (for 0 "good" prefix length would be 0).
172+
// Corner cases (for 127.0.0.1):
173+
// bitToChange == 1 => allow address = 0.0.0.0
174+
// - "good" allow mask is "0.0.0.0/1";
175+
// - "bad" allow mask is "0.0.0.0/2";
176+
// bitToChange == 31 => allow address = 127.0.0.0
177+
// - "good" allow mask is "127.0.0.0/31";
178+
// - "bad" allow mask is "127.0.0.0/32".
179+
int bitToChange = r.nextInt(bytes.length * 8 - 2) + 1;
172180
setBit(bytes, bitToChange, !getBit(bytes, bitToChange));
173181
// clear rest of the bits for mask address
174182
for (int i = bitToChange + 1; i < bytes.length * 8; i++) {
175183
setBit(bytes, i, false);
176184
}
177185
allowAddress = InetAddress.getByAddress(bytes).getHostAddress();
178186

179-
prefixLengthBad = bitToChange;
180-
prefixLengthGood = bitToChange - 1;
187+
prefixLengthGood = bitToChange;
188+
prefixLengthBad = bitToChange + 1;
181189
}
182190

183191
private static boolean getBit(byte[] bytes, int pos) {
@@ -203,7 +211,7 @@ private static void init() throws Exception {
203211
throw new RuntimeException("No addresses is returned for 'localhost'");
204212
}
205213
localAddr = addrs[0].getHostAddress();
206-
System.err.println("localhost address: " + localAddr);
214+
System.out.println("localhost address: " + localAddr);
207215

208216
for (int i = 0; i < addrs.length; i++) {
209217
maskTests.add(new MaskTest(addrs[i]));
@@ -243,11 +251,11 @@ public static void main(String[] args) throws Exception {
243251
localAddr = test.localAddress;
244252
positiveTest("PositiveMaskTest(" + test.localAddress + ")",
245253
test.allowAddress + "/" + test.prefixLengthGood);
246-
positiveTest("NegativeMaskTest(" + test.localAddress + ")",
254+
negativeTest("NegativeMaskTest(" + test.localAddress + ")",
247255
test.allowAddress + "/" + test.prefixLengthBad);
248256
}
249257

250-
System.err.println("\nTest PASSED");
258+
System.out.println("\nTest PASSED");
251259
}
252260

253261
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Dec 18, 2023

@openjdk-notifier[bot]
Please sign in to comment.