Merge pull request #3528 from BrzVlad/fix-sgen-check-before-collections
[mono.git] / mono / tests / reference-loader.cs
1 //
2 // reference-loader.cs:
3 //
4 //  Test for reference assembly loading
5
6 using System;
7 using System.IO;
8 using System.Reflection;
9
10 public class Tests {
11         public static int Main (string[] args)
12         {
13                 return TestDriver.RunTests (typeof (Tests), args);
14         }
15
16         public static int test_0_loadFrom_reference ()
17         {
18                 // Check that loading a reference assembly by filename for execution is an error
19                 try {
20                         var a = Assembly.LoadFrom ("./TestingReferenceAssembly.dll");
21                 } catch (BadImageFormatException exn) {
22                         // Console.Error.WriteLine ("exn was {0}", exn);
23                         return 0;
24                 }
25                 return 1;
26         }
27
28         public static int test_0_load_reference ()
29         {
30                 // Check that loading a reference assembly for execution is an error
31                 try {
32                         var an = new AssemblyName ("TestingReferenceAssembly");
33                         var a = Assembly.Load (an);
34                 } catch (BadImageFormatException exn) {
35                         //Console.Error.WriteLine ("exn was {0}", exn);
36                         return 0;
37                 } catch (FileNotFoundException exn) {
38                         Console.Error.WriteLine ("incorrect exn was {0}", exn);
39                         return 2;
40                 }
41                 return 1;
42         }
43
44         public static int test_0_reflection_load_reference ()
45         {
46                 // Check that reflection-only loading a reference assembly is okay
47                 var an = new AssemblyName ("TestingReferenceAssembly");
48                 var a = Assembly.ReflectionOnlyLoad (an.FullName);
49                 var t = a.GetType ("X");
50                 var f = t.GetField ("Y");
51                 if (f.FieldType.Equals (typeof (Int32)))
52                         return 0;
53                 return 1;
54         }
55
56         public static int test_0_load_reference_asm_via_reference ()
57         {
58                 // Check that loading an assembly that references a reference assembly doesn't succeed.
59                 var an = new AssemblyName ("TestingReferenceReferenceAssembly");
60                 try {
61                         var a = Assembly.Load (an);
62                         var t = a.GetType ("Z");
63                 } catch (FileNotFoundException){
64                         return 0;
65                 }
66                 return 1;
67         }
68
69         public static int test_0_reflection_load_reference_asm_via_reference ()
70         {
71                 // Check that reflection-only loading an assembly that
72                 // references a reference assembly is okay.
73                 var an = new AssemblyName ("TestingReferenceReferenceAssembly");
74                 var a = Assembly.ReflectionOnlyLoad (an.FullName);
75                 var t = a.GetType ("Z");
76                 var f = t.GetField ("Y");
77                 if (f.FieldType.Equals (typeof (Int32)))
78                         return 0;
79                 return 1;
80         }
81
82
83         public static int test_0_load_reference_bytes ()
84         {
85                 // Check that loading a reference assembly from a byte array for execution is an error
86                 byte[] bs = File.ReadAllBytes ("./TestingReferenceAssembly.dll");
87                 try {
88                         var a = Assembly.Load (bs);
89                 } catch (BadImageFormatException) {
90                         return 0;
91                 } catch (FileNotFoundException exn) {
92                         Console.Error.WriteLine ("incorrect exn was {0}", exn);
93                         return 2;
94                 }
95                 return 1;
96         }
97
98         public static int test_0_reflection_load_reference_bytes ()
99         {
100                 // Check that loading a reference assembly from a byte
101                 // array for reflection only is okay.
102                 byte[] bs = File.ReadAllBytes ("./TestingReferenceAssembly.dll");
103                 var a = Assembly.ReflectionOnlyLoad (bs);
104                 var t = a.GetType ("X");
105                 var f = t.GetField ("Y");
106                 if (f.FieldType.Equals (typeof (Int32)))
107                         return 0;
108                 return 1;
109         }
110
111 }