ksgi
/
tests.c
769 строк · 14.1 Кб
1#if TEST___PROGNAME2int
3main(void)4{
5extern char *__progname;6
7return !__progname;8}
9#endif /* TEST___PROGNAME */10#if TEST_ARC4RANDOM11#include <stdlib.h>12
13int
14main(void)15{
16return (arc4random() + 1) ? 0 : 1;17}
18#endif /* TEST_ARC4RANDOM */19#if TEST_B64_NTOP20#include <netinet/in.h>21#include <resolv.h>22
23int
24main(void)25{
26const char *src = "hello world";27char output[1024];28
29return b64_ntop((const unsigned char *)src, 11, output, sizeof(output)) > 0 ? 0 : 1;30}
31#endif /* TEST_B64_NTOP */32#if TEST_CAPSICUM33#include <sys/capsicum.h>34
35int
36main(void)37{
38cap_enter();39return(0);40}
41#endif /* TEST_CAPSICUM */42#if TEST_CRYPT43#if defined(__linux__) || defined(__wasi__)44# define _GNU_SOURCE /* old glibc */45# define _DEFAULT_SOURCE /* new glibc */46#endif47#if defined(__sun)48# ifndef _XOPEN_SOURCE /* SunOS already defines */49# define _XOPEN_SOURCE /* XPGx */50# endif51# define _XOPEN_SOURCE_EXTENDED 1 /* XPG4v2 */52# ifndef __EXTENSIONS__ /* SunOS already defines */53# define __EXTENSIONS__ /* reallocarray, etc. */54# endif55#endif56#include <unistd.h>57
58int main(void)59{
60char *v;61
62v = crypt("this_is_a_key", "123455");63return v == NULL;64}
65#endif /* TEST_CRYPT */66#if TEST_CRYPT_NEWHASH67#include <pwd.h> /* _PASSWORD_LEN */68#include <unistd.h>69
70int
71main(void)72{
73const char *v = "password";74char hash[_PASSWORD_LEN];75
76if (crypt_newhash(v, "bcrypt,a", hash, sizeof(hash)) == -1)77return 1;78if (crypt_checkpass(v, hash) == -1)79return 1;80
81return 0;82}
83#endif /* TEST_CRYPT_NEWHASH */84#if TEST_ENDIAN_H85#if defined(__linux__) || defined(__wasi__)86# define _DEFAULT_SOURCE87#endif88#include <endian.h>89
90int
91main(void)92{
93return !htole32(23);94}
95#endif /* TEST_ENDIAN_H */96#if TEST_ERR97/*
98* Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
99*
100* Permission to use, copy, modify, and distribute this software for any
101* purpose with or without fee is hereby granted, provided that the above
102* copyright notice and this permission notice appear in all copies.
103*
104* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
105* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
106* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
107* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
108* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
109* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
110* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
111*/
112
113#include <err.h>114#include <errno.h>115
116int
117main(void)118{
119warnx("%d. warnx", 1);120warnc(ENOENT, "%d. warn", ENOENT);121warn("%d. warn", 2);122err(0, "%d. err", 3);123errx(0, "%d. err", 3);124errc(0, ENOENT, "%d. err", 3);125/* NOTREACHED */126return 1;127}
128#endif /* TEST_ERR */129#if TEST_EXPLICIT_BZERO130#include <string.h>131
132int
133main(void)134{
135char foo[10];136
137explicit_bzero(foo, sizeof(foo));138return(0);139}
140#endif /* TEST_EXPLICIT_BZERO */141#if TEST_FTS142#include <stddef.h>143#include <sys/types.h>144#include <sys/stat.h>145#include <fts.h>146
147int
148main(void)149{
150const char *argv[2];151FTS *ftsp;152FTSENT *entry;153
154argv[0] = ".";155argv[1] = (char *)NULL;156
157ftsp = fts_open((char * const *)argv,158FTS_PHYSICAL | FTS_NOCHDIR, NULL);159
160if (ftsp == NULL)161return 1;162
163entry = fts_read(ftsp);164
165if (entry == NULL)166return 1;167
168if (fts_set(ftsp, entry, FTS_SKIP) != 0)169return 1;170
171if (fts_close(ftsp) != 0)172return 1;173
174return 0;175}
176#endif /* TEST_FTS */177#if TEST_GETEXECNAME178#include <stdlib.h>179
180int
181main(void)182{
183const char * progname;184
185progname = getexecname();186return progname == NULL;187}
188#endif /* TEST_GETEXECNAME */189#if TEST_GETPROGNAME190#include <stdlib.h>191
192int
193main(void)194{
195const char * progname;196
197progname = getprogname();198return progname == NULL;199}
200#endif /* TEST_GETPROGNAME */201#if TEST_INFTIM202/*
203* Linux doesn't (always?) have this.
204*/
205
206#include <poll.h>207#include <stdio.h>208
209int
210main(void)211{
212printf("INFTIM is defined to be %ld\n", (long)INFTIM);213return 0;214}
215#endif /* TEST_INFTIM */216#if TEST_LANDLOCK217#include <linux/landlock.h>218#include <linux/prctl.h>219#include <stdlib.h>220#include <sys/prctl.h>221#include <sys/syscall.h>222#include <unistd.h>223#include <stdint.h>224
225#ifndef landlock_create_ruleset226static inline int landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,227const size_t size, const __u32 flags)228{
229return syscall(__NR_landlock_create_ruleset, attr, size, flags);230}
231#endif232
233#ifndef landlock_restrict_self234static inline int landlock_restrict_self(const int ruleset_fd,235const __u32 flags)236{
237return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);238}
239#endif240
241int
242main(void)243{
244uint64_t mask = LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE;245struct landlock_ruleset_attr rules = {246.handled_access_fs = mask247};248int fd = landlock_create_ruleset(&rules, sizeof(rules), 0);249
250if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))251return 1;252return landlock_restrict_self(fd, 0) ? 1 : 0;253}
254#endif /* TEST_LANDLOCK */255#if TEST_LIB_SOCKET256#include <sys/socket.h>257
258int
259main(void)260{
261int fds[2], c;262
263c = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);264return c == -1;265}
266#endif /* TEST_LIB_SOCKET */267#if TEST_MD5268#include <sys/types.h>269#include <md5.h>270
271int main(void)272{
273MD5_CTX ctx;274char result[MD5_DIGEST_STRING_LENGTH];275
276MD5Init(&ctx);277MD5Update(&ctx, (const unsigned char *)"abcd", 4);278MD5End(&ctx, result);279
280return 0;281}
282#endif /* TEST_MD5 */283#if TEST_MEMMEM284#define _GNU_SOURCE285#include <string.h>286
287int
288main(void)289{
290char *a = memmem("hello, world", strlen("hello, world"), "world", strlen("world"));291return(NULL == a);292}
293#endif /* TEST_MEMMEM */294#if TEST_MEMRCHR295#if defined(__linux__) || defined(__MINT__) || defined(__wasi__)296#define _GNU_SOURCE /* See test-*.c what needs this. */297#endif298#include <string.h>299
300int
301main(void)302{
303const char *buf = "abcdef";304void *res;305
306res = memrchr(buf, 'a', strlen(buf));307return(NULL == res ? 1 : 0);308}
309#endif /* TEST_MEMRCHR */310#if TEST_MEMSET_S311#include <string.h>312
313int main(void)314{
315char buf[10];316memset_s(buf, 0, 'c', sizeof(buf));317return 0;318}
319#endif /* TEST_MEMSET_S */320#if TEST_MKFIFOAT321#include <sys/stat.h>322#include <fcntl.h>323
324int main(void) {325mkfifoat(AT_FDCWD, "this/path/should/not/exist", 0600);326return 0;327}
328#endif /* TEST_MKFIFOAT */329#if TEST_MKNODAT330#include <sys/stat.h>331#include <fcntl.h>332
333int main(void) {334mknodat(AT_FDCWD, "this/path/should/not/exist", S_IFIFO | 0600, 0);335return 0;336}
337#endif /* TEST_MKNODAT */338#if TEST_OSBYTEORDER_H339#include <libkern/OSByteOrder.h>340
341int
342main(void)343{
344return !OSSwapHostToLittleInt32(23);345}
346#endif /* TEST_OSBYTEORDER_H */347#if TEST_PATH_MAX348/*
349* POSIX allows PATH_MAX to not be defined, see
350* http://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html;
351* the GNU Hurd is an example of a system not having it.
352*
353* Arguably, it would be better to test sysconf(_SC_PATH_MAX),
354* but since the individual *.c files include "config.h" before
355* <limits.h>, overriding an excessive value of PATH_MAX from
356* "config.h" is impossible anyway, so for now, the simplest
357* fix is to provide a value only on systems not having any.
358* So far, we encountered no system defining PATH_MAX to an
359* impractically large value, even though POSIX explicitly
360* allows that.
361*
362* The real fix would be to replace all static buffers of size
363* PATH_MAX by dynamically allocated buffers. But that is
364* somewhat intrusive because it touches several files and
365* because it requires changing struct mlink in mandocdb.c.
366* So i'm postponing that for now.
367*/
368
369#include <limits.h>370#include <stdio.h>371
372int
373main(void)374{
375printf("PATH_MAX is defined to be %ld\n", (long)PATH_MAX);376return 0;377}
378#endif /* TEST_PATH_MAX */379#if TEST_PLEDGE380#include <unistd.h>381
382int
383main(void)384{
385return !!pledge("stdio", NULL);386}
387#endif /* TEST_PLEDGE */388#if TEST_PROGRAM_INVOCATION_SHORT_NAME389#define _GNU_SOURCE /* See feature_test_macros(7) */390#include <errno.h>391
392int
393main(void)394{
395
396return !program_invocation_short_name;397}
398#endif /* TEST_PROGRAM_INVOCATION_SHORT_NAME */399#if TEST_READPASSPHRASE400#include <stddef.h>401#include <readpassphrase.h>402
403int
404main(void)405{
406return !!readpassphrase("prompt: ", NULL, 0, 0);407}
408#endif /* TEST_READPASSPHRASE */409#if TEST_REALLOCARRAY410#ifdef __NetBSD__411# define _OPENBSD_SOURCE412#endif413#include <stdlib.h>414
415int
416main(void)417{
418return !reallocarray(NULL, 2, 2);419}
420#endif /* TEST_REALLOCARRAY */421#if TEST_RECALLOCARRAY422#include <stdlib.h>423
424int
425main(void)426{
427return !recallocarray(NULL, 0, 2, 2);428}
429#endif /* TEST_RECALLOCARRAY */430#if TEST_SANDBOX_INIT431#include <sandbox.h>432
433int
434main(void)435{
436char *ep;437int rc;438
439rc = sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, &ep);440if (-1 == rc)441sandbox_free_error(ep);442return(-1 == rc);443}
444#endif /* TEST_SANDBOX_INIT */445#if TEST_SCAN_SCALED446#include <util.h>447
448int
449main(void)450{
451char *cinput = (char *)"1.5K", buf[FMT_SCALED_STRSIZE];452long long ninput = 10483892, result;453return scan_scaled(cinput, &result) == 0;454}
455#endif /* TEST_SCAN_SCALED */456#if TEST_SECCOMP_FILTER457#include <sys/prctl.h>458#include <linux/seccomp.h>459#include <errno.h>460
461int
462main(void)463{
464
465prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, 0);466return(EFAULT == errno ? 0 : 1);467}
468#endif /* TEST_SECCOMP_FILTER */469#if TEST_SETRESGID470#define _GNU_SOURCE /* linux */471#include <sys/types.h>472#include <unistd.h>473
474int
475main(void)476{
477return setresgid(-1, -1, -1) == -1;478}
479#endif /* TEST_SETRESGID */480#if TEST_SETRESUID481#define _GNU_SOURCE /* linux */482#include <sys/types.h>483#include <unistd.h>484
485int
486main(void)487{
488return setresuid(-1, -1, -1) == -1;489}
490#endif /* TEST_SETRESUID */491#if TEST_SHA2492#include <sys/types.h>493#include <sha2.h>494
495int main(void)496{
497SHA2_CTX ctx;498char result[SHA256_DIGEST_STRING_LENGTH];499
500SHA256Init(&ctx);501SHA256Update(&ctx, (const unsigned char *)"abcd", 4);502SHA256End(&ctx, result);503
504return 0;505}
506#endif /* TEST_SHA2 */507#if TEST_SOCK_NONBLOCK508/*
509* Linux doesn't (always?) have this.
510*/
511
512#include <sys/socket.h>513
514int
515main(void)516{
517int fd[2];518socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fd);519return 0;520}
521#endif /* TEST_SOCK_NONBLOCK */522#if TEST_STATIC523int
524main(void)525{
526return 0; /* not meant to do anything */527}
528#endif /* TEST_STATIC */529#if TEST_STRLCAT530#include <string.h>531
532int
533main(void)534{
535char buf[3] = "a";536return ! (strlcat(buf, "b", sizeof(buf)) == 2 &&537buf[0] == 'a' && buf[1] == 'b' && buf[2] == '\0');538}
539#endif /* TEST_STRLCAT */540#if TEST_STRLCPY541#include <string.h>542
543int
544main(void)545{
546char buf[2] = "";547return ! (strlcpy(buf, "a", sizeof(buf)) == 1 &&548buf[0] == 'a' && buf[1] == '\0');549}
550#endif /* TEST_STRLCPY */551#if TEST_STRNDUP552#include <string.h>553
554int
555main(void)556{
557const char *foo = "bar";558char *baz;559
560baz = strndup(foo, 1);561return(0 != strcmp(baz, "b"));562}
563#endif /* TEST_STRNDUP */564#if TEST_STRNLEN565#include <string.h>566
567int
568main(void)569{
570const char *foo = "bar";571size_t sz;572
573sz = strnlen(foo, 1);574return(1 != sz);575}
576#endif /* TEST_STRNLEN */577#if TEST_STRTONUM578/*
579* Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
580*
581* Permission to use, copy, modify, and distribute this software for any
582* purpose with or without fee is hereby granted, provided that the above
583* copyright notice and this permission notice appear in all copies.
584*
585* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
586* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
587* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
588* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
589* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
590* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
591* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
592*/
593#ifdef __NetBSD__594# define _OPENBSD_SOURCE595#endif596#include <stdlib.h>597
598int
599main(void)600{
601const char *errstr;602
603if (strtonum("1", 0, 2, &errstr) != 1)604return 1;605if (errstr != NULL)606return 2;607if (strtonum("1x", 0, 2, &errstr) != 0)608return 3;609if (errstr == NULL)610return 4;611if (strtonum("2", 0, 1, &errstr) != 0)612return 5;613if (errstr == NULL)614return 6;615if (strtonum("0", 1, 2, &errstr) != 0)616return 7;617if (errstr == NULL)618return 8;619return 0;620}
621#endif /* TEST_STRTONUM */622#if TEST_SYS_BYTEORDER_H623#include <sys/byteorder.h>624
625int
626main(void)627{
628return !LE_32(23);629}
630#endif /* TEST_SYS_BYTEORDER_H */631#if TEST_SYS_ENDIAN_H632#include <sys/endian.h>633
634int
635main(void)636{
637return !htole32(23);638}
639#endif /* TEST_SYS_ENDIAN_H */640#if TEST_SYS_MKDEV_H641#include <sys/types.h>642#include <sys/mkdev.h>643
644int
645main(void)646{
647return !minor(0);648}
649#endif /* TEST_SYS_MKDEV_H */650#if TEST_SYS_QUEUE651#include <sys/queue.h>652#include <stddef.h>653
654struct foo {655int bar;656TAILQ_ENTRY(foo) entries;657};658
659TAILQ_HEAD(fooq, foo);660
661int
662main(void)663{
664struct fooq foo_q, bar_q;665struct foo *p, *tmp;666int i = 0;667
668TAILQ_INIT(&foo_q);669TAILQ_INIT(&bar_q);670
671/*672* Use TAILQ_FOREACH_SAFE because some systems (e.g., Linux)
673* have TAILQ_FOREACH but not the safe variant.
674*/
675
676TAILQ_FOREACH_SAFE(p, &foo_q, entries, tmp)677p->bar = i++;678
679/* Test for newer macros as well. */680
681TAILQ_CONCAT(&foo_q, &bar_q, entries);682return 0;683}
684#endif /* TEST_SYS_QUEUE */685#if TEST_SYS_SYSMACROS_H686#include <sys/sysmacros.h>687
688int
689main(void)690{
691return !minor(0);692}
693#endif /* TEST_SYS_SYSMACROS_H */694#if TEST_SYS_TREE695#include <sys/tree.h>696#include <stdlib.h>697
698struct node {699RB_ENTRY(node) entry;700int i;701};702
703static int704intcmp(struct node *e1, struct node *e2)705{
706return (e1->i < e2->i ? -1 : e1->i > e2->i);707}
708
709RB_HEAD(inttree, node) head = RB_INITIALIZER(&head);710RB_PROTOTYPE(inttree, node, entry, intcmp)711RB_GENERATE(inttree, node, entry, intcmp)712
713int testdata[] = {71420, 16, 17, 13, 3, 6, 1, 8, 2, 4715};716
717int
718main(void)719{
720size_t i;721struct node *n;722
723for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) {724if ((n = malloc(sizeof(struct node))) == NULL)725return 1;726n->i = testdata[i];727RB_INSERT(inttree, &head, n);728}729
730return 0;731}
732
733#endif /* TEST_SYS_TREE */734#if TEST_TERMIOS735#include <sys/ioctl.h>736#include <string.h> /* memset */737#include <termios.h>738
739int
740main(void)741{
742struct winsize size;743
744memset(&size, 0, sizeof(struct winsize));745if (ioctl(1, TIOCGWINSZ, &size) == -1)746return 72;747return size.ws_col;748}
749#endif /* TEST_TERMIOS */750#if TEST_UNVEIL751#include <unistd.h>752
753int
754main(void)755{
756return -1 != unveil(NULL, NULL);757}
758#endif /* TEST_UNVEIL */759#if TEST_WAIT_ANY760#include <sys/wait.h>761
762int
763main(void)764{
765int st;766
767return waitpid(WAIT_ANY, &st, WNOHANG) != -1;768}
769#endif /* TEST_WAIT_ANY */770