Merge pull request #2346 from xmcclure/proxy-load-fail
[mono.git] / mcs / class / System / Test / System.Security.AccessControl / SemaphoreSecurityTest.cs
1 // SemaphoreSecurityTest.cs - NUnit Test Cases for SemaphoreSecurity
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 SemaphoreSecurityTest
19         {
20                 // TODO: Mono System.Threading.Semaphore does not throw exceptions on failure (except in OpenExisting).
21                 [Test, ExpectedExceptionAttribute (typeof (UnauthorizedAccessException))]
22                 public void PermissionsActuallyWork ()
23                 {
24                         if (PlatformID.Win32NT != Environment.OSVersion.Platform) {
25                                 Assert.Ignore ();
26                         }
27
28                         bool createdNew; SemaphoreSecurity security;
29                         string name = @"Local\MonoTestSemaphore";
30
31                         using (Semaphore semaphore = new Semaphore (1, 1, name, out createdNew)) {
32                                 Assert.IsFalse (semaphore.SafeWaitHandle.IsInvalid);
33                                 Assert.IsTrue (createdNew);
34
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);
40                                 }
41
42                                 // Let's make a deny all.
43                                 security = semaphore.GetAccessControl ();
44
45                                 foreach (SemaphoreAccessRule rule in security.GetAccessRules
46                                          (true, false, typeof (SecurityIdentifier))) {
47                                         security.RemoveAccessRuleSpecific (rule);
48                                 }
49
50                                 Assert.AreEqual (0, security.GetAccessRules (true, false, typeof (SecurityIdentifier)).Count);
51                                 semaphore.SetAccessControl (security);
52
53                                 security = semaphore.GetAccessControl ();
54                                 Assert.AreEqual (0, security.GetAccessRules (true, false, typeof (SecurityIdentifier)).Count);
55
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 ();
62                         }
63                 }
64         }
65 }
66