2004-04-14 Sebastien Pouliot <sebastien@ximian.com>
authorSebastien Pouliot <sebastien@ximian.com>
Wed, 14 Apr 2004 12:47:09 +0000 (12:47 -0000)
committerSebastien Pouliot <sebastien@ximian.com>
Wed, 14 Apr 2004 12:47:09 +0000 (12:47 -0000)
* WindowsIdentityTest.cs: Added reflection and serialization tests.

svn path=/trunk/mcs/; revision=25467

mcs/class/corlib/Test/System.Security.Principal/ChangeLog
mcs/class/corlib/Test/System.Security.Principal/WindowsIdentityTest.cs

index 6879b53483d34d88043cf6c3135145681b069c37..737ebf3bacaa56f70e5f46421defbba4bddef2df 100644 (file)
@@ -1,3 +1,7 @@
+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
index c436a611d73960d1de9542af1aad6fb4005e36c7..3aef9fe639fa2ab2bc8c9a7bc262b9403ff4f1ab 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 {
@@ -175,5 +178,47 @@ 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 () 
+               {
+                       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)
+               }
        }
 }