1 30 package org.objectweb.asm.util; 31 32 import java.io.PrintWriter ; 33 34 import junit.framework.TestCase; 35 36 import org.objectweb.asm.AnnotationVisitor; 37 import org.objectweb.asm.ClassReader; 38 import org.objectweb.asm.ClassVisitor; 39 import org.objectweb.asm.ClassWriter; 40 import org.objectweb.asm.FieldVisitor; 41 import org.objectweb.asm.Label; 42 import org.objectweb.asm.MethodVisitor; 43 import org.objectweb.asm.Opcodes; 44 import org.objectweb.asm.attrs.Comment; 45 import org.objectweb.asm.commons.EmptyVisitor; 46 47 public class CheckClassAdapterUnitTest extends TestCase implements Opcodes { 48 49 public void testCheckClassVisitor() throws Exception { 50 String s = getClass().getName(); 51 CheckClassAdapter.main(new String [0]); 52 CheckClassAdapter.main(new String [] { s }); 53 CheckClassAdapter.main(new String [] { "output/test/cases/Interface.class" }); 54 } 55 56 public void testVerifyValidClass() throws Exception { 57 ClassReader cr = new ClassReader(getClass().getName()); 58 CheckClassAdapter.verify(cr, true, new PrintWriter (System.err)); 59 } 60 61 public void testVerifyInvalidClass() { 62 ClassWriter cw = new ClassWriter(0); 63 cw.visit(V1_1, ACC_PUBLIC, "C", null, "java/lang/Object", null); 64 MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "m", "()V", null, null); 65 mv.visitCode(); 66 mv.visitVarInsn(ALOAD, 0); 67 mv.visitVarInsn(ISTORE, 30); 68 mv.visitInsn(RETURN); 69 mv.visitMaxs(1, 31); 70 mv.visitEnd(); 71 cw.visitEnd(); 72 ClassReader cr = new ClassReader(cw.toByteArray()); 73 CheckClassAdapter.verify(cr, true, new PrintWriter (System.err)); 74 } 75 76 public void testIllegalClassAccessFlag() { 77 ClassVisitor cv = new CheckClassAdapter(new EmptyVisitor()); 78 try { 79 cv.visit(V1_1, 1 << 20, "C", null, "java/lang/Object", null); 80 fail(); 81 } catch (Exception e) { 82 } 83 } 84 85 public void testIllegalSuperClass() { 86 ClassVisitor cv = new CheckClassAdapter(new EmptyVisitor()); 87 try { 88 cv.visit(V1_1, 89 ACC_PUBLIC, 90 "java/lang/Object", 91 null, 92 "java/lang/Object", 93 null); 94 fail(); 95 } catch (Exception e) { 96 } 97 } 98 99 public void testIllegalInterfaceSuperClass() { 100 ClassVisitor cv = new CheckClassAdapter(new EmptyVisitor()); 101 try { 102 cv.visit(V1_1, ACC_INTERFACE, "I", null, "C", null); 103 fail(); 104 } catch (Exception e) { 105 } 106 } 107 108 public void testIllegalClassAccessFlagSet() { 109 ClassVisitor cv = new CheckClassAdapter(new EmptyVisitor()); 110 try { 111 cv.visit(V1_1, 112 ACC_FINAL + ACC_ABSTRACT, 113 "C", 114 null, 115 "java/lang/Object", 116 null); 117 fail(); 118 } catch (Exception e) { 119 } 120 } 121 122 public void testIllegalClassMemberVisitBeforeStart() { 123 ClassVisitor cv = new CheckClassAdapter(new EmptyVisitor()); 124 try { 125 cv.visitSource(null, null); 126 fail(); 127 } catch (Exception e) { 128 } 129 } 130 131 public void testIllegalClassAttribute() { 132 ClassVisitor cv = new CheckClassAdapter(new EmptyVisitor()); 133 cv.visit(V1_1, ACC_PUBLIC, "C", null, "java/lang/Object", null); 134 try { 135 cv.visitAttribute(null); 136 fail(); 137 } catch (Exception e) { 138 } 139 } 140 141 public void testIllegalMultipleVisitCalls() { 142 ClassVisitor cv = new CheckClassAdapter(new EmptyVisitor()); 143 cv.visit(V1_1, ACC_PUBLIC, "C", null, "java/lang/Object", null); 144 try { 145 cv.visit(V1_1, ACC_PUBLIC, "C", null, "java/lang/Object", null); 146 fail(); 147 } catch (Exception e) { 148 } 149 } 150 151 public void testIllegalMultipleVisitSourceCalls() { 152 ClassVisitor cv = new CheckClassAdapter(new EmptyVisitor()); 153 cv.visit(V1_1, ACC_PUBLIC, "C", null, "java/lang/Object", null); 154 cv.visitSource(null, null); 155 try { 156 cv.visitSource(null, null); 157 fail(); 158 } catch (Exception e) { 159 } 160 } 161 162 public void testIllegalOuterClassName() { 163 ClassVisitor cv = new CheckClassAdapter(new EmptyVisitor()); 164 cv.visit(V1_1, ACC_PUBLIC, "C", null, "java/lang/Object", null); 165 try { 166 cv.visitOuterClass(null, null, null); 167 fail(); 168 } catch (Exception e) { 169 } 170 } 171 172 public void testIllegalMultipleVisitOuterClassCalls() { 173 ClassVisitor cv = new CheckClassAdapter(new EmptyVisitor()); 174 cv.visit(V1_1, ACC_PUBLIC, "C", null, "java/lang/Object", null); 175 cv.visitOuterClass("name", null, null); 176 try { 177 cv.visitOuterClass(null, null, null); 178 fail(); 179 } catch (Exception e) { 180 } 181 } 182 183 public void testIllegalFieldAccessFlagSet() { 184 ClassVisitor cv = new CheckClassAdapter(new EmptyVisitor()); 185 cv.visit(V1_1, ACC_PUBLIC, "C", null, "java/lang/Object", null); 186 try { 187 cv.visitField(ACC_PUBLIC + ACC_PRIVATE, "i", "I", null, null); 188 fail(); 189 } catch (Exception e) { 190 } 191 } 192 193 public void testIllegalClassMemberVisitAfterEnd() { 194 ClassVisitor cv = new CheckClassAdapter(new EmptyVisitor()); 195 cv.visit(V1_1, ACC_PUBLIC, "C", null, "java/lang/Object", null); 196 cv.visitEnd(); 197 try { 198 cv.visitSource(null, null); 199 fail(); 200 } catch (Exception e) { 201 } 202 } 203 204 public void testIllegalFieldMemberVisitAfterEnd() { 205 FieldVisitor fv = new CheckFieldAdapter(new EmptyVisitor()); 206 fv.visitEnd(); 207 try { 208 fv.visitAttribute(new Comment()); 209 fail(); 210 } catch (Exception e) { 211 } 212 } 213 214 public void testIllegalFieldAttribute() { 215 FieldVisitor fv = new CheckFieldAdapter(new EmptyVisitor()); 216 try { 217 fv.visitAttribute(null); 218 fail(); 219 } catch (Exception e) { 220 } 221 } 222 223 public void testIllegalAnnotationDesc() { 224 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 225 try { 226 mv.visitParameterAnnotation(0, "'", true); 227 fail(); 228 } catch (Exception e) { 229 } 230 } 231 232 public void testIllegalAnnotationName() { 233 AnnotationVisitor av = new CheckAnnotationAdapter(new EmptyVisitor()); 234 try { 235 av.visit(null, new Integer (0)); 236 fail(); 237 } catch (Exception e) { 238 } 239 } 240 241 public void testIllegalAnnotationValue() { 242 AnnotationVisitor av = new CheckAnnotationAdapter(new EmptyVisitor()); 243 try { 244 av.visit("name", new Object ()); 245 fail(); 246 } catch (Exception e) { 247 } 248 } 249 250 public void testIllegalAnnotationEnumValue() { 251 AnnotationVisitor av = new CheckAnnotationAdapter(new EmptyVisitor()); 252 try { 253 av.visitEnum("name", "Lpkg/Enum;", null); 254 fail(); 255 } catch (Exception e) { 256 } 257 } 258 259 public void testIllegalAnnotationValueAfterEnd() { 260 AnnotationVisitor av = new CheckAnnotationAdapter(new EmptyVisitor()); 261 av.visitEnd(); 262 try { 263 av.visit("name", new Integer (0)); 264 fail(); 265 } catch (Exception e) { 266 } 267 } 268 269 public void testIllegalMethodMemberVisitAfterEnd() { 270 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 271 mv.visitEnd(); 272 try { 273 mv.visitAttribute(new Comment()); 274 fail(); 275 } catch (Exception e) { 276 } 277 } 278 279 public void testIllegalMethodAttribute() { 280 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 281 try { 282 mv.visitAttribute(null); 283 fail(); 284 } catch (Exception e) { 285 } 286 } 287 288 public void testIllegalMethodInsnVisitBeforeStart() { 289 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 290 try { 291 mv.visitInsn(NOP); 292 fail(); 293 } catch (Exception e) { 294 } 295 } 296 297 public void testIllegalFrameType() { 298 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 299 mv.visitCode(); 300 try { 301 mv.visitFrame(123, 0, null, 0, null); 302 fail(); 303 } catch (Exception e) { 304 } 305 } 306 307 public void testIllegalFrameLocalCount() { 308 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 309 mv.visitCode(); 310 try { 311 mv.visitFrame(F_SAME, 1, new Object [] { INTEGER }, 0, null); 312 fail(); 313 } catch (Exception e) { 314 } 315 } 316 317 public void testIllegalFrameStackCount() { 318 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 319 mv.visitCode(); 320 try { 321 mv.visitFrame(F_SAME, 0, null, 1, new Object [] { INTEGER }); 322 fail(); 323 } catch (Exception e) { 324 } 325 } 326 327 public void testIllegalFrameLocalArray() { 328 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 329 mv.visitCode(); 330 try { 331 mv.visitFrame(F_APPEND, 1, new Object [0], 0, null); 332 fail(); 333 } catch (Exception e) { 334 } 335 } 336 337 public void testIllegalFrameStackArray() { 338 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 339 mv.visitCode(); 340 try { 341 mv.visitFrame(F_SAME1, 0, null, 1, new Object [0]); 342 fail(); 343 } catch (Exception e) { 344 } 345 } 346 347 public void testIllegalMethodInsn() { 348 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 349 mv.visitCode(); 350 try { 351 mv.visitInsn(-1); 352 fail(); 353 } catch (Exception e) { 354 } 355 } 356 357 public void testIllegalByteInsnOperand() { 358 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 359 mv.visitCode(); 360 try { 361 mv.visitIntInsn(BIPUSH, Integer.MAX_VALUE); 362 fail(); 363 } catch (Exception e) { 364 } 365 } 366 367 public void testIllegalShortInsnOperand() { 368 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 369 mv.visitCode(); 370 try { 371 mv.visitIntInsn(SIPUSH, Integer.MAX_VALUE); 372 fail(); 373 } catch (Exception e) { 374 } 375 } 376 377 public void testIllegalVarInsnOperand() { 378 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 379 mv.visitCode(); 380 try { 381 mv.visitVarInsn(ALOAD, -1); 382 fail(); 383 } catch (Exception e) { 384 } 385 } 386 387 public void testIllegalIntInsnOperand() { 388 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 389 mv.visitCode(); 390 try { 391 mv.visitIntInsn(NEWARRAY, 0); 392 fail(); 393 } catch (Exception e) { 394 } 395 } 396 397 public void testIllegalTypeInsnOperand() { 398 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 399 mv.visitCode(); 400 try { 401 mv.visitTypeInsn(NEW, "[I"); 402 fail(); 403 } catch (Exception e) { 404 } 405 } 406 407 public void testIllegalLabelInsnOperand() { 408 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 409 mv.visitCode(); 410 Label l = new Label(); 411 mv.visitLabel(l); 412 try { 413 mv.visitLabel(l); 414 fail(); 415 } catch (Exception e) { 416 } 417 } 418 419 public void testIllegalTableSwitchParameters1() { 420 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 421 mv.visitCode(); 422 try { 423 mv.visitTableSwitchInsn(1, 0, new Label(), new Label[0]); 424 fail(); 425 } catch (Exception e) { 426 } 427 } 428 429 public void testIllegalTableSwitchParameters2() { 430 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 431 mv.visitCode(); 432 try { 433 mv.visitTableSwitchInsn(0, 1, null, new Label[0]); 434 fail(); 435 } catch (Exception e) { 436 } 437 } 438 439 public void testIllegalTableSwitchParameters3() { 440 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 441 mv.visitCode(); 442 try { 443 mv.visitTableSwitchInsn(0, 1, new Label(), null); 444 fail(); 445 } catch (Exception e) { 446 } 447 } 448 449 public void testIllegalTableSwitchParameters4() { 450 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 451 mv.visitCode(); 452 try { 453 mv.visitTableSwitchInsn(0, 1, new Label(), new Label[0]); 454 fail(); 455 } catch (Exception e) { 456 } 457 } 458 459 public void testIllegalLookupSwitchParameters1() { 460 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 461 mv.visitCode(); 462 try { 463 mv.visitLookupSwitchInsn(new Label(), null, new Label[0]); 464 fail(); 465 } catch (Exception e) { 466 } 467 } 468 469 public void testIllegalLookupSwitchParameters2() { 470 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 471 mv.visitCode(); 472 try { 473 mv.visitLookupSwitchInsn(new Label(), new int[0], null); 474 fail(); 475 } catch (Exception e) { 476 } 477 } 478 479 public void testIllegalLookupSwitchParameters3() { 480 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 481 mv.visitCode(); 482 try { 483 mv.visitLookupSwitchInsn(new Label(), new int[0], new Label[1]); 484 fail(); 485 } catch (Exception e) { 486 } 487 } 488 489 public void testIllegalFieldInsnNullOwner() { 490 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 491 mv.visitCode(); 492 try { 493 mv.visitFieldInsn(GETFIELD, null, "i", "I"); 494 fail(); 495 } catch (Exception e) { 496 } 497 } 498 499 public void testIllegalFieldInsnOwner() { 500 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 501 mv.visitCode(); 502 try { 503 mv.visitFieldInsn(GETFIELD, "-", "i", "I"); 504 fail(); 505 } catch (Exception e) { 506 } 507 } 508 509 public void testIllegalFieldInsnNullName() { 510 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 511 mv.visitCode(); 512 try { 513 mv.visitFieldInsn(GETFIELD, "C", null, "I"); 514 fail(); 515 } catch (Exception e) { 516 } 517 } 518 519 public void testIllegalFieldInsnName() { 520 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 521 mv.visitCode(); 522 try { 523 mv.visitFieldInsn(GETFIELD, "C", "-", "I"); 524 fail(); 525 } catch (Exception e) { 526 } 527 } 528 529 public void testIllegalFieldInsnName2() { 530 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 531 mv.visitCode(); 532 533 try { 534 mv.visitFieldInsn(GETFIELD, "C", "a-", "I"); 535 fail(); 536 } catch (Exception e) { 537 } 538 } 539 540 public void testIllegalFieldInsnNullDesc() { 541 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 542 mv.visitCode(); 543 try { 544 mv.visitFieldInsn(GETFIELD, "C", "i", null); 545 fail(); 546 } catch (Exception e) { 547 } 548 } 549 550 public void testIllegalFieldInsnVoidDesc() { 551 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 552 mv.visitCode(); 553 try { 554 mv.visitFieldInsn(GETFIELD, "C", "i", "V"); 555 fail(); 556 } catch (Exception e) { 557 } 558 } 559 560 public void testIllegalFieldInsnPrimitiveDesc() { 561 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 562 mv.visitCode(); 563 try { 564 mv.visitFieldInsn(GETFIELD, "C", "i", "II"); 565 fail(); 566 } catch (Exception e) { 567 } 568 } 569 570 public void testIllegalFieldInsnArrayDesc() { 571 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 572 mv.visitCode(); 573 try { 574 mv.visitFieldInsn(GETFIELD, "C", "i", "["); 575 fail(); 576 } catch (Exception e) { 577 } 578 } 579 580 public void testIllegalFieldInsnReferenceDesc() { 581 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 582 mv.visitCode(); 583 try { 584 mv.visitFieldInsn(GETFIELD, "C", "i", "L"); 585 fail(); 586 } catch (Exception e) { 587 } 588 } 589 590 public void testIllegalFieldInsnReferenceDesc2() { 591 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 592 mv.visitCode(); 593 try { 594 mv.visitFieldInsn(GETFIELD, "C", "i", "L-;"); 595 fail(); 596 } catch (Exception e) { 597 } 598 } 599 600 public void testIllegalMethodInsnNullName() { 601 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 602 mv.visitCode(); 603 try { 604 mv.visitMethodInsn(INVOKEVIRTUAL, "C", null, "()V"); 605 fail(); 606 } catch (Exception e) { 607 } 608 } 609 610 public void testIllegalMethodInsnName() { 611 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 612 mv.visitCode(); 613 try { 614 mv.visitMethodInsn(INVOKEVIRTUAL, "C", "-", "()V"); 615 fail(); 616 } catch (Exception e) { 617 } 618 } 619 620 public void testIllegalMethodInsnName2() { 621 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 622 mv.visitCode(); 623 try { 624 mv.visitMethodInsn(INVOKEVIRTUAL, "C", "a-", "()V"); 625 fail(); 626 } catch (Exception e) { 627 } 628 } 629 630 public void testIllegalMethodInsnNullDesc() { 631 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 632 mv.visitCode(); 633 try { 634 mv.visitMethodInsn(INVOKEVIRTUAL, "C", "m", null); 635 fail(); 636 } catch (Exception e) { 637 } 638 } 639 640 public void testIllegalMethodInsnDesc() { 641 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 642 mv.visitCode(); 643 try { 644 mv.visitMethodInsn(INVOKEVIRTUAL, "C", "m", "I"); 645 fail(); 646 } catch (Exception e) { 647 } 648 } 649 650 public void testIllegalMethodInsnParameterDesc() { 651 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 652 mv.visitCode(); 653 try { 654 mv.visitMethodInsn(INVOKEVIRTUAL, "C", "m", "(V)V"); 655 fail(); 656 } catch (Exception e) { 657 } 658 } 659 660 public void testIllegalMethodInsnReturnDesc() { 661 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 662 mv.visitCode(); 663 try { 664 mv.visitMethodInsn(INVOKEVIRTUAL, "C", "m", "()VV"); 665 fail(); 666 } catch (Exception e) { 667 } 668 } 669 670 public void testIllegalLdcInsnOperand() { 671 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 672 mv.visitCode(); 673 try { 674 mv.visitLdcInsn(new Object ()); 675 fail(); 676 } catch (Exception e) { 677 } 678 } 679 680 public void testIllegalMultiANewArrayDesc() { 681 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 682 mv.visitCode(); 683 try { 684 mv.visitMultiANewArrayInsn("I", 1); 685 fail(); 686 } catch (Exception e) { 687 } 688 } 689 690 public void testIllegalMultiANewArrayDims() { 691 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 692 mv.visitCode(); 693 try { 694 mv.visitMultiANewArrayInsn("[[I", 0); 695 fail(); 696 } catch (Exception e) { 697 } 698 } 699 700 public void testIllegalMultiANewArrayDims2() { 701 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 702 mv.visitCode(); 703 try { 704 mv.visitMultiANewArrayInsn("[[I", 3); 705 fail(); 706 } catch (Exception e) { 707 } 708 } 709 710 public void testIllegalLocalVariableLabels() { 711 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 712 mv.visitCode(); 713 Label m = new Label(); 714 Label n = new Label(); 715 mv.visitLabel(n); 716 mv.visitInsn(NOP); 717 mv.visitLabel(m); 718 try { 719 mv.visitLocalVariable("i", "I", null, m, n, 0); 720 fail(); 721 } catch (Exception e) { 722 } 723 } 724 725 public void testIllegalLineNumerLabel() { 726 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 727 mv.visitCode(); 728 try { 729 mv.visitLineNumber(0, new Label()); 730 fail(); 731 } catch (Exception e) { 732 } 733 } 734 735 public void testIllegalInsnVisitAfterEnd() { 736 MethodVisitor mv = new CheckMethodAdapter(new EmptyVisitor()); 737 mv.visitCode(); 738 mv.visitMaxs(0, 0); 739 try { 740 mv.visitInsn(NOP); 741 fail(); 742 } catch (Exception e) { 743 } 744 } 745 } 746 | Popular Tags |