New test.
[mono.git] / mono / tests / cas / linkdemand / noncas2.cs
1 using System;
2 using System.Security;
3 using System.Security.Permissions;
4 using System.Security.Principal;
5 using System.Threading;
6
7 // Note: this should also fail if you replace "me" with your username
8
9 [PrincipalPermission (SecurityAction.LinkDemand, Name="me")]
10 public class LinkDemand {
11
12         static public int Test () 
13         {
14                 Console.WriteLine ("*1* [this should not print]");
15                 return 1;
16         }
17 }
18
19 class LinkDemandTest {
20
21         static int Test ()
22         {
23                 Console.WriteLine ("[this should not print - as JIT will reject the LinkDemand]");
24
25                 GenericIdentity identity = new GenericIdentity ("me");
26                 string[] roles = new string [1] { "mono hacker" };
27                 Thread.CurrentPrincipal = new GenericPrincipal (identity, roles);
28
29                 // Note: if the next line is commented then no exception will 
30                 // be thrown as the JIT will never reach the LinkDemand class
31                 return LinkDemand.Test ();
32         }
33
34         [STAThread]
35         static int Main (string[] args)
36         {
37                 try {
38                         return Test ();
39                 }
40                 catch (SecurityException se) {
41                         Console.WriteLine ("*0* Expected SecurityException\n{0}", se);
42                         return 0;
43                 }
44                 catch (Exception e) {
45                         Console.WriteLine ("*2* Unexpected Exception\n{0}", e);
46                         return 2;
47                 }
48         }
49 }