**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / Test / System.Security.Principal / WindowsIdentityTest.cs
blobc7a85f78e5d5e1e0eea3fee4f08f233d69bc7b2b
1 //
2 // WindowsIdentityTest.cs - NUnit Test Cases for WindowsIdentity
3 //
4 // Author:
5 // Sebastien Pouliot (sebastien@ximian.com)
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
9 //
11 using NUnit.Framework;
12 using System;
13 using System.IO;
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 {
21 [TestFixture]
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 {
30 get {
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)
35 return false;
37 if (os.Version.Major > 5) {
38 return false;
40 else if (os.Version.Major == 5) {
41 return (os.Version.Minor > 1);
43 return false;
47 [Test]
48 public void ConstructorIntPtrZero ()
50 // should fail on Windows (invalid token)
51 // should not fail on Posix (root uid)
52 try {
53 WindowsIdentity id = new WindowsIdentity (IntPtr.Zero);
54 if (!IsPosix)
55 Fail ("Expected ArgumentException on Windows platforms");
57 catch (ArgumentException) {
58 if (IsPosix)
59 throw;
62 #if !NET_1_0
63 [Test]
64 //[ExpectedException (typeof (ArgumentNullException))]
65 [ExpectedException (typeof (NullReferenceException))]
66 public void ConstructorW2KS1_Null ()
68 WindowsIdentity id = new WindowsIdentity (null);
71 [Test]
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)
77 // - running Posix
78 try {
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)
85 throw;
89 [Test]
90 //[ExpectedException (typeof (ArgumentNullException))]
91 [ExpectedException (typeof (NullReferenceException))]
92 public void ConstructorW2KS2_UserNull ()
94 WindowsIdentity id = new WindowsIdentity (null, "NTLM");
97 [Test]
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)
103 // - running Posix
104 try {
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)
111 throw;
115 [Test]
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)
121 // - running Posix
122 try {
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)
129 throw;
132 #endif
133 [Test]
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);
142 if (IsPosix) {
143 Assert ("Token", (IntPtr.Zero != id.Token));
144 AssertNotNull ("Name", id.Name);
146 else {
147 AssertEquals ("Token", IntPtr.Zero, id.Token);
148 AssertEquals ("Name", String.Empty, id.Name);
152 [Test]
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)));
162 if (!IsPosix) {
163 Assert ("Token", (id.Token != IntPtr.Zero));
165 AssertNotNull ("Name", id.Name);
168 [Test]
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);
178 #if NET_1_1
179 ISerializable s = (id as ISerializable);
180 AssertNotNull ("ISerializable", s);
181 #endif
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;
195 [Test]
196 public void GetRolesViaReflection ()
198 // remove g_warning from being show during unit tests
199 if (IsPosix)
200 return;
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 ?
207 if (role != null)
208 Assert (role, wp.IsInRole (role));
212 [Test]
213 public void SerializeRoundTrip ()
215 WindowsIdentity wi = WindowsIdentity.GetCurrent ();
216 MemoryStream ms = new MemoryStream ();
217 IFormatter formatter = new BinaryFormatter ();
218 formatter.Serialize (ms, wi);
219 ms.Position = 0;
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)