1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2009 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker import other.Mutant; 18*795d594fSAndroid Build Coastguard Worker import other.InaccessibleClass; 19*795d594fSAndroid Build Coastguard Worker import other.InaccessibleMethod; 20*795d594fSAndroid Build Coastguard Worker 21*795d594fSAndroid Build Coastguard Worker /** 22*795d594fSAndroid Build Coastguard Worker * Test some problematic situations that the verifier detects. 23*795d594fSAndroid Build Coastguard Worker */ 24*795d594fSAndroid Build Coastguard Worker public class Main { 25*795d594fSAndroid Build Coastguard Worker public static final boolean VERBOSE = false; 26*795d594fSAndroid Build Coastguard Worker main(String[] args)27*795d594fSAndroid Build Coastguard Worker public static void main(String[] args) { 28*795d594fSAndroid Build Coastguard Worker testClassNewInstance(); 29*795d594fSAndroid Build Coastguard Worker testMissingStuff(); 30*795d594fSAndroid Build Coastguard Worker testBadAccess(); 31*795d594fSAndroid Build Coastguard Worker testBadInterfaceMethod(); 32*795d594fSAndroid Build Coastguard Worker } 33*795d594fSAndroid Build Coastguard Worker /** 34*795d594fSAndroid Build Coastguard Worker * Try to create and invoke a non-existent interface method. 35*795d594fSAndroid Build Coastguard Worker */ testBadInterfaceMethod()36*795d594fSAndroid Build Coastguard Worker static void testBadInterfaceMethod() { 37*795d594fSAndroid Build Coastguard Worker BadInterface badiface = new BadIfaceImpl(); 38*795d594fSAndroid Build Coastguard Worker try { 39*795d594fSAndroid Build Coastguard Worker badiface.internalClone(); 40*795d594fSAndroid Build Coastguard Worker } catch (IncompatibleClassChangeError icce) { 41*795d594fSAndroid Build Coastguard Worker // TODO b/64274113 This should really be an NSME 42*795d594fSAndroid Build Coastguard Worker System.out.println("Got expected IncompatibleClassChangeError (interface)"); 43*795d594fSAndroid Build Coastguard Worker if (VERBOSE) System.out.println("--- " + icce); 44*795d594fSAndroid Build Coastguard Worker } 45*795d594fSAndroid Build Coastguard Worker } 46*795d594fSAndroid Build Coastguard Worker 47*795d594fSAndroid Build Coastguard Worker /** 48*795d594fSAndroid Build Coastguard Worker * Try to create a new instance of an abstract class. 49*795d594fSAndroid Build Coastguard Worker */ testClassNewInstance()50*795d594fSAndroid Build Coastguard Worker static void testClassNewInstance() { 51*795d594fSAndroid Build Coastguard Worker try { 52*795d594fSAndroid Build Coastguard Worker MaybeAbstract ma = new MaybeAbstract(); 53*795d594fSAndroid Build Coastguard Worker System.out.println("ERROR: MaybeAbstract succeeded unexpectedly"); 54*795d594fSAndroid Build Coastguard Worker } catch (InstantiationError ie) { 55*795d594fSAndroid Build Coastguard Worker System.out.println("Got expected InstantationError"); 56*795d594fSAndroid Build Coastguard Worker if (VERBOSE) System.out.println("--- " + ie); 57*795d594fSAndroid Build Coastguard Worker } catch (Exception ex) { 58*795d594fSAndroid Build Coastguard Worker System.out.println("Got unexpected MaybeAbstract failure"); 59*795d594fSAndroid Build Coastguard Worker } 60*795d594fSAndroid Build Coastguard Worker } 61*795d594fSAndroid Build Coastguard Worker 62*795d594fSAndroid Build Coastguard Worker /** 63*795d594fSAndroid Build Coastguard Worker * Test stuff that disappears. 64*795d594fSAndroid Build Coastguard Worker */ testMissingStuff()65*795d594fSAndroid Build Coastguard Worker static void testMissingStuff() { 66*795d594fSAndroid Build Coastguard Worker Mutant mutant = new Mutant(); 67*795d594fSAndroid Build Coastguard Worker 68*795d594fSAndroid Build Coastguard Worker try { 69*795d594fSAndroid Build Coastguard Worker int x = mutant.disappearingField; 70*795d594fSAndroid Build Coastguard Worker } catch (NoSuchFieldError nsfe) { 71*795d594fSAndroid Build Coastguard Worker System.out.println("Got expected NoSuchFieldError"); 72*795d594fSAndroid Build Coastguard Worker if (VERBOSE) System.out.println("--- " + nsfe); 73*795d594fSAndroid Build Coastguard Worker } 74*795d594fSAndroid Build Coastguard Worker 75*795d594fSAndroid Build Coastguard Worker try { 76*795d594fSAndroid Build Coastguard Worker int y = Mutant.disappearingStaticField; 77*795d594fSAndroid Build Coastguard Worker } catch (NoSuchFieldError nsfe) { 78*795d594fSAndroid Build Coastguard Worker System.out.println("Got expected NoSuchFieldError"); 79*795d594fSAndroid Build Coastguard Worker if (VERBOSE) System.out.println("--- " + nsfe); 80*795d594fSAndroid Build Coastguard Worker } 81*795d594fSAndroid Build Coastguard Worker 82*795d594fSAndroid Build Coastguard Worker try { 83*795d594fSAndroid Build Coastguard Worker mutant.disappearingMethod(); 84*795d594fSAndroid Build Coastguard Worker } catch (NoSuchMethodError nsme) { 85*795d594fSAndroid Build Coastguard Worker System.out.println("Got expected NoSuchMethodError"); 86*795d594fSAndroid Build Coastguard Worker if (VERBOSE) System.out.println("--- " + nsme); 87*795d594fSAndroid Build Coastguard Worker } 88*795d594fSAndroid Build Coastguard Worker 89*795d594fSAndroid Build Coastguard Worker try { 90*795d594fSAndroid Build Coastguard Worker Mutant.disappearingStaticMethod(); 91*795d594fSAndroid Build Coastguard Worker } catch (NoSuchMethodError nsme) { 92*795d594fSAndroid Build Coastguard Worker System.out.println("Got expected NoSuchMethodError"); 93*795d594fSAndroid Build Coastguard Worker if (VERBOSE) System.out.println("--- " + nsme); 94*795d594fSAndroid Build Coastguard Worker } 95*795d594fSAndroid Build Coastguard Worker } 96*795d594fSAndroid Build Coastguard Worker 97*795d594fSAndroid Build Coastguard Worker /** 98*795d594fSAndroid Build Coastguard Worker * Test stuff that becomes inaccessible. 99*795d594fSAndroid Build Coastguard Worker */ testBadAccess()100*795d594fSAndroid Build Coastguard Worker static void testBadAccess() { 101*795d594fSAndroid Build Coastguard Worker Mutant mutant = new Mutant(); 102*795d594fSAndroid Build Coastguard Worker 103*795d594fSAndroid Build Coastguard Worker try { 104*795d594fSAndroid Build Coastguard Worker int x = mutant.inaccessibleField; 105*795d594fSAndroid Build Coastguard Worker System.out.println("ERROR: bad access succeeded (ifield)"); 106*795d594fSAndroid Build Coastguard Worker } catch (IllegalAccessError iae) { 107*795d594fSAndroid Build Coastguard Worker System.out.println("Got expected IllegalAccessError (ifield)"); 108*795d594fSAndroid Build Coastguard Worker if (VERBOSE) System.out.println("--- " + iae); 109*795d594fSAndroid Build Coastguard Worker } 110*795d594fSAndroid Build Coastguard Worker 111*795d594fSAndroid Build Coastguard Worker try { 112*795d594fSAndroid Build Coastguard Worker int y = Mutant.inaccessibleStaticField; 113*795d594fSAndroid Build Coastguard Worker System.out.println("ERROR: bad access succeeded (sfield)"); 114*795d594fSAndroid Build Coastguard Worker } catch (IllegalAccessError iae) { 115*795d594fSAndroid Build Coastguard Worker System.out.println("Got expected IllegalAccessError (sfield)"); 116*795d594fSAndroid Build Coastguard Worker if (VERBOSE) System.out.println("--- " + iae); 117*795d594fSAndroid Build Coastguard Worker } 118*795d594fSAndroid Build Coastguard Worker 119*795d594fSAndroid Build Coastguard Worker try { 120*795d594fSAndroid Build Coastguard Worker mutant.inaccessibleMethod(); 121*795d594fSAndroid Build Coastguard Worker System.out.println("ERROR: bad access succeeded (method)"); 122*795d594fSAndroid Build Coastguard Worker } catch (IllegalAccessError iae) { 123*795d594fSAndroid Build Coastguard Worker System.out.println("Got expected IllegalAccessError (method)"); 124*795d594fSAndroid Build Coastguard Worker if (VERBOSE) System.out.println("--- " + iae); 125*795d594fSAndroid Build Coastguard Worker } 126*795d594fSAndroid Build Coastguard Worker 127*795d594fSAndroid Build Coastguard Worker try { 128*795d594fSAndroid Build Coastguard Worker Mutant.inaccessibleStaticMethod(); 129*795d594fSAndroid Build Coastguard Worker System.out.println("ERROR: bad access succeeded (smethod)"); 130*795d594fSAndroid Build Coastguard Worker } catch (IllegalAccessError iae) { 131*795d594fSAndroid Build Coastguard Worker System.out.println("Got expected IllegalAccessError (smethod)"); 132*795d594fSAndroid Build Coastguard Worker if (VERBOSE) System.out.println("--- " + iae); 133*795d594fSAndroid Build Coastguard Worker } 134*795d594fSAndroid Build Coastguard Worker 135*795d594fSAndroid Build Coastguard Worker try { 136*795d594fSAndroid Build Coastguard Worker /* accessible static method in an inaccessible class */ 137*795d594fSAndroid Build Coastguard Worker InaccessibleClass.test(); 138*795d594fSAndroid Build Coastguard Worker System.out.println("ERROR: bad meth-class access succeeded (meth-class)"); 139*795d594fSAndroid Build Coastguard Worker } catch (IllegalAccessError iae) { 140*795d594fSAndroid Build Coastguard Worker System.out.println("Got expected IllegalAccessError (meth-class)"); 141*795d594fSAndroid Build Coastguard Worker if (VERBOSE) System.out.println("--- " + iae); 142*795d594fSAndroid Build Coastguard Worker } 143*795d594fSAndroid Build Coastguard Worker 144*795d594fSAndroid Build Coastguard Worker try { 145*795d594fSAndroid Build Coastguard Worker /* accessible static field in an inaccessible class */ 146*795d594fSAndroid Build Coastguard Worker int blah = InaccessibleClass.blah; 147*795d594fSAndroid Build Coastguard Worker System.out.println("ERROR: bad field-class access succeeded (field-class)"); 148*795d594fSAndroid Build Coastguard Worker } catch (IllegalAccessError iae) { 149*795d594fSAndroid Build Coastguard Worker System.out.println("Got expected IllegalAccessError (field-class)"); 150*795d594fSAndroid Build Coastguard Worker if (VERBOSE) System.out.println("--- " + iae); 151*795d594fSAndroid Build Coastguard Worker } 152*795d594fSAndroid Build Coastguard Worker 153*795d594fSAndroid Build Coastguard Worker try { 154*795d594fSAndroid Build Coastguard Worker /* inaccessible static method in an accessible class */ 155*795d594fSAndroid Build Coastguard Worker InaccessibleMethod.test(); 156*795d594fSAndroid Build Coastguard Worker System.out.println("ERROR: bad access succeeded (meth-meth)"); 157*795d594fSAndroid Build Coastguard Worker } catch (IllegalAccessError iae) { 158*795d594fSAndroid Build Coastguard Worker System.out.println("Got expected IllegalAccessError (meth-meth)"); 159*795d594fSAndroid Build Coastguard Worker if (VERBOSE) System.out.println("--- " + iae); 160*795d594fSAndroid Build Coastguard Worker } 161*795d594fSAndroid Build Coastguard Worker } 162*795d594fSAndroid Build Coastguard Worker } 163