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_static; 18 19 import dot.junit.DxTestCase; 20 import dot.junit.DxUtil; 21 import dot.junit.opcodes.invoke_static.d.T_invoke_static_1; 22 import dot.junit.opcodes.invoke_static.d.T_invoke_static_14; 23 import dot.junit.opcodes.invoke_static.d.T_invoke_static_15; 24 import dot.junit.opcodes.invoke_static.d.T_invoke_static_18; 25 import dot.junit.opcodes.invoke_static.d.T_invoke_static_2; 26 import dot.junit.opcodes.invoke_static.d.T_invoke_static_4; 27 import dot.junit.opcodes.invoke_static.d.T_invoke_static_6; 28 29 30 31 public class Test_invoke_static extends DxTestCase { 32 33 /** 34 * @title Static method from library class Math 35 */ testN1()36 public void testN1() { 37 T_invoke_static_1 t = new T_invoke_static_1(); 38 assertEquals(1234567, t.run()); 39 } 40 41 /** 42 * @title Static method from user class 43 */ testN2()44 public void testN2() { 45 //@uses dot.junit.opcodes.invoke_static.d.T_invoke_static_2 46 //@uses dot.junit.opcodes.invoke_static.TestClass 47 T_invoke_static_2 t = new T_invoke_static_2(); 48 assertEquals(777, t.run()); 49 } 50 51 /** 52 * @title Check that <clinit> is called 53 */ testN3()54 public void testN3() { 55 assertEquals(123456789l, T_invoke_static_4.run()); 56 } 57 58 59 /** 60 * @title Check that new frame is created by invoke_static and 61 * arguments are passed to method 62 */ testN5()63 public void testN5() { 64 //@uses dot.junit.opcodes.invoke_static.d.T_invoke_static_15 65 //@uses dot.junit.opcodes.invoke_static.TestClass 66 T_invoke_static_15 t = new T_invoke_static_15(); 67 assertTrue(t.run()); 68 } 69 70 /** 71 * @title Static protected method from other class in the same package 72 */ testN6()73 public void testN6() { 74 T_invoke_static_18 t = new T_invoke_static_18(); 75 assertEquals(888, t.run()); 76 } 77 78 /** 79 * @title Native method can't be linked 80 * 81 */ testE2()82 public void testE2() { 83 T_invoke_static_6 t = new T_invoke_static_6(); 84 try { 85 t.run(); 86 fail("expected UnsatisfiedLinkError"); 87 } catch (UnsatisfiedLinkError ule) { 88 // expected 89 } 90 } 91 92 93 /** 94 * @title initialization of referenced class throws exception 95 */ testE7()96 public void testE7() { 97 T_invoke_static_14 t = new T_invoke_static_14(); 98 try { 99 t.run(); 100 fail("expected Error"); 101 } catch (Error e) { 102 // expected 103 } 104 } 105 106 107 /** 108 * @constraint A13 109 * @title invalid constant pool index 110 */ testVFE1()111 public void testVFE1() { 112 try { 113 Class.forName("dot.junit.opcodes.invoke_static.d.T_invoke_static_3"); 114 fail("expected a verification exception"); 115 } catch (Throwable t) { 116 DxUtil.checkVerifyException(t); 117 } 118 } 119 120 /** 121 * @constraint A15 122 * @title <clinit> may not be called using invoke-static 123 */ testVFE3()124 public void testVFE3() { 125 try { 126 Class.forName("dot.junit.opcodes.invoke_static.d.T_invoke_static_10"); 127 fail("expected a verification exception"); 128 } catch (Throwable t) { 129 DxUtil.checkVerifyException(t); 130 } 131 } 132 133 /** 134 * @constraint B1 135 * @title number of arguments passed to method. 136 */ testVFE4()137 public void testVFE4() { 138 try { 139 Class.forName("dot.junit.opcodes.invoke_static.d.T_invoke_static_11"); 140 fail("expected a verification exception"); 141 } catch (Throwable t) { 142 DxUtil.checkVerifyException(t); 143 } 144 } 145 146 /** 147 * @constraint A15 148 * @title <init> may not be called using invoke_static 149 */ testVFE5()150 public void testVFE5() { 151 try { 152 Class.forName("dot.junit.opcodes.invoke_static.d.T_invoke_static_19"); 153 fail("expected a verification exception"); 154 } catch (Throwable t) { 155 DxUtil.checkVerifyException(t); 156 } 157 } 158 159 /** 160 * @constraint B9 161 * @title types of arguments passed to method 162 */ testVFE6()163 public void testVFE6() { 164 try { 165 Class.forName("dot.junit.opcodes.invoke_static.d.T_invoke_static_20"); 166 fail("expected a verification exception"); 167 } catch (Throwable t) { 168 DxUtil.checkVerifyException(t); 169 } 170 } 171 172 173 /** 174 * @constraint n/a 175 * @title Attempt to call non-static method. Java throws IncompatibleClassChangeError 176 * on first access but Dalvik throws VerifyError on class loading. 177 */ testVFE7()178 public void testVFE7() { 179 try { 180 Class.forName("dot.junit.opcodes.invoke_static.d.T_invoke_static_5"); 181 fail("expected a verification exception"); 182 } catch (Throwable t) { 183 DxUtil.checkVerifyException(t); 184 } 185 } 186 187 /** 188 * @constraint n/a 189 * @title Attempt to call undefined method. Java throws NoSuchMethodError 190 * on first access but Dalvik throws VerifyError on class loading. 191 */ testVFE8()192 public void testVFE8() { 193 try { 194 Class.forName("dot.junit.opcodes.invoke_static.d.T_invoke_static_7"); 195 fail("expected a verification exception"); 196 } catch (Throwable t) { 197 DxUtil.checkVerifyException(t); 198 } 199 } 200 201 /** 202 * @constraint n/a 203 * @title Attempt to call private method of other class. Java throws IllegalAccessError 204 * on first access but Dalvik throws VerifyError on class loading. 205 */ testVFE9()206 public void testVFE9() { 207 //@uses dot.junit.opcodes.invoke_static.d.T_invoke_static_8 208 //@uses dot.junit.opcodes.invoke_static.TestClass 209 try { 210 Class.forName("dot.junit.opcodes.invoke_static.d.T_invoke_static_8"); 211 fail("expected a verification exception"); 212 } catch (Throwable t) { 213 DxUtil.checkVerifyException(t); 214 } 215 } 216 217 /** 218 * @constraint n/a 219 * @title Method has different signature. Java throws NoSuchMethodError 220 * on first access but Dalvik throws VerifyError on class loading. 221 */ testVFE10()222 public void testVFE10() { 223 //@uses dot.junit.opcodes.invoke_static.d.T_invoke_static_13 224 //@uses dot.junit.opcodes.invoke_static.TestClass 225 try { 226 Class.forName("dot.junit.opcodes.invoke_static.d.T_invoke_static_13"); 227 fail("expected a verification exception"); 228 } catch (Throwable t) { 229 DxUtil.checkVerifyException(t); 230 } 231 } 232 233 234 /** 235 * @constraint B12 236 * @title Attempt to call protected method of unrelated class. Java throws IllegalAccessError 237 * on first access but Dalvik throws VerifyError on class loading. 238 */ testVFE12()239 public void testVFE12() { 240 //@uses dot.junit.opcodes.invoke_static.d.T_invoke_static_17 241 //@uses dot.junit.opcodes.invoke_static.TestClass 242 try { 243 Class.forName("dot.junit.opcodes.invoke_static.d.T_invoke_static_17"); 244 fail("expected a verification exception"); 245 } catch (Throwable t) { 246 DxUtil.checkVerifyException(t); 247 } 248 } 249 250 /** 251 * @constraint A23 252 * @title number of registers 253 */ testVFE13()254 public void testVFE13() { 255 try { 256 Class.forName("dot.junit.opcodes.invoke_static.d.T_invoke_static_16"); 257 fail("expected a verification exception"); 258 } catch (Throwable t) { 259 DxUtil.checkVerifyException(t); 260 } 261 } 262 263 /** 264 * @constraint A13 265 * @title attempt to invoke interface method 266 */ testVFE18()267 public void testVFE18() { 268 try { 269 Class.forName("dot.junit.opcodes.invoke_static.d.T_invoke_static_24"); 270 fail("expected a verification exception"); 271 } catch (Throwable t) { 272 DxUtil.checkVerifyException(t); 273 } 274 } 275 } 276