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;
15 using System.Collections;
19 using Passwd = Mono.Unix.Native.Passwd;
20 using Syscall = Mono.Unix.Native.Syscall;
22 namespace MonoTests.Mono.Unix {
24 [TestFixture, Category ("NotDotNet")]
25 public class UnixUserTest
28 [Category ("AndroidNotWorking")] // setpwent is missing from bionic
29 public void ListAllUsers_ToString ()
32 Console.WriteLine ("Listing all users");
33 foreach (UnixUserInfo user in UnixUserInfo.GetLocalUsers ()) {
34 Console.WriteLine ("\t{0}", user);
39 string.Format ("#TLAU_TS: Exception listing local users: {0}",
45 // According to bug 72293, this may not work:
46 // On systems with NIS, it is possible to have multiple users in the passwd
47 // file with the same name, so the assertion above no longer holds.
48 [Category ("NotWorking")]
49 public void ReentrantConstructors ()
51 ArrayList user_ids = new ArrayList (4);
52 IList users = UnixUserInfo.GetLocalUsers ();
53 foreach (UnixUserInfo user in users) {
55 UnixUserInfo byName = new UnixUserInfo (user.UserName);
56 Assert.AreEqual (user, byName, "#TRC: construct by name");
58 if (! user_ids.Contains (user.UserId))
59 user_ids.Add (user.UserId);
63 string.Format ("#TRC: Exception constructing UnixUserInfo (string): {0}",
68 foreach (uint uid in user_ids) {
70 UnixUserInfo byId = new UnixUserInfo (uid);
71 Assert.IsTrue (users.Contains (byId), "TRC: construct by uid");
75 string.Format ("#TRC: Exception constructing UnixUserInfo (uint): {0}",
83 [Category ("NotOnMac")]
84 [Category ("AndroidNotWorking")] // setpwent is missing from bionic
85 public void NonReentrantSyscalls ()
87 ArrayList user_ids = new ArrayList (4);
88 IList users = UnixUserInfo.GetLocalUsers ();
90 foreach (UnixUserInfo user in users) {
92 Passwd byName = Syscall.getpwnam (user.UserName);
93 Assert.IsNotNull (byName, "#TNRS: access by name");
94 UnixUserInfo n = new UnixUserInfo (byName);
95 Assert.AreEqual (user, n, "#TNRS: construct by name");
97 if (! user_ids.Contains (user.UserId))
98 user_ids.Add (user.UserId);
100 catch (Exception e) {
102 string.Format ("#TNRS: Exception constructing UnixUserInfo (string): {0}",
107 foreach (long uid in user_ids) {
109 Passwd byId = Syscall.getpwuid (Convert.ToUInt32 (uid));
110 Assert.IsNotNull (byId, "#TNRS: access by uid");
112 UnixUserInfo u = new UnixUserInfo (byId);
113 Assert.IsTrue (users.Contains (u), "TNRS: construct by uid");
115 catch (Exception e) {
117 string.Format ("#TNRS: Exception constructing UnixUserInfo (uint): {0}",
124 [Category ("AndroidNotWorking")] // API 21 has getpwnam_r in the NDK headers, but bionic doesn't export it
125 public void InvalidUsers_Constructor_Name ()
127 string[] badUsers = new string[]{"i'm bad", "so am i", "does-not-exist"};
128 foreach (string u in badUsers) {
130 new UnixUserInfo (u);
131 Assert.Fail ("#TIUCN: exception not thrown");
133 catch (ArgumentException) {
136 catch (Exception e) {
137 Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
138 "expected ArgumentException, got {0}: {1}",
139 e.GetType().FullName, e.Message));
145 public void InvalidUsers_Syscall_Name ()
147 string[] badUsers = new string[]{"i'm bad", "so am i", "does-not-exist"};
148 foreach (string u in badUsers) {
150 Passwd pw = Syscall.getpwnam (u);
151 Assert.IsNull (pw, "#TIUSN: invalid users should return null!");
153 catch (Exception e) {
154 Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
155 "expected ArgumentException, got {0}: {1}",
156 e.GetType().FullName, e.Message));
162 public void Equality ()
164 Passwd orig = new Passwd ();
165 Passwd mod = new Passwd ();
166 mod.pw_name = orig.pw_name = "some name";
167 mod.pw_passwd = orig.pw_passwd = "some passwd";
168 mod.pw_uid = orig.pw_uid = 500;
169 mod.pw_gid = orig.pw_gid = 500;
170 mod.pw_gecos = orig.pw_gecos = "some gecos";
171 mod.pw_dir = orig.pw_dir = "/some/dir";
172 mod.pw_shell = orig.pw_shell = "/some/shell";
174 Assert.AreEqual (orig, mod, "#TE: copies should be equal");
176 mod.pw_name = "another name";
177 Assert.IsFalse (orig.Equals (mod), "#TE: changes should be reflected");