1 // EventWaitHandleSecurityTest.cs - NUnit Test Cases for EventWaitHandleSecurity
2 //
3 // Authors:
4 //	James Bellinger  <jfb@zer7.com>
5 //
6 // Copyright (C) 2012 James Bellinger
7 
8 using System;
9 using System.Collections.Generic;
10 using System.Security.AccessControl;
11 using System.Security.Principal;
12 using System.Threading;
13 using NUnit.Framework;
14 
15 namespace MonoTests.System.Security.AccessControl
16 {
17 	[TestFixture]
18 	public class EventWaitHandleSecurityTest
19 	{
20 		// TODO: Mono System.Threading.EventWaitHandle does not throw exceptions on failure!
21 		[Test, ExpectedExceptionAttribute (typeof (UnauthorizedAccessException))]
PermissionsActuallyWork()22 		public void PermissionsActuallyWork ()
23 		{
24 			if (PlatformID.Win32NT != Environment.OSVersion.Platform) {
25 				Assert.Ignore ();
26 			}
27 
28 			bool createdNew; EventWaitHandleSecurity security;
29 			string name = @"Local\MonoTestWaitHandle";
30 
31 			using (EventWaitHandle handle = new EventWaitHandle (false, EventResetMode.ManualReset,
32 			                                                     name, out createdNew)) {
33 				Assert.IsFalse (handle.SafeWaitHandle.IsInvalid);
34 				Assert.IsTrue (createdNew);
35 
36 				// Make sure our later error will be due to permissions and not some sharing bug.
37 				bool createdAnotherNew;
38 				using (EventWaitHandle anotherHandle = new EventWaitHandle (false, EventResetMode.ManualReset,
39 			                                                            	    name, out createdAnotherNew)) {
40 					Assert.IsFalse (anotherHandle.SafeWaitHandle.IsInvalid);
41 					Assert.IsFalse (createdAnotherNew);
42 				}
43 
44 				// Let's make a deny all.
45 				security = handle.GetAccessControl ();
46 
47 				foreach (EventWaitHandleAccessRule rule in security.GetAccessRules
48 				         (true, false, typeof (SecurityIdentifier))) {
49 					security.RemoveAccessRuleSpecific (rule);
50 				}
51 
52 				Assert.AreEqual (0, security.GetAccessRules (true, false, typeof (SecurityIdentifier)).Count);
53 				handle.SetAccessControl (security);
54 
55 				security = handle.GetAccessControl ();
56 				Assert.AreEqual (0, security.GetAccessRules (true, false, typeof (SecurityIdentifier)).Count);
57 
58 				// MS.NET will throw on the first line below.
59 				// For Mono testing the latter verifies the rest until the EventWaitHandle bug is fixed.
60 				// Also, NUnit 2.4 appears to lacks Assert.Pass ().
61 				EventWaitHandle badHandle = new EventWaitHandle(false, EventResetMode.ManualReset, name);
62 				if (badHandle.SafeWaitHandle.IsInvalid)
63 					throw new UnauthorizedAccessException ();
64 			}
65 		}
66 	}
67 }
68 
69