column name and ordinal fix...tested on 10.1
[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                 [Category ("AndroidNotWorking")] // API 21 conditionally has setgrent in the NDK headers, but bionic doesn't export it
29                 public void ListAllGroups_ToString ()
30                 {
31                         try {
32                                 Console.WriteLine ("Listing all groups");
33                                 foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups ()) {
34                                         Console.WriteLine ("\t{0}", group);
35                                 }
36                         }
37                         catch (Exception e) {
38                                 Assert.Fail (
39                                                 string.Format ("#TLAU_TS: Exception listing local groups: {0}",
40                                                         e.ToString()));
41                         }
42                 }
43
44                 [Test]
45                 [Category ("AndroidNotWorking")] // API 21 conditionally has setgrent in the NDK headers, but bionic doesn't export it
46                 public void ReentrantConstructors ()
47                 {
48                         var seen = new Dictionary<string, object> ();
49                         foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups ()) {
50                                 if (seen.ContainsKey (group.GroupName))
51                                         continue;
52                                 seen.Add (group.GroupName, null);
53                                 try {
54                                         UnixGroupInfo byName = new UnixGroupInfo (group.GroupName);
55                                         UnixGroupInfo byId   = new UnixGroupInfo (group.GroupId);
56
57                                         Assert.AreEqual (group, byName, "#TRC: construct by name");
58                                         Assert.AreEqual (group, byId,   "#TRC: construct by gid");
59                                         Assert.AreEqual (byName, byId,  "#TRC: name == gid?");
60                                 }
61                                 catch (Exception e) {
62                                         Assert.Fail (
63                                                 string.Format ("#TRC: Exception constructing UnixGroupInfo: {0}",
64                                                         e.ToString()));
65                                 }
66                         }
67                 }
68
69                 [Test]
70                 [Category ("AndroidNotWorking")] // API 21 conditionally has setgrent in the NDK headers, but bionic doesn't export it
71                 public void NonReentrantSyscalls ()
72                 {
73                         var seen = new Dictionary<string, object> ();
74                         foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups ()) {
75                                 if (seen.ContainsKey (group.GroupName))
76                                         continue;
77                                 seen.Add (group.GroupName, null);
78                                 try {
79                                         Group byName = Syscall.getgrnam (group.GroupName);
80                                         Group byId   = Syscall.getgrgid ((uint) group.GroupId);
81
82                                         Assert.IsNotNull (byName, "#TNRS: access by name");
83                                         Assert.IsNotNull (byId,   "#TNRS: access by gid");
84
85                                         UnixGroupInfo n = new UnixGroupInfo (byName);
86                                         UnixGroupInfo u = new UnixGroupInfo (byId);
87
88                                         Assert.AreEqual (group, n, "#TNRS: construct by name");
89                                         Assert.AreEqual (group, u, "#TNRS: construct by gid");
90                                         Assert.AreEqual (n, u,     "#TNRS: name == gid?");
91                                 }
92                                 catch (Exception e) {
93                                         Assert.Fail (
94                                                 string.Format ("#TRC: Exception constructing UnixGroupInfo: {0}",
95                                                         e.ToString()));
96                                 }
97                         }
98                 }
99
100                 [Test]
101                 [Category ("AndroidNotWorking")] // API 21 conditionally has getgrnam_r in the NDK headers, but bionic doesn't export it
102                 public void InvalidGroups_Constructor_Name ()
103                 {
104                         string[] badGroups = new string[]{"i'm bad", "so am i", "does-not-exist"};
105                         foreach (string u in badGroups) {
106                                 try {
107                                         new UnixGroupInfo (u);
108                                         Assert.Fail ("#TIUCN: exception not thrown");
109                                 }
110                                 catch (ArgumentException) {
111                                         // expected
112                                 }
113                                 catch (Exception e) {
114                                         Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
115                                                                 "expected ArgumentException, got {0}: {1}",
116                                                                 e.GetType().FullName, e.Message));
117                                 }
118                         }
119                 }
120
121                 [Test]
122                 public void InvalidGroups_Syscall_Name ()
123                 {
124                         string[] badGroups = new string[]{"i'm bad", "so am i", "does-not-exist"};
125                         foreach (string u in badGroups) {
126                                 try {
127                                         Group pw = Syscall.getgrnam (u);
128                                         Assert.IsNull (pw, "#TIUSN: invalid groups should return null!");
129                                 }
130                                 catch (Exception e) {
131                                         Assert.Fail (string.Format ("#TIUCN: invalid exception thrown: " +
132                                                                 "expected null return, got {0}: {1}",
133                                                                 e.GetType().FullName, e.Message));
134                                 }
135                         }
136                 }
137
138                 [Test]
139                 public void Equality ()
140                 {
141                         Group orig = new Group ();
142                         Group mod  = new Group ();
143                         mod.gr_name   = orig.gr_name   = "some name";
144                         mod.gr_passwd = orig.gr_passwd = "some passwd";
145                         mod.gr_gid    = orig.gr_gid    = 500;
146                         mod.gr_mem    = orig.gr_mem    = new string[]{"foo", "bar"};
147
148                         Assert.AreEqual (orig, mod, "#TE: copies should be equal");
149
150                         mod.gr_name = "another name";
151                         Assert.IsFalse (orig.Equals (mod), "#TE: changes should be reflected");
152                 }
153         }
154 }
155