2005-05-21 Ben Maurer <bmaurer@ximian.com>
[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.Configuration;
14 using System.Diagnostics;
15 using System.Xml;
16
17 using Mono.Unix;
18
19 namespace MonoTests.Mono.Unix {
20
21         [TestFixture, Category ("NotDotNet")]
22         public class UnixGroupTest
23         {
24                 [Test]
25                 public void ListAllGroups_ToString ()
26                 {
27                         try {
28                                 Console.WriteLine ("Listing all groups");
29                                 foreach (UnixGroupInfo group in UnixGroup.GetLocalGroups ()) {
30                                         Console.WriteLine ("\t{0}", group);
31                                 }
32                         }
33                         catch (Exception e) {
34                                 Assert.Fail (
35                                                 string.Format ("#TLAU_TS: Exception listing local groups: {0}",
36                                                         e.ToString()));
37                         }
38                 }
39
40                 [Test]
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 ()
46                 {
47                         foreach (UnixGroupInfo group in UnixGroup.GetLocalGroups ()) {
48                                 try {
49                                         UnixGroupInfo byName = new UnixGroupInfo (group.GroupName);
50                                         UnixGroupInfo byId   = new UnixGroupInfo (group.GroupId);
51
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?");
55                                 }
56                                 catch (Exception e) {
57                                         Assert.Fail (
58                                                 string.Format ("#TRC: Exception constructing UnixGroupInfo: {0}",
59                                                         e.ToString()));
60                                 }
61                         }
62                 }
63
64                 [Test]
65                 public void NonReentrantSyscalls ()
66                 {
67                         foreach (UnixGroupInfo group in UnixGroup.GetLocalGroups ()) {
68                                 try {
69                                         Group byName = Syscall.getgrnam (group.GroupName);
70                                         Group byId   = Syscall.getgrgid (group.GroupId);
71
72                                         Assert.IsNotNull (byName, "#TNRS: access by name");
73                                         Assert.IsNotNull (byId,   "#TNRS: access by gid");
74
75                                         UnixGroupInfo n = new UnixGroupInfo (byName);
76                                         UnixGroupInfo u = new UnixGroupInfo (byId);
77
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?");
81                                 }
82                                 catch (Exception e) {
83                                         Assert.Fail (
84                                                 string.Format ("#TRC: Exception constructing UnixGroupInfo: {0}",
85                                                         e.ToString()));
86                                 }
87                         }
88                 }
89
90                 [Test]
91                 public void InvalidGroups_Constructor_Name ()
92                 {
93                         string[] badGroups = new string[]{"i'm bad", "so am i", "does-not-exist"};
94                         foreach (string u in badGroups) {
95                                 try {
96                                         new UnixGroupInfo (u);
97                                         Assert.Fail ("#TIUCN: exception not thrown");
98                                 }
99                                 catch (ArgumentException) {
100                                         // expected
101                                 }
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));
106                                 }
107                         }
108                 }
109
110                 [Test]
111                 public void InvalidGroups_Syscall_Name ()
112                 {
113                         string[] badGroups = new string[]{"i'm bad", "so am i", "does-not-exist"};
114                         foreach (string u in badGroups) {
115                                 try {
116                                         Group pw = Syscall.getgrnam (u);
117                                         Assert.IsNull (pw, "#TIUSN: invalid groups should return null!");
118                                 }
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));
123                                 }
124                         }
125                 }
126
127                 [Test]
128                 public void Equality ()
129                 {
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"};
136
137                         Assert.AreEqual (orig, mod, "#TE: copies should be equal");
138
139                         mod.gr_name = "another name";
140                         Assert.IsFalse (orig.Equals (mod), "#TE: changes should be reflected");
141                 }
142         }
143 }
144