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