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 */
210 const char * progname;
212 progname = getexecname();
213 return progname == NULL;
215 #endif /* TEST_GETEXECNAME */
222 const char * progname;
224 progname = getprogname();
225 return progname == NULL;
227 #endif /* TEST_GETPROGNAME */
229 #include <sys/types.h>
230 #include <sys/queue.h>
244 #endif /* TEST_IMSG */
247 * Linux doesn't (always?) have this.
256 printf("INFTIM is defined to be %ld\n", (long)INFTIM);
259 #endif /* TEST_INFTIM */
261 #include <linux/landlock.h>
263 #include <sys/prctl.h>
264 #include <sys/syscall.h>
268 #ifndef landlock_create_ruleset
269 static inline int landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,
270 const size_t size, const __u32 flags)
272 return syscall(__NR_landlock_create_ruleset, attr, size, flags);
276 #ifndef landlock_restrict_self
277 static inline int landlock_restrict_self(const int ruleset_fd,
280 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
287 uint64_t mask = LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE;
288 struct landlock_ruleset_attr rules = {
289 .handled_access_fs = mask
291 int fd = landlock_create_ruleset(&rules, sizeof(rules), 0);
293 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
295 return landlock_restrict_self(fd, 0) ? 1 : 0;
297 #endif /* TEST_LANDLOCK */
299 #include <sys/socket.h>
306 c = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
309 #endif /* TEST_LIB_SOCKET */
311 #include <sys/types.h>
317 char result[MD5_DIGEST_STRING_LENGTH];
320 MD5Update(&ctx, (const unsigned char *)"abcd", 4);
321 MD5End(&ctx, result);
325 #endif /* TEST_MD5 */
333 char *a = memmem("hello, world", strlen("hello, world"), "world", strlen("world"));
336 #endif /* TEST_MEMMEM */
338 #if defined(__linux__) || defined(__MINT__)
339 #define _GNU_SOURCE /* See test-*.c what needs this. */
346 const char *buf = "abcdef";
349 res = memrchr(buf, 'a', strlen(buf));
350 return(NULL == res ? 1 : 0);
352 #endif /* TEST_MEMRCHR */
359 memset_s(buf, 0, 'c', sizeof(buf));
362 #endif /* TEST_MEMSET_S */
364 #include <sys/stat.h>
368 mkfifoat(AT_FDCWD, "this/path/should/not/exist", 0600);
371 #endif /* TEST_MKFIFOAT */
373 #include <sys/stat.h>
377 mknodat(AT_FDCWD, "this/path/should/not/exist", S_IFIFO | 0600, 0);
380 #endif /* TEST_MKNODAT */
385 main(int argc, char **argv)
388 getopt(argc, argv, "");
391 #endif /* TEST_OPTRESET */
392 #if TEST_OSBYTEORDER_H
393 #include <libkern/OSByteOrder.h>
398 return !OSSwapHostToLittleInt32(23);
400 #endif /* TEST_OSBYTEORDER_H */
403 * POSIX allows PATH_MAX to not be defined, see
404 * http://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html;
405 * the GNU Hurd is an example of a system not having it.
407 * Arguably, it would be better to test sysconf(_SC_PATH_MAX),
408 * but since the individual *.c files include "config.h" before
409 * <limits.h>, overriding an excessive value of PATH_MAX from
410 * "config.h" is impossible anyway, so for now, the simplest
411 * fix is to provide a value only on systems not having any.
412 * So far, we encountered no system defining PATH_MAX to an
413 * impractically large value, even though POSIX explicitly
416 * The real fix would be to replace all static buffers of size
417 * PATH_MAX by dynamically allocated buffers. But that is
418 * somewhat intrusive because it touches several files and
419 * because it requires changing struct mlink in mandocdb.c.
420 * So i'm postponing that for now.
429 printf("PATH_MAX is defined to be %ld\n", (long)PATH_MAX);
432 #endif /* TEST_PATH_MAX */
439 return !!pledge("stdio", NULL);
441 #endif /* TEST_PLEDGE */
442 #if TEST_PROGRAM_INVOCATION_SHORT_NAME
443 #define _GNU_SOURCE /* See feature_test_macros(7) */
450 return !program_invocation_short_name;
452 #endif /* TEST_PROGRAM_INVOCATION_SHORT_NAME */
454 #include <sys/prctl.h>
459 prctl(PR_SET_NAME, "foo");
462 #endif /* TEST_PR_SET_NAME */
463 #if TEST_READPASSPHRASE
465 #include <readpassphrase.h>
470 return !!readpassphrase("prompt: ", NULL, 0, 0);
472 #endif /* TEST_READPASSPHRASE */
473 #if TEST_REALLOCARRAY
475 # define _OPENBSD_SOURCE
482 return !reallocarray(NULL, 2, 2);
484 #endif /* TEST_REALLOCARRAY */
485 #if TEST_RECALLOCARRAY
491 return !recallocarray(NULL, 0, 2, 2);
493 #endif /* TEST_RECALLOCARRAY */
494 #if TEST_SANDBOX_INIT
503 rc = sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, &ep);
505 sandbox_free_error(ep);
508 #endif /* TEST_SANDBOX_INIT */
509 #if TEST_SETPROCTITLE
515 setproctitle("#%d test program", 7);
526 hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1);
531 #endif /* TEST_SIO_FLUSH */
538 char *cinput = (char *)"1.5K", buf[FMT_SCALED_STRSIZE];
539 long long ninput = 10483892, result;
540 return scan_scaled(cinput, &result) == 0;
542 #endif /* TEST_SCAN_SCALED */
543 #if TEST_SECCOMP_FILTER
544 #include <sys/prctl.h>
545 #include <linux/seccomp.h>
552 prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, 0);
553 return(EFAULT == errno ? 0 : 1);
555 #endif /* TEST_SECCOMP_FILTER */
557 #define _GNU_SOURCE /* linux */
558 #include <sys/types.h>
564 return setresgid(-1, -1, -1) == -1;
566 #endif /* TEST_SETRESGID */
568 #define _GNU_SOURCE /* linux */
569 #include <sys/types.h>
575 return setresuid(-1, -1, -1) == -1;
577 #endif /* TEST_SETRESUID */
579 #include <sys/types.h>
585 char result[SHA256_DIGEST_STRING_LENGTH];
588 SHA256Update(&ctx, (const unsigned char *)"abcd", 4);
589 SHA256End(&ctx, result);
593 #endif /* TEST_SHA2 */
594 #if TEST_SOCK_NONBLOCK
596 * Linux doesn't (always?) have this.
599 #include <sys/socket.h>
605 socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fd);
608 #endif /* TEST_SOCK_NONBLOCK */
613 return 0; /* not meant to do anything */
615 #endif /* TEST_STATIC */
623 return ! (strlcat(buf, "b", sizeof(buf)) == 2 &&
624 buf[0] == 'a' && buf[1] == 'b' && buf[2] == '\0');
626 #endif /* TEST_STRLCAT */
634 return ! (strlcpy(buf, "a", sizeof(buf)) == 1 &&
635 buf[0] == 'a' && buf[1] == '\0');
637 #endif /* TEST_STRLCPY */
644 const char *foo = "bar";
647 baz = strndup(foo, 1);
648 return(0 != strcmp(baz, "b"));
650 #endif /* TEST_STRNDUP */
657 const char *foo = "bar";
660 sz = strnlen(foo, 1);
663 #endif /* TEST_STRNLEN */
666 * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
668 * Permission to use, copy, modify, and distribute this software for any
669 * purpose with or without fee is hereby granted, provided that the above
670 * copyright notice and this permission notice appear in all copies.
672 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
673 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
674 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
675 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
676 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
677 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
678 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
681 # define _OPENBSD_SOURCE
690 if (strtonum("1", 0, 2, &errstr) != 1)
694 if (strtonum("1x", 0, 2, &errstr) != 0)
698 if (strtonum("2", 0, 1, &errstr) != 0)
702 if (strtonum("0", 1, 2, &errstr) != 0)
708 #endif /* TEST_STRTONUM */
709 #if TEST_SYS_BYTEORDER_H
710 #include <sys/byteorder.h>
717 #endif /* TEST_SYS_BYTEORDER_H */
718 #if TEST_SYS_ENDIAN_H
719 #include <sys/endian.h>
726 #endif /* TEST_SYS_ENDIAN_H */
728 #include <sys/types.h>
729 #include <sys/mkdev.h>
736 #endif /* TEST_SYS_MKDEV_H */
738 #include <sys/file.h>
743 flock(0, LOCK_SH|LOCK_NB);
746 #endif /* TEST_SYS_FILE */
748 #include <sys/queue.h>
753 TAILQ_ENTRY(foo) entries;
756 TAILQ_HEAD(fooq, foo);
761 struct fooq foo_q, bar_q;
769 * Use TAILQ_FOREACH_SAFE because some systems (e.g., Linux)
770 * have TAILQ_FOREACH but not the safe variant.
773 TAILQ_FOREACH_SAFE(p, &foo_q, entries, tmp)
776 /* Test for newer macros as well. */
778 TAILQ_CONCAT(&foo_q, &bar_q, entries);
781 #endif /* TEST_SYS_QUEUE */
782 #if TEST_SYS_SYSMACROS_H
783 #include <sys/sysmacros.h>
790 #endif /* TEST_SYS_SYSMACROS_H */
792 #include <sys/tree.h>
796 RB_ENTRY(node) entry;
801 intcmp(struct node *e1, struct node *e2)
803 return (e1->i < e2->i ? -1 : e1->i > e2->i);
806 RB_HEAD(inttree, node) head = RB_INITIALIZER(&head);
807 RB_PROTOTYPE(inttree, node, entry, intcmp)
808 RB_GENERATE(inttree, node, entry, intcmp)
811 20, 16, 17, 13, 3, 6, 1, 8, 2, 4
820 for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) {
821 if ((n = malloc(sizeof(struct node))) == NULL)
824 RB_INSERT(inttree, &head, n);
830 #endif /* TEST_SYS_TREE */
837 return -1 != unveil(NULL, NULL);
839 #endif /* TEST_UNVEIL */
841 #include <sys/wait.h>
848 return waitpid(WAIT_ANY, &st, WNOHANG) != -1;
850 #endif /* TEST_WAIT_ANY */
859 event_set(&ev, 0, EV_READ, NULL, NULL);
860 event_add(&ev, NULL);
864 #endif /* TEST_LIBEVENT */
866 #include <event2/event.h>
867 #include <event2/event_compat.h>
868 #include <event2/event_struct.h>
869 #include <event2/buffer.h>
870 #include <event2/buffer_compat.h>
871 #include <event2/bufferevent.h>
872 #include <event2/bufferevent_struct.h>
873 #include <event2/bufferevent_compat.h>
880 event_set(&ev, 0, EV_READ, NULL, NULL);
881 event_add(&ev, NULL);
885 #endif /* TEST_LIBEVENT2 */
887 #include <FLAC/stream_decoder.h>
892 FLAC__StreamDecoder *decoder = NULL;
893 FLAC__StreamDecoderInitStatus init_status;
895 decoder = FLAC__stream_decoder_new();
896 FLAC__stream_decoder_delete(decoder);
899 #endif /* TEST_LIB_FLAC */
908 mh = mpg123_new(NULL, NULL);
912 #endif/* TEST_LIB_MPG123 */
913 #if TEST_LIB_OPUSFILE
915 #include <opusfile.h>
922 OpusFileCallbacks cb = {NULL, NULL, NULL, NULL};
925 f = op_fdopen(&cb, 0, "r");
926 of = op_open_callbacks(f, &cb, NULL, 0, &r);
930 #endif /* TEST_LIB_OPUSFILE */
931 #if TEST_LIB_VORBISFILE
933 #include <vorbis/vorbisfile.h>
940 ov_open_callbacks(stdin, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE);
944 #endif /* TEST_LIB_VORBISFILE */
952 hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1);
956 #endif /* TEST_LIB_SNDIO */