2 // WindowsIdentityTest.cs - NUnit Test Cases for WindowsIdentity
5 // Sebastien Pouliot (sebastien@ximian.com)
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
11 using NUnit
.Framework
;
14 using System
.Reflection
;
15 using System
.Runtime
.Serialization
;
16 using System
.Runtime
.Serialization
.Formatters
.Binary
;
17 using System
.Security
.Principal
;
19 namespace MonoTests
.System
.Security
.Principal
{
22 public class WindowsIdentityTest
: Assertion
{
24 private bool IsPosix
{
25 get { return ((int) Environment.OSVersion.Platform == 128); }
28 // some features works only in Windows 2003 and later
29 private bool IsWin2k3orLater
{
31 // requires both a W2K3 client and server (domain)
32 // which I don't have access to debug/support
33 OperatingSystem os
= Environment
.OSVersion
;
34 if (os
.Platform
!= PlatformID
.Win32NT
)
37 if (os
.Version
.Major
> 5) {
40 else if (os
.Version
.Major
== 5) {
41 return (os
.Version
.Minor
> 1);
48 public void ConstructorIntPtrZero ()
50 // should fail on Windows (invalid token)
51 // should not fail on Posix (root uid)
53 WindowsIdentity id
= new WindowsIdentity (IntPtr
.Zero
);
55 Fail ("Expected ArgumentException on Windows platforms");
57 catch (ArgumentException
) {
64 //[ExpectedException (typeof (ArgumentNullException))]
65 [ExpectedException (typeof (NullReferenceException
))]
66 public void ConstructorW2KS1_Null ()
68 WindowsIdentity id
= new WindowsIdentity (null);
72 public void ConstructorW2KS1 ()
74 WindowsIdentity wi
= WindowsIdentity
.GetCurrent ();
75 // should fail with ArgumentException unless
76 // - running Windows 2003 or later (both client and domain server)
79 WindowsIdentity id
= new WindowsIdentity (wi
.Name
);
80 /*if (!IsWin2k3orLater && !IsPosix)
81 Fail ("Expected ArgumentException but got none");*/
83 catch (ArgumentException
) {
84 if (/*IsWin2k3orLater ||*/ IsPosix
)
90 //[ExpectedException (typeof (ArgumentNullException))]
91 [ExpectedException (typeof (NullReferenceException
))]
92 public void ConstructorW2KS2_UserNull ()
94 WindowsIdentity id
= new WindowsIdentity (null, "NTLM");
98 public void ConstructorW2KS2_TypeNull()
100 WindowsIdentity wi
= WindowsIdentity
.GetCurrent ();
101 // should fail with ArgumentException unless
102 // - running Windows 2003 or later (both client and domain server)
105 WindowsIdentity id
= new WindowsIdentity (wi
.Name
, null);
106 /*if (!IsWin2k3orLater && !IsPosix)
107 Fail ("Expected ArgumentException but got none");*/
109 catch (ArgumentException
) {
110 if (/*IsWin2k3orLater ||*/ IsPosix
)
116 public void ConstructorW2KS2 ()
118 WindowsIdentity wi
= WindowsIdentity
.GetCurrent ();
119 // should fail with ArgumentException unless
120 // - running Windows 2003 or later (both client and domain server)
123 WindowsIdentity id
= new WindowsIdentity (wi
.Name
, wi
.AuthenticationType
);
124 /*if (!IsWin2k3orLater && !IsPosix)
125 Fail ("Expected ArgumentException but got none");*/
127 catch (ArgumentException
) {
128 if (/*IsWin2k3orLater ||*/ IsPosix
)
134 public void Anonymous ()
136 WindowsIdentity id
= WindowsIdentity
.GetAnonymous ();
137 AssertEquals ("AuthenticationType", String
.Empty
, id
.AuthenticationType
);
138 Assert ("IsAnonymous", id
.IsAnonymous
);
139 Assert ("IsAuthenticated", !id
.IsAuthenticated
);
140 Assert ("IsGuest", !id
.IsGuest
);
141 Assert ("IsSystem", !id
.IsSystem
);
143 Assert ("Token", (IntPtr
.Zero
!= id
.Token
));
144 AssertNotNull ("Name", id
.Name
);
147 AssertEquals ("Token", IntPtr
.Zero
, id
.Token
);
148 AssertEquals ("Name", String
.Empty
, id
.Name
);
153 public void Current ()
155 WindowsIdentity id
= WindowsIdentity
.GetCurrent ();
156 AssertNotNull ("AuthenticationType", id
.AuthenticationType
);
157 Assert ("IsAnonymous", !id
.IsAnonymous
);
158 Assert ("IsAuthenticated", id
.IsAuthenticated
);
159 Assert ("IsGuest", !id
.IsGuest
);
160 // root is 0 - so IntPtr.Zero is valid on Linux (but not on Windows)
161 Assert ("IsSystem", (!id
.IsSystem
|| (id
.Token
== IntPtr
.Zero
)));
163 Assert ("Token", (id
.Token
!= IntPtr
.Zero
));
165 AssertNotNull ("Name", id
.Name
);
169 public void Interfaces ()
171 WindowsIdentity id
= WindowsIdentity
.GetAnonymous ();
173 IIdentity i
= (id
as IIdentity
);
174 AssertNotNull ("IIdentity", i
);
176 IDeserializationCallback dc
= (id
as IDeserializationCallback
);
177 AssertNotNull ("IDeserializationCallback", dc
);
179 ISerializable s
= (id
as ISerializable
);
180 AssertNotNull ("ISerializable", s
);
184 // This is clearly a hack - but I've seen it too many times so I think we
185 // better support it too :(
186 // http://dotnetjunkies.com/WebLog/chris.taylor/archive/2004/02/25/7945.aspx
187 public string[] GetWindowsIdentityRoles (WindowsIdentity identity
)
189 object result
= typeof(WindowsIdentity
).InvokeMember ("_GetRoles",
190 BindingFlags
.Static
| BindingFlags
.InvokeMethod
| BindingFlags
.NonPublic
,
191 null, identity
, new object[] {identity.Token}
, null);
192 return (string[]) result
;
196 public void GetRolesViaReflection ()
198 // remove g_warning from being show during unit tests
202 WindowsIdentity wi
= WindowsIdentity
.GetCurrent ();
203 WindowsPrincipal wp
= new WindowsPrincipal (wi
);
204 string[] roles
= GetWindowsIdentityRoles (wi
);
205 foreach (string role
in roles
) {
206 // somehow I got a null in there ?
208 Assert (role
, wp
.IsInRole (role
));
213 public void SerializeRoundTrip ()
215 WindowsIdentity wi
= WindowsIdentity
.GetCurrent ();
216 MemoryStream ms
= new MemoryStream ();
217 IFormatter formatter
= new BinaryFormatter ();
218 formatter
.Serialize (ms
, wi
);
220 WindowsIdentity back
= (WindowsIdentity
) formatter
.Deserialize (ms
);
221 AssertEquals ("AuthenticationType", wi
.AuthenticationType
, back
.AuthenticationType
);
222 AssertEquals ("IsAnonymous", wi
.IsAnonymous
, back
.IsAnonymous
);
223 AssertEquals ("IsAuthenticated", wi
.IsAuthenticated
, back
.IsAuthenticated
);
224 AssertEquals ("IsGuest", wi
.IsGuest
, back
.IsGuest
);
225 AssertEquals ("IsSystem", wi
.IsSystem
, back
.IsSystem
);
226 AssertEquals ("Name", wi
.Name
, back
.Name
);
227 // note: token may be different (no compare)