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 */
219 const char * progname;
221 progname = getexecname();
222 return progname == NULL;
224 #endif /* TEST_GETEXECNAME */
231 const char * progname;
233 progname = getprogname();
234 return progname == NULL;
236 #endif /* TEST_GETPROGNAME */
238 #include <sys/types.h>
239 #include <sys/queue.h>
253 #endif /* TEST_LIB_IMSG */
256 * Linux doesn't (always?) have this.
265 printf("INFTIM is defined to be %ld\n", (long)INFTIM);
268 #endif /* TEST_INFTIM */
270 #include <linux/landlock.h>
272 #include <sys/prctl.h>
273 #include <sys/syscall.h>
277 #ifndef landlock_create_ruleset
278 static inline int landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,
279 const size_t size, const __u32 flags)
281 return syscall(__NR_landlock_create_ruleset, attr, size, flags);
285 #ifndef landlock_restrict_self
286 static inline int landlock_restrict_self(const int ruleset_fd,
289 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
296 uint64_t mask = LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE;
297 struct landlock_ruleset_attr rules = {
298 .handled_access_fs = mask
300 int fd = landlock_create_ruleset(&rules, sizeof(rules), 0);
302 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
304 return landlock_restrict_self(fd, 0) ? 1 : 0;
306 #endif /* TEST_LANDLOCK */
308 #include <sys/socket.h>
315 c = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
318 #endif /* TEST_LIB_SOCKET */
320 #include <sys/types.h>
326 char result[MD5_DIGEST_STRING_LENGTH];
329 MD5Update(&ctx, (const unsigned char *)"abcd", 4);
330 MD5End(&ctx, result);
334 #endif /* TEST_MD5 */
342 char *a = memmem("hello, world", strlen("hello, world"), "world", strlen("world"));
345 #endif /* TEST_MEMMEM */
347 #if defined(__linux__) || defined(__MINT__)
348 #define _GNU_SOURCE /* See test-*.c what needs this. */
355 const char *buf = "abcdef";
358 res = memrchr(buf, 'a', strlen(buf));
359 return(NULL == res ? 1 : 0);
361 #endif /* TEST_MEMRCHR */
368 memset_s(buf, 0, 'c', sizeof(buf));
371 #endif /* TEST_MEMSET_S */
373 #include <sys/stat.h>
377 mkfifoat(AT_FDCWD, "this/path/should/not/exist", 0600);
380 #endif /* TEST_MKFIFOAT */
382 #include <sys/stat.h>
386 mknodat(AT_FDCWD, "this/path/should/not/exist", S_IFIFO | 0600, 0);
389 #endif /* TEST_MKNODAT */
394 main(int argc, char **argv)
397 getopt(argc, argv, "");
400 #endif /* TEST_OPTRESET */
401 #if TEST_OSBYTEORDER_H
402 #include <libkern/OSByteOrder.h>
407 return !OSSwapHostToLittleInt32(23);
409 #endif /* TEST_OSBYTEORDER_H */
412 * POSIX allows PATH_MAX to not be defined, see
413 * http://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html;
414 * the GNU Hurd is an example of a system not having it.
416 * Arguably, it would be better to test sysconf(_SC_PATH_MAX),
417 * but since the individual *.c files include "config.h" before
418 * <limits.h>, overriding an excessive value of PATH_MAX from
419 * "config.h" is impossible anyway, so for now, the simplest
420 * fix is to provide a value only on systems not having any.
421 * So far, we encountered no system defining PATH_MAX to an
422 * impractically large value, even though POSIX explicitly
425 * The real fix would be to replace all static buffers of size
426 * PATH_MAX by dynamically allocated buffers. But that is
427 * somewhat intrusive because it touches several files and
428 * because it requires changing struct mlink in mandocdb.c.
429 * So i'm postponing that for now.
438 printf("PATH_MAX is defined to be %ld\n", (long)PATH_MAX);
441 #endif /* TEST_PATH_MAX */
448 return !!pledge("stdio", NULL);
450 #endif /* TEST_PLEDGE */
451 #if TEST_PROGRAM_INVOCATION_SHORT_NAME
452 #define _GNU_SOURCE /* See feature_test_macros(7) */
459 return !program_invocation_short_name;
461 #endif /* TEST_PROGRAM_INVOCATION_SHORT_NAME */
463 #include <sys/prctl.h>
468 prctl(PR_SET_NAME, "foo");
471 #endif /* TEST_PR_SET_NAME */
472 #if TEST_READPASSPHRASE
474 #include <readpassphrase.h>
479 return !!readpassphrase("prompt: ", NULL, 0, 0);
481 #endif /* TEST_READPASSPHRASE */
482 #if TEST_REALLOCARRAY
484 # define _OPENBSD_SOURCE
491 return !reallocarray(NULL, 2, 2);
493 #endif /* TEST_REALLOCARRAY */
494 #if TEST_RECALLOCARRAY
500 return !recallocarray(NULL, 0, 2, 2);
502 #endif /* TEST_RECALLOCARRAY */
503 #if TEST_SANDBOX_INIT
512 rc = sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, &ep);
514 sandbox_free_error(ep);
517 #endif /* TEST_SANDBOX_INIT */
518 #if TEST_SETPROCTITLE
524 setproctitle("#%d test program", 7);
535 hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1);
540 #endif /* TEST_SIO_FLUSH */
547 char *cinput = (char *)"1.5K", buf[FMT_SCALED_STRSIZE];
548 long long ninput = 10483892, result;
549 return scan_scaled(cinput, &result) == 0;
551 #endif /* TEST_SCAN_SCALED */
552 #if TEST_SECCOMP_FILTER
553 #include <sys/prctl.h>
554 #include <linux/seccomp.h>
561 prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, 0);
562 return(EFAULT == errno ? 0 : 1);
564 #endif /* TEST_SECCOMP_FILTER */
566 #define _GNU_SOURCE /* linux */
567 #include <sys/types.h>
573 return setresgid(-1, -1, -1) == -1;
575 #endif /* TEST_SETRESGID */
577 #define _GNU_SOURCE /* linux */
578 #include <sys/types.h>
584 return setresuid(-1, -1, -1) == -1;
586 #endif /* TEST_SETRESUID */
588 #include <sys/types.h>
594 char result[SHA256_DIGEST_STRING_LENGTH];
597 SHA256Update(&ctx, (const unsigned char *)"abcd", 4);
598 SHA256End(&ctx, result);
602 #endif /* TEST_SHA2 */
603 #if TEST_SOCK_NONBLOCK
605 * Linux doesn't (always?) have this.
608 #include <sys/socket.h>
614 socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fd);
617 #endif /* TEST_SOCK_NONBLOCK */
622 return 0; /* not meant to do anything */
624 #endif /* TEST_STATIC */
632 return ! (strlcat(buf, "b", sizeof(buf)) == 2 &&
633 buf[0] == 'a' && buf[1] == 'b' && buf[2] == '\0');
635 #endif /* TEST_STRLCAT */
643 return ! (strlcpy(buf, "a", sizeof(buf)) == 1 &&
644 buf[0] == 'a' && buf[1] == '\0');
646 #endif /* TEST_STRLCPY */
653 const char *foo = "bar";
656 baz = strndup(foo, 1);
657 return(0 != strcmp(baz, "b"));
659 #endif /* TEST_STRNDUP */
666 const char *foo = "bar";
669 sz = strnlen(foo, 1);
672 #endif /* TEST_STRNLEN */
675 * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
677 * Permission to use, copy, modify, and distribute this software for any
678 * purpose with or without fee is hereby granted, provided that the above
679 * copyright notice and this permission notice appear in all copies.
681 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
682 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
683 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
684 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
685 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
686 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
687 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
690 # define _OPENBSD_SOURCE
699 if (strtonum("1", 0, 2, &errstr) != 1)
703 if (strtonum("1x", 0, 2, &errstr) != 0)
707 if (strtonum("2", 0, 1, &errstr) != 0)
711 if (strtonum("0", 1, 2, &errstr) != 0)
717 #endif /* TEST_STRTONUM */
718 #if TEST_SYS_BYTEORDER_H
719 #include <sys/byteorder.h>
726 #endif /* TEST_SYS_BYTEORDER_H */
727 #if TEST_SYS_ENDIAN_H
728 #include <sys/endian.h>
735 #endif /* TEST_SYS_ENDIAN_H */
737 #include <sys/types.h>
738 #include <sys/mkdev.h>
745 #endif /* TEST_SYS_MKDEV_H */
747 #include <sys/file.h>
752 flock(0, LOCK_SH|LOCK_NB);
755 #endif /* TEST_SYS_FILE */
757 #include <sys/queue.h>
762 TAILQ_ENTRY(foo) entries;
765 TAILQ_HEAD(fooq, foo);
770 struct fooq foo_q, bar_q;
778 * Use TAILQ_FOREACH_SAFE because some systems (e.g., Linux)
779 * have TAILQ_FOREACH but not the safe variant.
782 TAILQ_FOREACH_SAFE(p, &foo_q, entries, tmp)
785 /* Test for newer macros as well. */
787 TAILQ_CONCAT(&foo_q, &bar_q, entries);
790 #endif /* TEST_SYS_QUEUE */
791 #if TEST_SYS_SYSMACROS_H
792 #include <sys/sysmacros.h>
799 #endif /* TEST_SYS_SYSMACROS_H */
801 #include <sys/tree.h>
805 RB_ENTRY(node) entry;
810 intcmp(struct node *e1, struct node *e2)
812 return (e1->i < e2->i ? -1 : e1->i > e2->i);
815 RB_HEAD(inttree, node) head = RB_INITIALIZER(&head);
816 RB_PROTOTYPE(inttree, node, entry, intcmp)
817 RB_GENERATE(inttree, node, entry, intcmp)
820 20, 16, 17, 13, 3, 6, 1, 8, 2, 4
829 for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) {
830 if ((n = malloc(sizeof(struct node))) == NULL)
833 RB_INSERT(inttree, &head, n);
839 #endif /* TEST_SYS_TREE */
841 #include <sys/time.h>
846 struct timespec a = {0, 0}, b = {0, 0}, c;
848 timespecsub(&a, &b, &c);
851 #endif /* TEST_TIMESPECSUB */
858 return -1 != unveil(NULL, NULL);
860 #endif /* TEST_UNVEIL */
862 #include <sys/wait.h>
869 return waitpid(WAIT_ANY, &st, WNOHANG) != -1;
871 #endif /* TEST_WAIT_ANY */
873 #include <FLAC/stream_decoder.h>
878 FLAC__StreamDecoder *decoder = NULL;
879 FLAC__StreamDecoderInitStatus init_status;
881 decoder = FLAC__stream_decoder_new();
882 FLAC__stream_decoder_delete(decoder);
885 #endif /* TEST_LIB_FLAC */
894 mh = mpg123_new(NULL, NULL);
898 #endif/* TEST_LIB_MPG123 */
899 #if TEST_LIB_OPUSFILE
901 #include <opusfile.h>
908 OpusFileCallbacks cb = {NULL, NULL, NULL, NULL};
911 f = op_fdopen(&cb, 0, "r");
912 of = op_open_callbacks(f, &cb, NULL, 0, &r);
916 #endif /* TEST_LIB_OPUSFILE */
917 #if TEST_LIB_VORBISFILE
919 #include <vorbis/vorbisfile.h>
926 ov_open_callbacks(stdin, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE);
930 #endif /* TEST_LIB_VORBISFILE */
932 #include <alsa/asoundlib.h>
940 err = snd_pcm_open(&pcm, "default", SND_PCM_STREAM_PLAYBACK,
944 #endif /* TEST_LIB_ASOUND */
952 hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1);
956 #endif /* TEST_LIB_SNDIO */
958 #include <sys/types.h>
965 char result[SHA1_DIGEST_STRING_LENGTH];
968 SHA1Update(&ctx, (const unsigned char *)"abcd", 4);
969 SHA1End(&ctx, result);
973 #endif /* TEST_LIB_MD */