Merge pull request #5428 from kumpera/wasm-support-p2
[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                         // .NET Framework 4.6.2 throws BIFE here.
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 (FileNotFoundException exn) {
35                         return 0;
36                 } catch (BadImageFormatException exn) {
37                         // .NET Framework 4.6.2 throws BIFE here.
38                         return 0;
39                 }
40                 return 1;
41         }
42
43         public static int test_0_reflection_load_reference ()
44         {
45                 // Check that reflection-only loading a reference assembly is okay
46                 var an = new AssemblyName ("TestingReferenceAssembly");
47                 var a = Assembly.ReflectionOnlyLoad (an.FullName);
48                 var t = a.GetType ("X");
49                 var f = t.GetField ("Y");
50                 if (f.FieldType.Equals (typeof (Int32)))
51                         return 0;
52                 return 1;
53         }
54
55         public static int test_0_load_reference_asm_via_reference ()
56         {
57                 // Check that loading an assembly that references a reference assembly doesn't succeed.
58                 var an = new AssemblyName ("TestingReferenceReferenceAssembly");
59                 try {
60                         var a = Assembly.Load (an);
61                         var t = a.GetType ("Z");
62                 } catch (FileNotFoundException){
63                         return 0;
64                 }
65                 return 1;
66         }
67
68         public static int test_0_reflection_load_reference_asm_via_reference ()
69         {
70                 // Check that reflection-only loading an assembly that
71                 // references a reference assembly is okay.
72                 var an = new AssemblyName ("TestingReferenceReferenceAssembly");
73                 var a = Assembly.ReflectionOnlyLoad (an.FullName);
74                 var t = a.GetType ("Z");
75                 var f = t.GetField ("Y");
76                 if (f.FieldType.Equals (typeof (Int32)))
77                         return 0;
78                 return 1;
79         }
80
81
82         public static int test_0_load_reference_bytes ()
83         {
84                 // Check that loading a reference assembly from a byte array for execution is an error
85                 byte[] bs = File.ReadAllBytes ("./TestingReferenceAssembly.dll");
86                 try {
87                         var a = Assembly.Load (bs);
88                 } catch (BadImageFormatException) {
89                         return 0;
90                 } catch (FileNotFoundException exn) {
91                         Console.Error.WriteLine ("incorrect exn was {0}", exn);
92                         return 2;
93                 }
94                 return 1;
95         }
96
97         public static int test_0_reflection_load_reference_bytes ()
98         {
99                 // Check that loading a reference assembly from a byte
100                 // array for reflection only is okay.
101                 byte[] bs = File.ReadAllBytes ("./TestingReferenceAssembly.dll");
102                 var a = Assembly.ReflectionOnlyLoad (bs);
103                 var t = a.GetType ("X");
104                 var f = t.GetField ("Y");
105                 if (f.FieldType.Equals (typeof (Int32)))
106                         return 0;
107                 return 1;
108         }
109
110 }