summaryrefslogtreecommitdiff
path: root/lib/nss_wrapper/testsuite.c
blob: 2a9cfaa391e4887858b80a58766dda9ea44d29b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
   Unix SMB/CIFS implementation.

   local testing of the nss wrapper

   Copyright (C) Guenther Deschner 2009

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "torture/torture.h"
#include "lib/replace/system/passwd.h"
#include "lib/nss_wrapper/nss_wrapper.h"

static void print_passwd(struct passwd *pwd)
{
	printf("%s:%s:%lu:%lu:%s:%s:%s\n",
	       pwd->pw_name,
	       pwd->pw_passwd,
	       (unsigned long)pwd->pw_uid,
	       (unsigned long)pwd->pw_gid,
	       pwd->pw_gecos,
	       pwd->pw_dir,
	       pwd->pw_shell);
}


static bool test_nwrap_getpwnam(struct torture_context *tctx,
				const char *name)
{
	struct passwd *pwd;

	torture_comment(tctx, "Testing getpwnam: %s\n", name);

	pwd = getpwnam(name);
	if (pwd) {
		print_passwd(pwd);
	}

	return pwd ? true : false;
}

static bool test_nwrap_getpwuid(struct torture_context *tctx,
				uid_t uid)
{
	struct passwd *pwd;

	torture_comment(tctx, "Testing getpwuid: %lu\n", (unsigned long)uid);

	pwd = getpwuid(uid);
	if (pwd) {
		print_passwd(pwd);
	}

	return pwd ? true : false;
}

static void print_group(struct group *grp)
{
	int i;
	printf("%s:%s:%lu:",
	       grp->gr_name,
	       grp->gr_passwd,
	       (unsigned long)grp->gr_gid);

	if (!grp->gr_mem[0]) {
		printf("\n");
		return;
	}

	for (i=0; grp->gr_mem[i+1]; i++) {
		printf("%s,", grp->gr_mem[i]);
	}
	printf("%s\n", grp->gr_mem[i]);
}

static bool test_nwrap_getgrnam(struct torture_context *tctx,
				const char *name)
{
	struct group *grp;

	torture_comment(tctx, "Testing getgrnam: %s\n", name);

	grp = getgrnam(name);
	if (grp) {
		print_group(grp);
	}

	return grp ? true : false;
}

static bool test_nwrap_getgrgid(struct torture_context *tctx,
				gid_t gid)
{
	struct group *grp;

	torture_comment(tctx, "Testing getgrgid: %lu\n", (unsigned long)gid);

	grp = getgrgid(gid);
	if (grp) {
		print_group(grp);
	}

	return grp ? true : false;
}


static bool test_nwrap_passwd(struct torture_context *tctx)
{
	struct passwd *pwd;
	const char **names = NULL;
	uid_t *uids = NULL;
	int num_names = 0;
	size_t num_uids = 0;
	int i;

	torture_comment(tctx, "Testing setpwent\n");
	setpwent();

	while ((pwd = getpwent())) {
		torture_comment(tctx, "Testing getpwent\n");

		if (pwd) {
			print_passwd(pwd);
			add_string_to_array(tctx, pwd->pw_name, &names, &num_names);
			add_uid_to_array_unique(tctx, pwd->pw_uid, &uids, &num_uids);
		}
	}

	torture_comment(tctx, "Testing endpwent\n");
	endpwent();

	torture_assert_int_equal(tctx, num_names, num_uids, "invalid results");

	for (i=0; i < num_names; i++) {
		torture_assert(tctx, test_nwrap_getpwnam(tctx, names[i]),
			"failed to call getpwnam for enumerated user");
		torture_assert(tctx, test_nwrap_getpwuid(tctx, uids[i]),
			"failed to call getpwuid for enumerated user");
	}

	return true;
}

static bool test_nwrap_group(struct torture_context *tctx)
{
	struct group *grp;
	const char **names = NULL;
	gid_t *gids = NULL;
	int num_names = 0;
	size_t num_gids = 0;
	int i;

	torture_comment(tctx, "Testing setgrent\n");
	setgrent();

	do {
		torture_comment(tctx, "Testing getgrent\n");
		grp = getgrent();
		if (grp) {
			print_group(grp);
			add_string_to_array(tctx, grp->gr_name, &names, &num_names);
			add_gid_to_array_unique(tctx, grp->gr_gid, &gids, &num_gids);
		}
	} while (grp);

	torture_comment(tctx, "Testing endgrent\n");
	endgrent();

	torture_assert_int_equal(tctx, num_names, num_gids, "invalid results");

	for (i=0; i < num_names; i++) {
		torture_assert(tctx, test_nwrap_getgrnam(tctx, names[i]),
			"failed to call getgrnam for enumerated user");
		torture_assert(tctx, test_nwrap_getgrgid(tctx, gids[i]),
			"failed to call getgrgid for enumerated user");
	}

	return true;
}

static bool test_nwrap_env(struct torture_context *tctx)
{
	const char *old_pwd = getenv("NSS_WRAPPER_PASSWD");
	const char *old_group = getenv("NSS_WRAPPER_GROUP");

	if (!old_pwd || !old_group) {
		torture_skip(tctx, "nothing to test\n");
		return true;
	}

	torture_assert(tctx, test_nwrap_passwd(tctx),
			"failed to test users");
	torture_assert(tctx, test_nwrap_group(tctx),
			"failed to test groups");

	return true;
}

struct torture_suite *torture_local_nss_wrapper(TALLOC_CTX *mem_ctx)
{
	struct torture_suite *suite = torture_suite_create(mem_ctx, "NSS-WRAPPER");

	torture_suite_add_simple_test(suite, "env", test_nwrap_env);

	return suite;
}