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.iput; 18 19 import dot.junit.DxTestCase; 20 import dot.junit.DxUtil; 21 import dot.junit.opcodes.iput.d.T_iput_1; 22 import dot.junit.opcodes.iput.d.T_iput_10; 23 import dot.junit.opcodes.iput.d.T_iput_11; 24 import dot.junit.opcodes.iput.d.T_iput_12; 25 import dot.junit.opcodes.iput.d.T_iput_13; 26 import dot.junit.opcodes.iput.d.T_iput_14; 27 import dot.junit.opcodes.iput.d.T_iput_15; 28 import dot.junit.opcodes.iput.d.T_iput_17; 29 import dot.junit.opcodes.iput.d.T_iput_19; 30 import dot.junit.opcodes.iput.d.T_iput_5; 31 import dot.junit.opcodes.iput.d.T_iput_7; 32 import dot.junit.opcodes.iput.d.T_iput_8; 33 import dot.junit.opcodes.iput.d.T_iput_9; 34 35 public class Test_iput extends DxTestCase { 36 37 /** 38 * @title type - int 39 */ 40 public void testN1() { 41 T_iput_1 t = new T_iput_1(); 42 assertEquals(0, t.st_i1); 43 t.run(); 44 assertEquals(1000000, t.st_i1); 45 } 46 47 /** 48 * @title type - float 49 */ 50 public void testN2() { 51 T_iput_19 t = new T_iput_19(); 52 assertEquals(0.0f, t.st_f1); 53 t.run(); 54 assertEquals(3.14f, t.st_f1); 55 } 56 57 58 /** 59 * @title modification of final field 60 */ 61 public void testN3() { 62 T_iput_12 t = new T_iput_12(); 63 assertEquals(0, t.st_i1); 64 t.run(); 65 assertEquals(1000000, t.st_i1); 66 } 67 68 /** 69 * @title modification of protected field from subclass 70 */ 71 public void testN4() { 72 //@uses dot.junit.opcodes.iput.d.T_iput_1 73 //@uses dot.junit.opcodes.iput.d.T_iput_14 74 T_iput_14 t = new T_iput_14(); 75 assertEquals(0, t.getProtectedField()); 76 t.run(); 77 assertEquals(1000000, t.getProtectedField()); 78 } 79 80 /** 81 * @title Trying to put float into integer field. Dalvik doens't distinguish 32-bits types 82 * internally, so this operation makes no sense but shall not crash the VM. 83 */ 84 public void testN6() { 85 T_iput_5 t = new T_iput_5(); 86 try { 87 t.run(3.14f); 88 } catch (Throwable e) { 89 } 90 } 91 92 93 /** 94 * @title expected NullPointerException 95 */ 96 public void testE2() { 97 T_iput_13 t = new T_iput_13(); 98 try { 99 t.run(); 100 fail("expected NullPointerException"); 101 } catch (NullPointerException e) { 102 // expected 103 } 104 } 105 106 /** 107 * @constraint A11 108 * @title constant pool index 109 */ 110 public void testVFE1() { 111 try { 112 Class.forName("dot.junit.opcodes.iput.d.T_iput_3"); 113 fail("expected a verification exception"); 114 } catch (Throwable t) { 115 DxUtil.checkVerifyException(t); 116 } 117 } 118 119 /** 120 * 121 * @constraint A23 122 * @title number of registers 123 */ 124 public void testVFE2() { 125 try { 126 Class.forName("dot.junit.opcodes.iput.d.T_iput_4"); 127 fail("expected a verification exception"); 128 } catch (Throwable t) { 129 DxUtil.checkVerifyException(t); 130 } 131 } 132 133 134 /** 135 * 136 * @constraint B14 137 * @title put integer into long field - only field with same name but 138 * different type exists 139 */ 140 public void testVFE5() { 141 try { 142 new T_iput_17().run(); 143 fail("expected NoSuchFieldError"); 144 } catch (NoSuchFieldError t) { 145 DxUtil.checkVerifyException(t); 146 } 147 } 148 149 /** 150 * 151 * @constraint B14 152 * @title type of field doesn't match opcode - attempt to modify double field 153 * with single-width register 154 */ 155 public void testVFE7() { 156 try { 157 Class.forName("dot.junit.opcodes.iput.d.T_iput_18"); 158 fail("expected a verification exception"); 159 } catch (Throwable t) { 160 DxUtil.checkVerifyException(t); 161 } 162 } 163 164 /** 165 * @constraint A11 166 * @title Attempt to set static field. 167 */ 168 public void testVFE8() { 169 try { 170 new T_iput_7().run(); 171 fail("expected IncompatibleClassChangeError"); 172 } catch (IncompatibleClassChangeError t) { 173 } 174 } 175 176 /** 177 * @constraint B12 178 * @title Attempt to modify inaccessible protected field. 179 */ 180 public void testVFE9() { 181 //@uses dot.junit.opcodes.iput.TestStubs 182 //@uses dot.junit.opcodes.iput.d.T_iput_8 183 try { 184 new T_iput_8().run(); 185 fail("expected IllegalAccessError"); 186 } catch (IllegalAccessError t) { 187 } 188 } 189 190 /** 191 * @constraint n/a 192 * @title Attempt to modify field of undefined class. 193 */ 194 public void testVFE10() { 195 try { 196 new T_iput_9().run(); 197 fail("expected NoClassDefFoundError"); 198 } catch (NoClassDefFoundError t) { 199 } 200 } 201 202 /** 203 * @constraint n/a 204 * @title Attempt to modify undefined field. 205 */ 206 public void testVFE11() { 207 try { 208 new T_iput_10().run(); 209 fail("expected NoSuchFieldError"); 210 } catch (NoSuchFieldError t) { 211 } 212 } 213 214 215 216 /** 217 * @constraint n/a 218 * @title Attempt to modify superclass' private field from subclass. 219 */ 220 public void testVFE12() { 221 //@uses dot.junit.opcodes.iput.d.T_iput_1 222 //@uses dot.junit.opcodes.iput.d.T_iput_15 223 try { 224 new T_iput_15().run(); 225 fail("expected IllegalAccessError"); 226 } catch (IllegalAccessError t) { 227 DxUtil.checkVerifyException(t); 228 } 229 } 230 231 232 /** 233 * @constraint B1 234 * @title iput shall not work for wide numbers 235 */ 236 public void testVFE13() { 237 try { 238 Class.forName("dot.junit.opcodes.iput.d.T_iput_2"); 239 fail("expected a verification exception"); 240 } catch (Throwable t) { 241 DxUtil.checkVerifyException(t); 242 } 243 } 244 245 /** 246 * 247 * @constraint B1 248 * @title iput shall not work for reference fields 249 */ 250 public void testVFE14() { 251 try { 252 Class.forName("dot.junit.opcodes.iput.d.T_iput_20"); 253 fail("expected a verification exception"); 254 } catch (Throwable t) { 255 DxUtil.checkVerifyException(t); 256 } 257 } 258 259 /** 260 * 261 * @constraint B1 262 * @title iput shall not work for short fields 263 */ 264 public void testVFE15() { 265 try { 266 Class.forName("dot.junit.opcodes.iput.d.T_iput_21"); 267 fail("expected a verification exception"); 268 } catch (Throwable t) { 269 DxUtil.checkVerifyException(t); 270 } 271 } 272 273 /** 274 * 275 * @constraint B1 276 * @title iput shall not work for boolean fields 277 */ 278 public void testVFE16() { 279 try { 280 Class.forName("dot.junit.opcodes.iput.d.T_iput_22"); 281 fail("expected a verification exception"); 282 } catch (Throwable t) { 283 DxUtil.checkVerifyException(t); 284 } 285 } 286 287 /** 288 * 289 * @constraint B1 290 * @title iput shall not work for char fields 291 */ 292 public void testVFE17() { 293 try { 294 Class.forName("dot.junit.opcodes.iput.d.T_iput_23"); 295 fail("expected a verification exception"); 296 } catch (Throwable t) { 297 DxUtil.checkVerifyException(t); 298 } 299 } 300 301 /** 302 * 303 * @constraint B1 304 * @title iput shall not work for byte fields 305 */ 306 public void testVFE18() { 307 try { 308 Class.forName("dot.junit.opcodes.iput.d.T_iput_24"); 309 fail("expected a verification exception"); 310 } catch (Throwable t) { 311 DxUtil.checkVerifyException(t); 312 } 313 } 314 315 316 /** 317 * @constraint B6 318 * @title instance fields may only be accessed on already initialized instances. 319 */ 320 public void testVFE30() { 321 try { 322 Class.forName("dot.junit.opcodes.iput.d.T_iput_30"); 323 fail("expected a verification exception"); 324 } catch (Throwable t) { 325 DxUtil.checkVerifyException(t); 326 } 327 } 328 329 /** 330 * @constraint n/a 331 * @title Modification of final field in other class 332 */ 333 public void testE5() { 334 //@uses dot.junit.opcodes.iput.TestStubs 335 //@uses dot.junit.opcodes.iput.d.T_iput_11 336 try { 337 new T_iput_11().run(); 338 fail("expected IllegalAccessError"); 339 } catch (IllegalAccessError t) { 340 } 341 } 342 } 343 344