Home | History | Annotate | Download | only in sput
      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      * @title Trying to put float into integer field. Dalvik doens't distinguish 32-bits types
     83      * internally, so this operation makes no sense but shall not crash the VM.
     84      */
     85     public void testN6() {
     86         T_sput_5 t = new  T_sput_5();
     87         try {
     88             t.run(3.14f);
     89         } catch (Throwable e) {
     90         }
     91     }
     92 
     93 
     94 
     95     /**
     96      * @title initialization of referenced class throws exception
     97      */
     98     public void testE6() {
     99         T_sput_13 t = new T_sput_13();
    100         try {
    101             t.run();
    102             fail("expected Error");
    103         } catch (Error e) {
    104             // expected
    105         }
    106     }
    107 
    108     /**
    109      * @constraint A12
    110      * @title  constant pool index
    111      */
    112     public void testVFE1() {
    113         try {
    114             Class.forName("dot.junit.opcodes.sput.d.T_sput_3");
    115             fail("expected a verification exception");
    116         } catch (Throwable t) {
    117             DxUtil.checkVerifyException(t);
    118         }
    119     }
    120 
    121     /**
    122      *
    123      * @constraint A23
    124      * @title  number of registers
    125      */
    126     public void testVFE2() {
    127         try {
    128             Class.forName("dot.junit.opcodes.sput.d.T_sput_4");
    129             fail("expected a verification exception");
    130         } catch (Throwable t) {
    131             DxUtil.checkVerifyException(t);
    132         }
    133     }
    134 
    135 
    136     /**
    137      *
    138      * @constraint B13
    139      * @title  put integer into long field - only field with same name but
    140      * different type exists
    141      */
    142     public void testVFE5() {
    143         try {
    144             new T_sput_17().run();
    145             fail("expected NoSuchFieldError");
    146         } catch (NoSuchFieldError t) {
    147         }
    148     }
    149 
    150     /**
    151      *
    152      * @constraint B13
    153      * @title type of field doesn't match opcode - attempt to modify double field
    154      * with single-width register
    155      */
    156     public void testVFE7() {
    157         try {
    158             Class.forName("dot.junit.opcodes.sput.d.T_sput_18");
    159             fail("expected a verification exception");
    160         } catch (Throwable t) {
    161             DxUtil.checkVerifyException(t);
    162         }
    163     }
    164 
    165     /**
    166      *
    167      * @constraint A12
    168      * @title Attempt to set non-static field.
    169      */
    170     public void testVFE8() {
    171          try {
    172              new T_sput_7().run();
    173              fail("expected IncompatibleClassChangeError");
    174          } catch (IncompatibleClassChangeError t) {
    175          }
    176     }
    177 
    178     /**
    179      * @constraint n/a
    180      * @title Attempt to modify inaccessible field.
    181      */
    182     public void testVFE9() {
    183         //@uses dot.junit.opcodes.sput.TestStubs
    184         //@uses dot.junit.opcodes.sput.d.T_sput_8
    185         try {
    186             new T_sput_8().run();
    187             fail("expected IllegalAccessError");
    188         } catch (IllegalAccessError t) {
    189         }
    190     }
    191 
    192     /**
    193      * @constraint n/a
    194      * @title Attempt to modify field of undefined class.
    195      */
    196     public void testVFE10() {
    197         try {
    198             new T_sput_9().run();
    199             fail("expected NoClassDefFoundError");
    200         } catch (NoClassDefFoundError t) {
    201         }
    202     }
    203 
    204     /**
    205      * @constraint n/a
    206      * @title Attempt to modify undefined field.
    207      */
    208     public void testVFE11() {
    209         try {
    210             new T_sput_10().run();
    211             fail("expected NoSuchFieldError");
    212         } catch (NoSuchFieldError t) {
    213         }
    214     }
    215 
    216 
    217 
    218     /**
    219      * @constraint n/a
    220      * @title Attempt to modify superclass' private field from subclass.
    221      */
    222     public void testVFE12() {
    223         //@uses dot.junit.opcodes.sput.d.T_sput_1
    224         //@uses dot.junit.opcodes.sput.d.T_sput_15
    225         try {
    226              new T_sput_15().run();
    227             fail("expected IllegalAccessError");
    228         } catch (IllegalAccessError t) {
    229         }
    230     }
    231 
    232 
    233     /**
    234      * @constraint B1
    235      * @title sput shall not work for wide numbers
    236      */
    237     public void testVFE13() {
    238         try {
    239             Class.forName("dot.junit.opcodes.sput.d.T_sput_2");
    240             fail("expected a verification exception");
    241         } catch (Throwable t) {
    242             DxUtil.checkVerifyException(t);
    243         }
    244     }
    245 
    246     /**
    247      *
    248      * @constraint B1
    249      * @title sput shall not work for reference fields
    250      */
    251     public void testVFE14() {
    252         try {
    253             Class.forName("dot.junit.opcodes.sput.d.T_sput_20");
    254             fail("expected a verification exception");
    255         } catch (Throwable t) {
    256             DxUtil.checkVerifyException(t);
    257         }
    258     }
    259 
    260     /**
    261      *
    262      * @constraint B1
    263      * @title sput shall not work for short fields
    264      */
    265     public void testVFE15() {
    266         try {
    267             Class.forName("dot.junit.opcodes.sput.d.T_sput_21");
    268             fail("expected a verification exception");
    269         } catch (Throwable t) {
    270             DxUtil.checkVerifyException(t);
    271         }
    272     }
    273 
    274     /**
    275      *
    276      * @constraint B1
    277      * @title sput shall not work for boolean fields
    278      */
    279     public void testVFE16() {
    280         try {
    281             Class.forName("dot.junit.opcodes.sput.d.T_sput_22");
    282             fail("expected a verification exception");
    283         } catch (Throwable t) {
    284             DxUtil.checkVerifyException(t);
    285         }
    286     }
    287 
    288     /**
    289      *
    290      * @constraint B1
    291      * @title sput shall not work for char fields
    292      */
    293     public void testVFE17() {
    294         try {
    295             Class.forName("dot.junit.opcodes.sput.d.T_sput_23");
    296             fail("expected a verification exception");
    297         } catch (Throwable t) {
    298             DxUtil.checkVerifyException(t);
    299         }
    300     }
    301 
    302     /**
    303      *
    304      * @constraint B1
    305      * @title sput shall not work for byte fields
    306      */
    307     public void testVFE18() {
    308         try {
    309             Class.forName("dot.junit.opcodes.sput.d.T_sput_24");
    310             fail("expected a verification exception");
    311         } catch (Throwable t) {
    312             DxUtil.checkVerifyException(t);
    313         }
    314     }
    315 
    316     /**
    317      * @constraint n/a
    318      * @title Modification of final field in other class
    319      */
    320     public void testVFE19() {
    321         //@uses dot.junit.opcodes.sput.TestStubs
    322         //@uses dot.junit.opcodes.sput.d.T_sput_11
    323     	try {
    324             new T_sput_11().run();
    325             fail("expected a verification exception");
    326         } catch (IllegalAccessError t) {
    327         }
    328     }
    329 
    330 }
    331