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.iget; 18 19 import dot.junit.DxTestCase; 20 import dot.junit.DxUtil; 21 import dot.junit.opcodes.iget.d.T_iget_1; 22 import dot.junit.opcodes.iget.d.T_iget_11; 23 import dot.junit.opcodes.iget.d.T_iget_2; 24 import dot.junit.opcodes.iget.d.T_iget_9; 25 26 public class Test_iget extends DxTestCase { 27 28 /** 29 * @title type - int 30 */ testN1()31 public void testN1() { 32 T_iget_1 t = new T_iget_1(); 33 assertEquals(5, t.run()); 34 } 35 36 /** 37 * @title type - float 38 */ testN2()39 public void testN2() { 40 T_iget_2 t = new T_iget_2(); 41 assertEquals(123f, t.run()); 42 } 43 44 /** 45 * @title access protected field from subclass 46 */ testN3()47 public void testN3() { 48 //@uses dot.junit.opcodes.iget.d.T_iget_1 49 //@uses dot.junit.opcodes.iget.d.T_iget_11 50 T_iget_11 t = new T_iget_11(); 51 assertEquals(10, t.run()); 52 } 53 54 /** 55 * @title expected NullPointerException 56 */ testE2()57 public void testE2() { 58 T_iget_9 t = new T_iget_9(); 59 try { 60 t.run(); 61 fail("expected NullPointerException"); 62 } catch (NullPointerException e) { 63 // expected 64 } 65 } 66 67 /** 68 * @constraint A11 69 * @title constant pool index 70 */ testVFE1()71 public void testVFE1() { 72 try { 73 Class.forName("dot.junit.opcodes.iget.d.T_iget_4"); 74 fail("expected a verification exception"); 75 } catch (Throwable t) { 76 DxUtil.checkVerifyException(t); 77 } 78 } 79 80 /** 81 * 82 * @constraint A23 83 * @title number of registers 84 */ testVFE2()85 public void testVFE2() { 86 try { 87 Class.forName("dot.junit.opcodes.iget.d.T_iget_3"); 88 fail("expected a verification exception"); 89 } catch (Throwable t) { 90 DxUtil.checkVerifyException(t); 91 } 92 } 93 94 /** 95 * @constraint B13 96 * @title read integer from long field - only field with same name but 97 * different type exist 98 */ testVFE3()99 public void testVFE3() { 100 try { 101 Class.forName("dot.junit.opcodes.iget.d.T_iget_13"); 102 fail("expected a verification exception"); 103 } catch (Throwable t) { 104 DxUtil.checkVerifyException(t); 105 } 106 } 107 108 /** 109 * @constraint n/a 110 * @title Attempt to read inaccessible private field. Java throws IllegalAccessError 111 * on first access but Dalvik throws VerifyError on class loading. 112 */ testVFE4()113 public void testVFE4() { 114 //@uses dot.junit.opcodes.iget.d.T_iget_6 115 //@uses dot.junit.opcodes.iget.TestStubs 116 try { 117 Class.forName("dot.junit.opcodes.iget.d.T_iget_6"); 118 fail("expected a verification exception"); 119 } catch (Throwable t) { 120 DxUtil.checkVerifyException(t); 121 } 122 } 123 124 /** 125 * @constraint n/a 126 * @title Attempt to read field of undefined class. Java throws NoClassDefFoundError 127 * on first access but Dalvik throws VerifyError on class loading. 128 */ testVFE5()129 public void testVFE5() { 130 try { 131 Class.forName("dot.junit.opcodes.iget.d.T_iget_7"); 132 fail("expected a verification exception"); 133 } catch (Throwable t) { 134 DxUtil.checkVerifyException(t); 135 } 136 } 137 138 /** 139 * @constraint n/a 140 * @title Attempt to read undefined field. Java throws NoSuchFieldError 141 * on first access but Dalvik throws VerifyError on class loading. 142 */ testVFE6()143 public void testVFE6() { 144 try { 145 Class.forName("dot.junit.opcodes.iget.d.T_iget_8"); 146 fail("expected a verification exception"); 147 } catch (Throwable t) { 148 DxUtil.checkVerifyException(t); 149 } 150 } 151 152 /** 153 * @constraint n/a 154 * @title Attempt to read superclass' private field from subclass. Java 155 * throws IllegalAccessError on first access but Dalvik throws VerifyError on class loading. 156 */ testVFE7()157 public void testVFE7() { 158 //@uses dot.junit.opcodes.iget.d.T_iget_12 159 //@uses dot.junit.opcodes.iget.d.T_iget_1 160 try { 161 Class.forName("dot.junit.opcodes.iget.d.T_iget_12"); 162 fail("expected a verification exception"); 163 } catch (Throwable t) { 164 DxUtil.checkVerifyException(t); 165 } 166 } 167 168 /** 169 * @constraint B1 170 * @title iget shall not work for reference fields 171 */ testVFE8()172 public void testVFE8() { 173 try { 174 Class.forName("dot.junit.opcodes.iget.d.T_iget_14"); 175 fail("expected a verification exception"); 176 } catch (Throwable t) { 177 DxUtil.checkVerifyException(t); 178 } 179 } 180 181 /** 182 * 183 * @constraint B1 184 * @title iget shall not work for short fields 185 */ testVFE9()186 public void testVFE9() { 187 try { 188 Class.forName("dot.junit.opcodes.iget.d.T_iget_15"); 189 fail("expected a verification exception"); 190 } catch (Throwable t) { 191 DxUtil.checkVerifyException(t); 192 } 193 } 194 195 /** 196 * 197 * @constraint B1 198 * @title iget shall not work for boolean fields 199 */ testVFE10()200 public void testVFE10() { 201 try { 202 Class.forName("dot.junit.opcodes.iget.d.T_iget_16"); 203 fail("expected a verification exception"); 204 } catch (Throwable t) { 205 DxUtil.checkVerifyException(t); 206 } 207 } 208 209 /** 210 * 211 * @constraint B1 212 * @title iget shall not work for char fields 213 */ testVFE11()214 public void testVFE11() { 215 try { 216 Class.forName("dot.junit.opcodes.iget.d.T_iget_17"); 217 fail("expected a verification exception"); 218 } catch (Throwable t) { 219 DxUtil.checkVerifyException(t); 220 } 221 } 222 223 /** 224 * 225 * @constraint B1 226 * @title iget shall not work for byte fields 227 */ testVFE12()228 public void testVFE12() { 229 try { 230 Class.forName("dot.junit.opcodes.iget.d.T_iget_18"); 231 fail("expected a verification exception"); 232 } catch (Throwable t) { 233 DxUtil.checkVerifyException(t); 234 } 235 } 236 237 /** 238 * 239 * @constraint B1 240 * @title iget shall not work for double fields 241 */ testVFE13()242 public void testVFE13() { 243 try { 244 Class.forName("dot.junit.opcodes.iget.d.T_iget_19"); 245 fail("expected a verification exception"); 246 } catch (Throwable t) { 247 DxUtil.checkVerifyException(t); 248 } 249 } 250 251 /** 252 * 253 * @constraint B1 254 * @title iget shall not work for long fields 255 */ testVFE14()256 public void testVFE14() { 257 try { 258 Class.forName("dot.junit.opcodes.iget.d.T_iget_20"); 259 fail("expected a verification exception"); 260 } catch (Throwable t) { 261 DxUtil.checkVerifyException(t); 262 } 263 } 264 265 /** 266 * @constraint B12 267 * @title Attempt to read protected field of unrelated class. Java throws IllegalAccessError 268 * on first access but Dalvik throws VerifyError on class loading. 269 */ testVFE15()270 public void testVFE15() { 271 //@uses dot.junit.opcodes.iget.d.T_iget_21 272 //@uses dot.junit.opcodes.iget.TestStubs 273 try { 274 Class.forName("dot.junit.opcodes.iget.d.T_iget_21"); 275 fail("expected a verification exception"); 276 } catch (Throwable t) { 277 DxUtil.checkVerifyException(t); 278 } 279 } 280 281 282 /** 283 * @constraint A11 284 * @title Attempt to read static field. Java throws IncompatibleClassChangeError 285 * on first access but Dalvik throws VerifyError on class loading. 286 */ testVFE16()287 public void testVFE16() { 288 //@uses dot.junit.opcodes.iget.d.T_iget_5 289 //@uses dot.junit.opcodes.iget.TestStubs 290 try { 291 Class.forName("dot.junit.opcodes.iget.d.T_iget_5"); 292 fail("expected a verification exception"); 293 } catch (Throwable t) { 294 DxUtil.checkVerifyException(t); 295 } 296 } 297 298 299 /** 300 * @constraint B6 301 * @title instance fields may only be accessed on already initialized instances. 302 */ testVFE30()303 public void testVFE30() { 304 try { 305 Class.forName("dot.junit.opcodes.iget.d.T_iget_30"); 306 fail("expected a verification exception"); 307 } catch (Throwable t) { 308 DxUtil.checkVerifyException(t); 309 } 310 } 311 } 312 313