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 // Copyright (C) 2004-2005 Novell, Inc (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
{
24 private bool IsPosix
{
26 // check for Unix platforms - see FAQ for more details
27 // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
28 int platform
= (int) Environment
.OSVersion
.Platform
;
29 return ((platform
== 4) || (platform
== 128) || (platform
== 6));
33 // some features works only in Windows 2003 and later
34 private bool IsWin2k3orLater
{
36 // requires both a W2K3 client and server (domain)
37 // which I don't have access to debug/support
38 OperatingSystem os
= Environment
.OSVersion
;
39 if (os
.Platform
!= PlatformID
.Win32NT
)
42 if (os
.Version
.Major
> 5) {
45 else if (os
.Version
.Major
== 5) {
46 return (os
.Version
.Minor
> 1);
53 public void ConstructorIntPtrZero ()
55 // should fail on Windows (invalid token)
56 // should not fail on Posix (root uid)
58 WindowsIdentity id
= new WindowsIdentity (IntPtr
.Zero
);
60 Assert
.Fail ("Expected ArgumentException on Windows platforms");
62 catch (ArgumentException
) {
69 //[ExpectedException (typeof (ArgumentNullException))]
70 [ExpectedException (typeof (NullReferenceException
))]
71 public void ConstructorW2KS1_Null ()
73 WindowsIdentity id
= new WindowsIdentity (null);
77 public void ConstructorW2KS1 ()
79 WindowsIdentity wi
= WindowsIdentity
.GetCurrent ();
80 // should fail with ArgumentException unless
81 // - running Windows 2003 or later (both client and domain server)
84 WindowsIdentity id
= new WindowsIdentity (wi
.Name
);
85 /*if (!IsWin2k3orLater && !IsPosix)
86 Assert.Fail ("Expected ArgumentException but got none");*/
88 catch (ArgumentException
) {
89 if (/*IsWin2k3orLater ||*/ IsPosix
)
95 //[ExpectedException (typeof (ArgumentNullException))]
96 [ExpectedException (typeof (NullReferenceException
))]
97 public void ConstructorW2KS2_UserNull ()
99 WindowsIdentity id
= new WindowsIdentity (null, "NTLM");
103 public void ConstructorW2KS2_TypeNull()
105 WindowsIdentity wi
= WindowsIdentity
.GetCurrent ();
106 // should fail with ArgumentException unless
107 // - running Windows 2003 or later (both client and domain server)
110 WindowsIdentity id
= new WindowsIdentity (wi
.Name
, null);
111 /*if (!IsWin2k3orLater && !IsPosix)
112 Assert.Fail ("Expected ArgumentException but got none");*/
114 catch (ArgumentException
) {
115 if (/*IsWin2k3orLater ||*/ IsPosix
)
121 public void ConstructorW2KS2 ()
123 WindowsIdentity wi
= WindowsIdentity
.GetCurrent ();
124 // should fail with ArgumentException unless
125 // - running Windows 2003 or later (both client and domain server)
128 WindowsIdentity id
= new WindowsIdentity (wi
.Name
, wi
.AuthenticationType
);
129 /*if (!IsWin2k3orLater && !IsPosix)
130 Assert.Fail ("Expected ArgumentException but got none");*/
132 catch (ArgumentException
) {
133 if (/*IsWin2k3orLater ||*/ IsPosix
)
139 public void Anonymous ()
141 WindowsIdentity id
= WindowsIdentity
.GetAnonymous ();
142 Assert
.AreEqual (String
.Empty
, id
.AuthenticationType
, "AuthenticationType");
143 Assert
.IsTrue (id
.IsAnonymous
, "IsAnonymous");
144 Assert
.IsTrue (!id
.IsAuthenticated
, "IsAuthenticated");
145 Assert
.IsTrue (!id
.IsGuest
, "IsGuest");
146 Assert
.IsTrue (!id
.IsSystem
, "IsSystem");
148 Assert
.IsTrue ((IntPtr
.Zero
!= id
.Token
), "Token");
149 Assert
.IsNotNull (id
.Name
, "Name");
152 Assert
.AreEqual (IntPtr
.Zero
, id
.Token
, "Token");
153 Assert
.AreEqual (String
.Empty
, id
.Name
, "Name");
158 public void Current ()
160 WindowsIdentity id
= WindowsIdentity
.GetCurrent ();
161 Assert
.IsNotNull (id
.AuthenticationType
, "AuthenticationType");
162 Assert
.IsTrue (!id
.IsAnonymous
, "IsAnonymous");
163 Assert
.IsTrue (id
.IsAuthenticated
, "IsAuthenticated");
164 Assert
.IsTrue (!id
.IsGuest
, "IsGuest");
165 // root is 0 - so IntPtr.Zero is valid on Linux (but not on Windows)
166 Assert
.IsTrue ((!id
.IsSystem
|| (id
.Token
== IntPtr
.Zero
)), "IsSystem");
168 Assert
.IsTrue ((id
.Token
!= IntPtr
.Zero
), "Token");
170 Assert
.IsNotNull (id
.Name
, "Name");
174 public void Interfaces ()
176 WindowsIdentity id
= WindowsIdentity
.GetAnonymous ();
178 IIdentity i
= (id
as IIdentity
);
179 Assert
.IsNotNull (i
, "IIdentity");
181 IDeserializationCallback dc
= (id
as IDeserializationCallback
);
182 Assert
.IsNotNull (dc
, "IDeserializationCallback");
183 ISerializable s
= (id
as ISerializable
);
184 Assert
.IsNotNull (s
, "ISerializable");
187 // This is clearly a hack - but I've seen it too many times so I think we
188 // better support it too :(
189 // http://dotnetjunkies.com/WebLog/chris.taylor/archive/2004/02/25/7945.aspx
190 public string[] GetWindowsIdentityRoles (WindowsIdentity identity
)
192 object result
= typeof(WindowsIdentity
).InvokeMember ("_GetRoles",
193 BindingFlags
.Static
| BindingFlags
.InvokeMethod
| BindingFlags
.NonPublic
,
194 null, identity
, new object[] {identity.Token}
, null);
195 return (string[]) result
;
199 public void GetRolesViaReflection ()
201 // remove g_warning from being show during unit tests
203 Assert
.Ignore ("Running on Unix.");
205 WindowsIdentity wi
= WindowsIdentity
.GetCurrent ();
206 WindowsPrincipal wp
= new WindowsPrincipal (wi
);
207 string[] roles
= GetWindowsIdentityRoles (wi
);
208 foreach (string role
in roles
) {
209 // somehow I got a null in there ?
211 Assert
.IsTrue (wp
.IsInRole (role
), role
);
216 public void SerializeRoundTrip ()
218 WindowsIdentity wi
= WindowsIdentity
.GetCurrent ();
219 MemoryStream ms
= new MemoryStream ();
220 IFormatter formatter
= new BinaryFormatter ();
221 formatter
.Serialize (ms
, wi
);
223 WindowsIdentity back
= (WindowsIdentity
) formatter
.Deserialize (ms
);
224 Assert
.AreEqual (wi
.AuthenticationType
, back
.AuthenticationType
, "AuthenticationType");
225 Assert
.AreEqual (wi
.IsAnonymous
, back
.IsAnonymous
, "IsAnonymous");
226 Assert
.AreEqual (wi
.IsAuthenticated
, back
.IsAuthenticated
, "IsAuthenticated");
227 Assert
.AreEqual (wi
.IsGuest
, back
.IsGuest
, "IsGuest");
228 Assert
.AreEqual (wi
.IsSystem
, back
.IsSystem
, "IsSystem");
229 Assert
.AreEqual (wi
.Name
, back
.Name
, "Name");
230 // note: token may be different (no compare)