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.Collections.Generic;
14 using System.Configuration;
15 using System.Diagnostics;
19 using Group = Mono.Unix.Native.Group;
20 using Syscall = Mono.Unix.Native.Syscall;
22 namespace MonoTests.Mono.Unix {
24 [TestFixture, Category ("NotDotNet")]
25 public class UnixGroupTest
28 public void ListAllGroups_ToString ()
31 Console.WriteLine ("Listing all groups");
32 foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups ()) {
33 Console.WriteLine ("\t{0}", group);
38 string.Format ("#TLAU_TS: Exception listing local groups: {0}",
44 public void ReentrantConstructors ()
46 var seen = new Dictionary<string, object> ();
47 foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups ()) {
48 if (seen.ContainsKey (group.GroupName))
50 seen.Add (group.GroupName, null);
52 UnixGroupInfo byName = new UnixGroupInfo (group.GroupName);
53 UnixGroupInfo byId = new UnixGroupInfo (group.GroupId);
55 Assert.AreEqual (group, byName, "#TRC: construct by name");
56 Assert.AreEqual (group, byId, "#TRC: construct by gid");
57 Assert.AreEqual (byName, byId, "#TRC: name == gid?");
61 string.Format ("#TRC: Exception constructing UnixGroupInfo: {0}",
68 public void NonReentrantSyscalls ()
70 var seen = new Dictionary<string, object> ();
71 foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups ()) {
72 if (seen.ContainsKey (group.GroupName))
74 seen.Add (group.GroupName, null);
76 Group byName = Syscall.getgrnam (group.GroupName);
77 Group byId = Syscall.getgrgid ((uint) group.GroupId);
79 Assert.IsNotNull (byName, "#TNRS: access by name");
80 Assert.IsNotNull (byId, "#TNRS: access by gid");
82 UnixGroupInfo n = new UnixGroupInfo (byName);
83 UnixGroupInfo u = new UnixGroupInfo (byId);
85 Assert.AreEqual (group, n, "#TNRS: construct by name");
86 Assert.AreEqual (group, u, "#TNRS: construct by gid");
87 Assert.AreEqual (n, u, "#TNRS: name == gid?");
91 string.Format ("#TRC: Exception constructing UnixGroupInfo: {0}",
98 public void InvalidGroups_Constructor_Name ()
100 string[] badGroups = new string[]{"i'm bad", "so am i", "does-not-exist"};
101 foreach (string u in badGroups) {
103 new UnixGroupInfo (u);
104 Assert.Fail ("#TIUCN: exception not thrown");
106 catch (ArgumentException) {
109 catch (Exception e) {
110 Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
111 "expected ArgumentException, got {0}: {1}",
112 e.GetType().FullName, e.Message));
118 public void InvalidGroups_Syscall_Name ()
120 string[] badGroups = new string[]{"i'm bad", "so am i", "does-not-exist"};
121 foreach (string u in badGroups) {
123 Group pw = Syscall.getgrnam (u);
124 Assert.IsNull (pw, "#TIUSN: invalid groups should return null!");
126 catch (Exception e) {
127 Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
128 "expected null return, got {0}: {1}",
129 e.GetType().FullName, e.Message));
135 public void Equality ()
137 Group orig = new Group ();
138 Group mod = new Group ();
139 mod.gr_name = orig.gr_name = "some name";
140 mod.gr_passwd = orig.gr_passwd = "some passwd";
141 mod.gr_gid = orig.gr_gid = 500;
142 mod.gr_mem = orig.gr_mem = new string[]{"foo", "bar"};
144 Assert.AreEqual (orig, mod, "#TE: copies should be equal");
146 mod.gr_name = "another name";
147 Assert.IsFalse (orig.Equals (mod), "#TE: changes should be reflected");