1 //
2 // SecurityIdentifierTest.cs - NUnit Test Cases for SecurityIdentifier
3 //
4 // Author:
5 //	Kenneth Bell
6 //
7 
8 using System;
9 using System.Security.Principal;
10 using System.Text;
11 using NUnit.Framework;
12 
13 namespace MonoTests.System.Security.Principal
14 {
15 	[TestFixture]
16 	public class SecurityIdentifierTest
17 	{
18 		[Test, ExpectedException (typeof (ArgumentNullException))]
ConstructorNull()19 		public void ConstructorNull ()
20 		{
21 			new SecurityIdentifier (null);
22 		}
23 
CheckStringCtor(string strValue, byte[] expectedBinary)24 		private void CheckStringCtor (string strValue, byte[] expectedBinary)
25 		{
26 			SecurityIdentifier sid = new SecurityIdentifier (strValue);
27 			byte[] buffer = new byte[sid.BinaryLength];
28 			sid.GetBinaryForm (buffer, 0);
29 
30 			Assert.AreEqual (expectedBinary.Length, buffer.Length, "SID length mismatch");
31 			Assert.AreEqual (expectedBinary, buffer, "SIDs different in binary form");
32 		}
33 
CheckUnqualifiedWellKnownSid(WellKnownSidType type, string sddl)34 		private void CheckUnqualifiedWellKnownSid (WellKnownSidType type, string sddl)
35 		{
36 			SecurityIdentifier sid = new SecurityIdentifier (type, null);
37 			Assert.AreEqual (sddl, sid.Value, "Bad SID for type: " + type);
38 		}
39 
CheckQualifiedWellKnownSid(WellKnownSidType type, SecurityIdentifier domain, string sddl)40 		private void CheckQualifiedWellKnownSid (WellKnownSidType type, SecurityIdentifier domain, string sddl)
41 		{
42 			SecurityIdentifier sid = new SecurityIdentifier (type, domain);
43 			Assert.AreEqual (sddl, sid.Value, "Bad SID for type: " + type);
44 		}
45 
CheckWellKnownSidLookup(WellKnownSidType wellKnownSidType, string name)46 		private void CheckWellKnownSidLookup (WellKnownSidType wellKnownSidType, string name)
47 		{
48 			Assert.AreEqual (name, ((NTAccount)new SecurityIdentifier (wellKnownSidType, null).Translate (typeof(NTAccount))).Value);
49 		}
50 
51 		[Test]
ConstructorString()52 		public void ConstructorString ()
53 		{
54 			CheckStringCtor ("S-1-0-0",
55 			                 new byte[] {
56 				0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 				0x00, 0x00 });
58 			CheckStringCtor ("S-1-5-33",
59 			                 new byte[] {
60 				0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x21, 0x00,
61 				0x00, 0x00 });
62 			CheckStringCtor ("s-1-5-334-234",
63 			                 new byte[] {
64 				0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x4E, 0x01,
65 				0x00, 0x00, 0xEA, 0x00, 0x00, 0x00 });
66 			CheckStringCtor ("S-1-5-0x3432",
67 			                 new byte[] {
68 				0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x32, 0x34,
69 				0x00, 0x00 });
70 			CheckStringCtor ("S-1-0xCBA987654321-0",
71 			                 new byte[] {
72 				0x01, 0x01, 0xCB, 0xA9, 0x87, 0x65, 0x43, 0x21, 0x00, 0x00,
73 				0x00, 0x00 });
74 		}
75 
76 		[Test]
ConstructorStringSddl()77 		public void ConstructorStringSddl ()
78 		{
79 			Assert.AreEqual ("S-1-5-32-545",
80 			                 new SecurityIdentifier ("BU").Value);
81 		}
82 
83 		[Test]
84 		[ExpectedException(typeof(ArgumentException))]
ConstructorStringBadRevision()85 		public void ConstructorStringBadRevision ()
86 		{
87 			CheckStringCtor ("S-2-0-0",
88 			                 new byte[] {
89 				0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90 				0x00, 0x00 });
91 		}
92 
93 		[Test]
94 		[ExpectedException(typeof(ArgumentException))]
ConstructorInvalidString()95 		public void ConstructorInvalidString ()
96 		{
97 			new SecurityIdentifier ("M");
98 		}
99 
100 		[Test]
ConstructorBinary()101 		public void ConstructorBinary ()
102 		{
103 			byte[] inForm = new byte[] {
104 				0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x4E, 0x01,
105 				0x00, 0x00, 0xEA, 0x00, 0x00, 0x00 };
106 			SecurityIdentifier sid = new SecurityIdentifier (inForm, 0);
107 
108 			byte[] outForm = new byte[inForm.Length];
109 			sid.GetBinaryForm (outForm, 0);
110 			Assert.AreEqual (inForm, outForm);
111 		}
112 
113 		[Test]
ConstructorWellKnownSids()114 		public void ConstructorWellKnownSids ()
115 		{
116 			CheckUnqualifiedWellKnownSid (WellKnownSidType.NullSid, "S-1-0-0");
117 			CheckUnqualifiedWellKnownSid (WellKnownSidType.WorldSid, "S-1-1-0");
118 			CheckUnqualifiedWellKnownSid (WellKnownSidType.LocalSid, "S-1-2-0");
119 			CheckUnqualifiedWellKnownSid (WellKnownSidType.CreatorOwnerSid, "S-1-3-0");
120 			CheckUnqualifiedWellKnownSid (WellKnownSidType.CreatorGroupSid, "S-1-3-1");
121 			CheckUnqualifiedWellKnownSid (WellKnownSidType.CreatorOwnerServerSid, "S-1-3-2");
122 			CheckUnqualifiedWellKnownSid (WellKnownSidType.CreatorGroupServerSid, "S-1-3-3");
123 			CheckUnqualifiedWellKnownSid (WellKnownSidType.NTAuthoritySid, "S-1-5");
124 			CheckUnqualifiedWellKnownSid (WellKnownSidType.DialupSid, "S-1-5-1");
125 			CheckUnqualifiedWellKnownSid (WellKnownSidType.NetworkSid, "S-1-5-2");
126 			CheckUnqualifiedWellKnownSid (WellKnownSidType.BatchSid, "S-1-5-3");
127 			CheckUnqualifiedWellKnownSid (WellKnownSidType.InteractiveSid, "S-1-5-4");
128 			CheckUnqualifiedWellKnownSid (WellKnownSidType.ServiceSid, "S-1-5-6");
129 			CheckUnqualifiedWellKnownSid (WellKnownSidType.AnonymousSid, "S-1-5-7");
130 			CheckUnqualifiedWellKnownSid (WellKnownSidType.ProxySid, "S-1-5-8");
131 			CheckUnqualifiedWellKnownSid (WellKnownSidType.EnterpriseControllersSid, "S-1-5-9");
132 			CheckUnqualifiedWellKnownSid (WellKnownSidType.SelfSid, "S-1-5-10");
133 			CheckUnqualifiedWellKnownSid (WellKnownSidType.AuthenticatedUserSid, "S-1-5-11");
134 			CheckUnqualifiedWellKnownSid (WellKnownSidType.RestrictedCodeSid, "S-1-5-12");
135 			CheckUnqualifiedWellKnownSid (WellKnownSidType.TerminalServerSid, "S-1-5-13");
136 			CheckUnqualifiedWellKnownSid (WellKnownSidType.RemoteLogonIdSid, "S-1-5-14");
137 			CheckUnqualifiedWellKnownSid (WellKnownSidType.LocalSystemSid, "S-1-5-18");
138 			CheckUnqualifiedWellKnownSid (WellKnownSidType.LocalServiceSid, "S-1-5-19");
139 			CheckUnqualifiedWellKnownSid (WellKnownSidType.NetworkServiceSid, "S-1-5-20");
140 			CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinDomainSid, "S-1-5-32");
141 			CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinAdministratorsSid, "S-1-5-32-544");
142 			CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinUsersSid, "S-1-5-32-545");
143 			CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinGuestsSid, "S-1-5-32-546");
144 			CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinPowerUsersSid, "S-1-5-32-547");
145 			CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinAccountOperatorsSid, "S-1-5-32-548");
146 			CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinSystemOperatorsSid, "S-1-5-32-549");
147 			CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinPrintOperatorsSid, "S-1-5-32-550");
148 			CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinBackupOperatorsSid, "S-1-5-32-551");
149 			CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinReplicatorSid, "S-1-5-32-552");
150 			CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinPreWindows2000CompatibleAccessSid, "S-1-5-32-554");
151 			CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinRemoteDesktopUsersSid, "S-1-5-32-555");
152 			CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinNetworkConfigurationOperatorsSid, "S-1-5-32-556");
153 			CheckQualifiedWellKnownSid (WellKnownSidType.AccountAdministratorSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-500");
154 			CheckQualifiedWellKnownSid (WellKnownSidType.AccountGuestSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-501");
155 			CheckQualifiedWellKnownSid (WellKnownSidType.AccountKrbtgtSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-502");
156 			CheckQualifiedWellKnownSid (WellKnownSidType.AccountDomainAdminsSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-512");
157 			CheckQualifiedWellKnownSid (WellKnownSidType.AccountDomainUsersSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-513");
158 			CheckQualifiedWellKnownSid (WellKnownSidType.AccountDomainGuestsSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-514");
159 			CheckQualifiedWellKnownSid (WellKnownSidType.AccountComputersSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-515");
160 			CheckQualifiedWellKnownSid (WellKnownSidType.AccountControllersSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-516");
161 			CheckQualifiedWellKnownSid (WellKnownSidType.AccountCertAdminsSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-517");
162 			CheckQualifiedWellKnownSid (WellKnownSidType.AccountSchemaAdminsSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-518");
163 			CheckQualifiedWellKnownSid (WellKnownSidType.AccountEnterpriseAdminsSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-519");
164 			CheckQualifiedWellKnownSid (WellKnownSidType.AccountPolicyAdminsSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-520");
165 			CheckQualifiedWellKnownSid (WellKnownSidType.AccountRasAndIasServersSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-553");
166 			CheckUnqualifiedWellKnownSid (WellKnownSidType.NtlmAuthenticationSid, "S-1-5-64-10");
167 			CheckUnqualifiedWellKnownSid (WellKnownSidType.DigestAuthenticationSid, "S-1-5-64-21");
168 			CheckUnqualifiedWellKnownSid (WellKnownSidType.SChannelAuthenticationSid, "S-1-5-64-14");
169 			CheckUnqualifiedWellKnownSid (WellKnownSidType.ThisOrganizationSid, "S-1-5-15");
170 			CheckUnqualifiedWellKnownSid (WellKnownSidType.OtherOrganizationSid, "S-1-5-1000");
171 			CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinIncomingForestTrustBuildersSid, "S-1-5-32-557");
172 			CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinPerformanceMonitoringUsersSid, "S-1-5-32-558");
173 			CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinPerformanceLoggingUsersSid, "S-1-5-32-559");
174 			CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinAuthorizationAccessSid, "S-1-5-32-560");
175 			CheckUnqualifiedWellKnownSid (WellKnownSidType.WinBuiltinTerminalServerLicenseServersSid, "S-1-5-32-561");
176 			CheckUnqualifiedWellKnownSid (WellKnownSidType.MaxDefined, "S-1-5-32-561");
177 		}
178 
179 		[Test]
180 		[ExpectedException(typeof(ArgumentException))]
ConstructorWellKnownSidLogonIds()181 		public void ConstructorWellKnownSidLogonIds ()
182 		{
183 			CheckQualifiedWellKnownSid (WellKnownSidType.LogonIdsSid,
184 			                            new SecurityIdentifier ("S-1-5-21-125-3215-342"),
185 			                            "S-1-5-21-125-3215-342-3");
186 		}
187 
188 		[Test]
AccountDomainSid()189 		public void AccountDomainSid ()
190 		{
191 			Assert.AreEqual ("S-1-5-21-125-3215-342", new SecurityIdentifier ("S-1-5-21-125-3215-342-324-1000").AccountDomainSid.Value);
192 			Assert.AreEqual ("S-1-5-21-125-3215-342", new SecurityIdentifier ("S-1-5-21-125-3215-342-1000").AccountDomainSid.Value);
193 			Assert.AreEqual ("S-1-5-21-125-3215-1", new SecurityIdentifier ("S-1-5-21-125-3215-1").AccountDomainSid.Value);
194 			Assert.IsNull (new SecurityIdentifier ("S-1-5-21-125-1").AccountDomainSid);
195 			Assert.IsNull (new SecurityIdentifier ("S-1-0-0").AccountDomainSid);
196 			Assert.IsNull (new SecurityIdentifier ("S-1-5-44-125-3215-1").AccountDomainSid);
197 		}
198 
199 		[Test]
BinaryLength()200 		public void BinaryLength ()
201 		{
202 			Assert.AreEqual (12, new SecurityIdentifier ("S-1-0-0").BinaryLength);
203 		}
204 
205 		[Test]
Value()206 		public void Value ()
207 		{
208 			Assert.AreEqual ("S-1-5-13362", new SecurityIdentifier ("s-1-5-0x3432").Value);
209 		}
210 
211 		[Test]
Equals()212 		public void Equals ()
213 		{
214 			Assert.IsTrue (new SecurityIdentifier ("S-1-5-13362").Equals (new SecurityIdentifier ("s-1-5-0x3432")));
215 		}
216 
217 		[Test]
IsAccountSid()218 		public void IsAccountSid ()
219 		{
220 			Assert.IsTrue (new SecurityIdentifier ("S-1-5-21-125-3215-342-324-1000").IsAccountSid ());
221 			Assert.IsTrue (new SecurityIdentifier ("S-1-5-21-125-3215-342-1000").IsAccountSid ());
222 			Assert.IsTrue (new SecurityIdentifier ("S-1-5-21-125-3215-1").IsAccountSid ());
223 			Assert.IsFalse (new SecurityIdentifier ("S-1-5-21-125-1").IsAccountSid ());
224 			Assert.IsFalse (new SecurityIdentifier ("S-1-0-0").IsAccountSid ());
225 		}
226 
227 		[Test]
IsEqualDomainSid()228 		public void IsEqualDomainSid ()
229 		{
230 			Assert.IsTrue (new SecurityIdentifier ("S-1-5-21-125-3215-342-1000").IsEqualDomainSid (new SecurityIdentifier ("S-1-5-21-125-3215-342-333")));
231 			Assert.IsTrue (new SecurityIdentifier ("S-1-5-21-125-3215-342-1000").IsEqualDomainSid (new SecurityIdentifier ("S-1-5-21-125-3215-342-324-333")));
232 			Assert.IsFalse (new SecurityIdentifier ("S-1-5-21-125-1").IsEqualDomainSid (new SecurityIdentifier ("S-1-5-21-125-2")));
233 			Assert.IsFalse (new SecurityIdentifier ("S-1-0-0").IsEqualDomainSid (new SecurityIdentifier ("S-1-0-0")));
234 		}
235 
236 		[Test]
IsValidTargetType()237 		public void IsValidTargetType ()
238 		{
239 			Assert.IsTrue (new SecurityIdentifier ("S-1-0-0").IsValidTargetType (typeof(SecurityIdentifier)));
240 			Assert.IsTrue (new SecurityIdentifier ("S-1-0-0").IsValidTargetType (typeof(NTAccount)));
241 			Assert.IsFalse (new SecurityIdentifier ("S-1-0-0").IsValidTargetType (typeof(WindowsPrincipal)));
242 			Assert.IsFalse (new SecurityIdentifier ("S-1-0-0").IsValidTargetType (typeof(WindowsIdentity)));
243 		}
244 
245 		[Test]
IsWellKnown()246 		public void IsWellKnown ()
247 		{
248 			Assert.IsTrue (new SecurityIdentifier ("S-1-0-0").IsWellKnown (WellKnownSidType.NullSid));
249 			Assert.IsTrue (new SecurityIdentifier ("S-1-5-21-125-3215-342-500").IsWellKnown (WellKnownSidType.AccountAdministratorSid));
250 			Assert.IsTrue (new SecurityIdentifier ("S-1-5-21-125-3215-342-513").IsWellKnown (WellKnownSidType.AccountDomainUsersSid));
251 			Assert.IsFalse (new SecurityIdentifier ("S-1-6-21-125-3215-342-513").IsWellKnown (WellKnownSidType.AccountDomainUsersSid));
252 			Assert.IsFalse (new SecurityIdentifier ("S-1-5-22-125-3215-342-513").IsWellKnown (WellKnownSidType.AccountDomainUsersSid));
253 		}
254 
255 		[Test]
Translate()256 		public void Translate ()
257 		{
258 			CheckWellKnownSidLookup (WellKnownSidType.NullSid, @"NULL SID");
259 			CheckWellKnownSidLookup (WellKnownSidType.WorldSid, @"Everyone");
260 			CheckWellKnownSidLookup (WellKnownSidType.LocalSid, @"LOCAL");
261 			CheckWellKnownSidLookup (WellKnownSidType.CreatorOwnerSid, @"CREATOR OWNER");
262 			CheckWellKnownSidLookup (WellKnownSidType.CreatorGroupSid, @"CREATOR GROUP");
263 			CheckWellKnownSidLookup (WellKnownSidType.CreatorOwnerServerSid, @"CREATOR OWNER SERVER");
264 			CheckWellKnownSidLookup (WellKnownSidType.CreatorGroupServerSid, @"CREATOR GROUP SERVER");
265 			CheckWellKnownSidLookup (WellKnownSidType.DialupSid, @"NT AUTHORITY\DIALUP");
266 			CheckWellKnownSidLookup (WellKnownSidType.NetworkSid, @"NT AUTHORITY\NETWORK");
267 			CheckWellKnownSidLookup (WellKnownSidType.BatchSid, @"NT AUTHORITY\BATCH");
268 			CheckWellKnownSidLookup (WellKnownSidType.InteractiveSid, @"NT AUTHORITY\INTERACTIVE");
269 			CheckWellKnownSidLookup (WellKnownSidType.ServiceSid, @"NT AUTHORITY\SERVICE");
270 			CheckWellKnownSidLookup (WellKnownSidType.AnonymousSid, @"NT AUTHORITY\ANONYMOUS LOGON");
271 			CheckWellKnownSidLookup (WellKnownSidType.ProxySid, @"NT AUTHORITY\PROXY");
272 			CheckWellKnownSidLookup (WellKnownSidType.EnterpriseControllersSid, @"NT AUTHORITY\ENTERPRISE DOMAIN CONTROLLERS");
273 			CheckWellKnownSidLookup (WellKnownSidType.SelfSid, @"NT AUTHORITY\SELF");
274 			CheckWellKnownSidLookup (WellKnownSidType.AuthenticatedUserSid, @"NT AUTHORITY\Authenticated Users");
275 			CheckWellKnownSidLookup (WellKnownSidType.RestrictedCodeSid, @"NT AUTHORITY\RESTRICTED");
276 			CheckWellKnownSidLookup (WellKnownSidType.TerminalServerSid, @"NT AUTHORITY\TERMINAL SERVER USER");
277 			CheckWellKnownSidLookup (WellKnownSidType.RemoteLogonIdSid, @"NT AUTHORITY\REMOTE INTERACTIVE LOGON");
278 			CheckWellKnownSidLookup (WellKnownSidType.LocalSystemSid, @"NT AUTHORITY\SYSTEM");
279 			CheckWellKnownSidLookup (WellKnownSidType.LocalServiceSid, @"NT AUTHORITY\LOCAL SERVICE");
280 			CheckWellKnownSidLookup (WellKnownSidType.NetworkServiceSid, @"NT AUTHORITY\NETWORK SERVICE");
281 			CheckWellKnownSidLookup (WellKnownSidType.BuiltinAdministratorsSid, @"BUILTIN\Administrators");
282 			CheckWellKnownSidLookup (WellKnownSidType.BuiltinUsersSid, @"BUILTIN\Users");
283 			CheckWellKnownSidLookup (WellKnownSidType.BuiltinGuestsSid, @"BUILTIN\Guests");
284 			CheckWellKnownSidLookup (WellKnownSidType.NtlmAuthenticationSid, @"NT AUTHORITY\NTLM Authentication");
285 			CheckWellKnownSidLookup (WellKnownSidType.DigestAuthenticationSid, @"NT AUTHORITY\Digest Authentication");
286 			CheckWellKnownSidLookup (WellKnownSidType.SChannelAuthenticationSid, @"NT AUTHORITY\SChannel Authentication");
287 			CheckWellKnownSidLookup (WellKnownSidType.ThisOrganizationSid, @"NT AUTHORITY\This Organization");
288 			CheckWellKnownSidLookup (WellKnownSidType.OtherOrganizationSid, @"NT AUTHORITY\Other Organization");
289 			CheckWellKnownSidLookup (WellKnownSidType.BuiltinPerformanceMonitoringUsersSid, @"BUILTIN\Performance Monitor Users");
290 			CheckWellKnownSidLookup (WellKnownSidType.BuiltinPerformanceLoggingUsersSid, @"BUILTIN\Performance Log Users");
291 		}
292 
293 		[Test]
294 		[ExpectedException(typeof(IdentityNotMappedException))]
TranslateUnknown()295 		public void TranslateUnknown ()
296 		{
297 			new SecurityIdentifier ("S-1-5-21-125-3215-342-513").Translate (typeof(NTAccount));
298 		}
299 
300 		[Test]
LengthLimits()301 		public void LengthLimits ()
302 		{
303 			Assert.AreEqual (8, SecurityIdentifier.MinBinaryLength);
304 			Assert.AreEqual (68, SecurityIdentifier.MaxBinaryLength);
305 		}
306 
307 		[Test]
CompareOrdering()308 		public void CompareOrdering ()
309 		{
310 			SecurityIdentifier[] sids = new SecurityIdentifier[] {
311 				new SecurityIdentifier ("S-1-5-32-544"),
312 				new SecurityIdentifier ("S-1-5-40"),
313 				new SecurityIdentifier ("S-1-5-32-5432"),
314 				new SecurityIdentifier ("S-1-6-0"),
315 				new SecurityIdentifier ("S-1-5-32-99"),
316 				new SecurityIdentifier ("S-1-0-2")
317 			};
318 
319 			SecurityIdentifier[] sortedSids = (SecurityIdentifier[])sids.Clone ();
320 			Array.Sort (sortedSids);
321 
322 			Assert.AreSame (sids [5], sortedSids [0]);
323 			Assert.AreSame (sids [1], sortedSids [1]);
324 			Assert.AreSame (sids [4], sortedSids [2]);
325 			Assert.AreSame (sids [0], sortedSids [3]);
326 			Assert.AreSame (sids [2], sortedSids [4]);
327 			Assert.AreSame (sids [3], sortedSids [5]);
328 		}
329 
330 		[Test, ExpectedExceptionAttribute (typeof (ArgumentNullException))]
CompareToNull()331 		public void CompareToNull ()
332 		{
333 			SecurityIdentifier sid = new SecurityIdentifier (WellKnownSidType.BuiltinUsersSid, null);
334 			sid.CompareTo ((SecurityIdentifier)null);
335 		}
336 
337 		[Test]
EqualsNull()338 		public void EqualsNull ()
339 		{
340 			SecurityIdentifier sid = new SecurityIdentifier (WellKnownSidType.BuiltinUsersSid, null);
341 			Assert.IsFalse (sid.Equals ((object)null));
342 			Assert.IsFalse (sid.Equals ((SecurityIdentifier)null));
343 		}
344 
345 		[Test]
IntPtrRoundtrip()346 		public unsafe void IntPtrRoundtrip ()
347 		{
348 			SecurityIdentifier sidIn, sidOut;
349 			byte[] binaryFormIn, binaryFormOut;
350 
351 			sidIn = new SecurityIdentifier ("WD");
352 			binaryFormIn = new byte[sidIn.BinaryLength];
353 			sidIn.GetBinaryForm (binaryFormIn, 0);
354 
355 			fixed (byte* pointerForm = binaryFormIn)
356 				sidOut = new SecurityIdentifier ((IntPtr)pointerForm);
357 			binaryFormOut = new byte[sidOut.BinaryLength];
358 			sidOut.GetBinaryForm (binaryFormOut, 0);
359 
360 			Assert.AreEqual (sidIn, sidOut);
361 			Assert.AreEqual (binaryFormIn, binaryFormOut);
362 		}
363 	}
364 }
365