1 /*- 2 * Copyright (c) 2006 Michael Bushkov <bushman@freebsd.org> 3 * All rights reserved. 4 * 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. 13 * 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. 25 * 26 */ 27 28 #include <sys/cdefs.h> 29 #include <arpa/inet.h> 30 #include <rpc/rpc.h> 31 #include <errno.h> 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <stringlist.h> 36 #include <unistd.h> 37 38 #include <atf-c.h> 39 40 #include "testutil.h" 41 42 enum test_methods { 43 TEST_GETRPCENT, 44 TEST_GETRPCBYNAME, 45 TEST_GETRPCBYNUMBER, 46 TEST_GETRPCENT_2PASS, 47 TEST_BUILD_SNAPSHOT 48 }; 49 50 DECLARE_TEST_DATA(rpcent) 51 DECLARE_TEST_FILE_SNAPSHOT(rpcent) 52 DECLARE_1PASS_TEST(rpcent) 53 DECLARE_2PASS_TEST(rpcent) 54 55 static void clone_rpcent(struct rpcent *, struct rpcent const *); 56 static int compare_rpcent(struct rpcent *, struct rpcent *, void *); 57 static void dump_rpcent(struct rpcent *); 58 static void free_rpcent(struct rpcent *); 59 60 static void sdump_rpcent(struct rpcent *, char *, size_t); 61 static int rpcent_read_snapshot_func(struct rpcent *, char *); 62 63 static int rpcent_check_ambiguity(struct rpcent_test_data *, 64 struct rpcent *); 65 static int rpcent_fill_test_data(struct rpcent_test_data *); 66 static int rpcent_test_correctness(struct rpcent *, void *); 67 static int rpcent_test_getrpcbyname(struct rpcent *, void *); 68 static int rpcent_test_getrpcbynumber(struct rpcent *, void *); 69 static int rpcent_test_getrpcent(struct rpcent *, void *); 70 71 IMPLEMENT_TEST_DATA(rpcent) 72 IMPLEMENT_TEST_FILE_SNAPSHOT(rpcent) 73 IMPLEMENT_1PASS_TEST(rpcent) 74 IMPLEMENT_2PASS_TEST(rpcent) 75 76 static void 77 clone_rpcent(struct rpcent *dest, struct rpcent const *src) 78 { 79 ATF_REQUIRE(dest != NULL); 80 ATF_REQUIRE(src != NULL); 81 82 char **cp; 83 int aliases_num; 84 85 memset(dest, 0, sizeof(struct rpcent)); 86 87 if (src->r_name != NULL) { 88 dest->r_name = strdup(src->r_name); 89 ATF_REQUIRE(dest->r_name != NULL); 90 } 91 92 dest->r_number = src->r_number; 93 94 if (src->r_aliases != NULL) { 95 aliases_num = 0; 96 for (cp = src->r_aliases; *cp; ++cp) 97 ++aliases_num; 98 99 dest->r_aliases = calloc(aliases_num + 1, sizeof(char *)); 100 ATF_REQUIRE(dest->r_aliases != NULL); 101 102 for (cp = src->r_aliases; *cp; ++cp) { 103 dest->r_aliases[cp - src->r_aliases] = strdup(*cp); 104 ATF_REQUIRE(dest->r_aliases[cp - src->r_aliases] != NULL); 105 } 106 } 107 } 108 109 static void 110 free_rpcent(struct rpcent *rpc) 111 { 112 char **cp; 113 114 ATF_REQUIRE(rpc != NULL); 115 116 free(rpc->r_name); 117 118 for (cp = rpc->r_aliases; *cp; ++cp) 119 free(*cp); 120 free(rpc->r_aliases); 121 } 122 123 static int 124 compare_rpcent(struct rpcent *rpc1, struct rpcent *rpc2, void *mdata) 125 { 126 char **c1, **c2; 127 128 if (rpc1 == rpc2) 129 return 0; 130 131 if ((rpc1 == NULL) || (rpc2 == NULL)) 132 goto errfin; 133 134 if ((strcmp(rpc1->r_name, rpc2->r_name) != 0) || 135 (rpc1->r_number != rpc2->r_number)) 136 goto errfin; 137 138 c1 = rpc1->r_aliases; 139 c2 = rpc2->r_aliases; 140 141 if ((rpc1->r_aliases == NULL) || (rpc2->r_aliases == NULL)) 142 goto errfin; 143 144 for (;*c1 && *c2; ++c1, ++c2) 145 if (strcmp(*c1, *c2) != 0) 146 goto errfin; 147 148 if ((*c1 != NULL) || (*c2 != NULL)) 149 goto errfin; 150 151 return 0; 152 153 errfin: 154 if (mdata == NULL) { 155 printf("following structures are not equal:\n"); 156 dump_rpcent(rpc1); 157 dump_rpcent(rpc2); 158 } 159 160 return (-1); 161 } 162 163 static void 164 sdump_rpcent(struct rpcent *rpc, char *buffer, size_t buflen) 165 { 166 char **cp; 167 int written; 168 169 written = snprintf(buffer, buflen, "%s %d", 170 rpc->r_name, rpc->r_number); 171 buffer += written; 172 if (written > (int)buflen) 173 return; 174 buflen -= written; 175 176 if (rpc->r_aliases != NULL) { 177 if (*(rpc->r_aliases) != NULL) { 178 for (cp = rpc->r_aliases; *cp; ++cp) { 179 written = snprintf(buffer, buflen, " %s", *cp); 180 buffer += written; 181 if (written > (int)buflen) 182 return; 183 buflen -= written; 184 185 if (buflen == 0) 186 return; 187 } 188 } else 189 snprintf(buffer, buflen, " noaliases"); 190 } else 191 snprintf(buffer, buflen, " (null)"); 192 } 193 194 static int 195 rpcent_read_snapshot_func(struct rpcent *rpc, char *line) 196 { 197 StringList *sl; 198 char *s, *ps, *ts; 199 int i; 200 201 printf("1 line read from snapshot:\n%s\n", line); 202 203 i = 0; 204 sl = NULL; 205 ps = line; 206 memset(rpc, 0, sizeof(struct rpcent)); 207 while ((s = strsep(&ps, " ")) != NULL) { 208 switch (i) { 209 case 0: 210 rpc->r_name = strdup(s); 211 ATF_REQUIRE(rpc->r_name != NULL); 212 break; 213 214 case 1: 215 rpc->r_number = (int)strtol(s, &ts, 10); 216 if (*ts != '\0') { 217 free(rpc->r_name); 218 return (-1); 219 } 220 break; 221 222 default: 223 if (sl == NULL) { 224 if (strcmp(s, "(null)") == 0) 225 return (0); 226 227 sl = sl_init(); 228 ATF_REQUIRE(sl != NULL); 229 230 if (strcmp(s, "noaliases") != 0) { 231 ts = strdup(s); 232 ATF_REQUIRE(ts != NULL); 233 sl_add(sl, ts); 234 } 235 } else { 236 ts = strdup(s); 237 ATF_REQUIRE(ts != NULL); 238 sl_add(sl, ts); 239 } 240 break; 241 } 242 i++; 243 } 244 245 if (i < 3) { 246 free(rpc->r_name); 247 memset(rpc, 0, sizeof(struct rpcent)); 248 return (-1); 249 } 250 251 sl_add(sl, NULL); 252 rpc->r_aliases = sl->sl_str; 253 254 /* NOTE: is it a dirty hack or not? */ 255 free(sl); 256 return (0); 257 } 258 259 static void 260 dump_rpcent(struct rpcent *result) 261 { 262 if (result != NULL) { 263 char buffer[1024]; 264 sdump_rpcent(result, buffer, sizeof(buffer)); 265 printf("%s\n", buffer); 266 } else 267 printf("(null)\n"); 268 } 269 270 static int 271 rpcent_fill_test_data(struct rpcent_test_data *td) 272 { 273 struct rpcent *rpc; 274 275 setrpcent(1); 276 while ((rpc = getrpcent()) != NULL) { 277 if (rpcent_test_correctness(rpc, NULL) == 0) 278 TEST_DATA_APPEND(rpcent, td, rpc); 279 else 280 return (-1); 281 } 282 endrpcent(); 283 284 return (0); 285 } 286 287 static int 288 rpcent_test_correctness(struct rpcent *rpc, void *mdata __unused) 289 { 290 291 printf("testing correctness with the following data:\n"); 292 dump_rpcent(rpc); 293 294 if (rpc == NULL) 295 goto errfin; 296 297 if (rpc->r_name == NULL) 298 goto errfin; 299 300 if (rpc->r_number < 0) 301 goto errfin; 302 303 if (rpc->r_aliases == NULL) 304 goto errfin; 305 306 printf("correct\n"); 307 308 return (0); 309 errfin: 310 printf("incorrect\n"); 311 312 return (-1); 313 } 314 315 /* rpcent_check_ambiguity() is needed when one port+rpc is associated with 316 * more than one piece (these cases are usually marked as PROBLEM in 317 * /etc/peices. This functions is needed also when one piece+rpc is 318 * associated with several ports. We have to check all the rpcent structures 319 * to make sure that rpc really exists and correct */ 320 static int 321 rpcent_check_ambiguity(struct rpcent_test_data *td, struct rpcent *rpc) 322 { 323 324 return (TEST_DATA_FIND(rpcent, td, rpc, compare_rpcent, 325 NULL) != NULL ? 0 : -1); 326 } 327 328 static int 329 rpcent_test_getrpcbyname(struct rpcent *rpc_model, void *mdata) 330 { 331 char **alias; 332 struct rpcent *rpc; 333 334 printf("testing getrpcbyname() with the following data:\n"); 335 dump_rpcent(rpc_model); 336 337 rpc = getrpcbyname(rpc_model->r_name); 338 if (rpcent_test_correctness(rpc, NULL) != 0) 339 goto errfin; 340 341 if ((compare_rpcent(rpc, rpc_model, NULL) != 0) && 342 (rpcent_check_ambiguity((struct rpcent_test_data *)mdata, rpc) 343 !=0)) 344 goto errfin; 345 346 for (alias = rpc_model->r_aliases; *alias; ++alias) { 347 rpc = getrpcbyname(*alias); 348 349 if (rpcent_test_correctness(rpc, NULL) != 0) 350 goto errfin; 351 352 if ((compare_rpcent(rpc, rpc_model, NULL) != 0) && 353 (rpcent_check_ambiguity( 354 (struct rpcent_test_data *)mdata, rpc) != 0)) 355 goto errfin; 356 } 357 358 printf("ok\n"); 359 return (0); 360 361 errfin: 362 printf("not ok\n"); 363 364 return (-1); 365 } 366 367 static int 368 rpcent_test_getrpcbynumber(struct rpcent *rpc_model, void *mdata) 369 { 370 struct rpcent *rpc; 371 372 printf("testing getrpcbyport() with the following data...\n"); 373 dump_rpcent(rpc_model); 374 375 rpc = getrpcbynumber(rpc_model->r_number); 376 if (rpcent_test_correctness(rpc, NULL) != 0 || 377 (compare_rpcent(rpc, rpc_model, NULL) != 0 && 378 rpcent_check_ambiguity((struct rpcent_test_data *)mdata, rpc) 379 != 0)) { 380 printf("not ok\n"); 381 return (-1); 382 } else { 383 printf("ok\n"); 384 return (0); 385 } 386 } 387 388 static int 389 rpcent_test_getrpcent(struct rpcent *rpc, void *mdata __unused) 390 { 391 392 /* 393 * Only correctness can be checked when doing 1-pass test for 394 * getrpcent(). 395 */ 396 return (rpcent_test_correctness(rpc, NULL)); 397 } 398 399 static int 400 run_tests(const char *snapshot_file, enum test_methods method) 401 { 402 struct rpcent_test_data td, td_snap, td_2pass; 403 int rv; 404 405 TEST_DATA_INIT(rpcent, &td, clone_rpcent, free_rpcent); 406 TEST_DATA_INIT(rpcent, &td_snap, clone_rpcent, free_rpcent); 407 if (snapshot_file != NULL) { 408 if (access(snapshot_file, W_OK | R_OK) != 0) { 409 if (errno == ENOENT) 410 method = TEST_BUILD_SNAPSHOT; 411 else { 412 printf("can't access the file %s\n", 413 snapshot_file); 414 415 rv = -1; 416 goto fin; 417 } 418 } else { 419 if (method == TEST_BUILD_SNAPSHOT) { 420 rv = 0; 421 goto fin; 422 } 423 424 TEST_SNAPSHOT_FILE_READ(rpcent, snapshot_file, 425 &td_snap, rpcent_read_snapshot_func); 426 } 427 } 428 429 rv = rpcent_fill_test_data(&td); 430 if (rv == -1) 431 return (-1); 432 switch (method) { 433 case TEST_GETRPCBYNAME: 434 if (snapshot_file == NULL) 435 rv = DO_1PASS_TEST(rpcent, &td, 436 rpcent_test_getrpcbyname, (void *)&td); 437 else 438 rv = DO_1PASS_TEST(rpcent, &td_snap, 439 rpcent_test_getrpcbyname, (void *)&td_snap); 440 break; 441 case TEST_GETRPCBYNUMBER: 442 if (snapshot_file == NULL) 443 rv = DO_1PASS_TEST(rpcent, &td, 444 rpcent_test_getrpcbynumber, (void *)&td); 445 else 446 rv = DO_1PASS_TEST(rpcent, &td_snap, 447 rpcent_test_getrpcbynumber, (void *)&td_snap); 448 break; 449 case TEST_GETRPCENT: 450 if (snapshot_file == NULL) 451 rv = DO_1PASS_TEST(rpcent, &td, rpcent_test_getrpcent, 452 (void *)&td); 453 else 454 rv = DO_2PASS_TEST(rpcent, &td, &td_snap, 455 compare_rpcent, NULL); 456 break; 457 case TEST_GETRPCENT_2PASS: 458 TEST_DATA_INIT(rpcent, &td_2pass, clone_rpcent, free_rpcent); 459 rv = rpcent_fill_test_data(&td_2pass); 460 if (rv != -1) 461 rv = DO_2PASS_TEST(rpcent, &td, &td_2pass, 462 compare_rpcent, NULL); 463 TEST_DATA_DESTROY(rpcent, &td_2pass); 464 break; 465 case TEST_BUILD_SNAPSHOT: 466 if (snapshot_file != NULL) 467 rv = TEST_SNAPSHOT_FILE_WRITE(rpcent, snapshot_file, &td, 468 sdump_rpcent); 469 break; 470 default: 471 rv = 0; 472 break; 473 } 474 475 fin: 476 TEST_DATA_DESTROY(rpcent, &td_snap); 477 TEST_DATA_DESTROY(rpcent, &td); 478 479 return (rv); 480 } 481 482 #define SNAPSHOT_FILE "snapshot_rpc" 483 484 ATF_TC_WITHOUT_HEAD(build_snapshot); 485 ATF_TC_BODY(build_snapshot, tc) 486 { 487 488 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0); 489 } 490 491 ATF_TC_WITHOUT_HEAD(getrpcbyname); 492 ATF_TC_BODY(getrpcbyname, tc) 493 { 494 495 ATF_REQUIRE(run_tests(NULL, TEST_GETRPCBYNAME) == 0); 496 } 497 498 ATF_TC_WITHOUT_HEAD(getrpcbyname_with_snapshot); 499 ATF_TC_BODY(getrpcbyname_with_snapshot, tc) 500 { 501 502 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0); 503 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETRPCBYNAME) == 0); 504 } 505 506 ATF_TC_WITHOUT_HEAD(getrpcbynumber); 507 ATF_TC_BODY(getrpcbynumber, tc) 508 { 509 510 ATF_REQUIRE(run_tests(NULL, TEST_GETRPCBYNUMBER) == 0); 511 } 512 513 ATF_TC_WITHOUT_HEAD(getrpcbynumber_with_snapshot); 514 ATF_TC_BODY(getrpcbynumber_with_snapshot, tc) 515 { 516 517 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0); 518 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETRPCBYNUMBER) == 0); 519 } 520 521 ATF_TC_WITHOUT_HEAD(getrpcbyent); 522 ATF_TC_BODY(getrpcbyent, tc) 523 { 524 525 ATF_REQUIRE(run_tests(NULL, TEST_GETRPCENT) == 0); 526 } 527 528 ATF_TC_WITHOUT_HEAD(getrpcbyent_with_snapshot); 529 ATF_TC_BODY(getrpcbyent_with_snapshot, tc) 530 { 531 532 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0); 533 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETRPCENT) == 0); 534 } 535 536 ATF_TC_WITHOUT_HEAD(getrpcbyent_with_two_pass); 537 ATF_TC_BODY(getrpcbyent_with_two_pass, tc) 538 { 539 540 ATF_REQUIRE(run_tests(NULL, TEST_GETRPCENT_2PASS) == 0); 541 } 542 543 ATF_TP_ADD_TCS(tp) 544 { 545 546 ATF_TP_ADD_TC(tp, build_snapshot); 547 ATF_TP_ADD_TC(tp, getrpcbyname); 548 ATF_TP_ADD_TC(tp, getrpcbyname_with_snapshot); 549 ATF_TP_ADD_TC(tp, getrpcbynumber); 550 ATF_TP_ADD_TC(tp, getrpcbynumber_with_snapshot); 551 ATF_TP_ADD_TC(tp, getrpcbyent); 552 ATF_TP_ADD_TC(tp, getrpcbyent_with_snapshot); 553 ATF_TP_ADD_TC(tp, getrpcbyent_with_two_pass); 554 555 return (atf_no_error()); 556 } 557