blob: ba4990a76e8db6044fcd0469ac65b5dce3333a06 [file] [log] [blame]
Andreas Gampe8fda9f22014-10-03 16:15:37 -07001/*
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 Marko920506d2014-11-18 14:47:31 +000017import java.lang.reflect.InvocationTargetException;
Andreas Gampe8fda9f22014-10-03 16:15:37 -070018import java.lang.reflect.Method;
Ian Rogers8e1f4f82014-11-05 11:07:30 -080019import java.lang.reflect.Modifier;
Andreas Gampe8fda9f22014-10-03 16:15:37 -070020import java.util.LinkedList;
21import java.util.List;
22
23/**
24 * Smali excercise.
25 */
26public 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>();
buzbee6489d222014-11-25 10:52:19 -080052 testCases.add(new TestCase("PackedSwitch", "PackedSwitch", "packedSwitch",
Sebastien Hertz270a0e12015-01-16 19:49:09 +010053 new Object[]{123}, null, 123));
Andreas Gampe8fda9f22014-10-03 16:15:37 -070054
55 testCases.add(new TestCase("b/17790197", "B17790197", "getInt", null, null, 100));
Stephen Kyle8fe0e352014-10-16 15:02:42 +010056 testCases.add(new TestCase("FloatBadArgReg", "FloatBadArgReg", "getInt",
Sebastien Hertz270a0e12015-01-16 19:49:09 +010057 new Object[]{100}, null, 100));
nikolay serdjukd24c9342014-11-10 16:53:27 +070058 testCases.add(new TestCase("negLong", "negLong", "negLong", null, null, 122142L));
Vladimir Marko7a7c1db2014-11-17 15:13:34 +000059 testCases.add(new TestCase("sameFieldNames", "sameFieldNames", "getInt", null, null, 7));
Vladimir Marko920506d2014-11-18 14:47:31 +000060 testCases.add(new TestCase("b/18380491", "B18380491ConcreteClass", "foo",
Sebastien Hertz270a0e12015-01-16 19:49:09 +010061 new Object[]{42}, null, 42));
Vladimir Marko920506d2014-11-18 14:47:31 +000062 testCases.add(new TestCase("invoke-super abstract", "B18380491ConcreteClass", "foo",
Sebastien Hertz270a0e12015-01-16 19:49:09 +010063 new Object[]{0}, new AbstractMethodError(), null));
64 testCases.add(new TestCase("BadCaseInOpRegRegReg", "BadCaseInOpRegRegReg", "getInt", null,
65 null, 2));
Nicolas Geoffray32b2a522014-11-27 14:54:18 +000066 testCases.add(new TestCase("CmpLong", "CmpLong", "run", null, null, 0));
Sebastien Hertz270a0e12015-01-16 19:49:09 +010067 testCases.add(new TestCase("FloatIntConstPassing", "FloatIntConstPassing", "run", null,
68 null, 2));
Vladimir Marko341e4252014-12-19 10:29:51 +000069 testCases.add(new TestCase("b/18718277", "B18718277", "getInt", null, null, 0));
Sebastien Hertz270a0e12015-01-16 19:49:09 +010070 testCases.add(new TestCase("b/18800943 (1)", "B18800943_1", "n_a", null, new VerifyError(),
71 0));
72 testCases.add(new TestCase("b/18800943 (2)", "B18800943_2", "n_a", null, new VerifyError(),
73 0));
74 testCases.add(new TestCase("MoveExc", "MoveExc", "run", null, new ArithmeticException(),
75 null));
76 testCases.add(new TestCase("MoveExceptionOnEntry", "MoveExceptionOnEntry",
77 "moveExceptionOnEntry", new Object[]{0}, new VerifyError(), null));
Jeff Hao9ccd1512015-03-20 18:11:45 -070078 testCases.add(new TestCase("EmptySparseSwitch", "EmptySparseSwitch", "run", null, null,
79 null));
Andreas Gampeb588f4c2015-05-26 13:35:39 -070080 testCases.add(new TestCase("b/20224106", "B20224106", "run", null, new VerifyError(),
81 0));
Andreas Gampeda9badb2015-06-05 20:22:12 -070082 testCases.add(new TestCase("b/17410612", "B17410612", "run", null, new VerifyError(),
83 0));
Nicolas Geoffrayea9ef4d2015-06-19 10:05:50 +010084 testCases.add(new TestCase("b/21863767", "B21863767", "run", null, null,
Nicolas Geoffray69505f82015-06-18 18:04:12 +010085 null));
Vladimir Marko2d1a0a42015-06-18 17:40:00 +010086 testCases.add(new TestCase("b/21873167", "B21873167", "test", null, null, null));
Vladimir Markof11c4202015-06-19 12:58:22 +010087 testCases.add(new TestCase("b/21614284", "B21614284", "test", new Object[] { null },
Andreas Gamped12e7822015-06-25 10:26:40 -070088 new NullPointerException(), null));
Jeff Haodde98272015-06-17 16:04:26 -070089 testCases.add(new TestCase("b/21902684", "B21902684", "test", null, null, null));
Andreas Gampea32210c2015-06-24 10:26:13 -070090 testCases.add(new TestCase("b/22045582", "B22045582", "run", null, new VerifyError(),
91 0));
92 testCases.add(new TestCase("b/22045582 (int)", "B22045582Int", "run", null,
93 new VerifyError(), 0));
94 testCases.add(new TestCase("b/22045582 (wide)", "B22045582Wide", "run", null,
95 new VerifyError(), 0));
Vladimir Marko414000e2015-06-23 17:45:21 +010096 testCases.add(new TestCase("b/21886894", "B21886894", "test", null, new VerifyError(),
Andreas Gamped12e7822015-06-25 10:26:40 -070097 null));
98 testCases.add(new TestCase("b/22080519", "B22080519", "run", null,
99 new NullPointerException(), null));
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700100 testCases.add(new TestCase("b/21645819", "B21645819", "run", new Object[] { null },
101 null, null));
Andreas Gampe185a5582015-07-06 14:00:39 -0700102 testCases.add(new TestCase("b/22244733", "B22244733", "run", new Object[] { "abc" },
103 null, "abc"));
Andreas Gampe38536282015-07-08 17:22:57 -0700104 testCases.add(new TestCase("b/22331663", "B22331663", "run", new Object[] { false },
105 null, null));
Andreas Gampe97a1ff32015-07-09 11:30:14 -0700106 testCases.add(new TestCase("b/22331663 (pass)", "B22331663Pass", "run",
107 new Object[] { false }, null, null));
108 testCases.add(new TestCase("b/22331663 (fail)", "B22331663Fail", "run",
109 new Object[] { false }, new VerifyError(), null));
Andreas Gampe891dfaa2015-07-13 21:12:43 -0700110 testCases.add(new TestCase("b/22411633 (1)", "B22411633_1", "run", new Object[] { false },
111 null, null));
112 testCases.add(new TestCase("b/22411633 (2)", "B22411633_2", "run", new Object[] { false },
113 new VerifyError(), null));
114 testCases.add(new TestCase("b/22411633 (3)", "B22411633_3", "run", new Object[] { false },
115 null, null));
116 testCases.add(new TestCase("b/22411633 (4)", "B22411633_4", "run", new Object[] { false },
117 new VerifyError(), null));
118 testCases.add(new TestCase("b/22411633 (5)", "B22411633_5", "run", new Object[] { false },
119 null, null));
Andreas Gampebe2aa442015-07-27 21:41:49 -0700120 testCases.add(new TestCase("b/22777307", "B22777307", "run", null, new InstantiationError(),
121 null));
Andreas Gampe2ea7b702015-08-07 08:07:16 -0700122 testCases.add(new TestCase("b/22881413", "B22881413", "run", null, null, null));
Andreas Gampef10b6e12015-08-12 10:48:12 -0700123 testCases.add(new TestCase("b/20843113", "B20843113", "run", null, null, null));
Andreas Gampe4bf4c782015-08-14 14:07:43 -0700124 testCases.add(new TestCase("b/23201502 (float)", "B23201502", "runFloat", null,
125 new NullPointerException(), null));
126 testCases.add(new TestCase("b/23201502 (double)", "B23201502", "runDouble", null,
127 new NullPointerException(), null));
Andreas Gampef5cdc412015-08-18 08:57:44 -0700128 testCases.add(new TestCase("b/23300986", "B23300986", "runAliasAfterEnter",
129 new Object[] { new Object() }, null, null));
Andreas Gampec1474102015-08-18 08:57:44 -0700130 testCases.add(new TestCase("b/23300986 (2)", "B23300986", "runAliasBeforeEnter",
131 new Object[] { new Object() }, null, null));
Andreas Gampead238ce2015-08-24 21:13:08 -0700132 testCases.add(new TestCase("b/23502994 (if-eqz)", "B23502994", "runIF_EQZ",
133 new Object[] { new Object() }, null, null));
134 testCases.add(new TestCase("b/23502994 (check-cast)", "B23502994", "runCHECKCAST",
135 new Object[] { "abc" }, null, null));
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700136 }
137
138 public void runTests() {
139 for (TestCase tc : testCases) {
140 System.out.println(tc.testName);
141 try {
142 runTest(tc);
143 } catch (Exception exc) {
144 exc.printStackTrace(System.out);
145 }
146 }
147 }
148
149 private void runTest(TestCase tc) throws Exception {
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700150 Exception errorReturn = null;
151 try {
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800152 Class<?> c = Class.forName(tc.testClass);
153
154 Method[] methods = c.getDeclaredMethods();
155
156 // For simplicity we assume that test methods are not overloaded. So searching by name
157 // will give us the method we need to run.
158 Method method = null;
159 for (Method m : methods) {
160 if (m.getName().equals(tc.testMethodName)) {
161 method = m;
162 break;
163 }
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700164 }
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800165
166 if (method == null) {
167 errorReturn = new IllegalArgumentException("Could not find test method " +
168 tc.testMethodName + " in class " +
169 tc.testClass + " for test " +
170 tc.testName);
171 } else {
172 Object retValue;
173 if (Modifier.isStatic(method.getModifiers())) {
174 retValue = method.invoke(null, tc.values);
175 } else {
176 retValue = method.invoke(method.getDeclaringClass().newInstance(), tc.values);
177 }
178 if (tc.expectedException != null) {
179 errorReturn = new IllegalStateException("Expected an exception in test " +
180 tc.testName);
181 }
182 if (tc.expectedReturn == null && retValue != null) {
183 errorReturn = new IllegalStateException("Expected a null result in test " +
184 tc.testName);
185 } else if (tc.expectedReturn != null &&
186 (retValue == null || !tc.expectedReturn.equals(retValue))) {
187 errorReturn = new IllegalStateException("Expected return " +
188 tc.expectedReturn +
189 ", but got " + retValue);
190 } else {
191 // Expected result, do nothing.
192 }
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700193 }
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800194 } catch (Throwable exc) {
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700195 if (tc.expectedException == null) {
196 errorReturn = new IllegalStateException("Did not expect exception", exc);
Vladimir Marko920506d2014-11-18 14:47:31 +0000197 } else if (exc instanceof InvocationTargetException && exc.getCause() != null &&
198 exc.getCause().getClass().equals(tc.expectedException.getClass())) {
199 // Expected exception is wrapped in InvocationTargetException.
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700200 } else if (!tc.expectedException.getClass().equals(exc.getClass())) {
201 errorReturn = new IllegalStateException("Expected " +
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800202 tc.expectedException.getClass().getName() +
203 ", but got " + exc.getClass(), exc);
204 } else {
205 // Expected exception, do nothing.
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700206 }
207 } finally {
208 if (errorReturn != null) {
209 throw errorReturn;
210 }
211 }
212 }
213
214 public static void main(String[] args) throws Exception {
215 Main main = new Main();
216
217 main.runTests();
218
219 System.out.println("Done!");
220 }
221}