12 extern char *__progname;
16 #endif /* TEST___PROGNAME */
23 return (arc4random() + 1) ? 0 : 1;
25 #endif /* TEST_ARC4RANDOM */
27 #include <netinet/in.h>
33 const char *src = "hello world";
36 return b64_ntop((const unsigned char *)src, 11, output, sizeof(output)) > 0 ? 0 : 1;
38 #endif /* TEST_B64_NTOP */
40 #include <sys/capsicum.h>
48 #endif /* TEST_CAPSICUM */
50 #if defined(__linux__)
51 # define _GNU_SOURCE /* old glibc */
52 # define _DEFAULT_SOURCE /* new glibc */
55 # ifndef _XOPEN_SOURCE /* SunOS already defines */
56 # define _XOPEN_SOURCE /* XPGx */
58 # define _XOPEN_SOURCE_EXTENDED 1 /* XPG4v2 */
59 # ifndef __EXTENSIONS__ /* SunOS already defines */
60 # define __EXTENSIONS__ /* reallocarray, etc. */
69 v = crypt("this_is_a_key", "123455");
72 #endif /* TEST_CRYPT */
73 #if TEST_CRYPT_NEWHASH
74 #include <pwd.h> /* _PASSWORD_LEN */
80 const char *v = "password";
81 char hash[_PASSWORD_LEN];
83 if (crypt_newhash(v, "bcrypt,a", hash, sizeof(hash)) == -1)
85 if (crypt_checkpass(v, hash) == -1)
90 #endif /* TEST_CRYPT_NEWHASH */
93 # define _DEFAULT_SOURCE
102 #endif /* TEST_ENDIAN_H */
105 * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
107 * Permission to use, copy, modify, and distribute this software for any
108 * purpose with or without fee is hereby granted, provided that the above
109 * copyright notice and this permission notice appear in all copies.
111 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
112 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
113 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
114 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
115 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
116 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
117 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
126 warnx("%d. warnx", 1);
127 warnc(ENOENT, "%d. warn", ENOENT);
129 err(0, "%d. err", 3);
130 errx(0, "%d. err", 3);
131 errc(0, ENOENT, "%d. err", 3);
135 #endif /* TEST_ERR */
136 #if TEST_EXPLICIT_BZERO
144 explicit_bzero(foo, sizeof(foo));
147 #endif /* TEST_EXPLICIT_BZERO */
154 flock(0, LOCK_SH|LOCK_NB);
157 #endif /* TEST_FLOCK */
167 #endif /* TEST_FREEZERO */
170 #include <sys/types.h>
171 #include <sys/stat.h>
182 argv[1] = (char *)NULL;
184 ftsp = fts_open((char * const *)argv,
185 FTS_PHYSICAL | FTS_NOCHDIR, NULL);
190 entry = fts_read(ftsp);
195 if (fts_set(ftsp, entry, FTS_SKIP) != 0)
198 if (fts_close(ftsp) != 0)
203 #endif /* TEST_FTS */
204 #if TEST_GETDTABLECOUNT
210 return getdtablecount();
212 #endif /* TEST_GETDTABLECOUNT */
213 #if TEST_GETDTABLESIZE
219 return getdtablesize();
221 #endif /* TEST_GETDTABLESIZE */
228 const char * progname;
230 progname = getexecname();
231 return progname == NULL;
233 #endif /* TEST_GETEXECNAME */
240 const char * progname;
242 progname = getprogname();
243 return progname == NULL;
245 #endif /* TEST_GETPROGNAME */
247 #include <sys/types.h>
248 #include <sys/queue.h>
262 #endif /* TEST_LIB_IMSG */
265 * Linux doesn't (always?) have this.
274 printf("INFTIM is defined to be %ld\n", (long)INFTIM);
277 #endif /* TEST_INFTIM */
279 #include <linux/landlock.h>
281 #include <sys/prctl.h>
282 #include <sys/syscall.h>
286 #ifndef landlock_create_ruleset
287 static inline int landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,
288 const size_t size, const __u32 flags)
290 return syscall(__NR_landlock_create_ruleset, attr, size, flags);
294 #ifndef landlock_restrict_self
295 static inline int landlock_restrict_self(const int ruleset_fd,
298 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
305 uint64_t mask = LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE;
306 struct landlock_ruleset_attr rules = {
307 .handled_access_fs = mask
309 int fd = landlock_create_ruleset(&rules, sizeof(rules), 0);
311 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
313 return landlock_restrict_self(fd, 0) ? 1 : 0;
315 #endif /* TEST_LANDLOCK */
317 #include <sys/socket.h>
324 c = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
327 #endif /* TEST_LIB_SOCKET */
329 #include <sys/types.h>
335 char result[MD5_DIGEST_STRING_LENGTH];
338 MD5Update(&ctx, (const unsigned char *)"abcd", 4);
339 MD5End(&ctx, result);
343 #endif /* TEST_MD5 */
351 char *a = memmem("hello, world", strlen("hello, world"), "world", strlen("world"));
354 #endif /* TEST_MEMMEM */
356 #if defined(__linux__) || defined(__MINT__)
357 #define _GNU_SOURCE /* See test-*.c what needs this. */
364 const char *buf = "abcdef";
367 res = memrchr(buf, 'a', strlen(buf));
368 return(NULL == res ? 1 : 0);
370 #endif /* TEST_MEMRCHR */
377 memset_s(buf, 0, 'c', sizeof(buf));
380 #endif /* TEST_MEMSET_S */
382 #include <sys/stat.h>
386 mkfifoat(AT_FDCWD, "this/path/should/not/exist", 0600);
389 #endif /* TEST_MKFIFOAT */
391 #include <sys/stat.h>
395 mknodat(AT_FDCWD, "this/path/should/not/exist", S_IFIFO | 0600, 0);
398 #endif /* TEST_MKNODAT */
403 main(int argc, char **argv)
406 getopt(argc, argv, "");
409 #endif /* TEST_OPTRESET */
410 #if TEST_OSBYTEORDER_H
411 #include <libkern/OSByteOrder.h>
416 return !OSSwapHostToLittleInt32(23);
418 #endif /* TEST_OSBYTEORDER_H */
421 * POSIX allows PATH_MAX to not be defined, see
422 * http://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html;
423 * the GNU Hurd is an example of a system not having it.
425 * Arguably, it would be better to test sysconf(_SC_PATH_MAX),
426 * but since the individual *.c files include "config.h" before
427 * <limits.h>, overriding an excessive value of PATH_MAX from
428 * "config.h" is impossible anyway, so for now, the simplest
429 * fix is to provide a value only on systems not having any.
430 * So far, we encountered no system defining PATH_MAX to an
431 * impractically large value, even though POSIX explicitly
434 * The real fix would be to replace all static buffers of size
435 * PATH_MAX by dynamically allocated buffers. But that is
436 * somewhat intrusive because it touches several files and
437 * because it requires changing struct mlink in mandocdb.c.
438 * So i'm postponing that for now.
447 printf("PATH_MAX is defined to be %ld\n", (long)PATH_MAX);
450 #endif /* TEST_PATH_MAX */
457 return !!pledge("stdio", NULL);
459 #endif /* TEST_PLEDGE */
460 #if TEST_PROGRAM_INVOCATION_SHORT_NAME
461 #define _GNU_SOURCE /* See feature_test_macros(7) */
468 return !program_invocation_short_name;
470 #endif /* TEST_PROGRAM_INVOCATION_SHORT_NAME */
472 #include <sys/prctl.h>
477 prctl(PR_SET_NAME, "foo");
480 #endif /* TEST_PR_SET_NAME */
481 #if TEST_READPASSPHRASE
483 #include <readpassphrase.h>
488 return !!readpassphrase("prompt: ", NULL, 0, 0);
490 #endif /* TEST_READPASSPHRASE */
491 #if TEST_REALLOCARRAY
493 # define _OPENBSD_SOURCE
500 return !reallocarray(NULL, 2, 2);
502 #endif /* TEST_REALLOCARRAY */
503 #if TEST_RECALLOCARRAY
509 return !recallocarray(NULL, 0, 2, 2);
511 #endif /* TEST_RECALLOCARRAY */
512 #if TEST_SANDBOX_INIT
521 rc = sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, &ep);
523 sandbox_free_error(ep);
526 #endif /* TEST_SANDBOX_INIT */
527 #if TEST_SETPROCTITLE
533 setproctitle("#%d test program", 7);
544 hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1);
549 #endif /* TEST_SIO_FLUSH */
556 char *cinput = (char *)"1.5K", buf[FMT_SCALED_STRSIZE];
557 long long ninput = 10483892, result;
558 return scan_scaled(cinput, &result) == 0;
560 #endif /* TEST_SCAN_SCALED */
561 #if TEST_SECCOMP_FILTER
562 #include <sys/prctl.h>
563 #include <linux/seccomp.h>
570 prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, 0);
571 return(EFAULT == errno ? 0 : 1);
573 #endif /* TEST_SECCOMP_FILTER */
575 #define _GNU_SOURCE /* linux */
576 #include <sys/types.h>
582 return setresgid(-1, -1, -1) == -1;
584 #endif /* TEST_SETRESGID */
586 #define _GNU_SOURCE /* linux */
587 #include <sys/types.h>
593 return setresuid(-1, -1, -1) == -1;
595 #endif /* TEST_SETRESUID */
597 #include <sys/types.h>
603 char result[SHA256_DIGEST_STRING_LENGTH];
606 SHA256Update(&ctx, (const unsigned char *)"abcd", 4);
607 SHA256End(&ctx, result);
611 #endif /* TEST_SHA2 */
612 #if TEST_SOCK_NONBLOCK
614 * Linux doesn't (always?) have this.
617 #include <sys/socket.h>
623 socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fd);
626 #endif /* TEST_SOCK_NONBLOCK */
631 return 0; /* not meant to do anything */
633 #endif /* TEST_STATIC */
641 return ! (strlcat(buf, "b", sizeof(buf)) == 2 &&
642 buf[0] == 'a' && buf[1] == 'b' && buf[2] == '\0');
644 #endif /* TEST_STRLCAT */
652 return ! (strlcpy(buf, "a", sizeof(buf)) == 1 &&
653 buf[0] == 'a' && buf[1] == '\0');
655 #endif /* TEST_STRLCPY */
662 const char *foo = "bar";
665 baz = strndup(foo, 1);
666 return(0 != strcmp(baz, "b"));
668 #endif /* TEST_STRNDUP */
675 const char *foo = "bar";
678 sz = strnlen(foo, 1);
681 #endif /* TEST_STRNLEN */
684 * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
686 * Permission to use, copy, modify, and distribute this software for any
687 * purpose with or without fee is hereby granted, provided that the above
688 * copyright notice and this permission notice appear in all copies.
690 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
691 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
692 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
693 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
694 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
695 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
696 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
699 # define _OPENBSD_SOURCE
708 if (strtonum("1", 0, 2, &errstr) != 1)
712 if (strtonum("1x", 0, 2, &errstr) != 0)
716 if (strtonum("2", 0, 1, &errstr) != 0)
720 if (strtonum("0", 1, 2, &errstr) != 0)
726 #endif /* TEST_STRTONUM */
727 #if TEST_SYS_BYTEORDER_H
728 #include <sys/byteorder.h>
735 #endif /* TEST_SYS_BYTEORDER_H */
736 #if TEST_SYS_ENDIAN_H
737 #include <sys/endian.h>
744 #endif /* TEST_SYS_ENDIAN_H */
746 #include <sys/types.h>
747 #include <sys/mkdev.h>
754 #endif /* TEST_SYS_MKDEV_H */
756 #include <sys/file.h>
761 flock(0, LOCK_SH|LOCK_NB);
764 #endif /* TEST_SYS_FILE */
766 #include <sys/queue.h>
771 TAILQ_ENTRY(foo) entries;
774 TAILQ_HEAD(fooq, foo);
779 struct fooq foo_q, bar_q;
787 * Use TAILQ_FOREACH_SAFE because some systems (e.g., Linux)
788 * have TAILQ_FOREACH but not the safe variant.
791 TAILQ_FOREACH_SAFE(p, &foo_q, entries, tmp)
794 /* Test for newer macros as well. */
796 TAILQ_CONCAT(&foo_q, &bar_q, entries);
799 #endif /* TEST_SYS_QUEUE */
800 #if TEST_SYS_SYSMACROS_H
801 #include <sys/sysmacros.h>
808 #endif /* TEST_SYS_SYSMACROS_H */
810 #include <sys/tree.h>
814 RB_ENTRY(node) entry;
819 intcmp(struct node *e1, struct node *e2)
821 return (e1->i < e2->i ? -1 : e1->i > e2->i);
824 RB_HEAD(inttree, node) head = RB_INITIALIZER(&head);
825 RB_PROTOTYPE(inttree, node, entry, intcmp)
826 RB_GENERATE(inttree, node, entry, intcmp)
829 20, 16, 17, 13, 3, 6, 1, 8, 2, 4
838 for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) {
839 if ((n = malloc(sizeof(struct node))) == NULL)
842 RB_INSERT(inttree, &head, n);
848 #endif /* TEST_SYS_TREE */
850 #include <sys/time.h>
855 struct timespec a = {0, 0}, b = {0, 0}, c;
857 timespecsub(&a, &b, &c);
860 #endif /* TEST_TIMESPECSUB */
867 return -1 != unveil(NULL, NULL);
869 #endif /* TEST_UNVEIL */
871 #include <sys/wait.h>
878 return waitpid(WAIT_ANY, &st, WNOHANG) != -1;
880 #endif /* TEST_WAIT_ANY */
882 #include <FLAC/stream_decoder.h>
887 FLAC__StreamDecoder *decoder = NULL;
888 FLAC__StreamDecoderInitStatus init_status;
890 decoder = FLAC__stream_decoder_new();
891 FLAC__stream_decoder_delete(decoder);
894 #endif /* TEST_LIB_FLAC */
903 mh = mpg123_new(NULL, NULL);
907 #endif/* TEST_LIB_MPG123 */
915 pthread_mutex_t mutex;
917 return (pthread_mutex_init(&mutex, NULL));
919 #endif/* TEST_LIB_PTHREAD */
920 #if TEST_LIB_OPUSFILE
922 #include <opusfile.h>
929 OpusFileCallbacks cb = {NULL, NULL, NULL, NULL};
932 f = op_fdopen(&cb, 0, "r");
933 of = op_open_callbacks(f, &cb, NULL, 0, &r);
937 #endif /* TEST_LIB_OPUSFILE */
938 #if TEST_LIB_VORBISFILE
940 #include <vorbis/vorbisfile.h>
947 ov_open_callbacks(stdin, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE);
951 #endif /* TEST_LIB_VORBISFILE */
963 #endif /* TEST_LIB_AO */
965 #include <alsa/asoundlib.h>
973 err = snd_pcm_open(&pcm, "default", SND_PCM_STREAM_PLAYBACK,
977 #endif /* TEST_LIB_ASOUND */
985 hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1);
989 #endif /* TEST_LIB_SNDIO */
991 #include <sys/types.h>
998 char result[SHA1_DIGEST_STRING_LENGTH];
1001 SHA1Update(&ctx, (const unsigned char *)"abcd", 4);
1002 SHA1End(&ctx, result);
1006 #endif /* TEST_LIB_MD */