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 f26a3c73 2023-09-06 op #if TEST_GETDTABLECOUNT
205 f26a3c73 2023-09-06 op #include <unistd.h>
210 f26a3c73 2023-09-06 op return getdtablecount();
212 f26a3c73 2023-09-06 op #endif /* TEST_GETDTABLECOUNT */
213 4e457e62 2023-12-10 op #if TEST_GETDTABLESIZE
214 4e457e62 2023-12-10 op #include <unistd.h>
219 4e457e62 2023-12-10 op return getdtablesize();
221 4e457e62 2023-12-10 op #endif /* TEST_GETDTABLESIZE */
222 f36fd90a 2022-07-09 op #if TEST_GETEXECNAME
223 f36fd90a 2022-07-09 op #include <stdlib.h>
228 f36fd90a 2022-07-09 op const char * progname;
230 f36fd90a 2022-07-09 op progname = getexecname();
231 f36fd90a 2022-07-09 op return progname == NULL;
233 f36fd90a 2022-07-09 op #endif /* TEST_GETEXECNAME */
234 f36fd90a 2022-07-09 op #if TEST_GETPROGNAME
235 f36fd90a 2022-07-09 op #include <stdlib.h>
240 f36fd90a 2022-07-09 op const char * progname;
242 f36fd90a 2022-07-09 op progname = getprogname();
243 f36fd90a 2022-07-09 op return progname == NULL;
245 f36fd90a 2022-07-09 op #endif /* TEST_GETPROGNAME */
246 82556d5b 2023-09-07 op #if TEST_LIB_IMSG
247 f36fd90a 2022-07-09 op #include <sys/types.h>
248 f36fd90a 2022-07-09 op #include <sys/queue.h>
249 f36fd90a 2022-07-09 op #include <sys/uio.h>
250 f36fd90a 2022-07-09 op #include <stdint.h>
251 f36fd90a 2022-07-09 op #include <imsg.h>
256 f36fd90a 2022-07-09 op struct imsgbuf ibuf;
258 f36fd90a 2022-07-09 op imsg_init(&ibuf, 3);
259 f36fd90a 2022-07-09 op imsg_clear(&ibuf);
262 82556d5b 2023-09-07 op #endif /* TEST_LIB_IMSG */
263 f36fd90a 2022-07-09 op #if TEST_INFTIM
265 f36fd90a 2022-07-09 op * Linux doesn't (always?) have this.
268 f36fd90a 2022-07-09 op #include <poll.h>
269 f36fd90a 2022-07-09 op #include <stdio.h>
274 f36fd90a 2022-07-09 op printf("INFTIM is defined to be %ld\n", (long)INFTIM);
277 f36fd90a 2022-07-09 op #endif /* TEST_INFTIM */
278 f36fd90a 2022-07-09 op #if TEST_LANDLOCK
279 f36fd90a 2022-07-09 op #include <linux/landlock.h>
280 f36fd90a 2022-07-09 op #include <stdlib.h>
281 f36fd90a 2022-07-09 op #include <sys/prctl.h>
282 f36fd90a 2022-07-09 op #include <sys/syscall.h>
283 f36fd90a 2022-07-09 op #include <unistd.h>
284 f36fd90a 2022-07-09 op #include <stdint.h>
286 f36fd90a 2022-07-09 op #ifndef landlock_create_ruleset
287 f36fd90a 2022-07-09 op static inline int landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,
288 f36fd90a 2022-07-09 op const size_t size, const __u32 flags)
290 f36fd90a 2022-07-09 op return syscall(__NR_landlock_create_ruleset, attr, size, flags);
294 f36fd90a 2022-07-09 op #ifndef landlock_restrict_self
295 f36fd90a 2022-07-09 op static inline int landlock_restrict_self(const int ruleset_fd,
296 f36fd90a 2022-07-09 op const __u32 flags)
298 f36fd90a 2022-07-09 op return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
305 f36fd90a 2022-07-09 op uint64_t mask = LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE;
306 f36fd90a 2022-07-09 op struct landlock_ruleset_attr rules = {
307 f36fd90a 2022-07-09 op .handled_access_fs = mask
309 f36fd90a 2022-07-09 op int fd = landlock_create_ruleset(&rules, sizeof(rules), 0);
311 f36fd90a 2022-07-09 op if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
313 f36fd90a 2022-07-09 op return landlock_restrict_self(fd, 0) ? 1 : 0;
315 f36fd90a 2022-07-09 op #endif /* TEST_LANDLOCK */
316 f36fd90a 2022-07-09 op #if TEST_LIB_SOCKET
317 f36fd90a 2022-07-09 op #include <sys/socket.h>
322 f36fd90a 2022-07-09 op int fds[2], c;
324 f36fd90a 2022-07-09 op c = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
325 f36fd90a 2022-07-09 op return c == -1;
327 f36fd90a 2022-07-09 op #endif /* TEST_LIB_SOCKET */
329 f36fd90a 2022-07-09 op #include <sys/types.h>
330 f36fd90a 2022-07-09 op #include <md5.h>
332 f36fd90a 2022-07-09 op int main(void)
335 f36fd90a 2022-07-09 op char result[MD5_DIGEST_STRING_LENGTH];
337 f36fd90a 2022-07-09 op MD5Init(&ctx);
338 f36fd90a 2022-07-09 op MD5Update(&ctx, (const unsigned char *)"abcd", 4);
339 f36fd90a 2022-07-09 op MD5End(&ctx, result);
343 f36fd90a 2022-07-09 op #endif /* TEST_MD5 */
344 f36fd90a 2022-07-09 op #if TEST_MEMMEM
345 f36fd90a 2022-07-09 op #define _GNU_SOURCE
346 f36fd90a 2022-07-09 op #include <string.h>
351 f36fd90a 2022-07-09 op char *a = memmem("hello, world", strlen("hello, world"), "world", strlen("world"));
352 f36fd90a 2022-07-09 op return(NULL == a);
354 f36fd90a 2022-07-09 op #endif /* TEST_MEMMEM */
355 f36fd90a 2022-07-09 op #if TEST_MEMRCHR
356 f36fd90a 2022-07-09 op #if defined(__linux__) || defined(__MINT__)
357 f36fd90a 2022-07-09 op #define _GNU_SOURCE /* See test-*.c what needs this. */
359 f36fd90a 2022-07-09 op #include <string.h>
364 f36fd90a 2022-07-09 op const char *buf = "abcdef";
367 f36fd90a 2022-07-09 op res = memrchr(buf, 'a', strlen(buf));
368 f36fd90a 2022-07-09 op return(NULL == res ? 1 : 0);
370 f36fd90a 2022-07-09 op #endif /* TEST_MEMRCHR */
371 f36fd90a 2022-07-09 op #if TEST_MEMSET_S
372 f36fd90a 2022-07-09 op #include <string.h>
374 f36fd90a 2022-07-09 op int main(void)
376 f36fd90a 2022-07-09 op char buf[10];
377 f36fd90a 2022-07-09 op memset_s(buf, 0, 'c', sizeof(buf));
380 f36fd90a 2022-07-09 op #endif /* TEST_MEMSET_S */
381 f36fd90a 2022-07-09 op #if TEST_MKFIFOAT
382 f36fd90a 2022-07-09 op #include <sys/stat.h>
383 f36fd90a 2022-07-09 op #include <fcntl.h>
385 f36fd90a 2022-07-09 op int main(void) {
386 f36fd90a 2022-07-09 op mkfifoat(AT_FDCWD, "this/path/should/not/exist", 0600);
389 f36fd90a 2022-07-09 op #endif /* TEST_MKFIFOAT */
390 f36fd90a 2022-07-09 op #if TEST_MKNODAT
391 f36fd90a 2022-07-09 op #include <sys/stat.h>
392 f36fd90a 2022-07-09 op #include <fcntl.h>
394 f36fd90a 2022-07-09 op int main(void) {
395 f36fd90a 2022-07-09 op mknodat(AT_FDCWD, "this/path/should/not/exist", S_IFIFO | 0600, 0);
398 f36fd90a 2022-07-09 op #endif /* TEST_MKNODAT */
399 f36fd90a 2022-07-09 op #if TEST_OPTRESET
400 f36fd90a 2022-07-09 op #include <unistd.h>
403 f36fd90a 2022-07-09 op main(int argc, char **argv)
405 f36fd90a 2022-07-09 op optreset = 1;
406 f36fd90a 2022-07-09 op getopt(argc, argv, "");
409 f36fd90a 2022-07-09 op #endif /* TEST_OPTRESET */
410 f36fd90a 2022-07-09 op #if TEST_OSBYTEORDER_H
411 f36fd90a 2022-07-09 op #include <libkern/OSByteOrder.h>
416 f36fd90a 2022-07-09 op return !OSSwapHostToLittleInt32(23);
418 f36fd90a 2022-07-09 op #endif /* TEST_OSBYTEORDER_H */
419 f36fd90a 2022-07-09 op #if TEST_PATH_MAX
421 f36fd90a 2022-07-09 op * POSIX allows PATH_MAX to not be defined, see
422 f36fd90a 2022-07-09 op * http://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html;
423 f36fd90a 2022-07-09 op * the GNU Hurd is an example of a system not having it.
425 f36fd90a 2022-07-09 op * Arguably, it would be better to test sysconf(_SC_PATH_MAX),
426 f36fd90a 2022-07-09 op * but since the individual *.c files include "config.h" before
427 f36fd90a 2022-07-09 op * <limits.h>, overriding an excessive value of PATH_MAX from
428 f36fd90a 2022-07-09 op * "config.h" is impossible anyway, so for now, the simplest
429 f36fd90a 2022-07-09 op * fix is to provide a value only on systems not having any.
430 f36fd90a 2022-07-09 op * So far, we encountered no system defining PATH_MAX to an
431 f36fd90a 2022-07-09 op * impractically large value, even though POSIX explicitly
432 f36fd90a 2022-07-09 op * allows that.
434 f36fd90a 2022-07-09 op * The real fix would be to replace all static buffers of size
435 f36fd90a 2022-07-09 op * PATH_MAX by dynamically allocated buffers. But that is
436 f36fd90a 2022-07-09 op * somewhat intrusive because it touches several files and
437 f36fd90a 2022-07-09 op * because it requires changing struct mlink in mandocdb.c.
438 f36fd90a 2022-07-09 op * So i'm postponing that for now.
441 f36fd90a 2022-07-09 op #include <limits.h>
442 f36fd90a 2022-07-09 op #include <stdio.h>
447 f36fd90a 2022-07-09 op printf("PATH_MAX is defined to be %ld\n", (long)PATH_MAX);
450 f36fd90a 2022-07-09 op #endif /* TEST_PATH_MAX */
451 f36fd90a 2022-07-09 op #if TEST_PLEDGE
452 f36fd90a 2022-07-09 op #include <unistd.h>
457 f36fd90a 2022-07-09 op return !!pledge("stdio", NULL);
459 f36fd90a 2022-07-09 op #endif /* TEST_PLEDGE */
460 f36fd90a 2022-07-09 op #if TEST_PROGRAM_INVOCATION_SHORT_NAME
461 f36fd90a 2022-07-09 op #define _GNU_SOURCE /* See feature_test_macros(7) */
462 f36fd90a 2022-07-09 op #include <errno.h>
468 f36fd90a 2022-07-09 op return !program_invocation_short_name;
470 f36fd90a 2022-07-09 op #endif /* TEST_PROGRAM_INVOCATION_SHORT_NAME */
471 f36fd90a 2022-07-09 op #if TEST_PR_SET_NAME
472 f36fd90a 2022-07-09 op #include <sys/prctl.h>
477 f36fd90a 2022-07-09 op prctl(PR_SET_NAME, "foo");
480 f36fd90a 2022-07-09 op #endif /* TEST_PR_SET_NAME */
481 f36fd90a 2022-07-09 op #if TEST_READPASSPHRASE
482 f36fd90a 2022-07-09 op #include <stddef.h>
483 f36fd90a 2022-07-09 op #include <readpassphrase.h>
488 f36fd90a 2022-07-09 op return !!readpassphrase("prompt: ", NULL, 0, 0);
490 f36fd90a 2022-07-09 op #endif /* TEST_READPASSPHRASE */
491 f36fd90a 2022-07-09 op #if TEST_REALLOCARRAY
492 f36fd90a 2022-07-09 op #ifdef __NetBSD__
493 f36fd90a 2022-07-09 op # define _OPENBSD_SOURCE
495 f36fd90a 2022-07-09 op #include <stdlib.h>
500 f36fd90a 2022-07-09 op return !reallocarray(NULL, 2, 2);
502 f36fd90a 2022-07-09 op #endif /* TEST_REALLOCARRAY */
503 f36fd90a 2022-07-09 op #if TEST_RECALLOCARRAY
504 f36fd90a 2022-07-09 op #include <stdlib.h>
509 f36fd90a 2022-07-09 op return !recallocarray(NULL, 0, 2, 2);
511 f36fd90a 2022-07-09 op #endif /* TEST_RECALLOCARRAY */
512 f36fd90a 2022-07-09 op #if TEST_SANDBOX_INIT
513 f36fd90a 2022-07-09 op #include <sandbox.h>
521 f36fd90a 2022-07-09 op rc = sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, &ep);
522 f36fd90a 2022-07-09 op if (-1 == rc)
523 f36fd90a 2022-07-09 op sandbox_free_error(ep);
524 f36fd90a 2022-07-09 op return(-1 == rc);
526 f36fd90a 2022-07-09 op #endif /* TEST_SANDBOX_INIT */
527 f36fd90a 2022-07-09 op #if TEST_SETPROCTITLE
528 f36fd90a 2022-07-09 op #include <stdlib.h>
533 f36fd90a 2022-07-09 op setproctitle("#%d test program", 7);
537 f36fd90a 2022-07-09 op #if TEST_SIO_FLUSH
538 f36fd90a 2022-07-09 op #include <sndio.h>
543 f36fd90a 2022-07-09 op struct sio_hdl *hdl;
544 f36fd90a 2022-07-09 op hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1);
545 f36fd90a 2022-07-09 op sio_flush(hdl);
546 f36fd90a 2022-07-09 op sio_close(hdl);
549 f36fd90a 2022-07-09 op #endif /* TEST_SIO_FLUSH */
550 f36fd90a 2022-07-09 op #if TEST_SCAN_SCALED
551 f36fd90a 2022-07-09 op #include <util.h>
556 f36fd90a 2022-07-09 op char *cinput = (char *)"1.5K", buf[FMT_SCALED_STRSIZE];
557 f36fd90a 2022-07-09 op long long ninput = 10483892, result;
558 f36fd90a 2022-07-09 op return scan_scaled(cinput, &result) == 0;
560 f36fd90a 2022-07-09 op #endif /* TEST_SCAN_SCALED */
561 f36fd90a 2022-07-09 op #if TEST_SECCOMP_FILTER
562 f36fd90a 2022-07-09 op #include <sys/prctl.h>
563 f36fd90a 2022-07-09 op #include <linux/seccomp.h>
564 f36fd90a 2022-07-09 op #include <errno.h>
570 f36fd90a 2022-07-09 op prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, 0);
571 f36fd90a 2022-07-09 op return(EFAULT == errno ? 0 : 1);
573 f36fd90a 2022-07-09 op #endif /* TEST_SECCOMP_FILTER */
574 f36fd90a 2022-07-09 op #if TEST_SETRESGID
575 f36fd90a 2022-07-09 op #define _GNU_SOURCE /* linux */
576 f36fd90a 2022-07-09 op #include <sys/types.h>
577 f36fd90a 2022-07-09 op #include <unistd.h>
582 f36fd90a 2022-07-09 op return setresgid(-1, -1, -1) == -1;
584 f36fd90a 2022-07-09 op #endif /* TEST_SETRESGID */
585 f36fd90a 2022-07-09 op #if TEST_SETRESUID
586 f36fd90a 2022-07-09 op #define _GNU_SOURCE /* linux */
587 f36fd90a 2022-07-09 op #include <sys/types.h>
588 f36fd90a 2022-07-09 op #include <unistd.h>
593 f36fd90a 2022-07-09 op return setresuid(-1, -1, -1) == -1;
595 f36fd90a 2022-07-09 op #endif /* TEST_SETRESUID */
596 f36fd90a 2022-07-09 op #if TEST_SHA2
597 f36fd90a 2022-07-09 op #include <sys/types.h>
598 f36fd90a 2022-07-09 op #include <sha2.h>
600 f36fd90a 2022-07-09 op int main(void)
602 f36fd90a 2022-07-09 op SHA2_CTX ctx;
603 f36fd90a 2022-07-09 op char result[SHA256_DIGEST_STRING_LENGTH];
605 f36fd90a 2022-07-09 op SHA256Init(&ctx);
606 f36fd90a 2022-07-09 op SHA256Update(&ctx, (const unsigned char *)"abcd", 4);
607 f36fd90a 2022-07-09 op SHA256End(&ctx, result);
611 f36fd90a 2022-07-09 op #endif /* TEST_SHA2 */
612 f36fd90a 2022-07-09 op #if TEST_SOCK_NONBLOCK
614 f36fd90a 2022-07-09 op * Linux doesn't (always?) have this.
617 f36fd90a 2022-07-09 op #include <sys/socket.h>
623 f36fd90a 2022-07-09 op socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fd);
626 f36fd90a 2022-07-09 op #endif /* TEST_SOCK_NONBLOCK */
627 f36fd90a 2022-07-09 op #if TEST_STATIC
631 f36fd90a 2022-07-09 op return 0; /* not meant to do anything */
633 f36fd90a 2022-07-09 op #endif /* TEST_STATIC */
634 f36fd90a 2022-07-09 op #if TEST_STRLCAT
635 f36fd90a 2022-07-09 op #include <string.h>
640 f36fd90a 2022-07-09 op char buf[3] = "a";
641 f36fd90a 2022-07-09 op return ! (strlcat(buf, "b", sizeof(buf)) == 2 &&
642 f36fd90a 2022-07-09 op buf[0] == 'a' && buf[1] == 'b' && buf[2] == '\0');
644 f36fd90a 2022-07-09 op #endif /* TEST_STRLCAT */
645 f36fd90a 2022-07-09 op #if TEST_STRLCPY
646 f36fd90a 2022-07-09 op #include <string.h>
651 f36fd90a 2022-07-09 op char buf[2] = "";
652 f36fd90a 2022-07-09 op return ! (strlcpy(buf, "a", sizeof(buf)) == 1 &&
653 f36fd90a 2022-07-09 op buf[0] == 'a' && buf[1] == '\0');
655 f36fd90a 2022-07-09 op #endif /* TEST_STRLCPY */
656 f36fd90a 2022-07-09 op #if TEST_STRNDUP
657 f36fd90a 2022-07-09 op #include <string.h>
662 f36fd90a 2022-07-09 op const char *foo = "bar";
665 f36fd90a 2022-07-09 op baz = strndup(foo, 1);
666 f36fd90a 2022-07-09 op return(0 != strcmp(baz, "b"));
668 f36fd90a 2022-07-09 op #endif /* TEST_STRNDUP */
669 f36fd90a 2022-07-09 op #if TEST_STRNLEN
670 f36fd90a 2022-07-09 op #include <string.h>
675 f36fd90a 2022-07-09 op const char *foo = "bar";
678 f36fd90a 2022-07-09 op sz = strnlen(foo, 1);
679 f36fd90a 2022-07-09 op return(1 != sz);
681 f36fd90a 2022-07-09 op #endif /* TEST_STRNLEN */
682 f36fd90a 2022-07-09 op #if TEST_STRTONUM
684 f36fd90a 2022-07-09 op * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
686 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
687 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
688 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
690 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
691 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
692 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
693 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
694 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
695 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
696 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
698 f36fd90a 2022-07-09 op #ifdef __NetBSD__
699 f36fd90a 2022-07-09 op # define _OPENBSD_SOURCE
701 f36fd90a 2022-07-09 op #include <stdlib.h>
706 f36fd90a 2022-07-09 op const char *errstr;
708 f36fd90a 2022-07-09 op if (strtonum("1", 0, 2, &errstr) != 1)
710 f36fd90a 2022-07-09 op if (errstr != NULL)
712 f36fd90a 2022-07-09 op if (strtonum("1x", 0, 2, &errstr) != 0)
714 f36fd90a 2022-07-09 op if (errstr == NULL)
716 f36fd90a 2022-07-09 op if (strtonum("2", 0, 1, &errstr) != 0)
718 f36fd90a 2022-07-09 op if (errstr == NULL)
720 f36fd90a 2022-07-09 op if (strtonum("0", 1, 2, &errstr) != 0)
722 f36fd90a 2022-07-09 op if (errstr == NULL)
726 f36fd90a 2022-07-09 op #endif /* TEST_STRTONUM */
727 f36fd90a 2022-07-09 op #if TEST_SYS_BYTEORDER_H
728 f36fd90a 2022-07-09 op #include <sys/byteorder.h>
733 f36fd90a 2022-07-09 op return !LE_32(23);
735 f36fd90a 2022-07-09 op #endif /* TEST_SYS_BYTEORDER_H */
736 f36fd90a 2022-07-09 op #if TEST_SYS_ENDIAN_H
737 f36fd90a 2022-07-09 op #include <sys/endian.h>
742 f36fd90a 2022-07-09 op return !htole32(23);
744 f36fd90a 2022-07-09 op #endif /* TEST_SYS_ENDIAN_H */
745 f36fd90a 2022-07-09 op #if TEST_SYS_MKDEV_H
746 f36fd90a 2022-07-09 op #include <sys/types.h>
747 f36fd90a 2022-07-09 op #include <sys/mkdev.h>
752 f36fd90a 2022-07-09 op return !minor(0);
754 f36fd90a 2022-07-09 op #endif /* TEST_SYS_MKDEV_H */
755 f36fd90a 2022-07-09 op #if TEST_SYS_FILE
756 f36fd90a 2022-07-09 op #include <sys/file.h>
761 f36fd90a 2022-07-09 op flock(0, LOCK_SH|LOCK_NB);
764 f36fd90a 2022-07-09 op #endif /* TEST_SYS_FILE */
765 f36fd90a 2022-07-09 op #if TEST_SYS_QUEUE
766 f36fd90a 2022-07-09 op #include <sys/queue.h>
767 f36fd90a 2022-07-09 op #include <stddef.h>
771 f36fd90a 2022-07-09 op TAILQ_ENTRY(foo) entries;
774 f36fd90a 2022-07-09 op TAILQ_HEAD(fooq, foo);
779 f36fd90a 2022-07-09 op struct fooq foo_q, bar_q;
780 f36fd90a 2022-07-09 op struct foo *p, *tmp;
783 f36fd90a 2022-07-09 op TAILQ_INIT(&foo_q);
784 f36fd90a 2022-07-09 op TAILQ_INIT(&bar_q);
787 f36fd90a 2022-07-09 op * Use TAILQ_FOREACH_SAFE because some systems (e.g., Linux)
788 f36fd90a 2022-07-09 op * have TAILQ_FOREACH but not the safe variant.
791 f36fd90a 2022-07-09 op TAILQ_FOREACH_SAFE(p, &foo_q, entries, tmp)
792 f36fd90a 2022-07-09 op p->bar = i++;
794 f36fd90a 2022-07-09 op /* Test for newer macros as well. */
796 f36fd90a 2022-07-09 op TAILQ_CONCAT(&foo_q, &bar_q, entries);
799 f36fd90a 2022-07-09 op #endif /* TEST_SYS_QUEUE */
800 f36fd90a 2022-07-09 op #if TEST_SYS_SYSMACROS_H
801 f36fd90a 2022-07-09 op #include <sys/sysmacros.h>
806 f36fd90a 2022-07-09 op return !minor(0);
808 f36fd90a 2022-07-09 op #endif /* TEST_SYS_SYSMACROS_H */
809 f36fd90a 2022-07-09 op #if TEST_SYS_TREE
810 f36fd90a 2022-07-09 op #include <sys/tree.h>
811 f36fd90a 2022-07-09 op #include <stdlib.h>
813 f36fd90a 2022-07-09 op struct node {
814 f36fd90a 2022-07-09 op RB_ENTRY(node) entry;
819 f36fd90a 2022-07-09 op intcmp(struct node *e1, struct node *e2)
821 f36fd90a 2022-07-09 op return (e1->i < e2->i ? -1 : e1->i > e2->i);
824 f36fd90a 2022-07-09 op RB_HEAD(inttree, node) head = RB_INITIALIZER(&head);
825 f36fd90a 2022-07-09 op RB_PROTOTYPE(inttree, node, entry, intcmp)
826 f36fd90a 2022-07-09 op RB_GENERATE(inttree, node, entry, intcmp)
828 f36fd90a 2022-07-09 op int testdata[] = {
829 f36fd90a 2022-07-09 op 20, 16, 17, 13, 3, 6, 1, 8, 2, 4
836 f36fd90a 2022-07-09 op struct node *n;
838 f36fd90a 2022-07-09 op for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) {
839 f36fd90a 2022-07-09 op if ((n = malloc(sizeof(struct node))) == NULL)
841 f36fd90a 2022-07-09 op n->i = testdata[i];
842 f36fd90a 2022-07-09 op RB_INSERT(inttree, &head, n);
848 f36fd90a 2022-07-09 op #endif /* TEST_SYS_TREE */
849 31e12e67 2023-09-07 op #if TEST_TIMESPECSUB
850 31e12e67 2023-09-07 op #include <sys/time.h>
855 31e12e67 2023-09-07 op struct timespec a = {0, 0}, b = {0, 0}, c;
857 31e12e67 2023-09-07 op timespecsub(&a, &b, &c);
858 31e12e67 2023-09-07 op return c.tv_sec;
860 31e12e67 2023-09-07 op #endif /* TEST_TIMESPECSUB */
861 f36fd90a 2022-07-09 op #if TEST_UNVEIL
862 f36fd90a 2022-07-09 op #include <unistd.h>
867 f36fd90a 2022-07-09 op return -1 != unveil(NULL, NULL);
869 f36fd90a 2022-07-09 op #endif /* TEST_UNVEIL */
870 f36fd90a 2022-07-09 op #if TEST_WAIT_ANY
871 f36fd90a 2022-07-09 op #include <sys/wait.h>
878 f36fd90a 2022-07-09 op return waitpid(WAIT_ANY, &st, WNOHANG) != -1;
880 f36fd90a 2022-07-09 op #endif /* TEST_WAIT_ANY */
881 f36fd90a 2022-07-09 op #if TEST_LIB_FLAC
882 f36fd90a 2022-07-09 op #include <FLAC/stream_decoder.h>
887 f36fd90a 2022-07-09 op FLAC__StreamDecoder *decoder = NULL;
888 f36fd90a 2022-07-09 op FLAC__StreamDecoderInitStatus init_status;
890 f36fd90a 2022-07-09 op decoder = FLAC__stream_decoder_new();
891 f36fd90a 2022-07-09 op FLAC__stream_decoder_delete(decoder);
894 f36fd90a 2022-07-09 op #endif /* TEST_LIB_FLAC */
895 f36fd90a 2022-07-09 op #if TEST_LIB_MPG123
896 f36fd90a 2022-07-09 op #include <mpg123.h>
901 f36fd90a 2022-07-09 op mpg123_handle *mh;
903 f36fd90a 2022-07-09 op mh = mpg123_new(NULL, NULL);
904 f36fd90a 2022-07-09 op mpg123_delete(mh);
907 f36fd90a 2022-07-09 op #endif/* TEST_LIB_MPG123 */
908 d1705aab 2023-10-07 op #if TEST_LIB_PTHREAD
909 d1705aab 2023-10-07 op #include <stdlib.h>
910 d1705aab 2023-10-07 op #include <pthread.h>
915 d1705aab 2023-10-07 op pthread_mutex_t mutex;
917 d1705aab 2023-10-07 op return (pthread_mutex_init(&mutex, NULL));
919 d1705aab 2023-10-07 op #endif/* TEST_LIB_PTHREAD */
920 f36fd90a 2022-07-09 op #if TEST_LIB_OPUSFILE
921 f36fd90a 2022-07-09 op #include <stdio.h>
922 f36fd90a 2022-07-09 op #include <opusfile.h>
928 f36fd90a 2022-07-09 op OggOpusFile *of;
929 f36fd90a 2022-07-09 op OpusFileCallbacks cb = {NULL, NULL, NULL, NULL};
932 f36fd90a 2022-07-09 op f = op_fdopen(&cb, 0, "r");
933 f36fd90a 2022-07-09 op of = op_open_callbacks(f, &cb, NULL, 0, &r);
937 f36fd90a 2022-07-09 op #endif /* TEST_LIB_OPUSFILE */
938 f36fd90a 2022-07-09 op #if TEST_LIB_VORBISFILE
939 f36fd90a 2022-07-09 op #include <stdio.h>
940 f36fd90a 2022-07-09 op #include <vorbis/vorbisfile.h>
945 f36fd90a 2022-07-09 op OggVorbis_File vf;
947 f36fd90a 2022-07-09 op ov_open_callbacks(stdin, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE);
948 f36fd90a 2022-07-09 op ov_clear(&vf);
951 f36fd90a 2022-07-09 op #endif /* TEST_LIB_VORBISFILE */
952 d1705aab 2023-10-07 op #if TEST_LIB_AO
953 d1705aab 2023-10-07 op #include <stdio.h>
954 d1705aab 2023-10-07 op #include <string.h>
955 d1705aab 2023-10-07 op #include <ao/ao.h>
960 d1705aab 2023-10-07 op ao_initialize();
963 d1705aab 2023-10-07 op #endif /* TEST_LIB_AO */
964 34674849 2023-03-23 op #if TEST_LIB_ASOUND
965 34674849 2023-03-23 op #include <alsa/asoundlib.h>
970 34674849 2023-03-23 op snd_pcm_t *pcm;
973 34674849 2023-03-23 op err = snd_pcm_open(&pcm, "default", SND_PCM_STREAM_PLAYBACK,
974 34674849 2023-03-23 op SND_PCM_NONBLOCK);
977 34674849 2023-03-23 op #endif /* TEST_LIB_ASOUND */
978 f36fd90a 2022-07-09 op #if TEST_LIB_SNDIO
979 f36fd90a 2022-07-09 op #include <sndio.h>
984 f36fd90a 2022-07-09 op struct sio_hdl *hdl;
985 f36fd90a 2022-07-09 op hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1);
986 f36fd90a 2022-07-09 op sio_close(hdl);
989 f36fd90a 2022-07-09 op #endif /* TEST_LIB_SNDIO */
990 56de3111 2023-09-07 op #if TEST_LIB_MD
991 56de3111 2023-09-07 op #include <sys/types.h>
992 56de3111 2023-09-07 op #include <sha1.h>
997 56de3111 2023-09-07 op SHA1_CTX ctx;
998 56de3111 2023-09-07 op char result[SHA1_DIGEST_STRING_LENGTH];
1000 56de3111 2023-09-07 op SHA1Init(&ctx);
1001 56de3111 2023-09-07 op SHA1Update(&ctx, (const unsigned char *)"abcd", 4);
1002 56de3111 2023-09-07 op SHA1End(&ctx, result);
1006 56de3111 2023-09-07 op #endif /* TEST_LIB_MD */