tests.c (8255B)
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_REALLOCARRAY 214 #include <stdlib.h> 215 216 int 217 main(void) 218 { 219 return !reallocarray(NULL, 2, 2); 220 } 221 #endif /* TEST_REALLOCARRAY */ 222 #if TEST_RECALLOCARRAY 223 #include <stdlib.h> 224 225 int 226 main(void) 227 { 228 return !recallocarray(NULL, 0, 2, 2); 229 } 230 #endif /* TEST_RECALLOCARRAY */ 231 #if TEST_SANDBOX_INIT 232 #include <sandbox.h> 233 234 int 235 main(void) 236 { 237 char *ep; 238 int rc; 239 240 rc = sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, &ep); 241 if (-1 == rc) 242 sandbox_free_error(ep); 243 return(-1 == rc); 244 } 245 #endif /* TEST_SANDBOX_INIT */ 246 #if TEST_SECCOMP_FILTER 247 #include <sys/prctl.h> 248 #include <linux/seccomp.h> 249 #include <errno.h> 250 251 int 252 main(void) 253 { 254 255 prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, 0); 256 return(EFAULT == errno ? 0 : 1); 257 } 258 #endif /* TEST_SECCOMP_FILTER */ 259 #if TEST_SOCK_NONBLOCK 260 /* 261 * Linux doesn't (always?) have this. 262 */ 263 264 #include <sys/socket.h> 265 266 int 267 main(void) 268 { 269 int fd[2]; 270 socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fd); 271 return 0; 272 } 273 #endif /* TEST_SOCK_NONBLOCK */ 274 #if TEST_STRLCAT 275 #include <string.h> 276 277 int 278 main(void) 279 { 280 char buf[3] = "a"; 281 return ! (strlcat(buf, "b", sizeof(buf)) == 2 && 282 buf[0] == 'a' && buf[1] == 'b' && buf[2] == '\0'); 283 } 284 #endif /* TEST_STRLCAT */ 285 #if TEST_STRLCPY 286 #include <string.h> 287 288 int 289 main(void) 290 { 291 char buf[2] = ""; 292 return ! (strlcpy(buf, "a", sizeof(buf)) == 1 && 293 buf[0] == 'a' && buf[1] == '\0'); 294 } 295 #endif /* TEST_STRLCPY */ 296 #if TEST_STRNDUP 297 #include <string.h> 298 299 int 300 main(void) 301 { 302 const char *foo = "bar"; 303 char *baz; 304 305 baz = strndup(foo, 1); 306 return(0 != strcmp(baz, "b")); 307 } 308 #endif /* TEST_STRNDUP */ 309 #if TEST_STRNLEN 310 #include <string.h> 311 312 int 313 main(void) 314 { 315 const char *foo = "bar"; 316 size_t sz; 317 318 sz = strnlen(foo, 1); 319 return(1 != sz); 320 } 321 #endif /* TEST_STRNLEN */ 322 #if TEST_STRTONUM 323 /* 324 * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org> 325 * 326 * Permission to use, copy, modify, and distribute this software for any 327 * purpose with or without fee is hereby granted, provided that the above 328 * copyright notice and this permission notice appear in all copies. 329 * 330 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 331 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 332 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 333 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 334 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 335 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 336 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 337 */ 338 339 #include <stdlib.h> 340 341 int 342 main(void) 343 { 344 const char *errstr; 345 346 if (strtonum("1", 0, 2, &errstr) != 1) 347 return 1; 348 if (errstr != NULL) 349 return 2; 350 if (strtonum("1x", 0, 2, &errstr) != 0) 351 return 3; 352 if (errstr == NULL) 353 return 4; 354 if (strtonum("2", 0, 1, &errstr) != 0) 355 return 5; 356 if (errstr == NULL) 357 return 6; 358 if (strtonum("0", 1, 2, &errstr) != 0) 359 return 7; 360 if (errstr == NULL) 361 return 8; 362 return 0; 363 } 364 #endif /* TEST_STRTONUM */ 365 #if TEST_SYS_QUEUE 366 #include <sys/queue.h> 367 #include <stddef.h> 368 369 struct foo { 370 int bar; 371 TAILQ_ENTRY(foo) entries; 372 }; 373 374 TAILQ_HEAD(fooq, foo); 375 376 int 377 main(void) 378 { 379 struct fooq foo_q; 380 struct foo *p, *tmp; 381 int i = 0; 382 383 TAILQ_INIT(&foo_q); 384 385 /* 386 * Use TAILQ_FOREACH_SAFE because some systems (e.g., Linux) 387 * have TAILQ_FOREACH but not the safe variant. 388 */ 389 390 TAILQ_FOREACH_SAFE(p, &foo_q, entries, tmp) 391 p->bar = i++; 392 return 0; 393 } 394 #endif /* TEST_SYS_QUEUE */ 395 #if TEST_SYSTRACE 396 #include <sys/param.h> 397 #include <dev/systrace.h> 398 399 #include <stdlib.h> 400 401 int 402 main(void) 403 { 404 405 return(0); 406 } 407 #endif /* TEST_SYSTRACE */ 408 #if TEST_UNVEIL 409 #include <unistd.h> 410 411 int 412 main(void) 413 { 414 return -1 != unveil(NULL, NULL); 415 } 416 #endif /* TEST_UNVEIL */ 417 #if TEST_ZLIB 418 #include <stddef.h> 419 #include <zlib.h> 420 421 int 422 main(void) 423 { 424 gzFile gz; 425 426 if (NULL == (gz = gzopen("/dev/null", "w"))) 427 return(1); 428 gzputs(gz, "foo"); 429 gzclose(gz); 430 return(0); 431 } 432 #endif /* TEST_ZLIB */