Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Vladimir Marko | 920506d | 2014-11-18 14:47:31 +0000 | [diff] [blame] | 17 | import java.lang.reflect.InvocationTargetException; |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 18 | import java.lang.reflect.Method; |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 19 | import java.lang.reflect.Modifier; |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 20 | import java.util.LinkedList; |
| 21 | import java.util.List; |
| 22 | |
| 23 | /** |
| 24 | * Smali excercise. |
| 25 | */ |
| 26 | public class Main { |
| 27 | |
| 28 | private static class TestCase { |
| 29 | public TestCase(String testName, String testClass, String testMethodName, Object[] values, |
| 30 | Throwable expectedException, Object expectedReturn) { |
| 31 | this.testName = testName; |
| 32 | this.testClass = testClass; |
| 33 | this.testMethodName = testMethodName; |
| 34 | this.values = values; |
| 35 | this.expectedException = expectedException; |
| 36 | this.expectedReturn = expectedReturn; |
| 37 | } |
| 38 | |
| 39 | String testName; |
| 40 | String testClass; |
| 41 | String testMethodName; |
| 42 | Object[] values; |
| 43 | Throwable expectedException; |
| 44 | Object expectedReturn; |
| 45 | } |
| 46 | |
| 47 | private List<TestCase> testCases; |
| 48 | |
| 49 | public Main() { |
| 50 | // Create the test cases. |
| 51 | testCases = new LinkedList<TestCase>(); |
buzbee | 6489d22 | 2014-11-25 10:52:19 -0800 | [diff] [blame] | 52 | testCases.add(new TestCase("PackedSwitch", "PackedSwitch", "packedSwitch", |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame^] | 53 | new Object[]{123}, null, 123)); |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 54 | |
| 55 | testCases.add(new TestCase("b/17790197", "B17790197", "getInt", null, null, 100)); |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame^] | 56 | testCases.add(new TestCase("b/17978759", "B17978759", "test", null, new VerifyError(), |
| 57 | null)); |
Stephen Kyle | 8fe0e35 | 2014-10-16 15:02:42 +0100 | [diff] [blame] | 58 | testCases.add(new TestCase("FloatBadArgReg", "FloatBadArgReg", "getInt", |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame^] | 59 | new Object[]{100}, null, 100)); |
nikolay serdjuk | d24c934 | 2014-11-10 16:53:27 +0700 | [diff] [blame] | 60 | testCases.add(new TestCase("negLong", "negLong", "negLong", null, null, 122142L)); |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 61 | testCases.add(new TestCase("sameFieldNames", "sameFieldNames", "getInt", null, null, 7)); |
Vladimir Marko | 920506d | 2014-11-18 14:47:31 +0000 | [diff] [blame] | 62 | testCases.add(new TestCase("b/18380491", "B18380491ConcreteClass", "foo", |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame^] | 63 | new Object[]{42}, null, 42)); |
Vladimir Marko | 920506d | 2014-11-18 14:47:31 +0000 | [diff] [blame] | 64 | testCases.add(new TestCase("invoke-super abstract", "B18380491ConcreteClass", "foo", |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame^] | 65 | new Object[]{0}, new AbstractMethodError(), null)); |
| 66 | testCases.add(new TestCase("BadCaseInOpRegRegReg", "BadCaseInOpRegRegReg", "getInt", null, |
| 67 | null, 2)); |
Nicolas Geoffray | 32b2a52 | 2014-11-27 14:54:18 +0000 | [diff] [blame] | 68 | testCases.add(new TestCase("CmpLong", "CmpLong", "run", null, null, 0)); |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame^] | 69 | testCases.add(new TestCase("FloatIntConstPassing", "FloatIntConstPassing", "run", null, |
| 70 | null, 2)); |
Vladimir Marko | 341e425 | 2014-12-19 10:29:51 +0000 | [diff] [blame] | 71 | testCases.add(new TestCase("b/18718277", "B18718277", "getInt", null, null, 0)); |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame^] | 72 | testCases.add(new TestCase("b/18800943 (1)", "B18800943_1", "n_a", null, new VerifyError(), |
| 73 | 0)); |
| 74 | testCases.add(new TestCase("b/18800943 (2)", "B18800943_2", "n_a", null, new VerifyError(), |
| 75 | 0)); |
| 76 | testCases.add(new TestCase("MoveExc", "MoveExc", "run", null, new ArithmeticException(), |
| 77 | null)); |
| 78 | testCases.add(new TestCase("MoveExceptionOnEntry", "MoveExceptionOnEntry", |
| 79 | "moveExceptionOnEntry", new Object[]{0}, new VerifyError(), null)); |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | public void runTests() { |
| 83 | for (TestCase tc : testCases) { |
| 84 | System.out.println(tc.testName); |
| 85 | try { |
| 86 | runTest(tc); |
| 87 | } catch (Exception exc) { |
| 88 | exc.printStackTrace(System.out); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | private void runTest(TestCase tc) throws Exception { |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 94 | Exception errorReturn = null; |
| 95 | try { |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 96 | Class<?> c = Class.forName(tc.testClass); |
| 97 | |
| 98 | Method[] methods = c.getDeclaredMethods(); |
| 99 | |
| 100 | // For simplicity we assume that test methods are not overloaded. So searching by name |
| 101 | // will give us the method we need to run. |
| 102 | Method method = null; |
| 103 | for (Method m : methods) { |
| 104 | if (m.getName().equals(tc.testMethodName)) { |
| 105 | method = m; |
| 106 | break; |
| 107 | } |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 108 | } |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 109 | |
| 110 | if (method == null) { |
| 111 | errorReturn = new IllegalArgumentException("Could not find test method " + |
| 112 | tc.testMethodName + " in class " + |
| 113 | tc.testClass + " for test " + |
| 114 | tc.testName); |
| 115 | } else { |
| 116 | Object retValue; |
| 117 | if (Modifier.isStatic(method.getModifiers())) { |
| 118 | retValue = method.invoke(null, tc.values); |
| 119 | } else { |
| 120 | retValue = method.invoke(method.getDeclaringClass().newInstance(), tc.values); |
| 121 | } |
| 122 | if (tc.expectedException != null) { |
| 123 | errorReturn = new IllegalStateException("Expected an exception in test " + |
| 124 | tc.testName); |
| 125 | } |
| 126 | if (tc.expectedReturn == null && retValue != null) { |
| 127 | errorReturn = new IllegalStateException("Expected a null result in test " + |
| 128 | tc.testName); |
| 129 | } else if (tc.expectedReturn != null && |
| 130 | (retValue == null || !tc.expectedReturn.equals(retValue))) { |
| 131 | errorReturn = new IllegalStateException("Expected return " + |
| 132 | tc.expectedReturn + |
| 133 | ", but got " + retValue); |
| 134 | } else { |
| 135 | // Expected result, do nothing. |
| 136 | } |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 137 | } |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 138 | } catch (Throwable exc) { |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 139 | if (tc.expectedException == null) { |
| 140 | errorReturn = new IllegalStateException("Did not expect exception", exc); |
Vladimir Marko | 920506d | 2014-11-18 14:47:31 +0000 | [diff] [blame] | 141 | } else if (exc instanceof InvocationTargetException && exc.getCause() != null && |
| 142 | exc.getCause().getClass().equals(tc.expectedException.getClass())) { |
| 143 | // Expected exception is wrapped in InvocationTargetException. |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 144 | } else if (!tc.expectedException.getClass().equals(exc.getClass())) { |
| 145 | errorReturn = new IllegalStateException("Expected " + |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 146 | tc.expectedException.getClass().getName() + |
| 147 | ", but got " + exc.getClass(), exc); |
| 148 | } else { |
| 149 | // Expected exception, do nothing. |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 150 | } |
| 151 | } finally { |
| 152 | if (errorReturn != null) { |
| 153 | throw errorReturn; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | public static void main(String[] args) throws Exception { |
| 159 | Main main = new Main(); |
| 160 | |
| 161 | main.runTests(); |
| 162 | |
| 163 | System.out.println("Done!"); |
| 164 | } |
| 165 | } |