2009-04-17 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mono / tests / coreclr-security.cs
1 using System.Security;
2 using System;
3 using System.Runtime.InteropServices;
4 using System.Reflection;
5
6 [SecurityCriticalAttribute]
7 public class CClass
8 {
9         public CClass ()
10         {
11                 //Console.WriteLine ("c ctor");
12         }
13
14         public virtual void Method ()
15         {
16                 //Console.WriteLine ("c class");
17         }
18
19         public static void StaticMethod ()
20         {
21                 //Console.WriteLine ("c class static");
22         }
23 }
24
25 [SecuritySafeCriticalAttribute]
26 public class SCClass
27 {
28         public SCClass ()
29         {
30                 //Console.WriteLine ("sc ctor");
31         }
32
33         public virtual void Method ()
34         {
35                 //Console.WriteLine ("sc class");
36                 CClass cc = new CClass ();
37                 cc.Method ();
38         }
39 }
40
41 public class SCDevClass : SCClass
42 {
43         public SCDevClass ()
44         {
45                 Test.error ("safe-critical-derived class instantiated");
46         }
47
48         public override void Method ()
49         {
50                 //base.Method ();
51                 Test.error ("safe-critical-derived method called");
52         }
53 }
54
55 public class CMethodClass
56 {
57         public CMethodClass ()
58         {
59                 //Console.WriteLine ("cmethod ctor");
60         }
61
62         [SecurityCriticalAttribute]
63         public virtual void Method ()
64         {
65                 //Console.WriteLine ("cmethod");
66         }
67 }
68
69 public class CMethodDevClass : CMethodClass
70 {
71         public CMethodDevClass ()
72         {
73                 Test.error ("critical-derived constructor called");
74         }
75
76         public override void Method ()
77         {
78                 //base.Method();
79                 Test.error ("critical-derived method called");
80         }
81 }
82
83 public interface CMethodInterface {
84         [SecurityCriticalAttribute]
85         void Method ();
86 }
87
88 public class CInterfaceClass : CMethodInterface {
89         public CInterfaceClass () { }
90
91         public void Method ()
92         {
93                 Test.error ("security-critical-interface-derived method called");
94         }
95 }
96
97 [SecurityCriticalAttribute]
98 public class CriticalClass {
99
100         public class NestedClassInsideCritical {
101
102                 static public void Method ()
103                 {
104                         Test.error ("critical inner class method called");
105                 }
106         }
107 }
108
109 public class TransparentBaseClass {
110         public virtual void TransparentMethod ()
111         {
112         }
113
114         [SecuritySafeCritical]
115         public virtual void SafeCriticalMethod ()
116         {
117         }
118
119         [SecurityCritical]
120         public virtual void CriticalMethod ()
121         {
122         }
123 }
124
125 public class BadTransparentOverrideClass : TransparentBaseClass {
126         [SecurityCritical]
127         public override void TransparentMethod ()
128         {
129                 Test.error ("this method is critical and cannot override its base (transparent)");
130         }
131 }
132
133 public class BadSafeCriticalOverrideClass : TransparentBaseClass {
134         [SecurityCritical]
135         public override void SafeCriticalMethod ()
136         {
137                 Test.error ("this method is critical and cannot override its base (safe critical)");
138         }
139 }
140
141 public class BadCriticalOverrideClass : TransparentBaseClass {
142         public override void CriticalMethod ()
143         {
144                 Test.error ("this method is NOT critical and cannot override its base (critical)");
145         }
146 }
147
148 public delegate void MethodDelegate ();
149
150 public delegate Object InvokeDelegate (Object obj, Object[] parms);
151
152 // the 0.1% case from http://blogs.msdn.com/shawnfa/archive/2007/05/11/silverlight-security-iii-inheritance.aspx
153 public class TransparentClassWithSafeCriticalDefaultConstructor {
154
155         [SecuritySafeCritical]
156         public TransparentClassWithSafeCriticalDefaultConstructor ()
157         {
158         }
159 }
160
161 public class TransparentInheritFromSafeCriticalDefaultConstructor : TransparentClassWithSafeCriticalDefaultConstructor {
162
163         public TransparentInheritFromSafeCriticalDefaultConstructor ()
164         {
165         }
166 }
167
168 public class SafeInheritFromSafeCriticalDefaultConstructor : TransparentClassWithSafeCriticalDefaultConstructor {
169
170         [SecuritySafeCritical]
171         public SafeInheritFromSafeCriticalDefaultConstructor ()
172         {
173         }
174 }
175
176 public class Test
177 {
178         static bool haveError = false;
179
180         public static void error (string text)
181         {
182                 Console.WriteLine (text);
183                 haveError = true;
184         }
185
186         [SecurityCriticalAttribute]
187         static void CMethod ()
188         {
189                 //Console.WriteLine ("c");
190         }
191
192         [SecuritySafeCriticalAttribute]
193         static void SCMethod ()
194         {
195                 //Console.WriteLine ("sc");
196                 CMethod ();
197         }
198
199         static void doSCDev ()
200         {
201                 SCDevClass scdev = new SCDevClass ();
202                 scdev.Method ();
203         }
204
205         static void doCMethodDev ()
206         {
207                 CMethodDevClass cmdev = new CMethodDevClass ();
208                 error ("critical-derived object instantiated");
209                 cmdev.Method ();
210                 Console.WriteLine ("critical-derived method called");
211         }
212
213         static void doSCInterfaceDev ()
214         {
215                 CMethodInterface mi = new CInterfaceClass ();
216                 error ("safe-critical-interface-derived object instantiated");
217                 mi.Method ();
218                 error ("safe-critical-interface-derived method called");
219         }
220
221         /*
222         static unsafe void unsafeMethod ()
223         {
224                 byte *p = null;
225                 error ("unsafe method called");
226         }
227         */
228
229         static void doBadTransparentOverrideClass ()
230         {
231                 new BadTransparentOverrideClass ();
232         }
233
234         static void doBadSafeCriticalOverrideClass ()
235         {
236                 new BadSafeCriticalOverrideClass ();
237         }
238
239         static void doBadCriticalOverrideClass ()
240         {
241                 new BadCriticalOverrideClass ();
242         }
243
244         public static void TransparentReflectionCMethod ()
245         {
246         }
247
248         [SecurityCriticalAttribute]
249         public static void ReflectionCMethod ()
250         {
251                 error ("method called via reflection");
252         }
253
254         [SecurityCriticalAttribute]
255         public static unsafe void StringTest ()
256         {
257                 string str = "blabla";
258                 char [] arr = str.ToCharArray ();
259                 string r;
260
261                 fixed (char *tarr = arr) {
262                         int ss = 1, l = 3;
263                         r = new string (tarr, ss, l - ss);
264                 }
265         }
266
267         [SecuritySafeCriticalAttribute]
268         public static void CallStringTest ()
269         {
270                 StringTest ();
271         }
272
273         [DllImport ("/lib64/libc.so.6")]
274         static extern int getpid ();
275
276         public static int Main ()
277         {
278                 SCMethod ();
279
280                 try {
281                         CMethod ();
282                         error ("static critical method called");
283                 } catch (MethodAccessException) {
284                 }
285
286                 SCClass sc = new SCClass ();
287                 sc.Method ();
288
289                 try {
290                         CClass c = new CClass (); // Illegal
291                         error ("critical object instantiated");
292                         c.Method ();    // Illegal
293                         error ("critical method called");
294                 } catch (MethodAccessException) {
295                 }
296
297                 try {
298                         doSCDev ();
299                         error ("security-critical-derived class error");
300                 } catch (TypeLoadException) {
301                 }
302
303                 try {
304                         doCMethodDev ();
305                 } catch (TypeLoadException) {
306                 }
307
308                 try {
309                         getpid ();
310                         error ("pinvoke called");
311                 } catch (MethodAccessException) {
312                 }
313
314                 try {
315                         MethodDelegate md = new MethodDelegate (CClass.StaticMethod);
316                         md ();
317                         error ("critical method called via delegate");
318                 } catch (MethodAccessException) {
319                 }
320
321                 try {
322                         CriticalClass.NestedClassInsideCritical.Method ();
323                 } catch (MethodAccessException) {
324                 }
325
326                 try {
327                         doSCInterfaceDev ();
328                 } catch (TypeLoadException) {
329                 }
330
331                 /*
332                 try {
333                         unsafeMethod ();
334                 } catch (VerificationException) {
335                 }
336                 */
337
338                 try {
339                         Type type = Type.GetType ("Test");
340                         MethodInfo method = type.GetMethod ("TransparentReflectionCMethod");
341
342                         method.Invoke(null, null);
343                 } catch (MethodAccessException) {
344                         error ("transparent method not called via reflection");
345                 }
346
347                 try {
348                         Type type = Type.GetType ("Test");
349                         MethodInfo method = type.GetMethod ("ReflectionCMethod");
350
351                         method.Invoke(null, null);
352                 } catch (MethodAccessException) {
353                 }
354
355                 try {
356                         Type type = Type.GetType ("Test");
357                         MethodInfo method = type.GetMethod ("TransparentReflectionCMethod");
358                         InvokeDelegate id = new InvokeDelegate (method.Invoke);
359
360                         id (null, null);
361                 } catch (MethodAccessException) {
362                         error ("transparent method not called via reflection delegate");
363                 }
364
365                 try {
366                         Type type = Type.GetType ("Test");
367                         MethodInfo method = type.GetMethod ("ReflectionCMethod");
368                         InvokeDelegate id = new InvokeDelegate (method.Invoke);
369
370                         id (null, null);
371                 } catch (MethodAccessException) {
372                 }
373
374
375                 // wrapper 7
376                 try {
377                         CallStringTest ();
378                 } catch (MethodAccessException) {
379                         error ("string test failed");
380                 }
381
382                 try {
383                         doBadTransparentOverrideClass ();
384                         error ("BadTransparentOverrideClass error");
385                 } catch (TypeLoadException) {
386                 }
387
388                 try {
389                         doBadSafeCriticalOverrideClass ();
390                         error ("BadSafeCriticalOverrideClass error");
391                 } catch (TypeLoadException) {
392                 }
393
394                 try {
395                         doBadCriticalOverrideClass ();
396                         error ("BadCriticalOverrideClass error");
397                 } catch (TypeLoadException) {
398                 }
399
400                 new TransparentClassWithSafeCriticalDefaultConstructor ();
401                 try {
402                         new TransparentInheritFromSafeCriticalDefaultConstructor ();
403                 } catch (TypeLoadException) {
404                 }
405                 new SafeInheritFromSafeCriticalDefaultConstructor ();
406
407                 if (haveError)
408                         return 1;
409
410 //              Console.WriteLine ("ok");
411                 return 0;
412         }
413 }