-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
MutexAclTests.cs
262 lines (220 loc) · 10.3 KB
/
MutexAclTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Security.Principal;
using Xunit;
namespace System.Threading.Tests
{
public class MutexAclTests : AclTests
{
[Fact]
public void Mutex_Create_NullSecurity()
{
CreateAndVerifyMutex(initiallyOwned: true, GetRandomName(), expectedSecurity: null, expectedCreatedNew: true).Dispose();
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void Mutex_Create_NameMultipleNew(string name)
{
MutexSecurity security = GetBasicMutexSecurity();
using Mutex mutex1 = CreateAndVerifyMutex(initiallyOwned: true, name, security, expectedCreatedNew: true);
using Mutex mutex2 = CreateAndVerifyMutex(initiallyOwned: true, name, security, expectedCreatedNew: true);
}
[Fact]
public void Mutex_Create_CreateNewExisting()
{
string name = GetRandomName();
MutexSecurity security = GetBasicMutexSecurity();
using Mutex mutexNew = CreateAndVerifyMutex(initiallyOwned: true, name, security, expectedCreatedNew: true);
using Mutex mutexExisting = CreateAndVerifyMutex(initiallyOwned: true, name, security, expectedCreatedNew: false);
}
[Fact]
public void Mutex_Create_BeyondMaxPathLength()
{
// GetRandomName prevents name collision when two tests run at the same time
string name = GetRandomName() + new string('x', Interop.Kernel32.MAX_PATH);
if (PlatformDetection.IsNetFramework)
{
Assert.Throws<ArgumentException>(() =>
{
CreateAndVerifyMutex(initiallyOwned: true, name, GetBasicMutexSecurity(), expectedCreatedNew: true).Dispose();
});
}
else
{
using Mutex created = CreateAndVerifyMutex(initiallyOwned: true, name, GetBasicMutexSecurity(), expectedCreatedNew: true);
using Mutex openedByName = Mutex.OpenExisting(name);
Assert.NotNull(openedByName);
}
}
public static IEnumerable<object[]> GetMutexSpecificParameters() =>
from initiallyOwned in new[] { false, true }
from rights in new[] { MutexRights.FullControl, MutexRights.Synchronize, MutexRights.Modify, MutexRights.Modify | MutexRights.Synchronize }
from accessControl in new[] { AccessControlType.Allow, AccessControlType.Deny }
select new object[] { initiallyOwned, rights, accessControl };
[Theory]
[MemberData(nameof(GetMutexSpecificParameters))]
public void Mutex_Create_SpecificParameters(bool initiallyOwned, MutexRights rights, AccessControlType accessControl)
{
MutexSecurity security = GetMutexSecurity(WellKnownSidType.BuiltinUsersSid, rights, accessControl);
CreateAndVerifyMutex(initiallyOwned, GetRandomName(), security, expectedCreatedNew: true).Dispose();
}
[Fact]
public void Mutex_OpenExisting()
{
string name = GetRandomName();
MutexSecurity expectedSecurity = GetMutexSecurity(WellKnownSidType.BuiltinUsersSid, MutexRights.FullControl, AccessControlType.Allow);
using Mutex mutexNew = CreateAndVerifyMutex(initiallyOwned: true, name, expectedSecurity, expectedCreatedNew: true);
using Mutex mutexExisting = MutexAcl.OpenExisting(name, MutexRights.FullControl);
VerifyHandles(mutexNew, mutexExisting);
MutexSecurity actualSecurity = mutexExisting.GetAccessControl();
VerifyMutexSecurity(expectedSecurity, actualSecurity);
}
[Fact]
public void Mutex_TryOpenExisting()
{
string name = GetRandomName();
MutexSecurity expectedSecurity = GetMutexSecurity(WellKnownSidType.BuiltinUsersSid, MutexRights.FullControl, AccessControlType.Allow);
using Mutex mutexNew = CreateAndVerifyMutex(initiallyOwned: true, name, expectedSecurity, expectedCreatedNew: true);
Assert.True(MutexAcl.TryOpenExisting(name, MutexRights.FullControl, out Mutex mutexExisting));
Assert.NotNull(mutexExisting);
VerifyHandles(mutexNew, mutexExisting);
MutexSecurity actualSecurity = mutexExisting.GetAccessControl();
VerifyMutexSecurity(expectedSecurity, actualSecurity);
mutexExisting.Dispose();
}
[Fact]
public void Mutex_OpenExisting_NameNotFound()
{
string name = "ThisShouldNotExist";
Assert.Throws<WaitHandleCannotBeOpenedException>(() =>
{
MutexAcl.OpenExisting(name, MutexRights.FullControl).Dispose();
});
Assert.False(MutexAcl.TryOpenExisting(name, MutexRights.FullControl, out _));
}
[Fact]
public void Mutex_OpenExisting_NameInvalid()
{
string name = '\0'.ToString();
Assert.Throws<WaitHandleCannotBeOpenedException>(() =>
{
MutexAcl.OpenExisting(name, MutexRights.FullControl).Dispose();
});
Assert.False(MutexAcl.TryOpenExisting(name, MutexRights.FullControl, out _));
}
[Fact]
public void Mutex_OpenExisting_PathNotFound()
{
string name = @"global\foo";
Assert.Throws<DirectoryNotFoundException>(() =>
{
MutexAcl.OpenExisting(name, MutexRights.FullControl).Dispose();
});
Assert.False(MutexAcl.TryOpenExisting(name, MutexRights.FullControl, out _));
}
[Fact]
public void Mutex_OpenExisting_BadPathName()
{
string name = @"\\?\Path";
Assert.Throws<System.IO.IOException>(() =>
{
MutexAcl.OpenExisting(name, MutexRights.FullControl).Dispose();
});
Assert.Throws<System.IO.IOException>(() =>
{
MutexAcl.TryOpenExisting(name, MutexRights.FullControl, out _);
});
}
[Fact]
public void Mutex_OpenExisting_NullName()
{
Assert.Throws<ArgumentNullException>(() =>
{
MutexAcl.OpenExisting(null, MutexRights.FullControl).Dispose();
});
Assert.Throws<ArgumentNullException>(() =>
{
MutexAcl.TryOpenExisting(null, MutexRights.FullControl, out _);
});
}
[Fact]
public void Mutex_OpenExisting_EmptyName()
{
Assert.Throws<ArgumentException>(() =>
{
MutexAcl.OpenExisting(string.Empty, MutexRights.FullControl).Dispose();
});
Assert.Throws<ArgumentException>(() =>
{
MutexAcl.TryOpenExisting(string.Empty, MutexRights.FullControl, out _);
});
}
private MutexSecurity GetBasicMutexSecurity()
{
return GetMutexSecurity(
WellKnownSidType.BuiltinUsersSid,
MutexRights.FullControl,
AccessControlType.Allow);
}
private MutexSecurity GetMutexSecurity(WellKnownSidType sid, MutexRights rights, AccessControlType accessControl)
{
MutexSecurity security = new MutexSecurity();
SecurityIdentifier identity = new SecurityIdentifier(sid, null);
MutexAccessRule accessRule = new MutexAccessRule(identity, rights, accessControl);
security.AddAccessRule(accessRule);
return security;
}
private Mutex CreateAndVerifyMutex(bool initiallyOwned, string name, MutexSecurity expectedSecurity, bool expectedCreatedNew)
{
Mutex mutex = MutexAcl.Create(initiallyOwned, name, out bool createdNew, expectedSecurity);
Assert.NotNull(mutex);
Assert.Equal(createdNew, expectedCreatedNew);
if (expectedSecurity != null)
{
MutexSecurity actualSecurity = mutex.GetAccessControl();
VerifyMutexSecurity(expectedSecurity, actualSecurity);
}
return mutex;
}
private void VerifyHandles(Mutex expected, Mutex actual)
{
Assert.NotNull(expected.SafeWaitHandle);
Assert.NotNull(actual.SafeWaitHandle);
Assert.False(expected.SafeWaitHandle.IsClosed);
Assert.False(actual.SafeWaitHandle.IsClosed);
Assert.False(expected.SafeWaitHandle.IsInvalid);
Assert.False(actual.SafeWaitHandle.IsInvalid);
}
private void VerifyMutexSecurity(MutexSecurity expectedSecurity, MutexSecurity actualSecurity)
{
Assert.Equal(typeof(MutexRights), expectedSecurity.AccessRightType);
Assert.Equal(typeof(MutexRights), actualSecurity.AccessRightType);
List<MutexAccessRule> expectedAccessRules = expectedSecurity.GetAccessRules(includeExplicit: true, includeInherited: false, typeof(SecurityIdentifier))
.Cast<MutexAccessRule>().ToList();
List<MutexAccessRule> actualAccessRules = actualSecurity.GetAccessRules(includeExplicit: true, includeInherited: false, typeof(SecurityIdentifier))
.Cast<MutexAccessRule>().ToList();
Assert.Equal(expectedAccessRules.Count, actualAccessRules.Count);
if (expectedAccessRules.Count > 0)
{
Assert.All(expectedAccessRules, actualAccessRule =>
{
int count = expectedAccessRules.Count(expectedAccessRule => AreAccessRulesEqual(expectedAccessRule, actualAccessRule));
Assert.True(count > 0);
});
}
}
private bool AreAccessRulesEqual(MutexAccessRule expectedRule, MutexAccessRule actualRule)
{
return
expectedRule.AccessControlType == actualRule.AccessControlType &&
expectedRule.MutexRights == actualRule.MutexRights &&
expectedRule.InheritanceFlags == actualRule.InheritanceFlags &&
expectedRule.PropagationFlags == actualRule.PropagationFlags;
}
}
}