Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
2009-06-24 Zoltan Varga <vargaz@gmail.com>
* *.cs: Convert all tests to new-style nunit classes/methods.
2009-02-04 Zoltan Varga <vargaz@gmail.com>
* GenericIdentityTest.cs GenericPrincipalTest.cs: Make these tests
quiet.
2006-08-08 Sebastien Pouliot <sebastien@ximian.com>
* GenericIdentityTest.cs: Added serialization test cases.
* GenericPrincipalTest.cs: Added serialization test cases.
2005-08-30 Sebastien Pouliot <sebastien@ximian.com>
* GenericPrincipalTest.cs: Roles are case insensitive.
2005-06-17 Sebastien Pouliot <sebastien@ximian.com>
* WindowsPrincipalTest.cs: In NET_2_0 multiple IsInRole methods can
now accept null.
2005-05-09 Sebastien Pouliot <sebastien@ximian.com>
* WindowsIdentityTest.cs: Fixed IsPosix for NET_2_0.
2004-04-20 Sebastien Pouliot <sebastien@ximian.com>
* WindowsIdentityTest.cs: Don't fail on Windows for some contructor
tests (in NET_1_1). Checking for Windows 2003 Server isn't enough as
the domain must also be Windows 2003 Server based to work. Also skip
the GetRolesViaReflection when running os POSIX systems.
2004-04-14 Sebastien Pouliot <sebastien@ximian.com>
* WindowsIdentityTest.cs: Added reflection and serialization tests.
2004-04-13 Sebastien Pouliot <sebastien@ximian.com>
* WindowsIdentityTest.cs: Adjusted unit tests to run on both Windows
and Linux (or any POSIX compliant OS).
* WindowsPrincipalTest.cs: Removed [Ignore] from some tests.
2003-12-29 Sebastien Pouliot <spouliot@videotron.ca>
* WindowsIdentityTest.cs: New. Partial unit test for WindowsIdentity.
* WindowsPrincipalTest.cs: New. Partial unit test for WindowsPrincipal.
2003-07-01 Sebastien Pouliot <spouliot@videotron.ca>
* GenericIdentityTest.cs: New. Complete unit test.
* GenericPrincipalTest.cs: New. Complete unit test.
* ChangeLog: New.

View File

@@ -0,0 +1,98 @@
//
// GenericIdentityTest.cs - NUnit Test Cases for GenericIdentity
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2006 Novell, Inc (http://www.novell.com)
//
using NUnit.Framework;
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Principal;
namespace MonoTests.System.Security.Principal {
[TestFixture]
public class GenericIdentityTest {
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void NullName ()
{
GenericIdentity gi = new GenericIdentity (null);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void NullAuthenticationType ()
{
GenericIdentity gi = new GenericIdentity ("user", null);
}
[Test]
public void Name ()
{
GenericIdentity gi = new GenericIdentity ("user");
Assert.AreEqual ("user", gi.Name, "Name");
Assert.AreEqual (String.Empty, gi.AuthenticationType, "AuthenticationType");
Assert.IsTrue (gi.IsAuthenticated, "IsAuthenticated");
}
[Test]
public void NameAuthenticationType ()
{
GenericIdentity gi = new GenericIdentity ("user", "blood oath");
Assert.AreEqual ("user", gi.Name, "Name");
Assert.AreEqual ("blood oath", gi.AuthenticationType, "AuthenticationType");
Assert.IsTrue (gi.IsAuthenticated, "IsAuthenticated");
}
[Test]
public void EmptyName ()
{
GenericIdentity gi = new GenericIdentity ("");
Assert.AreEqual (String.Empty, gi.Name, "Name");
Assert.AreEqual (String.Empty, gi.AuthenticationType, "AuthenticationType");
Assert.IsFalse (gi.IsAuthenticated, "IsAuthenticated");
}
[Test]
public void SerializationRoundtrip ()
{
GenericIdentity gi = new GenericIdentity ("mono", "dna");
BinaryFormatter bf = new BinaryFormatter ();
MemoryStream ms = new MemoryStream ();
bf.Serialize (ms, gi);
//Console.WriteLine (BitConverter.ToString (ms.ToArray ()));
ms.Position = 0;
GenericIdentity clone = (GenericIdentity) bf.Deserialize (ms);
Assert.AreEqual (gi.Name, clone.Name, "Name");
Assert.AreEqual (gi.AuthenticationType, clone.AuthenticationType, "AuthenticationType");
Assert.AreEqual (gi.IsAuthenticated, clone.IsAuthenticated, "IsAuthenticated");
}
#if NET_2_0
static byte[] identity = { 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0x29, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x53, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x2E, 0x50, 0x72, 0x69, 0x6E, 0x63, 0x69, 0x70, 0x61, 0x6C, 0x2E, 0x47, 0x65, 0x6E, 0x65, 0x72, 0x69, 0x63, 0x49, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x74, 0x79, 0x02, 0x00, 0x00, 0x00, 0x06, 0x6D, 0x5F, 0x6E, 0x61, 0x6D, 0x65, 0x06, 0x6D, 0x5F, 0x74, 0x79, 0x70, 0x65, 0x01, 0x01, 0x06, 0x02, 0x00, 0x00, 0x00, 0x04, 0x6D, 0x6F, 0x6E, 0x6F, 0x06, 0x03, 0x00, 0x00, 0x00, 0x03, 0x64, 0x6E, 0x61, 0x0B };
#else
static byte[] identity = { 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0x29, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x53, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x2E, 0x50, 0x72, 0x69, 0x6E, 0x63, 0x69, 0x70, 0x61, 0x6C, 0x2E, 0x47, 0x65, 0x6E, 0x65, 0x72, 0x69, 0x63, 0x49, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x74, 0x79, 0x02, 0x00, 0x00, 0x00, 0x06, 0x6D, 0x5F, 0x6E, 0x61, 0x6D, 0x65, 0x06, 0x6D, 0x5F, 0x74, 0x79, 0x70, 0x65, 0x01, 0x01, 0x06, 0x02, 0x00, 0x00, 0x00, 0x04, 0x6D, 0x6F, 0x6E, 0x6F, 0x06, 0x03, 0x00, 0x00, 0x00, 0x03, 0x64, 0x6E, 0x61, 0x0B };
#endif
[Test]
public void Deserialize ()
{
BinaryFormatter bf = new BinaryFormatter ();
MemoryStream ms = new MemoryStream (identity);
GenericIdentity gi = (GenericIdentity) bf.Deserialize (ms);
Assert.AreEqual ("mono", gi.Name, "Name");
Assert.AreEqual ("dna", gi.AuthenticationType, "AuthenticationType");
Assert.IsTrue (gi.IsAuthenticated, "IsAuthenticated");
}
}
}

View File

@@ -0,0 +1,101 @@
//
// GenericPrincipalTest.cs - NUnit Test Cases for GenericPrincipal
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2006 Novell, Inc (http://www.novell.com)
//
using NUnit.Framework;
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Principal;
namespace MonoTests.System.Security.Principal {
[TestFixture]
public class GenericPrincipalTest {
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void NullIdentity ()
{
GenericPrincipal gp = new GenericPrincipal (null, new string [5]);
}
[Test]
public void NullRoles ()
{
GenericIdentity gi = new GenericIdentity ("user");
GenericPrincipal gp = new GenericPrincipal (gi, null);
Assert.AreEqual ("user", gp.Identity.Name, "Identity");
Assert.IsFalse (gp.IsInRole ("role 1"), "NoRole.IsInRole(x)");
}
[Test]
public void IsInRole ()
{
GenericIdentity gi = new GenericIdentity ("user");
string[] roles = new string [5];
roles [0] = "role 1";
GenericPrincipal gp = new GenericPrincipal (gi, roles);
roles [1] = "role 2";
Assert.IsTrue (gp.IsInRole ("role 1"), "IsInRole (role added before constructor)");
Assert.IsFalse (gp.IsInRole ("role 2"), "IsInRole (role added after constructor)");
}
[Test]
public void IsInRole_CaseInsensitive ()
{
GenericIdentity gi = new GenericIdentity ("user");
GenericPrincipal gp = new GenericPrincipal (gi, new string[2] { "mono", "hackers" });
Assert.IsTrue (gp.IsInRole ("MoNo"), "MoNo");
Assert.IsTrue (gp.IsInRole ("hAcKeRs"), "hAcKeRs");
}
[Test]
public void SerializationRoundtrip ()
{
GenericIdentity gi = new GenericIdentity ("mono", "dna");
GenericPrincipal gp = new GenericPrincipal (gi, new string[2] { "monkey", "hackers" });
BinaryFormatter bf = new BinaryFormatter ();
MemoryStream ms = new MemoryStream ();
bf.Serialize (ms, gp);
//Console.WriteLine (BitConverter.ToString (ms.ToArray ()));
ms.Position = 0;
GenericPrincipal clone = (GenericPrincipal) bf.Deserialize (ms);
Assert.AreEqual (gp.Identity.Name, clone.Identity.Name, "Name");
Assert.AreEqual (gp.Identity.AuthenticationType, clone.Identity.AuthenticationType, "AuthenticationType");
Assert.AreEqual (gp.Identity.IsAuthenticated, clone.Identity.IsAuthenticated, "IsAuthenticated");
Assert.IsTrue (gp.IsInRole ("monkey"), "IsInRole-monkey");
Assert.IsTrue (gp.IsInRole ("hackers"), "IsInRole-hackers");
Assert.IsFalse (gp.IsInRole ("donkey"), "IsInRole-donkey");
}
#if NET_2_0
static byte[] principal = { 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0x2A, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x53, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x2E, 0x50, 0x72, 0x69, 0x6E, 0x63, 0x69, 0x70, 0x61, 0x6C, 0x2E, 0x47, 0x65, 0x6E, 0x65, 0x72, 0x69, 0x63, 0x50, 0x72, 0x69, 0x6E, 0x63, 0x69, 0x70, 0x61, 0x6C, 0x02, 0x00, 0x00, 0x00, 0x0A, 0x6D, 0x5F, 0x69, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x74, 0x79, 0x07, 0x6D, 0x5F, 0x72, 0x6F, 0x6C, 0x65, 0x73, 0x03, 0x06, 0x29, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x53, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x2E, 0x50, 0x72, 0x69, 0x6E, 0x63, 0x69, 0x70, 0x61, 0x6C, 0x2E, 0x47, 0x65, 0x6E, 0x65, 0x72, 0x69, 0x63, 0x49, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x74, 0x79, 0x09, 0x02, 0x00, 0x00, 0x00, 0x09, 0x03, 0x00, 0x00, 0x00, 0x04, 0x02, 0x00, 0x00, 0x00, 0x29, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x53, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x2E, 0x50, 0x72, 0x69, 0x6E, 0x63, 0x69, 0x70, 0x61, 0x6C, 0x2E, 0x47, 0x65, 0x6E, 0x65, 0x72, 0x69, 0x63, 0x49, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x74, 0x79, 0x02, 0x00, 0x00, 0x00, 0x06, 0x6D, 0x5F, 0x6E, 0x61, 0x6D, 0x65, 0x06, 0x6D, 0x5F, 0x74, 0x79, 0x70, 0x65, 0x01, 0x01, 0x06, 0x04, 0x00, 0x00, 0x00, 0x04, 0x6D, 0x6F, 0x6E, 0x6F, 0x06, 0x05, 0x00, 0x00, 0x00, 0x03, 0x64, 0x6E, 0x61, 0x11, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x06, 0x06, 0x00, 0x00, 0x00, 0x06, 0x6D, 0x6F, 0x6E, 0x6B, 0x65, 0x79, 0x06, 0x07, 0x00, 0x00, 0x00, 0x07, 0x68, 0x61, 0x63, 0x6B, 0x65, 0x72, 0x73, 0x0B };
#else
static byte[] principal = { 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0x2A, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x53, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x2E, 0x50, 0x72, 0x69, 0x6E, 0x63, 0x69, 0x70, 0x61, 0x6C, 0x2E, 0x47, 0x65, 0x6E, 0x65, 0x72, 0x69, 0x63, 0x50, 0x72, 0x69, 0x6E, 0x63, 0x69, 0x70, 0x61, 0x6C, 0x02, 0x00, 0x00, 0x00, 0x0A, 0x6D, 0x5F, 0x69, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x74, 0x79, 0x07, 0x6D, 0x5F, 0x72, 0x6F, 0x6C, 0x65, 0x73, 0x03, 0x06, 0x29, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x53, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x2E, 0x50, 0x72, 0x69, 0x6E, 0x63, 0x69, 0x70, 0x61, 0x6C, 0x2E, 0x47, 0x65, 0x6E, 0x65, 0x72, 0x69, 0x63, 0x49, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x74, 0x79, 0x09, 0x02, 0x00, 0x00, 0x00, 0x09, 0x03, 0x00, 0x00, 0x00, 0x04, 0x02, 0x00, 0x00, 0x00, 0x29, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x53, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x2E, 0x50, 0x72, 0x69, 0x6E, 0x63, 0x69, 0x70, 0x61, 0x6C, 0x2E, 0x47, 0x65, 0x6E, 0x65, 0x72, 0x69, 0x63, 0x49, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x74, 0x79, 0x02, 0x00, 0x00, 0x00, 0x06, 0x6D, 0x5F, 0x6E, 0x61, 0x6D, 0x65, 0x06, 0x6D, 0x5F, 0x74, 0x79, 0x70, 0x65, 0x01, 0x01, 0x06, 0x04, 0x00, 0x00, 0x00, 0x04, 0x6D, 0x6F, 0x6E, 0x6F, 0x06, 0x05, 0x00, 0x00, 0x00, 0x03, 0x64, 0x6E, 0x61, 0x11, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x06, 0x06, 0x00, 0x00, 0x00, 0x06, 0x6D, 0x6F, 0x6E, 0x6B, 0x65, 0x79, 0x06, 0x07, 0x00, 0x00, 0x00, 0x07, 0x68, 0x61, 0x63, 0x6B, 0x65, 0x72, 0x73, 0x0B };
#endif
[Test]
public void Deserialize ()
{
BinaryFormatter bf = new BinaryFormatter ();
MemoryStream ms = new MemoryStream (principal);
GenericPrincipal gp = (GenericPrincipal) bf.Deserialize (ms);
Assert.AreEqual ("mono", gp.Identity.Name, "Name");
Assert.AreEqual ("dna", gp.Identity.AuthenticationType, "AuthenticationType");
Assert.IsTrue (gp.Identity.IsAuthenticated, "IsAuthenticated");
Assert.IsTrue (gp.IsInRole ("monkey"), "IsInRole-monkey");
Assert.IsTrue (gp.IsInRole ("hackers"), "IsInRole-hackers");
Assert.IsFalse (gp.IsInRole ("donkey"), "IsInRole-donkey");
}
}
}

View File

@@ -0,0 +1,49 @@
//
// NTAccountTest.cs - NUnit Test Cases for NTAccount
//
// Author:
// Kenneth Bell
//
using System;
using System.Security.Principal;
using System.Text;
using NUnit.Framework;
namespace MonoTests.System.Security.Principal
{
[TestFixture]
public class NTAccountTest
{
[Test]
public void ConstructorOneString()
{
Assert.AreEqual(@"Everyone", new NTAccount("Everyone").Value);
Assert.AreEqual(@"EVERYONE", new NTAccount("EVERYONE").Value);
Assert.AreEqual(@"DoMaIn\uSeR", new NTAccount(@"DoMaIn\uSeR").Value);
}
[Test]
public void ConstructorTwoString()
{
Assert.AreEqual(@"DoMaIn\uSeR", new NTAccount("DoMaIn", "uSeR").Value);
Assert.AreEqual(@"uSeR", new NTAccount(null, "uSeR").Value);
}
[Test]
public void Translate()
{
NTAccount acct = new NTAccount("Everyone");
SecurityIdentifier sid = (SecurityIdentifier)acct.Translate(typeof(SecurityIdentifier));
Assert.AreEqual("S-1-1-0", sid.Value);
}
[Test]
[ExpectedException(typeof(IdentityNotMappedException))]
public void TranslateUnknown()
{
NTAccount acct = new NTAccount(@"UnknownDomain\UnknownUser");
acct.Translate(typeof(SecurityIdentifier));
}
}
}

View File

@@ -0,0 +1,364 @@
//
// SecurityIdentifierTest.cs - NUnit Test Cases for SecurityIdentifier
//
// Author:
// Kenneth Bell
//
using System;
using System.Security.Principal;
using System.Text;
using NUnit.Framework;
namespace MonoTests.System.Security.Principal
{
[TestFixture]
public class SecurityIdentifierTest
{
[Test, ExpectedException (typeof (ArgumentNullException))]
public void ConstructorNull ()
{
new SecurityIdentifier (null);
}
private void CheckStringCtor (string strValue, byte[] expectedBinary)
{
SecurityIdentifier sid = new SecurityIdentifier (strValue);
byte[] buffer = new byte[sid.BinaryLength];
sid.GetBinaryForm (buffer, 0);
Assert.AreEqual (expectedBinary.Length, buffer.Length, "SID length mismatch");
Assert.AreEqual (expectedBinary, buffer, "SIDs different in binary form");
}
private void CheckUnqualifiedWellKnownSid (WellKnownSidType type, string sddl)
{
SecurityIdentifier sid = new SecurityIdentifier (type, null);
Assert.AreEqual (sddl, sid.Value, "Bad SID for type: " + type);
}
private void CheckQualifiedWellKnownSid (WellKnownSidType type, SecurityIdentifier domain, string sddl)
{
SecurityIdentifier sid = new SecurityIdentifier (type, domain);
Assert.AreEqual (sddl, sid.Value, "Bad SID for type: " + type);
}
private void CheckWellKnownSidLookup (WellKnownSidType wellKnownSidType, string name)
{
Assert.AreEqual (name, ((NTAccount)new SecurityIdentifier (wellKnownSidType, null).Translate (typeof(NTAccount))).Value);
}
[Test]
public void ConstructorString ()
{
CheckStringCtor ("S-1-0-0",
new byte[] {
0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00 });
CheckStringCtor ("S-1-5-33",
new byte[] {
0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x21, 0x00,
0x00, 0x00 });
CheckStringCtor ("s-1-5-334-234",
new byte[] {
0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x4E, 0x01,
0x00, 0x00, 0xEA, 0x00, 0x00, 0x00 });
CheckStringCtor ("S-1-5-0x3432",
new byte[] {
0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x32, 0x34,
0x00, 0x00 });
CheckStringCtor ("S-1-0xCBA987654321-0",
new byte[] {
0x01, 0x01, 0xCB, 0xA9, 0x87, 0x65, 0x43, 0x21, 0x00, 0x00,
0x00, 0x00 });
}
[Test]
public void ConstructorStringSddl ()
{
Assert.AreEqual ("S-1-5-32-545",
new SecurityIdentifier ("BU").Value);
}
[Test]
[ExpectedException(typeof(ArgumentException))]
public void ConstructorStringBadRevision ()
{
CheckStringCtor ("S-2-0-0",
new byte[] {
0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00 });
}
[Test]
[ExpectedException(typeof(ArgumentException))]
public void ConstructorInvalidString ()
{
new SecurityIdentifier ("M");
}
[Test]
public void ConstructorBinary ()
{
byte[] inForm = new byte[] {
0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x4E, 0x01,
0x00, 0x00, 0xEA, 0x00, 0x00, 0x00 };
SecurityIdentifier sid = new SecurityIdentifier (inForm, 0);
byte[] outForm = new byte[inForm.Length];
sid.GetBinaryForm (outForm, 0);
Assert.AreEqual (inForm, outForm);
}
[Test]
public void ConstructorWellKnownSids ()
{
CheckUnqualifiedWellKnownSid (WellKnownSidType.NullSid, "S-1-0-0");
CheckUnqualifiedWellKnownSid (WellKnownSidType.WorldSid, "S-1-1-0");
CheckUnqualifiedWellKnownSid (WellKnownSidType.LocalSid, "S-1-2-0");
CheckUnqualifiedWellKnownSid (WellKnownSidType.CreatorOwnerSid, "S-1-3-0");
CheckUnqualifiedWellKnownSid (WellKnownSidType.CreatorGroupSid, "S-1-3-1");
CheckUnqualifiedWellKnownSid (WellKnownSidType.CreatorOwnerServerSid, "S-1-3-2");
CheckUnqualifiedWellKnownSid (WellKnownSidType.CreatorGroupServerSid, "S-1-3-3");
CheckUnqualifiedWellKnownSid (WellKnownSidType.NTAuthoritySid, "S-1-5");
CheckUnqualifiedWellKnownSid (WellKnownSidType.DialupSid, "S-1-5-1");
CheckUnqualifiedWellKnownSid (WellKnownSidType.NetworkSid, "S-1-5-2");
CheckUnqualifiedWellKnownSid (WellKnownSidType.BatchSid, "S-1-5-3");
CheckUnqualifiedWellKnownSid (WellKnownSidType.InteractiveSid, "S-1-5-4");
CheckUnqualifiedWellKnownSid (WellKnownSidType.ServiceSid, "S-1-5-6");
CheckUnqualifiedWellKnownSid (WellKnownSidType.AnonymousSid, "S-1-5-7");
CheckUnqualifiedWellKnownSid (WellKnownSidType.ProxySid, "S-1-5-8");
CheckUnqualifiedWellKnownSid (WellKnownSidType.EnterpriseControllersSid, "S-1-5-9");
CheckUnqualifiedWellKnownSid (WellKnownSidType.SelfSid, "S-1-5-10");
CheckUnqualifiedWellKnownSid (WellKnownSidType.AuthenticatedUserSid, "S-1-5-11");
CheckUnqualifiedWellKnownSid (WellKnownSidType.RestrictedCodeSid, "S-1-5-12");
CheckUnqualifiedWellKnownSid (WellKnownSidType.TerminalServerSid, "S-1-5-13");
CheckUnqualifiedWellKnownSid (WellKnownSidType.RemoteLogonIdSid, "S-1-5-14");
CheckUnqualifiedWellKnownSid (WellKnownSidType.LocalSystemSid, "S-1-5-18");
CheckUnqualifiedWellKnownSid (WellKnownSidType.LocalServiceSid, "S-1-5-19");
CheckUnqualifiedWellKnownSid (WellKnownSidType.NetworkServiceSid, "S-1-5-20");
CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinDomainSid, "S-1-5-32");
CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinAdministratorsSid, "S-1-5-32-544");
CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinUsersSid, "S-1-5-32-545");
CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinGuestsSid, "S-1-5-32-546");
CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinPowerUsersSid, "S-1-5-32-547");
CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinAccountOperatorsSid, "S-1-5-32-548");
CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinSystemOperatorsSid, "S-1-5-32-549");
CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinPrintOperatorsSid, "S-1-5-32-550");
CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinBackupOperatorsSid, "S-1-5-32-551");
CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinReplicatorSid, "S-1-5-32-552");
CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinPreWindows2000CompatibleAccessSid, "S-1-5-32-554");
CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinRemoteDesktopUsersSid, "S-1-5-32-555");
CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinNetworkConfigurationOperatorsSid, "S-1-5-32-556");
CheckQualifiedWellKnownSid (WellKnownSidType.AccountAdministratorSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-500");
CheckQualifiedWellKnownSid (WellKnownSidType.AccountGuestSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-501");
CheckQualifiedWellKnownSid (WellKnownSidType.AccountKrbtgtSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-502");
CheckQualifiedWellKnownSid (WellKnownSidType.AccountDomainAdminsSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-512");
CheckQualifiedWellKnownSid (WellKnownSidType.AccountDomainUsersSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-513");
CheckQualifiedWellKnownSid (WellKnownSidType.AccountDomainGuestsSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-514");
CheckQualifiedWellKnownSid (WellKnownSidType.AccountComputersSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-515");
CheckQualifiedWellKnownSid (WellKnownSidType.AccountControllersSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-516");
CheckQualifiedWellKnownSid (WellKnownSidType.AccountCertAdminsSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-517");
CheckQualifiedWellKnownSid (WellKnownSidType.AccountSchemaAdminsSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-518");
CheckQualifiedWellKnownSid (WellKnownSidType.AccountEnterpriseAdminsSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-519");
CheckQualifiedWellKnownSid (WellKnownSidType.AccountPolicyAdminsSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-520");
CheckQualifiedWellKnownSid (WellKnownSidType.AccountRasAndIasServersSid, new SecurityIdentifier ("S-1-5-21-125-3215-342"), "S-1-5-21-125-3215-342-553");
CheckUnqualifiedWellKnownSid (WellKnownSidType.NtlmAuthenticationSid, "S-1-5-64-10");
CheckUnqualifiedWellKnownSid (WellKnownSidType.DigestAuthenticationSid, "S-1-5-64-21");
CheckUnqualifiedWellKnownSid (WellKnownSidType.SChannelAuthenticationSid, "S-1-5-64-14");
CheckUnqualifiedWellKnownSid (WellKnownSidType.ThisOrganizationSid, "S-1-5-15");
CheckUnqualifiedWellKnownSid (WellKnownSidType.OtherOrganizationSid, "S-1-5-1000");
CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinIncomingForestTrustBuildersSid, "S-1-5-32-557");
CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinPerformanceMonitoringUsersSid, "S-1-5-32-558");
CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinPerformanceLoggingUsersSid, "S-1-5-32-559");
CheckUnqualifiedWellKnownSid (WellKnownSidType.BuiltinAuthorizationAccessSid, "S-1-5-32-560");
CheckUnqualifiedWellKnownSid (WellKnownSidType.WinBuiltinTerminalServerLicenseServersSid, "S-1-5-32-561");
CheckUnqualifiedWellKnownSid (WellKnownSidType.MaxDefined, "S-1-5-32-561");
}
[Test]
[ExpectedException(typeof(ArgumentException))]
public void ConstructorWellKnownSidLogonIds ()
{
CheckQualifiedWellKnownSid (WellKnownSidType.LogonIdsSid,
new SecurityIdentifier ("S-1-5-21-125-3215-342"),
"S-1-5-21-125-3215-342-3");
}
[Test]
public void AccountDomainSid ()
{
Assert.AreEqual ("S-1-5-21-125-3215-342", new SecurityIdentifier ("S-1-5-21-125-3215-342-324-1000").AccountDomainSid.Value);
Assert.AreEqual ("S-1-5-21-125-3215-342", new SecurityIdentifier ("S-1-5-21-125-3215-342-1000").AccountDomainSid.Value);
Assert.AreEqual ("S-1-5-21-125-3215-1", new SecurityIdentifier ("S-1-5-21-125-3215-1").AccountDomainSid.Value);
Assert.IsNull (new SecurityIdentifier ("S-1-5-21-125-1").AccountDomainSid);
Assert.IsNull (new SecurityIdentifier ("S-1-0-0").AccountDomainSid);
Assert.IsNull (new SecurityIdentifier ("S-1-5-44-125-3215-1").AccountDomainSid);
}
[Test]
public void BinaryLength ()
{
Assert.AreEqual (12, new SecurityIdentifier ("S-1-0-0").BinaryLength);
}
[Test]
public void Value ()
{
Assert.AreEqual ("S-1-5-13362", new SecurityIdentifier ("s-1-5-0x3432").Value);
}
[Test]
public void Equals ()
{
Assert.IsTrue (new SecurityIdentifier ("S-1-5-13362").Equals (new SecurityIdentifier ("s-1-5-0x3432")));
}
[Test]
public void IsAccountSid ()
{
Assert.IsTrue (new SecurityIdentifier ("S-1-5-21-125-3215-342-324-1000").IsAccountSid ());
Assert.IsTrue (new SecurityIdentifier ("S-1-5-21-125-3215-342-1000").IsAccountSid ());
Assert.IsTrue (new SecurityIdentifier ("S-1-5-21-125-3215-1").IsAccountSid ());
Assert.IsFalse (new SecurityIdentifier ("S-1-5-21-125-1").IsAccountSid ());
Assert.IsFalse (new SecurityIdentifier ("S-1-0-0").IsAccountSid ());
}
[Test]
public void IsEqualDomainSid ()
{
Assert.IsTrue (new SecurityIdentifier ("S-1-5-21-125-3215-342-1000").IsEqualDomainSid (new SecurityIdentifier ("S-1-5-21-125-3215-342-333")));
Assert.IsTrue (new SecurityIdentifier ("S-1-5-21-125-3215-342-1000").IsEqualDomainSid (new SecurityIdentifier ("S-1-5-21-125-3215-342-324-333")));
Assert.IsFalse (new SecurityIdentifier ("S-1-5-21-125-1").IsEqualDomainSid (new SecurityIdentifier ("S-1-5-21-125-2")));
Assert.IsFalse (new SecurityIdentifier ("S-1-0-0").IsEqualDomainSid (new SecurityIdentifier ("S-1-0-0")));
}
[Test]
public void IsValidTargetType ()
{
Assert.IsTrue (new SecurityIdentifier ("S-1-0-0").IsValidTargetType (typeof(SecurityIdentifier)));
Assert.IsTrue (new SecurityIdentifier ("S-1-0-0").IsValidTargetType (typeof(NTAccount)));
Assert.IsFalse (new SecurityIdentifier ("S-1-0-0").IsValidTargetType (typeof(WindowsPrincipal)));
Assert.IsFalse (new SecurityIdentifier ("S-1-0-0").IsValidTargetType (typeof(WindowsIdentity)));
}
[Test]
public void IsWellKnown ()
{
Assert.IsTrue (new SecurityIdentifier ("S-1-0-0").IsWellKnown (WellKnownSidType.NullSid));
Assert.IsTrue (new SecurityIdentifier ("S-1-5-21-125-3215-342-500").IsWellKnown (WellKnownSidType.AccountAdministratorSid));
Assert.IsTrue (new SecurityIdentifier ("S-1-5-21-125-3215-342-513").IsWellKnown (WellKnownSidType.AccountDomainUsersSid));
Assert.IsFalse (new SecurityIdentifier ("S-1-6-21-125-3215-342-513").IsWellKnown (WellKnownSidType.AccountDomainUsersSid));
Assert.IsFalse (new SecurityIdentifier ("S-1-5-22-125-3215-342-513").IsWellKnown (WellKnownSidType.AccountDomainUsersSid));
}
[Test]
public void Translate ()
{
CheckWellKnownSidLookup (WellKnownSidType.NullSid, @"NULL SID");
CheckWellKnownSidLookup (WellKnownSidType.WorldSid, @"Everyone");
CheckWellKnownSidLookup (WellKnownSidType.LocalSid, @"LOCAL");
CheckWellKnownSidLookup (WellKnownSidType.CreatorOwnerSid, @"CREATOR OWNER");
CheckWellKnownSidLookup (WellKnownSidType.CreatorGroupSid, @"CREATOR GROUP");
CheckWellKnownSidLookup (WellKnownSidType.CreatorOwnerServerSid, @"CREATOR OWNER SERVER");
CheckWellKnownSidLookup (WellKnownSidType.CreatorGroupServerSid, @"CREATOR GROUP SERVER");
CheckWellKnownSidLookup (WellKnownSidType.DialupSid, @"NT AUTHORITY\DIALUP");
CheckWellKnownSidLookup (WellKnownSidType.NetworkSid, @"NT AUTHORITY\NETWORK");
CheckWellKnownSidLookup (WellKnownSidType.BatchSid, @"NT AUTHORITY\BATCH");
CheckWellKnownSidLookup (WellKnownSidType.InteractiveSid, @"NT AUTHORITY\INTERACTIVE");
CheckWellKnownSidLookup (WellKnownSidType.ServiceSid, @"NT AUTHORITY\SERVICE");
CheckWellKnownSidLookup (WellKnownSidType.AnonymousSid, @"NT AUTHORITY\ANONYMOUS LOGON");
CheckWellKnownSidLookup (WellKnownSidType.ProxySid, @"NT AUTHORITY\PROXY");
CheckWellKnownSidLookup (WellKnownSidType.EnterpriseControllersSid, @"NT AUTHORITY\ENTERPRISE DOMAIN CONTROLLERS");
CheckWellKnownSidLookup (WellKnownSidType.SelfSid, @"NT AUTHORITY\SELF");
CheckWellKnownSidLookup (WellKnownSidType.AuthenticatedUserSid, @"NT AUTHORITY\Authenticated Users");
CheckWellKnownSidLookup (WellKnownSidType.RestrictedCodeSid, @"NT AUTHORITY\RESTRICTED");
CheckWellKnownSidLookup (WellKnownSidType.TerminalServerSid, @"NT AUTHORITY\TERMINAL SERVER USER");
CheckWellKnownSidLookup (WellKnownSidType.RemoteLogonIdSid, @"NT AUTHORITY\REMOTE INTERACTIVE LOGON");
CheckWellKnownSidLookup (WellKnownSidType.LocalSystemSid, @"NT AUTHORITY\SYSTEM");
CheckWellKnownSidLookup (WellKnownSidType.LocalServiceSid, @"NT AUTHORITY\LOCAL SERVICE");
CheckWellKnownSidLookup (WellKnownSidType.NetworkServiceSid, @"NT AUTHORITY\NETWORK SERVICE");
CheckWellKnownSidLookup (WellKnownSidType.BuiltinAdministratorsSid, @"BUILTIN\Administrators");
CheckWellKnownSidLookup (WellKnownSidType.BuiltinUsersSid, @"BUILTIN\Users");
CheckWellKnownSidLookup (WellKnownSidType.BuiltinGuestsSid, @"BUILTIN\Guests");
CheckWellKnownSidLookup (WellKnownSidType.NtlmAuthenticationSid, @"NT AUTHORITY\NTLM Authentication");
CheckWellKnownSidLookup (WellKnownSidType.DigestAuthenticationSid, @"NT AUTHORITY\Digest Authentication");
CheckWellKnownSidLookup (WellKnownSidType.SChannelAuthenticationSid, @"NT AUTHORITY\SChannel Authentication");
CheckWellKnownSidLookup (WellKnownSidType.ThisOrganizationSid, @"NT AUTHORITY\This Organization");
CheckWellKnownSidLookup (WellKnownSidType.OtherOrganizationSid, @"NT AUTHORITY\Other Organization");
CheckWellKnownSidLookup (WellKnownSidType.BuiltinPerformanceMonitoringUsersSid, @"BUILTIN\Performance Monitor Users");
CheckWellKnownSidLookup (WellKnownSidType.BuiltinPerformanceLoggingUsersSid, @"BUILTIN\Performance Log Users");
}
[Test]
[ExpectedException(typeof(IdentityNotMappedException))]
public void TranslateUnknown ()
{
new SecurityIdentifier ("S-1-5-21-125-3215-342-513").Translate (typeof(NTAccount));
}
[Test]
public void LengthLimits ()
{
Assert.AreEqual (8, SecurityIdentifier.MinBinaryLength);
Assert.AreEqual (68, SecurityIdentifier.MaxBinaryLength);
}
[Test]
public void CompareOrdering ()
{
SecurityIdentifier[] sids = new SecurityIdentifier[] {
new SecurityIdentifier ("S-1-5-32-544"),
new SecurityIdentifier ("S-1-5-40"),
new SecurityIdentifier ("S-1-5-32-5432"),
new SecurityIdentifier ("S-1-6-0"),
new SecurityIdentifier ("S-1-5-32-99"),
new SecurityIdentifier ("S-1-0-2")
};
SecurityIdentifier[] sortedSids = (SecurityIdentifier[])sids.Clone ();
Array.Sort (sortedSids);
Assert.AreSame (sids [5], sortedSids [0]);
Assert.AreSame (sids [1], sortedSids [1]);
Assert.AreSame (sids [4], sortedSids [2]);
Assert.AreSame (sids [0], sortedSids [3]);
Assert.AreSame (sids [2], sortedSids [4]);
Assert.AreSame (sids [3], sortedSids [5]);
}
[Test, ExpectedExceptionAttribute (typeof (ArgumentNullException))]
public void CompareToNull ()
{
SecurityIdentifier sid = new SecurityIdentifier (WellKnownSidType.BuiltinUsersSid, null);
sid.CompareTo ((SecurityIdentifier)null);
}
[Test]
public void EqualsNull ()
{
SecurityIdentifier sid = new SecurityIdentifier (WellKnownSidType.BuiltinUsersSid, null);
Assert.IsFalse (sid.Equals ((object)null));
Assert.IsFalse (sid.Equals ((SecurityIdentifier)null));
}
[Test]
public unsafe void IntPtrRoundtrip ()
{
SecurityIdentifier sidIn, sidOut;
byte[] binaryFormIn, binaryFormOut;
sidIn = new SecurityIdentifier ("WD");
binaryFormIn = new byte[sidIn.BinaryLength];
sidIn.GetBinaryForm (binaryFormIn, 0);
fixed (byte* pointerForm = binaryFormIn)
sidOut = new SecurityIdentifier ((IntPtr)pointerForm);
binaryFormOut = new byte[sidOut.BinaryLength];
sidOut.GetBinaryForm (binaryFormOut, 0);
Assert.AreEqual (sidIn, sidOut);
Assert.AreEqual (binaryFormIn, binaryFormOut);
}
}
}

View File

@@ -0,0 +1,233 @@
//
// WindowsIdentityTest.cs - NUnit Test Cases for WindowsIdentity
//
// Author:
// Sebastien Pouliot (sebastien@ximian.com)
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
//
using NUnit.Framework;
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Principal;
namespace MonoTests.System.Security.Principal {
[TestFixture]
public class WindowsIdentityTest {
private bool IsPosix {
get {
// check for Unix platforms - see FAQ for more details
// http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
int platform = (int) Environment.OSVersion.Platform;
return ((platform == 4) || (platform == 128) || (platform == 6));
}
}
// some features works only in Windows 2003 and later
private bool IsWin2k3orLater {
get {
// requires both a W2K3 client and server (domain)
// which I don't have access to debug/support
OperatingSystem os = Environment.OSVersion;
if (os.Platform != PlatformID.Win32NT)
return false;
if (os.Version.Major > 5) {
return false;
}
else if (os.Version.Major == 5) {
return (os.Version.Minor > 1);
}
return false;
}
}
[Test]
public void ConstructorIntPtrZero ()
{
// should fail on Windows (invalid token)
// should not fail on Posix (root uid)
try {
WindowsIdentity id = new WindowsIdentity (IntPtr.Zero);
if (!IsPosix)
Assert.Fail ("Expected ArgumentException on Windows platforms");
}
catch (ArgumentException) {
if (IsPosix)
throw;
}
}
#if !NET_1_0
[Test]
//[ExpectedException (typeof (ArgumentNullException))]
[ExpectedException (typeof (NullReferenceException))]
public void ConstructorW2KS1_Null ()
{
WindowsIdentity id = new WindowsIdentity (null);
}
[Test]
public void ConstructorW2KS1 ()
{
WindowsIdentity wi = WindowsIdentity.GetCurrent ();
// should fail with ArgumentException unless
// - running Windows 2003 or later (both client and domain server)
// - running Posix
try {
WindowsIdentity id = new WindowsIdentity (wi.Name);
/*if (!IsWin2k3orLater && !IsPosix)
Assert.Fail ("Expected ArgumentException but got none");*/
}
catch (ArgumentException) {
if (/*IsWin2k3orLater ||*/ IsPosix)
throw;
}
}
[Test]
//[ExpectedException (typeof (ArgumentNullException))]
[ExpectedException (typeof (NullReferenceException))]
public void ConstructorW2KS2_UserNull ()
{
WindowsIdentity id = new WindowsIdentity (null, "NTLM");
}
[Test]
public void ConstructorW2KS2_TypeNull()
{
WindowsIdentity wi = WindowsIdentity.GetCurrent ();
// should fail with ArgumentException unless
// - running Windows 2003 or later (both client and domain server)
// - running Posix
try {
WindowsIdentity id = new WindowsIdentity (wi.Name, null);
/*if (!IsWin2k3orLater && !IsPosix)
Assert.Fail ("Expected ArgumentException but got none");*/
}
catch (ArgumentException) {
if (/*IsWin2k3orLater ||*/ IsPosix)
throw;
}
}
[Test]
public void ConstructorW2KS2 ()
{
WindowsIdentity wi = WindowsIdentity.GetCurrent ();
// should fail with ArgumentException unless
// - running Windows 2003 or later (both client and domain server)
// - running Posix
try {
WindowsIdentity id = new WindowsIdentity (wi.Name, wi.AuthenticationType);
/*if (!IsWin2k3orLater && !IsPosix)
Assert.Fail ("Expected ArgumentException but got none");*/
}
catch (ArgumentException) {
if (/*IsWin2k3orLater ||*/ IsPosix)
throw;
}
}
#endif
[Test]
public void Anonymous ()
{
WindowsIdentity id = WindowsIdentity.GetAnonymous ();
Assert.AreEqual (String.Empty, id.AuthenticationType, "AuthenticationType");
Assert.IsTrue (id.IsAnonymous, "IsAnonymous");
Assert.IsTrue (!id.IsAuthenticated, "IsAuthenticated");
Assert.IsTrue (!id.IsGuest, "IsGuest");
Assert.IsTrue (!id.IsSystem, "IsSystem");
if (IsPosix) {
Assert.IsTrue ((IntPtr.Zero != id.Token), "Token");
Assert.IsNotNull (id.Name, "Name");
}
else {
Assert.AreEqual (IntPtr.Zero, id.Token, "Token");
Assert.AreEqual (String.Empty, id.Name, "Name");
}
}
[Test]
public void Current ()
{
WindowsIdentity id = WindowsIdentity.GetCurrent ();
Assert.IsNotNull (id.AuthenticationType, "AuthenticationType");
Assert.IsTrue (!id.IsAnonymous, "IsAnonymous");
Assert.IsTrue (id.IsAuthenticated, "IsAuthenticated");
Assert.IsTrue (!id.IsGuest, "IsGuest");
// root is 0 - so IntPtr.Zero is valid on Linux (but not on Windows)
Assert.IsTrue ((!id.IsSystem || (id.Token == IntPtr.Zero)), "IsSystem");
if (!IsPosix) {
Assert.IsTrue ((id.Token != IntPtr.Zero), "Token");
}
Assert.IsNotNull (id.Name, "Name");
}
[Test]
public void Interfaces ()
{
WindowsIdentity id = WindowsIdentity.GetAnonymous ();
IIdentity i = (id as IIdentity);
Assert.IsNotNull (i, "IIdentity");
IDeserializationCallback dc = (id as IDeserializationCallback);
Assert.IsNotNull (dc, "IDeserializationCallback");
ISerializable s = (id as ISerializable);
Assert.IsNotNull (s, "ISerializable");
}
// This is clearly a hack - but I've seen it too many times so I think we
// better support it too :(
// http://dotnetjunkies.com/WebLog/chris.taylor/archive/2004/02/25/7945.aspx
public string[] GetWindowsIdentityRoles (WindowsIdentity identity)
{
object result = typeof(WindowsIdentity).InvokeMember ("_GetRoles",
BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.NonPublic,
null, identity, new object[] {identity.Token}, null);
return (string[]) result;
}
[Test]
public void GetRolesViaReflection ()
{
// remove g_warning from being show during unit tests
if (IsPosix)
Assert.Ignore ("Running on Unix.");
WindowsIdentity wi = WindowsIdentity.GetCurrent ();
WindowsPrincipal wp = new WindowsPrincipal (wi);
string[] roles = GetWindowsIdentityRoles (wi);
foreach (string role in roles) {
// somehow I got a null in there ?
if (role != null)
Assert.IsTrue (wp.IsInRole (role), role);
}
}
[Test]
public void SerializeRoundTrip ()
{
WindowsIdentity wi = WindowsIdentity.GetCurrent ();
MemoryStream ms = new MemoryStream ();
IFormatter formatter = new BinaryFormatter ();
formatter.Serialize (ms, wi);
ms.Position = 0;
WindowsIdentity back = (WindowsIdentity) formatter.Deserialize (ms);
Assert.AreEqual (wi.AuthenticationType, back.AuthenticationType, "AuthenticationType");
Assert.AreEqual (wi.IsAnonymous, back.IsAnonymous, "IsAnonymous");
Assert.AreEqual (wi.IsAuthenticated, back.IsAuthenticated, "IsAuthenticated");
Assert.AreEqual (wi.IsGuest, back.IsGuest, "IsGuest");
Assert.AreEqual (wi.IsSystem, back.IsSystem, "IsSystem");
Assert.AreEqual (wi.Name, back.Name, "Name");
// note: token may be different (no compare)
}
}
}

View File

@@ -0,0 +1,83 @@
//
// WindowsPrincipalTest.cs - NUnit Test Cases for WindowsPrincipal
//
// Author:
// Sebastien Pouliot (sebastien@ximian.com)
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// (C) 2004 Novell (http://www.novell.com)
//
using NUnit.Framework;
using System;
using System.Runtime.Serialization;
using System.Security.Principal;
namespace MonoTests.System.Security.Principal {
[TestFixture]
public class WindowsPrincipalTest {
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorNull ()
{
WindowsPrincipal p = new WindowsPrincipal (null);
}
[Test]
public void Current ()
{
WindowsPrincipal p = new WindowsPrincipal (WindowsIdentity.GetCurrent ());
bool test;
// we don't Assert as we don't know the current user roles
test = p.IsInRole (WindowsBuiltInRole.Administrator);
test = p.IsInRole (WindowsBuiltInRole.BackupOperator);
test = p.IsInRole (WindowsBuiltInRole.Guest);
test = p.IsInRole (WindowsBuiltInRole.PowerUser);
test = p.IsInRole (WindowsBuiltInRole.Replicator);
test = p.IsInRole (WindowsBuiltInRole.User);
// doesn't work under XP in a workgroup (ArgumentException)
// test = p.IsInRole (WindowsBuiltInRole.AccountOperator);
// test = p.IsInRole (WindowsBuiltInRole.PrintOperator);
// test = p.IsInRole (WindowsBuiltInRole.SystemOperator);
}
[Test]
public void Anonymous ()
{
WindowsPrincipal p = new WindowsPrincipal (WindowsIdentity.GetAnonymous ());
Assert.IsFalse (p.IsInRole (WindowsBuiltInRole.Administrator), "Administrator");
Assert.IsFalse (p.IsInRole (WindowsBuiltInRole.BackupOperator), "BackupOperator");
Assert.IsFalse (p.IsInRole (WindowsBuiltInRole.Guest), "Guest");
Assert.IsFalse (p.IsInRole (WindowsBuiltInRole.PowerUser), "PowerUser");
Assert.IsFalse (p.IsInRole (WindowsBuiltInRole.Replicator), "Replicator");
Assert.IsFalse (p.IsInRole (WindowsBuiltInRole.User), "User");
// doesn't work under XP in a workgroup (ArgumentException)
// Assert ("AccountOperator", !p.IsInRole (WindowsBuiltInRole.AccountOperator));
// Assert ("PrintOperator", !p.IsInRole (WindowsBuiltInRole.PrintOperator));
// Assert ("SystemOperator", !p.IsInRole (WindowsBuiltInRole.SystemOperator));
}
[Test]
//[ExpectedException (typeof (ArgumentNullException))]
public void IsInRole_Null ()
{
WindowsPrincipal p = new WindowsPrincipal (WindowsIdentity.GetAnonymous ());
Assert.IsFalse (p.IsInRole ((string)null));
}
[Test]
public void Interface ()
{
WindowsPrincipal wp = new WindowsPrincipal (WindowsIdentity.GetAnonymous ());
IPrincipal p = (wp as IPrincipal);
Assert.IsNotNull (p);
}
}
}