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>
52 DECLARE_TEST_DATA(rpcent)
53 DECLARE_TEST_FILE_SNAPSHOT(rpcent)
54 DECLARE_1PASS_TEST(rpcent)
55 DECLARE_2PASS_TEST(rpcent)
57 static void clone_rpcent(struct rpcent *, struct rpcent const *);
58 static int compare_rpcent(struct rpcent *, struct rpcent *, void *);
59 static void dump_rpcent(struct rpcent *);
60 static void free_rpcent(struct rpcent *);
62 static void sdump_rpcent(struct rpcent *, char *, size_t);
63 static int rpcent_read_snapshot_func(struct rpcent *, char *);
65 static int rpcent_check_ambiguity(struct rpcent_test_data *,
67 static int rpcent_fill_test_data(struct rpcent_test_data *);
68 static int rpcent_test_correctness(struct rpcent *, void *);
69 static int rpcent_test_getrpcbyname(struct rpcent *, void *);
70 static int rpcent_test_getrpcbynumber(struct rpcent *, void *);
71 static int rpcent_test_getrpcent(struct rpcent *, void *);
73 static void usage(void) __attribute__((__noreturn__));
75 IMPLEMENT_TEST_DATA(rpcent)
76 IMPLEMENT_TEST_FILE_SNAPSHOT(rpcent)
77 IMPLEMENT_1PASS_TEST(rpcent)
78 IMPLEMENT_2PASS_TEST(rpcent)
81 clone_rpcent(struct rpcent *dest, struct rpcent const *src)
83 ATF_REQUIRE(dest != NULL);
84 ATF_REQUIRE(src != NULL);
89 memset(dest, 0, sizeof(struct rpcent));
91 if (src->r_name != NULL) {
92 dest->r_name = strdup(src->r_name);
93 ATF_REQUIRE(dest->r_name != NULL);
96 dest->r_number = src->r_number;
98 if (src->r_aliases != NULL) {
100 for (cp = src->r_aliases; *cp; ++cp)
103 dest->r_aliases = calloc(1, (aliases_num + 1) * sizeof(char *));
104 ATF_REQUIRE(dest->r_aliases != NULL);
106 for (cp = src->r_aliases; *cp; ++cp) {
107 dest->r_aliases[cp - src->r_aliases] = strdup(*cp);
108 ATF_REQUIRE(dest->r_aliases[cp - src->r_aliases] != NULL);
114 free_rpcent(struct rpcent *rpc)
118 ATF_REQUIRE(rpc != NULL);
122 for (cp = rpc->r_aliases; *cp; ++cp)
124 free(rpc->r_aliases);
128 compare_rpcent(struct rpcent *rpc1, struct rpcent *rpc2, void *mdata)
135 if ((rpc1 == NULL) || (rpc2 == NULL))
138 if ((strcmp(rpc1->r_name, rpc2->r_name) != 0) ||
139 (rpc1->r_number != rpc2->r_number))
142 c1 = rpc1->r_aliases;
143 c2 = rpc2->r_aliases;
145 if ((rpc1->r_aliases == NULL) || (rpc2->r_aliases == NULL))
148 for (;*c1 && *c2; ++c1, ++c2)
149 if (strcmp(*c1, *c2) != 0)
152 if ((*c1 != '\0') || (*c2 != '\0'))
159 printf("following structures are not equal:\n");
168 sdump_rpcent(struct rpcent *rpc, char *buffer, size_t buflen)
173 written = snprintf(buffer, buflen, "%s %d",
174 rpc->r_name, rpc->r_number);
176 if (written > buflen)
180 if (rpc->r_aliases != NULL) {
181 if (*(rpc->r_aliases) != '\0') {
182 for (cp = rpc->r_aliases; *cp; ++cp) {
183 written = snprintf(buffer, buflen, " %s",*cp);
185 if (written > buflen)
193 snprintf(buffer, buflen, " noaliases");
195 snprintf(buffer, buflen, " (null)");
199 rpcent_read_snapshot_func(struct rpcent *rpc, char *line)
205 printf("1 line read from snapshot:\n%s\n", line);
210 memset(rpc, 0, sizeof(struct rpcent));
211 while ((s = strsep(&ps, " ")) != NULL) {
214 rpc->r_name = strdup(s);
215 ATF_REQUIRE(rpc->r_name != NULL);
219 rpc->r_number = (int)strtol(s, &ts, 10);
228 if (strcmp(s, "(null)") == 0)
232 ATF_REQUIRE(sl != NULL);
234 if (strcmp(s, "noaliases") != 0) {
236 ATF_REQUIRE(ts != NULL);
241 ATF_REQUIRE(ts != NULL);
251 memset(rpc, 0, sizeof(struct rpcent));
256 rpc->r_aliases = sl->sl_str;
258 /* NOTE: is it a dirty hack or not? */
264 dump_rpcent(struct rpcent *result)
266 if (result != NULL) {
268 sdump_rpcent(result, buffer, sizeof(buffer));
269 printf("%s\n", buffer);
275 rpcent_fill_test_data(struct rpcent_test_data *td)
280 while ((rpc = getrpcent()) != NULL) {
281 if (rpcent_test_correctness(rpc, NULL) == 0)
282 TEST_DATA_APPEND(rpcent, td, rpc);
292 rpcent_test_correctness(struct rpcent *rpc, void *mdata)
295 printf("testing correctness with the following data:\n");
301 if (rpc->r_name == NULL)
304 if (rpc->r_number < 0)
307 if (rpc->r_aliases == NULL)
314 printf("incorrect\n");
319 /* rpcent_check_ambiguity() is needed when one port+rpc is associated with
320 * more than one peice (these cases are usually marked as PROBLEM in
321 * /etc/peices. This functions is needed also when one peice+rpc is
322 * associated with several ports. We have to check all the rpcent structures
323 * to make sure that rpc really exists and correct */
325 rpcent_check_ambiguity(struct rpcent_test_data *td, struct rpcent *rpc)
328 return (TEST_DATA_FIND(rpcent, td, rpc, compare_rpcent,
329 NULL) != NULL ? 0 : -1);
333 rpcent_test_getrpcbyname(struct rpcent *rpc_model, void *mdata)
338 printf("testing getrpcbyname() with the following data:\n");
339 dump_rpcent(rpc_model);
341 rpc = getrpcbyname(rpc_model->r_name);
342 if (rpcent_test_correctness(rpc, NULL) != 0)
345 if ((compare_rpcent(rpc, rpc_model, NULL) != 0) &&
346 (rpcent_check_ambiguity((struct rpcent_test_data *)mdata, rpc)
350 for (alias = rpc_model->r_aliases; *alias; ++alias) {
351 rpc = getrpcbyname(*alias);
353 if (rpcent_test_correctness(rpc, NULL) != 0)
356 if ((compare_rpcent(rpc, rpc_model, NULL) != 0) &&
357 (rpcent_check_ambiguity(
358 (struct rpcent_test_data *)mdata, rpc) != 0))
372 rpcent_test_getrpcbynumber(struct rpcent *rpc_model, void *mdata)
376 printf("testing getrpcbyport() with the following data...\n");
377 dump_rpcent(rpc_model);
379 rpc = getrpcbynumber(rpc_model->r_number);
380 if (rpcent_test_correctness(rpc, NULL) != 0 ||
381 (compare_rpcent(rpc, rpc_model, NULL) != 0 &&
382 rpcent_check_ambiguity((struct rpcent_test_data *)mdata, rpc)
393 rpcent_test_getrpcent(struct rpcent *rpc, void *mdata)
397 * Only correctness can be checked when doing 1-pass test for
400 return (rpcent_test_correctness(rpc, NULL));
404 run_tests(const char *snapshot_file, enum test_methods method)
406 struct rpcent_test_data td, td_snap, td_2pass;
409 TEST_DATA_INIT(rpcent, &td, clone_rpcent, free_rpcent);
410 TEST_DATA_INIT(rpcent, &td_snap, clone_rpcent, free_rpcent);
411 if (snapshot_file != NULL) {
412 if (access(snapshot_file, W_OK | R_OK) != 0) {
414 method = TEST_BUILD_SNAPSHOT;
416 printf("can't access the file %s\n",
423 if (method == TEST_BUILD_SNAPSHOT) {
428 TEST_SNAPSHOT_FILE_READ(rpcent, snapshot_file,
429 &td_snap, rpcent_read_snapshot_func);
433 rv = rpcent_fill_test_data(&td);
437 case TEST_GETRPCBYNAME:
438 if (snapshot_file == NULL)
439 rv = DO_1PASS_TEST(rpcent, &td,
440 rpcent_test_getrpcbyname, (void *)&td);
442 rv = DO_1PASS_TEST(rpcent, &td_snap,
443 rpcent_test_getrpcbyname, (void *)&td_snap);
445 case TEST_GETRPCBYNUMBER:
446 if (snapshot_file == NULL)
447 rv = DO_1PASS_TEST(rpcent, &td,
448 rpcent_test_getrpcbynumber, (void *)&td);
450 rv = DO_1PASS_TEST(rpcent, &td_snap,
451 rpcent_test_getrpcbynumber, (void *)&td_snap);
454 if (snapshot_file == NULL)
455 rv = DO_1PASS_TEST(rpcent, &td, rpcent_test_getrpcent,
458 rv = DO_2PASS_TEST(rpcent, &td, &td_snap,
459 compare_rpcent, NULL);
461 case TEST_GETRPCENT_2PASS:
462 TEST_DATA_INIT(rpcent, &td_2pass, clone_rpcent, free_rpcent);
463 rv = rpcent_fill_test_data(&td_2pass);
465 rv = DO_2PASS_TEST(rpcent, &td, &td_2pass,
466 compare_rpcent, NULL);
467 TEST_DATA_DESTROY(rpcent, &td_2pass);
469 case TEST_BUILD_SNAPSHOT:
470 if (snapshot_file != NULL)
471 rv = TEST_SNAPSHOT_FILE_WRITE(rpcent, snapshot_file, &td,
480 TEST_DATA_DESTROY(rpcent, &td_snap);
481 TEST_DATA_DESTROY(rpcent, &td);
486 #define SNAPSHOT_FILE "snapshot_rpc"
488 ATF_TC_WITHOUT_HEAD(build_snapshot);
489 ATF_TC_BODY(build_snapshot, tc)
492 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
495 ATF_TC_WITHOUT_HEAD(getrpcbyname);
496 ATF_TC_BODY(getrpcbyname, tc)
499 ATF_REQUIRE(run_tests(NULL, TEST_GETRPCBYNAME) == 0);
502 ATF_TC_WITHOUT_HEAD(getrpcbyname_with_snapshot);
503 ATF_TC_BODY(getrpcbyname_with_snapshot, tc)
506 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
507 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETRPCBYNAME) == 0);
510 ATF_TC_WITHOUT_HEAD(getrpcbynumber);
511 ATF_TC_BODY(getrpcbynumber, tc)
514 ATF_REQUIRE(run_tests(NULL, TEST_GETRPCBYNUMBER) == 0);
517 ATF_TC_WITHOUT_HEAD(getrpcbynumber_with_snapshot);
518 ATF_TC_BODY(getrpcbynumber_with_snapshot, tc)
521 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
522 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETRPCBYNUMBER) == 0);
525 ATF_TC_WITHOUT_HEAD(getrpcbyent);
526 ATF_TC_BODY(getrpcbyent, tc)
529 ATF_REQUIRE(run_tests(NULL, TEST_GETRPCENT) == 0);
532 ATF_TC_WITHOUT_HEAD(getrpcbyent_with_snapshot);
533 ATF_TC_BODY(getrpcbyent_with_snapshot, tc)
536 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);
537 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETRPCENT) == 0);
540 ATF_TC_WITHOUT_HEAD(getrpcbyent_with_two_pass);
541 ATF_TC_BODY(getrpcbyent_with_two_pass, tc)
544 ATF_REQUIRE(run_tests(NULL, TEST_GETRPCENT_2PASS) == 0);
550 ATF_TP_ADD_TC(tp, build_snapshot);
551 ATF_TP_ADD_TC(tp, getrpcbyname);
552 ATF_TP_ADD_TC(tp, getrpcbyname_with_snapshot);
553 ATF_TP_ADD_TC(tp, getrpcbynumber);
554 ATF_TP_ADD_TC(tp, getrpcbynumber_with_snapshot);
555 ATF_TP_ADD_TC(tp, getrpcbyent);
556 ATF_TP_ADD_TC(tp, getrpcbyent_with_snapshot);
557 ATF_TP_ADD_TC(tp, getrpcbyent_with_two_pass);
559 return (atf_no_error());