Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/lib/libc/tests/nss/getusershell_test.c
39530 views
1
/*-
2
* Copyright (c) 2006 Michael Bushkov <[email protected]>
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 <assert.h>
29
#include <errno.h>
30
#include <stdio.h>
31
#include <stdlib.h>
32
#include <string.h>
33
#include <unistd.h>
34
35
#include <atf-c.h>
36
37
#include "testutil.h"
38
39
enum test_methods {
40
TEST_GETUSERSHELL,
41
TEST_BUILD_SNAPSHOT
42
};
43
44
struct usershell {
45
char *path;
46
};
47
48
DECLARE_TEST_DATA(usershell)
49
DECLARE_TEST_FILE_SNAPSHOT(usershell)
50
DECLARE_2PASS_TEST(usershell)
51
52
static void clone_usershell(struct usershell *, struct usershell const *);
53
static int compare_usershell(struct usershell *, struct usershell *, void *);
54
static void free_usershell(struct usershell *);
55
56
static void sdump_usershell(struct usershell *, char *, size_t);
57
static void dump_usershell(struct usershell *);
58
59
IMPLEMENT_TEST_DATA(usershell)
60
IMPLEMENT_TEST_FILE_SNAPSHOT(usershell)
61
IMPLEMENT_2PASS_TEST(usershell)
62
63
static void
64
clone_usershell(struct usershell *dest, struct usershell const *src)
65
{
66
assert(dest != NULL);
67
assert(src != NULL);
68
69
if (src->path != NULL) {
70
dest->path = strdup(src->path);
71
assert(dest->path != NULL);
72
}
73
}
74
75
static int
76
compare_usershell(struct usershell *us1, struct usershell *us2,
77
void *mdata __unused)
78
{
79
int rv;
80
81
assert(us1 != NULL);
82
assert(us2 != NULL);
83
84
dump_usershell(us1);
85
dump_usershell(us2);
86
87
if (us1 == us2)
88
return (0);
89
90
rv = strcmp(us1->path, us2->path);
91
if (rv != 0) {
92
printf("following structures are not equal:\n");
93
dump_usershell(us1);
94
dump_usershell(us2);
95
}
96
97
return (rv);
98
}
99
100
static void
101
free_usershell(struct usershell *us)
102
{
103
free(us->path);
104
}
105
106
static void
107
sdump_usershell(struct usershell *us, char *buffer, size_t buflen)
108
{
109
snprintf(buffer, buflen, "%s", us->path);
110
}
111
112
static void
113
dump_usershell(struct usershell *us)
114
{
115
if (us != NULL) {
116
char buffer[2048];
117
sdump_usershell(us, buffer, sizeof(buffer));
118
printf("%s\n", buffer);
119
} else
120
printf("(null)\n");
121
}
122
123
static int
124
usershell_read_snapshot_func(struct usershell *us, char *line)
125
{
126
127
us->path = strdup(line);
128
ATF_REQUIRE(us->path != NULL);
129
130
return (0);
131
}
132
133
static int
134
run_tests(const char *snapshot_file, enum test_methods method)
135
{
136
struct usershell_test_data td, td_snap;
137
struct usershell ushell;
138
int rv;
139
140
rv = 0;
141
142
TEST_DATA_INIT(usershell, &td, clone_usershell, free_usershell);
143
TEST_DATA_INIT(usershell, &td_snap, clone_usershell, free_usershell);
144
145
setusershell();
146
while ((ushell.path = getusershell()) != NULL) {
147
printf("usershell found:\n");
148
dump_usershell(&ushell);
149
TEST_DATA_APPEND(usershell, &td, &ushell);
150
}
151
endusershell();
152
153
if (snapshot_file != NULL) {
154
if (access(snapshot_file, W_OK | R_OK) != 0) {
155
if (errno == ENOENT)
156
method = TEST_BUILD_SNAPSHOT;
157
else {
158
printf("can't access the snapshot file %s\n",
159
snapshot_file);
160
161
rv = -1;
162
goto fin;
163
}
164
} else {
165
rv = TEST_SNAPSHOT_FILE_READ(usershell, snapshot_file,
166
&td_snap, usershell_read_snapshot_func);
167
if (rv != 0) {
168
printf("error reading snapshot file\n");
169
goto fin;
170
}
171
}
172
}
173
174
switch (method) {
175
case TEST_GETUSERSHELL:
176
rv = DO_2PASS_TEST(usershell, &td, &td_snap,
177
compare_usershell, NULL);
178
break;
179
case TEST_BUILD_SNAPSHOT:
180
if (snapshot_file != NULL) {
181
rv = TEST_SNAPSHOT_FILE_WRITE(usershell, snapshot_file,
182
&td, sdump_usershell);
183
}
184
break;
185
default:
186
rv = 0;
187
break;
188
}
189
190
fin:
191
TEST_DATA_DESTROY(usershell, &td_snap);
192
TEST_DATA_DESTROY(usershell, &td);
193
194
return (rv);
195
}
196
197
#define SNAPSHOT_FILE "snapshot_usershell"
198
199
ATF_TC_WITHOUT_HEAD(getusershell_with_snapshot);
200
ATF_TC_BODY(getusershell_with_snapshot, tc)
201
{
202
203
ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
204
}
205
206
ATF_TC_WITHOUT_HEAD(getusershell_with_two_pass);
207
ATF_TC_BODY(getusershell_with_two_pass, tc)
208
{
209
210
ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
211
ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETUSERSHELL) == 0);
212
}
213
214
ATF_TP_ADD_TCS(tp)
215
{
216
217
ATF_TP_ADD_TC(tp, getusershell_with_snapshot);
218
ATF_TP_ADD_TC(tp, getusershell_with_two_pass);
219
220
return (atf_no_error());
221
}
222
223