Test for CoreCLR security wrapper bug.
[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         [SecurityCriticalAttribute]
177         public static unsafe void StringTest ()
178         {
179                 string str = "blabla";
180                 char [] arr = str.ToCharArray ();
181                 string r;
182
183                 fixed (char *tarr = arr) {
184                         int ss = 1, l = 3;
185                         r = new string (tarr, ss, l - ss);
186                 }
187         }
188
189         [SecuritySafeCriticalAttribute]
190         public static void CallStringTest ()
191         {
192                 StringTest ();
193         }
194
195         [DllImport ("/lib64/libc.so.6")]
196         static extern int getpid ();
197
198         public static int Main ()
199         {
200                 SCMethod ();
201
202                 try {
203                         CMethod ();
204                         error ("static critical method called");
205                 } catch (MethodAccessException) {
206                 }
207
208                 SCClass sc = new SCClass ();
209                 sc.Method ();
210
211                 try {
212                         CClass c = new CClass (); // Illegal
213                         error ("critical object instantiated");
214                         c.Method ();    // Illegal
215                         error ("critical method called");
216                 } catch (MethodAccessException) {
217                 }
218
219                 try {
220                         doSCDev ();
221                         error ("security-critical-derived class error");
222                 } catch (TypeLoadException) {
223                 }
224
225                 try {
226                         doCMethodDev ();
227                 } catch (TypeLoadException) {
228                 }
229
230                 try {
231                         getpid ();
232                         error ("pinvoke called");
233                 } catch (MethodAccessException) {
234                 }
235
236                 try {
237                         MethodDelegate md = new MethodDelegate (CClass.StaticMethod);
238                         md ();
239                         error ("critical method called via delegate");
240                 } catch (MethodAccessException) {
241                 }
242
243                 try {
244                         CriticalClass.NestedClassInsideCritical.Method ();
245                 } catch (MethodAccessException) {
246                 }
247
248                 try {
249                         doSCInterfaceDev ();
250                 } catch (TypeLoadException) {
251                 }
252
253                 /*
254                 try {
255                         unsafeMethod ();
256                 } catch (VerificationException) {
257                 }
258                 */
259
260                 try {
261                         Type type = Type.GetType ("Test");
262                         MethodInfo method = type.GetMethod ("TransparentReflectionCMethod");
263
264                         method.Invoke(null, null);
265                 } catch (MethodAccessException) {
266                         error ("transparent method not called via reflection");
267                 }
268
269                 try {
270                         Type type = Type.GetType ("Test");
271                         MethodInfo method = type.GetMethod ("ReflectionCMethod");
272
273                         method.Invoke(null, null);
274                 } catch (MethodAccessException) {
275                 }
276
277                 try {
278                         Type type = Type.GetType ("Test");
279                         MethodInfo method = type.GetMethod ("TransparentReflectionCMethod");
280                         InvokeDelegate id = new InvokeDelegate (method.Invoke);
281
282                         id (null, null);
283                 } catch (MethodAccessException) {
284                         error ("transparent method not called via reflection delegate");
285                 }
286
287                 try {
288                         Type type = Type.GetType ("Test");
289                         MethodInfo method = type.GetMethod ("ReflectionCMethod");
290                         InvokeDelegate id = new InvokeDelegate (method.Invoke);
291
292                         id (null, null);
293                 } catch (MethodAccessException) {
294                 }
295
296
297                 // wrapper 7
298                 try {
299                         CallStringTest ();
300                 } catch (MethodAccessException) {
301                         error ("string test failed");
302                 }
303
304                 //Console.WriteLine ("ok");
305
306                 if (haveError)
307                         return 1;
308
309                 return 0;
310         }
311 }