1 /* 2 * Copyright 2013, 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.analysis.util; 33 34 import com.google.common.collect.ImmutableList; 35 import com.google.common.collect.ImmutableSet; 36 import junit.framework.Assert; 37 import org.jf.dexlib2.Opcodes; 38 import org.jf.dexlib2.analysis.ClassPath; 39 import org.jf.dexlib2.analysis.DexClassProvider; 40 import org.jf.dexlib2.analysis.TestUtils; 41 import org.jf.dexlib2.analysis.TypeProto; 42 import org.jf.dexlib2.iface.ClassDef; 43 import org.jf.dexlib2.immutable.ImmutableDexFile; 44 import org.junit.Test; 45 46 import java.io.IOException; 47 48 public class SuperclassChainTest { 49 50 @Test 51 public void testGetSuperclassChain() throws IOException { 52 ClassDef objectClassDef = TestUtils.makeClassDef("Ljava/lang/Object;", null); 53 ClassDef oneClassDef = TestUtils.makeClassDef("Ltest/one;", "Ljava/lang/Object;"); 54 ClassDef twoClassDef = TestUtils.makeClassDef("Ltest/two;", "Ltest/one;"); 55 ClassDef threeClassDef = TestUtils.makeClassDef("Ltest/three;", "Ltest/two;"); 56 57 ImmutableSet<ClassDef> classes = ImmutableSet.<ClassDef>of( 58 objectClassDef, oneClassDef, twoClassDef, threeClassDef); 59 60 ClassPath classPath = new ClassPath(new DexClassProvider(new ImmutableDexFile(Opcodes.getDefault(), classes))); 61 62 TypeProto objectClassProto = classPath.getClass("Ljava/lang/Object;"); 63 TypeProto oneClassProto = classPath.getClass("Ltest/one;"); 64 TypeProto twoClassProto = classPath.getClass("Ltest/two;"); 65 TypeProto threeClassProto = classPath.getClass("Ltest/three;"); 66 67 Assert.assertEquals( 68 ImmutableList.<TypeProto>of(), 69 ImmutableList.copyOf(TypeProtoUtils.getSuperclassChain(objectClassProto))); 70 71 Assert.assertEquals( 72 ImmutableList.<TypeProto>of(objectClassProto), 73 ImmutableList.copyOf(TypeProtoUtils.getSuperclassChain(oneClassProto))); 74 75 Assert.assertEquals( 76 ImmutableList.<TypeProto>of(oneClassProto, objectClassProto), 77 ImmutableList.copyOf(TypeProtoUtils.getSuperclassChain(twoClassProto))); 78 79 Assert.assertEquals( 80 ImmutableList.<TypeProto>of(twoClassProto, oneClassProto, objectClassProto), 81 ImmutableList.copyOf(TypeProtoUtils.getSuperclassChain(threeClassProto))); 82 } 83 84 @Test 85 public void testGetSuperclassChain_Unresolved() throws IOException { 86 // Ltest/one; isn't defined 87 88 ClassDef twoClassDef = TestUtils.makeClassDef("Ltest/two;", "Ltest/one;"); 89 ClassDef threeClassDef = TestUtils.makeClassDef("Ltest/three;", "Ltest/two;"); 90 ImmutableSet<ClassDef> classes = ImmutableSet.<ClassDef>of(twoClassDef, threeClassDef); 91 ClassPath classPath = new ClassPath(new DexClassProvider(new ImmutableDexFile(Opcodes.getDefault(), classes))); 92 93 TypeProto unknownClassProto = classPath.getUnknownClass(); 94 TypeProto oneClassProto = classPath.getClass("Ltest/one;"); 95 TypeProto twoClassProto = classPath.getClass("Ltest/two;"); 96 TypeProto threeClassProto = classPath.getClass("Ltest/three;"); 97 98 Assert.assertEquals( 99 ImmutableList.<TypeProto>of(oneClassProto, unknownClassProto), 100 ImmutableList.copyOf(TypeProtoUtils.getSuperclassChain(twoClassProto))); 101 102 Assert.assertEquals( 103 ImmutableList.<TypeProto>of(twoClassProto, oneClassProto, unknownClassProto), 104 ImmutableList.copyOf(TypeProtoUtils.getSuperclassChain(threeClassProto))); 105 } 106 } 107