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>
256 struct imsgbuf imsgbuf;
259 imsg_init(&imsgbuf, 3);
260 imsg_clear(&imsgbuf);
264 #endif /* TEST_LIB_IMSG */
267 * Linux doesn't (always?) have this.
276 printf("INFTIM is defined to be %ld\n", (long)INFTIM);
279 #endif /* TEST_INFTIM */
281 #include <linux/landlock.h>
283 #include <sys/prctl.h>
284 #include <sys/syscall.h>
288 #ifndef landlock_create_ruleset
289 static inline int landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,
290 const size_t size, const __u32 flags)
292 return syscall(__NR_landlock_create_ruleset, attr, size, flags);
296 #ifndef landlock_restrict_self
297 static inline int landlock_restrict_self(const int ruleset_fd,
300 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
307 uint64_t mask = LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE;
308 struct landlock_ruleset_attr rules = {
309 .handled_access_fs = mask
311 int fd = landlock_create_ruleset(&rules, sizeof(rules), 0);
313 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
315 return landlock_restrict_self(fd, 0) ? 1 : 0;
317 #endif /* TEST_LANDLOCK */
319 #include <sys/socket.h>
326 c = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
329 #endif /* TEST_LIB_SOCKET */
331 #include <sys/types.h>
337 char result[MD5_DIGEST_STRING_LENGTH];
340 MD5Update(&ctx, (const unsigned char *)"abcd", 4);
341 MD5End(&ctx, result);
345 #endif /* TEST_MD5 */
353 char *a = memmem("hello, world", strlen("hello, world"), "world", strlen("world"));
356 #endif /* TEST_MEMMEM */
358 #if defined(__linux__) || defined(__MINT__)
359 #define _GNU_SOURCE /* See test-*.c what needs this. */
366 const char *buf = "abcdef";
369 res = memrchr(buf, 'a', strlen(buf));
370 return(NULL == res ? 1 : 0);
372 #endif /* TEST_MEMRCHR */
379 memset_s(buf, 0, 'c', sizeof(buf));
382 #endif /* TEST_MEMSET_S */
384 #include <sys/stat.h>
388 mkfifoat(AT_FDCWD, "this/path/should/not/exist", 0600);
391 #endif /* TEST_MKFIFOAT */
393 #include <sys/stat.h>
397 mknodat(AT_FDCWD, "this/path/should/not/exist", S_IFIFO | 0600, 0);
400 #endif /* TEST_MKNODAT */
405 main(int argc, char **argv)
408 getopt(argc, argv, "");
411 #endif /* TEST_OPTRESET */
412 #if TEST_OSBYTEORDER_H
413 #include <libkern/OSByteOrder.h>
418 return !OSSwapHostToLittleInt32(23);
420 #endif /* TEST_OSBYTEORDER_H */
423 * POSIX allows PATH_MAX to not be defined, see
424 * http://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html;
425 * the GNU Hurd is an example of a system not having it.
427 * Arguably, it would be better to test sysconf(_SC_PATH_MAX),
428 * but since the individual *.c files include "config.h" before
429 * <limits.h>, overriding an excessive value of PATH_MAX from
430 * "config.h" is impossible anyway, so for now, the simplest
431 * fix is to provide a value only on systems not having any.
432 * So far, we encountered no system defining PATH_MAX to an
433 * impractically large value, even though POSIX explicitly
436 * The real fix would be to replace all static buffers of size
437 * PATH_MAX by dynamically allocated buffers. But that is
438 * somewhat intrusive because it touches several files and
439 * because it requires changing struct mlink in mandocdb.c.
440 * So i'm postponing that for now.
449 printf("PATH_MAX is defined to be %ld\n", (long)PATH_MAX);
452 #endif /* TEST_PATH_MAX */
459 return !!pledge("stdio", NULL);
461 #endif /* TEST_PLEDGE */
462 #if TEST_PROGRAM_INVOCATION_SHORT_NAME
463 #define _GNU_SOURCE /* See feature_test_macros(7) */
470 return !program_invocation_short_name;
472 #endif /* TEST_PROGRAM_INVOCATION_SHORT_NAME */
474 #include <sys/prctl.h>
479 prctl(PR_SET_NAME, "foo");
482 #endif /* TEST_PR_SET_NAME */
483 #if TEST_READPASSPHRASE
485 #include <readpassphrase.h>
490 return !!readpassphrase("prompt: ", NULL, 0, 0);
492 #endif /* TEST_READPASSPHRASE */
493 #if TEST_REALLOCARRAY
495 # define _OPENBSD_SOURCE
502 return !reallocarray(NULL, 2, 2);
504 #endif /* TEST_REALLOCARRAY */
505 #if TEST_RECALLOCARRAY
511 return !recallocarray(NULL, 0, 2, 2);
513 #endif /* TEST_RECALLOCARRAY */
514 #if TEST_SANDBOX_INIT
523 rc = sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, &ep);
525 sandbox_free_error(ep);
528 #endif /* TEST_SANDBOX_INIT */
529 #if TEST_SETPROCTITLE
535 setproctitle("#%d test program", 7);
546 hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1);
551 #endif /* TEST_SIO_FLUSH */
558 char *cinput = (char *)"1.5K", buf[FMT_SCALED_STRSIZE];
559 long long ninput = 10483892, result;
560 return scan_scaled(cinput, &result) == 0;
562 #endif /* TEST_SCAN_SCALED */
563 #if TEST_SECCOMP_FILTER
564 #include <sys/prctl.h>
565 #include <linux/seccomp.h>
572 prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, 0);
573 return(EFAULT == errno ? 0 : 1);
575 #endif /* TEST_SECCOMP_FILTER */
577 #define _GNU_SOURCE /* linux */
578 #include <sys/types.h>
584 return setresgid(-1, -1, -1) == -1;
586 #endif /* TEST_SETRESGID */
588 #define _GNU_SOURCE /* linux */
589 #include <sys/types.h>
595 return setresuid(-1, -1, -1) == -1;
597 #endif /* TEST_SETRESUID */
599 #include <sys/types.h>
605 char result[SHA256_DIGEST_STRING_LENGTH];
608 SHA256Update(&ctx, (const unsigned char *)"abcd", 4);
609 SHA256End(&ctx, result);
613 #endif /* TEST_SHA2 */
614 #if TEST_SOCK_NONBLOCK
616 * Linux doesn't (always?) have this.
619 #include <sys/socket.h>
625 socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fd);
628 #endif /* TEST_SOCK_NONBLOCK */
633 return 0; /* not meant to do anything */
635 #endif /* TEST_STATIC */
643 return ! (strlcat(buf, "b", sizeof(buf)) == 2 &&
644 buf[0] == 'a' && buf[1] == 'b' && buf[2] == '\0');
646 #endif /* TEST_STRLCAT */
654 return ! (strlcpy(buf, "a", sizeof(buf)) == 1 &&
655 buf[0] == 'a' && buf[1] == '\0');
657 #endif /* TEST_STRLCPY */
664 const char *foo = "bar";
667 baz = strndup(foo, 1);
668 return(0 != strcmp(baz, "b"));
670 #endif /* TEST_STRNDUP */
677 const char *foo = "bar";
680 sz = strnlen(foo, 1);
683 #endif /* TEST_STRNLEN */
686 * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
688 * Permission to use, copy, modify, and distribute this software for any
689 * purpose with or without fee is hereby granted, provided that the above
690 * copyright notice and this permission notice appear in all copies.
692 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
693 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
694 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
695 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
696 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
697 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
698 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
701 # define _OPENBSD_SOURCE
710 if (strtonum("1", 0, 2, &errstr) != 1)
714 if (strtonum("1x", 0, 2, &errstr) != 0)
718 if (strtonum("2", 0, 1, &errstr) != 0)
722 if (strtonum("0", 1, 2, &errstr) != 0)
728 #endif /* TEST_STRTONUM */
729 #if TEST_SYS_BYTEORDER_H
730 #include <sys/byteorder.h>
737 #endif /* TEST_SYS_BYTEORDER_H */
738 #if TEST_SYS_ENDIAN_H
739 #include <sys/endian.h>
746 #endif /* TEST_SYS_ENDIAN_H */
748 #include <sys/types.h>
749 #include <sys/mkdev.h>
756 #endif /* TEST_SYS_MKDEV_H */
758 #include <sys/file.h>
763 flock(0, LOCK_SH|LOCK_NB);
766 #endif /* TEST_SYS_FILE */
768 #include <sys/queue.h>
773 TAILQ_ENTRY(foo) entries;
776 TAILQ_HEAD(fooq, foo);
781 struct fooq foo_q, bar_q;
789 * Use TAILQ_FOREACH_SAFE because some systems (e.g., Linux)
790 * have TAILQ_FOREACH but not the safe variant.
793 TAILQ_FOREACH_SAFE(p, &foo_q, entries, tmp)
796 /* Test for newer macros as well. */
798 TAILQ_CONCAT(&foo_q, &bar_q, entries);
801 #endif /* TEST_SYS_QUEUE */
802 #if TEST_SYS_SYSMACROS_H
803 #include <sys/sysmacros.h>
810 #endif /* TEST_SYS_SYSMACROS_H */
812 #include <sys/tree.h>
816 RB_ENTRY(node) entry;
821 intcmp(struct node *e1, struct node *e2)
823 return (e1->i < e2->i ? -1 : e1->i > e2->i);
826 RB_HEAD(inttree, node) head = RB_INITIALIZER(&head);
827 RB_PROTOTYPE(inttree, node, entry, intcmp)
828 RB_GENERATE(inttree, node, entry, intcmp)
831 20, 16, 17, 13, 3, 6, 1, 8, 2, 4
840 for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) {
841 if ((n = malloc(sizeof(struct node))) == NULL)
844 RB_INSERT(inttree, &head, n);
850 #endif /* TEST_SYS_TREE */
852 #include <sys/time.h>
857 struct timespec a = {0, 0}, b = {0, 0}, c;
859 timespecsub(&a, &b, &c);
862 #endif /* TEST_TIMESPECSUB */
869 return -1 != unveil(NULL, NULL);
871 #endif /* TEST_UNVEIL */
873 #include <sys/wait.h>
880 return waitpid(WAIT_ANY, &st, WNOHANG) != -1;
882 #endif /* TEST_WAIT_ANY */
884 #include <FLAC/stream_decoder.h>
889 FLAC__StreamDecoder *decoder = NULL;
890 FLAC__StreamDecoderInitStatus init_status;
892 decoder = FLAC__stream_decoder_new();
893 FLAC__stream_decoder_delete(decoder);
896 #endif /* TEST_LIB_FLAC */
905 mh = mpg123_new(NULL, NULL);
909 #endif/* TEST_LIB_MPG123 */
917 pthread_mutex_t mutex;
919 return (pthread_mutex_init(&mutex, NULL));
921 #endif/* TEST_LIB_PTHREAD */
922 #if TEST_LIB_OPUSFILE
924 #include <opusfile.h>
931 OpusFileCallbacks cb = {NULL, NULL, NULL, NULL};
934 f = op_fdopen(&cb, 0, "r");
935 of = op_open_callbacks(f, &cb, NULL, 0, &r);
939 #endif /* TEST_LIB_OPUSFILE */
940 #if TEST_LIB_VORBISFILE
942 #include <vorbis/vorbisfile.h>
949 ov_open_callbacks(stdin, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE);
953 #endif /* TEST_LIB_VORBISFILE */
965 #endif /* TEST_LIB_AO */
967 #include <alsa/asoundlib.h>
975 err = snd_pcm_open(&pcm, "default", SND_PCM_STREAM_PLAYBACK,
979 #endif /* TEST_LIB_ASOUND */
987 hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1);
991 #endif /* TEST_LIB_SNDIO */
993 #include <sys/types.h>
1000 char result[SHA1_DIGEST_STRING_LENGTH];
1003 SHA1Update(&ctx, (const unsigned char *)"abcd", 4);
1004 SHA1End(&ctx, result);
1008 #endif /* TEST_LIB_MD */