Ignore machine-check MSRs
[freebsd-src/fkvm-freebsd.git] / tools / regression / lib / libc / nss / test-getserv.c
blob79d42f017af24a7fabf5efdc272abd01e52e6d93
1 /*-
2 * Copyright (c) 2006 Michael Bushkov <bushman@freebsd.org>
3 * All rights reserved.
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 <assert.h>
33 #include <errno.h>
34 #include <netdb.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_GETSERVENT,
44 TEST_GETSERVBYNAME,
45 TEST_GETSERVBYPORT,
46 TEST_GETSERVENT_2PASS,
47 TEST_BUILD_SNAPSHOT
50 static int debug = 0;
51 static enum test_methods method = TEST_BUILD_SNAPSHOT;
53 DECLARE_TEST_DATA(servent)
54 DECLARE_TEST_FILE_SNAPSHOT(servent)
55 DECLARE_1PASS_TEST(servent)
56 DECLARE_2PASS_TEST(servent)
58 static void clone_servent(struct servent *, struct servent const *);
59 static int compare_servent(struct servent *, struct servent *, void *);
60 static void dump_servent(struct servent *);
61 static void free_servent(struct servent *);
63 static void sdump_servent(struct servent *, char *, size_t);
64 static int servent_read_snapshot_func(struct servent *, char *);
66 static int servent_check_ambiguity(struct servent_test_data *,
67 struct servent *);
68 static int servent_fill_test_data(struct servent_test_data *);
69 static int servent_test_correctness(struct servent *, void *);
70 static int servent_test_getservbyname(struct servent *, void *);
71 static int servent_test_getservbyport(struct servent *, void *);
72 static int servent_test_getservent(struct servent *, void *);
74 static void usage(void) __attribute__((__noreturn__));
76 IMPLEMENT_TEST_DATA(servent)
77 IMPLEMENT_TEST_FILE_SNAPSHOT(servent)
78 IMPLEMENT_1PASS_TEST(servent)
79 IMPLEMENT_2PASS_TEST(servent)
81 static void
82 clone_servent(struct servent *dest, struct servent const *src)
84 assert(dest != NULL);
85 assert(src != NULL);
87 char **cp;
88 int aliases_num;
90 memset(dest, 0, sizeof(struct servent));
92 if (src->s_name != NULL) {
93 dest->s_name = strdup(src->s_name);
94 assert(dest->s_name != NULL);
97 if (src->s_proto != NULL) {
98 dest->s_proto = strdup(src->s_proto);
99 assert(dest->s_proto != NULL);
101 dest->s_port = src->s_port;
103 if (src->s_aliases != NULL) {
104 aliases_num = 0;
105 for (cp = src->s_aliases; *cp; ++cp)
106 ++aliases_num;
108 dest->s_aliases = (char **)malloc((aliases_num+1) * (sizeof(char *)));
109 assert(dest->s_aliases != NULL);
110 memset(dest->s_aliases, 0, (aliases_num+1) * (sizeof(char *)));
112 for (cp = src->s_aliases; *cp; ++cp) {
113 dest->s_aliases[cp - src->s_aliases] = strdup(*cp);
114 assert(dest->s_aliases[cp - src->s_aliases] != NULL);
119 static void
120 free_servent(struct servent *serv)
122 char **cp;
124 assert(serv != NULL);
126 free(serv->s_name);
127 free(serv->s_proto);
129 for (cp = serv->s_aliases; *cp; ++cp)
130 free(*cp);
131 free(serv->s_aliases);
134 static int
135 compare_servent(struct servent *serv1, struct servent *serv2, void *mdata)
137 char **c1, **c2;
139 if (serv1 == serv2)
140 return 0;
142 if ((serv1 == NULL) || (serv2 == NULL))
143 goto errfin;
145 if ((strcmp(serv1->s_name, serv2->s_name) != 0) ||
146 (strcmp(serv1->s_proto, serv2->s_proto) != 0) ||
147 (serv1->s_port != serv2->s_port))
148 goto errfin;
150 c1 = serv1->s_aliases;
151 c2 = serv2->s_aliases;
153 if ((serv1->s_aliases == NULL) || (serv2->s_aliases == NULL))
154 goto errfin;
156 for (;*c1 && *c2; ++c1, ++c2)
157 if (strcmp(*c1, *c2) != 0)
158 goto errfin;
160 if ((*c1 != '\0') || (*c2 != '\0'))
161 goto errfin;
163 return 0;
165 errfin:
166 if ((debug) && (mdata == NULL)) {
167 printf("following structures are not equal:\n");
168 dump_servent(serv1);
169 dump_servent(serv2);
172 return (-1);
175 static void
176 sdump_servent(struct servent *serv, char *buffer, size_t buflen)
178 char **cp;
179 int written;
181 written = snprintf(buffer, buflen, "%s %d %s",
182 serv->s_name, ntohs(serv->s_port), serv->s_proto);
183 buffer += written;
184 if (written > buflen)
185 return;
186 buflen -= written;
188 if (serv->s_aliases != NULL) {
189 if (*(serv->s_aliases) != '\0') {
190 for (cp = serv->s_aliases; *cp; ++cp) {
191 written = snprintf(buffer, buflen, " %s",*cp);
192 buffer += written;
193 if (written > buflen)
194 return;
195 buflen -= written;
197 if (buflen == 0)
198 return;
200 } else
201 snprintf(buffer, buflen, " noaliases");
202 } else
203 snprintf(buffer, buflen, " (null)");
206 static int
207 servent_read_snapshot_func(struct servent *serv, char *line)
209 StringList *sl;
210 char *s, *ps, *ts;
211 int i;
213 if (debug)
214 printf("1 line read from snapshot:\n%s\n", line);
216 i = 0;
217 sl = NULL;
218 ps = line;
219 memset(serv, 0, sizeof(struct servent));
220 while ( (s = strsep(&ps, " ")) != NULL) {
221 switch (i) {
222 case 0:
223 serv->s_name = strdup(s);
224 assert(serv->s_name != NULL);
225 break;
227 case 1:
228 serv->s_port = htons(
229 (int)strtol(s, &ts, 10));
230 if (*ts != '\0') {
231 free(serv->s_name);
232 return (-1);
234 break;
236 case 2:
237 serv->s_proto = strdup(s);
238 assert(serv->s_proto != NULL);
239 break;
241 default:
242 if (sl == NULL) {
243 if (strcmp(s, "(null)") == 0)
244 return (0);
246 sl = sl_init();
247 assert(sl != NULL);
249 if (strcmp(s, "noaliases") != 0) {
250 ts = strdup(s);
251 assert(ts != NULL);
252 sl_add(sl, ts);
254 } else {
255 ts = strdup(s);
256 assert(ts != NULL);
257 sl_add(sl, ts);
259 break;
261 ++i;
264 if (i < 3) {
265 free(serv->s_name);
266 free(serv->s_proto);
267 memset(serv, 0, sizeof(struct servent));
268 return (-1);
271 sl_add(sl, NULL);
272 serv->s_aliases = sl->sl_str;
274 /* NOTE: is it a dirty hack or not? */
275 free(sl);
276 return (0);
279 static void
280 dump_servent(struct servent *result)
282 if (result != NULL) {
283 char buffer[1024];
284 sdump_servent(result, buffer, sizeof(buffer));
285 printf("%s\n", buffer);
286 } else
287 printf("(null)\n");
290 static int
291 servent_fill_test_data(struct servent_test_data *td)
293 struct servent *serv;
295 setservent(1);
296 while ((serv = getservent()) != NULL) {
297 if (servent_test_correctness(serv, NULL) == 0)
298 TEST_DATA_APPEND(servent, td, serv);
299 else
300 return (-1);
302 endservent();
304 return (0);
307 static int
308 servent_test_correctness(struct servent *serv, void *mdata)
310 if (debug) {
311 printf("testing correctness with the following data:\n");
312 dump_servent(serv);
315 if (serv == NULL)
316 goto errfin;
318 if (serv->s_name == NULL)
319 goto errfin;
321 if (serv->s_proto == NULL)
322 goto errfin;
324 if (ntohs(serv->s_port < 0))
325 goto errfin;
327 if (serv->s_aliases == NULL)
328 goto errfin;
330 if (debug)
331 printf("correct\n");
333 return (0);
334 errfin:
335 if (debug)
336 printf("incorrect\n");
338 return (-1);
341 /* servent_check_ambiguity() is needed when one port+proto is associated with
342 * more than one service (these cases are usually marked as PROBLEM in
343 * /etc/services. This functions is needed also when one service+proto is
344 * associated with several ports. We have to check all the servent structures
345 * to make sure that serv really exists and correct */
346 static int
347 servent_check_ambiguity(struct servent_test_data *td, struct servent *serv)
350 return (TEST_DATA_FIND(servent, td, serv, compare_servent,
351 NULL) != NULL ? 0 : -1);
354 static int
355 servent_test_getservbyname(struct servent *serv_model, void *mdata)
357 char **alias;
358 struct servent *serv;
360 if (debug) {
361 printf("testing getservbyname() with the following data:\n");
362 dump_servent(serv_model);
365 serv = getservbyname(serv_model->s_name, serv_model->s_proto);
366 if (servent_test_correctness(serv, NULL) != 0)
367 goto errfin;
369 if ((compare_servent(serv, serv_model, NULL) != 0) &&
370 (servent_check_ambiguity((struct servent_test_data *)mdata, serv)
371 !=0))
372 goto errfin;
374 for (alias = serv_model->s_aliases; *alias; ++alias) {
375 serv = getservbyname(*alias, serv_model->s_proto);
377 if (servent_test_correctness(serv, NULL) != 0)
378 goto errfin;
380 if ((compare_servent(serv, serv_model, NULL) != 0) &&
381 (servent_check_ambiguity(
382 (struct servent_test_data *)mdata, serv) != 0))
383 goto errfin;
386 if (debug)
387 printf("ok\n");
388 return (0);
390 errfin:
391 if (debug)
392 printf("not ok\n");
394 return (-1);
397 static int
398 servent_test_getservbyport(struct servent *serv_model, void *mdata)
400 struct servent *serv;
402 if (debug) {
403 printf("testing getservbyport() with the following data...\n");
404 dump_servent(serv_model);
407 serv = getservbyport(serv_model->s_port, serv_model->s_proto);
408 if ((servent_test_correctness(serv, NULL) != 0) ||
409 ((compare_servent(serv, serv_model, NULL) != 0) &&
410 (servent_check_ambiguity((struct servent_test_data *)mdata, serv)
411 != 0))) {
412 if (debug)
413 printf("not ok\n");
414 return (-1);
415 } else {
416 if (debug)
417 printf("ok\n");
418 return (0);
422 static int
423 servent_test_getservent(struct servent *serv, void *mdata)
425 /* Only correctness can be checked when doing 1-pass test for
426 * getservent(). */
427 return (servent_test_correctness(serv, NULL));
430 static void
431 usage(void)
433 (void)fprintf(stderr,
434 "Usage: %s -npe2 [-d] [-s <file>]\n",
435 getprogname());
436 exit(1);
440 main(int argc, char **argv)
442 struct servent_test_data td, td_snap, td_2pass;
443 char *snapshot_file;
444 int rv;
445 int c;
447 if (argc < 2)
448 usage();
450 snapshot_file = NULL;
451 while ((c = getopt(argc, argv, "npe2ds:")) != -1)
452 switch (c) {
453 case 'd':
454 debug++;
455 break;
456 case 'n':
457 method = TEST_GETSERVBYNAME;
458 break;
459 case 'p':
460 method = TEST_GETSERVBYPORT;
461 break;
462 case 'e':
463 method = TEST_GETSERVENT;
464 break;
465 case '2':
466 method = TEST_GETSERVENT_2PASS;
467 break;
468 case 's':
469 snapshot_file = strdup(optarg);
470 break;
471 default:
472 usage();
475 TEST_DATA_INIT(servent, &td, clone_servent, free_servent);
476 TEST_DATA_INIT(servent, &td_snap, clone_servent, free_servent);
477 if (snapshot_file != NULL) {
478 if (access(snapshot_file, W_OK | R_OK) != 0) {
479 if (errno == ENOENT)
480 method = TEST_BUILD_SNAPSHOT;
481 else {
482 if (debug)
483 printf("can't access the file %s\n",
484 snapshot_file);
486 rv = -1;
487 goto fin;
489 } else {
490 if (method == TEST_BUILD_SNAPSHOT) {
491 rv = 0;
492 goto fin;
495 TEST_SNAPSHOT_FILE_READ(servent, snapshot_file,
496 &td_snap, servent_read_snapshot_func);
500 rv = servent_fill_test_data(&td);
501 if (rv == -1)
502 return (-1);
503 switch (method) {
504 case TEST_GETSERVBYNAME:
505 if (snapshot_file == NULL)
506 rv = DO_1PASS_TEST(servent, &td,
507 servent_test_getservbyname, (void *)&td);
508 else
509 rv = DO_1PASS_TEST(servent, &td_snap,
510 servent_test_getservbyname, (void *)&td_snap);
511 break;
512 case TEST_GETSERVBYPORT:
513 if (snapshot_file == NULL)
514 rv = DO_1PASS_TEST(servent, &td,
515 servent_test_getservbyport, (void *)&td);
516 else
517 rv = DO_1PASS_TEST(servent, &td_snap,
518 servent_test_getservbyport, (void *)&td_snap);
519 break;
520 case TEST_GETSERVENT:
521 if (snapshot_file == NULL)
522 rv = DO_1PASS_TEST(servent, &td, servent_test_getservent,
523 (void *)&td);
524 else
525 rv = DO_2PASS_TEST(servent, &td, &td_snap,
526 compare_servent, NULL);
527 break;
528 case TEST_GETSERVENT_2PASS:
529 TEST_DATA_INIT(servent, &td_2pass, clone_servent, free_servent);
530 rv = servent_fill_test_data(&td_2pass);
531 if (rv != -1)
532 rv = DO_2PASS_TEST(servent, &td, &td_2pass,
533 compare_servent, NULL);
534 TEST_DATA_DESTROY(servent, &td_2pass);
535 break;
536 case TEST_BUILD_SNAPSHOT:
537 if (snapshot_file != NULL)
538 rv = TEST_SNAPSHOT_FILE_WRITE(servent, snapshot_file, &td,
539 sdump_servent);
540 break;
541 default:
542 rv = 0;
543 break;
546 fin:
547 TEST_DATA_DESTROY(servent, &td_snap);
548 TEST_DATA_DESTROY(servent, &td);
549 free(snapshot_file);
550 return (rv);