X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2Fcorlib%2FTest%2FSystem.Security.Principal%2FWindowsIdentityTest.cs;h=c7a85f78e5d5e1e0eea3fee4f08f233d69bc7b2b;hb=c39145af2464b19374fac41b252e07480ae1a197;hp=34507141fced618cfc01a6d97595b22afc288342;hpb=ef13b3da617880eb7089d7d6e8ea1e9b87480c30;p=mono.git diff --git a/mcs/class/corlib/Test/System.Security.Principal/WindowsIdentityTest.cs b/mcs/class/corlib/Test/System.Security.Principal/WindowsIdentityTest.cs index 34507141fce..c7a85f78e5d 100755 --- a/mcs/class/corlib/Test/System.Security.Principal/WindowsIdentityTest.cs +++ b/mcs/class/corlib/Test/System.Security.Principal/WindowsIdentityTest.cs @@ -2,14 +2,18 @@ // WindowsIdentityTest.cs - NUnit Test Cases for WindowsIdentity // // Author: -// Sebastien Pouliot (spouliot@motus.com) +// 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.IO; +using System.Reflection; using System.Runtime.Serialization; +using System.Runtime.Serialization.Formatters.Binary; using System.Security.Principal; namespace MonoTests.System.Security.Principal { @@ -17,13 +21,45 @@ namespace MonoTests.System.Security.Principal { [TestFixture] public class WindowsIdentityTest : Assertion { + private bool IsPosix { + get { return ((int) Environment.OSVersion.Platform == 128); } + } + + // 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] - [ExpectedException (typeof (ArgumentException))] public void ConstructorIntPtrZero () { - WindowsIdentity id = new WindowsIdentity (IntPtr.Zero); + // should fail on Windows (invalid token) + // should not fail on Posix (root uid) + try { + WindowsIdentity id = new WindowsIdentity (IntPtr.Zero); + if (!IsPosix) + Fail ("Expected ArgumentException on Windows platforms"); + } + catch (ArgumentException) { + if (IsPosix) + throw; + } } - +#if !NET_1_0 [Test] //[ExpectedException (typeof (ArgumentNullException))] [ExpectedException (typeof (NullReferenceException))] @@ -33,34 +69,67 @@ namespace MonoTests.System.Security.Principal { } [Test] - [ExpectedException (typeof (ArgumentException))] public void ConstructorW2KS1 () { - WindowsIdentity id = new WindowsIdentity (@"FARSCAPE\spouliot"); + 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) + Fail ("Expected ArgumentException but got none");*/ + } + catch (ArgumentException) { + if (/*IsWin2k3orLater ||*/ IsPosix) + throw; + } } [Test] //[ExpectedException (typeof (ArgumentNullException))] [ExpectedException (typeof (NullReferenceException))] - public void ConstructorW2KS2_NullType () + public void ConstructorW2KS2_UserNull () { WindowsIdentity id = new WindowsIdentity (null, "NTLM"); } [Test] - [ExpectedException (typeof (ArgumentException))] - public void ConstructorW2KS2_UserNull() + public void ConstructorW2KS2_TypeNull() { - WindowsIdentity id = new WindowsIdentity (@"FARSCAPE\spouliot", null); + 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) + Fail ("Expected ArgumentException but got none");*/ + } + catch (ArgumentException) { + if (/*IsWin2k3orLater ||*/ IsPosix) + throw; + } } [Test] - [ExpectedException (typeof (ArgumentException))] public void ConstructorW2KS2 () { - WindowsIdentity id = new WindowsIdentity (@"FARSCAPE\spouliot", "NTLM"); + 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) + Fail ("Expected ArgumentException but got none");*/ + } + catch (ArgumentException) { + if (/*IsWin2k3orLater ||*/ IsPosix) + throw; + } } - +#endif [Test] public void Anonymous () { @@ -70,27 +139,34 @@ namespace MonoTests.System.Security.Principal { Assert ("IsAuthenticated", !id.IsAuthenticated); Assert ("IsGuest", !id.IsGuest); Assert ("IsSystem", !id.IsSystem); - AssertEquals ("Token", IntPtr.Zero, id.Token); - AssertEquals ("Name", String.Empty, id.Name); + if (IsPosix) { + Assert ("Token", (IntPtr.Zero != id.Token)); + AssertNotNull ("Name", id.Name); + } + else { + AssertEquals ("Token", IntPtr.Zero, id.Token); + AssertEquals ("Name", String.Empty, id.Name); + } } [Test] - [Ignore ("not currently supported on mono")] public void Current () { WindowsIdentity id = WindowsIdentity.GetCurrent (); - AssertEquals ("AuthenticationType", "NTLM", id.AuthenticationType); + AssertNotNull ("AuthenticationType", id.AuthenticationType); Assert ("IsAnonymous", !id.IsAnonymous); Assert ("IsAuthenticated", id.IsAuthenticated); Assert ("IsGuest", !id.IsGuest); - Assert ("IsSystem", !id.IsSystem); - Assert ("Token", (IntPtr.Zero != id.Token)); - // e.g. FARSCAPE\spouliot - Assert ("Name", (id.Name.IndexOf (@"\") > 0)); + // root is 0 - so IntPtr.Zero is valid on Linux (but not on Windows) + Assert ("IsSystem", (!id.IsSystem || (id.Token == IntPtr.Zero))); + if (!IsPosix) { + Assert ("Token", (id.Token != IntPtr.Zero)); + } + AssertNotNull ("Name", id.Name); } [Test] - public void Interface () + public void Interfaces () { WindowsIdentity id = WindowsIdentity.GetAnonymous (); @@ -104,5 +180,51 @@ namespace MonoTests.System.Security.Principal { AssertNotNull ("ISerializable", s); #endif } + + // 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) + return; + + 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 (role, wp.IsInRole (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); + AssertEquals ("AuthenticationType", wi.AuthenticationType, back.AuthenticationType); + AssertEquals ("IsAnonymous", wi.IsAnonymous, back.IsAnonymous); + AssertEquals ("IsAuthenticated", wi.IsAuthenticated, back.IsAuthenticated); + AssertEquals ("IsGuest", wi.IsGuest, back.IsGuest); + AssertEquals ("IsSystem", wi.IsSystem, back.IsSystem); + AssertEquals ("Name", wi.Name, back.Name); + // note: token may be different (no compare) + } } }