update readme (#21797)
[mono-project.git] / mono / tests / reference-loader.cs
blob1aeb218ed23cb78d17068cf41b5a71242dc6b9b9
1 //
2 // reference-loader.cs:
3 //
4 // Test for reference assembly loading
6 using System;
7 using System.IO;
8 using System.Reflection;
10 public class Tests {
11 public static int Main (string[] args)
13 return TestDriver.RunTests (typeof (Tests), args);
16 public static int test_0_loadFrom_reference ()
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;
25 return 1;
28 public static int test_0_load_reference ()
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;
40 return 1;
43 public static int test_0_reflection_load_reference ()
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;
55 public static int test_0_load_reference_asm_via_reference_parent ()
57 // Check that loading an assembly that references a reference
58 // assembly and then using a type derived from a reference
59 // assembly type doesn't succeed.
60 var an = new AssemblyName ("TestingReferenceReferenceAssembly");
61 try {
62 var a = Assembly.Load (an);
63 var t = a.GetType ("Z");
64 } catch (FileNotFoundException){
65 return 0;
67 return 1;
70 public static int test_0_load_reference_asm_via_reference_field ()
72 // Check that loading an assembly that references a reference
73 // assembly and then using a type that has a field from the
74 // reference assembly is okay as long as we don't instantiate
75 // the type.
76 var an = new AssemblyName ("TestingReferenceReferenceAssembly");
77 var a = Assembly.Load (an);
78 var t = a.GetType ("HasFieldFromReferenceAssembly");
79 t.IsSubclassOf (typeof (int));
80 return 0;
84 public static int test_0_reflection_load_reference_asm_via_reference ()
86 // Check that reflection-only loading an assembly that
87 // references a reference assembly is okay.
88 var an = new AssemblyName ("TestingReferenceReferenceAssembly");
89 var a = Assembly.ReflectionOnlyLoad (an.FullName);
90 var t = a.GetType ("Z");
91 var f = t.GetField ("Y");
92 if (f.FieldType.Equals (typeof (Int32)))
93 return 0;
94 return 1;
98 public static int test_0_load_reference_bytes ()
100 // Check that loading a reference assembly from a byte array for execution is an error
101 byte[] bs = File.ReadAllBytes ("./TestingReferenceAssembly.dll");
102 try {
103 var a = Assembly.Load (bs);
104 } catch (BadImageFormatException) {
105 return 0;
106 } catch (FileNotFoundException exn) {
107 Console.Error.WriteLine ("incorrect exn was {0}", exn);
108 return 2;
110 return 1;
113 public static int test_0_reflection_load_reference_bytes ()
115 // Check that loading a reference assembly from a byte
116 // array for reflection only is okay.
117 byte[] bs = File.ReadAllBytes ("./TestingReferenceAssembly.dll");
118 var a = Assembly.ReflectionOnlyLoad (bs);
119 var t = a.GetType ("X");
120 var f = t.GetField ("Y");
121 if (f.FieldType.Equals (typeof (Int32)))
122 return 0;
123 return 1;