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 119ab664 2024-01-21 op struct imsgbuf imsgbuf;
257 119ab664 2024-01-21 op struct imsg imsg;
259 119ab664 2024-01-21 op imsg_init(&imsgbuf, 3);
260 119ab664 2024-01-21 op imsg_clear(&imsgbuf);
261 119ab664 2024-01-21 op imsg_get_fd(&imsg);
264 82556d5b 2023-09-07 op #endif /* TEST_LIB_IMSG */
265 f36fd90a 2022-07-09 op #if TEST_INFTIM
267 f36fd90a 2022-07-09 op * Linux doesn't (always?) have this.
270 f36fd90a 2022-07-09 op #include <poll.h>
271 f36fd90a 2022-07-09 op #include <stdio.h>
276 f36fd90a 2022-07-09 op printf("INFTIM is defined to be %ld\n", (long)INFTIM);
279 f36fd90a 2022-07-09 op #endif /* TEST_INFTIM */
280 f36fd90a 2022-07-09 op #if TEST_LANDLOCK
281 f36fd90a 2022-07-09 op #include <linux/landlock.h>
282 f36fd90a 2022-07-09 op #include <stdlib.h>
283 f36fd90a 2022-07-09 op #include <sys/prctl.h>
284 f36fd90a 2022-07-09 op #include <sys/syscall.h>
285 f36fd90a 2022-07-09 op #include <unistd.h>
286 f36fd90a 2022-07-09 op #include <stdint.h>
288 f36fd90a 2022-07-09 op #ifndef landlock_create_ruleset
289 f36fd90a 2022-07-09 op static inline int landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,
290 f36fd90a 2022-07-09 op const size_t size, const __u32 flags)
292 f36fd90a 2022-07-09 op return syscall(__NR_landlock_create_ruleset, attr, size, flags);
296 f36fd90a 2022-07-09 op #ifndef landlock_restrict_self
297 f36fd90a 2022-07-09 op static inline int landlock_restrict_self(const int ruleset_fd,
298 f36fd90a 2022-07-09 op const __u32 flags)
300 f36fd90a 2022-07-09 op return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
307 f36fd90a 2022-07-09 op uint64_t mask = LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE;
308 f36fd90a 2022-07-09 op struct landlock_ruleset_attr rules = {
309 f36fd90a 2022-07-09 op .handled_access_fs = mask
311 f36fd90a 2022-07-09 op int fd = landlock_create_ruleset(&rules, sizeof(rules), 0);
313 f36fd90a 2022-07-09 op if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
315 f36fd90a 2022-07-09 op return landlock_restrict_self(fd, 0) ? 1 : 0;
317 f36fd90a 2022-07-09 op #endif /* TEST_LANDLOCK */
318 f36fd90a 2022-07-09 op #if TEST_LIB_SOCKET
319 f36fd90a 2022-07-09 op #include <sys/socket.h>
324 f36fd90a 2022-07-09 op int fds[2], c;
326 f36fd90a 2022-07-09 op c = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
327 f36fd90a 2022-07-09 op return c == -1;
329 f36fd90a 2022-07-09 op #endif /* TEST_LIB_SOCKET */
331 f36fd90a 2022-07-09 op #include <sys/types.h>
332 f36fd90a 2022-07-09 op #include <md5.h>
334 f36fd90a 2022-07-09 op int main(void)
337 f36fd90a 2022-07-09 op char result[MD5_DIGEST_STRING_LENGTH];
339 f36fd90a 2022-07-09 op MD5Init(&ctx);
340 f36fd90a 2022-07-09 op MD5Update(&ctx, (const unsigned char *)"abcd", 4);
341 f36fd90a 2022-07-09 op MD5End(&ctx, result);
345 f36fd90a 2022-07-09 op #endif /* TEST_MD5 */
346 f36fd90a 2022-07-09 op #if TEST_MEMMEM
347 f36fd90a 2022-07-09 op #define _GNU_SOURCE
348 f36fd90a 2022-07-09 op #include <string.h>
353 f36fd90a 2022-07-09 op char *a = memmem("hello, world", strlen("hello, world"), "world", strlen("world"));
354 f36fd90a 2022-07-09 op return(NULL == a);
356 f36fd90a 2022-07-09 op #endif /* TEST_MEMMEM */
357 f36fd90a 2022-07-09 op #if TEST_MEMRCHR
358 f36fd90a 2022-07-09 op #if defined(__linux__) || defined(__MINT__)
359 f36fd90a 2022-07-09 op #define _GNU_SOURCE /* See test-*.c what needs this. */
361 f36fd90a 2022-07-09 op #include <string.h>
366 f36fd90a 2022-07-09 op const char *buf = "abcdef";
369 f36fd90a 2022-07-09 op res = memrchr(buf, 'a', strlen(buf));
370 f36fd90a 2022-07-09 op return(NULL == res ? 1 : 0);
372 f36fd90a 2022-07-09 op #endif /* TEST_MEMRCHR */
373 f36fd90a 2022-07-09 op #if TEST_MEMSET_S
374 f36fd90a 2022-07-09 op #include <string.h>
376 f36fd90a 2022-07-09 op int main(void)
378 f36fd90a 2022-07-09 op char buf[10];
379 f36fd90a 2022-07-09 op memset_s(buf, 0, 'c', sizeof(buf));
382 f36fd90a 2022-07-09 op #endif /* TEST_MEMSET_S */
383 f36fd90a 2022-07-09 op #if TEST_MKFIFOAT
384 f36fd90a 2022-07-09 op #include <sys/stat.h>
385 f36fd90a 2022-07-09 op #include <fcntl.h>
387 f36fd90a 2022-07-09 op int main(void) {
388 f36fd90a 2022-07-09 op mkfifoat(AT_FDCWD, "this/path/should/not/exist", 0600);
391 f36fd90a 2022-07-09 op #endif /* TEST_MKFIFOAT */
392 f36fd90a 2022-07-09 op #if TEST_MKNODAT
393 f36fd90a 2022-07-09 op #include <sys/stat.h>
394 f36fd90a 2022-07-09 op #include <fcntl.h>
396 f36fd90a 2022-07-09 op int main(void) {
397 f36fd90a 2022-07-09 op mknodat(AT_FDCWD, "this/path/should/not/exist", S_IFIFO | 0600, 0);
400 f36fd90a 2022-07-09 op #endif /* TEST_MKNODAT */
401 f36fd90a 2022-07-09 op #if TEST_OPTRESET
402 f36fd90a 2022-07-09 op #include <unistd.h>
405 f36fd90a 2022-07-09 op main(int argc, char **argv)
407 f36fd90a 2022-07-09 op optreset = 1;
408 f36fd90a 2022-07-09 op getopt(argc, argv, "");
411 f36fd90a 2022-07-09 op #endif /* TEST_OPTRESET */
412 f36fd90a 2022-07-09 op #if TEST_OSBYTEORDER_H
413 f36fd90a 2022-07-09 op #include <libkern/OSByteOrder.h>
418 f36fd90a 2022-07-09 op return !OSSwapHostToLittleInt32(23);
420 f36fd90a 2022-07-09 op #endif /* TEST_OSBYTEORDER_H */
421 f36fd90a 2022-07-09 op #if TEST_PATH_MAX
423 f36fd90a 2022-07-09 op * POSIX allows PATH_MAX to not be defined, see
424 f36fd90a 2022-07-09 op * http://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html;
425 f36fd90a 2022-07-09 op * the GNU Hurd is an example of a system not having it.
427 f36fd90a 2022-07-09 op * Arguably, it would be better to test sysconf(_SC_PATH_MAX),
428 f36fd90a 2022-07-09 op * but since the individual *.c files include "config.h" before
429 f36fd90a 2022-07-09 op * <limits.h>, overriding an excessive value of PATH_MAX from
430 f36fd90a 2022-07-09 op * "config.h" is impossible anyway, so for now, the simplest
431 f36fd90a 2022-07-09 op * fix is to provide a value only on systems not having any.
432 f36fd90a 2022-07-09 op * So far, we encountered no system defining PATH_MAX to an
433 f36fd90a 2022-07-09 op * impractically large value, even though POSIX explicitly
434 f36fd90a 2022-07-09 op * allows that.
436 f36fd90a 2022-07-09 op * The real fix would be to replace all static buffers of size
437 f36fd90a 2022-07-09 op * PATH_MAX by dynamically allocated buffers. But that is
438 f36fd90a 2022-07-09 op * somewhat intrusive because it touches several files and
439 f36fd90a 2022-07-09 op * because it requires changing struct mlink in mandocdb.c.
440 f36fd90a 2022-07-09 op * So i'm postponing that for now.
443 f36fd90a 2022-07-09 op #include <limits.h>
444 f36fd90a 2022-07-09 op #include <stdio.h>
449 f36fd90a 2022-07-09 op printf("PATH_MAX is defined to be %ld\n", (long)PATH_MAX);
452 f36fd90a 2022-07-09 op #endif /* TEST_PATH_MAX */
453 f36fd90a 2022-07-09 op #if TEST_PLEDGE
454 f36fd90a 2022-07-09 op #include <unistd.h>
459 f36fd90a 2022-07-09 op return !!pledge("stdio", NULL);
461 f36fd90a 2022-07-09 op #endif /* TEST_PLEDGE */
462 f36fd90a 2022-07-09 op #if TEST_PROGRAM_INVOCATION_SHORT_NAME
463 f36fd90a 2022-07-09 op #define _GNU_SOURCE /* See feature_test_macros(7) */
464 f36fd90a 2022-07-09 op #include <errno.h>
470 f36fd90a 2022-07-09 op return !program_invocation_short_name;
472 f36fd90a 2022-07-09 op #endif /* TEST_PROGRAM_INVOCATION_SHORT_NAME */
473 f36fd90a 2022-07-09 op #if TEST_PR_SET_NAME
474 f36fd90a 2022-07-09 op #include <sys/prctl.h>
479 f36fd90a 2022-07-09 op prctl(PR_SET_NAME, "foo");
482 f36fd90a 2022-07-09 op #endif /* TEST_PR_SET_NAME */
483 f36fd90a 2022-07-09 op #if TEST_READPASSPHRASE
484 f36fd90a 2022-07-09 op #include <stddef.h>
485 f36fd90a 2022-07-09 op #include <readpassphrase.h>
490 f36fd90a 2022-07-09 op return !!readpassphrase("prompt: ", NULL, 0, 0);
492 f36fd90a 2022-07-09 op #endif /* TEST_READPASSPHRASE */
493 f36fd90a 2022-07-09 op #if TEST_REALLOCARRAY
494 f36fd90a 2022-07-09 op #ifdef __NetBSD__
495 f36fd90a 2022-07-09 op # define _OPENBSD_SOURCE
497 f36fd90a 2022-07-09 op #include <stdlib.h>
502 f36fd90a 2022-07-09 op return !reallocarray(NULL, 2, 2);
504 f36fd90a 2022-07-09 op #endif /* TEST_REALLOCARRAY */
505 f36fd90a 2022-07-09 op #if TEST_RECALLOCARRAY
506 f36fd90a 2022-07-09 op #include <stdlib.h>
511 f36fd90a 2022-07-09 op return !recallocarray(NULL, 0, 2, 2);
513 f36fd90a 2022-07-09 op #endif /* TEST_RECALLOCARRAY */
514 f36fd90a 2022-07-09 op #if TEST_SANDBOX_INIT
515 f36fd90a 2022-07-09 op #include <sandbox.h>
523 f36fd90a 2022-07-09 op rc = sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, &ep);
524 f36fd90a 2022-07-09 op if (-1 == rc)
525 f36fd90a 2022-07-09 op sandbox_free_error(ep);
526 f36fd90a 2022-07-09 op return(-1 == rc);
528 f36fd90a 2022-07-09 op #endif /* TEST_SANDBOX_INIT */
529 f36fd90a 2022-07-09 op #if TEST_SETPROCTITLE
530 f36fd90a 2022-07-09 op #include <stdlib.h>
535 f36fd90a 2022-07-09 op setproctitle("#%d test program", 7);
539 f36fd90a 2022-07-09 op #if TEST_SIO_FLUSH
540 f36fd90a 2022-07-09 op #include <sndio.h>
545 f36fd90a 2022-07-09 op struct sio_hdl *hdl;
546 f36fd90a 2022-07-09 op hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1);
547 f36fd90a 2022-07-09 op sio_flush(hdl);
548 f36fd90a 2022-07-09 op sio_close(hdl);
551 f36fd90a 2022-07-09 op #endif /* TEST_SIO_FLUSH */
552 f36fd90a 2022-07-09 op #if TEST_SCAN_SCALED
553 f36fd90a 2022-07-09 op #include <util.h>
558 f36fd90a 2022-07-09 op char *cinput = (char *)"1.5K", buf[FMT_SCALED_STRSIZE];
559 f36fd90a 2022-07-09 op long long ninput = 10483892, result;
560 f36fd90a 2022-07-09 op return scan_scaled(cinput, &result) == 0;
562 f36fd90a 2022-07-09 op #endif /* TEST_SCAN_SCALED */
563 f36fd90a 2022-07-09 op #if TEST_SECCOMP_FILTER
564 f36fd90a 2022-07-09 op #include <sys/prctl.h>
565 f36fd90a 2022-07-09 op #include <linux/seccomp.h>
566 f36fd90a 2022-07-09 op #include <errno.h>
572 f36fd90a 2022-07-09 op prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, 0);
573 f36fd90a 2022-07-09 op return(EFAULT == errno ? 0 : 1);
575 f36fd90a 2022-07-09 op #endif /* TEST_SECCOMP_FILTER */
576 f36fd90a 2022-07-09 op #if TEST_SETRESGID
577 f36fd90a 2022-07-09 op #define _GNU_SOURCE /* linux */
578 f36fd90a 2022-07-09 op #include <sys/types.h>
579 f36fd90a 2022-07-09 op #include <unistd.h>
584 f36fd90a 2022-07-09 op return setresgid(-1, -1, -1) == -1;
586 f36fd90a 2022-07-09 op #endif /* TEST_SETRESGID */
587 f36fd90a 2022-07-09 op #if TEST_SETRESUID
588 f36fd90a 2022-07-09 op #define _GNU_SOURCE /* linux */
589 f36fd90a 2022-07-09 op #include <sys/types.h>
590 f36fd90a 2022-07-09 op #include <unistd.h>
595 f36fd90a 2022-07-09 op return setresuid(-1, -1, -1) == -1;
597 f36fd90a 2022-07-09 op #endif /* TEST_SETRESUID */
598 f36fd90a 2022-07-09 op #if TEST_SHA2
599 f36fd90a 2022-07-09 op #include <sys/types.h>
600 f36fd90a 2022-07-09 op #include <sha2.h>
602 f36fd90a 2022-07-09 op int main(void)
604 f36fd90a 2022-07-09 op SHA2_CTX ctx;
605 f36fd90a 2022-07-09 op char result[SHA256_DIGEST_STRING_LENGTH];
607 f36fd90a 2022-07-09 op SHA256Init(&ctx);
608 f36fd90a 2022-07-09 op SHA256Update(&ctx, (const unsigned char *)"abcd", 4);
609 f36fd90a 2022-07-09 op SHA256End(&ctx, result);
613 f36fd90a 2022-07-09 op #endif /* TEST_SHA2 */
614 f36fd90a 2022-07-09 op #if TEST_SOCK_NONBLOCK
616 f36fd90a 2022-07-09 op * Linux doesn't (always?) have this.
619 f36fd90a 2022-07-09 op #include <sys/socket.h>
625 f36fd90a 2022-07-09 op socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fd);
628 f36fd90a 2022-07-09 op #endif /* TEST_SOCK_NONBLOCK */
629 f36fd90a 2022-07-09 op #if TEST_STATIC
633 f36fd90a 2022-07-09 op return 0; /* not meant to do anything */
635 f36fd90a 2022-07-09 op #endif /* TEST_STATIC */
636 f36fd90a 2022-07-09 op #if TEST_STRLCAT
637 f36fd90a 2022-07-09 op #include <string.h>
642 f36fd90a 2022-07-09 op char buf[3] = "a";
643 f36fd90a 2022-07-09 op return ! (strlcat(buf, "b", sizeof(buf)) == 2 &&
644 f36fd90a 2022-07-09 op buf[0] == 'a' && buf[1] == 'b' && buf[2] == '\0');
646 f36fd90a 2022-07-09 op #endif /* TEST_STRLCAT */
647 f36fd90a 2022-07-09 op #if TEST_STRLCPY
648 f36fd90a 2022-07-09 op #include <string.h>
653 f36fd90a 2022-07-09 op char buf[2] = "";
654 f36fd90a 2022-07-09 op return ! (strlcpy(buf, "a", sizeof(buf)) == 1 &&
655 f36fd90a 2022-07-09 op buf[0] == 'a' && buf[1] == '\0');
657 f36fd90a 2022-07-09 op #endif /* TEST_STRLCPY */
658 f36fd90a 2022-07-09 op #if TEST_STRNDUP
659 f36fd90a 2022-07-09 op #include <string.h>
664 f36fd90a 2022-07-09 op const char *foo = "bar";
667 f36fd90a 2022-07-09 op baz = strndup(foo, 1);
668 f36fd90a 2022-07-09 op return(0 != strcmp(baz, "b"));
670 f36fd90a 2022-07-09 op #endif /* TEST_STRNDUP */
671 f36fd90a 2022-07-09 op #if TEST_STRNLEN
672 f36fd90a 2022-07-09 op #include <string.h>
677 f36fd90a 2022-07-09 op const char *foo = "bar";
680 f36fd90a 2022-07-09 op sz = strnlen(foo, 1);
681 f36fd90a 2022-07-09 op return(1 != sz);
683 f36fd90a 2022-07-09 op #endif /* TEST_STRNLEN */
684 f36fd90a 2022-07-09 op #if TEST_STRTONUM
686 f36fd90a 2022-07-09 op * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
688 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
689 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
690 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
692 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
693 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
694 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
695 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
696 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
697 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
698 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
700 f36fd90a 2022-07-09 op #ifdef __NetBSD__
701 f36fd90a 2022-07-09 op # define _OPENBSD_SOURCE
703 f36fd90a 2022-07-09 op #include <stdlib.h>
708 f36fd90a 2022-07-09 op const char *errstr;
710 f36fd90a 2022-07-09 op if (strtonum("1", 0, 2, &errstr) != 1)
712 f36fd90a 2022-07-09 op if (errstr != NULL)
714 f36fd90a 2022-07-09 op if (strtonum("1x", 0, 2, &errstr) != 0)
716 f36fd90a 2022-07-09 op if (errstr == NULL)
718 f36fd90a 2022-07-09 op if (strtonum("2", 0, 1, &errstr) != 0)
720 f36fd90a 2022-07-09 op if (errstr == NULL)
722 f36fd90a 2022-07-09 op if (strtonum("0", 1, 2, &errstr) != 0)
724 f36fd90a 2022-07-09 op if (errstr == NULL)
728 f36fd90a 2022-07-09 op #endif /* TEST_STRTONUM */
729 f36fd90a 2022-07-09 op #if TEST_SYS_BYTEORDER_H
730 f36fd90a 2022-07-09 op #include <sys/byteorder.h>
735 f36fd90a 2022-07-09 op return !LE_32(23);
737 f36fd90a 2022-07-09 op #endif /* TEST_SYS_BYTEORDER_H */
738 f36fd90a 2022-07-09 op #if TEST_SYS_ENDIAN_H
739 f36fd90a 2022-07-09 op #include <sys/endian.h>
744 f36fd90a 2022-07-09 op return !htole32(23);
746 f36fd90a 2022-07-09 op #endif /* TEST_SYS_ENDIAN_H */
747 f36fd90a 2022-07-09 op #if TEST_SYS_MKDEV_H
748 f36fd90a 2022-07-09 op #include <sys/types.h>
749 f36fd90a 2022-07-09 op #include <sys/mkdev.h>
754 f36fd90a 2022-07-09 op return !minor(0);
756 f36fd90a 2022-07-09 op #endif /* TEST_SYS_MKDEV_H */
757 f36fd90a 2022-07-09 op #if TEST_SYS_FILE
758 f36fd90a 2022-07-09 op #include <sys/file.h>
763 f36fd90a 2022-07-09 op flock(0, LOCK_SH|LOCK_NB);
766 f36fd90a 2022-07-09 op #endif /* TEST_SYS_FILE */
767 f36fd90a 2022-07-09 op #if TEST_SYS_QUEUE
768 f36fd90a 2022-07-09 op #include <sys/queue.h>
769 f36fd90a 2022-07-09 op #include <stddef.h>
773 f36fd90a 2022-07-09 op TAILQ_ENTRY(foo) entries;
776 f36fd90a 2022-07-09 op TAILQ_HEAD(fooq, foo);
781 f36fd90a 2022-07-09 op struct fooq foo_q, bar_q;
782 f36fd90a 2022-07-09 op struct foo *p, *tmp;
785 f36fd90a 2022-07-09 op TAILQ_INIT(&foo_q);
786 f36fd90a 2022-07-09 op TAILQ_INIT(&bar_q);
789 f36fd90a 2022-07-09 op * Use TAILQ_FOREACH_SAFE because some systems (e.g., Linux)
790 f36fd90a 2022-07-09 op * have TAILQ_FOREACH but not the safe variant.
793 f36fd90a 2022-07-09 op TAILQ_FOREACH_SAFE(p, &foo_q, entries, tmp)
794 f36fd90a 2022-07-09 op p->bar = i++;
796 f36fd90a 2022-07-09 op /* Test for newer macros as well. */
798 f36fd90a 2022-07-09 op TAILQ_CONCAT(&foo_q, &bar_q, entries);
801 f36fd90a 2022-07-09 op #endif /* TEST_SYS_QUEUE */
802 f36fd90a 2022-07-09 op #if TEST_SYS_SYSMACROS_H
803 f36fd90a 2022-07-09 op #include <sys/sysmacros.h>
808 f36fd90a 2022-07-09 op return !minor(0);
810 f36fd90a 2022-07-09 op #endif /* TEST_SYS_SYSMACROS_H */
811 f36fd90a 2022-07-09 op #if TEST_SYS_TREE
812 f36fd90a 2022-07-09 op #include <sys/tree.h>
813 f36fd90a 2022-07-09 op #include <stdlib.h>
815 f36fd90a 2022-07-09 op struct node {
816 f36fd90a 2022-07-09 op RB_ENTRY(node) entry;
821 f36fd90a 2022-07-09 op intcmp(struct node *e1, struct node *e2)
823 f36fd90a 2022-07-09 op return (e1->i < e2->i ? -1 : e1->i > e2->i);
826 f36fd90a 2022-07-09 op RB_HEAD(inttree, node) head = RB_INITIALIZER(&head);
827 f36fd90a 2022-07-09 op RB_PROTOTYPE(inttree, node, entry, intcmp)
828 f36fd90a 2022-07-09 op RB_GENERATE(inttree, node, entry, intcmp)
830 f36fd90a 2022-07-09 op int testdata[] = {
831 f36fd90a 2022-07-09 op 20, 16, 17, 13, 3, 6, 1, 8, 2, 4
838 f36fd90a 2022-07-09 op struct node *n;
840 f36fd90a 2022-07-09 op for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) {
841 f36fd90a 2022-07-09 op if ((n = malloc(sizeof(struct node))) == NULL)
843 f36fd90a 2022-07-09 op n->i = testdata[i];
844 f36fd90a 2022-07-09 op RB_INSERT(inttree, &head, n);
850 f36fd90a 2022-07-09 op #endif /* TEST_SYS_TREE */
851 31e12e67 2023-09-07 op #if TEST_TIMESPECSUB
852 31e12e67 2023-09-07 op #include <sys/time.h>
857 31e12e67 2023-09-07 op struct timespec a = {0, 0}, b = {0, 0}, c;
859 31e12e67 2023-09-07 op timespecsub(&a, &b, &c);
860 31e12e67 2023-09-07 op return c.tv_sec;
862 31e12e67 2023-09-07 op #endif /* TEST_TIMESPECSUB */
863 f36fd90a 2022-07-09 op #if TEST_UNVEIL
864 f36fd90a 2022-07-09 op #include <unistd.h>
869 f36fd90a 2022-07-09 op return -1 != unveil(NULL, NULL);
871 f36fd90a 2022-07-09 op #endif /* TEST_UNVEIL */
872 f36fd90a 2022-07-09 op #if TEST_WAIT_ANY
873 f36fd90a 2022-07-09 op #include <sys/wait.h>
880 f36fd90a 2022-07-09 op return waitpid(WAIT_ANY, &st, WNOHANG) != -1;
882 f36fd90a 2022-07-09 op #endif /* TEST_WAIT_ANY */
883 f36fd90a 2022-07-09 op #if TEST_LIB_FLAC
884 f36fd90a 2022-07-09 op #include <FLAC/stream_decoder.h>
889 f36fd90a 2022-07-09 op FLAC__StreamDecoder *decoder = NULL;
890 f36fd90a 2022-07-09 op FLAC__StreamDecoderInitStatus init_status;
892 f36fd90a 2022-07-09 op decoder = FLAC__stream_decoder_new();
893 f36fd90a 2022-07-09 op FLAC__stream_decoder_delete(decoder);
896 f36fd90a 2022-07-09 op #endif /* TEST_LIB_FLAC */
897 f36fd90a 2022-07-09 op #if TEST_LIB_MPG123
898 f36fd90a 2022-07-09 op #include <mpg123.h>
903 f36fd90a 2022-07-09 op mpg123_handle *mh;
905 f36fd90a 2022-07-09 op mh = mpg123_new(NULL, NULL);
906 f36fd90a 2022-07-09 op mpg123_delete(mh);
909 f36fd90a 2022-07-09 op #endif/* TEST_LIB_MPG123 */
910 d1705aab 2023-10-07 op #if TEST_LIB_PTHREAD
911 d1705aab 2023-10-07 op #include <stdlib.h>
912 d1705aab 2023-10-07 op #include <pthread.h>
917 d1705aab 2023-10-07 op pthread_mutex_t mutex;
919 d1705aab 2023-10-07 op return (pthread_mutex_init(&mutex, NULL));
921 d1705aab 2023-10-07 op #endif/* TEST_LIB_PTHREAD */
922 f36fd90a 2022-07-09 op #if TEST_LIB_OPUSFILE
923 f36fd90a 2022-07-09 op #include <stdio.h>
924 f36fd90a 2022-07-09 op #include <opusfile.h>
930 f36fd90a 2022-07-09 op OggOpusFile *of;
931 f36fd90a 2022-07-09 op OpusFileCallbacks cb = {NULL, NULL, NULL, NULL};
934 f36fd90a 2022-07-09 op f = op_fdopen(&cb, 0, "r");
935 f36fd90a 2022-07-09 op of = op_open_callbacks(f, &cb, NULL, 0, &r);
939 f36fd90a 2022-07-09 op #endif /* TEST_LIB_OPUSFILE */
940 f36fd90a 2022-07-09 op #if TEST_LIB_VORBISFILE
941 f36fd90a 2022-07-09 op #include <stdio.h>
942 f36fd90a 2022-07-09 op #include <vorbis/vorbisfile.h>
947 f36fd90a 2022-07-09 op OggVorbis_File vf;
949 f36fd90a 2022-07-09 op ov_open_callbacks(stdin, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE);
950 f36fd90a 2022-07-09 op ov_clear(&vf);
953 f36fd90a 2022-07-09 op #endif /* TEST_LIB_VORBISFILE */
954 d1705aab 2023-10-07 op #if TEST_LIB_AO
955 d1705aab 2023-10-07 op #include <stdio.h>
956 d1705aab 2023-10-07 op #include <string.h>
957 d1705aab 2023-10-07 op #include <ao/ao.h>
962 d1705aab 2023-10-07 op ao_initialize();
965 d1705aab 2023-10-07 op #endif /* TEST_LIB_AO */
966 34674849 2023-03-23 op #if TEST_LIB_ASOUND
967 34674849 2023-03-23 op #include <alsa/asoundlib.h>
972 34674849 2023-03-23 op snd_pcm_t *pcm;
975 34674849 2023-03-23 op err = snd_pcm_open(&pcm, "default", SND_PCM_STREAM_PLAYBACK,
976 34674849 2023-03-23 op SND_PCM_NONBLOCK);
979 34674849 2023-03-23 op #endif /* TEST_LIB_ASOUND */
980 f36fd90a 2022-07-09 op #if TEST_LIB_SNDIO
981 f36fd90a 2022-07-09 op #include <sndio.h>
986 f36fd90a 2022-07-09 op struct sio_hdl *hdl;
987 f36fd90a 2022-07-09 op hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1);
988 f36fd90a 2022-07-09 op sio_close(hdl);
991 f36fd90a 2022-07-09 op #endif /* TEST_LIB_SNDIO */
992 56de3111 2023-09-07 op #if TEST_LIB_MD
993 56de3111 2023-09-07 op #include <sys/types.h>
994 56de3111 2023-09-07 op #include <sha1.h>
999 56de3111 2023-09-07 op SHA1_CTX ctx;
1000 56de3111 2023-09-07 op char result[SHA1_DIGEST_STRING_LENGTH];
1002 56de3111 2023-09-07 op SHA1Init(&ctx);
1003 56de3111 2023-09-07 op SHA1Update(&ctx, (const unsigned char *)"abcd", 4);
1004 56de3111 2023-09-07 op SHA1End(&ctx, result);
1008 56de3111 2023-09-07 op #endif /* TEST_LIB_MD */