7 f36fd90a 2022-07-09 op #endif /* TEST_NOOP */
8 f36fd90a 2022-07-09 op #if TEST___PROGNAME
12 f36fd90a 2022-07-09 op extern char *__progname;
14 f36fd90a 2022-07-09 op return !__progname;
16 f36fd90a 2022-07-09 op #endif /* TEST___PROGNAME */
17 f36fd90a 2022-07-09 op #if TEST_ARC4RANDOM
18 f36fd90a 2022-07-09 op #include <stdlib.h>
23 f36fd90a 2022-07-09 op return (arc4random() + 1) ? 0 : 1;
25 f36fd90a 2022-07-09 op #endif /* TEST_ARC4RANDOM */
26 f36fd90a 2022-07-09 op #if TEST_B64_NTOP
27 f36fd90a 2022-07-09 op #include <netinet/in.h>
28 f36fd90a 2022-07-09 op #include <resolv.h>
33 f36fd90a 2022-07-09 op const char *src = "hello world";
34 f36fd90a 2022-07-09 op char output[1024];
36 f36fd90a 2022-07-09 op return b64_ntop((const unsigned char *)src, 11, output, sizeof(output)) > 0 ? 0 : 1;
38 f36fd90a 2022-07-09 op #endif /* TEST_B64_NTOP */
39 f36fd90a 2022-07-09 op #if TEST_CAPSICUM
40 f36fd90a 2022-07-09 op #include <sys/capsicum.h>
48 f36fd90a 2022-07-09 op #endif /* TEST_CAPSICUM */
49 f36fd90a 2022-07-09 op #if TEST_CRYPT
50 f36fd90a 2022-07-09 op #if defined(__linux__)
51 f36fd90a 2022-07-09 op # define _GNU_SOURCE /* old glibc */
52 f36fd90a 2022-07-09 op # define _DEFAULT_SOURCE /* new glibc */
54 f36fd90a 2022-07-09 op #if defined(__sun)
55 f36fd90a 2022-07-09 op # ifndef _XOPEN_SOURCE /* SunOS already defines */
56 f36fd90a 2022-07-09 op # define _XOPEN_SOURCE /* XPGx */
58 f36fd90a 2022-07-09 op # define _XOPEN_SOURCE_EXTENDED 1 /* XPG4v2 */
59 f36fd90a 2022-07-09 op # ifndef __EXTENSIONS__ /* SunOS already defines */
60 f36fd90a 2022-07-09 op # define __EXTENSIONS__ /* reallocarray, etc. */
63 f36fd90a 2022-07-09 op #include <unistd.h>
65 f36fd90a 2022-07-09 op int main(void)
69 f36fd90a 2022-07-09 op v = crypt("this_is_a_key", "123455");
70 f36fd90a 2022-07-09 op return v == NULL;
72 f36fd90a 2022-07-09 op #endif /* TEST_CRYPT */
73 f36fd90a 2022-07-09 op #if TEST_CRYPT_NEWHASH
74 f36fd90a 2022-07-09 op #include <pwd.h> /* _PASSWORD_LEN */
75 f36fd90a 2022-07-09 op #include <unistd.h>
80 f36fd90a 2022-07-09 op const char *v = "password";
81 f36fd90a 2022-07-09 op char hash[_PASSWORD_LEN];
83 f36fd90a 2022-07-09 op if (crypt_newhash(v, "bcrypt,a", hash, sizeof(hash)) == -1)
85 f36fd90a 2022-07-09 op if (crypt_checkpass(v, hash) == -1)
90 f36fd90a 2022-07-09 op #endif /* TEST_CRYPT_NEWHASH */
91 f36fd90a 2022-07-09 op #if TEST_ENDIAN_H
92 f36fd90a 2022-07-09 op #ifdef __linux__
93 f36fd90a 2022-07-09 op # define _DEFAULT_SOURCE
95 f36fd90a 2022-07-09 op #include <endian.h>
100 f36fd90a 2022-07-09 op return !htole32(23);
102 f36fd90a 2022-07-09 op #endif /* TEST_ENDIAN_H */
105 f36fd90a 2022-07-09 op * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
107 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
108 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
109 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
111 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
112 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
113 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
114 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
115 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
116 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
117 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
120 f36fd90a 2022-07-09 op #include <err.h>
121 f36fd90a 2022-07-09 op #include <errno.h>
126 f36fd90a 2022-07-09 op warnx("%d. warnx", 1);
127 f36fd90a 2022-07-09 op warnc(ENOENT, "%d. warn", ENOENT);
128 f36fd90a 2022-07-09 op warn("%d. warn", 2);
129 f36fd90a 2022-07-09 op err(0, "%d. err", 3);
130 f36fd90a 2022-07-09 op errx(0, "%d. err", 3);
131 f36fd90a 2022-07-09 op errc(0, ENOENT, "%d. err", 3);
132 f36fd90a 2022-07-09 op /* NOTREACHED */
135 f36fd90a 2022-07-09 op #endif /* TEST_ERR */
136 f36fd90a 2022-07-09 op #if TEST_EXPLICIT_BZERO
137 f36fd90a 2022-07-09 op #include <string.h>
142 f36fd90a 2022-07-09 op char foo[10];
144 f36fd90a 2022-07-09 op explicit_bzero(foo, sizeof(foo));
147 f36fd90a 2022-07-09 op #endif /* TEST_EXPLICIT_BZERO */
148 f36fd90a 2022-07-09 op #if TEST_FLOCK
149 f36fd90a 2022-07-09 op #include <fcntl.h>
154 f36fd90a 2022-07-09 op flock(0, LOCK_SH|LOCK_NB);
157 f36fd90a 2022-07-09 op #endif /* TEST_FLOCK */
158 f36fd90a 2022-07-09 op #if TEST_FREEZERO
159 f36fd90a 2022-07-09 op #include <stdlib.h>
164 f36fd90a 2022-07-09 op freezero(NULL, 0);
167 f36fd90a 2022-07-09 op #endif /* TEST_FREEZERO */
169 f36fd90a 2022-07-09 op #include <stddef.h>
170 f36fd90a 2022-07-09 op #include <sys/types.h>
171 f36fd90a 2022-07-09 op #include <sys/stat.h>
172 f36fd90a 2022-07-09 op #include <fts.h>
177 f36fd90a 2022-07-09 op const char *argv[2];
179 f36fd90a 2022-07-09 op FTSENT *entry;
181 f36fd90a 2022-07-09 op argv[0] = ".";
182 f36fd90a 2022-07-09 op argv[1] = (char *)NULL;
184 f36fd90a 2022-07-09 op ftsp = fts_open((char * const *)argv,
185 f36fd90a 2022-07-09 op FTS_PHYSICAL | FTS_NOCHDIR, NULL);
187 f36fd90a 2022-07-09 op if (ftsp == NULL)
190 f36fd90a 2022-07-09 op entry = fts_read(ftsp);
192 f36fd90a 2022-07-09 op if (entry == NULL)
195 f36fd90a 2022-07-09 op if (fts_set(ftsp, entry, FTS_SKIP) != 0)
198 f36fd90a 2022-07-09 op if (fts_close(ftsp) != 0)
203 f36fd90a 2022-07-09 op #endif /* TEST_FTS */
204 f36fd90a 2022-07-09 op #if TEST_GETEXECNAME
205 f36fd90a 2022-07-09 op #include <stdlib.h>
210 f36fd90a 2022-07-09 op const char * progname;
212 f36fd90a 2022-07-09 op progname = getexecname();
213 f36fd90a 2022-07-09 op return progname == NULL;
215 f36fd90a 2022-07-09 op #endif /* TEST_GETEXECNAME */
216 f36fd90a 2022-07-09 op #if TEST_GETPROGNAME
217 f36fd90a 2022-07-09 op #include <stdlib.h>
222 f36fd90a 2022-07-09 op const char * progname;
224 f36fd90a 2022-07-09 op progname = getprogname();
225 f36fd90a 2022-07-09 op return progname == NULL;
227 f36fd90a 2022-07-09 op #endif /* TEST_GETPROGNAME */
228 f36fd90a 2022-07-09 op #if TEST_IMSG
229 f36fd90a 2022-07-09 op #include <sys/types.h>
230 f36fd90a 2022-07-09 op #include <sys/queue.h>
231 f36fd90a 2022-07-09 op #include <sys/uio.h>
232 f36fd90a 2022-07-09 op #include <stdint.h>
233 f36fd90a 2022-07-09 op #include <imsg.h>
238 f36fd90a 2022-07-09 op struct imsgbuf ibuf;
240 f36fd90a 2022-07-09 op imsg_init(&ibuf, 3);
241 f36fd90a 2022-07-09 op imsg_clear(&ibuf);
244 f36fd90a 2022-07-09 op #endif /* TEST_IMSG */
245 f36fd90a 2022-07-09 op #if TEST_INFTIM
247 f36fd90a 2022-07-09 op * Linux doesn't (always?) have this.
250 f36fd90a 2022-07-09 op #include <poll.h>
251 f36fd90a 2022-07-09 op #include <stdio.h>
256 f36fd90a 2022-07-09 op printf("INFTIM is defined to be %ld\n", (long)INFTIM);
259 f36fd90a 2022-07-09 op #endif /* TEST_INFTIM */
260 f36fd90a 2022-07-09 op #if TEST_LANDLOCK
261 f36fd90a 2022-07-09 op #include <linux/landlock.h>
262 f36fd90a 2022-07-09 op #include <stdlib.h>
263 f36fd90a 2022-07-09 op #include <sys/prctl.h>
264 f36fd90a 2022-07-09 op #include <sys/syscall.h>
265 f36fd90a 2022-07-09 op #include <unistd.h>
266 f36fd90a 2022-07-09 op #include <stdint.h>
268 f36fd90a 2022-07-09 op #ifndef landlock_create_ruleset
269 f36fd90a 2022-07-09 op static inline int landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,
270 f36fd90a 2022-07-09 op const size_t size, const __u32 flags)
272 f36fd90a 2022-07-09 op return syscall(__NR_landlock_create_ruleset, attr, size, flags);
276 f36fd90a 2022-07-09 op #ifndef landlock_restrict_self
277 f36fd90a 2022-07-09 op static inline int landlock_restrict_self(const int ruleset_fd,
278 f36fd90a 2022-07-09 op const __u32 flags)
280 f36fd90a 2022-07-09 op return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
287 f36fd90a 2022-07-09 op uint64_t mask = LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE;
288 f36fd90a 2022-07-09 op struct landlock_ruleset_attr rules = {
289 f36fd90a 2022-07-09 op .handled_access_fs = mask
291 f36fd90a 2022-07-09 op int fd = landlock_create_ruleset(&rules, sizeof(rules), 0);
293 f36fd90a 2022-07-09 op if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
295 f36fd90a 2022-07-09 op return landlock_restrict_self(fd, 0) ? 1 : 0;
297 f36fd90a 2022-07-09 op #endif /* TEST_LANDLOCK */
298 f36fd90a 2022-07-09 op #if TEST_LIB_SOCKET
299 f36fd90a 2022-07-09 op #include <sys/socket.h>
304 f36fd90a 2022-07-09 op int fds[2], c;
306 f36fd90a 2022-07-09 op c = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
307 f36fd90a 2022-07-09 op return c == -1;
309 f36fd90a 2022-07-09 op #endif /* TEST_LIB_SOCKET */
311 f36fd90a 2022-07-09 op #include <sys/types.h>
312 f36fd90a 2022-07-09 op #include <md5.h>
314 f36fd90a 2022-07-09 op int main(void)
317 f36fd90a 2022-07-09 op char result[MD5_DIGEST_STRING_LENGTH];
319 f36fd90a 2022-07-09 op MD5Init(&ctx);
320 f36fd90a 2022-07-09 op MD5Update(&ctx, (const unsigned char *)"abcd", 4);
321 f36fd90a 2022-07-09 op MD5End(&ctx, result);
325 f36fd90a 2022-07-09 op #endif /* TEST_MD5 */
326 f36fd90a 2022-07-09 op #if TEST_MEMMEM
327 f36fd90a 2022-07-09 op #define _GNU_SOURCE
328 f36fd90a 2022-07-09 op #include <string.h>
333 f36fd90a 2022-07-09 op char *a = memmem("hello, world", strlen("hello, world"), "world", strlen("world"));
334 f36fd90a 2022-07-09 op return(NULL == a);
336 f36fd90a 2022-07-09 op #endif /* TEST_MEMMEM */
337 f36fd90a 2022-07-09 op #if TEST_MEMRCHR
338 f36fd90a 2022-07-09 op #if defined(__linux__) || defined(__MINT__)
339 f36fd90a 2022-07-09 op #define _GNU_SOURCE /* See test-*.c what needs this. */
341 f36fd90a 2022-07-09 op #include <string.h>
346 f36fd90a 2022-07-09 op const char *buf = "abcdef";
349 f36fd90a 2022-07-09 op res = memrchr(buf, 'a', strlen(buf));
350 f36fd90a 2022-07-09 op return(NULL == res ? 1 : 0);
352 f36fd90a 2022-07-09 op #endif /* TEST_MEMRCHR */
353 f36fd90a 2022-07-09 op #if TEST_MEMSET_S
354 f36fd90a 2022-07-09 op #include <string.h>
356 f36fd90a 2022-07-09 op int main(void)
358 f36fd90a 2022-07-09 op char buf[10];
359 f36fd90a 2022-07-09 op memset_s(buf, 0, 'c', sizeof(buf));
362 f36fd90a 2022-07-09 op #endif /* TEST_MEMSET_S */
363 f36fd90a 2022-07-09 op #if TEST_MKFIFOAT
364 f36fd90a 2022-07-09 op #include <sys/stat.h>
365 f36fd90a 2022-07-09 op #include <fcntl.h>
367 f36fd90a 2022-07-09 op int main(void) {
368 f36fd90a 2022-07-09 op mkfifoat(AT_FDCWD, "this/path/should/not/exist", 0600);
371 f36fd90a 2022-07-09 op #endif /* TEST_MKFIFOAT */
372 f36fd90a 2022-07-09 op #if TEST_MKNODAT
373 f36fd90a 2022-07-09 op #include <sys/stat.h>
374 f36fd90a 2022-07-09 op #include <fcntl.h>
376 f36fd90a 2022-07-09 op int main(void) {
377 f36fd90a 2022-07-09 op mknodat(AT_FDCWD, "this/path/should/not/exist", S_IFIFO | 0600, 0);
380 f36fd90a 2022-07-09 op #endif /* TEST_MKNODAT */
381 f36fd90a 2022-07-09 op #if TEST_OPTRESET
382 f36fd90a 2022-07-09 op #include <unistd.h>
385 f36fd90a 2022-07-09 op main(int argc, char **argv)
387 f36fd90a 2022-07-09 op optreset = 1;
388 f36fd90a 2022-07-09 op getopt(argc, argv, "");
391 f36fd90a 2022-07-09 op #endif /* TEST_OPTRESET */
392 f36fd90a 2022-07-09 op #if TEST_OSBYTEORDER_H
393 f36fd90a 2022-07-09 op #include <libkern/OSByteOrder.h>
398 f36fd90a 2022-07-09 op return !OSSwapHostToLittleInt32(23);
400 f36fd90a 2022-07-09 op #endif /* TEST_OSBYTEORDER_H */
401 f36fd90a 2022-07-09 op #if TEST_PATH_MAX
403 f36fd90a 2022-07-09 op * POSIX allows PATH_MAX to not be defined, see
404 f36fd90a 2022-07-09 op * http://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html;
405 f36fd90a 2022-07-09 op * the GNU Hurd is an example of a system not having it.
407 f36fd90a 2022-07-09 op * Arguably, it would be better to test sysconf(_SC_PATH_MAX),
408 f36fd90a 2022-07-09 op * but since the individual *.c files include "config.h" before
409 f36fd90a 2022-07-09 op * <limits.h>, overriding an excessive value of PATH_MAX from
410 f36fd90a 2022-07-09 op * "config.h" is impossible anyway, so for now, the simplest
411 f36fd90a 2022-07-09 op * fix is to provide a value only on systems not having any.
412 f36fd90a 2022-07-09 op * So far, we encountered no system defining PATH_MAX to an
413 f36fd90a 2022-07-09 op * impractically large value, even though POSIX explicitly
414 f36fd90a 2022-07-09 op * allows that.
416 f36fd90a 2022-07-09 op * The real fix would be to replace all static buffers of size
417 f36fd90a 2022-07-09 op * PATH_MAX by dynamically allocated buffers. But that is
418 f36fd90a 2022-07-09 op * somewhat intrusive because it touches several files and
419 f36fd90a 2022-07-09 op * because it requires changing struct mlink in mandocdb.c.
420 f36fd90a 2022-07-09 op * So i'm postponing that for now.
423 f36fd90a 2022-07-09 op #include <limits.h>
424 f36fd90a 2022-07-09 op #include <stdio.h>
429 f36fd90a 2022-07-09 op printf("PATH_MAX is defined to be %ld\n", (long)PATH_MAX);
432 f36fd90a 2022-07-09 op #endif /* TEST_PATH_MAX */
433 f36fd90a 2022-07-09 op #if TEST_PLEDGE
434 f36fd90a 2022-07-09 op #include <unistd.h>
439 f36fd90a 2022-07-09 op return !!pledge("stdio", NULL);
441 f36fd90a 2022-07-09 op #endif /* TEST_PLEDGE */
442 f36fd90a 2022-07-09 op #if TEST_PROGRAM_INVOCATION_SHORT_NAME
443 f36fd90a 2022-07-09 op #define _GNU_SOURCE /* See feature_test_macros(7) */
444 f36fd90a 2022-07-09 op #include <errno.h>
450 f36fd90a 2022-07-09 op return !program_invocation_short_name;
452 f36fd90a 2022-07-09 op #endif /* TEST_PROGRAM_INVOCATION_SHORT_NAME */
453 f36fd90a 2022-07-09 op #if TEST_PR_SET_NAME
454 f36fd90a 2022-07-09 op #include <sys/prctl.h>
459 f36fd90a 2022-07-09 op prctl(PR_SET_NAME, "foo");
462 f36fd90a 2022-07-09 op #endif /* TEST_PR_SET_NAME */
463 f36fd90a 2022-07-09 op #if TEST_READPASSPHRASE
464 f36fd90a 2022-07-09 op #include <stddef.h>
465 f36fd90a 2022-07-09 op #include <readpassphrase.h>
470 f36fd90a 2022-07-09 op return !!readpassphrase("prompt: ", NULL, 0, 0);
472 f36fd90a 2022-07-09 op #endif /* TEST_READPASSPHRASE */
473 f36fd90a 2022-07-09 op #if TEST_REALLOCARRAY
474 f36fd90a 2022-07-09 op #ifdef __NetBSD__
475 f36fd90a 2022-07-09 op # define _OPENBSD_SOURCE
477 f36fd90a 2022-07-09 op #include <stdlib.h>
482 f36fd90a 2022-07-09 op return !reallocarray(NULL, 2, 2);
484 f36fd90a 2022-07-09 op #endif /* TEST_REALLOCARRAY */
485 f36fd90a 2022-07-09 op #if TEST_RECALLOCARRAY
486 f36fd90a 2022-07-09 op #include <stdlib.h>
491 f36fd90a 2022-07-09 op return !recallocarray(NULL, 0, 2, 2);
493 f36fd90a 2022-07-09 op #endif /* TEST_RECALLOCARRAY */
494 f36fd90a 2022-07-09 op #if TEST_SANDBOX_INIT
495 f36fd90a 2022-07-09 op #include <sandbox.h>
503 f36fd90a 2022-07-09 op rc = sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, &ep);
504 f36fd90a 2022-07-09 op if (-1 == rc)
505 f36fd90a 2022-07-09 op sandbox_free_error(ep);
506 f36fd90a 2022-07-09 op return(-1 == rc);
508 f36fd90a 2022-07-09 op #endif /* TEST_SANDBOX_INIT */
509 f36fd90a 2022-07-09 op #if TEST_SETPROCTITLE
510 f36fd90a 2022-07-09 op #include <stdlib.h>
515 f36fd90a 2022-07-09 op setproctitle("#%d test program", 7);
519 f36fd90a 2022-07-09 op #if TEST_SIO_FLUSH
520 f36fd90a 2022-07-09 op #include <sndio.h>
525 f36fd90a 2022-07-09 op struct sio_hdl *hdl;
526 f36fd90a 2022-07-09 op hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1);
527 f36fd90a 2022-07-09 op sio_flush(hdl);
528 f36fd90a 2022-07-09 op sio_close(hdl);
531 f36fd90a 2022-07-09 op #endif /* TEST_SIO_FLUSH */
532 f36fd90a 2022-07-09 op #if TEST_SCAN_SCALED
533 f36fd90a 2022-07-09 op #include <util.h>
538 f36fd90a 2022-07-09 op char *cinput = (char *)"1.5K", buf[FMT_SCALED_STRSIZE];
539 f36fd90a 2022-07-09 op long long ninput = 10483892, result;
540 f36fd90a 2022-07-09 op return scan_scaled(cinput, &result) == 0;
542 f36fd90a 2022-07-09 op #endif /* TEST_SCAN_SCALED */
543 f36fd90a 2022-07-09 op #if TEST_SECCOMP_FILTER
544 f36fd90a 2022-07-09 op #include <sys/prctl.h>
545 f36fd90a 2022-07-09 op #include <linux/seccomp.h>
546 f36fd90a 2022-07-09 op #include <errno.h>
552 f36fd90a 2022-07-09 op prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, 0);
553 f36fd90a 2022-07-09 op return(EFAULT == errno ? 0 : 1);
555 f36fd90a 2022-07-09 op #endif /* TEST_SECCOMP_FILTER */
556 f36fd90a 2022-07-09 op #if TEST_SETRESGID
557 f36fd90a 2022-07-09 op #define _GNU_SOURCE /* linux */
558 f36fd90a 2022-07-09 op #include <sys/types.h>
559 f36fd90a 2022-07-09 op #include <unistd.h>
564 f36fd90a 2022-07-09 op return setresgid(-1, -1, -1) == -1;
566 f36fd90a 2022-07-09 op #endif /* TEST_SETRESGID */
567 f36fd90a 2022-07-09 op #if TEST_SETRESUID
568 f36fd90a 2022-07-09 op #define _GNU_SOURCE /* linux */
569 f36fd90a 2022-07-09 op #include <sys/types.h>
570 f36fd90a 2022-07-09 op #include <unistd.h>
575 f36fd90a 2022-07-09 op return setresuid(-1, -1, -1) == -1;
577 f36fd90a 2022-07-09 op #endif /* TEST_SETRESUID */
578 f36fd90a 2022-07-09 op #if TEST_SHA2
579 f36fd90a 2022-07-09 op #include <sys/types.h>
580 f36fd90a 2022-07-09 op #include <sha2.h>
582 f36fd90a 2022-07-09 op int main(void)
584 f36fd90a 2022-07-09 op SHA2_CTX ctx;
585 f36fd90a 2022-07-09 op char result[SHA256_DIGEST_STRING_LENGTH];
587 f36fd90a 2022-07-09 op SHA256Init(&ctx);
588 f36fd90a 2022-07-09 op SHA256Update(&ctx, (const unsigned char *)"abcd", 4);
589 f36fd90a 2022-07-09 op SHA256End(&ctx, result);
593 f36fd90a 2022-07-09 op #endif /* TEST_SHA2 */
594 f36fd90a 2022-07-09 op #if TEST_SOCK_NONBLOCK
596 f36fd90a 2022-07-09 op * Linux doesn't (always?) have this.
599 f36fd90a 2022-07-09 op #include <sys/socket.h>
605 f36fd90a 2022-07-09 op socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fd);
608 f36fd90a 2022-07-09 op #endif /* TEST_SOCK_NONBLOCK */
609 f36fd90a 2022-07-09 op #if TEST_STATIC
613 f36fd90a 2022-07-09 op return 0; /* not meant to do anything */
615 f36fd90a 2022-07-09 op #endif /* TEST_STATIC */
616 f36fd90a 2022-07-09 op #if TEST_STRLCAT
617 f36fd90a 2022-07-09 op #include <string.h>
622 f36fd90a 2022-07-09 op char buf[3] = "a";
623 f36fd90a 2022-07-09 op return ! (strlcat(buf, "b", sizeof(buf)) == 2 &&
624 f36fd90a 2022-07-09 op buf[0] == 'a' && buf[1] == 'b' && buf[2] == '\0');
626 f36fd90a 2022-07-09 op #endif /* TEST_STRLCAT */
627 f36fd90a 2022-07-09 op #if TEST_STRLCPY
628 f36fd90a 2022-07-09 op #include <string.h>
633 f36fd90a 2022-07-09 op char buf[2] = "";
634 f36fd90a 2022-07-09 op return ! (strlcpy(buf, "a", sizeof(buf)) == 1 &&
635 f36fd90a 2022-07-09 op buf[0] == 'a' && buf[1] == '\0');
637 f36fd90a 2022-07-09 op #endif /* TEST_STRLCPY */
638 f36fd90a 2022-07-09 op #if TEST_STRNDUP
639 f36fd90a 2022-07-09 op #include <string.h>
644 f36fd90a 2022-07-09 op const char *foo = "bar";
647 f36fd90a 2022-07-09 op baz = strndup(foo, 1);
648 f36fd90a 2022-07-09 op return(0 != strcmp(baz, "b"));
650 f36fd90a 2022-07-09 op #endif /* TEST_STRNDUP */
651 f36fd90a 2022-07-09 op #if TEST_STRNLEN
652 f36fd90a 2022-07-09 op #include <string.h>
657 f36fd90a 2022-07-09 op const char *foo = "bar";
660 f36fd90a 2022-07-09 op sz = strnlen(foo, 1);
661 f36fd90a 2022-07-09 op return(1 != sz);
663 f36fd90a 2022-07-09 op #endif /* TEST_STRNLEN */
664 f36fd90a 2022-07-09 op #if TEST_STRTONUM
666 f36fd90a 2022-07-09 op * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
668 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
669 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
670 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
672 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
673 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
674 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
675 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
676 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
677 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
678 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
680 f36fd90a 2022-07-09 op #ifdef __NetBSD__
681 f36fd90a 2022-07-09 op # define _OPENBSD_SOURCE
683 f36fd90a 2022-07-09 op #include <stdlib.h>
688 f36fd90a 2022-07-09 op const char *errstr;
690 f36fd90a 2022-07-09 op if (strtonum("1", 0, 2, &errstr) != 1)
692 f36fd90a 2022-07-09 op if (errstr != NULL)
694 f36fd90a 2022-07-09 op if (strtonum("1x", 0, 2, &errstr) != 0)
696 f36fd90a 2022-07-09 op if (errstr == NULL)
698 f36fd90a 2022-07-09 op if (strtonum("2", 0, 1, &errstr) != 0)
700 f36fd90a 2022-07-09 op if (errstr == NULL)
702 f36fd90a 2022-07-09 op if (strtonum("0", 1, 2, &errstr) != 0)
704 f36fd90a 2022-07-09 op if (errstr == NULL)
708 f36fd90a 2022-07-09 op #endif /* TEST_STRTONUM */
709 f36fd90a 2022-07-09 op #if TEST_SYS_BYTEORDER_H
710 f36fd90a 2022-07-09 op #include <sys/byteorder.h>
715 f36fd90a 2022-07-09 op return !LE_32(23);
717 f36fd90a 2022-07-09 op #endif /* TEST_SYS_BYTEORDER_H */
718 f36fd90a 2022-07-09 op #if TEST_SYS_ENDIAN_H
719 f36fd90a 2022-07-09 op #include <sys/endian.h>
724 f36fd90a 2022-07-09 op return !htole32(23);
726 f36fd90a 2022-07-09 op #endif /* TEST_SYS_ENDIAN_H */
727 f36fd90a 2022-07-09 op #if TEST_SYS_MKDEV_H
728 f36fd90a 2022-07-09 op #include <sys/types.h>
729 f36fd90a 2022-07-09 op #include <sys/mkdev.h>
734 f36fd90a 2022-07-09 op return !minor(0);
736 f36fd90a 2022-07-09 op #endif /* TEST_SYS_MKDEV_H */
737 f36fd90a 2022-07-09 op #if TEST_SYS_FILE
738 f36fd90a 2022-07-09 op #include <sys/file.h>
743 f36fd90a 2022-07-09 op flock(0, LOCK_SH|LOCK_NB);
746 f36fd90a 2022-07-09 op #endif /* TEST_SYS_FILE */
747 f36fd90a 2022-07-09 op #if TEST_SYS_QUEUE
748 f36fd90a 2022-07-09 op #include <sys/queue.h>
749 f36fd90a 2022-07-09 op #include <stddef.h>
753 f36fd90a 2022-07-09 op TAILQ_ENTRY(foo) entries;
756 f36fd90a 2022-07-09 op TAILQ_HEAD(fooq, foo);
761 f36fd90a 2022-07-09 op struct fooq foo_q, bar_q;
762 f36fd90a 2022-07-09 op struct foo *p, *tmp;
765 f36fd90a 2022-07-09 op TAILQ_INIT(&foo_q);
766 f36fd90a 2022-07-09 op TAILQ_INIT(&bar_q);
769 f36fd90a 2022-07-09 op * Use TAILQ_FOREACH_SAFE because some systems (e.g., Linux)
770 f36fd90a 2022-07-09 op * have TAILQ_FOREACH but not the safe variant.
773 f36fd90a 2022-07-09 op TAILQ_FOREACH_SAFE(p, &foo_q, entries, tmp)
774 f36fd90a 2022-07-09 op p->bar = i++;
776 f36fd90a 2022-07-09 op /* Test for newer macros as well. */
778 f36fd90a 2022-07-09 op TAILQ_CONCAT(&foo_q, &bar_q, entries);
781 f36fd90a 2022-07-09 op #endif /* TEST_SYS_QUEUE */
782 f36fd90a 2022-07-09 op #if TEST_SYS_SYSMACROS_H
783 f36fd90a 2022-07-09 op #include <sys/sysmacros.h>
788 f36fd90a 2022-07-09 op return !minor(0);
790 f36fd90a 2022-07-09 op #endif /* TEST_SYS_SYSMACROS_H */
791 f36fd90a 2022-07-09 op #if TEST_SYS_TREE
792 f36fd90a 2022-07-09 op #include <sys/tree.h>
793 f36fd90a 2022-07-09 op #include <stdlib.h>
795 f36fd90a 2022-07-09 op struct node {
796 f36fd90a 2022-07-09 op RB_ENTRY(node) entry;
801 f36fd90a 2022-07-09 op intcmp(struct node *e1, struct node *e2)
803 f36fd90a 2022-07-09 op return (e1->i < e2->i ? -1 : e1->i > e2->i);
806 f36fd90a 2022-07-09 op RB_HEAD(inttree, node) head = RB_INITIALIZER(&head);
807 f36fd90a 2022-07-09 op RB_PROTOTYPE(inttree, node, entry, intcmp)
808 f36fd90a 2022-07-09 op RB_GENERATE(inttree, node, entry, intcmp)
810 f36fd90a 2022-07-09 op int testdata[] = {
811 f36fd90a 2022-07-09 op 20, 16, 17, 13, 3, 6, 1, 8, 2, 4
818 f36fd90a 2022-07-09 op struct node *n;
820 f36fd90a 2022-07-09 op for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) {
821 f36fd90a 2022-07-09 op if ((n = malloc(sizeof(struct node))) == NULL)
823 f36fd90a 2022-07-09 op n->i = testdata[i];
824 f36fd90a 2022-07-09 op RB_INSERT(inttree, &head, n);
830 f36fd90a 2022-07-09 op #endif /* TEST_SYS_TREE */
831 f36fd90a 2022-07-09 op #if TEST_UNVEIL
832 f36fd90a 2022-07-09 op #include <unistd.h>
837 f36fd90a 2022-07-09 op return -1 != unveil(NULL, NULL);
839 f36fd90a 2022-07-09 op #endif /* TEST_UNVEIL */
840 f36fd90a 2022-07-09 op #if TEST_WAIT_ANY
841 f36fd90a 2022-07-09 op #include <sys/wait.h>
848 f36fd90a 2022-07-09 op return waitpid(WAIT_ANY, &st, WNOHANG) != -1;
850 f36fd90a 2022-07-09 op #endif /* TEST_WAIT_ANY */
851 f36fd90a 2022-07-09 op #if TEST_LIBEVENT
852 f36fd90a 2022-07-09 op #include <event.h>
857 f36fd90a 2022-07-09 op struct event ev;
859 f36fd90a 2022-07-09 op event_set(&ev, 0, EV_READ, NULL, NULL);
860 f36fd90a 2022-07-09 op event_add(&ev, NULL);
861 f36fd90a 2022-07-09 op event_del(&ev);
864 f36fd90a 2022-07-09 op #endif /* TEST_LIBEVENT */
865 f36fd90a 2022-07-09 op #if TEST_LIBEVENT2
866 f36fd90a 2022-07-09 op #include <event2/event.h>
867 f36fd90a 2022-07-09 op #include <event2/event_compat.h>
868 f36fd90a 2022-07-09 op #include <event2/event_struct.h>
869 f36fd90a 2022-07-09 op #include <event2/buffer.h>
870 f36fd90a 2022-07-09 op #include <event2/buffer_compat.h>
871 f36fd90a 2022-07-09 op #include <event2/bufferevent.h>
872 f36fd90a 2022-07-09 op #include <event2/bufferevent_struct.h>
873 f36fd90a 2022-07-09 op #include <event2/bufferevent_compat.h>
878 f36fd90a 2022-07-09 op struct event ev;
880 f36fd90a 2022-07-09 op event_set(&ev, 0, EV_READ, NULL, NULL);
881 f36fd90a 2022-07-09 op event_add(&ev, NULL);
882 f36fd90a 2022-07-09 op event_del(&ev);
885 f36fd90a 2022-07-09 op #endif /* TEST_LIBEVENT2 */
886 f36fd90a 2022-07-09 op #if TEST_LIB_FLAC
887 f36fd90a 2022-07-09 op #include <FLAC/stream_decoder.h>
892 f36fd90a 2022-07-09 op FLAC__StreamDecoder *decoder = NULL;
893 f36fd90a 2022-07-09 op FLAC__StreamDecoderInitStatus init_status;
895 f36fd90a 2022-07-09 op decoder = FLAC__stream_decoder_new();
896 f36fd90a 2022-07-09 op FLAC__stream_decoder_delete(decoder);
899 f36fd90a 2022-07-09 op #endif /* TEST_LIB_FLAC */
900 f36fd90a 2022-07-09 op #if TEST_LIB_MPG123
901 f36fd90a 2022-07-09 op #include <mpg123.h>
906 f36fd90a 2022-07-09 op mpg123_handle *mh;
908 f36fd90a 2022-07-09 op mh = mpg123_new(NULL, NULL);
909 f36fd90a 2022-07-09 op mpg123_delete(mh);
912 f36fd90a 2022-07-09 op #endif/* TEST_LIB_MPG123 */
913 f36fd90a 2022-07-09 op #if TEST_LIB_OPUSFILE
914 f36fd90a 2022-07-09 op #include <stdio.h>
915 f36fd90a 2022-07-09 op #include <opusfile.h>
921 f36fd90a 2022-07-09 op OggOpusFile *of;
922 f36fd90a 2022-07-09 op OpusFileCallbacks cb = {NULL, NULL, NULL, NULL};
925 f36fd90a 2022-07-09 op f = op_fdopen(&cb, 0, "r");
926 f36fd90a 2022-07-09 op of = op_open_callbacks(f, &cb, NULL, 0, &r);
930 f36fd90a 2022-07-09 op #endif /* TEST_LIB_OPUSFILE */
931 f36fd90a 2022-07-09 op #if TEST_LIB_VORBISFILE
932 f36fd90a 2022-07-09 op #include <stdio.h>
933 f36fd90a 2022-07-09 op #include <vorbis/vorbisfile.h>
938 f36fd90a 2022-07-09 op OggVorbis_File vf;
940 f36fd90a 2022-07-09 op ov_open_callbacks(stdin, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE);
941 f36fd90a 2022-07-09 op ov_clear(&vf);
944 f36fd90a 2022-07-09 op #endif /* TEST_LIB_VORBISFILE */
945 34674849 2023-03-23 op #if TEST_LIB_ASOUND
946 34674849 2023-03-23 op #include <alsa/asoundlib.h>
951 34674849 2023-03-23 op snd_pcm_t *pcm;
954 34674849 2023-03-23 op err = snd_pcm_open(&pcm, "default", SND_PCM_STREAM_PLAYBACK,
955 34674849 2023-03-23 op SND_PCM_NONBLOCK);
958 34674849 2023-03-23 op #endif /* TEST_LIB_ASOUND */
959 f36fd90a 2022-07-09 op #if TEST_LIB_SNDIO
960 f36fd90a 2022-07-09 op #include <sndio.h>
965 f36fd90a 2022-07-09 op struct sio_hdl *hdl;
966 f36fd90a 2022-07-09 op hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1);
967 f36fd90a 2022-07-09 op sio_close(hdl);
970 f36fd90a 2022-07-09 op #endif /* TEST_LIB_SNDIO */