Merge pull request #273 from joncham/bug-getpid
[mono.git] / mcs / class / Mono.Posix / Test / Mono.Unix / UnixGroupTest.cs
1 //
2 // UnixGroupTest.cs:
3 //      NUnit Test Cases for Mono.Unix.UnixGroup
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.Collections.Generic;
14 using System.Configuration;
15 using System.Diagnostics;
16
17 using Mono.Unix;
18
19 using Group = Mono.Unix.Native.Group;
20 using Syscall = Mono.Unix.Native.Syscall;
21
22 namespace MonoTests.Mono.Unix {
23
24         [TestFixture, Category ("NotDotNet")]
25         public class UnixGroupTest
26         {
27                 [Test]
28                 public void ListAllGroups_ToString ()
29                 {
30                         try {
31                                 Console.WriteLine ("Listing all groups");
32                                 foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups ()) {
33                                         Console.WriteLine ("\t{0}", group);
34                                 }
35                         }
36                         catch (Exception e) {
37                                 Assert.Fail (
38                                                 string.Format ("#TLAU_TS: Exception listing local groups: {0}",
39                                                         e.ToString()));
40                         }
41                 }
42
43                 [Test]
44                 public void ReentrantConstructors ()
45                 {
46                         var seen = new Dictionary<string, object> ();
47                         foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups ()) {
48                                 if (seen.ContainsKey (group.GroupName))
49                                         continue;
50                                 seen.Add (group.GroupName, null);
51                                 try {
52                                         UnixGroupInfo byName = new UnixGroupInfo (group.GroupName);
53                                         UnixGroupInfo byId   = new UnixGroupInfo (group.GroupId);
54
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?");
58                                 }
59                                 catch (Exception e) {
60                                         Assert.Fail (
61                                                 string.Format ("#TRC: Exception constructing UnixGroupInfo: {0}",
62                                                         e.ToString()));
63                                 }
64                         }
65                 }
66
67                 [Test]
68                 public void NonReentrantSyscalls ()
69                 {
70                         var seen = new Dictionary<string, object> ();
71                         foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups ()) {
72                                 if (seen.ContainsKey (group.GroupName))
73                                         continue;
74                                 seen.Add (group.GroupName, null);
75                                 try {
76                                         Group byName = Syscall.getgrnam (group.GroupName);
77                                         Group byId   = Syscall.getgrgid ((uint) group.GroupId);
78
79                                         Assert.IsNotNull (byName, "#TNRS: access by name");
80                                         Assert.IsNotNull (byId,   "#TNRS: access by gid");
81
82                                         UnixGroupInfo n = new UnixGroupInfo (byName);
83                                         UnixGroupInfo u = new UnixGroupInfo (byId);
84
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?");
88                                 }
89                                 catch (Exception e) {
90                                         Assert.Fail (
91                                                 string.Format ("#TRC: Exception constructing UnixGroupInfo: {0}",
92                                                         e.ToString()));
93                                 }
94                         }
95                 }
96
97                 [Test]
98                 public void InvalidGroups_Constructor_Name ()
99                 {
100                         string[] badGroups = new string[]{"i'm bad", "so am i", "does-not-exist"};
101                         foreach (string u in badGroups) {
102                                 try {
103                                         new UnixGroupInfo (u);
104                                         Assert.Fail ("#TIUCN: exception not thrown");
105                                 }
106                                 catch (ArgumentException) {
107                                         // expected
108                                 }
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));
113                                 }
114                         }
115                 }
116
117                 [Test]
118                 public void InvalidGroups_Syscall_Name ()
119                 {
120                         string[] badGroups = new string[]{"i'm bad", "so am i", "does-not-exist"};
121                         foreach (string u in badGroups) {
122                                 try {
123                                         Group pw = Syscall.getgrnam (u);
124                                         Assert.IsNull (pw, "#TIUSN: invalid groups should return null!");
125                                 }
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));
130                                 }
131                         }
132                 }
133
134                 [Test]
135                 public void Equality ()
136                 {
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"};
143
144                         Assert.AreEqual (orig, mod, "#TE: copies should be equal");
145
146                         mod.gr_name = "another name";
147                         Assert.IsFalse (orig.Equals (mod), "#TE: changes should be reflected");
148                 }
149         }
150 }
151