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