2 * Copyright (c) 2006 Michael Bushkov <bushman@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <arpa/inet.h>
38 #include <stringlist.h>
46 TEST_GETSERVENT_2PASS,
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 *,
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)
82 clone_servent(struct servent *dest, struct servent const *src)
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) {
105 for (cp = src->s_aliases; *cp; ++cp)
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);
120 free_servent(struct servent *serv)
124 assert(serv != NULL);
129 for (cp = serv->s_aliases; *cp; ++cp)
131 free(serv->s_aliases);
135 compare_servent(struct servent *serv1, struct servent *serv2, void *mdata)
142 if ((serv1 == NULL) || (serv2 == NULL))
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))
150 c1 = serv1->s_aliases;
151 c2 = serv2->s_aliases;
153 if ((serv1->s_aliases == NULL) || (serv2->s_aliases == NULL))
156 for (;*c1 && *c2; ++c1, ++c2)
157 if (strcmp(*c1, *c2) != 0)
160 if ((*c1 != '\0') || (*c2 != '\0'))
166 if ((debug) && (mdata == NULL)) {
167 printf("following structures are not equal:\n");
176 sdump_servent(struct servent *serv, char *buffer, size_t buflen)
181 written = snprintf(buffer, buflen, "%s %d %s",
182 serv->s_name, ntohs(serv->s_port), serv->s_proto);
184 if (written > buflen)
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);
193 if (written > buflen)
201 snprintf(buffer, buflen, " noaliases");
203 snprintf(buffer, buflen, " (null)");
207 servent_read_snapshot_func(struct servent *serv, char *line)
214 printf("1 line read from snapshot:\n%s\n", line);
219 memset(serv, 0, sizeof(struct servent));
220 while ( (s = strsep(&ps, " ")) != NULL) {
223 serv->s_name = strdup(s);
224 assert(serv->s_name != NULL);
228 serv->s_port = htons(
229 (int)strtol(s, &ts, 10));
237 serv->s_proto = strdup(s);
238 assert(serv->s_proto != NULL);
243 if (strcmp(s, "(null)") == 0)
249 if (strcmp(s, "noaliases") != 0) {
267 memset(serv, 0, sizeof(struct servent));
272 serv->s_aliases = sl->sl_str;
274 /* NOTE: is it a dirty hack or not? */
280 dump_servent(struct servent *result)
282 if (result != NULL) {
284 sdump_servent(result, buffer, sizeof(buffer));
285 printf("%s\n", buffer);
291 servent_fill_test_data(struct servent_test_data *td)
293 struct servent *serv;
296 while ((serv = getservent()) != NULL) {
297 if (servent_test_correctness(serv, NULL) == 0)
298 TEST_DATA_APPEND(servent, td, serv);
308 servent_test_correctness(struct servent *serv, void *mdata)
311 printf("testing correctness with the following data:\n");
318 if (serv->s_name == NULL)
321 if (serv->s_proto == NULL)
324 if (ntohs(serv->s_port < 0))
327 if (serv->s_aliases == NULL)
336 printf("incorrect\n");
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 */
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);
355 servent_test_getservbyname(struct servent *serv_model, void *mdata)
358 struct servent *serv;
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)
369 if ((compare_servent(serv, serv_model, NULL) != 0) &&
370 (servent_check_ambiguity((struct servent_test_data *)mdata, serv)
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)
380 if ((compare_servent(serv, serv_model, NULL) != 0) &&
381 (servent_check_ambiguity(
382 (struct servent_test_data *)mdata, serv) != 0))
398 servent_test_getservbyport(struct servent *serv_model, void *mdata)
400 struct servent *serv;
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)
423 servent_test_getservent(struct servent *serv, void *mdata)
425 /* Only correctness can be checked when doing 1-pass test for
427 return (servent_test_correctness(serv, NULL));
433 (void)fprintf(stderr,
434 "Usage: %s -npe2 [-d] [-s <file>]\n",
440 main(int argc, char **argv)
442 struct servent_test_data td, td_snap, td_2pass;
450 snapshot_file = NULL;
451 while ((c = getopt(argc, argv, "npe2ds:")) != -1)
457 method = TEST_GETSERVBYNAME;
460 method = TEST_GETSERVBYPORT;
463 method = TEST_GETSERVENT;
466 method = TEST_GETSERVENT_2PASS;
469 snapshot_file = strdup(optarg);
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) {
480 method = TEST_BUILD_SNAPSHOT;
483 printf("can't access the file %s\n",
490 if (method == TEST_BUILD_SNAPSHOT) {
495 TEST_SNAPSHOT_FILE_READ(servent, snapshot_file,
496 &td_snap, servent_read_snapshot_func);
500 rv = servent_fill_test_data(&td);
504 case TEST_GETSERVBYNAME:
505 if (snapshot_file == NULL)
506 rv = DO_1PASS_TEST(servent, &td,
507 servent_test_getservbyname, (void *)&td);
509 rv = DO_1PASS_TEST(servent, &td_snap,
510 servent_test_getservbyname, (void *)&td_snap);
512 case TEST_GETSERVBYPORT:
513 if (snapshot_file == NULL)
514 rv = DO_1PASS_TEST(servent, &td,
515 servent_test_getservbyport, (void *)&td);
517 rv = DO_1PASS_TEST(servent, &td_snap,
518 servent_test_getservbyport, (void *)&td_snap);
520 case TEST_GETSERVENT:
521 if (snapshot_file == NULL)
522 rv = DO_1PASS_TEST(servent, &td, servent_test_getservent,
525 rv = DO_2PASS_TEST(servent, &td, &td_snap,
526 compare_servent, NULL);
528 case TEST_GETSERVENT_2PASS:
529 TEST_DATA_INIT(servent, &td_2pass, clone_servent, free_servent);
530 rv = servent_fill_test_data(&td_2pass);
532 rv = DO_2PASS_TEST(servent, &td, &td_2pass,
533 compare_servent, NULL);
534 TEST_DATA_DESTROY(servent, &td_2pass);
536 case TEST_BUILD_SNAPSHOT:
537 if (snapshot_file != NULL)
538 rv = TEST_SNAPSHOT_FILE_WRITE(servent, snapshot_file, &td,
547 TEST_DATA_DESTROY(servent, &td_snap);
548 TEST_DATA_DESTROY(servent, &td);