[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / corlib / Test / System.Security.Principal / WindowsIdentityTest.cs
blobbdb884534c124b4a726bfb9ae2d9ee8d65d41f5e
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 // Copyright (C) 2004-2005 Novell, Inc (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 {
24 private bool IsPosix {
25 get {
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 {
35 get {
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)
40 return false;
42 if (os.Version.Major > 5) {
43 return false;
45 else if (os.Version.Major == 5) {
46 return (os.Version.Minor > 1);
48 return false;
52 [Test]
53 public void ConstructorIntPtrZero ()
55 // should fail on Windows (invalid token)
56 // should not fail on Posix (root uid)
57 try {
58 WindowsIdentity id = new WindowsIdentity (IntPtr.Zero);
59 if (!IsPosix)
60 Assert.Fail ("Expected ArgumentException on Windows platforms");
62 catch (ArgumentException) {
63 if (IsPosix)
64 throw;
67 #if !NET_1_0
68 [Test]
69 //[ExpectedException (typeof (ArgumentNullException))]
70 [ExpectedException (typeof (NullReferenceException))]
71 public void ConstructorW2KS1_Null ()
73 WindowsIdentity id = new WindowsIdentity (null);
76 [Test]
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)
82 // - running Posix
83 try {
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)
90 throw;
94 [Test]
95 //[ExpectedException (typeof (ArgumentNullException))]
96 [ExpectedException (typeof (NullReferenceException))]
97 public void ConstructorW2KS2_UserNull ()
99 WindowsIdentity id = new WindowsIdentity (null, "NTLM");
102 [Test]
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)
108 // - running Posix
109 try {
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)
116 throw;
120 [Test]
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)
126 // - running Posix
127 try {
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)
134 throw;
137 #endif
138 [Test]
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");
147 if (IsPosix) {
148 Assert.IsTrue ((IntPtr.Zero != id.Token), "Token");
149 Assert.IsNotNull (id.Name, "Name");
151 else {
152 Assert.AreEqual (IntPtr.Zero, id.Token, "Token");
153 Assert.AreEqual (String.Empty, id.Name, "Name");
157 [Test]
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");
167 if (!IsPosix) {
168 Assert.IsTrue ((id.Token != IntPtr.Zero), "Token");
170 Assert.IsNotNull (id.Name, "Name");
173 [Test]
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;
198 [Test]
199 public void GetRolesViaReflection ()
201 // remove g_warning from being show during unit tests
202 if (IsPosix)
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 ?
210 if (role != null)
211 Assert.IsTrue (wp.IsInRole (role), role);
215 [Test]
216 public void SerializeRoundTrip ()
218 WindowsIdentity wi = WindowsIdentity.GetCurrent ();
219 MemoryStream ms = new MemoryStream ();
220 IFormatter formatter = new BinaryFormatter ();
221 formatter.Serialize (ms, wi);
222 ms.Position = 0;
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)