getusershell_test.c revision 168754
1148330Snetchild/*-
2148330Snetchild * Copyright (c) 2006 Michael Bushkov <bushman@freebsd.org>
3148330Snetchild * All rights repwded.
4148330Snetchild *
5148330Snetchild * Redistribution and use in source and binary forms, with or without
6148330Snetchild * modification, are permitted provided that the following conditions
7148330Snetchild * are met:
8148330Snetchild * 1. Redistributions of source code must retain the above copyright
9148330Snetchild *    notice, this list of conditions and the following disclaimer.
10148330Snetchild * 2. Redistributions in binary form must reproduce the above copyright
11148330Snetchild *    notice, this list of conditions and the following disclaimer in the
12148330Snetchild *    documentation and/or other materials provided with the distribution.
13148330Snetchild *
14148543Snetchild * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15148543Snetchild * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16148330Snetchild * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17167699Sdelphij * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18167699Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19167137Sbms * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20167137Sbms * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21166981Sru * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22166981Sru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23166668Sbrueffer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24166668Sbrueffer * SUCH DAMAGE.
25166389Srafan *
26166389Srafan */
27166389Srafan
28166308Sphk#include <sys/cdefs.h>
29166308Sphk__FBSDID("$FreeBSD: head/tools/regression/lib/libc/nss/test-getusershell.c 168754 2007-04-15 11:02:31Z bushman $");
30166246Speter
31166246Speter#include <assert.h>
32166246Speter#include <errno.h>
33166246Speter#include <stdio.h>
34166246Speter#include <stdlib.h>
35164796Spiso#include <string.h>
36164796Spiso#include <unistd.h>
37164796Spiso#include "testutil.h"
38164796Spiso
39164796Spisoenum test_methods {
40164796Spiso	TEST_GETUSERSHELL,
41164796Spiso	TEST_BUILD_SNAPSHOT
42164796Spiso};
43164796Spiso
44166672Sbruefferstruct usershell {
45164796Spiso	char *path;
46164796Spiso};
47164796Spiso
48164796Spisostatic int debug = 0;
49164796Spisostatic enum test_methods method = TEST_GETUSERSHELL;
50164796Spiso
51164796SpisoDECLARE_TEST_DATA(usershell)
52164796SpisoDECLARE_TEST_FILE_SNAPSHOT(usershell)
53165726SkientzleDECLARE_2PASS_TEST(usershell)
54165726Skientzle
55164610Simpstatic void clone_usershell(struct usershell *, struct usershell const *);
56164610Simpstatic int compare_usershell(struct usershell *, struct usershell *, void *);
57164537Srodrigcstatic void free_usershell(struct usershell *);
58164537Srodrigc
59164537Srodrigcstatic void sdump_usershell(struct usershell *, char *, size_t);
60164537Srodrigcstatic void dump_usershell(struct usershell *);
61164537Srodrigc
62164537Srodrigcstatic int usershell_read_snapshot_func(struct usershell *, char *);
63164537Srodrigc
64164537Srodrigcstatic void usage(void)  __attribute__((__noreturn__));
65164537Srodrigc
66164537SrodrigcIMPLEMENT_TEST_DATA(usershell)
67164537SrodrigcIMPLEMENT_TEST_FILE_SNAPSHOT(usershell)
68164537SrodrigcIMPLEMENT_2PASS_TEST(usershell)
69164537Srodrigc
70164344Sbruefferstatic void
71164344Sbruefferclone_usershell(struct usershell *dest, struct usershell const *src)
72164088Smarcel{
73164088Smarcel	assert(dest != NULL);
74164088Smarcel	assert(src != NULL);
75164088Smarcel
76163570Sru	if (src->path != NULL) {
77163570Sru		dest->path = strdup(src->path);
78162837Sdelphij		assert(dest->path != NULL);
79162837Sdelphij	}
80162780Sbms}
81162780Sbms
82162780Sbmsstatic int
83162780Sbmscompare_usershell(struct usershell *us1, struct usershell *us2, void *mdata)
84162780Sbms{
85162780Sbms	int rv;
86162780Sbms
87162780Sbms	assert(us1 != NULL);
88162780Sbms	assert(us2 != NULL);
89162598Ssimon
90162598Ssimon	dump_usershell(us1);
91162598Ssimon	dump_usershell(us2);
92162716Sdelphij
93162716Sdelphij	if (us1 == us2)
94161529Sflz		return (0);
95161529Sflz
96161529Sflz	rv = strcmp(us1->path, us2->path);
97160983Sbrooks	if (rv != 0) {
98160983Sbrooks		printf("following structures are not equal:\n");
99158687Sphk		dump_usershell(us1);
100158687Sphk		dump_usershell(us2);
101158687Sphk	}
102158687Sphk
103158687Sphk	return (rv);
104158687Sphk}
105158687Sphk
106158687Sphkstatic void
107158687Sphkfree_usershell(struct usershell *us)
108158687Sphk{
109158687Sphk	free(us->path);
110158687Sphk}
111158687Sphk
112158687Sphkstatic void
113158687Sphksdump_usershell(struct usershell *us, char *buffer, size_t buflen)
114158687Sphk{
115158687Sphk	snprintf(buffer, buflen, "%s", us->path);
116158687Sphk}
117158687Sphk
118158687Sphkstatic void
119158687Sphkdump_usershell(struct usershell *us)
120158687Sphk{
121158687Sphk	if (us != NULL) {
122158687Sphk		char buffer[2048];
123158687Sphk		sdump_usershell(us, buffer, sizeof(buffer));
124158687Sphk		printf("%s\n", buffer);
125158687Sphk	} else
126158687Sphk		printf("(null)\n");
127158687Sphk}
128158687Sphk
129158687Sphkstatic int
130158687Sphkusershell_read_snapshot_func(struct usershell *us, char *line)
131158687Sphk{
132158687Sphk	us->path = strdup(line);
133158687Sphk	assert(us->path != NULL);
134158687Sphk
135158687Sphk	return (0);
136158687Sphk}
137158687Sphk
138158687Sphkstatic void
139158687Sphkusage(void)
140158687Sphk{
141158687Sphk	(void)fprintf(stderr,
142158687Sphk	    "Usage: %s [-d] -s <file>\n",
143158687Sphk	    getprogname());
144158687Sphk	exit(1);
145158687Sphk}
146158687Sphk
147158687Sphkint
148158687Sphkmain(int argc, char **argv)
149158687Sphk{
150158687Sphk	struct usershell_test_data td, td_snap;
151158687Sphk	struct usershell ushell;
152158618Smaxim	char *snapshot_file;
153158618Smaxim	int rv;
154158618Smaxim	int c;
155158512Smlaier
156158512Smlaier	if (argc < 2)
157158512Smlaier		usage();
158158512Smlaier
159158512Smlaier	rv = 0;
160158754Smarcel	snapshot_file = NULL;
161158754Smarcel	while ((c = getopt(argc, argv, "ds:")) != -1) {
162157221Ssimon		switch (c) {
163157221Ssimon		case 'd':
164156676Sharti			debug = 1;
165156676Sharti			break;
166153662Sjhb		case 's':
167153662Sjhb			snapshot_file = strdup(optarg);
168153430Siedowse			break;
169153430Siedowse		default:
170153430Siedowse			usage();
171153430Siedowse		}
172153430Siedowse	}
173151845Syar
174151845Syar	TEST_DATA_INIT(usershell, &td, clone_usershell, free_usershell);
175151271Spjd	TEST_DATA_INIT(usershell, &td_snap, clone_usershell, free_usershell);
176151271Spjd
177162025Smatusita	setusershell();
178162025Smatusita	while ((ushell.path = getusershell()) != NULL) {
179162025Smatusita		if (debug) {
180162025Smatusita			printf("usershell found:\n");
181150676Smlaier			dump_usershell(&ushell);
182150677Smlaier		}
183150002Snetchild		TEST_DATA_APPEND(usershell, &td, &ushell);
184150002Snetchild	}
185150002Snetchild	endusershell();
186150002Snetchild
187149454Sglebius
188148808Sru	if (snapshot_file != NULL) {
189148808Sru		if (access(snapshot_file, W_OK | R_OK) != 0) {
190148825Snetchild			if (errno == ENOENT)
191148825Snetchild				method = TEST_BUILD_SNAPSHOT;
192148356Sdougb			else {
193148356Sdougb				if (debug)
194148572Snetchild				    printf("can't access the snapshot file %s\n",
195148330Snetchild				    snapshot_file);
196148330Snetchild
197148330Snetchild				rv = -1;
198148330Snetchild				goto fin;
199148572Snetchild			}
200148572Snetchild		} else {
201149105Simp			rv = TEST_SNAPSHOT_FILE_READ(usershell, snapshot_file,
202148572Snetchild				&td_snap, usershell_read_snapshot_func);
203148572Snetchild			if (rv != 0) {
204148572Snetchild				if (debug)
205148572Snetchild					printf("error reading snapshot file\n");
206148572Snetchild				goto fin;
207148572Snetchild			}
208148572Snetchild		}
209148572Snetchild	}
210148572Snetchild
211148572Snetchild	switch (method) {
212148572Snetchild	case TEST_GETUSERSHELL:
213148572Snetchild		if (snapshot_file != NULL) {
214148572Snetchild			rv = DO_2PASS_TEST(usershell, &td, &td_snap,
215148572Snetchild				compare_usershell, NULL);
216153939Snetchild		}
217153939Snetchild		break;
218153939Snetchild	case TEST_BUILD_SNAPSHOT:
219153939Snetchild		if (snapshot_file != NULL) {
220148330Snetchild		    rv = TEST_SNAPSHOT_FILE_WRITE(usershell, snapshot_file, &td,
221148330Snetchild			sdump_usershell);
222148423Sdougb		}
223148423Sdougb		break;
224148330Snetchild	default:
225148330Snetchild		rv = 0;
226148330Snetchild		break;
227148330Snetchild	};
228148572Snetchild
229148572Snetchildfin:
230148353Sdougb	TEST_DATA_DESTROY(usershell, &td_snap);
231148353Sdougb	TEST_DATA_DESTROY(usershell, &td);
232149105Simp	free(snapshot_file);
233149105Simp	return (rv);
234148572Snetchild
235148572Snetchild}
236149105Simp