ksgi

Форк
0
/
tests.c 
769 строк · 14.1 Кб
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_CRYPT
43
#if defined(__linux__) || defined(__wasi__)
44
# define _GNU_SOURCE /* old glibc */
45
# define _DEFAULT_SOURCE /* new glibc */
46
#endif
47
#if defined(__sun)
48
# ifndef _XOPEN_SOURCE /* SunOS already defines */
49
#  define _XOPEN_SOURCE /* XPGx */
50
# endif
51
# define _XOPEN_SOURCE_EXTENDED 1 /* XPG4v2 */
52
# ifndef __EXTENSIONS__ /* SunOS already defines */
53
#  define __EXTENSIONS__ /* reallocarray, etc. */
54
# endif
55
#endif
56
#include <unistd.h>
57

58
int main(void)
59
{
60
	char	*v;
61

62
	v = crypt("this_is_a_key", "123455");
63
	return v == NULL;
64
}
65
#endif /* TEST_CRYPT */
66
#if TEST_CRYPT_NEWHASH
67
#include <pwd.h> /* _PASSWORD_LEN */
68
#include <unistd.h>
69

70
int
71
main(void)
72
{
73
	const char	*v = "password";
74
	char		 hash[_PASSWORD_LEN];
75

76
	if (crypt_newhash(v, "bcrypt,a", hash, sizeof(hash)) == -1)
77
		return 1;
78
	if (crypt_checkpass(v, hash) == -1)
79
		return 1;
80

81
	return 0;
82
}
83
#endif /* TEST_CRYPT_NEWHASH */
84
#if TEST_ENDIAN_H
85
#if defined(__linux__) || defined(__wasi__)
86
# define _DEFAULT_SOURCE
87
#endif
88
#include <endian.h>
89

90
int
91
main(void)
92
{
93
	return !htole32(23);
94
}
95
#endif /* TEST_ENDIAN_H */
96
#if TEST_ERR
97
/*
98
 * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
99
 *
100
 * Permission to use, copy, modify, and distribute this software for any
101
 * purpose with or without fee is hereby granted, provided that the above
102
 * copyright notice and this permission notice appear in all copies.
103
 *
104
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
105
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
106
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
107
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
108
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
109
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
110
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
111
 */
112

113
#include <err.h>
114
#include <errno.h>
115

116
int
117
main(void)
118
{
119
	warnx("%d. warnx", 1);
120
	warnc(ENOENT, "%d. warn", ENOENT);
121
	warn("%d. warn", 2);
122
	err(0, "%d. err", 3);
123
	errx(0, "%d. err", 3);
124
	errc(0, ENOENT, "%d. err", 3);
125
	/* NOTREACHED */
126
	return 1;
127
}
128
#endif /* TEST_ERR */
129
#if TEST_EXPLICIT_BZERO
130
#include <string.h>
131

132
int
133
main(void)
134
{
135
	char foo[10];
136

137
	explicit_bzero(foo, sizeof(foo));
138
	return(0);
139
}
140
#endif /* TEST_EXPLICIT_BZERO */
141
#if TEST_FTS
142
#include <stddef.h>
143
#include <sys/types.h>
144
#include <sys/stat.h>
145
#include <fts.h>
146

147
int
148
main(void)
149
{
150
	const char	*argv[2];
151
	FTS		*ftsp;
152
	FTSENT		*entry;
153

154
	argv[0] = ".";
155
	argv[1] = (char *)NULL;
156

157
	ftsp = fts_open((char * const *)argv,
158
	    FTS_PHYSICAL | FTS_NOCHDIR, NULL);
159

160
	if (ftsp == NULL)
161
		return 1;
162

163
	entry = fts_read(ftsp);
164

165
	if (entry == NULL)
166
		return 1;
167

168
	if (fts_set(ftsp, entry, FTS_SKIP) != 0) 
169
		return 1;
170

171
	if (fts_close(ftsp) != 0)
172
		return 1;
173

174
	return 0;
175
}
176
#endif /* TEST_FTS */
177
#if TEST_GETEXECNAME
178
#include <stdlib.h>
179

180
int
181
main(void)
182
{
183
	const char * progname;
184

185
	progname = getexecname();
186
	return progname == NULL;
187
}
188
#endif /* TEST_GETEXECNAME */
189
#if TEST_GETPROGNAME
190
#include <stdlib.h>
191

192
int
193
main(void)
194
{
195
	const char * progname;
196

197
	progname = getprogname();
198
	return progname == NULL;
199
}
200
#endif /* TEST_GETPROGNAME */
201
#if TEST_INFTIM
202
/*
203
 * Linux doesn't (always?) have this.
204
 */
205

206
#include <poll.h>
207
#include <stdio.h>
208

209
int
210
main(void)
211
{
212
	printf("INFTIM is defined to be %ld\n", (long)INFTIM);
213
	return 0;
214
}
215
#endif /* TEST_INFTIM */
216
#if TEST_LANDLOCK
217
#include <linux/landlock.h>
218
#include <linux/prctl.h>
219
#include <stdlib.h>
220
#include <sys/prctl.h>
221
#include <sys/syscall.h>
222
#include <unistd.h>
223
#include <stdint.h>
224

225
#ifndef landlock_create_ruleset
226
static inline int landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,
227
	const size_t size, const __u32 flags)
228
{
229
	return syscall(__NR_landlock_create_ruleset, attr, size, flags);
230
}
231
#endif
232

233
#ifndef landlock_restrict_self
234
static inline int landlock_restrict_self(const int ruleset_fd,
235
	const __u32 flags)
236
{
237
	return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
238
}
239
#endif
240

241
int
242
main(void)
243
{
244
	uint64_t mask = LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE;
245
	struct landlock_ruleset_attr rules = {
246
		.handled_access_fs = mask
247
	};
248
	int fd = landlock_create_ruleset(&rules, sizeof(rules), 0);
249

250
	if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
251
		return 1;
252
	return landlock_restrict_self(fd, 0) ? 1 : 0;
253
}
254
#endif /* TEST_LANDLOCK */
255
#if TEST_LIB_SOCKET
256
#include <sys/socket.h>
257

258
int
259
main(void)
260
{
261
	int fds[2], c;
262

263
	c = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
264
	return c == -1;
265
}
266
#endif /* TEST_LIB_SOCKET */
267
#if TEST_MD5
268
#include <sys/types.h>
269
#include <md5.h>
270

271
int main(void)
272
{
273
	MD5_CTX ctx;
274
	char result[MD5_DIGEST_STRING_LENGTH];
275

276
	MD5Init(&ctx);
277
	MD5Update(&ctx, (const unsigned char *)"abcd", 4);
278
	MD5End(&ctx, result);
279

280
	return 0;
281
}
282
#endif /* TEST_MD5 */
283
#if TEST_MEMMEM
284
#define _GNU_SOURCE
285
#include <string.h>
286

287
int
288
main(void)
289
{
290
	char *a = memmem("hello, world", strlen("hello, world"), "world", strlen("world"));
291
	return(NULL == a);
292
}
293
#endif /* TEST_MEMMEM */
294
#if TEST_MEMRCHR
295
#if defined(__linux__) || defined(__MINT__) || defined(__wasi__)
296
#define _GNU_SOURCE	/* See test-*.c what needs this. */
297
#endif
298
#include <string.h>
299

300
int
301
main(void)
302
{
303
	const char *buf = "abcdef";
304
	void *res;
305

306
	res = memrchr(buf, 'a', strlen(buf));
307
	return(NULL == res ? 1 : 0);
308
}
309
#endif /* TEST_MEMRCHR */
310
#if TEST_MEMSET_S
311
#include <string.h>
312

313
int main(void)
314
{
315
	char buf[10];
316
	memset_s(buf, 0, 'c', sizeof(buf));
317
	return 0;
318
}
319
#endif /* TEST_MEMSET_S */
320
#if TEST_MKFIFOAT
321
#include <sys/stat.h>
322
#include <fcntl.h>
323

324
int main(void) {
325
	mkfifoat(AT_FDCWD, "this/path/should/not/exist", 0600);
326
	return 0;
327
}
328
#endif /* TEST_MKFIFOAT */
329
#if TEST_MKNODAT
330
#include <sys/stat.h>
331
#include <fcntl.h>
332

333
int main(void) {
334
	mknodat(AT_FDCWD, "this/path/should/not/exist", S_IFIFO | 0600, 0);
335
	return 0;
336
}
337
#endif /* TEST_MKNODAT */
338
#if TEST_OSBYTEORDER_H
339
#include <libkern/OSByteOrder.h>
340

341
int
342
main(void)
343
{
344
	return !OSSwapHostToLittleInt32(23);
345
}
346
#endif /* TEST_OSBYTEORDER_H */
347
#if TEST_PATH_MAX
348
/*
349
 * POSIX allows PATH_MAX to not be defined, see
350
 * http://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html;
351
 * the GNU Hurd is an example of a system not having it.
352
 *
353
 * Arguably, it would be better to test sysconf(_SC_PATH_MAX),
354
 * but since the individual *.c files include "config.h" before
355
 * <limits.h>, overriding an excessive value of PATH_MAX from
356
 * "config.h" is impossible anyway, so for now, the simplest
357
 * fix is to provide a value only on systems not having any.
358
 * So far, we encountered no system defining PATH_MAX to an
359
 * impractically large value, even though POSIX explicitly
360
 * allows that.
361
 *
362
 * The real fix would be to replace all static buffers of size
363
 * PATH_MAX by dynamically allocated buffers.  But that is
364
 * somewhat intrusive because it touches several files and
365
 * because it requires changing struct mlink in mandocdb.c.
366
 * So i'm postponing that for now.
367
 */
368

369
#include <limits.h>
370
#include <stdio.h>
371

372
int
373
main(void)
374
{
375
	printf("PATH_MAX is defined to be %ld\n", (long)PATH_MAX);
376
	return 0;
377
}
378
#endif /* TEST_PATH_MAX */
379
#if TEST_PLEDGE
380
#include <unistd.h>
381

382
int
383
main(void)
384
{
385
	return !!pledge("stdio", NULL);
386
}
387
#endif /* TEST_PLEDGE */
388
#if TEST_PROGRAM_INVOCATION_SHORT_NAME
389
#define _GNU_SOURCE         /* See feature_test_macros(7) */
390
#include <errno.h>
391

392
int
393
main(void)
394
{
395

396
	return !program_invocation_short_name;
397
}
398
#endif /* TEST_PROGRAM_INVOCATION_SHORT_NAME */
399
#if TEST_READPASSPHRASE
400
#include <stddef.h>
401
#include <readpassphrase.h>
402

403
int
404
main(void)
405
{
406
	return !!readpassphrase("prompt: ", NULL, 0, 0);
407
}
408
#endif /* TEST_READPASSPHRASE */
409
#if TEST_REALLOCARRAY
410
#ifdef __NetBSD__
411
# define _OPENBSD_SOURCE
412
#endif
413
#include <stdlib.h>
414

415
int
416
main(void)
417
{
418
	return !reallocarray(NULL, 2, 2);
419
}
420
#endif /* TEST_REALLOCARRAY */
421
#if TEST_RECALLOCARRAY
422
#include <stdlib.h>
423

424
int
425
main(void)
426
{
427
	return !recallocarray(NULL, 0, 2, 2);
428
}
429
#endif /* TEST_RECALLOCARRAY */
430
#if TEST_SANDBOX_INIT
431
#include <sandbox.h>
432

433
int
434
main(void)
435
{
436
	char	*ep;
437
	int	 rc;
438

439
	rc = sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, &ep);
440
	if (-1 == rc)
441
		sandbox_free_error(ep);
442
	return(-1 == rc);
443
}
444
#endif /* TEST_SANDBOX_INIT */
445
#if TEST_SCAN_SCALED
446
#include <util.h>
447

448
int
449
main(void)
450
{
451
	char *cinput = (char *)"1.5K", buf[FMT_SCALED_STRSIZE];
452
	long long ninput = 10483892, result;
453
	return scan_scaled(cinput, &result) == 0;
454
}
455
#endif /* TEST_SCAN_SCALED */
456
#if TEST_SECCOMP_FILTER
457
#include <sys/prctl.h>
458
#include <linux/seccomp.h>
459
#include <errno.h>
460

461
int
462
main(void)
463
{
464

465
	prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, 0);
466
	return(EFAULT == errno ? 0 : 1);
467
}
468
#endif /* TEST_SECCOMP_FILTER */
469
#if TEST_SETRESGID
470
#define _GNU_SOURCE /* linux */
471
#include <sys/types.h>
472
#include <unistd.h>
473

474
int
475
main(void)
476
{
477
	return setresgid(-1, -1, -1) == -1;
478
}
479
#endif /* TEST_SETRESGID */
480
#if TEST_SETRESUID
481
#define _GNU_SOURCE /* linux */
482
#include <sys/types.h>
483
#include <unistd.h>
484

485
int
486
main(void)
487
{
488
	return setresuid(-1, -1, -1) == -1;
489
}
490
#endif /* TEST_SETRESUID */
491
#if TEST_SHA2
492
#include <sys/types.h>
493
#include <sha2.h>
494

495
int main(void)
496
{
497
	SHA2_CTX ctx;
498
	char result[SHA256_DIGEST_STRING_LENGTH];
499

500
	SHA256Init(&ctx);
501
	SHA256Update(&ctx, (const unsigned char *)"abcd", 4);
502
	SHA256End(&ctx, result);
503

504
	return 0;
505
}
506
#endif /* TEST_SHA2 */
507
#if TEST_SOCK_NONBLOCK
508
/*
509
 * Linux doesn't (always?) have this.
510
 */
511

512
#include <sys/socket.h>
513

514
int
515
main(void)
516
{
517
	int fd[2];
518
	socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0, fd);
519
	return 0;
520
}
521
#endif /* TEST_SOCK_NONBLOCK */
522
#if TEST_STATIC
523
int
524
main(void)
525
{
526
	return 0; /* not meant to do anything */
527
}
528
#endif /* TEST_STATIC */
529
#if TEST_STRLCAT
530
#include <string.h>
531

532
int
533
main(void)
534
{
535
	char buf[3] = "a";
536
	return ! (strlcat(buf, "b", sizeof(buf)) == 2 &&
537
	    buf[0] == 'a' && buf[1] == 'b' && buf[2] == '\0');
538
}
539
#endif /* TEST_STRLCAT */
540
#if TEST_STRLCPY
541
#include <string.h>
542

543
int
544
main(void)
545
{
546
	char buf[2] = "";
547
	return ! (strlcpy(buf, "a", sizeof(buf)) == 1 &&
548
	    buf[0] == 'a' && buf[1] == '\0');
549
}
550
#endif /* TEST_STRLCPY */
551
#if TEST_STRNDUP
552
#include <string.h>
553

554
int
555
main(void)
556
{
557
	const char *foo = "bar";
558
	char *baz;
559

560
	baz = strndup(foo, 1);
561
	return(0 != strcmp(baz, "b"));
562
}
563
#endif /* TEST_STRNDUP */
564
#if TEST_STRNLEN
565
#include <string.h>
566

567
int
568
main(void)
569
{
570
	const char *foo = "bar";
571
	size_t sz;
572

573
	sz = strnlen(foo, 1);
574
	return(1 != sz);
575
}
576
#endif /* TEST_STRNLEN */
577
#if TEST_STRTONUM
578
/*
579
 * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
580
 *
581
 * Permission to use, copy, modify, and distribute this software for any
582
 * purpose with or without fee is hereby granted, provided that the above
583
 * copyright notice and this permission notice appear in all copies.
584
 *
585
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
586
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
587
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
588
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
589
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
590
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
591
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
592
 */
593
#ifdef __NetBSD__
594
# define _OPENBSD_SOURCE
595
#endif
596
#include <stdlib.h>
597

598
int
599
main(void)
600
{
601
	const char *errstr;
602

603
	if (strtonum("1", 0, 2, &errstr) != 1)
604
		return 1;
605
	if (errstr != NULL)
606
		return 2;
607
	if (strtonum("1x", 0, 2, &errstr) != 0)
608
		return 3;
609
	if (errstr == NULL)
610
		return 4;
611
	if (strtonum("2", 0, 1, &errstr) != 0)
612
		return 5;
613
	if (errstr == NULL)
614
		return 6;
615
	if (strtonum("0", 1, 2, &errstr) != 0)
616
		return 7;
617
	if (errstr == NULL)
618
		return 8;
619
	return 0;
620
}
621
#endif /* TEST_STRTONUM */
622
#if TEST_SYS_BYTEORDER_H
623
#include <sys/byteorder.h>
624

625
int
626
main(void)
627
{
628
	return !LE_32(23);
629
}
630
#endif /* TEST_SYS_BYTEORDER_H */
631
#if TEST_SYS_ENDIAN_H
632
#include <sys/endian.h>
633

634
int
635
main(void)
636
{
637
	return !htole32(23);
638
}
639
#endif /* TEST_SYS_ENDIAN_H */
640
#if TEST_SYS_MKDEV_H
641
#include <sys/types.h>
642
#include <sys/mkdev.h>
643

644
int
645
main(void)
646
{
647
	return !minor(0);
648
}
649
#endif /* TEST_SYS_MKDEV_H */
650
#if TEST_SYS_QUEUE
651
#include <sys/queue.h>
652
#include <stddef.h>
653

654
struct foo {
655
	int bar;
656
	TAILQ_ENTRY(foo) entries;
657
};
658

659
TAILQ_HEAD(fooq, foo);
660

661
int
662
main(void)
663
{
664
	struct fooq foo_q, bar_q;
665
	struct foo *p, *tmp;
666
	int i = 0;
667

668
	TAILQ_INIT(&foo_q);
669
	TAILQ_INIT(&bar_q);
670

671
	/*
672
	 * Use TAILQ_FOREACH_SAFE because some systems (e.g., Linux)
673
	 * have TAILQ_FOREACH but not the safe variant.
674
	 */
675

676
	TAILQ_FOREACH_SAFE(p, &foo_q, entries, tmp)
677
		p->bar = i++;
678

679
	/* Test for newer macros as well. */
680

681
	TAILQ_CONCAT(&foo_q, &bar_q, entries);
682
	return 0;
683
}
684
#endif /* TEST_SYS_QUEUE */
685
#if TEST_SYS_SYSMACROS_H
686
#include <sys/sysmacros.h>
687

688
int
689
main(void)
690
{
691
	return !minor(0);
692
}
693
#endif /* TEST_SYS_SYSMACROS_H */
694
#if TEST_SYS_TREE
695
#include <sys/tree.h>
696
#include <stdlib.h>
697

698
struct node {
699
	RB_ENTRY(node) entry;
700
	int i;
701
};
702

703
static int
704
intcmp(struct node *e1, struct node *e2)
705
{
706
	return (e1->i < e2->i ? -1 : e1->i > e2->i);
707
}
708

709
RB_HEAD(inttree, node) head = RB_INITIALIZER(&head);
710
RB_PROTOTYPE(inttree, node, entry, intcmp)
711
RB_GENERATE(inttree, node, entry, intcmp)
712

713
int testdata[] = {
714
	20, 16, 17, 13, 3, 6, 1, 8, 2, 4
715
};
716

717
int
718
main(void)
719
{
720
	size_t i;
721
	struct node *n;
722

723
	for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) {
724
		if ((n = malloc(sizeof(struct node))) == NULL)
725
			return 1;
726
		n->i = testdata[i];
727
		RB_INSERT(inttree, &head, n);
728
	}
729

730
	return 0;
731
}
732

733
#endif /* TEST_SYS_TREE */
734
#if TEST_TERMIOS
735
#include <sys/ioctl.h>
736
#include <string.h> /* memset */
737
#include <termios.h>
738

739
int
740
main(void)
741
{
742
	struct winsize	 size;
743

744
	memset(&size, 0, sizeof(struct winsize));
745
	if (ioctl(1, TIOCGWINSZ, &size) == -1)
746
		return 72;
747
	return size.ws_col;
748
}
749
#endif /* TEST_TERMIOS */
750
#if TEST_UNVEIL
751
#include <unistd.h>
752

753
int
754
main(void)
755
{
756
	return -1 != unveil(NULL, NULL);
757
}
758
#endif /* TEST_UNVEIL */
759
#if TEST_WAIT_ANY
760
#include <sys/wait.h>
761

762
int
763
main(void)
764
{
765
	int st;
766

767
	return waitpid(WAIT_ANY, &st, WNOHANG) != -1;
768
}
769
#endif /* TEST_WAIT_ANY */
770

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.