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 #include <arpa/inet.h>
35 #include <stringlist.h>
46 TEST_GETSERVENT_2PASS,
50 DECLARE_TEST_DATA(servent)
51 DECLARE_TEST_FILE_SNAPSHOT(servent)
52 DECLARE_1PASS_TEST(servent)
53 DECLARE_2PASS_TEST(servent)
55 static void clone_servent(struct servent *, struct servent const *);
56 static int compare_servent(struct servent *, struct servent *, void *);
57 static void dump_servent(struct servent *);
58 static void free_servent(struct servent *);
60 static void sdump_servent(struct servent *, char *, size_t);
61 static int servent_read_snapshot_func(struct servent *, char *);
63 static int servent_check_ambiguity(struct servent_test_data *,
65 static int servent_fill_test_data(struct servent_test_data *);
66 static int servent_test_correctness(struct servent *, void *);
67 static int servent_test_getservbyname(struct servent *, void *);
68 static int servent_test_getservbyport(struct servent *, void *);
69 static int servent_test_getservent(struct servent *, void *);
71 IMPLEMENT_TEST_DATA(servent)
72 IMPLEMENT_TEST_FILE_SNAPSHOT(servent)
73 IMPLEMENT_1PASS_TEST(servent)
74 IMPLEMENT_2PASS_TEST(servent)
77 clone_servent(struct servent *dest, struct servent const *src)
79 ATF_REQUIRE(dest != NULL);
80 ATF_REQUIRE(src != NULL);
85 memset(dest, 0, sizeof(struct servent));
87 if (src->s_name != NULL) {
88 dest->s_name = strdup(src->s_name);
89 ATF_REQUIRE(dest->s_name != NULL);
92 if (src->s_proto != NULL) {
93 dest->s_proto = strdup(src->s_proto);
94 ATF_REQUIRE(dest->s_proto != NULL);
96 dest->s_port = src->s_port;
98 if (src->s_aliases != NULL) {
100 for (cp = src->s_aliases; *cp; ++cp)
103 dest->s_aliases = calloc(aliases_num + 1, sizeof(char *));
104 ATF_REQUIRE(dest->s_aliases != NULL);
106 for (cp = src->s_aliases; *cp; ++cp) {
107 dest->s_aliases[cp - src->s_aliases] = strdup(*cp);
108 ATF_REQUIRE(dest->s_aliases[cp - src->s_aliases] != NULL);
114 free_servent(struct servent *serv)
118 ATF_REQUIRE(serv != NULL);
123 for (cp = serv->s_aliases; *cp; ++cp)
125 free(serv->s_aliases);
129 compare_servent(struct servent *serv1, struct servent *serv2, void *mdata)
136 if ((serv1 == NULL) || (serv2 == NULL))
139 if ((strcmp(serv1->s_name, serv2->s_name) != 0) ||
140 (strcmp(serv1->s_proto, serv2->s_proto) != 0) ||
141 (serv1->s_port != serv2->s_port))
144 c1 = serv1->s_aliases;
145 c2 = serv2->s_aliases;
147 if ((serv1->s_aliases == NULL) || (serv2->s_aliases == NULL))
150 for (;*c1 && *c2; ++c1, ++c2)
151 if (strcmp(*c1, *c2) != 0)
154 if ((*c1 != NULL) || (*c2 != NULL))
161 printf("following structures are not equal:\n");
170 sdump_servent(struct servent *serv, char *buffer, size_t buflen)
175 written = snprintf(buffer, buflen, "%s %d %s",
176 serv->s_name, ntohs(serv->s_port), serv->s_proto);
178 if (written > (int)buflen)
182 if (serv->s_aliases != NULL) {
183 if (*(serv->s_aliases) != NULL) {
184 for (cp = serv->s_aliases; *cp; ++cp) {
185 written = snprintf(buffer, buflen, " %s", *cp);
187 if (written > (int)buflen)
195 snprintf(buffer, buflen, " noaliases");
197 snprintf(buffer, buflen, " (null)");
201 servent_read_snapshot_func(struct servent *serv, char *line)
207 printf("1 line read from snapshot:\n%s\n", line);
212 memset(serv, 0, sizeof(struct servent));
213 while ( (s = strsep(&ps, " ")) != NULL) {
216 serv->s_name = strdup(s);
217 ATF_REQUIRE(serv->s_name != NULL);
221 serv->s_port = htons(
222 (int)strtol(s, &ts, 10));
230 serv->s_proto = strdup(s);
231 ATF_REQUIRE(serv->s_proto != NULL);
236 if (strcmp(s, "(null)") == 0)
240 ATF_REQUIRE(sl != NULL);
242 if (strcmp(s, "noaliases") != 0) {
244 ATF_REQUIRE(ts != NULL);
249 ATF_REQUIRE(ts != NULL);
260 memset(serv, 0, sizeof(struct servent));
265 serv->s_aliases = sl->sl_str;
267 /* NOTE: is it a dirty hack or not? */
273 dump_servent(struct servent *result)
275 if (result != NULL) {
277 sdump_servent(result, buffer, sizeof(buffer));
278 printf("%s\n", buffer);
284 servent_fill_test_data(struct servent_test_data *td)
286 struct servent *serv;
289 while ((serv = getservent()) != NULL) {
290 if (servent_test_correctness(serv, NULL) == 0)
291 TEST_DATA_APPEND(servent, td, serv);
301 servent_test_correctness(struct servent *serv, void *mdata __unused)
303 printf("testing correctness with the following data:\n");
309 if (serv->s_name == NULL)
312 if (serv->s_proto == NULL)
315 if (ntohs(serv->s_port < 0))
318 if (serv->s_aliases == NULL)
325 printf("incorrect\n");
330 /* servent_check_ambiguity() is needed when one port+proto is associated with
331 * more than one service (these cases are usually marked as PROBLEM in
332 * /etc/services. This functions is needed also when one service+proto is
333 * associated with several ports. We have to check all the servent structures
334 * to make sure that serv really exists and correct */
336 servent_check_ambiguity(struct servent_test_data *td, struct servent *serv)
339 return (TEST_DATA_FIND(servent, td, serv, compare_servent,
340 NULL) != NULL ? 0 : -1);
344 servent_test_getservbyname(struct servent *serv_model, void *mdata)
347 struct servent *serv;
349 printf("testing getservbyname() with the following data:\n");
350 dump_servent(serv_model);
352 serv = getservbyname(serv_model->s_name, serv_model->s_proto);
353 if (servent_test_correctness(serv, NULL) != 0)
356 if ((compare_servent(serv, serv_model, NULL) != 0) &&
357 (servent_check_ambiguity((struct servent_test_data *)mdata, serv)
361 for (alias = serv_model->s_aliases; *alias; ++alias) {
362 serv = getservbyname(*alias, serv_model->s_proto);
364 if (servent_test_correctness(serv, NULL) != 0)
367 if ((compare_servent(serv, serv_model, NULL) != 0) &&
368 (servent_check_ambiguity(
369 (struct servent_test_data *)mdata, serv) != 0))
383 servent_test_getservbyport(struct servent *serv_model, void *mdata)
385 struct servent *serv;
387 printf("testing getservbyport() with the following data...\n");
388 dump_servent(serv_model);
390 serv = getservbyport(serv_model->s_port, serv_model->s_proto);
391 if ((servent_test_correctness(serv, NULL) != 0) ||
392 ((compare_servent(serv, serv_model, NULL) != 0) &&
393 (servent_check_ambiguity((struct servent_test_data *)mdata, serv)
404 servent_test_getservent(struct servent *serv, void *mdata __unused)
406 /* Only correctness can be checked when doing 1-pass test for
408 return (servent_test_correctness(serv, NULL));
412 run_tests(const char *snapshot_file, enum test_methods method)
414 struct servent_test_data td, td_snap, td_2pass;
417 TEST_DATA_INIT(servent, &td, clone_servent, free_servent);
418 TEST_DATA_INIT(servent, &td_snap, clone_servent, free_servent);
419 if (snapshot_file != NULL) {
420 if (access(snapshot_file, W_OK | R_OK) != 0) {
422 method = TEST_BUILD_SNAPSHOT;
424 printf("can't access the file %s\n",
431 if (method == TEST_BUILD_SNAPSHOT) {
436 TEST_SNAPSHOT_FILE_READ(servent, snapshot_file,
437 &td_snap, servent_read_snapshot_func);
441 rv = servent_fill_test_data(&td);
445 case TEST_GETSERVBYNAME:
446 if (snapshot_file == NULL)
447 rv = DO_1PASS_TEST(servent, &td,
448 servent_test_getservbyname, (void *)&td);
450 rv = DO_1PASS_TEST(servent, &td_snap,
451 servent_test_getservbyname, (void *)&td_snap);
453 case TEST_GETSERVBYPORT:
454 if (snapshot_file == NULL)
455 rv = DO_1PASS_TEST(servent, &td,
456 servent_test_getservbyport, (void *)&td);
458 rv = DO_1PASS_TEST(servent, &td_snap,
459 servent_test_getservbyport, (void *)&td_snap);
461 case TEST_GETSERVENT:
462 if (snapshot_file == NULL)
463 rv = DO_1PASS_TEST(servent, &td, servent_test_getservent,
466 rv = DO_2PASS_TEST(servent, &td, &td_snap,
467 compare_servent, NULL);
469 case TEST_GETSERVENT_2PASS:
470 TEST_DATA_INIT(servent, &td_2pass, clone_servent, free_servent);
471 rv = servent_fill_test_data(&td_2pass);
473 rv = DO_2PASS_TEST(servent, &td, &td_2pass,
474 compare_servent, NULL);
475 TEST_DATA_DESTROY(servent, &td_2pass);
477 case TEST_BUILD_SNAPSHOT:
478 if (snapshot_file != NULL)
479 rv = TEST_SNAPSHOT_FILE_WRITE(servent, snapshot_file, &td,
488 TEST_DATA_DESTROY(servent, &td_snap);
489 TEST_DATA_DESTROY(servent, &td);
494 #define SNAPSHOT_FILE "snapshot_serv"
496 ATF_TC_WITHOUT_HEAD(build_snapshot);
497 ATF_TC_BODY(build_snapshot, tc)
500 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
503 ATF_TC_WITHOUT_HEAD(getservbyname);
504 ATF_TC_BODY(getservbyname, tc)
507 ATF_REQUIRE(run_tests(NULL, TEST_GETSERVBYNAME) == 0);
510 ATF_TC_WITHOUT_HEAD(getservbyname_with_snapshot);
511 ATF_TC_BODY(getservbyname_with_snapshot, tc)
514 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
515 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETSERVBYNAME) == 0);
518 ATF_TC_WITHOUT_HEAD(getservbyport);
519 ATF_TC_BODY(getservbyport, tc)
522 ATF_REQUIRE(run_tests(NULL, TEST_GETSERVBYPORT) == 0);
525 ATF_TC_WITHOUT_HEAD(getservbyport_with_snapshot);
526 ATF_TC_BODY(getservbyport_with_snapshot, tc)
529 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
530 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETSERVBYPORT) == 0);
533 ATF_TC_WITHOUT_HEAD(getservbyent);
534 ATF_TC_BODY(getservbyent, tc)
537 ATF_REQUIRE(run_tests(NULL, TEST_GETSERVENT) == 0);
540 ATF_TC_WITHOUT_HEAD(getservbyent_with_snapshot);
541 ATF_TC_BODY(getservbyent_with_snapshot, tc)
544 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
545 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETSERVENT) == 0);
548 ATF_TC_WITHOUT_HEAD(getservbyent_with_two_pass);
549 ATF_TC_BODY(getservbyent_with_two_pass, tc)
552 ATF_REQUIRE(run_tests(NULL, TEST_GETSERVENT_2PASS) == 0);
558 ATF_TP_ADD_TC(tp, build_snapshot);
559 ATF_TP_ADD_TC(tp, getservbyent);
560 ATF_TP_ADD_TC(tp, getservbyent_with_snapshot);
561 ATF_TP_ADD_TC(tp, getservbyent_with_two_pass);
562 ATF_TP_ADD_TC(tp, getservbyname);
563 ATF_TP_ADD_TC(tp, getservbyname_with_snapshot);
564 ATF_TP_ADD_TC(tp, getservbyport);
565 ATF_TP_ADD_TC(tp, getservbyport_with_snapshot);
567 return (atf_no_error());