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