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