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);
530 #endif /* TEST_SIO_FLUSH */
537 char *cinput = (char *)"1.5K", buf[FMT_SCALED_STRSIZE];
538 long long ninput = 10483892, result;
539 return scan_scaled(cinput, &result) == 0;
541 #endif /* TEST_SCAN_SCALED */
542 #if TEST_SECCOMP_FILTER
543 #include <sys/prctl.h>
544 #include <linux/seccomp.h>
551 prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, 0);
552 return(EFAULT == errno ? 0 : 1);
554 #endif /* TEST_SECCOMP_FILTER */
556 #define _GNU_SOURCE /* linux */
557 #include <sys/types.h>
563 return setresgid(-1, -1, -1) == -1;
565 #endif /* TEST_SETRESGID */
567 #define _GNU_SOURCE /* linux */
568 #include <sys/types.h>
574 return setresuid(-1, -1, -1) == -1;
576 #endif /* TEST_SETRESUID */
578 #include <sys/types.h>
584 char result[SHA256_DIGEST_STRING_LENGTH];
587 SHA256Update(&ctx, (const unsigned char *)"abcd", 4);
588 SHA256End(&ctx, result);
592 #endif /* TEST_SHA2 */
593 #if TEST_SOCK_NONBLOCK
595 * Linux doesn't (always?) have this.
598 #include <sys/socket.h>
604 socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fd);
607 #endif /* TEST_SOCK_NONBLOCK */
612 return 0; /* not meant to do anything */
614 #endif /* TEST_STATIC */
622 return ! (strlcat(buf, "b", sizeof(buf)) == 2 &&
623 buf[0] == 'a' && buf[1] == 'b' && buf[2] == '\0');
625 #endif /* TEST_STRLCAT */
633 return ! (strlcpy(buf, "a", sizeof(buf)) == 1 &&
634 buf[0] == 'a' && buf[1] == '\0');
636 #endif /* TEST_STRLCPY */
643 const char *foo = "bar";
646 baz = strndup(foo, 1);
647 return(0 != strcmp(baz, "b"));
649 #endif /* TEST_STRNDUP */
656 const char *foo = "bar";
659 sz = strnlen(foo, 1);
662 #endif /* TEST_STRNLEN */
665 * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
667 * Permission to use, copy, modify, and distribute this software for any
668 * purpose with or without fee is hereby granted, provided that the above
669 * copyright notice and this permission notice appear in all copies.
671 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
672 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
673 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
674 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
675 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
676 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
677 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
680 # define _OPENBSD_SOURCE
689 if (strtonum("1", 0, 2, &errstr) != 1)
693 if (strtonum("1x", 0, 2, &errstr) != 0)
697 if (strtonum("2", 0, 1, &errstr) != 0)
701 if (strtonum("0", 1, 2, &errstr) != 0)
707 #endif /* TEST_STRTONUM */
708 #if TEST_SYS_BYTEORDER_H
709 #include <sys/byteorder.h>
716 #endif /* TEST_SYS_BYTEORDER_H */
717 #if TEST_SYS_ENDIAN_H
718 #include <sys/endian.h>
725 #endif /* TEST_SYS_ENDIAN_H */
727 #include <sys/types.h>
728 #include <sys/mkdev.h>
735 #endif /* TEST_SYS_MKDEV_H */
737 #include <sys/file.h>
742 flock(0, LOCK_SH|LOCK_NB);
745 #endif /* TEST_SYS_FILE */
747 #include <sys/queue.h>
752 TAILQ_ENTRY(foo) entries;
755 TAILQ_HEAD(fooq, foo);
760 struct fooq foo_q, bar_q;
768 * Use TAILQ_FOREACH_SAFE because some systems (e.g., Linux)
769 * have TAILQ_FOREACH but not the safe variant.
772 TAILQ_FOREACH_SAFE(p, &foo_q, entries, tmp)
775 /* Test for newer macros as well. */
777 TAILQ_CONCAT(&foo_q, &bar_q, entries);
780 #endif /* TEST_SYS_QUEUE */
781 #if TEST_SYS_SYSMACROS_H
782 #include <sys/sysmacros.h>
789 #endif /* TEST_SYS_SYSMACROS_H */
791 #include <sys/tree.h>
795 RB_ENTRY(node) entry;
800 intcmp(struct node *e1, struct node *e2)
802 return (e1->i < e2->i ? -1 : e1->i > e2->i);
805 RB_HEAD(inttree, node) head = RB_INITIALIZER(&head);
806 RB_PROTOTYPE(inttree, node, entry, intcmp)
807 RB_GENERATE(inttree, node, entry, intcmp)
810 20, 16, 17, 13, 3, 6, 1, 8, 2, 4
819 for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) {
820 if ((n = malloc(sizeof(struct node))) == NULL)
823 RB_INSERT(inttree, &head, n);
829 #endif /* TEST_SYS_TREE */
836 return -1 != unveil(NULL, NULL);
838 #endif /* TEST_UNVEIL */
840 #include <sys/wait.h>
847 return waitpid(WAIT_ANY, &st, WNOHANG) != -1;
849 #endif /* TEST_WAIT_ANY */
858 event_set(&ev, 0, EV_READ, NULL, NULL);
859 event_add(&ev, NULL);
863 #endif /* TEST_LIBEVENT */
865 #include <event2/event.h>
866 #include <event2/event_compat.h>
867 #include <event2/event_struct.h>
868 #include <event2/buffer.h>
869 #include <event2/buffer_compat.h>
870 #include <event2/bufferevent.h>
871 #include <event2/bufferevent_struct.h>
872 #include <event2/bufferevent_compat.h>
879 event_set(&ev, 0, EV_READ, NULL, NULL);
880 event_add(&ev, NULL);
884 #endif /* TEST_LIBEVENT2 */
886 #include <FLAC/stream_decoder.h>
891 FLAC__StreamDecoder *decoder = NULL;
892 FLAC__StreamDecoderInitStatus init_status;
894 decoder = FLAC__stream_decoder_new();
895 FLAC__stream_decoder_delete(decoder);
898 #endif /* TEST_LIB_FLAC */
907 mh = mpg123_new(NULL, NULL);
911 #endif/* TEST_LIB_MPG123 */
912 #if TEST_LIB_OPUSFILE
914 #include <opusfile.h>
921 OpusFileCallbacks cb = {NULL, NULL, NULL, NULL};
924 f = op_fdopen(&cb, 0, "r");
925 of = op_open_callbacks(f, &cb, NULL, 0, &r);
929 #endif /* TEST_LIB_OPUSFILE */
930 #if TEST_LIB_VORBISFILE
932 #include <vorbis/vorbisfile.h>
939 ov_open_callbacks(stdin, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE);
943 #endif /* TEST_LIB_VORBISFILE */
951 hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1);
954 #endif /* TEST_LIB_SNDIO */