2004-04-13 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / Test / System.Security.Principal / WindowsIdentityTest.cs
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 //
10
11 using NUnit.Framework;
12 using System;
13 using System.Runtime.Serialization;
14 using System.Security.Principal;
15
16 namespace MonoTests.System.Security.Principal {
17
18         [TestFixture]
19         public class WindowsIdentityTest : Assertion {
20
21                 private bool IsPosix {
22                         get { return ((int) Environment.OSVersion.Platform == 128); }
23                 }
24
25                 // some features works only in Windows 2003 and later
26                 private bool IsWin2k3orLater {
27                         get {
28                                 OperatingSystem os = Environment.OSVersion;
29                                 if (os.Platform != PlatformID.Win32NT)
30                                         return false;
31
32                                 if (os.Version.Major > 5) {
33                                         return false;
34                                 }
35                                 else if (os.Version.Major == 5) {
36                                         return (os.Version.Minor > 1);
37                                 }
38                                 return false;
39                         }
40                 }
41
42                 [Test]
43                 public void ConstructorIntPtrZero () 
44                 {
45                         // should fail on Windows (invalid token)
46                         // should not fail on Posix (root uid)
47                         try {
48                                 WindowsIdentity id = new WindowsIdentity (IntPtr.Zero);
49                                 if (!IsPosix)
50                                         Fail ("Expected ArgumentException on Windows platforms");
51                         }
52                         catch (ArgumentException) {
53                                 if (IsPosix)
54                                         throw;
55                         }
56                 }
57 #if !NET_1_0
58                 [Test]
59                 //[ExpectedException (typeof (ArgumentNullException))]
60                 [ExpectedException (typeof (NullReferenceException))]
61                 public void ConstructorW2KS1_Null () 
62                 {
63                         WindowsIdentity id = new WindowsIdentity (null);
64                 }
65
66                 [Test]
67                 public void ConstructorW2KS1 () 
68                 {
69                         WindowsIdentity wi = WindowsIdentity.GetCurrent ();
70                         // should fail with ArgumentException unless
71                         // - running Windows 2003 or later
72                         // - running Posix
73                         try {
74                                 WindowsIdentity id = new WindowsIdentity (wi.Name);
75                                 if (!IsWin2k3orLater && !IsPosix)
76                                         Fail ("Expected ArgumentException but got none");
77                         }
78                         catch (ArgumentException) {
79                                 if (IsWin2k3orLater || IsPosix)
80                                         throw;
81                         }
82                 }
83
84                 [Test]
85                 //[ExpectedException (typeof (ArgumentNullException))]
86                 [ExpectedException (typeof (NullReferenceException))]
87                 public void ConstructorW2KS2_UserNull () 
88                 {
89                         WindowsIdentity id = new WindowsIdentity (null, "NTLM");
90                 }
91
92                 [Test]
93                 public void ConstructorW2KS2_TypeNull() 
94                 {
95                         WindowsIdentity wi = WindowsIdentity.GetCurrent ();
96                         // should fail with ArgumentException unless
97                         // - running Windows 2003 or later
98                         // - running Posix
99                         try {
100                                 WindowsIdentity id = new WindowsIdentity (wi.Name, null);
101                                 if (!IsWin2k3orLater && !IsPosix)
102                                         Fail ("Expected ArgumentException but got none");
103                         }
104                         catch (ArgumentException) {
105                                 if (IsWin2k3orLater || IsPosix)
106                                         throw;
107                         }
108                 }
109
110                 [Test]
111                 public void ConstructorW2KS2 () 
112                 {
113                         WindowsIdentity wi = WindowsIdentity.GetCurrent ();
114                         // should fail with ArgumentException unless
115                         // - running Windows 2003 or later
116                         // - running Posix
117                         try {
118                                 WindowsIdentity id = new WindowsIdentity (wi.Name, wi.AuthenticationType);
119                                 if (!IsWin2k3orLater && !IsPosix)
120                                         Fail ("Expected ArgumentException but got none");
121                         }
122                         catch (ArgumentException) {
123                                 if (IsWin2k3orLater || IsPosix)
124                                         throw;
125                         }
126                 }
127 #endif
128                 [Test]
129                 public void Anonymous () 
130                 {
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);
137                         if (IsPosix) {
138                                 Assert ("Token", (IntPtr.Zero != id.Token));
139                                 AssertNotNull ("Name", id.Name);
140                         }
141                         else {
142                                 AssertEquals ("Token", IntPtr.Zero, id.Token);
143                                 AssertEquals ("Name", String.Empty, id.Name);
144                         }
145                 }
146
147                 [Test]
148                 public void Current () 
149                 {
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)));
157                         if (!IsPosix) {
158                                 Assert ("Token", (id.Token != IntPtr.Zero));
159                         }
160                         AssertNotNull ("Name", id.Name);
161                 }
162
163                 [Test]
164                 public void Interfaces () 
165                 {
166                         WindowsIdentity id = WindowsIdentity.GetAnonymous ();
167
168                         IIdentity i = (id as IIdentity);
169                         AssertNotNull ("IIdentity", i);
170
171                         IDeserializationCallback dc = (id as IDeserializationCallback);
172                         AssertNotNull ("IDeserializationCallback", dc);
173 #if NET_1_1
174                         ISerializable s = (id as ISerializable);
175                         AssertNotNull ("ISerializable", s);
176 #endif
177                 }
178         }
179 }