Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / safehandle.2.cs
1 using System;
2 using System.Text;
3 using System.Runtime.InteropServices;
4 using System.Runtime.CompilerServices;
5 using Microsoft.Win32.SafeHandles;
6
7 public class Tests {
8
9         public class MyHandle : SafeHandle {
10                 public MyHandle () : base (IntPtr.Zero, true)
11                 {
12                 }
13
14                 public MyHandle (IntPtr x) : base (x, true)
15                 {
16                 }
17                 
18                 
19                 public override bool IsInvalid {
20                         get {
21                                 return false;
22                         }
23                 }
24
25                 protected override bool ReleaseHandle ()
26                 {
27                         return true;
28                 }
29         }
30
31         //
32         // No default public constructor here, this is so we can test
33         // that proper exceptions are thrown
34         //
35         public class MyHandleNoCtor : SafeHandle {
36                 public MyHandleNoCtor (IntPtr handle) : base (handle, true)
37                 {
38                 }
39                 
40                 public override bool IsInvalid {
41                         get {
42                                 return false;
43                         }
44                 }
45
46                 protected override bool ReleaseHandle ()
47                 {
48                         return true;
49                 }
50         }
51         
52         [DllImport ("libtest")]
53         public static extern void mono_safe_handle_ref (ref MyHandle handle);
54
55         [DllImport ("libtest", EntryPoint="mono_safe_handle_ref")]
56         public static extern void mono_safe_handle_ref2 (ref MyHandleNoCtor handle);
57
58         public static int test_0_safehandle_ref_noctor ()
59         {
60                 MyHandleNoCtor m = new MyHandleNoCtor ((IntPtr) 0xdead);
61
62                 try {
63                         mono_safe_handle_ref2 (ref m);
64                 } catch (MissingMethodException e){
65                         Console.WriteLine ("Good: got exception requried");
66                         return 0;
67                 }
68
69                 return 1;
70         }
71         
72         public static int test_0_safehandle_ref ()
73         {
74                 MyHandle m = new MyHandle ((IntPtr) 0xdead);
75
76                 mono_safe_handle_ref (ref m);
77                 
78                 if (m.DangerousGetHandle () != (IntPtr) 0x800d){
79                         Console.WriteLine ("test_0_safehandle_ref: fail; Expected 0x800d, got: {0:x}", m.DangerousGetHandle ());
80                         return 1;
81                 }
82                 Console.WriteLine ("test_0_safehandle_ref: pass");
83                 return 0;
84         }
85
86         [DllImport ("libtest")]
87         public static extern int mono_xr (SafeHandle sh);
88         
89         public static int test_0_marshal_safehandle_argument ()
90         {
91                 SafeHandle s = new SafeFileHandle ((IntPtr) 0xeadcafe, true);
92                 if (mono_xr (s) != (0xeadcafe + 1234))
93                         return 1;
94                 return 0;
95         }
96
97         public static int test_0_marshal_safehandle_argument_null ()
98         {
99                 try {
100                         mono_xr (null);
101                 } catch (ArgumentNullException){
102                         return 0;
103                 }
104                 return 1;
105         }
106         
107
108         [StructLayout (LayoutKind.Sequential)]
109         public struct StringOnStruct {
110                 public string a;
111         }
112
113         [StructLayout (LayoutKind.Sequential)]
114         public struct StructTest {
115                 public int a;
116                 public SafeHandle handle1;
117                 public SafeHandle handle2;
118                 public int b;
119         }
120
121         [StructLayout (LayoutKind.Sequential)]
122         public struct StructTest1 {
123                 public SafeHandle a;
124         }
125         
126         [DllImport ("libtest")]
127         public static extern int mono_safe_handle_struct_ref (ref StructTest test);
128
129         [DllImport ("libtest")]
130         public static extern int mono_safe_handle_struct (StructTest test);
131
132         [DllImport ("libtest")]
133         public static extern int mono_safe_handle_struct_simple (StructTest1 test);
134
135         [DllImport ("libtest", EntryPoint="mono_safe_handle_return")]
136         public static extern SafeHandle mono_safe_handle_return_1 ();
137
138         [DllImport ("libtest", EntryPoint="mono_safe_handle_return")]
139         public static extern MyHandle mono_safe_handle_return ();
140
141         [DllImport ("libtest", EntryPoint="mono_safe_handle_return")]
142         public static extern MyHandleNoCtor mono_safe_handle_return_2 ();
143         
144         static StructTest x = new StructTest ();
145
146         public static int test_0_safehandle_return_noctor ()
147         {
148                 try {
149                         MyHandleNoCtor m = mono_safe_handle_return_2 ();
150                 } catch (MissingMethodException e){
151                         Console.WriteLine ("GOOD: got exception required: " + e);
152
153                         return 0;
154                 }
155                 Console.WriteLine ("Failed, expected an exception because there is no paramterless ctor");
156                 return 1;
157         }
158         
159         public static int test_0_safehandle_return_exc ()
160         {
161                 try {
162                         SafeHandle x = mono_safe_handle_return_1 ();
163                 } catch (MarshalDirectiveException){
164                         Console.WriteLine ("GOOD: got exception required");
165                         return 0;
166                 }
167
168                 Console.WriteLine ("Error: should have generated an exception, since SafeHandle is abstract");
169                 return 1;
170         }
171
172         public static int test_0_safehandle_return ()
173         {
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;
177         }
178         
179         public static int test_0_marshal_safehandle_field ()
180         {
181                 x.a = 1234;
182                 x.b = 8743;
183                 x.handle1 = new SafeFileHandle ((IntPtr) 0x7080feed, false);
184                 x.handle2 = new SafeFileHandle ((IntPtr) 0x1234abcd, false);
185
186                 if (mono_safe_handle_struct (x) != 0xf00f)
187                         return 1;
188
189                 return 0;
190         }
191
192         public static int test_0_marshal_safehandle_field_ref ()
193         {
194                 x.a = 1234;
195                 x.b = 8743;
196                 x.handle1 = new SafeFileHandle ((IntPtr) 0x7080feed, false);
197                 x.handle2 = new SafeFileHandle ((IntPtr) 0x1234abcd, false);
198                 
199                 if (mono_safe_handle_struct_ref (ref x) != 0xf00d)
200                         return 1;
201
202                 return 0;
203         }
204         
205         public static int test_0_simple ()
206         {
207                 StructTest1 s = new StructTest1 ();
208                 s.a = new SafeFileHandle ((IntPtr)1234, false);
209
210                 return mono_safe_handle_struct_simple (s) == 2468 ? 0 : 1;
211         }
212
213         public static int test_0_struct_empty ()
214         {
215                 StructTest1 s = new StructTest1 ();
216
217                 try {
218                         mono_safe_handle_struct_simple (s);
219                 } catch (ArgumentNullException){
220                         return 0;
221                 }
222                 return 1;
223         }
224         
225         public static int test_0_sf_dispose ()
226         {
227                 SafeFileHandle sf = new SafeFileHandle ((IntPtr) 0x0d00d, false);
228                 sf.Dispose ();
229                 try {
230                         mono_xr (sf);
231                 } catch (ObjectDisposedException){
232                         return 0;
233                 }
234                 return 1;
235         }
236
237
238         static int Error (string msg)
239         {
240                 Console.WriteLine ("Error: " + msg);
241                 return 1;
242         }
243         
244         static int Main ()
245         {
246                 return TestDriver.RunTests (typeof (Tests));
247         }
248 }
249