]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - tools/regression/lib/libc/nss/test-getusershell.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / tools / regression / lib / libc / nss / test-getusershell.c
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$");
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
39 enum test_methods {
40         TEST_GETUSERSHELL,
41         TEST_BUILD_SNAPSHOT
42 };
43
44 struct usershell {
45         char *path;
46 };
47
48 static int debug = 0;
49 static enum test_methods method = TEST_GETUSERSHELL;
50
51 DECLARE_TEST_DATA(usershell)
52 DECLARE_TEST_FILE_SNAPSHOT(usershell)
53 DECLARE_2PASS_TEST(usershell)
54
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 *);
58
59 static void sdump_usershell(struct usershell *, char *, size_t);
60 static void dump_usershell(struct usershell *);
61
62 static int usershell_read_snapshot_func(struct usershell *, char *);
63
64 static void usage(void)  __attribute__((__noreturn__));
65
66 IMPLEMENT_TEST_DATA(usershell)
67 IMPLEMENT_TEST_FILE_SNAPSHOT(usershell)
68 IMPLEMENT_2PASS_TEST(usershell)
69
70 static void 
71 clone_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
82 static int 
83 compare_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
106 static void 
107 free_usershell(struct usershell *us)
108 {
109         free(us->path);
110 }
111
112 static void 
113 sdump_usershell(struct usershell *us, char *buffer, size_t buflen)
114 {
115         snprintf(buffer, buflen, "%s", us->path);
116 }
117
118 static void
119 dump_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
129 static int 
130 usershell_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
138 static void
139 usage(void)
140 {
141         (void)fprintf(stderr,
142             "Usage: %s [-d] -s <file>\n",
143             getprogname());
144         exit(1);
145 }
146
147 int
148 main(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
229 fin:
230         TEST_DATA_DESTROY(usershell, &td_snap);
231         TEST_DATA_DESTROY(usershell, &td);
232         free(snapshot_file);
233         return (rv);
234
235 }