2a9cfaa391e4887858b80a58766dda9ea44d29b4
[ira/wip.git] / lib / nss_wrapper / testsuite.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    local testing of the nss wrapper
5
6    Copyright (C) Guenther Deschner 2009
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "torture/torture.h"
24 #include "lib/replace/system/passwd.h"
25 #include "lib/nss_wrapper/nss_wrapper.h"
26
27 static void print_passwd(struct passwd *pwd)
28 {
29         printf("%s:%s:%lu:%lu:%s:%s:%s\n",
30                pwd->pw_name,
31                pwd->pw_passwd,
32                (unsigned long)pwd->pw_uid,
33                (unsigned long)pwd->pw_gid,
34                pwd->pw_gecos,
35                pwd->pw_dir,
36                pwd->pw_shell);
37 }
38
39
40 static bool test_nwrap_getpwnam(struct torture_context *tctx,
41                                 const char *name)
42 {
43         struct passwd *pwd;
44
45         torture_comment(tctx, "Testing getpwnam: %s\n", name);
46
47         pwd = getpwnam(name);
48         if (pwd) {
49                 print_passwd(pwd);
50         }
51
52         return pwd ? true : false;
53 }
54
55 static bool test_nwrap_getpwuid(struct torture_context *tctx,
56                                 uid_t uid)
57 {
58         struct passwd *pwd;
59
60         torture_comment(tctx, "Testing getpwuid: %lu\n", (unsigned long)uid);
61
62         pwd = getpwuid(uid);
63         if (pwd) {
64                 print_passwd(pwd);
65         }
66
67         return pwd ? true : false;
68 }
69
70 static void print_group(struct group *grp)
71 {
72         int i;
73         printf("%s:%s:%lu:",
74                grp->gr_name,
75                grp->gr_passwd,
76                (unsigned long)grp->gr_gid);
77
78         if (!grp->gr_mem[0]) {
79                 printf("\n");
80                 return;
81         }
82
83         for (i=0; grp->gr_mem[i+1]; i++) {
84                 printf("%s,", grp->gr_mem[i]);
85         }
86         printf("%s\n", grp->gr_mem[i]);
87 }
88
89 static bool test_nwrap_getgrnam(struct torture_context *tctx,
90                                 const char *name)
91 {
92         struct group *grp;
93
94         torture_comment(tctx, "Testing getgrnam: %s\n", name);
95
96         grp = getgrnam(name);
97         if (grp) {
98                 print_group(grp);
99         }
100
101         return grp ? true : false;
102 }
103
104 static bool test_nwrap_getgrgid(struct torture_context *tctx,
105                                 gid_t gid)
106 {
107         struct group *grp;
108
109         torture_comment(tctx, "Testing getgrgid: %lu\n", (unsigned long)gid);
110
111         grp = getgrgid(gid);
112         if (grp) {
113                 print_group(grp);
114         }
115
116         return grp ? true : false;
117 }
118
119
120 static bool test_nwrap_passwd(struct torture_context *tctx)
121 {
122         struct passwd *pwd;
123         const char **names = NULL;
124         uid_t *uids = NULL;
125         int num_names = 0;
126         size_t num_uids = 0;
127         int i;
128
129         torture_comment(tctx, "Testing setpwent\n");
130         setpwent();
131
132         while ((pwd = getpwent())) {
133                 torture_comment(tctx, "Testing getpwent\n");
134
135                 if (pwd) {
136                         print_passwd(pwd);
137                         add_string_to_array(tctx, pwd->pw_name, &names, &num_names);
138                         add_uid_to_array_unique(tctx, pwd->pw_uid, &uids, &num_uids);
139                 }
140         }
141
142         torture_comment(tctx, "Testing endpwent\n");
143         endpwent();
144
145         torture_assert_int_equal(tctx, num_names, num_uids, "invalid results");
146
147         for (i=0; i < num_names; i++) {
148                 torture_assert(tctx, test_nwrap_getpwnam(tctx, names[i]),
149                         "failed to call getpwnam for enumerated user");
150                 torture_assert(tctx, test_nwrap_getpwuid(tctx, uids[i]),
151                         "failed to call getpwuid for enumerated user");
152         }
153
154         return true;
155 }
156
157 static bool test_nwrap_group(struct torture_context *tctx)
158 {
159         struct group *grp;
160         const char **names = NULL;
161         gid_t *gids = NULL;
162         int num_names = 0;
163         size_t num_gids = 0;
164         int i;
165
166         torture_comment(tctx, "Testing setgrent\n");
167         setgrent();
168
169         do {
170                 torture_comment(tctx, "Testing getgrent\n");
171                 grp = getgrent();
172                 if (grp) {
173                         print_group(grp);
174                         add_string_to_array(tctx, grp->gr_name, &names, &num_names);
175                         add_gid_to_array_unique(tctx, grp->gr_gid, &gids, &num_gids);
176                 }
177         } while (grp);
178
179         torture_comment(tctx, "Testing endgrent\n");
180         endgrent();
181
182         torture_assert_int_equal(tctx, num_names, num_gids, "invalid results");
183
184         for (i=0; i < num_names; i++) {
185                 torture_assert(tctx, test_nwrap_getgrnam(tctx, names[i]),
186                         "failed to call getgrnam for enumerated user");
187                 torture_assert(tctx, test_nwrap_getgrgid(tctx, gids[i]),
188                         "failed to call getgrgid for enumerated user");
189         }
190
191         return true;
192 }
193
194 static bool test_nwrap_env(struct torture_context *tctx)
195 {
196         const char *old_pwd = getenv("NSS_WRAPPER_PASSWD");
197         const char *old_group = getenv("NSS_WRAPPER_GROUP");
198
199         if (!old_pwd || !old_group) {
200                 torture_skip(tctx, "nothing to test\n");
201                 return true;
202         }
203
204         torture_assert(tctx, test_nwrap_passwd(tctx),
205                         "failed to test users");
206         torture_assert(tctx, test_nwrap_group(tctx),
207                         "failed to test groups");
208
209         return true;
210 }
211
212 struct torture_suite *torture_local_nss_wrapper(TALLOC_CTX *mem_ctx)
213 {
214         struct torture_suite *suite = torture_suite_create(mem_ctx, "NSS-WRAPPER");
215
216         torture_suite_add_simple_test(suite, "env", test_nwrap_env);
217
218         return suite;
219 }