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