2005-05-21 Ben Maurer <bmaurer@ximian.com>
[mono.git] / mcs / class / Mono.Posix / Test / Mono.Unix / UnixUserTest.cs
1 //
2 // UnixUserTest.cs:
3 //      NUnit Test Cases for Mono.Unix.UnixUser
4 //
5 // Authors:
6 //   Jonathan Pryor (jonpryor@vt.edu)
7 //
8 // (C) 2004 Jonathan Pryor
9 // 
10
11 using NUnit.Framework;
12 using System;
13 using System.Configuration;
14 using System.Diagnostics;
15 using System.Xml;
16 using System.Collections;
17
18 using Mono.Unix;
19
20 namespace MonoTests.Mono.Unix {
21
22         [TestFixture, Category ("NotDotNet")]
23         public class UnixUserTest
24         {
25                 [Test]
26                 public void ListAllUsers_ToString ()
27                 {
28                         try {
29                                 Console.WriteLine ("Listing all users");
30                                 foreach (UnixUserInfo user in UnixUser.GetLocalUsers ()) {
31                                         Console.WriteLine ("\t{0}", user);
32                                 }
33                         }
34                         catch (Exception e) {
35                                 Assert.Fail (
36                                                 string.Format ("#TLAU_TS: Exception listing local users: {0}",
37                                                         e.ToString()));
38                         }
39                 }
40
41                 [Test]
42                 // According to bug 72293, this may not work:
43                 // On systems with NIS, it is possible to have multiple users in the passwd
44                 // file with the same name, so the assertion above no longer holds.
45                 [Category ("NotWorking")]
46                 public void ReentrantConstructors ()
47                 {
48                         ArrayList user_ids = new ArrayList (4);
49                         IList users = UnixUser.GetLocalUsers ();
50                         foreach (UnixUserInfo user in users) {
51                                 try {
52                                         UnixUserInfo byName = new UnixUserInfo (user.UserName);
53                                         Assert.AreEqual (user, byName, "#TRC: construct by name");
54
55                                         if (! user_ids.Contains (user.UserId))
56                                                 user_ids.Add (user.UserId);
57                                 }
58                                 catch (Exception e) {
59                                         Assert.Fail (
60                                                      string.Format ("#TRC: Exception constructing UnixUserInfo (string): {0}",
61                                                                     e.ToString()));
62                                 }
63                         }
64
65                         foreach (uint uid in user_ids) {
66                                 try {
67                                         UnixUserInfo byId = new UnixUserInfo (uid);
68                                         Assert.IsTrue (users.Contains (byId), "TRC: construct by uid");
69                                 }
70                                 catch (Exception e) {
71                                         Assert.Fail (
72                                                      string.Format ("#TRC: Exception constructing UnixUserInfo (uint): {0}",
73                                                                     e.ToString()));
74
75                                 }
76                         }
77                 }
78
79                 [Test]
80                 public void NonReentrantSyscalls ()
81                 {
82                         ArrayList user_ids = new ArrayList (4);
83                         IList users = UnixUser.GetLocalUsers ();
84
85                         foreach (UnixUserInfo user in users) {
86                                 try {
87                                         Passwd byName = Syscall.getpwnam (user.UserName);
88                                         Assert.IsNotNull (byName, "#TNRS: access by name");
89                                         UnixUserInfo n = new UnixUserInfo (byName);
90                                         Assert.AreEqual (user, n, "#TNRS: construct by name");
91
92                                         if (! user_ids.Contains (user.UserId))
93                                                 user_ids.Add (user.UserId);
94                                 }
95                                 catch (Exception e) {
96                                         Assert.Fail (
97                                                 string.Format ("#TNRS: Exception constructing UnixUserInfo (string): {0}",
98                                                         e.ToString()));
99                                 }
100                         }
101
102                         foreach (uint uid in user_ids) {
103                                 try {
104                                         Passwd byId   = Syscall.getpwuid (uid);
105                                         Assert.IsNotNull (byId,   "#TNRS: access by uid");
106
107                                         UnixUserInfo u = new UnixUserInfo (byId);
108                                         Assert.IsTrue (users.Contains (u), "TNRS: construct by uid");
109                                 }
110                                 catch (Exception e) {
111                                         Assert.Fail (
112                                                 string.Format ("#TNRS: Exception constructing UnixUserInfo (uint): {0}",
113                                                         e.ToString()));
114                                 }
115                         }
116                 }
117
118                 [Test]
119                 public void InvalidUsers_Constructor_Name ()
120                 {
121                         string[] badUsers = new string[]{"i'm bad", "so am i", "does-not-exist"};
122                         foreach (string u in badUsers) {
123                                 try {
124                                         new UnixUserInfo (u);
125                                         Assert.Fail ("#TIUCN: exception not thrown");
126                                 }
127                                 catch (ArgumentException) {
128                                         // expected
129                                 }
130                                 catch (Exception e) {
131                                         Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
132                                                                 "expected ArgumentException, got {0}: {1}",
133                                                                 e.GetType().FullName, e.Message));
134                                 }
135                         }
136                 }
137
138                 [Test]
139                 public void InvalidUsers_Syscall_Name ()
140                 {
141                         string[] badUsers = new string[]{"i'm bad", "so am i", "does-not-exist"};
142                         foreach (string u in badUsers) {
143                                 try {
144                                         Passwd pw = Syscall.getpwnam (u);
145                                         Assert.IsNull (pw, "#TIUSN: invalid users should return null!");
146                                 }
147                                 catch (Exception e) {
148                                         Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
149                                                                 "expected ArgumentException, got {0}: {1}",
150                                                                 e.GetType().FullName, e.Message));
151                                 }
152                         }
153                 }
154
155                 [Test]
156                 public void Equality ()
157                 {
158                         Passwd orig = new Passwd ();
159                         Passwd mod  = new Passwd ();
160                         mod.pw_name   = orig.pw_name   = "some name";
161                         mod.pw_passwd = orig.pw_passwd = "some passwd";
162                         mod.pw_uid    = orig.pw_uid    = 500;
163                         mod.pw_gid    = orig.pw_gid    = 500;
164                         mod.pw_gecos  = orig.pw_gecos  = "some gecos";
165                         mod.pw_dir    = orig.pw_dir    = "/some/dir";
166                         mod.pw_shell  = orig.pw_shell  = "/some/shell";
167
168                         Assert.AreEqual (orig, mod, "#TE: copies should be equal");
169
170                         mod.pw_name = "another name";
171                         Assert.IsFalse (orig.Equals (mod), "#TE: changes should be reflected");
172                 }
173         }
174 }
175