System.Drawing: added email to icon and test file headers
[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
16 using Mono.Unix;
17
18 using Group = Mono.Unix.Native.Group;
19 using Syscall = Mono.Unix.Native.Syscall;
20
21 namespace MonoTests.Mono.Unix {
22
23         [TestFixture, Category ("NotDotNet")]
24         public class UnixGroupTest
25         {
26                 [Test]
27                 public void ListAllGroups_ToString ()
28                 {
29                         try {
30                                 Console.WriteLine ("Listing all groups");
31                                 foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups ()) {
32                                         Console.WriteLine ("\t{0}", group);
33                                 }
34                         }
35                         catch (Exception e) {
36                                 Assert.Fail (
37                                                 string.Format ("#TLAU_TS: Exception listing local groups: {0}",
38                                                         e.ToString()));
39                         }
40                 }
41
42                 [Test]
43                 // According to bug 72293, this may not work:
44                 // On systems with NIS, it is possible to have multiple users in the passwd
45                 // file with the same name, so the assertion above no longer holds.
46                 [Category ("NotWorking")]
47                 public void ReentrantConstructors ()
48                 {
49                         foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups ()) {
50                                 try {
51                                         UnixGroupInfo byName = new UnixGroupInfo (group.GroupName);
52                                         UnixGroupInfo byId   = new UnixGroupInfo (group.GroupId);
53
54                                         Assert.AreEqual (group, byName, "#TRC: construct by name");
55                                         Assert.AreEqual (group, byId,   "#TRC: construct by gid");
56                                         Assert.AreEqual (byName, byId,  "#TRC: name == gid?");
57                                 }
58                                 catch (Exception e) {
59                                         Assert.Fail (
60                                                 string.Format ("#TRC: Exception constructing UnixGroupInfo: {0}",
61                                                         e.ToString()));
62                                 }
63                         }
64                 }
65
66                 [Test]
67                 public void NonReentrantSyscalls ()
68                 {
69                         foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups ()) {
70                                 try {
71                                         Group byName = Syscall.getgrnam (group.GroupName);
72                                         Group byId   = Syscall.getgrgid ((uint) group.GroupId);
73
74                                         Assert.IsNotNull (byName, "#TNRS: access by name");
75                                         Assert.IsNotNull (byId,   "#TNRS: access by gid");
76
77                                         UnixGroupInfo n = new UnixGroupInfo (byName);
78                                         UnixGroupInfo u = new UnixGroupInfo (byId);
79
80                                         Assert.AreEqual (group, n, "#TNRS: construct by name");
81                                         Assert.AreEqual (group, u, "#TNRS: construct by gid");
82                                         Assert.AreEqual (n, u,     "#TNRS: name == gid?");
83                                 }
84                                 catch (Exception e) {
85                                         Assert.Fail (
86                                                 string.Format ("#TRC: Exception constructing UnixGroupInfo: {0}",
87                                                         e.ToString()));
88                                 }
89                         }
90                 }
91
92                 [Test]
93                 public void InvalidGroups_Constructor_Name ()
94                 {
95                         string[] badGroups = new string[]{"i'm bad", "so am i", "does-not-exist"};
96                         foreach (string u in badGroups) {
97                                 try {
98                                         new UnixGroupInfo (u);
99                                         Assert.Fail ("#TIUCN: exception not thrown");
100                                 }
101                                 catch (ArgumentException) {
102                                         // expected
103                                 }
104                                 catch (Exception e) {
105                                         Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
106                                                                 "expected ArgumentException, got {0}: {1}",
107                                                                 e.GetType().FullName, e.Message));
108                                 }
109                         }
110                 }
111
112                 [Test]
113                 public void InvalidGroups_Syscall_Name ()
114                 {
115                         string[] badGroups = new string[]{"i'm bad", "so am i", "does-not-exist"};
116                         foreach (string u in badGroups) {
117                                 try {
118                                         Group pw = Syscall.getgrnam (u);
119                                         Assert.IsNull (pw, "#TIUSN: invalid groups should return null!");
120                                 }
121                                 catch (Exception e) {
122                                         Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
123                                                                 "expected null return, got {0}: {1}",
124                                                                 e.GetType().FullName, e.Message));
125                                 }
126                         }
127                 }
128
129                 [Test]
130                 public void Equality ()
131                 {
132                         Group orig = new Group ();
133                         Group mod  = new Group ();
134                         mod.gr_name   = orig.gr_name   = "some name";
135                         mod.gr_passwd = orig.gr_passwd = "some passwd";
136                         mod.gr_gid    = orig.gr_gid    = 500;
137                         mod.gr_mem    = orig.gr_mem    = new string[]{"foo", "bar"};
138
139                         Assert.AreEqual (orig, mod, "#TE: copies should be equal");
140
141                         mod.gr_name = "another name";
142                         Assert.IsFalse (orig.Equals (mod), "#TE: changes should be reflected");
143                 }
144         }
145 }
146