2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / Test / System.Security.Principal / WindowsIdentityTest.cs
index c436a611d73960d1de9542af1aad6fb4005e36c7..c7a85f78e5d5e1e0eea3fee4f08f233d69bc7b2b 100755 (executable)
 
 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 {
@@ -25,6 +28,8 @@ namespace MonoTests.System.Security.Principal {
                // 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;
@@ -68,15 +73,15 @@ namespace MonoTests.System.Security.Principal {
                {
                        WindowsIdentity wi = WindowsIdentity.GetCurrent ();
                        // should fail with ArgumentException unless
-                       // - running Windows 2003 or later
+                       // - 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");
+                               /*if (!IsWin2k3orLater && !IsPosix)
+                                       Fail ("Expected ArgumentException but got none");*/
                        }
                        catch (ArgumentException) {
-                               if (IsWin2k3orLater || IsPosix)
+                               if (/*IsWin2k3orLater ||*/ IsPosix)
                                        throw;
                        }
                }
@@ -94,15 +99,15 @@ namespace MonoTests.System.Security.Principal {
                {
                        WindowsIdentity wi = WindowsIdentity.GetCurrent ();
                        // should fail with ArgumentException unless
-                       // - running Windows 2003 or later
+                       // - 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");
+                               /*if (!IsWin2k3orLater && !IsPosix)
+                                       Fail ("Expected ArgumentException but got none");*/
                        }
                        catch (ArgumentException) {
-                               if (IsWin2k3orLater || IsPosix)
+                               if (/*IsWin2k3orLater ||*/ IsPosix)
                                        throw;
                        }
                }
@@ -112,15 +117,15 @@ namespace MonoTests.System.Security.Principal {
                {
                        WindowsIdentity wi = WindowsIdentity.GetCurrent ();
                        // should fail with ArgumentException unless
-                       // - running Windows 2003 or later
+                       // - 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");
+                               /*if (!IsWin2k3orLater && !IsPosix)
+                                       Fail ("Expected ArgumentException but got none");*/
                        }
                        catch (ArgumentException) {
-                               if (IsWin2k3orLater || IsPosix)
+                               if (/*IsWin2k3orLater ||*/ IsPosix)
                                        throw;
                        }
                }
@@ -175,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)
+               }
        }
 }