0e5fa71c1ec5ce8d2b54ca87914c09bd71c466ca
[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.Collections;
16
17 using Mono.Unix;
18
19 using Passwd = Mono.Unix.Native.Passwd;
20 using Syscall = Mono.Unix.Native.Syscall;
21
22 namespace MonoTests.Mono.Unix {
23
24         [TestFixture, Category ("NotDotNet")]
25         public class UnixUserTest
26         {
27                 [Test]
28                 [Category ("AndroidNotWorking")] // setpwent is missing from bionic
29                 public void ListAllUsers_ToString ()
30                 {
31                         try {
32                                 Console.WriteLine ("Listing all users");
33                                 foreach (UnixUserInfo user in UnixUserInfo.GetLocalUsers ()) {
34                                         Console.WriteLine ("\t{0}", user);
35                                 }
36                         }
37                         catch (Exception e) {
38                                 Assert.Fail (
39                                                 string.Format ("#TLAU_TS: Exception listing local users: {0}",
40                                                         e.ToString()));
41                         }
42                 }
43
44                 [Test]
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 ()
50                 {
51                         ArrayList user_ids = new ArrayList (4);
52                         IList users = UnixUserInfo.GetLocalUsers ();
53                         foreach (UnixUserInfo user in users) {
54                                 try {
55                                         UnixUserInfo byName = new UnixUserInfo (user.UserName);
56                                         Assert.AreEqual (user, byName, "#TRC: construct by name");
57
58                                         if (! user_ids.Contains (user.UserId))
59                                                 user_ids.Add (user.UserId);
60                                 }
61                                 catch (Exception e) {
62                                         Assert.Fail (
63                                                      string.Format ("#TRC: Exception constructing UnixUserInfo (string): {0}",
64                                                                     e.ToString()));
65                                 }
66                         }
67
68                         foreach (uint uid in user_ids) {
69                                 try {
70                                         UnixUserInfo byId = new UnixUserInfo (uid);
71                                         Assert.IsTrue (users.Contains (byId), "TRC: construct by uid");
72                                 }
73                                 catch (Exception e) {
74                                         Assert.Fail (
75                                                      string.Format ("#TRC: Exception constructing UnixUserInfo (uint): {0}",
76                                                                     e.ToString()));
77
78                                 }
79                         }
80                 }
81
82                 [Test]
83                 [Category ("NotOnMac")]
84                 [Category ("AndroidNotWorking")] // setpwent is missing from bionic
85                 public void NonReentrantSyscalls ()
86                 {
87                         ArrayList user_ids = new ArrayList (4);
88                         IList users = UnixUserInfo.GetLocalUsers ();
89
90                         foreach (UnixUserInfo user in users) {
91                                 try {
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");
96
97                                         if (! user_ids.Contains (user.UserId))
98                                                 user_ids.Add (user.UserId);
99                                 }
100                                 catch (Exception e) {
101                                         Assert.Fail (
102                                                 string.Format ("#TNRS: Exception constructing UnixUserInfo (string): {0}",
103                                                         e.ToString()));
104                                 }
105                         }
106
107                         foreach (long uid in user_ids) {
108                                 try {
109                                         Passwd byId   = Syscall.getpwuid (Convert.ToUInt32 (uid));
110                                         Assert.IsNotNull (byId,   "#TNRS: access by uid");
111
112                                         UnixUserInfo u = new UnixUserInfo (byId);
113                                         Assert.IsTrue (users.Contains (u), "TNRS: construct by uid");
114                                 }
115                                 catch (Exception e) {
116                                         Assert.Fail (
117                                                 string.Format ("#TNRS: Exception constructing UnixUserInfo (uint): {0}",
118                                                         e.ToString()));
119                                 }
120                         }
121                 }
122
123                 [Test]
124                 public void InvalidUsers_Constructor_Name ()
125                 {
126                         string[] badUsers = new string[]{"i'm bad", "so am i", "does-not-exist"};
127                         foreach (string u in badUsers) {
128                                 try {
129                                         new UnixUserInfo (u);
130                                         Assert.Fail ("#TIUCN: exception not thrown");
131                                 }
132                                 catch (ArgumentException) {
133                                         // expected
134                                 }
135                                 catch (Exception e) {
136                                         Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
137                                                                 "expected ArgumentException, got {0}: {1}",
138                                                                 e.GetType().FullName, e.Message));
139                                 }
140                         }
141                 }
142
143                 [Test]
144                 public void InvalidUsers_Syscall_Name ()
145                 {
146                         string[] badUsers = new string[]{"i'm bad", "so am i", "does-not-exist"};
147                         foreach (string u in badUsers) {
148                                 try {
149                                         Passwd pw = Syscall.getpwnam (u);
150                                         Assert.IsNull (pw, "#TIUSN: invalid users should return null!");
151                                 }
152                                 catch (Exception e) {
153                                         Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
154                                                                 "expected ArgumentException, got {0}: {1}",
155                                                                 e.GetType().FullName, e.Message));
156                                 }
157                         }
158                 }
159
160                 [Test]
161                 public void Equality ()
162                 {
163                         Passwd orig = new Passwd ();
164                         Passwd mod  = new Passwd ();
165                         mod.pw_name   = orig.pw_name   = "some name";
166                         mod.pw_passwd = orig.pw_passwd = "some passwd";
167                         mod.pw_uid    = orig.pw_uid    = 500;
168                         mod.pw_gid    = orig.pw_gid    = 500;
169                         mod.pw_gecos  = orig.pw_gecos  = "some gecos";
170                         mod.pw_dir    = orig.pw_dir    = "/some/dir";
171                         mod.pw_shell  = orig.pw_shell  = "/some/shell";
172
173                         Assert.AreEqual (orig, mod, "#TE: copies should be equal");
174
175                         mod.pw_name = "another name";
176                         Assert.IsFalse (orig.Equals (mod), "#TE: changes should be reflected");
177                 }
178         }
179 }
180