• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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 import other.Mutant;
18 import other.InaccessibleClass;
19 import other.InaccessibleMethod;
20 
21 /**
22  * Test some problematic situations that the verifier detects.
23  */
24 public class Main {
25     public static final boolean VERBOSE = false;
26 
main(String[] args)27     public static void main(String[] args) {
28         testClassNewInstance();
29         testMissingStuff();
30         testBadAccess();
31         testBadInterfaceMethod();
32      }
33     /**
34      * Try to create and invoke a non-existent interface method.
35      */
testBadInterfaceMethod()36     static void testBadInterfaceMethod() {
37         BadInterface badiface = new BadIfaceImpl();
38         try {
39             badiface.internalClone();
40         } catch (IncompatibleClassChangeError icce) {
41             // TODO b/64274113 This should really be an NSME
42             System.out.println("Got expected IncompatibleClassChangeError (interface)");
43             if (VERBOSE) System.out.println("--- " + icce);
44         }
45     }
46 
47     /**
48      * Try to create a new instance of an abstract class.
49      */
testClassNewInstance()50     static void testClassNewInstance() {
51         try {
52             MaybeAbstract ma = new MaybeAbstract();
53             System.out.println("ERROR: MaybeAbstract succeeded unexpectedly");
54         } catch (InstantiationError ie) {
55             System.out.println("Got expected InstantationError");
56             if (VERBOSE) System.out.println("--- " + ie);
57         } catch (Exception ex) {
58             System.out.println("Got unexpected MaybeAbstract failure");
59         }
60     }
61 
62     /**
63      * Test stuff that disappears.
64      */
testMissingStuff()65     static void testMissingStuff() {
66         Mutant mutant = new Mutant();
67 
68         try {
69             int x = mutant.disappearingField;
70         } catch (NoSuchFieldError nsfe) {
71             System.out.println("Got expected NoSuchFieldError");
72             if (VERBOSE) System.out.println("--- " + nsfe);
73         }
74 
75         try {
76             int y = Mutant.disappearingStaticField;
77         } catch (NoSuchFieldError nsfe) {
78             System.out.println("Got expected NoSuchFieldError");
79             if (VERBOSE) System.out.println("--- " + nsfe);
80         }
81 
82         try {
83             mutant.disappearingMethod();
84         } catch (NoSuchMethodError nsme) {
85             System.out.println("Got expected NoSuchMethodError");
86             if (VERBOSE) System.out.println("--- " + nsme);
87         }
88 
89         try {
90             Mutant.disappearingStaticMethod();
91         } catch (NoSuchMethodError nsme) {
92             System.out.println("Got expected NoSuchMethodError");
93             if (VERBOSE) System.out.println("--- " + nsme);
94         }
95     }
96 
97     /**
98      * Test stuff that becomes inaccessible.
99      */
testBadAccess()100     static void testBadAccess() {
101         Mutant mutant = new Mutant();
102 
103         try {
104             int x = mutant.inaccessibleField;
105             System.out.println("ERROR: bad access succeeded (ifield)");
106         } catch (IllegalAccessError iae) {
107             System.out.println("Got expected IllegalAccessError (ifield)");
108             if (VERBOSE) System.out.println("--- " + iae);
109         }
110 
111         try {
112             int y = Mutant.inaccessibleStaticField;
113             System.out.println("ERROR: bad access succeeded (sfield)");
114         } catch (IllegalAccessError iae) {
115             System.out.println("Got expected IllegalAccessError (sfield)");
116             if (VERBOSE) System.out.println("--- " + iae);
117         }
118 
119         try {
120             mutant.inaccessibleMethod();
121             System.out.println("ERROR: bad access succeeded (method)");
122         } catch (IllegalAccessError iae) {
123             System.out.println("Got expected IllegalAccessError (method)");
124             if (VERBOSE) System.out.println("--- " + iae);
125         }
126 
127         try {
128             Mutant.inaccessibleStaticMethod();
129             System.out.println("ERROR: bad access succeeded (smethod)");
130         } catch (IllegalAccessError iae) {
131             System.out.println("Got expected IllegalAccessError (smethod)");
132             if (VERBOSE) System.out.println("--- " + iae);
133         }
134 
135         try {
136             /* accessible static method in an inaccessible class */
137             InaccessibleClass.test();
138             System.out.println("ERROR: bad meth-class access succeeded (meth-class)");
139         } catch (IllegalAccessError iae) {
140             System.out.println("Got expected IllegalAccessError (meth-class)");
141             if (VERBOSE) System.out.println("--- " + iae);
142         }
143 
144         try {
145             /* accessible static field in an inaccessible class */
146             int blah = InaccessibleClass.blah;
147             System.out.println("ERROR: bad field-class access succeeded (field-class)");
148         } catch (IllegalAccessError iae) {
149             System.out.println("Got expected IllegalAccessError (field-class)");
150             if (VERBOSE) System.out.println("--- " + iae);
151         }
152 
153         try {
154             /* inaccessible static method in an accessible class */
155             InaccessibleMethod.test();
156             System.out.println("ERROR: bad access succeeded (meth-meth)");
157         } catch (IllegalAccessError iae) {
158             System.out.println("Got expected IllegalAccessError (meth-meth)");
159             if (VERBOSE) System.out.println("--- " + iae);
160         }
161     }
162 }
163