1 4 package com.tctest; 5 6 import org.apache.commons.io.IOUtils; 7 8 import com.tc.asm.ClassAdapter; 9 import com.tc.asm.ClassReader; 10 import com.tc.asm.ClassVisitor; 11 import com.tc.asm.MethodAdapter; 12 import com.tc.asm.MethodVisitor; 13 import com.tc.asm.Opcodes; 14 import com.tc.asm.commons.EmptyVisitor; 15 import com.tc.object.bytecode.hook.impl.JavaLangArrayHelpers; 16 import com.tc.test.TCTestCase; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.util.HashSet ; 21 import java.util.Set ; 22 23 public class InstrumentedJavaLangStringTest extends TCTestCase { 24 25 public InstrumentedJavaLangStringTest() { 26 } 28 29 public void testGetBytes() { 30 String s = "Timmy Teck"; 31 byte[] b = new byte[4]; 32 s.getBytes(6, 10, b, 0); 33 assertEquals((byte) 'T', b[0]); 34 assertEquals((byte) 'e', b[1]); 35 assertEquals((byte) 'c', b[2]); 36 assertEquals((byte) 'k', b[3]); 37 } 38 39 public void testGetChars() { 40 String s = "Timmy Teck"; 41 char[] c = new char[s.length() + 1]; 42 s.getChars(0, 5, c, 2); 43 assertEquals((char) 0, c[0]); 44 assertEquals((char) 0, c[1]); 45 assertEquals('T', c[2]); 46 assertEquals('i', c[3]); 47 assertEquals('m', c[4]); 48 assertEquals('m', c[5]); 49 assertEquals('y', c[6]); 50 51 try { 52 s.getChars(0, 15, c, 0); 53 fail(); 54 } catch (StringIndexOutOfBoundsException e) { 55 } 57 58 try { 59 s.getChars(-1, 3, c, 0); 60 fail(); 61 } catch (StringIndexOutOfBoundsException e) { 62 } 64 65 try { 66 s.getChars(3, 1, c, 0); 67 fail(); 68 } catch (StringIndexOutOfBoundsException e) { 69 } 71 72 } 73 74 public void testStringIsInstrumented() throws IOException { 75 ClassReader reader = new ClassReader(getStringBytes()); 76 StringVisitor visitor = new StringVisitor(new EmptyVisitor()); 77 reader.accept(visitor, false); 78 79 assertEquals(visitor.verified.toString(), 2, visitor.verified.size()); 80 assertTrue(visitor.verified.contains("getBytes")); 81 assertTrue(visitor.verified.contains("getChars")); 82 } 83 84 private byte[] getStringBytes() throws IOException { 85 InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream( 86 String .class.getName().replace('.', '/') 87 .concat(".class")); 88 return IOUtils.toByteArray(is); 89 } 90 91 private static class StringVisitor extends ClassAdapter { 92 93 private final Set verified = new HashSet (); 94 95 public StringVisitor(ClassVisitor cv) { 96 super(cv); 97 } 98 99 public MethodVisitor visitMethod(int access, String name, String desc, String signature, String [] exceptions) { 100 if ((name.equals("getChars") && "(II[CI)V".equals(desc)) || (name.equals("getBytes") && desc.equals("(II[BI)V"))) { 101 return new VerifyArrayManagerAccess(super.visitMethod(access, name, desc, signature, exceptions), name); 103 } 104 return null; 105 } 106 107 private class VerifyArrayManagerAccess extends MethodAdapter { 108 109 private final String method; 110 111 public VerifyArrayManagerAccess(MethodVisitor mv, String method) { 112 super(mv); 113 this.method = method; 114 } 115 116 public void visitMethodInsn(int opcode, String owner, String name, String desc) { 117 if ((opcode == Opcodes.INVOKESTATIC) && JavaLangArrayHelpers.CLASS.equals(owner)) { 118 verified.add(method); 119 } 120 } 121 122 } 123 124 } 125 126 } 127
| Popular Tags
|