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>
37 #include <stringlist.h>
48 TEST_GETSERVENT_2PASS,
52 DECLARE_TEST_DATA(servent)
53 DECLARE_TEST_FILE_SNAPSHOT(servent)
54 DECLARE_1PASS_TEST(servent)
55 DECLARE_2PASS_TEST(servent)
57 static void clone_servent(struct servent *, struct servent const *);
58 static int compare_servent(struct servent *, struct servent *, void *);
59 static void dump_servent(struct servent *);
60 static void free_servent(struct servent *);
62 static void sdump_servent(struct servent *, char *, size_t);
63 static int servent_read_snapshot_func(struct servent *, char *);
65 static int servent_check_ambiguity(struct servent_test_data *,
67 static int servent_fill_test_data(struct servent_test_data *);
68 static int servent_test_correctness(struct servent *, void *);
69 static int servent_test_getservbyname(struct servent *, void *);
70 static int servent_test_getservbyport(struct servent *, void *);
71 static int servent_test_getservent(struct servent *, void *);
73 IMPLEMENT_TEST_DATA(servent)
74 IMPLEMENT_TEST_FILE_SNAPSHOT(servent)
75 IMPLEMENT_1PASS_TEST(servent)
76 IMPLEMENT_2PASS_TEST(servent)
79 clone_servent(struct servent *dest, struct servent const *src)
81 ATF_REQUIRE(dest != NULL);
82 ATF_REQUIRE(src != NULL);
87 memset(dest, 0, sizeof(struct servent));
89 if (src->s_name != NULL) {
90 dest->s_name = strdup(src->s_name);
91 ATF_REQUIRE(dest->s_name != NULL);
94 if (src->s_proto != NULL) {
95 dest->s_proto = strdup(src->s_proto);
96 ATF_REQUIRE(dest->s_proto != NULL);
98 dest->s_port = src->s_port;
100 if (src->s_aliases != NULL) {
102 for (cp = src->s_aliases; *cp; ++cp)
105 dest->s_aliases = calloc(1, (aliases_num + 1) * sizeof(char *));
106 ATF_REQUIRE(dest->s_aliases != NULL);
108 for (cp = src->s_aliases; *cp; ++cp) {
109 dest->s_aliases[cp - src->s_aliases] = strdup(*cp);
110 ATF_REQUIRE(dest->s_aliases[cp - src->s_aliases] != NULL);
116 free_servent(struct servent *serv)
120 ATF_REQUIRE(serv != NULL);
125 for (cp = serv->s_aliases; *cp; ++cp)
127 free(serv->s_aliases);
131 compare_servent(struct servent *serv1, struct servent *serv2, void *mdata)
138 if ((serv1 == NULL) || (serv2 == NULL))
141 if ((strcmp(serv1->s_name, serv2->s_name) != 0) ||
142 (strcmp(serv1->s_proto, serv2->s_proto) != 0) ||
143 (serv1->s_port != serv2->s_port))
146 c1 = serv1->s_aliases;
147 c2 = serv2->s_aliases;
149 if ((serv1->s_aliases == NULL) || (serv2->s_aliases == NULL))
152 for (;*c1 && *c2; ++c1, ++c2)
153 if (strcmp(*c1, *c2) != 0)
156 if ((*c1 != '\0') || (*c2 != '\0'))
163 printf("following structures are not equal:\n");
172 sdump_servent(struct servent *serv, char *buffer, size_t buflen)
177 written = snprintf(buffer, buflen, "%s %d %s",
178 serv->s_name, ntohs(serv->s_port), serv->s_proto);
180 if (written > buflen)
184 if (serv->s_aliases != NULL) {
185 if (*(serv->s_aliases) != '\0') {
186 for (cp = serv->s_aliases; *cp; ++cp) {
187 written = snprintf(buffer, buflen, " %s",*cp);
189 if (written > buflen)
197 snprintf(buffer, buflen, " noaliases");
199 snprintf(buffer, buflen, " (null)");
203 servent_read_snapshot_func(struct servent *serv, char *line)
209 printf("1 line read from snapshot:\n%s\n", line);
214 memset(serv, 0, sizeof(struct servent));
215 while ( (s = strsep(&ps, " ")) != NULL) {
218 serv->s_name = strdup(s);
219 ATF_REQUIRE(serv->s_name != NULL);
223 serv->s_port = htons(
224 (int)strtol(s, &ts, 10));
232 serv->s_proto = strdup(s);
233 ATF_REQUIRE(serv->s_proto != NULL);
238 if (strcmp(s, "(null)") == 0)
242 ATF_REQUIRE(sl != NULL);
244 if (strcmp(s, "noaliases") != 0) {
246 ATF_REQUIRE(ts != NULL);
251 ATF_REQUIRE(ts != NULL);
262 memset(serv, 0, sizeof(struct servent));
267 serv->s_aliases = sl->sl_str;
269 /* NOTE: is it a dirty hack or not? */
275 dump_servent(struct servent *result)
277 if (result != NULL) {
279 sdump_servent(result, buffer, sizeof(buffer));
280 printf("%s\n", buffer);
286 servent_fill_test_data(struct servent_test_data *td)
288 struct servent *serv;
291 while ((serv = getservent()) != NULL) {
292 if (servent_test_correctness(serv, NULL) == 0)
293 TEST_DATA_APPEND(servent, td, serv);
303 servent_test_correctness(struct servent *serv, void *mdata)
305 printf("testing correctness with the following data:\n");
311 if (serv->s_name == NULL)
314 if (serv->s_proto == NULL)
317 if (ntohs(serv->s_port < 0))
320 if (serv->s_aliases == NULL)
327 printf("incorrect\n");
332 /* servent_check_ambiguity() is needed when one port+proto is associated with
333 * more than one service (these cases are usually marked as PROBLEM in
334 * /etc/services. This functions is needed also when one service+proto is
335 * associated with several ports. We have to check all the servent structures
336 * to make sure that serv really exists and correct */
338 servent_check_ambiguity(struct servent_test_data *td, struct servent *serv)
341 return (TEST_DATA_FIND(servent, td, serv, compare_servent,
342 NULL) != NULL ? 0 : -1);
346 servent_test_getservbyname(struct servent *serv_model, void *mdata)
349 struct servent *serv;
351 printf("testing getservbyname() with the following data:\n");
352 dump_servent(serv_model);
354 serv = getservbyname(serv_model->s_name, serv_model->s_proto);
355 if (servent_test_correctness(serv, NULL) != 0)
358 if ((compare_servent(serv, serv_model, NULL) != 0) &&
359 (servent_check_ambiguity((struct servent_test_data *)mdata, serv)
363 for (alias = serv_model->s_aliases; *alias; ++alias) {
364 serv = getservbyname(*alias, 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(
371 (struct servent_test_data *)mdata, serv) != 0))
385 servent_test_getservbyport(struct servent *serv_model, void *mdata)
387 struct servent *serv;
389 printf("testing getservbyport() with the following data...\n");
390 dump_servent(serv_model);
392 serv = getservbyport(serv_model->s_port, serv_model->s_proto);
393 if ((servent_test_correctness(serv, NULL) != 0) ||
394 ((compare_servent(serv, serv_model, NULL) != 0) &&
395 (servent_check_ambiguity((struct servent_test_data *)mdata, serv)
406 servent_test_getservent(struct servent *serv, void *mdata)
408 /* Only correctness can be checked when doing 1-pass test for
410 return (servent_test_correctness(serv, NULL));
414 run_tests(const char *snapshot_file, enum test_methods method)
416 struct servent_test_data td, td_snap, td_2pass;
419 TEST_DATA_INIT(servent, &td, clone_servent, free_servent);
420 TEST_DATA_INIT(servent, &td_snap, clone_servent, free_servent);
421 if (snapshot_file != NULL) {
422 if (access(snapshot_file, W_OK | R_OK) != 0) {
424 method = TEST_BUILD_SNAPSHOT;
426 printf("can't access the file %s\n",
433 if (method == TEST_BUILD_SNAPSHOT) {
438 TEST_SNAPSHOT_FILE_READ(servent, snapshot_file,
439 &td_snap, servent_read_snapshot_func);
443 rv = servent_fill_test_data(&td);
447 case TEST_GETSERVBYNAME:
448 if (snapshot_file == NULL)
449 rv = DO_1PASS_TEST(servent, &td,
450 servent_test_getservbyname, (void *)&td);
452 rv = DO_1PASS_TEST(servent, &td_snap,
453 servent_test_getservbyname, (void *)&td_snap);
455 case TEST_GETSERVBYPORT:
456 if (snapshot_file == NULL)
457 rv = DO_1PASS_TEST(servent, &td,
458 servent_test_getservbyport, (void *)&td);
460 rv = DO_1PASS_TEST(servent, &td_snap,
461 servent_test_getservbyport, (void *)&td_snap);
463 case TEST_GETSERVENT:
464 if (snapshot_file == NULL)
465 rv = DO_1PASS_TEST(servent, &td, servent_test_getservent,
468 rv = DO_2PASS_TEST(servent, &td, &td_snap,
469 compare_servent, NULL);
471 case TEST_GETSERVENT_2PASS:
472 TEST_DATA_INIT(servent, &td_2pass, clone_servent, free_servent);
473 rv = servent_fill_test_data(&td_2pass);
475 rv = DO_2PASS_TEST(servent, &td, &td_2pass,
476 compare_servent, NULL);
477 TEST_DATA_DESTROY(servent, &td_2pass);
479 case TEST_BUILD_SNAPSHOT:
480 if (snapshot_file != NULL)
481 rv = TEST_SNAPSHOT_FILE_WRITE(servent, snapshot_file, &td,
490 TEST_DATA_DESTROY(servent, &td_snap);
491 TEST_DATA_DESTROY(servent, &td);
496 #define SNAPSHOT_FILE "snapshot_serv"
498 ATF_TC_WITHOUT_HEAD(build_snapshot);
499 ATF_TC_BODY(build_snapshot, tc)
502 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
505 ATF_TC_WITHOUT_HEAD(getservbyname);
506 ATF_TC_BODY(getservbyname, tc)
509 ATF_REQUIRE(run_tests(NULL, TEST_GETSERVBYNAME) == 0);
512 ATF_TC_WITHOUT_HEAD(getservbyname_with_snapshot);
513 ATF_TC_BODY(getservbyname_with_snapshot, tc)
516 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
517 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETSERVBYNAME) == 0);
520 ATF_TC_WITHOUT_HEAD(getservbyport);
521 ATF_TC_BODY(getservbyport, tc)
524 ATF_REQUIRE(run_tests(NULL, TEST_GETSERVBYPORT) == 0);
527 ATF_TC_WITHOUT_HEAD(getservbyport_with_snapshot);
528 ATF_TC_BODY(getservbyport_with_snapshot, tc)
531 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
532 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETSERVBYPORT) == 0);
535 ATF_TC_WITHOUT_HEAD(getservbyent);
536 ATF_TC_BODY(getservbyent, tc)
539 ATF_REQUIRE(run_tests(NULL, TEST_GETSERVENT) == 0);
542 ATF_TC_WITHOUT_HEAD(getservbyent_with_snapshot);
543 ATF_TC_BODY(getservbyent_with_snapshot, tc)
546 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
547 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETSERVENT) == 0);
550 ATF_TC_WITHOUT_HEAD(getservbyent_with_two_pass);
551 ATF_TC_BODY(getservbyent_with_two_pass, tc)
554 ATF_REQUIRE(run_tests(NULL, TEST_GETSERVENT_2PASS) == 0);
560 ATF_TP_ADD_TC(tp, build_snapshot);
561 ATF_TP_ADD_TC(tp, getservbyent);
562 ATF_TP_ADD_TC(tp, getservbyent_with_snapshot);
563 ATF_TP_ADD_TC(tp, getservbyent_with_two_pass);
564 ATF_TP_ADD_TC(tp, getservbyname);
565 ATF_TP_ADD_TC(tp, getservbyname_with_snapshot);
566 ATF_TP_ADD_TC(tp, getservbyport);
567 ATF_TP_ADD_TC(tp, getservbyport_with_snapshot);
569 return (atf_no_error());