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;
13 using System.Runtime.Serialization;
14 using System.Security.Principal;
16 namespace MonoTests.System.Security.Principal {
19 public class WindowsIdentityTest : Assertion {
21 private bool IsPosix {
22 get { return ((int) Environment.OSVersion.Platform == 128); }
25 // some features works only in Windows 2003 and later
26 private bool IsWin2k3orLater {
28 OperatingSystem os = Environment.OSVersion;
29 if (os.Platform != PlatformID.Win32NT)
32 if (os.Version.Major > 5) {
35 else if (os.Version.Major == 5) {
36 return (os.Version.Minor > 1);
43 public void ConstructorIntPtrZero ()
45 // should fail on Windows (invalid token)
46 // should not fail on Posix (root uid)
48 WindowsIdentity id = new WindowsIdentity (IntPtr.Zero);
50 Fail ("Expected ArgumentException on Windows platforms");
52 catch (ArgumentException) {
59 //[ExpectedException (typeof (ArgumentNullException))]
60 [ExpectedException (typeof (NullReferenceException))]
61 public void ConstructorW2KS1_Null ()
63 WindowsIdentity id = new WindowsIdentity (null);
67 public void ConstructorW2KS1 ()
69 WindowsIdentity wi = WindowsIdentity.GetCurrent ();
70 // should fail with ArgumentException unless
71 // - running Windows 2003 or later
74 WindowsIdentity id = new WindowsIdentity (wi.Name);
75 if (!IsWin2k3orLater && !IsPosix)
76 Fail ("Expected ArgumentException but got none");
78 catch (ArgumentException) {
79 if (IsWin2k3orLater || IsPosix)
85 //[ExpectedException (typeof (ArgumentNullException))]
86 [ExpectedException (typeof (NullReferenceException))]
87 public void ConstructorW2KS2_UserNull ()
89 WindowsIdentity id = new WindowsIdentity (null, "NTLM");
93 public void ConstructorW2KS2_TypeNull()
95 WindowsIdentity wi = WindowsIdentity.GetCurrent ();
96 // should fail with ArgumentException unless
97 // - running Windows 2003 or later
100 WindowsIdentity id = new WindowsIdentity (wi.Name, null);
101 if (!IsWin2k3orLater && !IsPosix)
102 Fail ("Expected ArgumentException but got none");
104 catch (ArgumentException) {
105 if (IsWin2k3orLater || IsPosix)
111 public void ConstructorW2KS2 ()
113 WindowsIdentity wi = WindowsIdentity.GetCurrent ();
114 // should fail with ArgumentException unless
115 // - running Windows 2003 or later
118 WindowsIdentity id = new WindowsIdentity (wi.Name, wi.AuthenticationType);
119 if (!IsWin2k3orLater && !IsPosix)
120 Fail ("Expected ArgumentException but got none");
122 catch (ArgumentException) {
123 if (IsWin2k3orLater || IsPosix)
129 public void Anonymous ()
131 WindowsIdentity id = WindowsIdentity.GetAnonymous ();
132 AssertEquals ("AuthenticationType", String.Empty, id.AuthenticationType);
133 Assert ("IsAnonymous", id.IsAnonymous);
134 Assert ("IsAuthenticated", !id.IsAuthenticated);
135 Assert ("IsGuest", !id.IsGuest);
136 Assert ("IsSystem", !id.IsSystem);
138 Assert ("Token", (IntPtr.Zero != id.Token));
139 AssertNotNull ("Name", id.Name);
142 AssertEquals ("Token", IntPtr.Zero, id.Token);
143 AssertEquals ("Name", String.Empty, id.Name);
148 public void Current ()
150 WindowsIdentity id = WindowsIdentity.GetCurrent ();
151 AssertNotNull ("AuthenticationType", id.AuthenticationType);
152 Assert ("IsAnonymous", !id.IsAnonymous);
153 Assert ("IsAuthenticated", id.IsAuthenticated);
154 Assert ("IsGuest", !id.IsGuest);
155 // root is 0 - so IntPtr.Zero is valid on Linux (but not on Windows)
156 Assert ("IsSystem", (!id.IsSystem || (id.Token == IntPtr.Zero)));
158 Assert ("Token", (id.Token != IntPtr.Zero));
160 AssertNotNull ("Name", id.Name);
164 public void Interfaces ()
166 WindowsIdentity id = WindowsIdentity.GetAnonymous ();
168 IIdentity i = (id as IIdentity);
169 AssertNotNull ("IIdentity", i);
171 IDeserializationCallback dc = (id as IDeserializationCallback);
172 AssertNotNull ("IDeserializationCallback", dc);
174 ISerializable s = (id as ISerializable);
175 AssertNotNull ("ISerializable", s);