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_virtual; 18 19 import dot.junit.DxTestCase; 20 import dot.junit.DxUtil; 21 import dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_1; 22 import dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_14; 23 import dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_17; 24 import dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_4; 25 import dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_6; 26 import dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_7; 27 28 public class Test_invoke_virtual extends DxTestCase { 29 30 /** 31 * @title invoke virtual method 32 */ testN1()33 public void testN1() { 34 T_invoke_virtual_1 t = new T_invoke_virtual_1(); 35 int a = 1; 36 String sa = "a" + a; 37 String sb = "a1"; 38 assertTrue(t.run(sa, sb)); 39 assertFalse(t.run(t, sa)); 40 assertFalse(t.run(sb, t)); 41 } 42 43 /** 44 * @title Invoke protected method of superclass 45 */ testN3()46 public void testN3() { 47 //@uses dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_7 48 //@uses dot.junit.opcodes.invoke_virtual.d.TSuper 49 T_invoke_virtual_7 t = new T_invoke_virtual_7(); 50 assertEquals(5, t.run()); 51 } 52 53 /** 54 * @title Check that new frame is created by invoke_virtual and 55 * arguments are passed to method 56 */ testN5()57 public void testN5() { 58 //@uses dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_14 59 //@uses dot.junit.opcodes.invoke_virtual.d.TSuper 60 T_invoke_virtual_14 t = new T_invoke_virtual_14(); 61 assertTrue(t.run()); 62 } 63 64 /** 65 * @title Recursion of method lookup procedure 66 */ testN6()67 public void testN6() { 68 //@uses dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_17 69 //@uses dot.junit.opcodes.invoke_virtual.d.TSuper 70 T_invoke_virtual_17 t = new T_invoke_virtual_17(); 71 assertEquals(5, t.run()); 72 } 73 74 /** 75 * @title expected NullPointerException 76 */ testE1()77 public void testE1() { 78 T_invoke_virtual_1 t = new T_invoke_virtual_1(); 79 String s = "s"; 80 try { 81 t.run(null, s); 82 fail("expected NullPointerException"); 83 } catch (NullPointerException npe) { 84 // expected 85 } 86 } 87 88 /** 89 * @title Native method can't be linked 90 */ testE2()91 public void testE2() { 92 T_invoke_virtual_4 t = new T_invoke_virtual_4(); 93 try { 94 t.run(); 95 fail("expected UnsatisfiedLinkError"); 96 } catch (UnsatisfiedLinkError ule) { 97 // expected 98 } 99 } 100 101 /** 102 * @title Attempt to invoke abstract method 103 */ testE4()104 public void testE4() { 105 //@uses dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_6 106 //@uses dot.junit.opcodes.invoke_virtual.ATest 107 T_invoke_virtual_6 t = new T_invoke_virtual_6(); 108 try { 109 t.run(); 110 fail("expected AbstractMethodError"); 111 } catch (AbstractMethodError iae) { 112 // expected 113 } 114 } 115 116 /** 117 * @constraint A13 118 * @title invalid constant pool index 119 */ testVFE1()120 public void testVFE1() { 121 try { 122 Class.forName("dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_8"); 123 fail("expected a verification exception"); 124 } catch (Throwable t) { 125 DxUtil.checkVerifyException(t); 126 } 127 } 128 129 /** 130 * @constraint A15 131 * @title <clinit> may not be called using invoke-virtual 132 */ testVFE3()133 public void testVFE3() { 134 try { 135 Class.forName("dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_10"); 136 fail("expected a verification exception"); 137 } catch (Throwable t) { 138 DxUtil.checkVerifyException(t); 139 } 140 } 141 142 /** 143 * @constraint B1 144 * @title number of arguments passed to method 145 */ testVFE4()146 public void testVFE4() { 147 try { 148 Class.forName("dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_11"); 149 fail("expected a verification exception"); 150 } catch (Throwable t) { 151 DxUtil.checkVerifyException(t); 152 } 153 } 154 155 /** 156 * @constraint B9 157 * @title types of arguments passed to method 158 */ testVFE5()159 public void testVFE5() { 160 try { 161 Class.forName("dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_12"); 162 fail("expected a verification exception"); 163 } catch (Throwable t) { 164 DxUtil.checkVerifyException(t); 165 } 166 } 167 168 /** 169 * @constraint A15 170 * @title <init> may not be called using invoke_virtual 171 */ testVFE6()172 public void testVFE6() { 173 try { 174 Class.forName("dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_16"); 175 fail("expected a verification exception"); 176 } catch (Throwable t) { 177 DxUtil.checkVerifyException(t); 178 } 179 } 180 181 /** 182 * @constraint B10 183 * @title assignment incompatible references when accessing 184 * protected method 185 */ testVFE8()186 public void testVFE8() { 187 //@uses dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_22 188 //@uses dot.junit.opcodes.invoke_virtual.d.TSuper 189 //@uses dot.junit.opcodes.invoke_virtual.d.TPlain 190 try { 191 Class.forName("dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_22"); 192 fail("expected a verification exception"); 193 } catch (Throwable t) { 194 DxUtil.checkVerifyException(t); 195 } 196 } 197 198 /** 199 * @constraint B10 200 * @title assignment incompatible references when accessing 201 * public method 202 */ testVFE9()203 public void testVFE9() { 204 //@uses dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_23 205 //@uses dot.junit.opcodes.invoke_virtual.d.TSuper 206 //@uses dot.junit.opcodes.invoke_virtual.d.TSuper2 207 try { 208 Class.forName("dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_23"); 209 fail("expected a verification exception"); 210 } catch (Throwable t) { 211 DxUtil.checkVerifyException(t); 212 } 213 } 214 215 216 /** 217 * @constraint n/a 218 * @title Attempt to call static method. Java throws IncompatibleClassChangeError 219 * on first access but Dalvik throws VerifyError on class loading. 220 */ testVFE10()221 public void testVFE10() { 222 try { 223 Class.forName("dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_5"); 224 fail("expected a verification exception"); 225 } catch (Throwable t) { 226 DxUtil.checkVerifyException(t); 227 } 228 } 229 230 231 /** 232 * @constraint n/a 233 * @title Attempt to invoke non-existing method. Java throws NoSuchMethodError 234 * on first access but Dalvik throws VerifyError on class loading. 235 */ testVFE12()236 public void testVFE12() { 237 try { 238 Class.forName("dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_15"); 239 fail("expected a verification exception"); 240 } catch (Throwable t) { 241 DxUtil.checkVerifyException(t); 242 } 243 } 244 245 /** 246 * @constraint n/a 247 * @title Attempt to invoke private method of other class. Java throws IllegalAccessError 248 * on first access but Dalvik throws VerifyError on class loading. 249 */ testVFE13()250 public void testVFE13() { 251 //@uses dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_18 252 //@uses dot.junit.opcodes.invoke_virtual.TestStubs 253 try { 254 Class.forName("dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_18"); 255 fail("expected a verification exception"); 256 } catch (Throwable t) { 257 DxUtil.checkVerifyException(t); 258 } 259 } 260 261 /** 262 * @constraint B12 263 * @title Attempt to invoke protected method of unrelated class. Java throws 264 * IllegalAccessError on first access but Dalvik throws VerifyError on class loading. 265 */ testVFE14()266 public void testVFE14() { 267 //@uses dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_20 268 //@uses dot.junit.opcodes.invoke_virtual.TestStubs 269 try { 270 Class.forName("dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_20"); 271 fail("expected a verification exception"); 272 } catch (Throwable t) { 273 DxUtil.checkVerifyException(t); 274 } 275 } 276 277 /** 278 * @constraint n/a 279 * @title Method has different signature. Java throws 280 * NoSuchMethodError on first access but Dalvik throws VerifyError on class loading. 281 */ testVFE15()282 public void testVFE15() { 283 //@uses dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_19 284 //@uses dot.junit.opcodes.invoke_virtual.d.TSuper 285 try { 286 Class.forName("dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_19"); 287 fail("expected a verification exception"); 288 } catch (Throwable t) { 289 DxUtil.checkVerifyException(t); 290 } 291 } 292 293 /** 294 * @constraint n/a 295 * @title invoke-virtual shall be used to invoke private methods 296 */ testVFE16()297 public void testVFE16() { 298 try { 299 Class.forName("dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_13"); 300 fail("expected a verification exception"); 301 } catch (Throwable t) { 302 DxUtil.checkVerifyException(t); 303 } 304 } 305 306 /** 307 * @constraint A23 308 * @title number of registers 309 */ testVFE17()310 public void testVFE17() { 311 try { 312 Class.forName("dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_9"); 313 fail("expected a verification exception"); 314 } catch (Throwable t) { 315 DxUtil.checkVerifyException(t); 316 } 317 } 318 319 /** 320 * @constraint A13 321 * @title attempt to invoke interface method 322 */ testVFE18()323 public void testVFE18() { 324 try { 325 Class.forName("dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_24"); 326 fail("expected a verification exception"); 327 } catch (Throwable t) { 328 DxUtil.checkVerifyException(t); 329 } 330 } 331 332 /** 333 * @constraint B6 334 * @title instance methods may only be invoked on already initialized instances. 335 */ testVFE19()336 public void testVFE19() { 337 try { 338 Class.forName("dot.junit.opcodes.invoke_virtual.d.T_invoke_virtual_25"); 339 fail("expected a verification exception"); 340 } catch (Throwable t) { 341 DxUtil.checkVerifyException(t); 342 } 343 } 344 345 } 346