2008-12-08 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mono / tests / cas / threads / tpool2.cs
1 using System;
2 using System.Security;
3 using System.Security.Permissions;
4 using System.Threading;
5
6 class Program {
7
8         static void ShowStackTrace (object o)
9         {
10                 if (debug)
11                         Console.WriteLine ((int)o);
12
13                 try {
14                         Console.WriteLine (Environment.UserName);
15                         result = 0;
16                 }
17                 catch (SecurityException se) {
18                         Console.WriteLine ("UNEXPECTED SecurityException {0}", se);
19                         result = 1;
20                 }
21                 catch (Exception ex) {
22                         Console.WriteLine ("UNEXPECTED {0}", ex);
23                         result = 1;
24                 }
25         }
26
27         static bool debug;
28         static int result = 0;
29
30         // this Deny will *NOT* prevent the Environment.UserName property from working
31         // as the UnsafeQueueUserWorkItem method *DOESN'T* propagate the stack
32         [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
33         static int Main (string[] args)
34         {
35                 debug = (args.Length > 0);
36                 if (debug) {
37                         SecurityManager.SecurityEnabled = (args [0] != "off");
38                 }
39
40                 if (SecurityManager.SecurityEnabled) {
41                         Console.WriteLine ("SecurityManager.SecurityEnabled: true");
42                         ShowStackTrace ((object)-1);
43                 } else {
44                         Console.WriteLine ("SecurityManager.SecurityEnabled: false");
45                 }
46
47                 result = 0;
48                 for (int i=0; i < 5; i++)
49                         ThreadPool.UnsafeQueueUserWorkItem (new WaitCallback (ShowStackTrace), i);
50                 
51                 System.Threading.Thread.Sleep (5000);
52                 return result;
53         }
54 }