1 /* 2 * Copyright 2015, Google Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 package org.jf.dexlib2.builder; 33 34 import org.jf.dexlib2.Opcode; 35 import org.jf.dexlib2.builder.instruction.BuilderInstruction10x; 36 import org.jf.dexlib2.builder.instruction.BuilderInstruction32x; 37 import org.jf.dexlib2.iface.MethodImplementation; 38 import org.junit.Assert; 39 import org.junit.Test; 40 41 public class MutableMethodImplementationTest { 42 43 @Test 44 public void testTryEndAtEndOfMethod() { 45 MethodImplementationBuilder builder = new MethodImplementationBuilder(10); 46 47 Label startLabel = builder.addLabel("start"); 48 builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); 49 builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); 50 builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); 51 builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); 52 builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); 53 builder.addInstruction(new BuilderInstruction32x(Opcode.MOVE_16, 0, 0)); 54 Label endLabel = builder.addLabel("end"); 55 56 builder.addCatch(startLabel, endLabel, startLabel); 57 58 MethodImplementation methodImplementation = builder.getMethodImplementation(); 59 60 Assert.assertEquals(0, methodImplementation.getTryBlocks().get(0).getStartCodeAddress()); 61 Assert.assertEquals(8, methodImplementation.getTryBlocks().get(0).getCodeUnitCount()); 62 63 methodImplementation = new MutableMethodImplementation(methodImplementation); 64 65 Assert.assertEquals(0, methodImplementation.getTryBlocks().get(0).getStartCodeAddress()); 66 Assert.assertEquals(8, methodImplementation.getTryBlocks().get(0).getCodeUnitCount()); 67 } 68 69 @Test 70 public void testNewLabelByAddress() { 71 MethodImplementationBuilder builder = new MethodImplementationBuilder(10); 72 73 builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); 74 builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); 75 builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); 76 builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); 77 builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); 78 builder.addInstruction(new BuilderInstruction32x(Opcode.MOVE_16, 0, 0)); 79 80 MutableMethodImplementation mutableMethodImplementation = 81 new MutableMethodImplementation(builder.getMethodImplementation()); 82 83 mutableMethodImplementation.addCatch( 84 mutableMethodImplementation.newLabelForAddress(0), 85 mutableMethodImplementation.newLabelForAddress(8), 86 mutableMethodImplementation.newLabelForAddress(1)); 87 88 Assert.assertEquals(0, mutableMethodImplementation.getTryBlocks().get(0).getStartCodeAddress()); 89 Assert.assertEquals(8, mutableMethodImplementation.getTryBlocks().get(0).getCodeUnitCount()); 90 Assert.assertEquals(1, mutableMethodImplementation.getTryBlocks().get(0).getExceptionHandlers().get(0) 91 .getHandlerCodeAddress()); 92 } 93 94 @Test 95 public void testNewLabelByIndex() { 96 MethodImplementationBuilder builder = new MethodImplementationBuilder(10); 97 98 builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); 99 builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); 100 builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); 101 builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); 102 builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); 103 builder.addInstruction(new BuilderInstruction32x(Opcode.MOVE_16, 0, 0)); 104 105 MutableMethodImplementation mutableMethodImplementation = 106 new MutableMethodImplementation(builder.getMethodImplementation()); 107 108 mutableMethodImplementation.addCatch( 109 mutableMethodImplementation.newLabelForIndex(0), 110 mutableMethodImplementation.newLabelForIndex(6), 111 mutableMethodImplementation.newLabelForIndex(1)); 112 113 Assert.assertEquals(0, mutableMethodImplementation.getTryBlocks().get(0).getStartCodeAddress()); 114 Assert.assertEquals(8, mutableMethodImplementation.getTryBlocks().get(0).getCodeUnitCount()); 115 Assert.assertEquals(1, mutableMethodImplementation.getTryBlocks().get(0).getExceptionHandlers().get(0) 116 .getHandlerCodeAddress()); 117 } 118 } 119