3 // NUnit Test Cases for Mono.Unix.UnixGroup
6 // Jonathan Pryor (jonpryor@vt.edu)
8 // (C) 2004 Jonathan Pryor
11 using NUnit.Framework;
13 using System.Configuration;
14 using System.Diagnostics;
18 using Group = Mono.Unix.Native.Group;
19 using Syscall = Mono.Unix.Native.Syscall;
21 namespace MonoTests.Mono.Unix {
23 [TestFixture, Category ("NotDotNet")]
24 public class UnixGroupTest
27 public void ListAllGroups_ToString ()
30 Console.WriteLine ("Listing all groups");
31 foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups ()) {
32 Console.WriteLine ("\t{0}", group);
37 string.Format ("#TLAU_TS: Exception listing local groups: {0}",
43 // According to bug 72293, this may not work:
44 // On systems with NIS, it is possible to have multiple users in the passwd
45 // file with the same name, so the assertion above no longer holds.
46 [Category ("NotWorking")]
47 public void ReentrantConstructors ()
49 foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups ()) {
51 UnixGroupInfo byName = new UnixGroupInfo (group.GroupName);
52 UnixGroupInfo byId = new UnixGroupInfo (group.GroupId);
54 Assert.AreEqual (group, byName, "#TRC: construct by name");
55 Assert.AreEqual (group, byId, "#TRC: construct by gid");
56 Assert.AreEqual (byName, byId, "#TRC: name == gid?");
60 string.Format ("#TRC: Exception constructing UnixGroupInfo: {0}",
67 public void NonReentrantSyscalls ()
69 foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups ()) {
71 Group byName = Syscall.getgrnam (group.GroupName);
72 Group byId = Syscall.getgrgid ((uint) group.GroupId);
74 Assert.IsNotNull (byName, "#TNRS: access by name");
75 Assert.IsNotNull (byId, "#TNRS: access by gid");
77 UnixGroupInfo n = new UnixGroupInfo (byName);
78 UnixGroupInfo u = new UnixGroupInfo (byId);
80 Assert.AreEqual (group, n, "#TNRS: construct by name");
81 Assert.AreEqual (group, u, "#TNRS: construct by gid");
82 Assert.AreEqual (n, u, "#TNRS: name == gid?");
86 string.Format ("#TRC: Exception constructing UnixGroupInfo: {0}",
93 public void InvalidGroups_Constructor_Name ()
95 string[] badGroups = new string[]{"i'm bad", "so am i", "does-not-exist"};
96 foreach (string u in badGroups) {
98 new UnixGroupInfo (u);
99 Assert.Fail ("#TIUCN: exception not thrown");
101 catch (ArgumentException) {
104 catch (Exception e) {
105 Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
106 "expected ArgumentException, got {0}: {1}",
107 e.GetType().FullName, e.Message));
113 public void InvalidGroups_Syscall_Name ()
115 string[] badGroups = new string[]{"i'm bad", "so am i", "does-not-exist"};
116 foreach (string u in badGroups) {
118 Group pw = Syscall.getgrnam (u);
119 Assert.IsNull (pw, "#TIUSN: invalid groups should return null!");
121 catch (Exception e) {
122 Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
123 "expected null return, got {0}: {1}",
124 e.GetType().FullName, e.Message));
130 public void Equality ()
132 Group orig = new Group ();
133 Group mod = new Group ();
134 mod.gr_name = orig.gr_name = "some name";
135 mod.gr_passwd = orig.gr_passwd = "some passwd";
136 mod.gr_gid = orig.gr_gid = 500;
137 mod.gr_mem = orig.gr_mem = new string[]{"foo", "bar"};
139 Assert.AreEqual (orig, mod, "#TE: copies should be equal");
141 mod.gr_name = "another name";
142 Assert.IsFalse (orig.Equals (mod), "#TE: changes should be reflected");