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;
19 namespace MonoTests.Mono.Unix {
21 [TestFixture, Category ("NotDotNet")]
22 public class UnixGroupTest
25 public void ListAllGroups_ToString ()
28 Console.WriteLine ("Listing all groups");
29 foreach (UnixGroupInfo group in UnixGroup.GetLocalGroups ()) {
30 Console.WriteLine ("\t{0}", group);
35 string.Format ("#TLAU_TS: Exception listing local groups: {0}",
41 // According to bug 72293, this may not work:
42 // On systems with NIS, it is possible to have multiple users in the passwd
43 // file with the same name, so the assertion above no longer holds.
44 [Category ("NotWorking")]
45 public void ReentrantConstructors ()
47 foreach (UnixGroupInfo group in UnixGroup.GetLocalGroups ()) {
49 UnixGroupInfo byName = new UnixGroupInfo (group.GroupName);
50 UnixGroupInfo byId = new UnixGroupInfo (group.GroupId);
52 Assert.AreEqual (group, byName, "#TRC: construct by name");
53 Assert.AreEqual (group, byId, "#TRC: construct by gid");
54 Assert.AreEqual (byName, byId, "#TRC: name == gid?");
58 string.Format ("#TRC: Exception constructing UnixGroupInfo: {0}",
65 public void NonReentrantSyscalls ()
67 foreach (UnixGroupInfo group in UnixGroup.GetLocalGroups ()) {
69 Group byName = Syscall.getgrnam (group.GroupName);
70 Group byId = Syscall.getgrgid (group.GroupId);
72 Assert.IsNotNull (byName, "#TNRS: access by name");
73 Assert.IsNotNull (byId, "#TNRS: access by gid");
75 UnixGroupInfo n = new UnixGroupInfo (byName);
76 UnixGroupInfo u = new UnixGroupInfo (byId);
78 Assert.AreEqual (group, n, "#TNRS: construct by name");
79 Assert.AreEqual (group, u, "#TNRS: construct by gid");
80 Assert.AreEqual (n, u, "#TNRS: name == gid?");
84 string.Format ("#TRC: Exception constructing UnixGroupInfo: {0}",
91 public void InvalidGroups_Constructor_Name ()
93 string[] badGroups = new string[]{"i'm bad", "so am i", "does-not-exist"};
94 foreach (string u in badGroups) {
96 new UnixGroupInfo (u);
97 Assert.Fail ("#TIUCN: exception not thrown");
99 catch (ArgumentException) {
102 catch (Exception e) {
103 Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
104 "expected ArgumentException, got {0}: {1}",
105 e.GetType().FullName, e.Message));
111 public void InvalidGroups_Syscall_Name ()
113 string[] badGroups = new string[]{"i'm bad", "so am i", "does-not-exist"};
114 foreach (string u in badGroups) {
116 Group pw = Syscall.getgrnam (u);
117 Assert.IsNull (pw, "#TIUSN: invalid groups should return null!");
119 catch (Exception e) {
120 Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
121 "expected null return, got {0}: {1}",
122 e.GetType().FullName, e.Message));
128 public void Equality ()
130 Group orig = new Group ();
131 Group mod = new Group ();
132 mod.gr_name = orig.gr_name = "some name";
133 mod.gr_passwd = orig.gr_passwd = "some passwd";
134 mod.gr_gid = orig.gr_gid = 500;
135 mod.gr_mem = orig.gr_mem = new string[]{"foo", "bar"};
137 Assert.AreEqual (orig, mod, "#TE: copies should be equal");
139 mod.gr_name = "another name";
140 Assert.IsFalse (orig.Equals (mod), "#TE: changes should be reflected");