1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package android.startop.test; 16 17 import dalvik.system.PathClassLoader; 18 19 import org.junit.Assert; 20 import org.junit.Test; 21 22 import java.lang.reflect.InvocationTargetException; 23 import java.lang.reflect.Method; 24 25 // Adding tests here requires changes in several other places. See README.md in 26 // the view_compiler directory for more information. 27 public class DexBuilderTest { loadDexFile(String filename)28 static ClassLoader loadDexFile(String filename) throws Exception { 29 return new PathClassLoader("/data/local/tmp/dex-builder-test/" + filename, 30 ClassLoader.getSystemClassLoader()); 31 } 32 hello()33 public void hello() {} 34 35 @Test loadTrivialDex()36 public void loadTrivialDex() throws Exception { 37 ClassLoader loader = loadDexFile("trivial.dex"); 38 loader.loadClass("android.startop.test.testcases.Trivial"); 39 } 40 41 @Test return5()42 public void return5() throws Exception { 43 ClassLoader loader = loadDexFile("simple.dex"); 44 Class clazz = loader.loadClass("android.startop.test.testcases.SimpleTests"); 45 Method method = clazz.getMethod("return5"); 46 Assert.assertEquals(5, method.invoke(null)); 47 } 48 49 @Test returnInteger5()50 public void returnInteger5() throws Exception { 51 ClassLoader loader = loadDexFile("simple.dex"); 52 Class clazz = loader.loadClass("android.startop.test.testcases.SimpleTests"); 53 Method method = clazz.getMethod("returnInteger5"); 54 Assert.assertEquals(5, method.invoke(null)); 55 } 56 57 @Test returnParam()58 public void returnParam() throws Exception { 59 ClassLoader loader = loadDexFile("simple.dex"); 60 Class clazz = loader.loadClass("android.startop.test.testcases.SimpleTests"); 61 Method method = clazz.getMethod("returnParam", int.class); 62 Assert.assertEquals(5, method.invoke(null, 5)); 63 Assert.assertEquals(42, method.invoke(null, 42)); 64 } 65 66 @Test returnStringLength()67 public void returnStringLength() throws Exception { 68 ClassLoader loader = loadDexFile("simple.dex"); 69 Class clazz = loader.loadClass("android.startop.test.testcases.SimpleTests"); 70 Method method = clazz.getMethod("returnStringLength", String.class); 71 Assert.assertEquals(13, method.invoke(null, "Hello, World!")); 72 } 73 74 @Test returnIfZero()75 public void returnIfZero() throws Exception { 76 ClassLoader loader = loadDexFile("simple.dex"); 77 Class clazz = loader.loadClass("android.startop.test.testcases.SimpleTests"); 78 Method method = clazz.getMethod("returnIfZero", int.class); 79 Assert.assertEquals(5, method.invoke(null, 0)); 80 Assert.assertEquals(3, method.invoke(null, 17)); 81 } 82 83 @Test returnIfNotZero()84 public void returnIfNotZero() throws Exception { 85 ClassLoader loader = loadDexFile("simple.dex"); 86 Class clazz = loader.loadClass("android.startop.test.testcases.SimpleTests"); 87 Method method = clazz.getMethod("returnIfNotZero", int.class); 88 Assert.assertEquals(3, method.invoke(null, 0)); 89 Assert.assertEquals(5, method.invoke(null, 17)); 90 } 91 92 @Test backwardsBranch()93 public void backwardsBranch() throws Exception { 94 ClassLoader loader = loadDexFile("simple.dex"); 95 Class clazz = loader.loadClass("android.startop.test.testcases.SimpleTests"); 96 Method method = clazz.getMethod("backwardsBranch"); 97 Assert.assertEquals(2, method.invoke(null)); 98 } 99 100 @Test returnNull()101 public void returnNull() throws Exception { 102 ClassLoader loader = loadDexFile("simple.dex"); 103 Class clazz = loader.loadClass("android.startop.test.testcases.SimpleTests"); 104 Method method = clazz.getMethod("returnNull"); 105 Assert.assertEquals(null, method.invoke(null)); 106 } 107 108 @Test makeString()109 public void makeString() throws Exception { 110 ClassLoader loader = loadDexFile("simple.dex"); 111 Class clazz = loader.loadClass("android.startop.test.testcases.SimpleTests"); 112 Method method = clazz.getMethod("makeString"); 113 Assert.assertEquals("Hello, World!", method.invoke(null)); 114 } 115 116 @Test returnStringIfZeroAB()117 public void returnStringIfZeroAB() throws Exception { 118 ClassLoader loader = loadDexFile("simple.dex"); 119 Class clazz = loader.loadClass("android.startop.test.testcases.SimpleTests"); 120 Method method = clazz.getMethod("returnStringIfZeroAB", int.class); 121 Assert.assertEquals("a", method.invoke(null, 0)); 122 Assert.assertEquals("b", method.invoke(null, 1)); 123 } 124 125 @Test returnStringIfZeroBA()126 public void returnStringIfZeroBA() throws Exception { 127 ClassLoader loader = loadDexFile("simple.dex"); 128 Class clazz = loader.loadClass("android.startop.test.testcases.SimpleTests"); 129 Method method = clazz.getMethod("returnStringIfZeroBA", int.class); 130 Assert.assertEquals("b", method.invoke(null, 0)); 131 Assert.assertEquals("a", method.invoke(null, 1)); 132 } 133 134 @Test invokeStaticReturnObject()135 public void invokeStaticReturnObject() throws Exception { 136 ClassLoader loader = loadDexFile("simple.dex"); 137 Class clazz = loader.loadClass("android.startop.test.testcases.SimpleTests"); 138 Method method = clazz.getMethod("invokeStaticReturnObject", int.class, int.class); 139 Assert.assertEquals("10", method.invoke(null, 10, 10)); 140 Assert.assertEquals("a", method.invoke(null, 10, 16)); 141 Assert.assertEquals("5", method.invoke(null, 5, 16)); 142 } 143 144 @Test invokeVirtualReturnObject()145 public void invokeVirtualReturnObject() throws Exception { 146 ClassLoader loader = loadDexFile("simple.dex"); 147 Class clazz = loader.loadClass("android.startop.test.testcases.SimpleTests"); 148 Method method = clazz.getMethod("invokeVirtualReturnObject", String.class, int.class); 149 Assert.assertEquals("bc", method.invoke(null, "abc", 1)); 150 } 151 152 @Test castObjectToString()153 public void castObjectToString() throws Exception { 154 ClassLoader loader = loadDexFile("simple.dex"); 155 Class clazz = loader.loadClass("android.startop.test.testcases.SimpleTests"); 156 Method method = clazz.getMethod("castObjectToString", Object.class); 157 Assert.assertEquals("abc", method.invoke(null, "abc")); 158 boolean castFailed = false; 159 try { 160 method.invoke(null, 5); 161 } catch (InvocationTargetException e) { 162 if (e.getCause() instanceof ClassCastException) { 163 castFailed = true; 164 } else { 165 throw e; 166 } 167 } 168 Assert.assertTrue(castFailed); 169 } 170 } 171