1 /* 2 * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 4439631 27 * @bug 4448721 28 * @bug 4448603 29 * @summary Test access to ranges within ArrayReferences 30 * 31 * @author Robert Field 32 * 33 * @run build TestScaffold VMConnection TargetListener TargetAdapter 34 * @run compile -g ArrayRangeTest.java 35 * @run driver ArrayRangeTest 36 */ 37 import com.sun.jdi.*; 38 import com.sun.jdi.event.*; 39 import com.sun.jdi.request.*; 40 41 import java.util.*; 42 43 /********** target program **********/ 44 45 class ArrayRangeTarg { 46 static int[] emptyArray = {}; 47 static int[] fullArray = {0, 100, 200, 300, 400}; 48 49 public static void main(String[] args) { 50 System.out.println("Goodbye from ArrayRangeTarg!"); 51 } 52 } 53 54 /********** test program **********/ 55 56 public class ArrayRangeTest extends TestScaffold { 57 ReferenceType targetClass; 58 59 class Sample { 60 Sample(String name, ArrayReference arrRef, int[] expected) { 61 this.name = name; 62 this.arrRef = arrRef; 63 this.expected = expected; 64 } 65 String name; 66 ArrayReference arrRef; 67 int[] expected; 68 } 69 70 ArrayRangeTest (String args[]) { 71 super(args); 72 } 73 74 public static void main(String[] args) throws Exception { 75 new ArrayRangeTest(args).startTests(); 76 } 77 78 /********** test assist **********/ 79 80 String arr(int a[]) { 81 StringBuffer buf = new StringBuffer(); 82 buf.append('['); 83 if (a.length > 0) { 84 buf.append(a[0]); 85 for (int i = 1; i < a.length; ++i) { 86 buf.append(','); 87 buf.append(a[i]); 88 } 89 } 90 buf.append(']'); 91 return buf.toString(); 92 } 93 94 void getValueGood(Sample samp, int index) { 95 try { 96 Value val = samp.arrRef.getValue(index); 97 int ival = ((IntegerValue)val).value(); 98 if (ival != samp.expected[index]) { 99 failure("FAIL - " + samp.name + 100 ".getValue(" + index + ") - wrong value=" + ival); 101 } else { 102 println("pass - " + samp.name + 103 ".getValue(" + index + ") - value=" + ival); 104 } 105 } catch (Throwable exc) { 106 failure("FAIL - " + samp.name + 107 ".getValue(" + index + ") - unexpected: " + exc); 108 } 109 } 110 111 void getValueBad(Sample samp, int index) { 112 try { 113 Value val = samp.arrRef.getValue(index); 114 failure("FAIL - " + samp.name + 115 ".getValue(" + index + ") - no expected exception"); 116 } catch (IndexOutOfBoundsException exc) { 117 println("pass - " + samp.name + 118 ".getValue(" + index + ") - got expected: " + exc); 119 } catch (Throwable exc) { 120 failure("FAIL - " + samp.name + 121 ".getValue(" + index + ") - unexpected: " + exc); 122 } 123 } 124 125 void getValuesGood(Sample samp) { 126 String desc = samp.name + ".getValues()"; 127 try { 128 List vals = samp.arrRef.getValues(); 129 if (vals.size() != samp.expected.length) { 130 failure("FAIL - " + desc + 131 " - wrong size=" + vals.size() + 132 " , expected: " + samp.expected.length); 133 } 134 for (int index = 0; index < vals.size(); ++index) { 135 int ival = ((IntegerValue)vals.get(index)).value(); 136 if (ival != samp.expected[index]) { 137 failure("FAIL - " + desc + 138 " - wrong value=" + ival); 139 return; 140 } 141 } 142 println("pass - " + samp.name + ".getValues())"); 143 } catch (Throwable exc) { 144 failure("FAIL - " + desc + 145 " - unexpected: " + exc); 146 } 147 } 148 149 void getValuesGood(Sample samp, int index, int length) { 150 try { 151 List vals = samp.arrRef.getValues(index, length); 152 if (vals.size() != 153 ((length==-1)? (samp.expected.length - index) : length)) { 154 failure("FAIL - " + samp.name + ".getValues(" + 155 index + ", " + length + ") - wrong size=" + 156 vals.size()); 157 } 158 for (int i = 0; i < vals.size(); ++i) { 159 int ival = ((IntegerValue)vals.get(i)).value(); 160 if (ival != samp.expected[index + i]) { 161 failure("FAIL - " + samp.name + ".getValues(" + 162 index + ", " + length + ") - wrong value=" + 163 ival); 164 return; 165 } 166 } 167 println("pass - " + samp.name + ".getValues(" + 168 index + ", " + length + "))"); 169 } catch (Throwable exc) { 170 failure("FAIL - " + samp.name + ".getValues(" + 171 index + ", " + length + ") - unexpected: " + exc); 172 } 173 } 174 175 void getValuesBad(Sample samp, int index, int length) { 176 try { 177 List vals = samp.arrRef.getValues(index, length); 178 failure("FAIL - " + samp.name + ".getValues(" + 179 index + ", " + length + ") - no expected exception"); 180 } catch (IndexOutOfBoundsException exc) { 181 println("pass - " + samp.name + ".getValue(" + 182 index + ", " + length + ") - got expected: " + exc); 183 } catch (Throwable exc) { 184 failure("FAIL - " + samp.name + ".getValues(" + 185 index + ", " + length + ") - unexpected: " + exc); 186 } 187 } 188 189 void setValueGood(Sample samp, int index, int ival) { 190 try { 191 Value val = vm().mirrorOf(ival); 192 samp.arrRef.setValue(index, val); 193 println("pass - " + samp.name + 194 ".setValue(" + index + ", ..)"); 195 } catch (Throwable exc) { 196 failure("FAIL - " + samp.name + 197 ".setValue(" + index + ",...) - unexpected: " + exc); 198 } 199 } 200 201 void setValueBad(Sample samp, int index, int ival) { 202 try { 203 Value val = vm().mirrorOf(ival); 204 samp.arrRef.setValue(index, val); 205 failure("FAIL - " + samp.name + 206 ".setValue(" + index + ", ..) - no expected exception"); 207 } catch (IndexOutOfBoundsException exc) { 208 println("pass - " + samp.name + 209 ".setValue(" + index + ",...) - got expected: " + exc); 210 } catch (Throwable exc) { 211 failure("FAIL - " + samp.name + 212 ".setValue(" + index + ",...) - unexpected: " + exc); 213 } 214 } 215 216 void setValuesGood(Sample samp, int[] valArray) { 217 String desc = samp.name + ".setValues(" + arr(valArray) + ")"; 218 try { 219 List values = new ArrayList(); 220 for (int i = 0; i < valArray.length; ++i) { 221 Value val = vm().mirrorOf(valArray[i]); 222 values.add(val); 223 } 224 samp.arrRef.setValues(values); 225 println("pass - " + desc); 226 } catch (Throwable exc) { 227 failure("FAIL - " + desc + " - unexpected: " + exc); 228 } 229 } 230 231 void setValuesGood(Sample samp, int index, int[] valArray, 232 int srcInx, int length) { 233 String desc = samp.name + ".setValues(" + index + ", " + 234 arr(valArray) + ", " + srcInx + ", " + length + ")"; 235 try { 236 List values = new ArrayList(); 237 for (int i = 0; i < valArray.length; ++i) { 238 Value val = vm().mirrorOf(valArray[i]); 239 values.add(val); 240 } 241 samp.arrRef.setValues(index, values, srcInx, length); 242 println("pass - " + desc); 243 } catch (Throwable exc) { 244 failure("FAIL - " + desc + " - unexpected: " + exc); 245 } 246 } 247 248 void setValuesBad(Sample samp, int index, int[] valArray, 249 int srcInx, int length) { 250 String desc = samp.name + ".setValues(" + index + ", " + 251 arr(valArray) + ", " + srcInx + ", " + length + ")"; 252 try { 253 List values = new ArrayList(); 254 for (int i = 0; i < valArray.length; ++i) { 255 Value val = vm().mirrorOf(valArray[i]); 256 values.add(val); 257 } 258 samp.arrRef.setValues(index, values, srcInx, length); 259 failure("FAIL - " + desc + " - no expected exception"); 260 } catch (IndexOutOfBoundsException exc) { 261 println("pass - " + desc + " - got expected: " + exc); 262 } catch (Throwable exc) { 263 failure("FAIL - " + desc + " - unexpected: " + exc); 264 } 265 } 266 267 void check(Sample samp, int[] expectArray) { 268 String desc = samp.name + " - check - " + arr(expectArray); 269 270 try { 271 List vals = samp.arrRef.getValues(); 272 if (vals.size() != expectArray.length) { 273 failure("FAIL - " + desc + 274 " - wrong size=" + vals.size() + 275 " , expected: " + expectArray.length); 276 } 277 for (int index = 0; index < vals.size(); ++index) { 278 int ival = ((IntegerValue)vals.get(index)).value(); 279 if (ival != expectArray[index]) { 280 failure("FAIL - " + desc + 281 " - wrong value=" + ival); 282 return; 283 } 284 } 285 println("pass - " + desc); 286 } catch (Throwable exc) { 287 failure("FAIL - " + desc + 288 " - unexpected: " + exc); 289 } 290 } 291 292 /********** test core **********/ 293 294 protected void runTests() throws Exception { 295 /* 296 * Get to the top of main() to determine targetClass 297 */ 298 BreakpointEvent bpe = startToMain("ArrayRangeTarg"); 299 targetClass = bpe.location().declaringType(); 300 Field fullField = targetClass.fieldByName("fullArray"); 301 Field emptyField = targetClass.fieldByName("emptyArray"); 302 ArrayReference emptyAR = (ArrayReference)targetClass.getValue(emptyField); 303 ArrayReference fullAR = (ArrayReference)targetClass.getValue(fullField); 304 Sample full = new Sample("full", fullAR, ArrayRangeTarg.fullArray); 305 Sample empty = new Sample("empty", emptyAR, ArrayRangeTarg.emptyArray); 306 307 getValueGood(full, 0); 308 getValueGood(full, 4); 309 310 // index < 0 311 getValueBad(full, -1); 312 getValueBad(full, -2); 313 getValueBad(empty, -1); 314 getValueBad(empty, -2); 315 316 // index >= length 317 getValueBad(full, 5); 318 getValueBad(empty, 0); 319 getValueBad(empty, 5); 320 321 getValuesGood(full); 322 getValuesGood(empty); 323 324 getValuesGood(full, 0, 5); 325 getValuesGood(full, 0, 4); 326 getValuesGood(full, 1, 4); 327 getValuesGood(full, 5, 0); 328 getValuesGood(full, 0, 0); 329 getValuesGood(full, 0, -1); 330 getValuesGood(full, 1, -1); 331 getValuesGood(full, 5, -1); 332 333 getValuesGood(empty, 0, 0); 334 getValuesGood(empty, 0, -1); 335 336 // index < 0 337 getValuesBad(full, -1, 0); 338 getValuesBad(full, -1, 3); 339 getValuesBad(full, -1, -1); 340 getValuesBad(empty, -1, 0); 341 getValuesBad(full, -2, 0); 342 getValuesBad(full, -2, 3); 343 getValuesBad(full, -2, -1); 344 getValuesBad(empty, -2, 0); 345 346 // index > length() 347 getValuesBad(full, 6, 0); 348 getValuesBad(full, 6, -1); 349 getValuesBad(empty, 1, 0); 350 getValuesBad(empty, 1, -1); 351 352 // length < 0 353 getValuesBad(full, 0, -2); 354 getValuesBad(empty, 0, -2); 355 356 // index + length > length() 357 getValuesBad(full, 0, 6); 358 getValuesBad(full, 1, 5); 359 getValuesBad(full, 2, 4); 360 getValuesBad(full, 5, 1); 361 getValuesBad(empty, 0, 1); 362 363 setValueGood(full, 0, 55); 364 setValueGood(full, 4, 66); 365 366 // index < 0 367 setValueBad(full, -1, 77); 368 setValueBad(full, -2, 77); 369 370 // index > length() 371 setValueBad(full, 5, 77); 372 setValueBad(full, 6, 77); 373 374 check(full, new int[] {55, 100, 200, 300, 66}); 375 376 // index < 0 377 setValueBad(empty, -1, 77); 378 setValueBad(empty, -2, 77); 379 380 // index > length() 381 setValueBad(empty, 0, 77); 382 setValueBad(empty, 1, 77); 383 384 setValuesGood(full, new int[] {40, 41, 42}); 385 setValuesGood(full, new int[] {}); 386 387 check(full, new int[] {40, 41, 42, 300, 66}); 388 389 setValuesGood(full, new int[] {99, 51, 52, 53, 54, 55}); 390 setValuesGood(full, new int[] {50}); 391 392 check(full, new int[] {50, 51, 52, 53, 54}); 393 394 setValuesGood(empty, new int[] {}); 395 setValuesGood(empty, new int[] {88}); 396 397 setValuesGood(full, 2, new int[] {30, 31, 32, 33, 34, 35}, 0, 3); 398 setValuesGood(full, 0, new int[] {80}, 0, 1); 399 400 check(full, new int[] {80, 51, 30, 31, 32}); 401 402 setValuesGood(full, 0, new int[] {90, 91, 92, 93, 94, 95}, 3, 3); 403 setValuesGood(full, 4, new int[] {81}, 0, 1); 404 405 check(full, new int[] {93, 94, 95, 31, 81}); 406 407 setValuesGood(full, 3, new int[] {60, 61, 62, 63}, 0, -1); 408 setValuesGood(full, 0, new int[] {82}, 0, -1); 409 410 check(full, new int[] {82, 94, 95, 60, 61}); 411 412 setValuesGood(full, 3, new int[] {20, 21, 22, 23}, 1, -1); 413 setValuesGood(full, 1, new int[] {83, 84}, 1, -1); 414 setValuesGood(full, 1, new int[] {}, 0, -1); 415 setValuesGood(full, 2, new int[] {}, 0, 0); 416 setValuesGood(full, 3, new int[] {99}, 0, 0); 417 setValuesGood(full, 4, new int[] {99, 98}, 1, 0); 418 419 check(full, new int[] {82, 84, 95, 21, 22}); 420 421 setValuesGood(empty, 0, new int[] {}, 0, -1); 422 setValuesGood(empty, 0, new int[] {}, 0, 0); 423 setValuesGood(empty, 0, new int[] {99}, 0, 0); 424 setValuesGood(empty, 0, new int[] {99, 98}, 1, 0); 425 426 // index < 0 427 setValuesBad(full, -1, new int[] {30, 31, 32, 33, 34, 35}, 0, 0); 428 setValuesBad(full, -1, new int[] {30, 31, 32, 33, 34, 35}, 0, -1); 429 setValuesBad(full, -2, new int[] {30, 31, 32, 33, 34, 35}, 0, -1); 430 setValuesBad(empty, -1, new int[] {}, 0, 0); 431 setValuesBad(empty, -2, new int[] {}, 0, 0); 432 433 // index > length() 434 setValuesBad(full, 6, new int[] {30, 31, 32, 33, 34, 35}, 0, 1); 435 setValuesBad(full, 6, new int[] {30, 31, 32, 33, 34, 35}, 0, -1); 436 setValuesBad(empty, 1, new int[] {4}, 0, 0); 437 setValuesBad(empty, 1, new int[] {}, 0, 0); 438 setValuesBad(empty, 1, new int[] {}, 0, -1); 439 440 // srcIndex < 0 441 setValuesBad(full, 0, new int[] {90, 91, 92, 93, 94, 95}, -1, 3); 442 setValuesBad(full, 0, new int[] {90, 91, 92, 93, 94, 95}, -1, 0); 443 setValuesBad(full, 0, new int[] {90, 91, 92, 93, 94, 95}, -1, -1); 444 setValuesBad(full, 0, new int[] {90, 91, 92, 93, 94, 95}, -2, -1); 445 setValuesBad(full, 1, new int[] {}, -1, -1); 446 setValuesBad(full, 2, new int[] {}, -1, 0); 447 setValuesBad(empty, 0, new int[] {}, -1, 0); 448 449 // srcIndex > values.size() 450 setValuesBad(full, 0, new int[] {81}, 2, 0); 451 setValuesBad(full, 0, new int[] {81}, 2, 1); 452 setValuesBad(full, 0, new int[] {81}, 2, -1); 453 setValuesBad(full, 4, new int[] {}, 1, 0); 454 setValuesBad(full, 1, new int[] {}, 1, -1); 455 setValuesBad(full, 2, new int[] {}, 1, 0); 456 setValuesBad(empty, 0, new int[] {}, 1, 0); 457 setValuesBad(empty, 0, new int[] {5}, 2, 0); 458 459 // length < 0 (length != -1) 460 setValuesBad(full, 3, new int[] {60, 61, 62, 63}, 0, -2); 461 setValuesBad(full, 3, new int[] {}, 0, -2); 462 463 // index + length > length() 464 setValuesBad(full, 0, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 6); 465 setValuesBad(full, 1, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 5); 466 setValuesBad(full, 2, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 4); 467 setValuesBad(full, 3, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 3); 468 setValuesBad(full, 4, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 2); 469 setValuesBad(full, 5, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 1); 470 setValuesBad(full, 6, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 0); 471 setValuesBad(full, 2, new int[] {20, 21, 22, 23, 24, 25, 26}, 1, 4); 472 setValuesBad(full, 3, new int[] {20, 21, 22, 23, 24, 25, 26}, 1, 3); 473 setValuesBad(full, 4, new int[] {20, 21, 22, 23, 24, 25, 26}, 2, 2); 474 setValuesBad(full, 5, new int[] {20, 21, 22, 23, 24, 25, 26}, 3, 1); 475 setValuesBad(full, 6, new int[] {20, 21, 22, 23, 24, 25, 26}, 4, 0); 476 setValuesBad(empty, 0, new int[] {6}, 0, 1); 477 478 // srcIndex + length > values.size() 479 setValuesBad(full, 0, new int[] {82}, 0, 2); 480 setValuesBad(full, 0, new int[] {82}, 1, 1); 481 setValuesBad(full, 0, new int[] {82}, 2, 0); 482 setValuesBad(full, 0, new int[] {20, 21, 22}, 0, 4); 483 setValuesBad(full, 0, new int[] {20, 21, 22}, 1, 3); 484 setValuesBad(full, 0, new int[] {20, 21, 22}, 2, 2); 485 setValuesBad(full, 0, new int[] {20, 21, 22}, 3, 1); 486 setValuesBad(full, 0, new int[] {20, 21, 22}, 4, 0); 487 488 check(full, new int[] {82, 84, 95, 21, 22}); 489 490 /* 491 * resume the target until end 492 */ 493 listenUntilVMDisconnect(); 494 495 /* 496 * deal with results of test 497 * if anything has called failure("foo") testFailed will be true 498 */ 499 if (!testFailed) { 500 println("ArrayRangeTest: passed"); 501 } else { 502 throw new Exception("ArrayRangeTest: failed"); 503 } 504 } 505 }