test-getusershell.c revision 291759
1/*-
2 * Copyright (c) 2006 Michael Bushkov <bushman@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: stable/10/tools/regression/lib/libc/nss/test-getusershell.c 291759 2015-12-04 09:18:12Z ngie $");
30
31#include <assert.h>
32#include <errno.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
37#include "testutil.h"
38
39enum test_methods {
40	TEST_GETUSERSHELL,
41	TEST_BUILD_SNAPSHOT
42};
43
44struct usershell {
45	char *path;
46};
47
48static int debug = 0;
49static enum test_methods method = TEST_GETUSERSHELL;
50
51DECLARE_TEST_DATA(usershell)
52DECLARE_TEST_FILE_SNAPSHOT(usershell)
53DECLARE_2PASS_TEST(usershell)
54
55static void clone_usershell(struct usershell *, struct usershell const *);
56static int compare_usershell(struct usershell *, struct usershell *, void *);
57static void free_usershell(struct usershell *);
58
59static void sdump_usershell(struct usershell *, char *, size_t);
60static void dump_usershell(struct usershell *);
61
62static int usershell_read_snapshot_func(struct usershell *, char *);
63
64static void usage(void)  __attribute__((__noreturn__));
65
66IMPLEMENT_TEST_DATA(usershell)
67IMPLEMENT_TEST_FILE_SNAPSHOT(usershell)
68IMPLEMENT_2PASS_TEST(usershell)
69
70static void
71clone_usershell(struct usershell *dest, struct usershell const *src)
72{
73	assert(dest != NULL);
74	assert(src != NULL);
75
76	if (src->path != NULL) {
77		dest->path = strdup(src->path);
78		assert(dest->path != NULL);
79	}
80}
81
82static int
83compare_usershell(struct usershell *us1, struct usershell *us2, void *mdata)
84{
85	int rv;
86
87	assert(us1 != NULL);
88	assert(us2 != NULL);
89
90	dump_usershell(us1);
91	dump_usershell(us2);
92
93	if (us1 == us2)
94		return (0);
95
96	rv = strcmp(us1->path, us2->path);
97	if (rv != 0) {
98		printf("following structures are not equal:\n");
99		dump_usershell(us1);
100		dump_usershell(us2);
101	}
102
103	return (rv);
104}
105
106static void
107free_usershell(struct usershell *us)
108{
109	free(us->path);
110}
111
112static void
113sdump_usershell(struct usershell *us, char *buffer, size_t buflen)
114{
115	snprintf(buffer, buflen, "%s", us->path);
116}
117
118static void
119dump_usershell(struct usershell *us)
120{
121	if (us != NULL) {
122		char buffer[2048];
123		sdump_usershell(us, buffer, sizeof(buffer));
124		printf("%s\n", buffer);
125	} else
126		printf("(null)\n");
127}
128
129static int
130usershell_read_snapshot_func(struct usershell *us, char *line)
131{
132	us->path = strdup(line);
133	assert(us->path != NULL);
134
135	return (0);
136}
137
138static void
139usage(void)
140{
141	(void)fprintf(stderr,
142	    "Usage: %s [-d] -s <file>\n",
143	    getprogname());
144	exit(1);
145}
146
147int
148main(int argc, char **argv)
149{
150	struct usershell_test_data td, td_snap;
151	struct usershell ushell;
152	char *snapshot_file;
153	int rv;
154	int c;
155
156	if (argc < 2)
157		usage();
158
159	rv = 0;
160	snapshot_file = NULL;
161	while ((c = getopt(argc, argv, "ds:")) != -1) {
162		switch (c) {
163		case 'd':
164			debug = 1;
165			break;
166		case 's':
167			snapshot_file = strdup(optarg);
168			break;
169		default:
170			usage();
171		}
172	}
173
174	TEST_DATA_INIT(usershell, &td, clone_usershell, free_usershell);
175	TEST_DATA_INIT(usershell, &td_snap, clone_usershell, free_usershell);
176
177	setusershell();
178	while ((ushell.path = getusershell()) != NULL) {
179		if (debug) {
180			printf("usershell found:\n");
181			dump_usershell(&ushell);
182		}
183		TEST_DATA_APPEND(usershell, &td, &ushell);
184	}
185	endusershell();
186
187
188	if (snapshot_file != NULL) {
189		if (access(snapshot_file, W_OK | R_OK) != 0) {
190			if (errno == ENOENT)
191				method = TEST_BUILD_SNAPSHOT;
192			else {
193				if (debug)
194				    printf("can't access the snapshot file %s\n",
195				    snapshot_file);
196
197				rv = -1;
198				goto fin;
199			}
200		} else {
201			rv = TEST_SNAPSHOT_FILE_READ(usershell, snapshot_file,
202				&td_snap, usershell_read_snapshot_func);
203			if (rv != 0) {
204				if (debug)
205					printf("error reading snapshot file\n");
206				goto fin;
207			}
208		}
209	}
210
211	switch (method) {
212	case TEST_GETUSERSHELL:
213		if (snapshot_file != NULL) {
214			rv = DO_2PASS_TEST(usershell, &td, &td_snap,
215				compare_usershell, NULL);
216		}
217		break;
218	case TEST_BUILD_SNAPSHOT:
219		if (snapshot_file != NULL) {
220		    rv = TEST_SNAPSHOT_FILE_WRITE(usershell, snapshot_file, &td,
221			sdump_usershell);
222		}
223		break;
224	default:
225		rv = 0;
226		break;
227	};
228
229fin:
230	TEST_DATA_DESTROY(usershell, &td_snap);
231	TEST_DATA_DESTROY(usershell, &td);
232	free(snapshot_file);
233	return (rv);
234
235}
236