diceware-c

Interactive Diceware Generator
git clone git://git.sgregoratto.me/diceware-c
Log | Files | Refs

tests.c (9591B)


      1 #if TEST___PROGNAME
      2 int
      3 main(void)
      4 {
      5 	extern char *__progname;
      6 
      7 	return !__progname;
      8 }
      9 #endif /* TEST___PROGNAME */
     10 #if TEST_ARC4RANDOM
     11 #include <stdlib.h>
     12 
     13 int
     14 main(void)
     15 {
     16 	return (arc4random() + 1) ? 0 : 1;
     17 }
     18 #endif /* TEST_ARC4RANDOM */
     19 #if TEST_B64_NTOP
     20 #include <netinet/in.h>
     21 #include <resolv.h>
     22 
     23 int
     24 main(void)
     25 {
     26 	const char *src = "hello world";
     27 	char output[1024];
     28 
     29 	return b64_ntop((const unsigned char *)src, 11, output, sizeof(output)) > 0 ? 0 : 1;
     30 }
     31 #endif /* TEST_B64_NTOP */
     32 #if TEST_CAPSICUM
     33 #include <sys/capsicum.h>
     34 
     35 int
     36 main(void)
     37 {
     38 	cap_enter();
     39 	return(0);
     40 }
     41 #endif /* TEST_CAPSICUM */
     42 #if TEST_ENDIAN_H
     43 #include <endian.h>
     44 
     45 int
     46 main(void)
     47 {
     48 	return !htole32(23);
     49 }
     50 #endif /* TEST_ENDIAN_H */
     51 #if TEST_ERR
     52 /*
     53  * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
     54  *
     55  * Permission to use, copy, modify, and distribute this software for any
     56  * purpose with or without fee is hereby granted, provided that the above
     57  * copyright notice and this permission notice appear in all copies.
     58  *
     59  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     60  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     61  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     62  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     63  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     64  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     65  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     66  */
     67 
     68 #include <err.h>
     69 
     70 int
     71 main(void)
     72 {
     73 	warnx("%d. warnx", 1);
     74 	warn("%d. warn", 2);
     75 	err(0, "%d. err", 3);
     76 	/* NOTREACHED */
     77 	return 1;
     78 }
     79 #endif /* TEST_ERR */
     80 #if TEST_EXPLICIT_BZERO
     81 #include <string.h>
     82 
     83 int
     84 main(void)
     85 {
     86 	char foo[10];
     87 
     88 	explicit_bzero(foo, sizeof(foo));
     89 	return(0);
     90 }
     91 #endif /* TEST_EXPLICIT_BZERO */
     92 #if TEST_GETEXECNAME
     93 #include <stdlib.h>
     94 
     95 int
     96 main(void)
     97 {
     98 	const char * progname;
     99 
    100 	progname = getexecname();
    101 	return progname == NULL;
    102 }
    103 #endif /* TEST_GETEXECNAME */
    104 #if TEST_GETPROGNAME
    105 #include <stdlib.h>
    106 
    107 int
    108 main(void)
    109 {
    110 	const char * progname;
    111 
    112 	progname = getprogname();
    113 	return progname == NULL;
    114 }
    115 #endif /* TEST_GETPROGNAME */
    116 #if TEST_GETRANDOM
    117 #include <sys/random.h>
    118 #include <stdlib.h>
    119 
    120 int
    121 main(void)
    122 {
    123 	ssize_t len;
    124 	int num;
    125 
    126 	len = getrandom(&num, sizeof(len), 0);
    127 	return (len != -1) ? 0 : 1;
    128 }
    129 #endif /* TEST_GETRANDOM */
    130 #if TEST_INFTIM
    131 /*
    132  * Linux doesn't (always?) have this.
    133  */
    134 
    135 #include <poll.h>
    136 #include <stdio.h>
    137 
    138 int
    139 main(void)
    140 {
    141 	printf("INFTIM is defined to be %ld\n", (long)INFTIM);
    142 	return 0;
    143 }
    144 #endif /* TEST_INFTIM */
    145 #if TEST_MD5
    146 #include <sys/types.h>
    147 #include <md5.h>
    148 
    149 int main(void)
    150 {
    151 	MD5_CTX ctx;
    152 
    153 	MD5Init(&ctx);
    154 	MD5Update(&ctx, "abcd", 4);
    155 
    156 	return 0;
    157 }
    158 #endif /* TEST_MD5 */
    159 #if TEST_MEMMEM
    160 #define _GNU_SOURCE
    161 #include <string.h>
    162 
    163 int
    164 main(void)
    165 {
    166 	char *a = memmem("hello, world", strlen("hello, world"), "world", strlen("world"));
    167 	return(NULL == a);
    168 }
    169 #endif /* TEST_MEMMEM */
    170 #if TEST_MEMRCHR
    171 #if defined(__linux__) || defined(__MINT__)
    172 #define _GNU_SOURCE	/* See test-*.c what needs this. */
    173 #endif
    174 #include <string.h>
    175 
    176 int
    177 main(void)
    178 {
    179 	const char *buf = "abcdef";
    180 	void *res;
    181 
    182 	res = memrchr(buf, 'a', strlen(buf));
    183 	return(NULL == res ? 1 : 0);
    184 }
    185 #endif /* TEST_MEMRCHR */
    186 #if TEST_MEMSET_S
    187 #include <string.h>
    188 
    189 int main(void)
    190 {
    191 	char buf[10];
    192 	memset_s(buf, 0, 'c', sizeof(buf));
    193 	return 0;
    194 }
    195 #endif /* TEST_MEMSET_S */
    196 #if TEST_PATH_MAX
    197 /*
    198  * POSIX allows PATH_MAX to not be defined, see
    199  * http://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html;
    200  * the GNU Hurd is an example of a system not having it.
    201  *
    202  * Arguably, it would be better to test sysconf(_SC_PATH_MAX),
    203  * but since the individual *.c files include "config.h" before
    204  * <limits.h>, overriding an excessive value of PATH_MAX from
    205  * "config.h" is impossible anyway, so for now, the simplest
    206  * fix is to provide a value only on systems not having any.
    207  * So far, we encountered no system defining PATH_MAX to an
    208  * impractically large value, even though POSIX explicitly
    209  * allows that.
    210  *
    211  * The real fix would be to replace all static buffers of size
    212  * PATH_MAX by dynamically allocated buffers.  But that is
    213  * somewhat intrusive because it touches several files and
    214  * because it requires changing struct mlink in mandocdb.c.
    215  * So i'm postponing that for now.
    216  */
    217 
    218 #include <limits.h>
    219 #include <stdio.h>
    220 
    221 int
    222 main(void)
    223 {
    224 	printf("PATH_MAX is defined to be %ld\n", (long)PATH_MAX);
    225 	return 0;
    226 }
    227 #endif /* TEST_PATH_MAX */
    228 #if TEST_PLEDGE
    229 #include <unistd.h>
    230 
    231 int
    232 main(void)
    233 {
    234 	return !!pledge("stdio", NULL);
    235 }
    236 #endif /* TEST_PLEDGE */
    237 #if TEST_PROGRAM_INVOCATION_SHORT_NAME
    238 #define _GNU_SOURCE         /* See feature_test_macros(7) */
    239 #include <errno.h>
    240 
    241 int
    242 main(void)
    243 {
    244 
    245 	return !program_invocation_short_name;
    246 }
    247 #endif /* TEST_PROGRAM_INVOCATION_SHORT_NAME */
    248 #if TEST_READPASSPHRASE
    249 #include <stddef.h>
    250 #include <readpassphrase.h>
    251 
    252 int
    253 main(void)
    254 {
    255 	return !!readpassphrase("prompt: ", NULL, 0, 0);
    256 }
    257 #endif /* TEST_READPASSPHRASE */
    258 #if TEST_REALLOCARRAY
    259 #include <stdlib.h>
    260 
    261 int
    262 main(void)
    263 {
    264 	return !reallocarray(NULL, 2, 2);
    265 }
    266 #endif /* TEST_REALLOCARRAY */
    267 #if TEST_RECALLOCARRAY
    268 #include <stdlib.h>
    269 
    270 int
    271 main(void)
    272 {
    273 	return !recallocarray(NULL, 0, 2, 2);
    274 }
    275 #endif /* TEST_RECALLOCARRAY */
    276 #if TEST_SANDBOX_INIT
    277 #include <sandbox.h>
    278 
    279 int
    280 main(void)
    281 {
    282 	char	*ep;
    283 	int	 rc;
    284 
    285 	rc = sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, &ep);
    286 	if (-1 == rc)
    287 		sandbox_free_error(ep);
    288 	return(-1 == rc);
    289 }
    290 #endif /* TEST_SANDBOX_INIT */
    291 #if TEST_SECCOMP_FILTER
    292 #include <sys/prctl.h>
    293 #include <linux/seccomp.h>
    294 #include <errno.h>
    295 
    296 int
    297 main(void)
    298 {
    299 
    300 	prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, 0);
    301 	return(EFAULT == errno ? 0 : 1);
    302 }
    303 #endif /* TEST_SECCOMP_FILTER */
    304 #if TEST_SOCK_NONBLOCK
    305 /*
    306  * Linux doesn't (always?) have this.
    307  */
    308 
    309 #include <sys/socket.h>
    310 
    311 int
    312 main(void)
    313 {
    314 	int fd[2];
    315 	socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fd);
    316 	return 0;
    317 }
    318 #endif /* TEST_SOCK_NONBLOCK */
    319 #if TEST_STRLCAT
    320 #include <string.h>
    321 
    322 int
    323 main(void)
    324 {
    325 	char buf[3] = "a";
    326 	return ! (strlcat(buf, "b", sizeof(buf)) == 2 &&
    327 	    buf[0] == 'a' && buf[1] == 'b' && buf[2] == '\0');
    328 }
    329 #endif /* TEST_STRLCAT */
    330 #if TEST_STRLCPY
    331 #include <string.h>
    332 
    333 int
    334 main(void)
    335 {
    336 	char buf[2] = "";
    337 	return ! (strlcpy(buf, "a", sizeof(buf)) == 1 &&
    338 	    buf[0] == 'a' && buf[1] == '\0');
    339 }
    340 #endif /* TEST_STRLCPY */
    341 #if TEST_STRNDUP
    342 #include <string.h>
    343 
    344 int
    345 main(void)
    346 {
    347 	const char *foo = "bar";
    348 	char *baz;
    349 
    350 	baz = strndup(foo, 1);
    351 	return(0 != strcmp(baz, "b"));
    352 }
    353 #endif /* TEST_STRNDUP */
    354 #if TEST_STRNLEN
    355 #include <string.h>
    356 
    357 int
    358 main(void)
    359 {
    360 	const char *foo = "bar";
    361 	size_t sz;
    362 
    363 	sz = strnlen(foo, 1);
    364 	return(1 != sz);
    365 }
    366 #endif /* TEST_STRNLEN */
    367 #if TEST_STRTONUM
    368 /*
    369  * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
    370  *
    371  * Permission to use, copy, modify, and distribute this software for any
    372  * purpose with or without fee is hereby granted, provided that the above
    373  * copyright notice and this permission notice appear in all copies.
    374  *
    375  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    376  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    377  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    378  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    379  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    380  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    381  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    382  */
    383 
    384 #include <stdlib.h>
    385 
    386 int
    387 main(void)
    388 {
    389 	const char *errstr;
    390 
    391 	if (strtonum("1", 0, 2, &errstr) != 1)
    392 		return 1;
    393 	if (errstr != NULL)
    394 		return 2;
    395 	if (strtonum("1x", 0, 2, &errstr) != 0)
    396 		return 3;
    397 	if (errstr == NULL)
    398 		return 4;
    399 	if (strtonum("2", 0, 1, &errstr) != 0)
    400 		return 5;
    401 	if (errstr == NULL)
    402 		return 6;
    403 	if (strtonum("0", 1, 2, &errstr) != 0)
    404 		return 7;
    405 	if (errstr == NULL)
    406 		return 8;
    407 	return 0;
    408 }
    409 #endif /* TEST_STRTONUM */
    410 #if TEST_SYS_QUEUE
    411 #include <sys/queue.h>
    412 #include <stddef.h>
    413 
    414 struct foo {
    415 	int bar;
    416 	TAILQ_ENTRY(foo) entries;
    417 };
    418 
    419 TAILQ_HEAD(fooq, foo);
    420 
    421 int
    422 main(void)
    423 {
    424 	struct fooq foo_q;
    425 	struct foo *p, *tmp;
    426 	int i = 0;
    427 
    428 	TAILQ_INIT(&foo_q);
    429 
    430 	/*
    431 	 * Use TAILQ_FOREACH_SAFE because some systems (e.g., Linux)
    432 	 * have TAILQ_FOREACH but not the safe variant.
    433 	 */
    434 
    435 	TAILQ_FOREACH_SAFE(p, &foo_q, entries, tmp)
    436 		p->bar = i++;
    437 	return 0;
    438 }
    439 #endif /* TEST_SYS_QUEUE */
    440 #if TEST_SYS_TREE
    441 #include <sys/tree.h>
    442 #include <stdlib.h>
    443 
    444 struct node {
    445 	RB_ENTRY(node) entry;
    446 	int i;
    447 };
    448 
    449 static int
    450 intcmp(struct node *e1, struct node *e2)
    451 {
    452 	return (e1->i < e2->i ? -1 : e1->i > e2->i);
    453 }
    454 
    455 RB_HEAD(inttree, node) head = RB_INITIALIZER(&head);
    456 RB_PROTOTYPE(inttree, node, entry, intcmp)
    457 RB_GENERATE(inttree, node, entry, intcmp)
    458 
    459 int testdata[] = {
    460 	20, 16, 17, 13, 3, 6, 1, 8, 2, 4
    461 };
    462 
    463 int
    464 main(void)
    465 {
    466 	size_t i;
    467 	struct node *n;
    468 
    469 	for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) {
    470 		if ((n = malloc(sizeof(struct node))) == NULL)
    471 			return 1;
    472 		n->i = testdata[i];
    473 		RB_INSERT(inttree, &head, n);
    474 	}
    475 
    476 	return 0;
    477 }
    478 
    479 #endif /* TEST_SYS_TREE */
    480 #if TEST_SYSTRACE
    481 #include <sys/param.h>
    482 #include <dev/systrace.h>
    483 
    484 #include <stdlib.h>
    485 
    486 int
    487 main(void)
    488 {
    489 
    490 	return(0);
    491 }
    492 #endif /* TEST_SYSTRACE */
    493 #if TEST_UNVEIL
    494 #include <unistd.h>
    495 
    496 int
    497 main(void)
    498 {
    499 	return -1 != unveil(NULL, NULL);
    500 }
    501 #endif /* TEST_UNVEIL */
    502 #if TEST_ZLIB
    503 #include <stddef.h>
    504 #include <zlib.h>
    505 
    506 int
    507 main(void)
    508 {
    509 	gzFile		 gz;
    510 
    511 	if (NULL == (gz = gzopen("/dev/null", "w")))
    512 		return(1);
    513 	gzputs(gz, "foo");
    514 	gzclose(gz);
    515 	return(0);
    516 }
    517 #endif /* TEST_ZLIB */