File: WindowsIdentityTest.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (233 lines) | stat: -rw-r--r-- 7,134 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
// WindowsIdentityTest.cs - NUnit Test Cases for WindowsIdentity
//
// Author:
//	Sebastien Pouliot (sebastien@ximian.com)
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
//

using NUnit.Framework;
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Principal;

namespace MonoTests.System.Security.Principal {

	[TestFixture]
	public class WindowsIdentityTest {

		private bool IsPosix {
			get {
				// check for Unix platforms - see FAQ for more details
				// http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
				int platform = (int) Environment.OSVersion.Platform;
				return ((platform == 4) || (platform == 128) || (platform == 6));
			}
		}

		// some features works only in Windows 2003 and later
		private bool IsWin2k3orLater {
			get {
				// requires both a W2K3 client and server (domain)
				// which I don't have access to debug/support
				OperatingSystem os = Environment.OSVersion;
				if (os.Platform != PlatformID.Win32NT)
					return false;

				if (os.Version.Major > 5) {
					return false;
				}
				else if (os.Version.Major == 5) {
					return (os.Version.Minor > 1);
				}
				return false;
			}
		}

		[Test]
		public void ConstructorIntPtrZero () 
		{
			// should fail on Windows (invalid token)
			// should not fail on Posix (root uid)
			try {
				WindowsIdentity id = new WindowsIdentity (IntPtr.Zero);
				if (!IsPosix)
					Assert.Fail ("Expected ArgumentException on Windows platforms");
			}
			catch (ArgumentException) {
				if (IsPosix)
					throw;
			}
		}
#if !NET_1_0
		[Test]
		//[ExpectedException (typeof (ArgumentNullException))]
		[ExpectedException (typeof (NullReferenceException))]
		public void ConstructorW2KS1_Null () 
		{
			WindowsIdentity id = new WindowsIdentity (null);
		}

		[Test]
		public void ConstructorW2KS1 () 
		{
			WindowsIdentity wi = WindowsIdentity.GetCurrent ();
			// should fail with ArgumentException unless
			// - running Windows 2003 or later (both client and domain server)
			// - running Posix
			try {
				WindowsIdentity id = new WindowsIdentity (wi.Name);
				/*if (!IsWin2k3orLater && !IsPosix)
					Assert.Fail ("Expected ArgumentException but got none");*/
			}
			catch (ArgumentException) {
				if (/*IsWin2k3orLater ||*/ IsPosix)
					throw;
			}
		}

		[Test]
		//[ExpectedException (typeof (ArgumentNullException))]
		[ExpectedException (typeof (NullReferenceException))]
		public void ConstructorW2KS2_UserNull () 
		{
			WindowsIdentity id = new WindowsIdentity (null, "NTLM");
		}

		[Test]
		public void ConstructorW2KS2_TypeNull() 
		{
			WindowsIdentity wi = WindowsIdentity.GetCurrent ();
			// should fail with ArgumentException unless
			// - running Windows 2003 or later (both client and domain server)
			// - running Posix
			try {
				WindowsIdentity id = new WindowsIdentity (wi.Name, null);
				/*if (!IsWin2k3orLater && !IsPosix)
					Assert.Fail ("Expected ArgumentException but got none");*/
			}
			catch (ArgumentException) {
				if (/*IsWin2k3orLater ||*/ IsPosix)
					throw;
			}
		}

		[Test]
		public void ConstructorW2KS2 () 
		{
			WindowsIdentity wi = WindowsIdentity.GetCurrent ();
			// should fail with ArgumentException unless
			// - running Windows 2003 or later (both client and domain server)
			// - running Posix
			try {
				WindowsIdentity id = new WindowsIdentity (wi.Name, wi.AuthenticationType);
				/*if (!IsWin2k3orLater && !IsPosix)
					Assert.Fail ("Expected ArgumentException but got none");*/
			}
			catch (ArgumentException) {
				if (/*IsWin2k3orLater ||*/ IsPosix)
					throw;
			}
		}
#endif
		[Test]
		public void Anonymous () 
		{
			WindowsIdentity id = WindowsIdentity.GetAnonymous ();
			Assert.AreEqual (String.Empty, id.AuthenticationType, "AuthenticationType");
			Assert.IsTrue (id.IsAnonymous, "IsAnonymous");
			Assert.IsTrue (!id.IsAuthenticated, "IsAuthenticated");
			Assert.IsTrue (!id.IsGuest, "IsGuest");
			Assert.IsTrue (!id.IsSystem, "IsSystem");
			if (IsPosix) {
				Assert.IsTrue ((IntPtr.Zero != id.Token), "Token");
				Assert.IsNotNull (id.Name, "Name");
			}
			else {
				Assert.AreEqual (IntPtr.Zero, id.Token, "Token");
				Assert.AreEqual (String.Empty, id.Name, "Name");
			}
		}

		[Test]
		public void Current () 
		{
			WindowsIdentity id = WindowsIdentity.GetCurrent ();
			Assert.IsNotNull (id.AuthenticationType, "AuthenticationType");
			Assert.IsTrue (!id.IsAnonymous, "IsAnonymous");
			Assert.IsTrue (id.IsAuthenticated, "IsAuthenticated");
			Assert.IsTrue (!id.IsGuest, "IsGuest");
			// root is 0 - so IntPtr.Zero is valid on Linux (but not on Windows)
			Assert.IsTrue ((!id.IsSystem || (id.Token == IntPtr.Zero)), "IsSystem");
			if (!IsPosix) {
				Assert.IsTrue ((id.Token != IntPtr.Zero), "Token");
			}
			Assert.IsNotNull (id.Name, "Name");
		}

		[Test]
		public void Interfaces () 
		{
			WindowsIdentity id = WindowsIdentity.GetAnonymous ();

			IIdentity i = (id as IIdentity);
			Assert.IsNotNull (i, "IIdentity");

			IDeserializationCallback dc = (id as IDeserializationCallback);
			Assert.IsNotNull (dc, "IDeserializationCallback");
			ISerializable s = (id as ISerializable);
			Assert.IsNotNull (s, "ISerializable");
		}

		// This is clearly a hack - but I've seen it too many times so I think we 
		// better support it too :(
		// http://dotnetjunkies.com/WebLog/chris.taylor/archive/2004/02/25/7945.aspx
		public string[] GetWindowsIdentityRoles (WindowsIdentity identity)
		{
			object result = typeof(WindowsIdentity).InvokeMember ("_GetRoles",
				BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.NonPublic,
				null, identity, new object[] {identity.Token}, null);
			return (string[]) result;
		}

		[Test]
		public void GetRolesViaReflection () 
		{
			// remove g_warning from being show during unit tests
			if (IsPosix)
				Assert.Ignore ("Running on Unix.");

			WindowsIdentity wi = WindowsIdentity.GetCurrent ();
			WindowsPrincipal wp = new WindowsPrincipal (wi);
			string[] roles = GetWindowsIdentityRoles (wi);
			foreach (string role in roles) {
				// somehow I got a null in there ?
				if (role != null)
					Assert.IsTrue (wp.IsInRole (role), role);
			}
		}

		[Test]
		public void SerializeRoundTrip () 
		{
			WindowsIdentity wi = WindowsIdentity.GetCurrent ();
			MemoryStream ms = new MemoryStream ();
			IFormatter formatter = new BinaryFormatter ();
			formatter.Serialize (ms, wi);
			ms.Position = 0;
			WindowsIdentity back = (WindowsIdentity) formatter.Deserialize (ms);
			Assert.AreEqual (wi.AuthenticationType, back.AuthenticationType, "AuthenticationType");
			Assert.AreEqual (wi.IsAnonymous, back.IsAnonymous, "IsAnonymous");
			Assert.AreEqual (wi.IsAuthenticated, back.IsAuthenticated, "IsAuthenticated");
			Assert.AreEqual (wi.IsGuest, back.IsGuest, "IsGuest");
			Assert.AreEqual (wi.IsSystem, back.IsSystem, "IsSystem");
			Assert.AreEqual (wi.Name, back.Name, "Name");
			// note: token may be different (no compare)
		}
	}
}