Add Unit Tests for Mono.Posix.
[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]
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                 public void ReentrantConstructors ()
42                 {
43                         foreach (UnixGroupInfo group in UnixGroup.GetLocalGroups ()) {
44                                 try {
45                                         UnixGroupInfo byName = new UnixGroupInfo (group.GroupName);
46                                         UnixGroupInfo byId   = new UnixGroupInfo (group.GroupId);
47
48                                         Assert.AreEqual (group, byName, "#TRC: construct by name");
49                                         Assert.AreEqual (group, byId,   "#TRC: construct by gid");
50                                         Assert.AreEqual (byName, byId,  "#TRC: name == gid?");
51                                 }
52                                 catch (Exception e) {
53                                         Assert.Fail (
54                                                 string.Format ("#TRC: Exception constructing UnixGroupInfo: {0}",
55                                                         e.ToString()));
56                                 }
57                         }
58                 }
59
60                 [Test]
61                 public void NonReentrantSyscalls ()
62                 {
63                         foreach (UnixGroupInfo group in UnixGroup.GetLocalGroups ()) {
64                                 try {
65                                         Group byName = Syscall.getgrnam (group.GroupName);
66                                         Group byId   = Syscall.getgrgid (group.GroupId);
67
68                                         Assert.IsNotNull (byName, "#TNRS: access by name");
69                                         Assert.IsNotNull (byId,   "#TNRS: access by gid");
70
71                                         UnixGroupInfo n = new UnixGroupInfo (byName);
72                                         UnixGroupInfo u = new UnixGroupInfo (byId);
73
74                                         Assert.AreEqual (group, n, "#TNRS: construct by name");
75                                         Assert.AreEqual (group, u, "#TNRS: construct by gid");
76                                         Assert.AreEqual (n, u,     "#TNRS: name == gid?");
77                                 }
78                                 catch (Exception e) {
79                                         Assert.Fail (
80                                                 string.Format ("#TRC: Exception constructing UnixGroupInfo: {0}",
81                                                         e.ToString()));
82                                 }
83                         }
84                 }
85
86                 [Test]
87                 public void InvalidGroups_Constructor_Name ()
88                 {
89                         string[] badGroups = new string[]{"i'm bad", "so am i", "does-not-exist"};
90                         foreach (string u in badGroups) {
91                                 try {
92                                         new UnixGroupInfo (u);
93                                         Assert.Fail ("#TIUCN: exception not thrown");
94                                 }
95                                 catch (ArgumentException) {
96                                         // expected
97                                 }
98                                 catch (Exception e) {
99                                         Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
100                                                                 "expected ArgumentException, got {0}: {1}",
101                                                                 e.GetType().FullName, e.Message));
102                                 }
103                         }
104                 }
105
106                 [Test]
107                 public void InvalidGroups_Syscall_Name ()
108                 {
109                         string[] badGroups = new string[]{"i'm bad", "so am i", "does-not-exist"};
110                         foreach (string u in badGroups) {
111                                 try {
112                                         Group pw = Syscall.getgrnam (u);
113                                         Assert.IsNull (pw, "#TIUSN: invalid groups should return null!");
114                                 }
115                                 catch (Exception e) {
116                                         Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
117                                                                 "expected null return, got {0}: {1}",
118                                                                 e.GetType().FullName, e.Message));
119                                 }
120                         }
121                 }
122
123                 [Test]
124                 public void Equality ()
125                 {
126                         Group orig = new Group ();
127                         Group mod  = new Group ();
128                         mod.gr_name   = orig.gr_name   = "some name";
129                         mod.gr_passwd = orig.gr_passwd = "some passwd";
130                         mod.gr_gid    = orig.gr_gid    = 500;
131                         mod.gr_mem    = orig.gr_mem    = new string[]{"foo", "bar"};
132
133                         Assert.AreEqual (orig, mod, "#TE: copies should be equal");
134
135                         mod.gr_name = "another name";
136                         Assert.IsFalse (orig.Equals (mod), "#TE: changes should be reflected");
137                 }
138         }
139 }
140