rpass

Strong password generator for humans
git clone git://git.sgregoratto.me/rpass
Log | Files | Refs | README

tests.c (9386B)


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