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 = 1, 46 TEST_GETGRNAM = 2, 47 TEST_GETGRGID = 4, 48 TEST_GETGRENT_2PASS = 8, 49 TEST_BUILD_SNAPSHOT = 16, 50 }; 51 52 static enum test_methods method = TEST_BUILD_SNAPSHOT; 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 *, 68 struct group *); 69 static int group_fill_test_data(struct group_test_data *); 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(1, (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 != '\0' || *c2 != '\0') 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 > buflen) 183 return; 184 buflen -= written; 185 186 if (grp->gr_mem != NULL) { 187 if (*(grp->gr_mem) != '\0') { 188 for (cp = grp->gr_mem; *cp; ++cp) { 189 written = snprintf(buffer, buflen, " %s",*cp); 190 buffer += written; 191 if (written > buflen) 192 return; 193 buflen -= written; 194 195 if (buflen == 0) 196 return; 197 } 198 } else 199 snprintf(buffer, buflen, " nomem"); 200 } else 201 snprintf(buffer, buflen, " (null)"); 202 } 203 204 static int 205 group_read_snapshot_func(struct group *grp, char *line) 206 { 207 StringList *sl; 208 char *s, *ps, *ts; 209 int i; 210 211 printf("1 line read from snapshot:\n%s\n", line); 212 213 i = 0; 214 sl = NULL; 215 ps = line; 216 memset(grp, 0, sizeof(struct group)); 217 while ((s = strsep(&ps, " ")) != NULL) { 218 switch (i) { 219 case 0: 220 grp->gr_name = strdup(s); 221 ATF_REQUIRE(grp->gr_name != NULL); 222 break; 223 224 case 1: 225 grp->gr_passwd = strdup(s); 226 ATF_REQUIRE(grp->gr_passwd != NULL); 227 break; 228 229 case 2: 230 grp->gr_gid = (gid_t)strtol(s, &ts, 10); 231 if (*ts != '\0') { 232 free(grp->gr_name); 233 free(grp->gr_passwd); 234 grp->gr_name = NULL; 235 grp->gr_passwd = NULL; 236 return (-1); 237 } 238 break; 239 240 default: 241 if (sl == NULL) { 242 if (strcmp(s, "(null)") == 0) 243 return (0); 244 245 sl = sl_init(); 246 ATF_REQUIRE(sl != NULL); 247 248 if (strcmp(s, "nomem") != 0) { 249 ts = strdup(s); 250 ATF_REQUIRE(ts != NULL); 251 sl_add(sl, ts); 252 } 253 } else { 254 ts = strdup(s); 255 ATF_REQUIRE(ts != NULL); 256 sl_add(sl, ts); 257 } 258 break; 259 } 260 ++i; 261 } 262 263 if (i < 3) { 264 free(grp->gr_name); 265 free(grp->gr_passwd); 266 memset(grp, 0, sizeof(struct group)); 267 return (-1); 268 } 269 270 sl_add(sl, NULL); 271 grp->gr_mem = sl->sl_str; 272 273 /* NOTE: is it a dirty hack or not? */ 274 free(sl); 275 return (0); 276 } 277 278 static void 279 dump_group(struct group *result) 280 { 281 if (result != NULL) { 282 char buffer[1024]; 283 sdump_group(result, buffer, sizeof(buffer)); 284 printf("%s\n", buffer); 285 } else 286 printf("(null)\n"); 287 } 288 289 static int 290 group_fill_test_data(struct group_test_data *td) 291 { 292 struct group *grp; 293 294 setgroupent(1); 295 while ((grp = getgrent()) != NULL) { 296 if (group_test_correctness(grp, NULL) == 0) 297 TEST_DATA_APPEND(group, td, grp); 298 else 299 return (-1); 300 } 301 endgrent(); 302 303 return (0); 304 } 305 306 static int 307 group_test_correctness(struct group *grp, void *mdata) 308 { 309 printf("testing correctness with the following data:\n"); 310 dump_group(grp); 311 312 if (grp == NULL) 313 goto errfin; 314 315 if (grp->gr_name == NULL) 316 goto errfin; 317 318 if (grp->gr_passwd == NULL) 319 goto errfin; 320 321 if (grp->gr_mem == NULL) 322 goto errfin; 323 324 printf("correct\n"); 325 326 return (0); 327 errfin: 328 printf("incorrect\n"); 329 330 return (-1); 331 } 332 333 /* group_check_ambiguity() is needed here because when doing the getgrent() 334 * calls sequence, records from different nsswitch sources can be different, 335 * though having the same pw_name/pw_uid */ 336 static int 337 group_check_ambiguity(struct group_test_data *td, struct group *pwd) 338 { 339 340 return (TEST_DATA_FIND(group, td, pwd, compare_group, 341 NULL) != NULL ? 0 : -1); 342 } 343 344 static int 345 group_test_getgrnam(struct group *grp_model, void *mdata) 346 { 347 struct group *grp; 348 349 printf("testing getgrnam() with the following data:\n"); 350 dump_group(grp_model); 351 352 grp = getgrnam(grp_model->gr_name); 353 if (group_test_correctness(grp, NULL) != 0) 354 goto errfin; 355 356 if (compare_group(grp, grp_model, NULL) != 0 && 357 group_check_ambiguity((struct group_test_data *)mdata, grp) != 0) 358 goto errfin; 359 360 return (0); 361 362 errfin: 363 return (-1); 364 } 365 366 static int 367 group_test_getgrgid(struct group *grp_model, void *mdata) 368 { 369 struct group *grp; 370 371 printf("testing getgrgid() with the following data...\n"); 372 dump_group(grp_model); 373 374 grp = getgrgid(grp_model->gr_gid); 375 if (group_test_correctness(grp, NULL) != 0 || 376 (compare_group(grp, grp_model, NULL) != 0 && 377 group_check_ambiguity((struct group_test_data *)mdata, grp) != 0)) { 378 return (-1); 379 } else { 380 return (0); 381 } 382 } 383 384 static int 385 group_test_getgrent(struct group *grp, void *mdata) 386 { 387 /* Only correctness can be checked when doing 1-pass test for 388 * getgrent(). */ 389 return (group_test_correctness(grp, NULL)); 390 } 391 392 static int 393 run_tests(const char *snapshot_file, enum test_methods method) 394 { 395 struct group_test_data td, td_snap, td_2pass; 396 int rv; 397 398 TEST_DATA_INIT(group, &td, clone_group, free_group); 399 TEST_DATA_INIT(group, &td_snap, clone_group, free_group); 400 if (snapshot_file != NULL) { 401 if (access(snapshot_file, W_OK | R_OK) != 0) { 402 if (errno == ENOENT) 403 method = TEST_BUILD_SNAPSHOT; 404 else { 405 printf("can't access the file %s\n", 406 snapshot_file); 407 408 rv = -1; 409 goto fin; 410 } 411 } else { 412 if (method == TEST_BUILD_SNAPSHOT) { 413 rv = 0; 414 goto fin; 415 } 416 417 TEST_SNAPSHOT_FILE_READ(group, snapshot_file, 418 &td_snap, group_read_snapshot_func); 419 } 420 } 421 422 rv = group_fill_test_data(&td); 423 if (rv == -1) 424 return (-1); 425 switch (method) { 426 case TEST_GETGRNAM: 427 if (snapshot_file == NULL) 428 rv = DO_1PASS_TEST(group, &td, 429 group_test_getgrnam, (void *)&td); 430 else 431 rv = DO_1PASS_TEST(group, &td_snap, 432 group_test_getgrnam, (void *)&td_snap); 433 break; 434 case TEST_GETGRGID: 435 if (snapshot_file == NULL) 436 rv = DO_1PASS_TEST(group, &td, 437 group_test_getgrgid, (void *)&td); 438 else 439 rv = DO_1PASS_TEST(group, &td_snap, 440 group_test_getgrgid, (void *)&td_snap); 441 break; 442 case TEST_GETGRENT: 443 if (snapshot_file == NULL) 444 rv = DO_1PASS_TEST(group, &td, group_test_getgrent, 445 (void *)&td); 446 else 447 rv = DO_2PASS_TEST(group, &td, &td_snap, 448 compare_group, NULL); 449 break; 450 case TEST_GETGRENT_2PASS: 451 TEST_DATA_INIT(group, &td_2pass, clone_group, free_group); 452 rv = group_fill_test_data(&td_2pass); 453 if (rv != -1) 454 rv = DO_2PASS_TEST(group, &td, &td_2pass, 455 compare_group, NULL); 456 TEST_DATA_DESTROY(group, &td_2pass); 457 break; 458 case TEST_BUILD_SNAPSHOT: 459 if (snapshot_file != NULL) 460 rv = TEST_SNAPSHOT_FILE_WRITE(group, snapshot_file, &td, 461 sdump_group); 462 break; 463 default: 464 rv = 0; 465 break; 466 } 467 468 fin: 469 TEST_DATA_DESTROY(group, &td_snap); 470 TEST_DATA_DESTROY(group, &td); 471 472 return (rv); 473 } 474 475 #define SNAPSHOT_FILE "snapshot_grp" 476 477 ATF_TC_BODY(getgrent, tc) 478 { 479 480 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRENT) == 0); 481 } 482 483 ATF_TC_WITHOUT_HEAD(getgrent_with_snapshot); 484 ATF_TC_BODY(getgrent_with_snapshot, tc) 485 { 486 487 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0); 488 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRENT) == 0); 489 } 490 491 ATF_TC_WITHOUT_HEAD(getgrent_with_two_pass); 492 ATF_TC_BODY(getgrent_with_two_pass, tc) 493 { 494 495 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRENT_2PASS) == 0); 496 } 497 498 ATF_TC_WITHOUT_HEAD(getgrgid); 499 ATF_TC_BODY(getgrgid, tc) 500 { 501 502 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRGID) == 0); 503 } 504 505 ATF_TC_WITHOUT_HEAD(getgrgid_with_snapshot); 506 ATF_TC_BODY(getgrgid_with_snapshot, tc) 507 { 508 509 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0); 510 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRGID) == 0); 511 } 512 513 ATF_TC_WITHOUT_HEAD(getgrnam); 514 ATF_TC_BODY(getgrnam, tc) 515 { 516 517 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRNAM) == 0); 518 } 519 520 ATF_TC_WITHOUT_HEAD(getgrnam_with_snapshot); 521 ATF_TC_BODY(getgrnam_with_snapshot, tc) 522 { 523 524 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0); 525 ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETGRNAM) == 0); 526 } 527 528 ATF_TC_WITHOUT_HEAD(getgrent); 529 ATF_TP_ADD_TCS(tp) 530 { 531 532 ATF_TP_ADD_TC(tp, getgrent); 533 ATF_TP_ADD_TC(tp, getgrent_with_snapshot); 534 ATF_TP_ADD_TC(tp, getgrent_with_two_pass); 535 ATF_TP_ADD_TC(tp, getgrgid); 536 ATF_TP_ADD_TC(tp, getgrgid_with_snapshot); 537 ATF_TP_ADD_TC(tp, getgrnam); 538 ATF_TP_ADD_TC(tp, getgrnam_with_snapshot); 539 540 return (atf_no_error()); 541 } 542