[tests] Disable on Android, getpwnam_r doesn't exist there
[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                 [Category ("AndroidNotWorking")] // API 21 has getpwnam_r in the NDK headers, but bionic doesn't export it
125                 public void InvalidUsers_Constructor_Name ()
126                 {
127                         string[] badUsers = new string[]{"i'm bad", "so am i", "does-not-exist"};
128                         foreach (string u in badUsers) {
129                                 try {
130                                         new UnixUserInfo (u);
131                                         Assert.Fail ("#TIUCN: exception not thrown");
132                                 }
133                                 catch (ArgumentException) {
134                                         // expected
135                                 }
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));
140                                 }
141                         }
142                 }
143
144                 [Test]
145                 public void InvalidUsers_Syscall_Name ()
146                 {
147                         string[] badUsers = new string[]{"i'm bad", "so am i", "does-not-exist"};
148                         foreach (string u in badUsers) {
149                                 try {
150                                         Passwd pw = Syscall.getpwnam (u);
151                                         Assert.IsNull (pw, "#TIUSN: invalid users should return null!");
152                                 }
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));
157                                 }
158                         }
159                 }
160
161                 [Test]
162                 public void Equality ()
163                 {
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";
173
174                         Assert.AreEqual (orig, mod, "#TE: copies should be equal");
175
176                         mod.pw_name = "another name";
177                         Assert.IsFalse (orig.Equals (mod), "#TE: changes should be reflected");
178                 }
179         }
180 }
181