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 [Category ("AndroidNotWorking")] // API 21 conditionally has setgrent in the NDK headers, but bionic doesn't export it
29 public void ListAllGroups_ToString ()
32 Console.WriteLine ("Listing all groups");
33 foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups ()) {
34 Console.WriteLine ("\t{0}", group);
39 string.Format ("#TLAU_TS: Exception listing local groups: {0}",
45 [Category ("AndroidNotWorking")] // API 21 conditionally has setgrent in the NDK headers, but bionic doesn't export it
46 public void ReentrantConstructors ()
48 var seen = new Dictionary<string, object> ();
49 foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups ()) {
50 if (seen.ContainsKey (group.GroupName))
52 seen.Add (group.GroupName, null);
54 UnixGroupInfo byName = new UnixGroupInfo (group.GroupName);
55 UnixGroupInfo byId = new UnixGroupInfo (group.GroupId);
57 Assert.AreEqual (group, byName, "#TRC: construct by name");
58 Assert.AreEqual (group, byId, "#TRC: construct by gid");
59 Assert.AreEqual (byName, byId, "#TRC: name == gid?");
63 string.Format ("#TRC: Exception constructing UnixGroupInfo: {0}",
70 [Category ("AndroidNotWorking")] // API 21 conditionally has setgrent in the NDK headers, but bionic doesn't export it
71 public void NonReentrantSyscalls ()
73 var seen = new Dictionary<string, object> ();
74 foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups ()) {
75 if (seen.ContainsKey (group.GroupName))
77 seen.Add (group.GroupName, null);
79 Group byName = Syscall.getgrnam (group.GroupName);
80 Group byId = Syscall.getgrgid ((uint) group.GroupId);
82 Assert.IsNotNull (byName, "#TNRS: access by name");
83 Assert.IsNotNull (byId, "#TNRS: access by gid");
85 UnixGroupInfo n = new UnixGroupInfo (byName);
86 UnixGroupInfo u = new UnixGroupInfo (byId);
88 Assert.AreEqual (group, n, "#TNRS: construct by name");
89 Assert.AreEqual (group, u, "#TNRS: construct by gid");
90 Assert.AreEqual (n, u, "#TNRS: name == gid?");
94 string.Format ("#TRC: Exception constructing UnixGroupInfo: {0}",
101 [Category ("AndroidNotWorking")] // API 21 conditionally has getgrnam_r in the NDK headers, but bionic doesn't export it
102 public void InvalidGroups_Constructor_Name ()
104 string[] badGroups = new string[]{"i'm bad", "so am i", "does-not-exist"};
105 foreach (string u in badGroups) {
107 new UnixGroupInfo (u);
108 Assert.Fail ("#TIUCN: exception not thrown");
110 catch (ArgumentException) {
113 catch (Exception e) {
114 Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
115 "expected ArgumentException, got {0}: {1}",
116 e.GetType().FullName, e.Message));
122 public void InvalidGroups_Syscall_Name ()
124 string[] badGroups = new string[]{"i'm bad", "so am i", "does-not-exist"};
125 foreach (string u in badGroups) {
127 Group pw = Syscall.getgrnam (u);
128 Assert.IsNull (pw, "#TIUSN: invalid groups should return null!");
130 catch (Exception e) {
131 Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
132 "expected null return, got {0}: {1}",
133 e.GetType().FullName, e.Message));
139 public void Equality ()
141 Group orig = new Group ();
142 Group mod = new Group ();
143 mod.gr_name = orig.gr_name = "some name";
144 mod.gr_passwd = orig.gr_passwd = "some passwd";
145 mod.gr_gid = orig.gr_gid = 500;
146 mod.gr_mem = orig.gr_mem = new string[]{"foo", "bar"};
148 Assert.AreEqual (orig, mod, "#TE: copies should be equal");
150 mod.gr_name = "another name";
151 Assert.IsFalse (orig.Equals (mod), "#TE: changes should be reflected");