tests.c (12452B)
1 #if TEST___PROGNAME 2 int 3 main(void) 4 { 5 extern char *__progname; 6 7 return !__progname; 8 } 9 #endif /* TEST___PROGNAME */ 10 #if TEST_ARC4RANDOM 11 #include <stdlib.h> 12 13 int 14 main(void) 15 { 16 return (arc4random() + 1) ? 0 : 1; 17 } 18 #endif /* TEST_ARC4RANDOM */ 19 #if TEST_B64_NTOP 20 #include <netinet/in.h> 21 #include <resolv.h> 22 23 int 24 main(void) 25 { 26 const char *src = "hello world"; 27 char output[1024]; 28 29 return b64_ntop((const unsigned char *)src, 11, output, sizeof(output)) > 0 ? 0 : 1; 30 } 31 #endif /* TEST_B64_NTOP */ 32 #if TEST_CAPSICUM 33 #include <sys/capsicum.h> 34 35 int 36 main(void) 37 { 38 cap_enter(); 39 return(0); 40 } 41 #endif /* TEST_CAPSICUM */ 42 #if TEST_CRYPT 43 #if defined(__linux__) 44 # define _GNU_SOURCE /* old glibc */ 45 # define _DEFAULT_SOURCE /* new glibc */ 46 #endif 47 #if defined(__sun) 48 # ifndef _XOPEN_SOURCE /* SunOS already defines */ 49 # define _XOPEN_SOURCE /* XPGx */ 50 # endif 51 # define _XOPEN_SOURCE_EXTENDED 1 /* XPG4v2 */ 52 # ifndef __EXTENSIONS__ /* SunOS already defines */ 53 # define __EXTENSIONS__ /* reallocarray, etc. */ 54 # endif 55 #endif 56 #include <unistd.h> 57 58 int main(void) 59 { 60 char *v; 61 62 v = crypt("this_is_a_key", "123455"); 63 return v == NULL; 64 } 65 #endif /* TEST_CRYPT */ 66 #if TEST_ENDIAN_H 67 #ifdef __linux__ 68 # define _DEFAULT_SOURCE 69 #endif 70 #include <endian.h> 71 72 int 73 main(void) 74 { 75 return !htole32(23); 76 } 77 #endif /* TEST_ENDIAN_H */ 78 #if TEST_ERR 79 /* 80 * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org> 81 * 82 * Permission to use, copy, modify, and distribute this software for any 83 * purpose with or without fee is hereby granted, provided that the above 84 * copyright notice and this permission notice appear in all copies. 85 * 86 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 87 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 88 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 89 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 90 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 91 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 92 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 93 */ 94 95 #include <err.h> 96 #include <errno.h> 97 98 int 99 main(void) 100 { 101 warnx("%d. warnx", 1); 102 warnc(ENOENT, "%d. warn", ENOENT); 103 warn("%d. warn", 2); 104 err(0, "%d. err", 3); 105 errx(0, "%d. err", 3); 106 errc(0, ENOENT, "%d. err", 3); 107 /* NOTREACHED */ 108 return 1; 109 } 110 #endif /* TEST_ERR */ 111 #if TEST_EXPLICIT_BZERO 112 #include <string.h> 113 114 int 115 main(void) 116 { 117 char foo[10]; 118 119 explicit_bzero(foo, sizeof(foo)); 120 return(0); 121 } 122 #endif /* TEST_EXPLICIT_BZERO */ 123 #if TEST_FTS 124 #include <sys/types.h> 125 #include <sys/stat.h> 126 #include <fts.h> 127 128 int 129 main(void) 130 { 131 const char *argv[2]; 132 FTS *ftsp; 133 FTSENT *entry; 134 135 argv[0] = "."; 136 argv[1] = (char *)NULL; 137 138 ftsp = fts_open((char * const *)argv, 139 FTS_PHYSICAL | FTS_NOCHDIR, NULL); 140 141 if (ftsp == NULL) 142 return 1; 143 144 entry = fts_read(ftsp); 145 146 if (entry == NULL) 147 return 1; 148 149 if (fts_set(ftsp, entry, FTS_SKIP) != 0) 150 return 1; 151 152 if (fts_close(ftsp) != 0) 153 return 1; 154 155 return 0; 156 } 157 #endif /* TEST_FTS */ 158 #if TEST_GETEXECNAME 159 #include <stdlib.h> 160 161 int 162 main(void) 163 { 164 const char * progname; 165 166 progname = getexecname(); 167 return progname == NULL; 168 } 169 #endif /* TEST_GETEXECNAME */ 170 #if TEST_GETPROGNAME 171 #include <stdlib.h> 172 173 int 174 main(void) 175 { 176 const char * progname; 177 178 progname = getprogname(); 179 return progname == NULL; 180 } 181 #endif /* TEST_GETPROGNAME */ 182 #if TEST_INFTIM 183 /* 184 * Linux doesn't (always?) have this. 185 */ 186 187 #include <poll.h> 188 #include <stdio.h> 189 190 int 191 main(void) 192 { 193 printf("INFTIM is defined to be %ld\n", (long)INFTIM); 194 return 0; 195 } 196 #endif /* TEST_INFTIM */ 197 #if TEST_LIB_SOCKET 198 #include <sys/socket.h> 199 200 int 201 main(void) 202 { 203 int fds[2], c; 204 205 c = socketpair(AF_UNIX, SOCK_STREAM, 0, fds); 206 return c == -1; 207 } 208 #endif /* TEST_LIB_SOCKET */ 209 #if TEST_MD5 210 #include <sys/types.h> 211 #include <md5.h> 212 213 int main(void) 214 { 215 MD5_CTX ctx; 216 char result[MD5_DIGEST_STRING_LENGTH]; 217 218 MD5Init(&ctx); 219 MD5Update(&ctx, (const unsigned char *)"abcd", 4); 220 MD5End(&ctx, result); 221 222 return 0; 223 } 224 #endif /* TEST_MD5 */ 225 #if TEST_MEMMEM 226 #define _GNU_SOURCE 227 #include <string.h> 228 229 int 230 main(void) 231 { 232 char *a = memmem("hello, world", strlen("hello, world"), "world", strlen("world")); 233 return(NULL == a); 234 } 235 #endif /* TEST_MEMMEM */ 236 #if TEST_MEMRCHR 237 #if defined(__linux__) || defined(__MINT__) 238 #define _GNU_SOURCE /* See test-*.c what needs this. */ 239 #endif 240 #include <string.h> 241 242 int 243 main(void) 244 { 245 const char *buf = "abcdef"; 246 void *res; 247 248 res = memrchr(buf, 'a', strlen(buf)); 249 return(NULL == res ? 1 : 0); 250 } 251 #endif /* TEST_MEMRCHR */ 252 #if TEST_MEMSET_S 253 #include <string.h> 254 255 int main(void) 256 { 257 char buf[10]; 258 memset_s(buf, 0, 'c', sizeof(buf)); 259 return 0; 260 } 261 #endif /* TEST_MEMSET_S */ 262 #if TEST_MKFIFOAT 263 #include <sys/stat.h> 264 #include <fcntl.h> 265 266 int main(void) { 267 mkfifoat(AT_FDCWD, "this/path/should/not/exist", 0600); 268 return 0; 269 } 270 #endif /* TEST_MKFIFOAT */ 271 #if TEST_MKNODAT 272 #include <sys/stat.h> 273 #include <fcntl.h> 274 275 int main(void) { 276 mknodat(AT_FDCWD, "this/path/should/not/exist", S_IFIFO | 0600, 0); 277 return 0; 278 } 279 #endif /* TEST_MKNODAT */ 280 #if TEST_OSBYTEORDER_H 281 #include <libkern/OSByteOrder.h> 282 283 int 284 main(void) 285 { 286 return !OSSwapHostToLittleInt32(23); 287 } 288 #endif /* TEST_OSBYTEORDER_H */ 289 #if TEST_PATH_MAX 290 /* 291 * POSIX allows PATH_MAX to not be defined, see 292 * http://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html; 293 * the GNU Hurd is an example of a system not having it. 294 * 295 * Arguably, it would be better to test sysconf(_SC_PATH_MAX), 296 * but since the individual *.c files include "config.h" before 297 * <limits.h>, overriding an excessive value of PATH_MAX from 298 * "config.h" is impossible anyway, so for now, the simplest 299 * fix is to provide a value only on systems not having any. 300 * So far, we encountered no system defining PATH_MAX to an 301 * impractically large value, even though POSIX explicitly 302 * allows that. 303 * 304 * The real fix would be to replace all static buffers of size 305 * PATH_MAX by dynamically allocated buffers. But that is 306 * somewhat intrusive because it touches several files and 307 * because it requires changing struct mlink in mandocdb.c. 308 * So i'm postponing that for now. 309 */ 310 311 #include <limits.h> 312 #include <stdio.h> 313 314 int 315 main(void) 316 { 317 printf("PATH_MAX is defined to be %ld\n", (long)PATH_MAX); 318 return 0; 319 } 320 #endif /* TEST_PATH_MAX */ 321 #if TEST_PLEDGE 322 #include <unistd.h> 323 324 int 325 main(void) 326 { 327 return !!pledge("stdio", NULL); 328 } 329 #endif /* TEST_PLEDGE */ 330 #if TEST_PROGRAM_INVOCATION_SHORT_NAME 331 #define _GNU_SOURCE /* See feature_test_macros(7) */ 332 #include <errno.h> 333 334 int 335 main(void) 336 { 337 338 return !program_invocation_short_name; 339 } 340 #endif /* TEST_PROGRAM_INVOCATION_SHORT_NAME */ 341 #if TEST_READPASSPHRASE 342 #include <stddef.h> 343 #include <readpassphrase.h> 344 345 int 346 main(void) 347 { 348 return !!readpassphrase("prompt: ", NULL, 0, 0); 349 } 350 #endif /* TEST_READPASSPHRASE */ 351 #if TEST_REALLOCARRAY 352 #ifdef __NetBSD__ 353 # define _OPENBSD_SOURCE 354 #endif 355 #include <stdlib.h> 356 357 int 358 main(void) 359 { 360 return !reallocarray(NULL, 2, 2); 361 } 362 #endif /* TEST_REALLOCARRAY */ 363 #if TEST_RECALLOCARRAY 364 #include <stdlib.h> 365 366 int 367 main(void) 368 { 369 return !recallocarray(NULL, 0, 2, 2); 370 } 371 #endif /* TEST_RECALLOCARRAY */ 372 #if TEST_SANDBOX_INIT 373 #include <sandbox.h> 374 375 int 376 main(void) 377 { 378 char *ep; 379 int rc; 380 381 rc = sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, &ep); 382 if (-1 == rc) 383 sandbox_free_error(ep); 384 return(-1 == rc); 385 } 386 #endif /* TEST_SANDBOX_INIT */ 387 #if TEST_SECCOMP_FILTER 388 #include <sys/prctl.h> 389 #include <linux/seccomp.h> 390 #include <errno.h> 391 392 int 393 main(void) 394 { 395 396 prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, 0); 397 return(EFAULT == errno ? 0 : 1); 398 } 399 #endif /* TEST_SECCOMP_FILTER */ 400 #if TEST_SETRESGID 401 #define _GNU_SOURCE /* linux */ 402 #include <sys/types.h> 403 #include <unistd.h> 404 405 int 406 main(void) 407 { 408 return setresgid(-1, -1, -1) == -1; 409 } 410 #endif /* TEST_SETRESGID */ 411 #if TEST_SETRESUID 412 #define _GNU_SOURCE /* linux */ 413 #include <sys/types.h> 414 #include <unistd.h> 415 416 int 417 main(void) 418 { 419 return setresuid(-1, -1, -1) == -1; 420 } 421 #endif /* TEST_SETRESUID */ 422 #if TEST_SHA2_H 423 #include <sys/types.h> 424 #include <sha2.h> 425 426 int main(void) 427 { 428 SHA2_CTX ctx; 429 char result[SHA256_DIGEST_STRING_LENGTH]; 430 431 SHA256Init(&ctx); 432 SHA256Update(&ctx, (const unsigned char *)"abcd", 4); 433 SHA256End(&ctx, result); 434 435 return 0; 436 } 437 #endif /* TEST_SHA2_H */ 438 #if TEST_SOCK_NONBLOCK 439 /* 440 * Linux doesn't (always?) have this. 441 */ 442 443 #include <sys/socket.h> 444 445 int 446 main(void) 447 { 448 int fd[2]; 449 socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fd); 450 return 0; 451 } 452 #endif /* TEST_SOCK_NONBLOCK */ 453 #if TEST_STATIC 454 int 455 main(void) 456 { 457 return 0; /* not meant to do anything */ 458 } 459 #endif /* TEST_STATIC */ 460 #if TEST_STRLCAT 461 #include <string.h> 462 463 int 464 main(void) 465 { 466 char buf[3] = "a"; 467 return ! (strlcat(buf, "b", sizeof(buf)) == 2 && 468 buf[0] == 'a' && buf[1] == 'b' && buf[2] == '\0'); 469 } 470 #endif /* TEST_STRLCAT */ 471 #if TEST_STRLCPY 472 #include <string.h> 473 474 int 475 main(void) 476 { 477 char buf[2] = ""; 478 return ! (strlcpy(buf, "a", sizeof(buf)) == 1 && 479 buf[0] == 'a' && buf[1] == '\0'); 480 } 481 #endif /* TEST_STRLCPY */ 482 #if TEST_STRNDUP 483 #include <string.h> 484 485 int 486 main(void) 487 { 488 const char *foo = "bar"; 489 char *baz; 490 491 baz = strndup(foo, 1); 492 return(0 != strcmp(baz, "b")); 493 } 494 #endif /* TEST_STRNDUP */ 495 #if TEST_STRNLEN 496 #include <string.h> 497 498 int 499 main(void) 500 { 501 const char *foo = "bar"; 502 size_t sz; 503 504 sz = strnlen(foo, 1); 505 return(1 != sz); 506 } 507 #endif /* TEST_STRNLEN */ 508 #if TEST_STRTONUM 509 /* 510 * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org> 511 * 512 * Permission to use, copy, modify, and distribute this software for any 513 * purpose with or without fee is hereby granted, provided that the above 514 * copyright notice and this permission notice appear in all copies. 515 * 516 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 517 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 518 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 519 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 520 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 521 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 522 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 523 */ 524 #ifdef __NetBSD__ 525 # define _OPENBSD_SOURCE 526 #endif 527 #include <stdlib.h> 528 529 int 530 main(void) 531 { 532 const char *errstr; 533 534 if (strtonum("1", 0, 2, &errstr) != 1) 535 return 1; 536 if (errstr != NULL) 537 return 2; 538 if (strtonum("1x", 0, 2, &errstr) != 0) 539 return 3; 540 if (errstr == NULL) 541 return 4; 542 if (strtonum("2", 0, 1, &errstr) != 0) 543 return 5; 544 if (errstr == NULL) 545 return 6; 546 if (strtonum("0", 1, 2, &errstr) != 0) 547 return 7; 548 if (errstr == NULL) 549 return 8; 550 return 0; 551 } 552 #endif /* TEST_STRTONUM */ 553 #if TEST_SYS_BYTEORDER_H 554 #include <sys/byteorder.h> 555 556 int 557 main(void) 558 { 559 return !LE_32(23); 560 } 561 #endif /* TEST_SYS_BYTEORDER_H */ 562 #if TEST_SYS_ENDIAN_H 563 #include <sys/endian.h> 564 565 int 566 main(void) 567 { 568 return !htole32(23); 569 } 570 #endif /* TEST_SYS_ENDIAN_H */ 571 #if TEST_SYS_MKDEV_H 572 #include <sys/types.h> 573 #include <sys/mkdev.h> 574 575 int 576 main(void) 577 { 578 return !minor(0); 579 } 580 #endif /* TEST_SYS_MKDEV_H */ 581 #if TEST_SYS_QUEUE 582 #include <sys/queue.h> 583 #include <stddef.h> 584 585 struct foo { 586 int bar; 587 TAILQ_ENTRY(foo) entries; 588 }; 589 590 TAILQ_HEAD(fooq, foo); 591 592 int 593 main(void) 594 { 595 struct fooq foo_q; 596 struct foo *p, *tmp; 597 int i = 0; 598 599 TAILQ_INIT(&foo_q); 600 601 /* 602 * Use TAILQ_FOREACH_SAFE because some systems (e.g., Linux) 603 * have TAILQ_FOREACH but not the safe variant. 604 */ 605 606 TAILQ_FOREACH_SAFE(p, &foo_q, entries, tmp) 607 p->bar = i++; 608 return 0; 609 } 610 #endif /* TEST_SYS_QUEUE */ 611 #if TEST_SYS_SYSMACROS_H 612 #include <sys/sysmacros.h> 613 614 int 615 main(void) 616 { 617 return !minor(0); 618 } 619 #endif /* TEST_SYS_SYSMACROS_H */ 620 #if TEST_SYS_TREE 621 #include <sys/tree.h> 622 #include <stdlib.h> 623 624 struct node { 625 RB_ENTRY(node) entry; 626 int i; 627 }; 628 629 static int 630 intcmp(struct node *e1, struct node *e2) 631 { 632 return (e1->i < e2->i ? -1 : e1->i > e2->i); 633 } 634 635 RB_HEAD(inttree, node) head = RB_INITIALIZER(&head); 636 RB_PROTOTYPE(inttree, node, entry, intcmp) 637 RB_GENERATE(inttree, node, entry, intcmp) 638 639 int testdata[] = { 640 20, 16, 17, 13, 3, 6, 1, 8, 2, 4 641 }; 642 643 int 644 main(void) 645 { 646 size_t i; 647 struct node *n; 648 649 for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) { 650 if ((n = malloc(sizeof(struct node))) == NULL) 651 return 1; 652 n->i = testdata[i]; 653 RB_INSERT(inttree, &head, n); 654 } 655 656 return 0; 657 } 658 659 #endif /* TEST_SYS_TREE */ 660 #if TEST_UNVEIL 661 #include <unistd.h> 662 663 int 664 main(void) 665 { 666 return -1 != unveil(NULL, NULL); 667 } 668 #endif /* TEST_UNVEIL */ 669 #if TEST_WAIT_ANY 670 #include <sys/wait.h> 671 672 int 673 main(void) 674 { 675 int st; 676 677 return waitpid(WAIT_ANY, &st, WNOHANG) != -1; 678 } 679 #endif /* TEST_WAIT_ANY */