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.check_cast; 18 19 import dot.junit.DxTestCase; 20 import dot.junit.DxUtil; 21 import dot.junit.opcodes.check_cast.d.T_check_cast_1; 22 import dot.junit.opcodes.check_cast.d.T_check_cast_2; 23 24 25 public class Test_check_cast extends DxTestCase { 26 27 28 /** 29 * @title (String)(Object)String 30 */ testN1()31 public void testN1() { 32 T_check_cast_1 t = new T_check_cast_1(); 33 String s = ""; 34 assertEquals(s, t.run(s)); 35 } 36 37 /** 38 * @title (String)(Object)null 39 */ testN2()40 public void testN2() { 41 T_check_cast_1 t = new T_check_cast_1(); 42 assertNull(t.run(null)); 43 } 44 45 /** 46 * @title check assignment compatibility rules 47 */ testN4()48 public void testN4() { 49 T_check_cast_2 t = new T_check_cast_2(); 50 assertEquals(5, t.run()); 51 } 52 53 /** 54 * @title expected ClassCastException 55 */ testE1()56 public void testE1() { 57 T_check_cast_1 t = new T_check_cast_1(); 58 try { 59 t.run(t); 60 fail("expected ClassCastException"); 61 } catch (ClassCastException iae) { 62 // expected 63 } 64 } 65 66 /** 67 * @constraint A18 68 * @title constant pool index 69 */ testVFE1()70 public void testVFE1() { 71 try { 72 Class.forName("dot.junit.opcodes.check_cast.d.T_check_cast_4"); 73 fail("expected a verification exception"); 74 } catch (Throwable t) { 75 DxUtil.checkVerifyException(t); 76 } 77 } 78 79 /** 80 * 81 * @constraint B1 82 * @title type of argument - int 83 */ testVFE2()84 public void testVFE2() { 85 try { 86 Class.forName("dot.junit.opcodes.check_cast.d.T_check_cast_5"); 87 fail("expected a verification exception"); 88 } catch (Throwable t) { 89 DxUtil.checkVerifyException(t); 90 } 91 } 92 93 /** 94 * 95 * @constraint B1 96 * @title type of argument - long 97 */ testVFE3()98 public void testVFE3() { 99 try { 100 Class.forName("dot.junit.opcodes.check_cast.d.T_check_cast_8"); 101 fail("expected a verification exception"); 102 } catch (Throwable t) { 103 DxUtil.checkVerifyException(t); 104 } 105 } 106 107 /** 108 * 109 * @constraint B1 110 * @title number of registers 111 */ testVFE4()112 public void testVFE4() { 113 try { 114 Class.forName("dot.junit.opcodes.check_cast.d.T_check_cast_6"); 115 fail("expected a verification exception"); 116 } catch (Throwable t) { 117 DxUtil.checkVerifyException(t); 118 } 119 } 120 121 122 /** 123 * @constraint n/a 124 * @title Attempt to access inaccessible class. Java throws IllegalAccessError 125 * on first access but Dalvik throws VerifyError on class loading. 126 */ testVFE5()127 public void testVFE5() { 128 //@uses dot.junit.opcodes.check_cast.TestStubs 129 //@uses dot.junit.opcodes.check_cast.d.T_check_cast_3 130 try { 131 Class.forName("dot.junit.opcodes.check_cast.d.T_check_cast_3"); 132 fail("expected a verification exception"); 133 } catch (Throwable t) { 134 DxUtil.checkVerifyException(t); 135 } 136 } 137 138 /** 139 * @constraint n/a 140 * @title Attempt to access undefined class. Java throws NoClassDefFoundError on 141 * first access but Dalvik throws VerifyError on class loading. 142 */ testVFE6()143 public void testVFE6() { 144 try { 145 Class.forName("dot.junit.opcodes.check_cast.d.T_check_cast_7"); 146 fail("expected a verification exception"); 147 } catch (Throwable t) { 148 DxUtil.checkVerifyException(t); 149 } 150 } 151 152 /** 153 * @constraint A18 154 * @title constant pool type 155 */ testVFE7()156 public void testVFE7() { 157 try { 158 Class.forName("dot.junit.opcodes.check_cast.d.T_check_cast_9"); 159 fail("expected a verification exception"); 160 } catch (Throwable t) { 161 DxUtil.checkVerifyException(t); 162 } 163 } 164 165 } 166