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)); |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 54 | testCases.add(new TestCase("PackedSwitch key INT_MAX", "PackedSwitch", |
| 55 | "packedSwitch_INT_MAX", new Object[]{123}, null, 123)); |
| 56 | testCases.add(new TestCase("PackedSwitch key overflow", "b_24399945", |
| 57 | "packedSwitch_overflow", new Object[]{123}, new VerifyError(), null)); |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 58 | |
| 59 | testCases.add(new TestCase("b/17790197", "B17790197", "getInt", null, null, 100)); |
Stephen Kyle | 8fe0e35 | 2014-10-16 15:02:42 +0100 | [diff] [blame] | 60 | testCases.add(new TestCase("FloatBadArgReg", "FloatBadArgReg", "getInt", |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 61 | new Object[]{100}, null, 100)); |
nikolay serdjuk | d24c934 | 2014-11-10 16:53:27 +0700 | [diff] [blame] | 62 | testCases.add(new TestCase("negLong", "negLong", "negLong", null, null, 122142L)); |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 63 | testCases.add(new TestCase("sameFieldNames", "sameFieldNames", "getInt", null, null, 7)); |
Vladimir Marko | 920506d | 2014-11-18 14:47:31 +0000 | [diff] [blame] | 64 | testCases.add(new TestCase("b/18380491", "B18380491ConcreteClass", "foo", |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 65 | new Object[]{42}, null, 42)); |
Vladimir Marko | 920506d | 2014-11-18 14:47:31 +0000 | [diff] [blame] | 66 | testCases.add(new TestCase("invoke-super abstract", "B18380491ConcreteClass", "foo", |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 67 | new Object[]{0}, new AbstractMethodError(), null)); |
| 68 | testCases.add(new TestCase("BadCaseInOpRegRegReg", "BadCaseInOpRegRegReg", "getInt", null, |
| 69 | null, 2)); |
Nicolas Geoffray | 32b2a52 | 2014-11-27 14:54:18 +0000 | [diff] [blame] | 70 | testCases.add(new TestCase("CmpLong", "CmpLong", "run", null, null, 0)); |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 71 | testCases.add(new TestCase("FloatIntConstPassing", "FloatIntConstPassing", "run", null, |
| 72 | null, 2)); |
Vladimir Marko | 341e425 | 2014-12-19 10:29:51 +0000 | [diff] [blame] | 73 | testCases.add(new TestCase("b/18718277", "B18718277", "getInt", null, null, 0)); |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 74 | testCases.add(new TestCase("b/18800943 (1)", "B18800943_1", "n_a", null, new VerifyError(), |
| 75 | 0)); |
| 76 | testCases.add(new TestCase("b/18800943 (2)", "B18800943_2", "n_a", null, new VerifyError(), |
| 77 | 0)); |
| 78 | testCases.add(new TestCase("MoveExc", "MoveExc", "run", null, new ArithmeticException(), |
| 79 | null)); |
| 80 | testCases.add(new TestCase("MoveExceptionOnEntry", "MoveExceptionOnEntry", |
| 81 | "moveExceptionOnEntry", new Object[]{0}, new VerifyError(), null)); |
Jeff Hao | 9ccd151 | 2015-03-20 18:11:45 -0700 | [diff] [blame] | 82 | testCases.add(new TestCase("EmptySparseSwitch", "EmptySparseSwitch", "run", null, null, |
| 83 | null)); |
Andreas Gampe | b588f4c | 2015-05-26 13:35:39 -0700 | [diff] [blame] | 84 | testCases.add(new TestCase("b/20224106", "B20224106", "run", null, new VerifyError(), |
| 85 | 0)); |
Andreas Gampe | da9badb | 2015-06-05 20:22:12 -0700 | [diff] [blame] | 86 | testCases.add(new TestCase("b/17410612", "B17410612", "run", null, new VerifyError(), |
| 87 | 0)); |
Nicolas Geoffray | ea9ef4d | 2015-06-19 10:05:50 +0100 | [diff] [blame] | 88 | testCases.add(new TestCase("b/21863767", "B21863767", "run", null, null, |
Nicolas Geoffray | 69505f8 | 2015-06-18 18:04:12 +0100 | [diff] [blame] | 89 | null)); |
Vladimir Marko | 2d1a0a4 | 2015-06-18 17:40:00 +0100 | [diff] [blame] | 90 | testCases.add(new TestCase("b/21873167", "B21873167", "test", null, null, null)); |
Vladimir Marko | f11c420 | 2015-06-19 12:58:22 +0100 | [diff] [blame] | 91 | testCases.add(new TestCase("b/21614284", "B21614284", "test", new Object[] { null }, |
Andreas Gampe | d12e782 | 2015-06-25 10:26:40 -0700 | [diff] [blame] | 92 | new NullPointerException(), null)); |
Jeff Hao | dde9827 | 2015-06-17 16:04:26 -0700 | [diff] [blame] | 93 | testCases.add(new TestCase("b/21902684", "B21902684", "test", null, null, null)); |
Andreas Gampe | a32210c | 2015-06-24 10:26:13 -0700 | [diff] [blame] | 94 | testCases.add(new TestCase("b/22045582", "B22045582", "run", null, new VerifyError(), |
| 95 | 0)); |
| 96 | testCases.add(new TestCase("b/22045582 (int)", "B22045582Int", "run", null, |
| 97 | new VerifyError(), 0)); |
| 98 | testCases.add(new TestCase("b/22045582 (wide)", "B22045582Wide", "run", null, |
| 99 | new VerifyError(), 0)); |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 100 | testCases.add(new TestCase("b/21886894", "B21886894", "test", null, new VerifyError(), |
Andreas Gampe | d12e782 | 2015-06-25 10:26:40 -0700 | [diff] [blame] | 101 | null)); |
| 102 | testCases.add(new TestCase("b/22080519", "B22080519", "run", null, |
| 103 | new NullPointerException(), null)); |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 104 | testCases.add(new TestCase("b/21645819", "B21645819", "run", new Object[] { null }, |
| 105 | null, null)); |
Andreas Gampe | 185a558 | 2015-07-06 14:00:39 -0700 | [diff] [blame] | 106 | testCases.add(new TestCase("b/22244733", "B22244733", "run", new Object[] { "abc" }, |
| 107 | null, "abc")); |
Andreas Gampe | 3853628 | 2015-07-08 17:22:57 -0700 | [diff] [blame] | 108 | testCases.add(new TestCase("b/22331663", "B22331663", "run", new Object[] { false }, |
| 109 | null, null)); |
Andreas Gampe | 97a1ff3 | 2015-07-09 11:30:14 -0700 | [diff] [blame] | 110 | testCases.add(new TestCase("b/22331663 (pass)", "B22331663Pass", "run", |
| 111 | new Object[] { false }, null, null)); |
| 112 | testCases.add(new TestCase("b/22331663 (fail)", "B22331663Fail", "run", |
| 113 | new Object[] { false }, new VerifyError(), null)); |
Andreas Gampe | 891dfaa | 2015-07-13 21:12:43 -0700 | [diff] [blame] | 114 | testCases.add(new TestCase("b/22411633 (1)", "B22411633_1", "run", new Object[] { false }, |
| 115 | null, null)); |
| 116 | testCases.add(new TestCase("b/22411633 (2)", "B22411633_2", "run", new Object[] { false }, |
| 117 | new VerifyError(), null)); |
| 118 | testCases.add(new TestCase("b/22411633 (3)", "B22411633_3", "run", new Object[] { false }, |
| 119 | null, null)); |
| 120 | testCases.add(new TestCase("b/22411633 (4)", "B22411633_4", "run", new Object[] { false }, |
| 121 | new VerifyError(), null)); |
| 122 | testCases.add(new TestCase("b/22411633 (5)", "B22411633_5", "run", new Object[] { false }, |
| 123 | null, null)); |
Andreas Gampe | be2aa44 | 2015-07-27 21:41:49 -0700 | [diff] [blame] | 124 | testCases.add(new TestCase("b/22777307", "B22777307", "run", null, new InstantiationError(), |
| 125 | null)); |
Andreas Gampe | 2ea7b70 | 2015-08-07 08:07:16 -0700 | [diff] [blame] | 126 | testCases.add(new TestCase("b/22881413", "B22881413", "run", null, null, null)); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 127 | testCases.add(new TestCase("b/20843113", "B20843113", "run", null, null, null)); |
Andreas Gampe | 4bf4c78 | 2015-08-14 14:07:43 -0700 | [diff] [blame] | 128 | testCases.add(new TestCase("b/23201502 (float)", "B23201502", "runFloat", null, |
| 129 | new NullPointerException(), null)); |
| 130 | testCases.add(new TestCase("b/23201502 (double)", "B23201502", "runDouble", null, |
| 131 | new NullPointerException(), null)); |
Andreas Gampe | f5cdc41 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 132 | testCases.add(new TestCase("b/23300986", "B23300986", "runAliasAfterEnter", |
| 133 | new Object[] { new Object() }, null, null)); |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 134 | testCases.add(new TestCase("b/23300986 (2)", "B23300986", "runAliasBeforeEnter", |
| 135 | new Object[] { new Object() }, null, null)); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 136 | testCases.add(new TestCase("b/23502994 (if-eqz)", "B23502994", "runIF_EQZ", |
| 137 | new Object[] { new Object() }, null, null)); |
| 138 | testCases.add(new TestCase("b/23502994 (check-cast)", "B23502994", "runCHECKCAST", |
| 139 | new Object[] { "abc" }, null, null)); |
Andreas Gampe | a4c98f2 | 2015-11-06 16:24:49 -0800 | [diff] [blame] | 140 | testCases.add(new TestCase("b/25494456", "B25494456", "run", null, new VerifyError(), |
| 141 | null)); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 142 | testCases.add(new TestCase("b/21869691", "B21869691A", "run", null, |
| 143 | new IncompatibleClassChangeError(), null)); |
Andreas Gampe | 97b1135 | 2015-12-10 16:23:41 -0800 | [diff] [blame] | 144 | testCases.add(new TestCase("b/26143249", "B26143249", "run", null, |
| 145 | new AbstractMethodError(), null)); |
David Brazdil | 87a5575 | 2016-01-15 14:55:13 +0000 | [diff] [blame] | 146 | testCases.add(new TestCase("b/26579108", "B26579108", "run", null, new VerifyError(), |
| 147 | null)); |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 148 | testCases.add(new TestCase("b/26594149 (1)", "B26594149_1", "run", null, new VerifyError(), |
| 149 | null)); |
| 150 | testCases.add(new TestCase("b/26594149 (2)", "B26594149_2", "run", null, new VerifyError(), |
| 151 | null)); |
| 152 | testCases.add(new TestCase("b/26594149 (3)", "B26594149_3", "run", null, new VerifyError(), |
| 153 | null)); |
| 154 | testCases.add(new TestCase("b/26594149 (4)", "B26594149_4", "run", null, new VerifyError(), |
| 155 | null)); |
| 156 | testCases.add(new TestCase("b/26594149 (5)", "B26594149_5", "run", null, null, null)); |
| 157 | testCases.add(new TestCase("b/26594149 (6)", "B26594149_6", "run", null, new VerifyError(), |
| 158 | null)); |
| 159 | testCases.add(new TestCase("b/26594149 (7)", "B26594149_7", "run", null, new VerifyError(), |
| 160 | null)); |
| 161 | testCases.add(new TestCase("b/26594149 (8)", "B26594149_8", "run", null, new VerifyError(), |
| 162 | null)); |
Pavel Vyssotski | 980027c | 2016-02-11 20:28:11 +0600 | [diff] [blame] | 163 | testCases.add(new TestCase("b/27148248", "B27148248", "run", null, new VerifyError(), |
| 164 | null)); |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 165 | testCases.add(new TestCase("b/26965384", "B26965384", "run", null, new VerifyError(), |
| 166 | null)); |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 167 | testCases.add(new TestCase("b/27799205 (1)", "B27799205Helper", "run1", null, null, null)); |
| 168 | testCases.add(new TestCase("b/27799205 (2)", "B27799205Helper", "run2", null, |
| 169 | new VerifyError(), null)); |
| 170 | testCases.add(new TestCase("b/27799205 (3)", "B27799205Helper", "run3", null, |
| 171 | new VerifyError(), null)); |
| 172 | testCases.add(new TestCase("b/27799205 (4)", "B27799205Helper", "run4", null, |
| 173 | new VerifyError(), null)); |
| 174 | testCases.add(new TestCase("b/27799205 (5)", "B27799205Helper", "run5", null, |
| 175 | new VerifyError(), null)); |
Andreas Gampe | 8d8fc48 | 2016-03-25 16:24:20 -0700 | [diff] [blame] | 176 | testCases.add(new TestCase("b/27799205 (6)", "B27799205Helper", "run6", null, null, null)); |
Roland Levillain | ab880f4 | 2016-05-12 16:24:36 +0100 | [diff] [blame] | 177 | testCases.add(new TestCase("b/28187158", "B28187158", "run", new Object[] { null }, |
Andreas Gampe | 6659624 | 2016-04-14 10:55:04 -0700 | [diff] [blame] | 178 | new VerifyError(), null)); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 179 | testCases.add(new TestCase("b/29778499 (1)", "B29778499_1", "run", null, |
| 180 | new IncompatibleClassChangeError(), null)); |
| 181 | testCases.add(new TestCase("b/29778499 (2)", "B29778499_2", "run", null, |
| 182 | new IncompatibleClassChangeError(), null)); |
David Brazdil | 0d638bb | 2016-07-27 15:29:25 +0100 | [diff] [blame] | 183 | testCases.add(new TestCase("b/30458218", "B30458218", "run", null, null, null)); |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | public void runTests() { |
| 187 | for (TestCase tc : testCases) { |
| 188 | System.out.println(tc.testName); |
| 189 | try { |
| 190 | runTest(tc); |
| 191 | } catch (Exception exc) { |
| 192 | exc.printStackTrace(System.out); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | private void runTest(TestCase tc) throws Exception { |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 198 | Exception errorReturn = null; |
| 199 | try { |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 200 | Class<?> c = Class.forName(tc.testClass); |
| 201 | |
| 202 | Method[] methods = c.getDeclaredMethods(); |
| 203 | |
| 204 | // For simplicity we assume that test methods are not overloaded. So searching by name |
| 205 | // will give us the method we need to run. |
| 206 | Method method = null; |
| 207 | for (Method m : methods) { |
| 208 | if (m.getName().equals(tc.testMethodName)) { |
| 209 | method = m; |
| 210 | break; |
| 211 | } |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 212 | } |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 213 | |
| 214 | if (method == null) { |
| 215 | errorReturn = new IllegalArgumentException("Could not find test method " + |
| 216 | tc.testMethodName + " in class " + |
| 217 | tc.testClass + " for test " + |
| 218 | tc.testName); |
| 219 | } else { |
| 220 | Object retValue; |
| 221 | if (Modifier.isStatic(method.getModifiers())) { |
| 222 | retValue = method.invoke(null, tc.values); |
| 223 | } else { |
| 224 | retValue = method.invoke(method.getDeclaringClass().newInstance(), tc.values); |
| 225 | } |
| 226 | if (tc.expectedException != null) { |
| 227 | errorReturn = new IllegalStateException("Expected an exception in test " + |
| 228 | tc.testName); |
David Brazdil | 87a5575 | 2016-01-15 14:55:13 +0000 | [diff] [blame] | 229 | } else if (tc.expectedReturn == null && retValue != null) { |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 230 | errorReturn = new IllegalStateException("Expected a null result in test " + |
| 231 | tc.testName); |
| 232 | } else if (tc.expectedReturn != null && |
| 233 | (retValue == null || !tc.expectedReturn.equals(retValue))) { |
| 234 | errorReturn = new IllegalStateException("Expected return " + |
| 235 | tc.expectedReturn + |
| 236 | ", but got " + retValue); |
| 237 | } else { |
| 238 | // Expected result, do nothing. |
| 239 | } |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 240 | } |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 241 | } catch (Throwable exc) { |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 242 | if (tc.expectedException == null) { |
| 243 | errorReturn = new IllegalStateException("Did not expect exception", exc); |
Vladimir Marko | 920506d | 2014-11-18 14:47:31 +0000 | [diff] [blame] | 244 | } else if (exc instanceof InvocationTargetException && exc.getCause() != null && |
| 245 | exc.getCause().getClass().equals(tc.expectedException.getClass())) { |
| 246 | // Expected exception is wrapped in InvocationTargetException. |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 247 | } else if (!tc.expectedException.getClass().equals(exc.getClass())) { |
| 248 | errorReturn = new IllegalStateException("Expected " + |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 249 | tc.expectedException.getClass().getName() + |
| 250 | ", but got " + exc.getClass(), exc); |
| 251 | } else { |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 252 | // Expected exception, do nothing. |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 253 | } |
| 254 | } finally { |
| 255 | if (errorReturn != null) { |
| 256 | throw errorReturn; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | public static void main(String[] args) throws Exception { |
| 262 | Main main = new Main(); |
| 263 | |
| 264 | main.runTests(); |
| 265 | |
| 266 | System.out.println("Done!"); |
| 267 | } |
| 268 | } |