Requires gmcs
[mono.git] / mono / tests / load-exceptions.cs
1 //
2 // load-exceptions.cs: Tests for loading missing types/methods/fields from IL
3 //
4
5 using System;
6 using System.IO;
7 using System.Reflection;
8
9 class Miss1 : Missing.Foo1 {
10 }
11
12 public class Tests : LoadMissing {
13
14         public delegate void TestDel ();
15
16         internal static int check_type_load (TestDel d) {
17                 try {
18                         d ();
19                 }
20                 catch (TypeLoadException ex) {
21                         //Console.WriteLine (ex.TypeName);
22                         //Console.WriteLine (ex);
23                         return 0;
24                 }
25
26                 return 1;
27         }
28
29         internal static int check_missing_method (TestDel d) {
30                 try {
31                         d ();
32                 }
33                 catch (MissingMethodException ex) {
34                         //Console.WriteLine (ex);
35                         return 0;
36                 }
37
38                 return 1;
39         }
40
41         internal static int check_file_not_found (TestDel d){
42                 try {
43                         d ();
44                 } catch (FileNotFoundException ex){
45                         return 0;
46                 }
47                 return 1;
48         }
49         
50         internal static int check_missing_field (TestDel d) {
51                 try {
52                         d ();
53                 }
54                 catch (MissingFieldException ex) {
55                         //Console.WriteLine (ex);
56                         return 0;
57                 }
58
59                 return 1;
60         }
61
62
63         //
64         // Base instructions
65         //
66
67         public static int test_0_call () {
68                 return check_missing_method (new TestDel (missing_call));
69         }
70
71         public static int test_0_jmp () {
72                 return check_missing_method (new TestDel (missing_jmp));
73         }
74
75         public static int test_0_ldftn () {
76                 return check_missing_method (new TestDel (missing_ldftn));
77         }
78
79         //
80         // Object model instructions
81         //
82
83         public static int test_0_box () {
84                 // Thrown earlier
85                 return 0;
86         }
87
88         public static int test_0_callvirt () {
89                 return check_missing_method (new TestDel (missing_callvirt));
90         }
91
92         public static int test_0_castclass () {
93                 return check_type_load (new TestDel (missing_castclass));
94         }
95
96         public static int test_0_cpobj () {
97                 return check_type_load (new TestDel (missing_cpobj));
98         }
99
100         public static int test_0_missing_type_on_parameter () {
101                 return check_type_load (new TestDel (missing_external_type_reference_on_parameter));
102         } 
103         public static int test_0_initobj () {
104                 return check_type_load (new TestDel (missing_initobj));
105         }
106
107         public static int test_0_isinst () {
108                 return check_type_load (new TestDel (missing_isinst));
109         }
110
111         public static int test_0_ldelem () {
112                 // Thrown earlier
113                 return 0;
114         }
115
116         public static int test_0_ldelema () {
117                 // Thrown earlier
118                 return 0;
119         }
120
121         public static int test_0_ldfld () {
122                 return check_missing_field (new TestDel (missing_ldfld));
123         }               
124
125         public static int test_0_ldflda () {
126                 return check_missing_field (new TestDel (missing_ldflda));
127         }               
128
129         public static int test_0_ldobj () {
130                 // Thrown earlier
131                 return 0;
132         }
133
134         public static int test_0_ldsfld () {
135                 return check_missing_field (new TestDel (missing_ldsfld));
136         }               
137
138         public static int test_0_ldsflda () {
139                 return check_missing_field (new TestDel (missing_ldsflda));
140         }               
141
142         public static int test_0_ldtoken_type () {
143                 return check_type_load (new TestDel (missing_ldtoken_type));
144         }
145
146         public static int test_0_ldtoken_method () {
147                 return check_missing_method (new TestDel (missing_ldtoken_method));
148         }
149
150         public static int test_0_ldtoken_field () {
151                 return check_missing_field (new TestDel (missing_ldtoken_field));
152         }
153
154         public static int test_0_ldvirtftn () {
155                 return check_missing_method (new TestDel (missing_ldvirtftn));
156         }
157
158         public static int test_0_mkrefany () {
159                 // Thrown earlier
160                 return 0;
161         }
162
163         public static int test_0_newarr () {
164                 return check_type_load (new TestDel (missing_newarr));
165         }               
166
167         public static int test_0_newobj () {
168                 return check_missing_method (new TestDel (missing_newobj));
169         }               
170
171         public static int test_0_refanyval () {
172                 return check_type_load (new TestDel (missing_refanyval));
173         }
174
175         public static int test_0_sizeof () {
176                 return check_type_load (new TestDel (missing_sizeof));
177         }
178
179         public static int test_0_stelem () {
180                 // Thrown earlier
181                 return 0;
182         }
183
184         public static int test_0_stfld () {
185                 return check_missing_field (new TestDel (missing_stfld));
186         }
187
188         public static int test_0_stobj () {
189                 // Thrown earlier
190                 return 0;
191         }
192
193         public static int test_0_stsfld () {
194                 return check_missing_field (new TestDel (missing_stsfld));
195         }
196
197         public static int test_0_unbox () {
198                 return check_type_load (new TestDel (missing_unbox));
199         }
200
201         public static int test_0_unbox_any () {
202                 return check_type_load (new TestDel (missing_unbox_any));
203         }
204
205 #if false
206         // Bummer: we regressed!   I should have put this before
207         public static int test_0_missing_assembly_in_fieldref () {
208                 return check_file_not_found (new TestDel (missing_assembly_in_fieldref));
209         }
210 #endif
211
212         // FIXME: the corrent exception here is FileNotFoundException
213         public static int test_0_missing_assembly_in_call () {
214                 return check_type_load (new TestDel (missing_assembly_in_call));
215         }
216
217         public static int test_0_missing_assembly_in_newobj () {
218                 return check_type_load (new TestDel (missing_assembly_in_newobj));
219         }
220
221         public static int test_0_missing_delegate_ctor_argument () {
222                 return check_type_load (new TestDel (missing_delegate_ctor_argument));
223         }
224         
225         //
226         // Missing classes referenced from metadata
227         //
228
229         public static int test_0_missing_local () {
230                 try {
231                         missing_local ();
232                 }
233                 catch (TypeLoadException ex) {
234                 }
235
236                 /* MS.NET doesn't throw an exception if a local is not found */
237                 return 0;
238         }
239
240         //Regression test for #508532
241         public static int test_0_assembly_throws_on_loader_error ()
242         {
243                 try {
244                         Assembly asm = Assembly.Load ("load-missing");
245                         asm.GetType ("BrokenClass", false);
246                         return 1;
247                 } catch (TypeLoadException) {}
248                 return 0;
249         }
250
251         public static int test_0_bad_method_override1 ()
252         {
253                 try {
254                         BadOverridesDriver.bad_override1 ();
255                         return 1;
256                 } catch (TypeLoadException) {}
257                 return 0;
258         }
259
260         public static int test_0_bad_method_override2 ()
261         {
262                 try {
263                         BadOverridesDriver.bad_override2 ();
264                         return 1;
265                 } catch (TypeLoadException) {}
266                 return 0;
267         }
268
269         public static int test_0_bad_method_override3 ()
270         {
271                 try {
272                         BadOverridesDriver.bad_override3 ();
273                         return 1;
274                 } catch (TypeLoadException) {}
275                 return 0;
276         }
277
278         public static int test_0_bad_method_override4 ()
279         {
280                 try {
281                         BadOverridesDriver.bad_override4 ();
282                         return 1;
283                 } catch (TypeLoadException) {}
284                 return 0;
285         }
286
287         public static void missing_outer () {
288                 new Missing.Foo1.InnerFoo ();
289         }
290
291         //Regression test for #508487
292         public static int test_0_missing_outer_type_in_typeref () {
293                 return check_type_load (new TestDel (missing_outer));
294         }
295
296         // #524498
297         public static int test_0_exception_while_jitting_cctor () {
298                 try {
299                         CCtorClass.foo ();
300                 } catch (TypeInitializationException) {
301                         return 0;
302                 }
303
304                 return 1;
305         }
306
307 #if FALSE
308         public static void missing_parent () {
309                 new Miss1 ();
310         }
311
312         public static int test_0_missing_parent () {
313                 return check_type_load (new TestDel (missing_parent));
314         }
315 #endif
316
317         public static int Main () {
318                 return TestDriver.RunTests (typeof (Tests));
319         }
320 }