1168754Sbushman/*-
2168754Sbushman * Copyright (c) 2006 Michael Bushkov <bushman@freebsd.org>
3251867Seadler * All rights reserved.
4168754Sbushman *
5168754Sbushman * Redistribution and use in source and binary forms, with or without
6168754Sbushman * modification, are permitted provided that the following conditions
7168754Sbushman * are met:
8168754Sbushman * 1. Redistributions of source code must retain the above copyright
9168754Sbushman *    notice, this list of conditions and the following disclaimer.
10168754Sbushman * 2. Redistributions in binary form must reproduce the above copyright
11168754Sbushman *    notice, this list of conditions and the following disclaimer in the
12168754Sbushman *    documentation and/or other materials provided with the distribution.
13168754Sbushman *
14168754Sbushman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15168754Sbushman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16168754Sbushman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17168754Sbushman * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18168754Sbushman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19168754Sbushman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20168754Sbushman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21168754Sbushman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22168754Sbushman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23168754Sbushman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24168754Sbushman * SUCH DAMAGE.
25168754Sbushman *
26168754Sbushman */
27168754Sbushman
28168754Sbushman#include <sys/cdefs.h>
29168754Sbushman__FBSDID("$FreeBSD: stable/10/lib/libc/tests/nss/getusershell_test.c 319298 2017-05-31 08:30:37Z ngie $");
30168754Sbushman
31168754Sbushman#include <assert.h>
32168754Sbushman#include <errno.h>
33168754Sbushman#include <stdio.h>
34168754Sbushman#include <stdlib.h>
35168754Sbushman#include <string.h>
36168754Sbushman#include <unistd.h>
37292323Sngie
38292323Sngie#include <atf-c.h>
39292323Sngie
40168754Sbushman#include "testutil.h"
41168754Sbushman
42168754Sbushmanenum test_methods {
43168754Sbushman	TEST_GETUSERSHELL,
44168754Sbushman	TEST_BUILD_SNAPSHOT
45168754Sbushman};
46168754Sbushman
47168754Sbushmanstruct usershell {
48168754Sbushman	char *path;
49168754Sbushman};
50168754Sbushman
51168754SbushmanDECLARE_TEST_DATA(usershell)
52168754SbushmanDECLARE_TEST_FILE_SNAPSHOT(usershell)
53168754SbushmanDECLARE_2PASS_TEST(usershell)
54168754Sbushman
55168754Sbushmanstatic void clone_usershell(struct usershell *, struct usershell const *);
56168754Sbushmanstatic int compare_usershell(struct usershell *, struct usershell *, void *);
57168754Sbushmanstatic void free_usershell(struct usershell *);
58168754Sbushman
59168754Sbushmanstatic void sdump_usershell(struct usershell *, char *, size_t);
60168754Sbushmanstatic void dump_usershell(struct usershell *);
61168754Sbushman
62168754SbushmanIMPLEMENT_TEST_DATA(usershell)
63168754SbushmanIMPLEMENT_TEST_FILE_SNAPSHOT(usershell)
64168754SbushmanIMPLEMENT_2PASS_TEST(usershell)
65168754Sbushman
66291363Sngiestatic void
67168754Sbushmanclone_usershell(struct usershell *dest, struct usershell const *src)
68168754Sbushman{
69168754Sbushman	assert(dest != NULL);
70168754Sbushman	assert(src != NULL);
71291363Sngie
72168754Sbushman	if (src->path != NULL) {
73168754Sbushman		dest->path = strdup(src->path);
74168754Sbushman		assert(dest->path != NULL);
75168754Sbushman	}
76168754Sbushman}
77168754Sbushman
78291363Sngiestatic int
79319298Sngiecompare_usershell(struct usershell *us1, struct usershell *us2,
80319298Sngie    void *mdata __unused)
81168754Sbushman{
82168754Sbushman	int rv;
83291363Sngie
84168754Sbushman	assert(us1 != NULL);
85168754Sbushman	assert(us2 != NULL);
86291363Sngie
87168754Sbushman	dump_usershell(us1);
88168754Sbushman	dump_usershell(us2);
89291363Sngie
90168754Sbushman	if (us1 == us2)
91168754Sbushman		return (0);
92168754Sbushman
93168754Sbushman	rv = strcmp(us1->path, us2->path);
94168754Sbushman	if (rv != 0) {
95168754Sbushman		printf("following structures are not equal:\n");
96168754Sbushman		dump_usershell(us1);
97168754Sbushman		dump_usershell(us2);
98168754Sbushman	}
99291363Sngie
100168754Sbushman	return (rv);
101168754Sbushman}
102168754Sbushman
103291363Sngiestatic void
104168754Sbushmanfree_usershell(struct usershell *us)
105168754Sbushman{
106168754Sbushman	free(us->path);
107168754Sbushman}
108168754Sbushman
109291363Sngiestatic void
110168754Sbushmansdump_usershell(struct usershell *us, char *buffer, size_t buflen)
111168754Sbushman{
112168754Sbushman	snprintf(buffer, buflen, "%s", us->path);
113168754Sbushman}
114168754Sbushman
115168754Sbushmanstatic void
116168754Sbushmandump_usershell(struct usershell *us)
117168754Sbushman{
118168754Sbushman	if (us != NULL) {
119168754Sbushman		char buffer[2048];
120168754Sbushman		sdump_usershell(us, buffer, sizeof(buffer));
121168754Sbushman		printf("%s\n", buffer);
122168754Sbushman	} else
123168754Sbushman		printf("(null)\n");
124168754Sbushman}
125168754Sbushman
126291363Sngiestatic int
127168754Sbushmanusershell_read_snapshot_func(struct usershell *us, char *line)
128168754Sbushman{
129292323Sngie
130168754Sbushman	us->path = strdup(line);
131292323Sngie	ATF_REQUIRE(us->path != NULL);
132291363Sngie
133168754Sbushman	return (0);
134168754Sbushman}
135168754Sbushman
136319298Sngiestatic int
137292323Sngierun_tests(const char *snapshot_file, enum test_methods method)
138168754Sbushman{
139168754Sbushman	struct usershell_test_data td, td_snap;
140168754Sbushman	struct usershell ushell;
141168754Sbushman	int rv;
142291363Sngie
143168754Sbushman	rv = 0;
144291363Sngie
145168754Sbushman	TEST_DATA_INIT(usershell, &td, clone_usershell, free_usershell);
146168754Sbushman	TEST_DATA_INIT(usershell, &td_snap, clone_usershell, free_usershell);
147291363Sngie
148168754Sbushman	setusershell();
149168754Sbushman	while ((ushell.path = getusershell()) != NULL) {
150292323Sngie		printf("usershell found:\n");
151292323Sngie		dump_usershell(&ushell);
152168754Sbushman		TEST_DATA_APPEND(usershell, &td, &ushell);
153168754Sbushman	}
154168754Sbushman	endusershell();
155291363Sngie
156168754Sbushman	if (snapshot_file != NULL) {
157291363Sngie		if (access(snapshot_file, W_OK | R_OK) != 0) {
158168754Sbushman			if (errno == ENOENT)
159168754Sbushman				method = TEST_BUILD_SNAPSHOT;
160168754Sbushman			else {
161292323Sngie				printf("can't access the snapshot file %s\n",
162168754Sbushman				    snapshot_file);
163291363Sngie
164168754Sbushman				rv = -1;
165168754Sbushman				goto fin;
166168754Sbushman			}
167168754Sbushman		} else {
168168754Sbushman			rv = TEST_SNAPSHOT_FILE_READ(usershell, snapshot_file,
169168754Sbushman				&td_snap, usershell_read_snapshot_func);
170168754Sbushman			if (rv != 0) {
171292323Sngie				printf("error reading snapshot file\n");
172168754Sbushman				goto fin;
173168754Sbushman			}
174168754Sbushman		}
175168754Sbushman	}
176291363Sngie
177168754Sbushman	switch (method) {
178168754Sbushman	case TEST_GETUSERSHELL:
179292323Sngie		rv = DO_2PASS_TEST(usershell, &td, &td_snap,
180292323Sngie			compare_usershell, NULL);
181168754Sbushman		break;
182168754Sbushman	case TEST_BUILD_SNAPSHOT:
183168754Sbushman		if (snapshot_file != NULL) {
184292323Sngie			rv = TEST_SNAPSHOT_FILE_WRITE(usershell, snapshot_file,
185292323Sngie			    &td, sdump_usershell);
186168754Sbushman		}
187168754Sbushman		break;
188168754Sbushman	default:
189168754Sbushman		rv = 0;
190168754Sbushman		break;
191292323Sngie	}
192168754Sbushman
193168754Sbushmanfin:
194168754Sbushman	TEST_DATA_DESTROY(usershell, &td_snap);
195168754Sbushman	TEST_DATA_DESTROY(usershell, &td);
196292323Sngie
197168754Sbushman	return (rv);
198292323Sngie}
199168754Sbushman
200292323Sngie#define	SNAPSHOT_FILE	"snapshot_usershell"
201292323Sngie
202292323SngieATF_TC_WITHOUT_HEAD(getusershell_with_snapshot);
203292323SngieATF_TC_BODY(getusershell_with_snapshot, tc)
204292323Sngie{
205292323Sngie
206292323Sngie	ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
207168754Sbushman}
208292323Sngie
209292323SngieATF_TC_WITHOUT_HEAD(getusershell_with_two_pass);
210292323SngieATF_TC_BODY(getusershell_with_two_pass, tc)
211292323Sngie{
212292323Sngie
213292323Sngie	ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
214292323Sngie	ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETUSERSHELL) == 0);
215292323Sngie}
216292323Sngie
217292323SngieATF_TP_ADD_TCS(tp)
218292323Sngie{
219292323Sngie
220292323Sngie	ATF_TP_ADD_TC(tp, getusershell_with_snapshot);
221292323Sngie	ATF_TP_ADD_TC(tp, getusershell_with_two_pass);
222292323Sngie
223292323Sngie	return (atf_no_error());
224292323Sngie}
225