[Microsoft.Build] Ignore ThreadAbortException in BuildNodeManager
[mono-project.git] / mono / tests / safehandle.2.cs
blob1731e0490a668a736661c8dc980bf5d48a5218a4
1 using System;
2 using System.Text;
3 using System.Runtime.InteropServices;
4 using System.Runtime.CompilerServices;
5 using Microsoft.Win32.SafeHandles;
7 public class Tests {
9 public class MyHandle : SafeHandle {
10 public MyHandle () : base (IntPtr.Zero, true)
14 public MyHandle (IntPtr x) : base (x, true)
19 public override bool IsInvalid {
20 get {
21 return false;
25 protected override bool ReleaseHandle ()
27 return true;
32 // No default public constructor here, this is so we can test
33 // that proper exceptions are thrown
35 public class MyHandleNoCtor : SafeHandle {
36 public MyHandleNoCtor (IntPtr handle) : base (handle, true)
40 public override bool IsInvalid {
41 get {
42 return false;
46 protected override bool ReleaseHandle ()
48 return true;
52 [DllImport ("libtest")]
53 public static extern void mono_safe_handle_ref (ref MyHandle handle);
55 [DllImport ("libtest", EntryPoint="mono_safe_handle_ref")]
56 public static extern void mono_safe_handle_ref2 (ref MyHandleNoCtor handle);
58 public static int test_0_safehandle_ref_noctor ()
60 MyHandleNoCtor m = new MyHandleNoCtor ((IntPtr) 0xdead);
62 try {
63 mono_safe_handle_ref2 (ref m);
64 } catch (MissingMethodException e){
65 Console.WriteLine ("Good: got exception requried");
66 return 0;
69 return 1;
72 public static int test_0_safehandle_ref ()
74 MyHandle m = new MyHandle ((IntPtr) 0xdead);
76 mono_safe_handle_ref (ref m);
78 if (m.DangerousGetHandle () != (IntPtr) 0x800d){
79 Console.WriteLine ("test_0_safehandle_ref: fail; Expected 0x800d, got: {0:x}", m.DangerousGetHandle ());
80 return 1;
82 Console.WriteLine ("test_0_safehandle_ref: pass");
83 return 0;
86 [DllImport ("libtest")]
87 public static extern int mono_xr (SafeHandle sh);
89 public static int test_0_marshal_safehandle_argument ()
91 SafeHandle s = new SafeFileHandle ((IntPtr) 0xeadcafe, true);
92 if (mono_xr (s) != (0xeadcafe + 1234))
93 return 1;
94 return 0;
97 public static int test_0_marshal_safehandle_argument_null ()
99 try {
100 mono_xr (null);
101 } catch (ArgumentNullException){
102 return 0;
104 return 1;
108 [StructLayout (LayoutKind.Sequential)]
109 public struct StringOnStruct {
110 public string a;
113 [StructLayout (LayoutKind.Sequential)]
114 public struct StructTest {
115 public int a;
116 public SafeHandle handle1;
117 public SafeHandle handle2;
118 public int b;
121 [StructLayout (LayoutKind.Sequential)]
122 public struct StructTest1 {
123 public SafeHandle a;
126 [DllImport ("libtest")]
127 public static extern int mono_safe_handle_struct_ref (ref StructTest test);
129 [DllImport ("libtest")]
130 public static extern int mono_safe_handle_struct (StructTest test);
132 [DllImport ("libtest")]
133 public static extern int mono_safe_handle_struct_simple (StructTest1 test);
135 [DllImport ("libtest", EntryPoint="mono_safe_handle_return")]
136 public static extern SafeHandle mono_safe_handle_return_1 ();
138 [DllImport ("libtest", EntryPoint="mono_safe_handle_return")]
139 public static extern MyHandle mono_safe_handle_return ();
141 [DllImport ("libtest", EntryPoint="mono_safe_handle_return")]
142 public static extern MyHandleNoCtor mono_safe_handle_return_2 ();
144 static StructTest x = new StructTest ();
146 public static int test_0_safehandle_return_noctor ()
148 try {
149 MyHandleNoCtor m = mono_safe_handle_return_2 ();
150 } catch (MissingMethodException e){
151 Console.WriteLine ("GOOD: got exception required: " + e);
153 return 0;
155 Console.WriteLine ("Failed, expected an exception because there is no paramterless ctor");
156 return 1;
159 public static int test_0_safehandle_return_exc ()
161 try {
162 SafeHandle x = mono_safe_handle_return_1 ();
163 } catch (MarshalDirectiveException){
164 Console.WriteLine ("GOOD: got exception required");
165 return 0;
168 Console.WriteLine ("Error: should have generated an exception, since SafeHandle is abstract");
169 return 1;
172 public static int test_0_safehandle_return ()
174 SafeHandle x = mono_safe_handle_return ();
175 Console.WriteLine ("Got the following handle: {0}", x.DangerousGetHandle ());
176 return x.DangerousGetHandle () == (IntPtr) 0x1000f00d ? 0 : 1;
179 public static int test_0_marshal_safehandle_field ()
181 x.a = 1234;
182 x.b = 8743;
183 x.handle1 = new SafeFileHandle ((IntPtr) 0x7080feed, false);
184 x.handle2 = new SafeFileHandle ((IntPtr) 0x1234abcd, false);
186 if (mono_safe_handle_struct (x) != 0xf00f)
187 return 1;
189 return 0;
192 public static int test_0_marshal_safehandle_field_ref ()
194 x.a = 1234;
195 x.b = 8743;
196 x.handle1 = new SafeFileHandle ((IntPtr) 0x7080feed, false);
197 x.handle2 = new SafeFileHandle ((IntPtr) 0x1234abcd, false);
199 if (mono_safe_handle_struct_ref (ref x) != 0xf00d)
200 return 1;
202 return 0;
205 public static int test_0_simple ()
207 StructTest1 s = new StructTest1 ();
208 s.a = new SafeFileHandle ((IntPtr)1234, false);
210 return mono_safe_handle_struct_simple (s) == 2468 ? 0 : 1;
213 public static int test_0_struct_empty ()
215 StructTest1 s = new StructTest1 ();
217 try {
218 mono_safe_handle_struct_simple (s);
219 } catch (ArgumentNullException){
220 return 0;
222 return 1;
225 public static int test_0_sf_dispose ()
227 SafeFileHandle sf = new SafeFileHandle ((IntPtr) 0x0d00d, false);
228 sf.Dispose ();
229 try {
230 mono_xr (sf);
231 } catch (ObjectDisposedException){
232 return 0;
234 return 1;
238 static int Error (string msg)
240 Console.WriteLine ("Error: " + msg);
241 return 1;
244 static int Main ()
246 return TestDriver.RunTests (typeof (Tests));