1 /* 2 * Copyright (C) 2008 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 17 package dot.junit.opcodes.invoke_interface; 18 19 import dot.junit.DxTestCase; 20 import dot.junit.DxUtil; 21 import dot.junit.opcodes.invoke_interface.d.T_invoke_interface_1; 22 import dot.junit.opcodes.invoke_interface.d.T_invoke_interface_11; 23 import dot.junit.opcodes.invoke_interface.d.T_invoke_interface_12; 24 import dot.junit.opcodes.invoke_interface.d.T_invoke_interface_13; 25 import dot.junit.opcodes.invoke_interface.d.T_invoke_interface_14; 26 import dot.junit.opcodes.invoke_interface.d.T_invoke_interface_16; 27 import dot.junit.opcodes.invoke_interface.d.T_invoke_interface_18; 28 import dot.junit.opcodes.invoke_interface.d.T_invoke_interface_20; 29 import dot.junit.opcodes.invoke_interface.d.T_invoke_interface_21; 30 import dot.junit.opcodes.invoke_interface.d.T_invoke_interface_3; 31 import dot.junit.opcodes.invoke_interface.d.T_invoke_interface_4; 32 import dot.junit.opcodes.invoke_interface.d.T_invoke_interface_5; 33 import dot.junit.opcodes.invoke_interface.d.T_invoke_interface_7; 34 35 public class Test_invoke_interface extends DxTestCase { 36 37 /** 38 * @title invoke interface method 39 */ 40 public void testN1() { 41 T_invoke_interface_1 t = new T_invoke_interface_1(); 42 assertEquals(0, t.run("aa", "aa")); 43 assertEquals(-1, t.run("aa", "bb")); 44 assertEquals(1, t.run("bb", "aa")); 45 } 46 47 /** 48 * @title Check that new frame is created by invoke_interface and 49 * arguments are passed to method 50 */ 51 public void testN2() { 52 //@uses dot.junit.opcodes.invoke_interface.d.T_invoke_interface_14 53 //@uses dot.junit.opcodes.invoke_interface.ITest 54 //@uses dot.junit.opcodes.invoke_interface.ITestImpl 55 T_invoke_interface_14 t = new T_invoke_interface_14(); 56 ITestImpl impl = new ITestImpl(); 57 assertEquals(1, t.run(impl)); 58 } 59 60 61 62 /** 63 * @title objref is null 64 */ 65 public void testE3() { 66 //@uses dot.junit.opcodes.invoke_interface.d.T_invoke_interface_3 67 //@uses dot.junit.opcodes.invoke_interface.ITest 68 try { 69 new T_invoke_interface_3(null); 70 fail("expected NullPointerException"); 71 } catch (NullPointerException npe) { 72 // expected 73 } 74 } 75 76 /** 77 * @title object doesn't implement interface 78 */ 79 public void testE4() { 80 //@uses dot.junit.opcodes.invoke_interface.d.T_invoke_interface_11 81 //@uses dot.junit.opcodes.invoke_interface.ITest 82 //@uses dot.junit.opcodes.invoke_interface.ITestImpl 83 T_invoke_interface_11 t = new T_invoke_interface_11(); 84 try { 85 t.run(); 86 fail("expected IncompatibleClassChangeError"); 87 } catch (IncompatibleClassChangeError e) { 88 // expected 89 } 90 } 91 92 /** 93 * @title dvmInterpFindInterfaceMethod failures were putting NULL Method*s 94 * in the interface cache, leading to a null pointer deference the second 95 * time you made the same bad call, with no exception thrown. 96 * See http://code.google.com/p/android/issues/detail?id=29358 for details. 97 */ 98 public void testE4_2() { 99 //@uses dot.junit.opcodes.invoke_interface.d.T_invoke_interface_11 100 //@uses dot.junit.opcodes.invoke_interface.ITest 101 //@uses dot.junit.opcodes.invoke_interface.ITestImpl 102 T_invoke_interface_11 t = new T_invoke_interface_11(); 103 try { 104 t.run(); 105 fail("expected IncompatibleClassChangeError"); 106 } catch (IncompatibleClassChangeError expected) { 107 } 108 try { 109 t.run(); 110 fail("expected IncompatibleClassChangeError"); 111 } catch (IncompatibleClassChangeError expected) { 112 } 113 } 114 115 /** 116 * @title Native method can't be linked 117 */ 118 public void testE5() { 119 //@uses dot.junit.opcodes.invoke_interface.d.T_invoke_interface_12 120 //@uses dot.junit.opcodes.invoke_interface.ITest 121 //@uses dot.junit.opcodes.invoke_interface.ITestImpl 122 T_invoke_interface_12 t = new T_invoke_interface_12(); 123 ITestImpl impl = new ITestImpl(); 124 try { 125 t.run(impl); 126 fail("expected UnsatisfiedLinkError"); 127 } catch (UnsatisfiedLinkError e) { 128 // expected 129 } 130 } 131 132 /** 133 * @title Attempt to invoke abstract method 134 */ 135 public void testE6() { 136 //@uses dot.junit.opcodes.invoke_interface.d.T_invoke_interface_13 137 //@uses dot.junit.opcodes.invoke_interface.ITest 138 //@uses dot.junit.opcodes.invoke_interface.ITestImpl 139 //@uses dot.junit.opcodes.invoke_interface.ITestImplAbstract 140 T_invoke_interface_13 t = new T_invoke_interface_13(); 141 try { 142 t.run(); 143 fail("expected AbstractMethodError"); 144 } catch (AbstractMethodError e) { 145 // expected 146 } 147 } 148 149 /** 150 * @constraint A16 151 * @title invalid constant pool index 152 */ 153 public void testVFE1() { 154 try { 155 Class.forName("dot.junit.opcodes.invoke_interface.d.T_invoke_interface_2"); 156 fail("expected a verification exception"); 157 } catch (Throwable t) { 158 DxUtil.checkVerifyException(t); 159 } 160 } 161 162 /** 163 * @constraint A16 164 * @title The referenced method_id must belong to an interface (not a class). 165 */ 166 public void testVFE2() { 167 try { 168 new T_invoke_interface_4().run(); 169 fail("expected NoSuchMethodError or IncompatibleClassChangeError"); 170 } catch (NoSuchMethodError t) { 171 } catch (IncompatibleClassChangeError e) { 172 } 173 } 174 175 /** 176 * @constraint B1 177 * @title number of arguments 178 */ 179 public void testVFE5() { 180 //@uses dot.junit.opcodes.invoke_interface.ITest 181 //@uses dot.junit.opcodes.invoke_interface.ITestImpl 182 try { 183 new T_invoke_interface_5(new ITestImpl()); 184 fail("expected VerifyError"); 185 } catch (VerifyError t) { 186 } 187 } 188 189 /** 190 * @constraint B1 191 * @title int is passed instead of objref 192 */ 193 public void testVFE6() { 194 try { 195 Class.forName("dot.junit.opcodes.invoke_interface.d.T_invoke_interface_10"); 196 fail("expected a verification exception"); 197 } catch (Throwable t) { 198 DxUtil.checkVerifyException(t); 199 } 200 } 201 202 /** 203 * @constraint B9 204 * @title number of arguments passed to method 205 */ 206 public void testVFE9() { 207 try { 208 Class.forName("dot.junit.opcodes.invoke_interface.d.T_invoke_interface_9"); 209 fail("expected a verification exception"); 210 } catch (Throwable t) { 211 DxUtil.checkVerifyException(t); 212 } 213 } 214 215 /** 216 * @constraint A15 217 * @title invoke-interface may not be used to call <init>. 218 */ 219 public void testVFE10() { 220 //@uses dot.junit.opcodes.invoke_interface.d.T_invoke_interface_18 221 try { 222 new T_invoke_interface_18().run(new ITestImpl()); 223 fail("expected NoSuchMethodError or verification exception"); 224 } catch (NoSuchMethodError t) { 225 } catch (Throwable t) { 226 DxUtil.checkVerifyException(t); 227 } 228 } 229 230 /** 231 * @constraint A15 232 * @title invoke-interface may not be used to call <clinit>. 233 */ 234 public void testVFE11() { 235 //@uses dot.junit.opcodes.invoke_interface.ITest 236 //@uses dot.junit.opcodes.invoke_interface.ITestImpl 237 try { 238 new T_invoke_interface_20().run(new ITestImpl()); 239 fail("expected NoSuchMethodError or verification exception"); 240 } catch (NoSuchMethodError t) { 241 } catch (Throwable t) { 242 DxUtil.checkVerifyException(t); 243 } 244 } 245 246 /** 247 * @constraint B9 248 * @title types of arguments passed to method 249 */ 250 public void testVFE12() { 251 //@uses dot.junit.opcodes.invoke_interface.ITest 252 //@uses dot.junit.opcodes.invoke_interface.ITestImpl 253 try { 254 new T_invoke_interface_21().run(new ITestImpl()); 255 fail("expected VerifyError"); 256 } catch (VerifyError t) { 257 } 258 } 259 260 /** 261 * @constraint A23 262 * @title number of registers 263 */ 264 public void testVFE13() { 265 try { 266 Class.forName("dot.junit.opcodes.invoke_interface.d.T_invoke_interface_8"); 267 fail("expected a verification exception"); 268 } catch (Throwable t) { 269 DxUtil.checkVerifyException(t); 270 } 271 } 272 273 /** 274 * @constraint n/a 275 * @title Attempt to call undefined method. 276 */ 277 public void testVFE14() { 278 //@uses dot.junit.opcodes.invoke_interface.d.T_invoke_interface_7 279 //@uses dot.junit.opcodes.invoke_interface.ITest 280 //@uses dot.junit.opcodes.invoke_interface.ITestImpl 281 try { 282 new T_invoke_interface_7().run(new ITestImpl()); 283 fail("expected NoSuchMethodError"); 284 } catch (NoSuchMethodError t) { 285 } 286 } 287 288 /** 289 * @constraint n/a 290 * @title Method has different signature. 291 */ 292 public void testVFE15() { 293 //@uses dot.junit.opcodes.invoke_interface.d.T_invoke_interface_16 294 //@uses dot.junit.opcodes.invoke_interface.ITest 295 //@uses dot.junit.opcodes.invoke_interface.ITestImpl 296 try { 297 new T_invoke_interface_16().run(new ITestImpl()); 298 fail("expected NoSuchMethodError"); 299 } catch (NoSuchMethodError t) { 300 } 301 } 302 303 304 /** 305 * @constraint B6 306 * @title instance methods may only be invoked on already initialized instances. 307 */ 308 public void testVFE21() { 309 //@uses dot.junit.opcodes.invoke_interface.d.T_invoke_interface_22 310 //@uses dot.junit.opcodes.invoke_interface.ITest 311 //@uses dot.junit.opcodes.invoke_interface.ITestImpl 312 try { 313 Class.forName("dot.junit.opcodes.invoke_interface.d.T_invoke_interface_22"); 314 fail("expected a verification exception"); 315 } catch (Throwable t) { 316 DxUtil.checkVerifyException(t); 317 } 318 } 319 } 320