turns printfs back on
[freebsd-src/fkvm-freebsd.git] / tools / regression / lib / libc / nss / test-getrpc.c
blobdf0bc839a6f158483c790ff75a98201085137bca
1 /*-
2 * Copyright (c) 2006 Michael Bushkov <bushman@freebsd.org>
3 * All rights repeed.
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 <arpa/inet.h>
32 #include <rpc/rpc.h>
33 #include <assert.h>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stringlist.h>
39 #include <unistd.h>
40 #include "testutil.h"
42 enum test_methods {
43 TEST_GETRPCENT,
44 TEST_GETRPCBYNAME,
45 TEST_GETRPCBYNUMBER,
46 TEST_GETRPCENT_2PASS,
47 TEST_BUILD_SNAPSHOT
50 static int debug = 0;
51 static enum test_methods method = TEST_BUILD_SNAPSHOT;
53 DECLARE_TEST_DATA(rpcent)
54 DECLARE_TEST_FILE_SNAPSHOT(rpcent)
55 DECLARE_1PASS_TEST(rpcent)
56 DECLARE_2PASS_TEST(rpcent)
58 static void clone_rpcent(struct rpcent *, struct rpcent const *);
59 static int compare_rpcent(struct rpcent *, struct rpcent *, void *);
60 static void dump_rpcent(struct rpcent *);
61 static void free_rpcent(struct rpcent *);
63 static void sdump_rpcent(struct rpcent *, char *, size_t);
64 static int rpcent_read_snapshot_func(struct rpcent *, char *);
66 static int rpcent_check_ambiguity(struct rpcent_test_data *,
67 struct rpcent *);
68 static int rpcent_fill_test_data(struct rpcent_test_data *);
69 static int rpcent_test_correctness(struct rpcent *, void *);
70 static int rpcent_test_getrpcbyname(struct rpcent *, void *);
71 static int rpcent_test_getrpcbynumber(struct rpcent *, void *);
72 static int rpcent_test_getrpcent(struct rpcent *, void *);
74 static void usage(void) __attribute__((__noreturn__));
76 IMPLEMENT_TEST_DATA(rpcent)
77 IMPLEMENT_TEST_FILE_SNAPSHOT(rpcent)
78 IMPLEMENT_1PASS_TEST(rpcent)
79 IMPLEMENT_2PASS_TEST(rpcent)
81 static void
82 clone_rpcent(struct rpcent *dest, struct rpcent const *src)
84 assert(dest != NULL);
85 assert(src != NULL);
87 char **cp;
88 int aliases_num;
90 memset(dest, 0, sizeof(struct rpcent));
92 if (src->r_name != NULL) {
93 dest->r_name = strdup(src->r_name);
94 assert(dest->r_name != NULL);
97 dest->r_number = src->r_number;
99 if (src->r_aliases != NULL) {
100 aliases_num = 0;
101 for (cp = src->r_aliases; *cp; ++cp)
102 ++aliases_num;
104 dest->r_aliases = (char **)malloc((aliases_num+1) * (sizeof(char *)));
105 assert(dest->r_aliases != NULL);
106 memset(dest->r_aliases, 0, (aliases_num+1) * (sizeof(char *)));
108 for (cp = src->r_aliases; *cp; ++cp) {
109 dest->r_aliases[cp - src->r_aliases] = strdup(*cp);
110 assert(dest->r_aliases[cp - src->r_aliases] != NULL);
115 static void
116 free_rpcent(struct rpcent *rpc)
118 char **cp;
120 assert(rpc != NULL);
122 free(rpc->r_name);
124 for (cp = rpc->r_aliases; *cp; ++cp)
125 free(*cp);
126 free(rpc->r_aliases);
129 static int
130 compare_rpcent(struct rpcent *rpc1, struct rpcent *rpc2, void *mdata)
132 char **c1, **c2;
134 if (rpc1 == rpc2)
135 return 0;
137 if ((rpc1 == NULL) || (rpc2 == NULL))
138 goto errfin;
140 if ((strcmp(rpc1->r_name, rpc2->r_name) != 0) ||
141 (rpc1->r_number != rpc2->r_number))
142 goto errfin;
144 c1 = rpc1->r_aliases;
145 c2 = rpc2->r_aliases;
147 if ((rpc1->r_aliases == NULL) || (rpc2->r_aliases == NULL))
148 goto errfin;
150 for (;*c1 && *c2; ++c1, ++c2)
151 if (strcmp(*c1, *c2) != 0)
152 goto errfin;
154 if ((*c1 != '\0') || (*c2 != '\0'))
155 goto errfin;
157 return 0;
159 errfin:
160 if ((debug) && (mdata == NULL)) {
161 printf("following structures are not equal:\n");
162 dump_rpcent(rpc1);
163 dump_rpcent(rpc2);
166 return (-1);
169 static void
170 sdump_rpcent(struct rpcent *rpc, char *buffer, size_t buflen)
172 char **cp;
173 int written;
175 written = snprintf(buffer, buflen, "%s %d",
176 rpc->r_name, rpc->r_number);
177 buffer += written;
178 if (written > buflen)
179 return;
180 buflen -= written;
182 if (rpc->r_aliases != NULL) {
183 if (*(rpc->r_aliases) != '\0') {
184 for (cp = rpc->r_aliases; *cp; ++cp) {
185 written = snprintf(buffer, buflen, " %s",*cp);
186 buffer += written;
187 if (written > buflen)
188 return;
189 buflen -= written;
191 if (buflen == 0)
192 return;
194 } else
195 snprintf(buffer, buflen, " noaliases");
196 } else
197 snprintf(buffer, buflen, " (null)");
200 static int
201 rpcent_read_snapshot_func(struct rpcent *rpc, char *line)
203 StringList *sl;
204 char *s, *ps, *ts;
205 int i;
207 if (debug)
208 printf("1 line read from snapshot:\n%s\n", line);
210 i = 0;
211 sl = NULL;
212 ps = line;
213 memset(rpc, 0, sizeof(struct rpcent));
214 while ( (s = strsep(&ps, " ")) != NULL) {
215 switch (i) {
216 case 0:
217 rpc->r_name = strdup(s);
218 assert(rpc->r_name != NULL);
219 break;
221 case 1:
222 rpc->r_number = (int)strtol(s, &ts, 10);
223 if (*ts != '\0') {
224 free(rpc->r_name);
225 return (-1);
227 break;
229 default:
230 if (sl == NULL) {
231 if (strcmp(s, "(null)") == 0)
232 return (0);
234 sl = sl_init();
235 assert(sl != NULL);
237 if (strcmp(s, "noaliases") != 0) {
238 ts = strdup(s);
239 assert(ts != NULL);
240 sl_add(sl, ts);
242 } else {
243 ts = strdup(s);
244 assert(ts != NULL);
245 sl_add(sl, ts);
247 break;
249 ++i;
252 if (i < 3) {
253 free(rpc->r_name);
254 memset(rpc, 0, sizeof(struct rpcent));
255 return (-1);
258 sl_add(sl, NULL);
259 rpc->r_aliases = sl->sl_str;
261 /* NOTE: is it a dirty hack or not? */
262 free(sl);
263 return (0);
266 static void
267 dump_rpcent(struct rpcent *result)
269 if (result != NULL) {
270 char buffer[1024];
271 sdump_rpcent(result, buffer, sizeof(buffer));
272 printf("%s\n", buffer);
273 } else
274 printf("(null)\n");
277 static int
278 rpcent_fill_test_data(struct rpcent_test_data *td)
280 struct rpcent *rpc;
282 setrpcent(1);
283 while ((rpc = getrpcent()) != NULL) {
284 if (rpcent_test_correctness(rpc, NULL) == 0)
285 TEST_DATA_APPEND(rpcent, td, rpc);
286 else
287 return (-1);
289 endrpcent();
291 return (0);
294 static int
295 rpcent_test_correctness(struct rpcent *rpc, void *mdata)
297 if (debug) {
298 printf("testing correctness with the following data:\n");
299 dump_rpcent(rpc);
302 if (rpc == NULL)
303 goto errfin;
305 if (rpc->r_name == NULL)
306 goto errfin;
308 if (rpc->r_number < 0)
309 goto errfin;
311 if (rpc->r_aliases == NULL)
312 goto errfin;
314 if (debug)
315 printf("correct\n");
317 return (0);
318 errfin:
319 if (debug)
320 printf("incorrect\n");
322 return (-1);
325 /* rpcent_check_ambiguity() is needed when one port+rpc is associated with
326 * more than one peice (these cases are usually marked as PROBLEM in
327 * /etc/peices. This functions is needed also when one peice+rpc is
328 * associated with several ports. We have to check all the rpcent structures
329 * to make sure that rpc really exists and correct */
330 static int
331 rpcent_check_ambiguity(struct rpcent_test_data *td, struct rpcent *rpc)
334 return (TEST_DATA_FIND(rpcent, td, rpc, compare_rpcent,
335 NULL) != NULL ? 0 : -1);
338 static int
339 rpcent_test_getrpcbyname(struct rpcent *rpc_model, void *mdata)
341 char **alias;
342 struct rpcent *rpc;
344 if (debug) {
345 printf("testing getrpcbyname() with the following data:\n");
346 dump_rpcent(rpc_model);
349 rpc = getrpcbyname(rpc_model->r_name);
350 if (rpcent_test_correctness(rpc, NULL) != 0)
351 goto errfin;
353 if ((compare_rpcent(rpc, rpc_model, NULL) != 0) &&
354 (rpcent_check_ambiguity((struct rpcent_test_data *)mdata, rpc)
355 !=0))
356 goto errfin;
358 for (alias = rpc_model->r_aliases; *alias; ++alias) {
359 rpc = getrpcbyname(*alias);
361 if (rpcent_test_correctness(rpc, NULL) != 0)
362 goto errfin;
364 if ((compare_rpcent(rpc, rpc_model, NULL) != 0) &&
365 (rpcent_check_ambiguity(
366 (struct rpcent_test_data *)mdata, rpc) != 0))
367 goto errfin;
370 if (debug)
371 printf("ok\n");
372 return (0);
374 errfin:
375 if (debug)
376 printf("not ok\n");
378 return (-1);
381 static int
382 rpcent_test_getrpcbynumber(struct rpcent *rpc_model, void *mdata)
384 struct rpcent *rpc;
386 if (debug) {
387 printf("testing getrpcbyport() with the following data...\n");
388 dump_rpcent(rpc_model);
391 rpc = getrpcbynumber(rpc_model->r_number);
392 if ((rpcent_test_correctness(rpc, NULL) != 0) ||
393 ((compare_rpcent(rpc, rpc_model, NULL) != 0) &&
394 (rpcent_check_ambiguity((struct rpcent_test_data *)mdata, rpc)
395 != 0))) {
396 if (debug)
397 printf("not ok\n");
398 return (-1);
399 } else {
400 if (debug)
401 printf("ok\n");
402 return (0);
406 static int
407 rpcent_test_getrpcent(struct rpcent *rpc, void *mdata)
409 /* Only correctness can be checked when doing 1-pass test for
410 * getrpcent(). */
411 return (rpcent_test_correctness(rpc, NULL));
414 static void
415 usage(void)
417 (void)fprintf(stderr,
418 "Usage: %s -nve2 [-d] [-s <file>]\n",
419 getprogname());
420 exit(1);
424 main(int argc, char **argv)
426 struct rpcent_test_data td, td_snap, td_2pass;
427 char *snapshot_file;
428 int rv;
429 int c;
431 if (argc < 2)
432 usage();
434 snapshot_file = NULL;
435 while ((c = getopt(argc, argv, "nve2ds:")) != -1)
436 switch (c) {
437 case 'd':
438 debug++;
439 break;
440 case 'n':
441 method = TEST_GETRPCBYNAME;
442 break;
443 case 'v':
444 method = TEST_GETRPCBYNUMBER;
445 break;
446 case 'e':
447 method = TEST_GETRPCENT;
448 break;
449 case '2':
450 method = TEST_GETRPCENT_2PASS;
451 break;
452 case 's':
453 snapshot_file = strdup(optarg);
454 break;
455 default:
456 usage();
459 TEST_DATA_INIT(rpcent, &td, clone_rpcent, free_rpcent);
460 TEST_DATA_INIT(rpcent, &td_snap, clone_rpcent, free_rpcent);
461 if (snapshot_file != NULL) {
462 if (access(snapshot_file, W_OK | R_OK) != 0) {
463 if (errno == ENOENT)
464 method = TEST_BUILD_SNAPSHOT;
465 else {
466 if (debug)
467 printf("can't access the file %s\n",
468 snapshot_file);
470 rv = -1;
471 goto fin;
473 } else {
474 if (method == TEST_BUILD_SNAPSHOT) {
475 rv = 0;
476 goto fin;
479 TEST_SNAPSHOT_FILE_READ(rpcent, snapshot_file,
480 &td_snap, rpcent_read_snapshot_func);
484 rv = rpcent_fill_test_data(&td);
485 if (rv == -1)
486 return (-1);
487 switch (method) {
488 case TEST_GETRPCBYNAME:
489 if (snapshot_file == NULL)
490 rv = DO_1PASS_TEST(rpcent, &td,
491 rpcent_test_getrpcbyname, (void *)&td);
492 else
493 rv = DO_1PASS_TEST(rpcent, &td_snap,
494 rpcent_test_getrpcbyname, (void *)&td_snap);
495 break;
496 case TEST_GETRPCBYNUMBER:
497 if (snapshot_file == NULL)
498 rv = DO_1PASS_TEST(rpcent, &td,
499 rpcent_test_getrpcbynumber, (void *)&td);
500 else
501 rv = DO_1PASS_TEST(rpcent, &td_snap,
502 rpcent_test_getrpcbynumber, (void *)&td_snap);
503 break;
504 case TEST_GETRPCENT:
505 if (snapshot_file == NULL)
506 rv = DO_1PASS_TEST(rpcent, &td, rpcent_test_getrpcent,
507 (void *)&td);
508 else
509 rv = DO_2PASS_TEST(rpcent, &td, &td_snap,
510 compare_rpcent, NULL);
511 break;
512 case TEST_GETRPCENT_2PASS:
513 TEST_DATA_INIT(rpcent, &td_2pass, clone_rpcent, free_rpcent);
514 rv = rpcent_fill_test_data(&td_2pass);
515 if (rv != -1)
516 rv = DO_2PASS_TEST(rpcent, &td, &td_2pass,
517 compare_rpcent, NULL);
518 TEST_DATA_DESTROY(rpcent, &td_2pass);
519 break;
520 case TEST_BUILD_SNAPSHOT:
521 if (snapshot_file != NULL)
522 rv = TEST_SNAPSHOT_FILE_WRITE(rpcent, snapshot_file, &td,
523 sdump_rpcent);
524 break;
525 default:
526 rv = 0;
527 break;
530 fin:
531 TEST_DATA_DESTROY(rpcent, &td_snap);
532 TEST_DATA_DESTROY(rpcent, &td);
533 free(snapshot_file);
534 return (rv);