3 // NUnit Test Cases for Mono.Unix.UnixUser
6 // Jonathan Pryor (jonpryor@vt.edu)
8 // (C) 2004 Jonathan Pryor
11 using NUnit.Framework;
13 using System.Configuration;
14 using System.Diagnostics;
16 using System.Collections;
20 namespace MonoTests.Mono.Unix {
22 [TestFixture, Category ("NotDotNet")]
23 public class UnixUserTest
26 public void ListAllUsers_ToString ()
29 Console.WriteLine ("Listing all users");
30 foreach (UnixUserInfo user in UnixUser.GetLocalUsers ()) {
31 Console.WriteLine ("\t{0}", user);
36 string.Format ("#TLAU_TS: Exception listing local users: {0}",
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 ()
48 ArrayList user_ids = new ArrayList (4);
49 IList users = UnixUser.GetLocalUsers ();
50 foreach (UnixUserInfo user in users) {
52 UnixUserInfo byName = new UnixUserInfo (user.UserName);
53 Assert.AreEqual (user, byName, "#TRC: construct by name");
55 if (! user_ids.Contains (user.UserId))
56 user_ids.Add (user.UserId);
60 string.Format ("#TRC: Exception constructing UnixUserInfo (string): {0}",
65 foreach (uint uid in user_ids) {
67 UnixUserInfo byId = new UnixUserInfo (uid);
68 Assert.IsTrue (users.Contains (byId), "TRC: construct by uid");
72 string.Format ("#TRC: Exception constructing UnixUserInfo (uint): {0}",
80 public void NonReentrantSyscalls ()
82 ArrayList user_ids = new ArrayList (4);
83 IList users = UnixUser.GetLocalUsers ();
85 foreach (UnixUserInfo user in users) {
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");
92 if (! user_ids.Contains (user.UserId))
93 user_ids.Add (user.UserId);
97 string.Format ("#TNRS: Exception constructing UnixUserInfo (string): {0}",
102 foreach (uint uid in user_ids) {
104 Passwd byId = Syscall.getpwuid (uid);
105 Assert.IsNotNull (byId, "#TNRS: access by uid");
107 UnixUserInfo u = new UnixUserInfo (byId);
108 Assert.IsTrue (users.Contains (u), "TNRS: construct by uid");
110 catch (Exception e) {
112 string.Format ("#TNRS: Exception constructing UnixUserInfo (uint): {0}",
119 public void InvalidUsers_Constructor_Name ()
121 string[] badUsers = new string[]{"i'm bad", "so am i", "does-not-exist"};
122 foreach (string u in badUsers) {
124 new UnixUserInfo (u);
125 Assert.Fail ("#TIUCN: exception not thrown");
127 catch (ArgumentException) {
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));
139 public void InvalidUsers_Syscall_Name ()
141 string[] badUsers = new string[]{"i'm bad", "so am i", "does-not-exist"};
142 foreach (string u in badUsers) {
144 Passwd pw = Syscall.getpwnam (u);
145 Assert.IsNull (pw, "#TIUSN: invalid users should return null!");
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));
156 public void Equality ()
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";
168 Assert.AreEqual (orig, mod, "#TE: copies should be equal");
170 mod.pw_name = "another name";
171 Assert.IsFalse (orig.Equals (mod), "#TE: changes should be reflected");