Ignore machine-check MSRs
[freebsd-src/fkvm-freebsd.git] / tools / regression / lib / libc / nss / test-getusershell.c
blob20faaacefecbce028fa22198c9dc82c7a4ac2ee6
1 /*-
2 * Copyright (c) 2006 Michael Bushkov <bushman@freebsd.org>
3 * All rights repwded.
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.
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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
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"
39 enum test_methods {
40 TEST_GETUSERSHELL,
41 TEST_BUILD_SNAPSHOT
44 struct usershell {
45 char *path;
48 static int debug = 0;
49 static enum test_methods method = TEST_GETUSERSHELL;
51 DECLARE_TEST_DATA(usershell)
52 DECLARE_TEST_FILE_SNAPSHOT(usershell)
53 DECLARE_2PASS_TEST(usershell)
55 static void clone_usershell(struct usershell *, struct usershell const *);
56 static int compare_usershell(struct usershell *, struct usershell *, void *);
57 static void free_usershell(struct usershell *);
59 static void sdump_usershell(struct usershell *, char *, size_t);
60 static void dump_usershell(struct usershell *);
62 static int usershell_read_snapshot_func(struct usershell *, char *);
64 static void usage(void) __attribute__((__noreturn__));
66 IMPLEMENT_TEST_DATA(usershell)
67 IMPLEMENT_TEST_FILE_SNAPSHOT(usershell)
68 IMPLEMENT_2PASS_TEST(usershell)
70 static void
71 clone_usershell(struct usershell *dest, struct usershell const *src)
73 assert(dest != NULL);
74 assert(src != NULL);
76 if (src->path != NULL) {
77 dest->path = strdup(src->path);
78 assert(dest->path != NULL);
82 static int
83 compare_usershell(struct usershell *us1, struct usershell *us2, void *mdata)
85 int rv;
87 assert(us1 != NULL);
88 assert(us2 != NULL);
90 dump_usershell(us1);
91 dump_usershell(us2);
93 if (us1 == us2)
94 return (0);
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);
103 return (rv);
106 static void
107 free_usershell(struct usershell *us)
109 free(us->path);
112 static void
113 sdump_usershell(struct usershell *us, char *buffer, size_t buflen)
115 snprintf(buffer, buflen, "%s", us->path);
118 static void
119 dump_usershell(struct usershell *us)
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");
129 static int
130 usershell_read_snapshot_func(struct usershell *us, char *line)
132 us->path = strdup(line);
133 assert(us->path != NULL);
135 return (0);
138 static void
139 usage(void)
141 (void)fprintf(stderr,
142 "Usage: %s [-d] -s <file>\n",
143 getprogname());
144 exit(1);
148 main(int argc, char **argv)
150 struct usershell_test_data td, td_snap;
151 struct usershell ushell;
152 char *snapshot_file;
153 int rv;
154 int c;
156 if (argc < 2)
157 usage();
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();
174 TEST_DATA_INIT(usershell, &td, clone_usershell, free_usershell);
175 TEST_DATA_INIT(usershell, &td_snap, clone_usershell, free_usershell);
177 setusershell();
178 while ((ushell.path = getusershell()) != NULL) {
179 if (debug) {
180 printf("usershell found:\n");
181 dump_usershell(&ushell);
183 TEST_DATA_APPEND(usershell, &td, &ushell);
185 endusershell();
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);
197 rv = -1;
198 goto fin;
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;
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);
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);
223 break;
224 default:
225 rv = 0;
226 break;
229 fin:
230 TEST_DATA_DESTROY(usershell, &td_snap);
231 TEST_DATA_DESTROY(usershell, &td);
232 free(snapshot_file);
233 return (rv);