2007-12-06 Jb Evain <jbevain@novell.com>
[mono.git] / mono / tests / coreclr-security.cs
blobb4f412c487b4bd9eebf2d0de7bb762543086b6b2
1 using System.Security;
2 using System;
3 using System.Runtime.InteropServices;
4 using System.Reflection;
6 [SecurityCriticalAttribute]
7 public class CClass
9 public CClass ()
11 //Console.WriteLine ("c ctor");
14 public virtual void Method ()
16 //Console.WriteLine ("c class");
19 public static void StaticMethod ()
21 //Console.WriteLine ("c class static");
25 [SecuritySafeCriticalAttribute]
26 public class SCClass
28 public SCClass ()
30 //Console.WriteLine ("sc ctor");
33 public virtual void Method ()
35 //Console.WriteLine ("sc class");
36 CClass cc = new CClass ();
37 cc.Method ();
41 public class SCDevClass : SCClass
43 public SCDevClass ()
45 Test.error ("safe-critical-derived class instantiated");
48 public override void Method ()
50 //base.Method ();
51 Test.error ("safe-critical-derived method called");
55 public class CMethodClass
57 public CMethodClass ()
59 //Console.WriteLine ("cmethod ctor");
62 [SecurityCriticalAttribute]
63 public virtual void Method ()
65 //Console.WriteLine ("cmethod");
69 public class CMethodDevClass : CMethodClass
71 public CMethodDevClass ()
73 Test.error ("critical-derived constructor called");
76 public override void Method ()
78 //base.Method();
79 Test.error ("critical-derived method called");
83 public interface CMethodInterface {
84 [SecurityCriticalAttribute]
85 void Method ();
88 public class CInterfaceClass : CMethodInterface {
89 public CInterfaceClass () { }
91 public void Method ()
93 Test.error ("security-critical-interface-derived method called");
97 [SecurityCriticalAttribute]
98 public class CriticalClass {
100 public class NestedClassInsideCritical {
102 static public void Method ()
104 Test.error ("critical inner class method called");
109 public delegate void MethodDelegate ();
111 public delegate Object InvokeDelegate (Object obj, Object[] parms);
113 public class Test
115 static bool haveError = false;
117 public static void error (string text)
119 Console.WriteLine (text);
120 haveError = true;
123 [SecurityCriticalAttribute]
124 static void CMethod ()
126 //Console.WriteLine ("c");
129 [SecuritySafeCriticalAttribute]
130 static void SCMethod ()
132 //Console.WriteLine ("sc");
133 CMethod ();
136 static void doSCDev ()
138 SCDevClass scdev = new SCDevClass ();
139 scdev.Method ();
142 static void doCMethodDev ()
144 CMethodDevClass cmdev = new CMethodDevClass ();
145 error ("critical-derived object instantiated");
146 cmdev.Method ();
147 Console.WriteLine ("critical-derived method called");
150 static void doSCInterfaceDev ()
152 CMethodInterface mi = new CInterfaceClass ();
153 error ("safe-critical-interface-derived object instantiated");
154 mi.Method ();
155 error ("safe-critical-interface-derived method called");
159 static unsafe void unsafeMethod ()
161 byte *p = null;
162 error ("unsafe method called");
166 public static void TransparentReflectionCMethod ()
170 [SecurityCriticalAttribute]
171 public static void ReflectionCMethod ()
173 error ("method called via reflection");
176 [DllImport ("/lib64/libc.so.6")]
177 static extern int getpid ();
179 public static int Main ()
181 SCMethod ();
183 try {
184 CMethod ();
185 error ("static critical method called");
186 } catch (MethodAccessException) {
189 SCClass sc = new SCClass ();
190 sc.Method ();
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) {
200 try {
201 doSCDev ();
202 error ("security-critical-derived class error");
203 } catch (TypeLoadException) {
206 try {
207 doCMethodDev ();
208 } catch (TypeLoadException) {
211 try {
212 getpid ();
213 error ("pinvoke called");
214 } catch (MethodAccessException) {
217 try {
218 MethodDelegate md = new MethodDelegate (CClass.StaticMethod);
219 md ();
220 error ("critical method called via delegate");
221 } catch (MethodAccessException) {
224 try {
225 CriticalClass.NestedClassInsideCritical.Method ();
226 } catch (MethodAccessException) {
229 try {
230 doSCInterfaceDev ();
231 } catch (TypeLoadException) {
235 try {
236 unsafeMethod ();
237 } catch (VerificationException) {
241 try {
242 Type type = Type.GetType ("Test");
243 MethodInfo method = type.GetMethod ("TransparentReflectionCMethod");
245 method.Invoke(null, null);
246 } catch (MethodAccessException) {
247 error ("transparent method not called via reflection");
250 try {
251 Type type = Type.GetType ("Test");
252 MethodInfo method = type.GetMethod ("ReflectionCMethod");
254 method.Invoke(null, null);
255 } catch (MethodAccessException) {
258 try {
259 Type type = Type.GetType ("Test");
260 MethodInfo method = type.GetMethod ("TransparentReflectionCMethod");
261 InvokeDelegate id = new InvokeDelegate (method.Invoke);
263 id (null, null);
264 } catch (MethodAccessException) {
265 error ("transparent method not called via reflection delegate");
268 try {
269 Type type = Type.GetType ("Test");
270 MethodInfo method = type.GetMethod ("ReflectionCMethod");
271 InvokeDelegate id = new InvokeDelegate (method.Invoke);
273 id (null, null);
274 } catch (MethodAccessException) {
277 //Console.WriteLine ("ok");
279 if (haveError)
280 return 1;
282 return 0;