merge -r 61110:61111
[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 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                 // According to bug 72293, this may not work:
45                 // On systems with NIS, it is possible to have multiple users in the passwd
46                 // file with the same name, so the assertion above no longer holds.
47                 [Category ("NotWorking")]
48                 public void ReentrantConstructors ()
49                 {
50                         foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups ()) {
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                         foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups ()) {
71                                 try {
72                                         Group byName = Syscall.getgrnam (group.GroupName);
73                                         Group byId   = Syscall.getgrgid ((uint) group.GroupId);
74
75                                         Assert.IsNotNull (byName, "#TNRS: access by name");
76                                         Assert.IsNotNull (byId,   "#TNRS: access by gid");
77
78                                         UnixGroupInfo n = new UnixGroupInfo (byName);
79                                         UnixGroupInfo u = new UnixGroupInfo (byId);
80
81                                         Assert.AreEqual (group, n, "#TNRS: construct by name");
82                                         Assert.AreEqual (group, u, "#TNRS: construct by gid");
83                                         Assert.AreEqual (n, u,     "#TNRS: name == gid?");
84                                 }
85                                 catch (Exception e) {
86                                         Assert.Fail (
87                                                 string.Format ("#TRC: Exception constructing UnixGroupInfo: {0}",
88                                                         e.ToString()));
89                                 }
90                         }
91                 }
92
93                 [Test]
94                 public void InvalidGroups_Constructor_Name ()
95                 {
96                         string[] badGroups = new string[]{"i'm bad", "so am i", "does-not-exist"};
97                         foreach (string u in badGroups) {
98                                 try {
99                                         new UnixGroupInfo (u);
100                                         Assert.Fail ("#TIUCN: exception not thrown");
101                                 }
102                                 catch (ArgumentException) {
103                                         // expected
104                                 }
105                                 catch (Exception e) {
106                                         Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
107                                                                 "expected ArgumentException, got {0}: {1}",
108                                                                 e.GetType().FullName, e.Message));
109                                 }
110                         }
111                 }
112
113                 [Test]
114                 public void InvalidGroups_Syscall_Name ()
115                 {
116                         string[] badGroups = new string[]{"i'm bad", "so am i", "does-not-exist"};
117                         foreach (string u in badGroups) {
118                                 try {
119                                         Group pw = Syscall.getgrnam (u);
120                                         Assert.IsNull (pw, "#TIUSN: invalid groups should return null!");
121                                 }
122                                 catch (Exception e) {
123                                         Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
124                                                                 "expected null return, got {0}: {1}",
125                                                                 e.GetType().FullName, e.Message));
126                                 }
127                         }
128                 }
129
130                 [Test]
131                 public void Equality ()
132                 {
133                         Group orig = new Group ();
134                         Group mod  = new Group ();
135                         mod.gr_name   = orig.gr_name   = "some name";
136                         mod.gr_passwd = orig.gr_passwd = "some passwd";
137                         mod.gr_gid    = orig.gr_gid    = 500;
138                         mod.gr_mem    = orig.gr_mem    = new string[]{"foo", "bar"};
139
140                         Assert.AreEqual (orig, mod, "#TE: copies should be equal");
141
142                         mod.gr_name = "another name";
143                         Assert.IsFalse (orig.Equals (mod), "#TE: changes should be reflected");
144                 }
145         }
146 }
147