@@ -57,7 +57,7 @@ public static int handshake(int port) throws IOException {
57
57
res = s .getInputStream ().read (buffer );
58
58
}
59
59
catch (SocketException ex ) {
60
- ex .printStackTrace ();
60
+ ex .printStackTrace (System . out );
61
61
// pass
62
62
} finally {
63
63
if (s != null ) {
@@ -98,7 +98,7 @@ private static int detectPort(LingeredApp app) {
98
98
99
99
public static void positiveTest (String testName , String allowOpt )
100
100
throws InterruptedException , IOException {
101
- System .err .println ("\n Starting " + testName );
101
+ System .out .println ("\n Starting " + testName );
102
102
String [] cmd = prepareCmd (allowOpt );
103
103
104
104
LingeredApp a = LingeredApp .startApp (cmd );
@@ -111,12 +111,12 @@ public static void positiveTest(String testName, String allowOpt)
111
111
if (res < 0 ) {
112
112
throw new RuntimeException (testName + " FAILED" );
113
113
}
114
- System .err .println (testName + " PASSED" );
114
+ System .out .println (testName + " PASSED" );
115
115
}
116
116
117
117
public static void negativeTest (String testName , String allowOpt )
118
118
throws InterruptedException , IOException {
119
- System .err .println ("\n Starting " + testName );
119
+ System .out .println ("\n Starting " + testName );
120
120
String [] cmd = prepareCmd (allowOpt );
121
121
122
122
LingeredApp a = LingeredApp .startApp (cmd );
@@ -127,24 +127,24 @@ public static void negativeTest(String testName, String allowOpt)
127
127
a .stopApp ();
128
128
}
129
129
if (res > 0 ) {
130
- System .err .println (testName + ": res=" + res );
130
+ System .out .println (testName + ": res=" + res );
131
131
throw new RuntimeException (testName + " FAILED" );
132
132
}
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" );
135
135
}
136
136
137
137
public static void badAllowOptionTest (String testName , String allowOpt )
138
138
throws InterruptedException , IOException {
139
- System .err .println ("\n Starting " + testName );
139
+ System .out .println ("\n Starting " + testName );
140
140
String [] cmd = prepareCmd (allowOpt );
141
141
142
142
LingeredApp a ;
143
143
try {
144
144
a = LingeredApp .startApp (cmd );
145
145
} 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" );
148
148
return ;
149
149
}
150
150
// LingeredApp.startApp is expected to fail, but if not, terminate the app
@@ -154,7 +154,7 @@ public static void badAllowOptionTest(String testName, String allowOpt)
154
154
155
155
/*
156
156
* 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
158
158
* and another doesn't.
159
159
*/
160
160
private static class MaskTest {
@@ -167,17 +167,25 @@ public MaskTest(InetAddress addr) throws Exception {
167
167
localAddress = addr .getHostAddress ();
168
168
byte [] bytes = addr .getAddress ();
169
169
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 ;
172
180
setBit (bytes , bitToChange , !getBit (bytes , bitToChange ));
173
181
// clear rest of the bits for mask address
174
182
for (int i = bitToChange + 1 ; i < bytes .length * 8 ; i ++) {
175
183
setBit (bytes , i , false );
176
184
}
177
185
allowAddress = InetAddress .getByAddress (bytes ).getHostAddress ();
178
186
179
- prefixLengthBad = bitToChange ;
180
- prefixLengthGood = bitToChange - 1 ;
187
+ prefixLengthGood = bitToChange ;
188
+ prefixLengthBad = bitToChange + 1 ;
181
189
}
182
190
183
191
private static boolean getBit (byte [] bytes , int pos ) {
@@ -203,7 +211,7 @@ private static void init() throws Exception {
203
211
throw new RuntimeException ("No addresses is returned for 'localhost'" );
204
212
}
205
213
localAddr = addrs [0 ].getHostAddress ();
206
- System .err .println ("localhost address: " + localAddr );
214
+ System .out .println ("localhost address: " + localAddr );
207
215
208
216
for (int i = 0 ; i < addrs .length ; i ++) {
209
217
maskTests .add (new MaskTest (addrs [i ]));
@@ -243,11 +251,11 @@ public static void main(String[] args) throws Exception {
243
251
localAddr = test .localAddress ;
244
252
positiveTest ("PositiveMaskTest(" + test .localAddress + ")" ,
245
253
test .allowAddress + "/" + test .prefixLengthGood );
246
- positiveTest ("NegativeMaskTest(" + test .localAddress + ")" ,
254
+ negativeTest ("NegativeMaskTest(" + test .localAddress + ")" ,
247
255
test .allowAddress + "/" + test .prefixLengthBad );
248
256
}
249
257
250
- System .err .println ("\n Test PASSED" );
258
+ System .out .println ("\n Test PASSED" );
251
259
}
252
260
253
261
}
1 commit comments
openjdk-notifier[bot] commentedon Dec 18, 2023
Review
Issues