3 using System.Runtime.InteropServices;
4 using System.Reflection;
6 [SecurityCriticalAttribute]
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]
30 //Console.WriteLine ("sc ctor");
33 public virtual void Method ()
35 //Console.WriteLine ("sc class");
36 CClass cc = new CClass ();
41 public class SCDevClass : SCClass
45 Test.error ("safe-critical-derived class instantiated");
48 public override void 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 ()
79 Test.error ("critical-derived method called");
83 public interface CMethodInterface {
84 [SecurityCriticalAttribute]
88 public class CInterfaceClass : CMethodInterface {
89 public CInterfaceClass () { }
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);
115 static bool haveError = false;
117 public static void error (string text)
119 Console.WriteLine (text);
123 [SecurityCriticalAttribute]
124 static void CMethod ()
126 //Console.WriteLine ("c");
129 [SecuritySafeCriticalAttribute]
130 static void SCMethod ()
132 //Console.WriteLine ("sc");
136 static void doSCDev ()
138 SCDevClass scdev = new SCDevClass ();
142 static void doCMethodDev ()
144 CMethodDevClass cmdev = new CMethodDevClass ();
145 error ("critical-derived object instantiated");
147 Console.WriteLine ("critical-derived method called");
150 static void doSCInterfaceDev ()
152 CMethodInterface mi = new CInterfaceClass ();
153 error ("safe-critical-interface-derived object instantiated");
155 error ("safe-critical-interface-derived method called");
159 static unsafe void unsafeMethod ()
162 error ("unsafe method called");
166 public static void TransparentReflectionCMethod ()
170 [SecurityCriticalAttribute]
171 public static void ReflectionCMethod ()
173 error ("method called via reflection");
176 [SecurityCriticalAttribute]
177 public static unsafe void StringTest ()
179 string str = "blabla";
180 char [] arr = str.ToCharArray ();
183 fixed (char *tarr = arr) {
185 r = new string (tarr, ss, l - ss);
189 [SecuritySafeCriticalAttribute]
190 public static void CallStringTest ()
195 [DllImport ("/lib64/libc.so.6")]
196 static extern int getpid ();
198 public static int Main ()
204 error ("static critical method called");
205 } catch (MethodAccessException) {
208 SCClass sc = new SCClass ();
212 CClass c = new CClass (); // Illegal
213 error ("critical object instantiated");
214 c.Method (); // Illegal
215 error ("critical method called");
216 } catch (MethodAccessException) {
221 error ("security-critical-derived class error");
222 } catch (TypeLoadException) {
227 } catch (TypeLoadException) {
232 error ("pinvoke called");
233 } catch (MethodAccessException) {
237 MethodDelegate md = new MethodDelegate (CClass.StaticMethod);
239 error ("critical method called via delegate");
240 } catch (MethodAccessException) {
244 CriticalClass.NestedClassInsideCritical.Method ();
245 } catch (MethodAccessException) {
250 } catch (TypeLoadException) {
256 } catch (VerificationException) {
261 Type type = Type.GetType ("Test");
262 MethodInfo method = type.GetMethod ("TransparentReflectionCMethod");
264 method.Invoke(null, null);
265 } catch (MethodAccessException) {
266 error ("transparent method not called via reflection");
270 Type type = Type.GetType ("Test");
271 MethodInfo method = type.GetMethod ("ReflectionCMethod");
273 method.Invoke(null, null);
274 } catch (MethodAccessException) {
278 Type type = Type.GetType ("Test");
279 MethodInfo method = type.GetMethod ("TransparentReflectionCMethod");
280 InvokeDelegate id = new InvokeDelegate (method.Invoke);
283 } catch (MethodAccessException) {
284 error ("transparent method not called via reflection delegate");
288 Type type = Type.GetType ("Test");
289 MethodInfo method = type.GetMethod ("ReflectionCMethod");
290 InvokeDelegate id = new InvokeDelegate (method.Invoke);
293 } catch (MethodAccessException) {
300 } catch (MethodAccessException) {
301 error ("string test failed");
304 //Console.WriteLine ("ok");