1 // SemaphoreSecurityTest.cs - NUnit Test Cases for SemaphoreSecurity
4 // James Bellinger <jfb@zer7.com>
6 // Copyright (C) 2012 James Bellinger
9 using System.Collections.Generic;
10 using System.Security.AccessControl;
11 using System.Security.Principal;
12 using System.Threading;
13 using NUnit.Framework;
15 namespace MonoTests.System.Security.AccessControl
18 public class SemaphoreSecurityTest
20 // TODO: Mono System.Threading.Semaphore does not throw exceptions on failure (except in OpenExisting).
21 [Test, ExpectedExceptionAttribute (typeof (UnauthorizedAccessException))]
22 public void PermissionsActuallyWork ()
24 if (PlatformID.Win32NT != Environment.OSVersion.Platform) {
28 bool createdNew; SemaphoreSecurity security;
29 string name = @"Local\MonoTestSemaphore";
31 using (Semaphore semaphore = new Semaphore (1, 1, name, out createdNew)) {
32 Assert.IsFalse (semaphore.SafeWaitHandle.IsInvalid);
33 Assert.IsTrue (createdNew);
35 // Make sure our later error will be due to permissions and not some sharing bug.
36 bool createdAnotherNew;
37 using (Semaphore anotherSemaphore = new Semaphore (1, 1, name, out createdAnotherNew)) {
38 Assert.IsFalse (anotherSemaphore.SafeWaitHandle.IsInvalid);
39 Assert.IsFalse (createdAnotherNew);
42 // Let's make a deny all.
43 security = semaphore.GetAccessControl ();
45 foreach (SemaphoreAccessRule rule in security.GetAccessRules
46 (true, false, typeof (SecurityIdentifier))) {
47 security.RemoveAccessRuleSpecific (rule);
50 Assert.AreEqual (0, security.GetAccessRules (true, false, typeof (SecurityIdentifier)).Count);
51 semaphore.SetAccessControl (security);
53 security = semaphore.GetAccessControl ();
54 Assert.AreEqual (0, security.GetAccessRules (true, false, typeof (SecurityIdentifier)).Count);
56 // MS.NET will throw on the first line below.
57 // For Mono testing the latter verifies the rest until the Semaphore bug is fixed.
58 // Also, NUnit 2.4 appears to lacks Assert.Pass ().
59 Semaphore badSemaphore = new Semaphore (1, 1, name);
60 if (badSemaphore.SafeWaitHandle.IsInvalid)
61 throw new UnauthorizedAccessException ();