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