2005-04-12 Dick Porter <dick@ximian.com>
[mono.git] / mono / tests / cas / linkdemand / refcas5.cs
1 using System;
2 using System.Reflection;
3 using System.Security;
4 using System.Security.Permissions;
5
6 public class Program {
7
8         [SecurityPermission (SecurityAction.LinkDemand, ControlPrincipal=true)]
9         public static int Test ()
10         {
11                 return 1;
12         }
13
14         static MethodInfo GetReflectedCall ()
15         {
16                 return typeof (Program).GetMethod ("Test", BindingFlags.Static | BindingFlags.Public);
17         }
18
19         static int LinkReflectedCall (MethodInfo mi)
20         {
21                 // if a LinkDemand is enforced this will succeed;
22                 // if a full Demand is enforced (stack walk) then it will fail
23                 return (int) mi.Invoke (null, null);
24         }
25
26         [SecurityPermission (SecurityAction.Deny, ControlPrincipal=true)]
27         static int CallReflectedCall (MethodInfo mi)
28         {
29                 return LinkReflectedCall (mi);
30         }
31
32         static int Main ()
33         {
34                 try {
35                         MethodInfo mi = GetReflectedCall ();
36                         if (mi == null) {
37                                 Console.WriteLine ("*2* Couldn't reflect on call Test (failure).");
38                                 return 2;
39                         } else {
40                                 int result = CallReflectedCall (mi);
41                                 if (result == 1)
42                                         Console.WriteLine ("*{0}* Unexpected calling thru reflection.", result);
43                                 else
44                                         Console.WriteLine ("*{0}* Unexpected return value from reflection.", result);
45
46                                 return result;
47                         }
48                 }
49                 catch (SecurityException) {
50                         Console.WriteLine ("*0* Expected SecurityException.");
51                         return 0;
52                 }
53         }
54 }