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