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