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 f36fd90a 2022-07-09 op #if TEST_GETEXECNAME
214 f36fd90a 2022-07-09 op #include <stdlib.h>
219 f36fd90a 2022-07-09 op const char * progname;
221 f36fd90a 2022-07-09 op progname = getexecname();
222 f36fd90a 2022-07-09 op return progname == NULL;
224 f36fd90a 2022-07-09 op #endif /* TEST_GETEXECNAME */
225 f36fd90a 2022-07-09 op #if TEST_GETPROGNAME
226 f36fd90a 2022-07-09 op #include <stdlib.h>
231 f36fd90a 2022-07-09 op const char * progname;
233 f36fd90a 2022-07-09 op progname = getprogname();
234 f36fd90a 2022-07-09 op return progname == NULL;
236 f36fd90a 2022-07-09 op #endif /* TEST_GETPROGNAME */
237 82556d5b 2023-09-07 op #if TEST_LIB_IMSG
238 f36fd90a 2022-07-09 op #include <sys/types.h>
239 f36fd90a 2022-07-09 op #include <sys/queue.h>
240 f36fd90a 2022-07-09 op #include <sys/uio.h>
241 f36fd90a 2022-07-09 op #include <stdint.h>
242 f36fd90a 2022-07-09 op #include <imsg.h>
247 f36fd90a 2022-07-09 op struct imsgbuf ibuf;
249 f36fd90a 2022-07-09 op imsg_init(&ibuf, 3);
250 f36fd90a 2022-07-09 op imsg_clear(&ibuf);
253 82556d5b 2023-09-07 op #endif /* TEST_LIB_IMSG */
254 f36fd90a 2022-07-09 op #if TEST_INFTIM
256 f36fd90a 2022-07-09 op * Linux doesn't (always?) have this.
259 f36fd90a 2022-07-09 op #include <poll.h>
260 f36fd90a 2022-07-09 op #include <stdio.h>
265 f36fd90a 2022-07-09 op printf("INFTIM is defined to be %ld\n", (long)INFTIM);
268 f36fd90a 2022-07-09 op #endif /* TEST_INFTIM */
269 f36fd90a 2022-07-09 op #if TEST_LANDLOCK
270 f36fd90a 2022-07-09 op #include <linux/landlock.h>
271 f36fd90a 2022-07-09 op #include <stdlib.h>
272 f36fd90a 2022-07-09 op #include <sys/prctl.h>
273 f36fd90a 2022-07-09 op #include <sys/syscall.h>
274 f36fd90a 2022-07-09 op #include <unistd.h>
275 f36fd90a 2022-07-09 op #include <stdint.h>
277 f36fd90a 2022-07-09 op #ifndef landlock_create_ruleset
278 f36fd90a 2022-07-09 op static inline int landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,
279 f36fd90a 2022-07-09 op const size_t size, const __u32 flags)
281 f36fd90a 2022-07-09 op return syscall(__NR_landlock_create_ruleset, attr, size, flags);
285 f36fd90a 2022-07-09 op #ifndef landlock_restrict_self
286 f36fd90a 2022-07-09 op static inline int landlock_restrict_self(const int ruleset_fd,
287 f36fd90a 2022-07-09 op const __u32 flags)
289 f36fd90a 2022-07-09 op return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
296 f36fd90a 2022-07-09 op uint64_t mask = LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE;
297 f36fd90a 2022-07-09 op struct landlock_ruleset_attr rules = {
298 f36fd90a 2022-07-09 op .handled_access_fs = mask
300 f36fd90a 2022-07-09 op int fd = landlock_create_ruleset(&rules, sizeof(rules), 0);
302 f36fd90a 2022-07-09 op if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
304 f36fd90a 2022-07-09 op return landlock_restrict_self(fd, 0) ? 1 : 0;
306 f36fd90a 2022-07-09 op #endif /* TEST_LANDLOCK */
307 f36fd90a 2022-07-09 op #if TEST_LIB_SOCKET
308 f36fd90a 2022-07-09 op #include <sys/socket.h>
313 f36fd90a 2022-07-09 op int fds[2], c;
315 f36fd90a 2022-07-09 op c = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
316 f36fd90a 2022-07-09 op return c == -1;
318 f36fd90a 2022-07-09 op #endif /* TEST_LIB_SOCKET */
320 f36fd90a 2022-07-09 op #include <sys/types.h>
321 f36fd90a 2022-07-09 op #include <md5.h>
323 f36fd90a 2022-07-09 op int main(void)
326 f36fd90a 2022-07-09 op char result[MD5_DIGEST_STRING_LENGTH];
328 f36fd90a 2022-07-09 op MD5Init(&ctx);
329 f36fd90a 2022-07-09 op MD5Update(&ctx, (const unsigned char *)"abcd", 4);
330 f36fd90a 2022-07-09 op MD5End(&ctx, result);
334 f36fd90a 2022-07-09 op #endif /* TEST_MD5 */
335 f36fd90a 2022-07-09 op #if TEST_MEMMEM
336 f36fd90a 2022-07-09 op #define _GNU_SOURCE
337 f36fd90a 2022-07-09 op #include <string.h>
342 f36fd90a 2022-07-09 op char *a = memmem("hello, world", strlen("hello, world"), "world", strlen("world"));
343 f36fd90a 2022-07-09 op return(NULL == a);
345 f36fd90a 2022-07-09 op #endif /* TEST_MEMMEM */
346 f36fd90a 2022-07-09 op #if TEST_MEMRCHR
347 f36fd90a 2022-07-09 op #if defined(__linux__) || defined(__MINT__)
348 f36fd90a 2022-07-09 op #define _GNU_SOURCE /* See test-*.c what needs this. */
350 f36fd90a 2022-07-09 op #include <string.h>
355 f36fd90a 2022-07-09 op const char *buf = "abcdef";
358 f36fd90a 2022-07-09 op res = memrchr(buf, 'a', strlen(buf));
359 f36fd90a 2022-07-09 op return(NULL == res ? 1 : 0);
361 f36fd90a 2022-07-09 op #endif /* TEST_MEMRCHR */
362 f36fd90a 2022-07-09 op #if TEST_MEMSET_S
363 f36fd90a 2022-07-09 op #include <string.h>
365 f36fd90a 2022-07-09 op int main(void)
367 f36fd90a 2022-07-09 op char buf[10];
368 f36fd90a 2022-07-09 op memset_s(buf, 0, 'c', sizeof(buf));
371 f36fd90a 2022-07-09 op #endif /* TEST_MEMSET_S */
372 f36fd90a 2022-07-09 op #if TEST_MKFIFOAT
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 mkfifoat(AT_FDCWD, "this/path/should/not/exist", 0600);
380 f36fd90a 2022-07-09 op #endif /* TEST_MKFIFOAT */
381 f36fd90a 2022-07-09 op #if TEST_MKNODAT
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 mknodat(AT_FDCWD, "this/path/should/not/exist", S_IFIFO | 0600, 0);
389 f36fd90a 2022-07-09 op #endif /* TEST_MKNODAT */
390 f36fd90a 2022-07-09 op #if TEST_OPTRESET
391 f36fd90a 2022-07-09 op #include <unistd.h>
394 f36fd90a 2022-07-09 op main(int argc, char **argv)
396 f36fd90a 2022-07-09 op optreset = 1;
397 f36fd90a 2022-07-09 op getopt(argc, argv, "");
400 f36fd90a 2022-07-09 op #endif /* TEST_OPTRESET */
401 f36fd90a 2022-07-09 op #if TEST_OSBYTEORDER_H
402 f36fd90a 2022-07-09 op #include <libkern/OSByteOrder.h>
407 f36fd90a 2022-07-09 op return !OSSwapHostToLittleInt32(23);
409 f36fd90a 2022-07-09 op #endif /* TEST_OSBYTEORDER_H */
410 f36fd90a 2022-07-09 op #if TEST_PATH_MAX
412 f36fd90a 2022-07-09 op * POSIX allows PATH_MAX to not be defined, see
413 f36fd90a 2022-07-09 op * http://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html;
414 f36fd90a 2022-07-09 op * the GNU Hurd is an example of a system not having it.
416 f36fd90a 2022-07-09 op * Arguably, it would be better to test sysconf(_SC_PATH_MAX),
417 f36fd90a 2022-07-09 op * but since the individual *.c files include "config.h" before
418 f36fd90a 2022-07-09 op * <limits.h>, overriding an excessive value of PATH_MAX from
419 f36fd90a 2022-07-09 op * "config.h" is impossible anyway, so for now, the simplest
420 f36fd90a 2022-07-09 op * fix is to provide a value only on systems not having any.
421 f36fd90a 2022-07-09 op * So far, we encountered no system defining PATH_MAX to an
422 f36fd90a 2022-07-09 op * impractically large value, even though POSIX explicitly
423 f36fd90a 2022-07-09 op * allows that.
425 f36fd90a 2022-07-09 op * The real fix would be to replace all static buffers of size
426 f36fd90a 2022-07-09 op * PATH_MAX by dynamically allocated buffers. But that is
427 f36fd90a 2022-07-09 op * somewhat intrusive because it touches several files and
428 f36fd90a 2022-07-09 op * because it requires changing struct mlink in mandocdb.c.
429 f36fd90a 2022-07-09 op * So i'm postponing that for now.
432 f36fd90a 2022-07-09 op #include <limits.h>
433 f36fd90a 2022-07-09 op #include <stdio.h>
438 f36fd90a 2022-07-09 op printf("PATH_MAX is defined to be %ld\n", (long)PATH_MAX);
441 f36fd90a 2022-07-09 op #endif /* TEST_PATH_MAX */
442 f36fd90a 2022-07-09 op #if TEST_PLEDGE
443 f36fd90a 2022-07-09 op #include <unistd.h>
448 f36fd90a 2022-07-09 op return !!pledge("stdio", NULL);
450 f36fd90a 2022-07-09 op #endif /* TEST_PLEDGE */
451 f36fd90a 2022-07-09 op #if TEST_PROGRAM_INVOCATION_SHORT_NAME
452 f36fd90a 2022-07-09 op #define _GNU_SOURCE /* See feature_test_macros(7) */
453 f36fd90a 2022-07-09 op #include <errno.h>
459 f36fd90a 2022-07-09 op return !program_invocation_short_name;
461 f36fd90a 2022-07-09 op #endif /* TEST_PROGRAM_INVOCATION_SHORT_NAME */
462 f36fd90a 2022-07-09 op #if TEST_PR_SET_NAME
463 f36fd90a 2022-07-09 op #include <sys/prctl.h>
468 f36fd90a 2022-07-09 op prctl(PR_SET_NAME, "foo");
471 f36fd90a 2022-07-09 op #endif /* TEST_PR_SET_NAME */
472 f36fd90a 2022-07-09 op #if TEST_READPASSPHRASE
473 f36fd90a 2022-07-09 op #include <stddef.h>
474 f36fd90a 2022-07-09 op #include <readpassphrase.h>
479 f36fd90a 2022-07-09 op return !!readpassphrase("prompt: ", NULL, 0, 0);
481 f36fd90a 2022-07-09 op #endif /* TEST_READPASSPHRASE */
482 f36fd90a 2022-07-09 op #if TEST_REALLOCARRAY
483 f36fd90a 2022-07-09 op #ifdef __NetBSD__
484 f36fd90a 2022-07-09 op # define _OPENBSD_SOURCE
486 f36fd90a 2022-07-09 op #include <stdlib.h>
491 f36fd90a 2022-07-09 op return !reallocarray(NULL, 2, 2);
493 f36fd90a 2022-07-09 op #endif /* TEST_REALLOCARRAY */
494 f36fd90a 2022-07-09 op #if TEST_RECALLOCARRAY
495 f36fd90a 2022-07-09 op #include <stdlib.h>
500 f36fd90a 2022-07-09 op return !recallocarray(NULL, 0, 2, 2);
502 f36fd90a 2022-07-09 op #endif /* TEST_RECALLOCARRAY */
503 f36fd90a 2022-07-09 op #if TEST_SANDBOX_INIT
504 f36fd90a 2022-07-09 op #include <sandbox.h>
512 f36fd90a 2022-07-09 op rc = sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, &ep);
513 f36fd90a 2022-07-09 op if (-1 == rc)
514 f36fd90a 2022-07-09 op sandbox_free_error(ep);
515 f36fd90a 2022-07-09 op return(-1 == rc);
517 f36fd90a 2022-07-09 op #endif /* TEST_SANDBOX_INIT */
518 f36fd90a 2022-07-09 op #if TEST_SETPROCTITLE
519 f36fd90a 2022-07-09 op #include <stdlib.h>
524 f36fd90a 2022-07-09 op setproctitle("#%d test program", 7);
528 f36fd90a 2022-07-09 op #if TEST_SIO_FLUSH
529 f36fd90a 2022-07-09 op #include <sndio.h>
534 f36fd90a 2022-07-09 op struct sio_hdl *hdl;
535 f36fd90a 2022-07-09 op hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1);
536 f36fd90a 2022-07-09 op sio_flush(hdl);
537 f36fd90a 2022-07-09 op sio_close(hdl);
540 f36fd90a 2022-07-09 op #endif /* TEST_SIO_FLUSH */
541 f36fd90a 2022-07-09 op #if TEST_SCAN_SCALED
542 f36fd90a 2022-07-09 op #include <util.h>
547 f36fd90a 2022-07-09 op char *cinput = (char *)"1.5K", buf[FMT_SCALED_STRSIZE];
548 f36fd90a 2022-07-09 op long long ninput = 10483892, result;
549 f36fd90a 2022-07-09 op return scan_scaled(cinput, &result) == 0;
551 f36fd90a 2022-07-09 op #endif /* TEST_SCAN_SCALED */
552 f36fd90a 2022-07-09 op #if TEST_SECCOMP_FILTER
553 f36fd90a 2022-07-09 op #include <sys/prctl.h>
554 f36fd90a 2022-07-09 op #include <linux/seccomp.h>
555 f36fd90a 2022-07-09 op #include <errno.h>
561 f36fd90a 2022-07-09 op prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, 0);
562 f36fd90a 2022-07-09 op return(EFAULT == errno ? 0 : 1);
564 f36fd90a 2022-07-09 op #endif /* TEST_SECCOMP_FILTER */
565 f36fd90a 2022-07-09 op #if TEST_SETRESGID
566 f36fd90a 2022-07-09 op #define _GNU_SOURCE /* linux */
567 f36fd90a 2022-07-09 op #include <sys/types.h>
568 f36fd90a 2022-07-09 op #include <unistd.h>
573 f36fd90a 2022-07-09 op return setresgid(-1, -1, -1) == -1;
575 f36fd90a 2022-07-09 op #endif /* TEST_SETRESGID */
576 f36fd90a 2022-07-09 op #if TEST_SETRESUID
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 setresuid(-1, -1, -1) == -1;
586 f36fd90a 2022-07-09 op #endif /* TEST_SETRESUID */
587 f36fd90a 2022-07-09 op #if TEST_SHA2
588 f36fd90a 2022-07-09 op #include <sys/types.h>
589 f36fd90a 2022-07-09 op #include <sha2.h>
591 f36fd90a 2022-07-09 op int main(void)
593 f36fd90a 2022-07-09 op SHA2_CTX ctx;
594 f36fd90a 2022-07-09 op char result[SHA256_DIGEST_STRING_LENGTH];
596 f36fd90a 2022-07-09 op SHA256Init(&ctx);
597 f36fd90a 2022-07-09 op SHA256Update(&ctx, (const unsigned char *)"abcd", 4);
598 f36fd90a 2022-07-09 op SHA256End(&ctx, result);
602 f36fd90a 2022-07-09 op #endif /* TEST_SHA2 */
603 f36fd90a 2022-07-09 op #if TEST_SOCK_NONBLOCK
605 f36fd90a 2022-07-09 op * Linux doesn't (always?) have this.
608 f36fd90a 2022-07-09 op #include <sys/socket.h>
614 f36fd90a 2022-07-09 op socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fd);
617 f36fd90a 2022-07-09 op #endif /* TEST_SOCK_NONBLOCK */
618 f36fd90a 2022-07-09 op #if TEST_STATIC
622 f36fd90a 2022-07-09 op return 0; /* not meant to do anything */
624 f36fd90a 2022-07-09 op #endif /* TEST_STATIC */
625 f36fd90a 2022-07-09 op #if TEST_STRLCAT
626 f36fd90a 2022-07-09 op #include <string.h>
631 f36fd90a 2022-07-09 op char buf[3] = "a";
632 f36fd90a 2022-07-09 op return ! (strlcat(buf, "b", sizeof(buf)) == 2 &&
633 f36fd90a 2022-07-09 op buf[0] == 'a' && buf[1] == 'b' && buf[2] == '\0');
635 f36fd90a 2022-07-09 op #endif /* TEST_STRLCAT */
636 f36fd90a 2022-07-09 op #if TEST_STRLCPY
637 f36fd90a 2022-07-09 op #include <string.h>
642 f36fd90a 2022-07-09 op char buf[2] = "";
643 f36fd90a 2022-07-09 op return ! (strlcpy(buf, "a", sizeof(buf)) == 1 &&
644 f36fd90a 2022-07-09 op buf[0] == 'a' && buf[1] == '\0');
646 f36fd90a 2022-07-09 op #endif /* TEST_STRLCPY */
647 f36fd90a 2022-07-09 op #if TEST_STRNDUP
648 f36fd90a 2022-07-09 op #include <string.h>
653 f36fd90a 2022-07-09 op const char *foo = "bar";
656 f36fd90a 2022-07-09 op baz = strndup(foo, 1);
657 f36fd90a 2022-07-09 op return(0 != strcmp(baz, "b"));
659 f36fd90a 2022-07-09 op #endif /* TEST_STRNDUP */
660 f36fd90a 2022-07-09 op #if TEST_STRNLEN
661 f36fd90a 2022-07-09 op #include <string.h>
666 f36fd90a 2022-07-09 op const char *foo = "bar";
669 f36fd90a 2022-07-09 op sz = strnlen(foo, 1);
670 f36fd90a 2022-07-09 op return(1 != sz);
672 f36fd90a 2022-07-09 op #endif /* TEST_STRNLEN */
673 f36fd90a 2022-07-09 op #if TEST_STRTONUM
675 f36fd90a 2022-07-09 op * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
677 f36fd90a 2022-07-09 op * Permission to use, copy, modify, and distribute this software for any
678 f36fd90a 2022-07-09 op * purpose with or without fee is hereby granted, provided that the above
679 f36fd90a 2022-07-09 op * copyright notice and this permission notice appear in all copies.
681 f36fd90a 2022-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
682 f36fd90a 2022-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
683 f36fd90a 2022-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
684 f36fd90a 2022-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
685 f36fd90a 2022-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
686 f36fd90a 2022-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
687 f36fd90a 2022-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
689 f36fd90a 2022-07-09 op #ifdef __NetBSD__
690 f36fd90a 2022-07-09 op # define _OPENBSD_SOURCE
692 f36fd90a 2022-07-09 op #include <stdlib.h>
697 f36fd90a 2022-07-09 op const char *errstr;
699 f36fd90a 2022-07-09 op if (strtonum("1", 0, 2, &errstr) != 1)
701 f36fd90a 2022-07-09 op if (errstr != NULL)
703 f36fd90a 2022-07-09 op if (strtonum("1x", 0, 2, &errstr) != 0)
705 f36fd90a 2022-07-09 op if (errstr == NULL)
707 f36fd90a 2022-07-09 op if (strtonum("2", 0, 1, &errstr) != 0)
709 f36fd90a 2022-07-09 op if (errstr == NULL)
711 f36fd90a 2022-07-09 op if (strtonum("0", 1, 2, &errstr) != 0)
713 f36fd90a 2022-07-09 op if (errstr == NULL)
717 f36fd90a 2022-07-09 op #endif /* TEST_STRTONUM */
718 f36fd90a 2022-07-09 op #if TEST_SYS_BYTEORDER_H
719 f36fd90a 2022-07-09 op #include <sys/byteorder.h>
724 f36fd90a 2022-07-09 op return !LE_32(23);
726 f36fd90a 2022-07-09 op #endif /* TEST_SYS_BYTEORDER_H */
727 f36fd90a 2022-07-09 op #if TEST_SYS_ENDIAN_H
728 f36fd90a 2022-07-09 op #include <sys/endian.h>
733 f36fd90a 2022-07-09 op return !htole32(23);
735 f36fd90a 2022-07-09 op #endif /* TEST_SYS_ENDIAN_H */
736 f36fd90a 2022-07-09 op #if TEST_SYS_MKDEV_H
737 f36fd90a 2022-07-09 op #include <sys/types.h>
738 f36fd90a 2022-07-09 op #include <sys/mkdev.h>
743 f36fd90a 2022-07-09 op return !minor(0);
745 f36fd90a 2022-07-09 op #endif /* TEST_SYS_MKDEV_H */
746 f36fd90a 2022-07-09 op #if TEST_SYS_FILE
747 f36fd90a 2022-07-09 op #include <sys/file.h>
752 f36fd90a 2022-07-09 op flock(0, LOCK_SH|LOCK_NB);
755 f36fd90a 2022-07-09 op #endif /* TEST_SYS_FILE */
756 f36fd90a 2022-07-09 op #if TEST_SYS_QUEUE
757 f36fd90a 2022-07-09 op #include <sys/queue.h>
758 f36fd90a 2022-07-09 op #include <stddef.h>
762 f36fd90a 2022-07-09 op TAILQ_ENTRY(foo) entries;
765 f36fd90a 2022-07-09 op TAILQ_HEAD(fooq, foo);
770 f36fd90a 2022-07-09 op struct fooq foo_q, bar_q;
771 f36fd90a 2022-07-09 op struct foo *p, *tmp;
774 f36fd90a 2022-07-09 op TAILQ_INIT(&foo_q);
775 f36fd90a 2022-07-09 op TAILQ_INIT(&bar_q);
778 f36fd90a 2022-07-09 op * Use TAILQ_FOREACH_SAFE because some systems (e.g., Linux)
779 f36fd90a 2022-07-09 op * have TAILQ_FOREACH but not the safe variant.
782 f36fd90a 2022-07-09 op TAILQ_FOREACH_SAFE(p, &foo_q, entries, tmp)
783 f36fd90a 2022-07-09 op p->bar = i++;
785 f36fd90a 2022-07-09 op /* Test for newer macros as well. */
787 f36fd90a 2022-07-09 op TAILQ_CONCAT(&foo_q, &bar_q, entries);
790 f36fd90a 2022-07-09 op #endif /* TEST_SYS_QUEUE */
791 f36fd90a 2022-07-09 op #if TEST_SYS_SYSMACROS_H
792 f36fd90a 2022-07-09 op #include <sys/sysmacros.h>
797 f36fd90a 2022-07-09 op return !minor(0);
799 f36fd90a 2022-07-09 op #endif /* TEST_SYS_SYSMACROS_H */
800 f36fd90a 2022-07-09 op #if TEST_SYS_TREE
801 f36fd90a 2022-07-09 op #include <sys/tree.h>
802 f36fd90a 2022-07-09 op #include <stdlib.h>
804 f36fd90a 2022-07-09 op struct node {
805 f36fd90a 2022-07-09 op RB_ENTRY(node) entry;
810 f36fd90a 2022-07-09 op intcmp(struct node *e1, struct node *e2)
812 f36fd90a 2022-07-09 op return (e1->i < e2->i ? -1 : e1->i > e2->i);
815 f36fd90a 2022-07-09 op RB_HEAD(inttree, node) head = RB_INITIALIZER(&head);
816 f36fd90a 2022-07-09 op RB_PROTOTYPE(inttree, node, entry, intcmp)
817 f36fd90a 2022-07-09 op RB_GENERATE(inttree, node, entry, intcmp)
819 f36fd90a 2022-07-09 op int testdata[] = {
820 f36fd90a 2022-07-09 op 20, 16, 17, 13, 3, 6, 1, 8, 2, 4
827 f36fd90a 2022-07-09 op struct node *n;
829 f36fd90a 2022-07-09 op for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) {
830 f36fd90a 2022-07-09 op if ((n = malloc(sizeof(struct node))) == NULL)
832 f36fd90a 2022-07-09 op n->i = testdata[i];
833 f36fd90a 2022-07-09 op RB_INSERT(inttree, &head, n);
839 f36fd90a 2022-07-09 op #endif /* TEST_SYS_TREE */
840 31e12e67 2023-09-07 op #if TEST_TIMESPECSUB
841 31e12e67 2023-09-07 op #include <sys/time.h>
846 31e12e67 2023-09-07 op struct timespec a = {0, 0}, b = {0, 0}, c;
848 31e12e67 2023-09-07 op timespecsub(&a, &b, &c);
849 31e12e67 2023-09-07 op return c.tv_sec;
851 31e12e67 2023-09-07 op #endif /* TEST_TIMESPECSUB */
852 f36fd90a 2022-07-09 op #if TEST_UNVEIL
853 f36fd90a 2022-07-09 op #include <unistd.h>
858 f36fd90a 2022-07-09 op return -1 != unveil(NULL, NULL);
860 f36fd90a 2022-07-09 op #endif /* TEST_UNVEIL */
861 f36fd90a 2022-07-09 op #if TEST_WAIT_ANY
862 f36fd90a 2022-07-09 op #include <sys/wait.h>
869 f36fd90a 2022-07-09 op return waitpid(WAIT_ANY, &st, WNOHANG) != -1;
871 f36fd90a 2022-07-09 op #endif /* TEST_WAIT_ANY */
872 f36fd90a 2022-07-09 op #if TEST_LIB_FLAC
873 f36fd90a 2022-07-09 op #include <FLAC/stream_decoder.h>
878 f36fd90a 2022-07-09 op FLAC__StreamDecoder *decoder = NULL;
879 f36fd90a 2022-07-09 op FLAC__StreamDecoderInitStatus init_status;
881 f36fd90a 2022-07-09 op decoder = FLAC__stream_decoder_new();
882 f36fd90a 2022-07-09 op FLAC__stream_decoder_delete(decoder);
885 f36fd90a 2022-07-09 op #endif /* TEST_LIB_FLAC */
886 f36fd90a 2022-07-09 op #if TEST_LIB_MPG123
887 f36fd90a 2022-07-09 op #include <mpg123.h>
892 f36fd90a 2022-07-09 op mpg123_handle *mh;
894 f36fd90a 2022-07-09 op mh = mpg123_new(NULL, NULL);
895 f36fd90a 2022-07-09 op mpg123_delete(mh);
898 f36fd90a 2022-07-09 op #endif/* TEST_LIB_MPG123 */
899 f36fd90a 2022-07-09 op #if TEST_LIB_OPUSFILE
900 f36fd90a 2022-07-09 op #include <stdio.h>
901 f36fd90a 2022-07-09 op #include <opusfile.h>
907 f36fd90a 2022-07-09 op OggOpusFile *of;
908 f36fd90a 2022-07-09 op OpusFileCallbacks cb = {NULL, NULL, NULL, NULL};
911 f36fd90a 2022-07-09 op f = op_fdopen(&cb, 0, "r");
912 f36fd90a 2022-07-09 op of = op_open_callbacks(f, &cb, NULL, 0, &r);
916 f36fd90a 2022-07-09 op #endif /* TEST_LIB_OPUSFILE */
917 f36fd90a 2022-07-09 op #if TEST_LIB_VORBISFILE
918 f36fd90a 2022-07-09 op #include <stdio.h>
919 f36fd90a 2022-07-09 op #include <vorbis/vorbisfile.h>
924 f36fd90a 2022-07-09 op OggVorbis_File vf;
926 f36fd90a 2022-07-09 op ov_open_callbacks(stdin, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE);
927 f36fd90a 2022-07-09 op ov_clear(&vf);
930 f36fd90a 2022-07-09 op #endif /* TEST_LIB_VORBISFILE */
931 34674849 2023-03-23 op #if TEST_LIB_ASOUND
932 34674849 2023-03-23 op #include <alsa/asoundlib.h>
937 34674849 2023-03-23 op snd_pcm_t *pcm;
940 34674849 2023-03-23 op err = snd_pcm_open(&pcm, "default", SND_PCM_STREAM_PLAYBACK,
941 34674849 2023-03-23 op SND_PCM_NONBLOCK);
944 34674849 2023-03-23 op #endif /* TEST_LIB_ASOUND */
945 f36fd90a 2022-07-09 op #if TEST_LIB_SNDIO
946 f36fd90a 2022-07-09 op #include <sndio.h>
951 f36fd90a 2022-07-09 op struct sio_hdl *hdl;
952 f36fd90a 2022-07-09 op hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1);
953 f36fd90a 2022-07-09 op sio_close(hdl);
956 f36fd90a 2022-07-09 op #endif /* TEST_LIB_SNDIO */
957 56de3111 2023-09-07 op #if TEST_LIB_MD
958 56de3111 2023-09-07 op #include <sys/types.h>
959 56de3111 2023-09-07 op #include <sha1.h>
964 56de3111 2023-09-07 op SHA1_CTX ctx;
965 56de3111 2023-09-07 op char result[SHA1_DIGEST_STRING_LENGTH];
967 56de3111 2023-09-07 op SHA1Init(&ctx);
968 56de3111 2023-09-07 op SHA1Update(&ctx, (const unsigned char *)"abcd", 4);
969 56de3111 2023-09-07 op SHA1End(&ctx, result);
973 56de3111 2023-09-07 op #endif /* TEST_LIB_MD */