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.sget; 18 19 import dot.junit.DxTestCase; 20 import dot.junit.DxUtil; 21 import dot.junit.opcodes.sget.d.T_sget_1; 22 import dot.junit.opcodes.sget.d.T_sget_11; 23 import dot.junit.opcodes.sget.d.T_sget_2; 24 import dot.junit.opcodes.sget.d.T_sget_5; 25 import dot.junit.opcodes.sget.d.T_sget_9; 26 27 public class Test_sget extends DxTestCase { 28 29 /** 30 * @title type - int 31 */ testN1()32 public void testN1() { 33 T_sget_1 t = new T_sget_1(); 34 assertEquals(5, t.run()); 35 } 36 37 /** 38 * @title type - float 39 */ testN2()40 public void testN2() { 41 T_sget_2 t = new T_sget_2(); 42 assertEquals(123f, t.run()); 43 } 44 45 /** 46 * @title access protected field from subclass 47 */ testN3()48 public void testN3() { 49 //@uses dot.junit.opcodes.sget.d.T_sget_1 50 //@uses dot.junit.opcodes.sget.d.T_sget_11 51 T_sget_11 t = new T_sget_11(); 52 assertEquals(10, t.run()); 53 } 54 55 /** 56 * @constraint A12 57 * @title attempt to access non-static field 58 */ testE1()59 public void testE1() { 60 T_sget_5 t = new T_sget_5(); 61 try { 62 t.run(); 63 fail("expected IncompatibleClassChangeError"); 64 } catch (IncompatibleClassChangeError e) { 65 // expected 66 } 67 } 68 69 /** 70 * @title initialization of referenced class throws exception 71 */ testE6()72 public void testE6() { 73 T_sget_9 t = new T_sget_9(); 74 try { 75 t.run(); 76 fail("expected Error"); 77 } catch (Error e) { 78 // expected 79 } 80 } 81 82 /** 83 * @constraint A12 84 * @title constant pool index 85 */ testVFE1()86 public void testVFE1() { 87 try { 88 Class.forName("dot.junit.opcodes.sget.d.T_sget_4"); 89 fail("expected a verification exception"); 90 } catch (Throwable t) { 91 DxUtil.checkVerifyException(t); 92 } 93 } 94 95 /** 96 * 97 * @constraint A23 98 * @title number of registers 99 */ testVFE2()100 public void testVFE2() { 101 try { 102 Class.forName("dot.junit.opcodes.sget.d.T_sget_3"); 103 fail("expected a verification exception"); 104 } catch (Throwable t) { 105 DxUtil.checkVerifyException(t); 106 } 107 } 108 109 /** 110 * 111 * @constraint B13 112 * @title read integer from long field - only field with same name but 113 * different type exists 114 */ testVFE3()115 public void testVFE3() { 116 try { 117 Class.forName("dot.junit.opcodes.sget.d.T_sget_13"); 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 inaccessible field. Java throws IllegalAccessError 127 * on first access but Dalvik throws VerifyError on class loading. 128 */ testVFE4()129 public void testVFE4() { 130 //@uses dot.junit.opcodes.sget.d.T_sget_6 131 //@uses dot.junit.opcodes.sget.TestStubs 132 try { 133 Class.forName("dot.junit.opcodes.sget.d.T_sget_6"); 134 fail("expected a verification exception"); 135 } catch (Throwable t) { 136 DxUtil.checkVerifyException(t); 137 } 138 } 139 140 /** 141 * @constraint n/a 142 * @title Attempt to read field of undefined class. Java throws NoClassDefFoundError 143 * on first access but Dalvik throws VerifyError on class loading. 144 */ testVFE5()145 public void testVFE5() { 146 try { 147 Class.forName("dot.junit.opcodes.sget.d.T_sget_7"); 148 fail("expected a verification exception"); 149 } catch (Throwable t) { 150 DxUtil.checkVerifyException(t); 151 } 152 } 153 154 /** 155 * @constraint n/a 156 * @title Attempt to read undefined field. Java throws NoSuchFieldError 157 * on first access but Dalvik throws VerifyError on class loading. 158 */ testVFE6()159 public void testVFE6() { 160 try { 161 Class.forName("dot.junit.opcodes.sget.d.T_sget_8"); 162 fail("expected a verification exception"); 163 } catch (Throwable t) { 164 DxUtil.checkVerifyException(t); 165 } 166 } 167 168 /** 169 * @constraint n/a 170 * @title Attempt to read superclass' private field from subclass. Java 171 * throws IllegalAccessError on first access but Dalvik throws VerifyError on class loading. 172 */ testVFE7()173 public void testVFE7() { 174 //@uses dot.junit.opcodes.sget.d.T_sget_12 175 //@uses dot.junit.opcodes.sget.d.T_sget_1 176 try { 177 Class.forName("dot.junit.opcodes.sget.d.T_sget_12"); 178 fail("expected a verification exception"); 179 } catch (Throwable t) { 180 DxUtil.checkVerifyException(t); 181 } 182 } 183 184 /** 185 * @constraint B1 186 * @title sget shall not work for reference fields 187 */ testVFE8()188 public void testVFE8() { 189 try { 190 Class.forName("dot.junit.opcodes.sget.d.T_sget_14"); 191 fail("expected a verification exception"); 192 } catch (Throwable t) { 193 DxUtil.checkVerifyException(t); 194 } 195 } 196 197 /** 198 * 199 * @constraint B1 200 * @title sget shall not work for short fields 201 */ testVFE9()202 public void testVFE9() { 203 try { 204 Class.forName("dot.junit.opcodes.sget.d.T_sget_15"); 205 fail("expected a verification exception"); 206 } catch (Throwable t) { 207 DxUtil.checkVerifyException(t); 208 } 209 } 210 211 /** 212 * 213 * @constraint B1 214 * @title sget shall not work for boolean fields 215 */ testVFE10()216 public void testVFE10() { 217 try { 218 Class.forName("dot.junit.opcodes.sget.d.T_sget_16"); 219 fail("expected a verification exception"); 220 } catch (Throwable t) { 221 DxUtil.checkVerifyException(t); 222 } 223 } 224 225 /** 226 * 227 * @constraint B1 228 * @title sget shall not work for char fields 229 */ testVFE11()230 public void testVFE11() { 231 try { 232 Class.forName("dot.junit.opcodes.sget.d.T_sget_17"); 233 fail("expected a verification exception"); 234 } catch (Throwable t) { 235 DxUtil.checkVerifyException(t); 236 } 237 } 238 239 /** 240 * 241 * @constraint B1 242 * @title sget shall not work for byte fields 243 */ testVFE12()244 public void testVFE12() { 245 try { 246 Class.forName("dot.junit.opcodes.sget.d.T_sget_18"); 247 fail("expected a verification exception"); 248 } catch (Throwable t) { 249 DxUtil.checkVerifyException(t); 250 } 251 } 252 253 /** 254 * 255 * @constraint B1 256 * @title sget shall not work for double fields 257 */ testVFE13()258 public void testVFE13() { 259 try { 260 Class.forName("dot.junit.opcodes.sget.d.T_sget_19"); 261 fail("expected a verification exception"); 262 } catch (Throwable t) { 263 DxUtil.checkVerifyException(t); 264 } 265 } 266 267 /** 268 * 269 * @constraint B1 270 * @title sget shall not work for long fields 271 */ testVFE14()272 public void testVFE14() { 273 try { 274 Class.forName("dot.junit.opcodes.sget.d.T_sget_20"); 275 fail("expected a verification exception"); 276 } catch (Throwable t) { 277 DxUtil.checkVerifyException(t); 278 } 279 } 280 } 281