2009-02-04 Zoltan Varga <vargaz@gmail.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 delegate void MethodDelegate ();
110
111 public delegate Object InvokeDelegate (Object obj, Object[] parms);
112
113 public class Test
114 {
115         static bool haveError = false;
116
117         public static void error (string text)
118         {
119                 Console.WriteLine (text);
120                 haveError = true;
121         }
122
123         [SecurityCriticalAttribute]
124         static void CMethod ()
125         {
126                 //Console.WriteLine ("c");
127         }
128
129         [SecuritySafeCriticalAttribute]
130         static void SCMethod ()
131         {
132                 //Console.WriteLine ("sc");
133                 CMethod ();
134         }
135
136         static void doSCDev ()
137         {
138                 SCDevClass scdev = new SCDevClass ();
139                 scdev.Method ();
140         }
141
142         static void doCMethodDev ()
143         {
144                 CMethodDevClass cmdev = new CMethodDevClass ();
145                 error ("critical-derived object instantiated");
146                 cmdev.Method ();
147                 Console.WriteLine ("critical-derived method called");
148         }
149
150         static void doSCInterfaceDev ()
151         {
152                 CMethodInterface mi = new CInterfaceClass ();
153                 error ("safe-critical-interface-derived object instantiated");
154                 mi.Method ();
155                 error ("safe-critical-interface-derived method called");
156         }
157
158         /*
159         static unsafe void unsafeMethod ()
160         {
161                 byte *p = null;
162                 error ("unsafe method called");
163         }
164         */
165
166         public static void TransparentReflectionCMethod ()
167         {
168         }
169
170         [SecurityCriticalAttribute]
171         public static void ReflectionCMethod ()
172         {
173                 error ("method called via reflection");
174         }
175
176         [DllImport ("/lib64/libc.so.6")]
177         static extern int getpid ();
178
179         public static int Main ()
180         {
181                 SCMethod ();
182
183                 try {
184                         CMethod ();
185                         error ("static critical method called");
186                 } catch (MethodAccessException) {
187                 }
188
189                 SCClass sc = new SCClass ();
190                 sc.Method ();
191
192                 try {
193                         CClass c = new CClass (); // Illegal
194                         error ("critical object instantiated");
195                         c.Method ();    // Illegal
196                         error ("critical method called");
197                 } catch (MethodAccessException) {
198                 }
199
200                 try {
201                         doSCDev ();
202                         error ("security-critical-derived class error");
203                 } catch (TypeLoadException) {
204                 }
205
206                 try {
207                         doCMethodDev ();
208                 } catch (TypeLoadException) {
209                 }
210
211                 try {
212                         getpid ();
213                         error ("pinvoke called");
214                 } catch (MethodAccessException) {
215                 }
216
217                 try {
218                         MethodDelegate md = new MethodDelegate (CClass.StaticMethod);
219                         md ();
220                         error ("critical method called via delegate");
221                 } catch (MethodAccessException) {
222                 }
223
224                 try {
225                         CriticalClass.NestedClassInsideCritical.Method ();
226                 } catch (MethodAccessException) {
227                 }
228
229                 try {
230                         doSCInterfaceDev ();
231                 } catch (TypeLoadException) {
232                 }
233
234                 /*
235                 try {
236                         unsafeMethod ();
237                 } catch (VerificationException) {
238                 }
239                 */
240
241                 try {
242                         Type type = Type.GetType ("Test");
243                         MethodInfo method = type.GetMethod ("TransparentReflectionCMethod");
244
245                         method.Invoke(null, null);
246                 } catch (MethodAccessException) {
247                         error ("transparent method not called via reflection");
248                 }
249
250                 try {
251                         Type type = Type.GetType ("Test");
252                         MethodInfo method = type.GetMethod ("ReflectionCMethod");
253
254                         method.Invoke(null, null);
255                 } catch (MethodAccessException) {
256                 }
257
258                 try {
259                         Type type = Type.GetType ("Test");
260                         MethodInfo method = type.GetMethod ("TransparentReflectionCMethod");
261                         InvokeDelegate id = new InvokeDelegate (method.Invoke);
262
263                         id (null, null);
264                 } catch (MethodAccessException) {
265                         error ("transparent method not called via reflection delegate");
266                 }
267
268                 try {
269                         Type type = Type.GetType ("Test");
270                         MethodInfo method = type.GetMethod ("ReflectionCMethod");
271                         InvokeDelegate id = new InvokeDelegate (method.Invoke);
272
273                         id (null, null);
274                 } catch (MethodAccessException) {
275                 }
276
277                 //Console.WriteLine ("ok");
278
279                 if (haveError)
280                         return 1;
281
282                 return 0;
283         }
284 }