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