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