diff options
Diffstat (limited to 'openbsd-compat/regress')
-rw-r--r-- | openbsd-compat/regress/Makefile.in | 38 | ||||
-rw-r--r-- | openbsd-compat/regress/closefromtest.c | 60 | ||||
-rw-r--r-- | openbsd-compat/regress/snprintftest.c | 73 | ||||
-rw-r--r-- | openbsd-compat/regress/strduptest.c | 45 | ||||
-rw-r--r-- | openbsd-compat/regress/strtonumtest.c | 66 |
5 files changed, 282 insertions, 0 deletions
diff --git a/openbsd-compat/regress/Makefile.in b/openbsd-compat/regress/Makefile.in new file mode 100644 index 000000000..bcf214bd0 --- /dev/null +++ b/openbsd-compat/regress/Makefile.in | |||
@@ -0,0 +1,38 @@ | |||
1 | # $Id: Makefile.in,v 1.4 2006/08/19 09:12:14 dtucker Exp $ | ||
2 | |||
3 | sysconfdir=@sysconfdir@ | ||
4 | piddir=@piddir@ | ||
5 | srcdir=@srcdir@ | ||
6 | top_srcdir=@top_srcdir@ | ||
7 | |||
8 | VPATH=@srcdir@ | ||
9 | CC=@CC@ | ||
10 | LD=@LD@ | ||
11 | CFLAGS=@CFLAGS@ | ||
12 | CPPFLAGS=-I. -I.. -I$(srcdir) -I$(srcdir)/.. @CPPFLAGS@ @DEFS@ | ||
13 | EXEEXT=@EXEEXT@ | ||
14 | LIBCOMPAT=../libopenbsd-compat.a | ||
15 | LIBS=@LIBS@ | ||
16 | LDFLAGS=@LDFLAGS@ $(LIBCOMPAT) | ||
17 | |||
18 | TESTPROGS=closefromtest$(EXEEXT) snprintftest$(EXEEXT) strduptest$(EXEEXT) \ | ||
19 | strtonumtest$(EXEEXT) | ||
20 | |||
21 | all: t-exec ${OTHERTESTS} | ||
22 | |||
23 | %$(EXEEXT): %.c | ||
24 | $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $< $(LIBCOMPAT) $(LIBS) | ||
25 | |||
26 | t-exec: $(TESTPROGS) | ||
27 | @echo running compat regress tests | ||
28 | @for TEST in ""$?; do \ | ||
29 | echo "run test $${TEST}" ... 1>&2; \ | ||
30 | ./$${TEST}$(EXEEXT) || exit $$? ; \ | ||
31 | done | ||
32 | @echo finished compat regress tests | ||
33 | |||
34 | clean: | ||
35 | rm -f *.o *.a core $(TESTPROGS) valid.out | ||
36 | |||
37 | distclean: clean | ||
38 | rm -f Makefile *~ | ||
diff --git a/openbsd-compat/regress/closefromtest.c b/openbsd-compat/regress/closefromtest.c new file mode 100644 index 000000000..feb1b567d --- /dev/null +++ b/openbsd-compat/regress/closefromtest.c | |||
@@ -0,0 +1,60 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2006 Darren Tucker | ||
3 | * | ||
4 | * Permission to use, copy, modify, and distribute this software for any | ||
5 | * purpose with or without fee is hereby granted, provided that the above | ||
6 | * copyright notice and this permission notice appear in all copies. | ||
7 | * | ||
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
15 | */ | ||
16 | |||
17 | #include <sys/types.h> | ||
18 | #include <sys/stat.h> | ||
19 | |||
20 | #include <fcntl.h> | ||
21 | #include <stdio.h> | ||
22 | #include <stdlib.h> | ||
23 | #include <unistd.h> | ||
24 | |||
25 | #define NUM_OPENS 10 | ||
26 | |||
27 | void | ||
28 | fail(char *msg) | ||
29 | { | ||
30 | fprintf(stderr, "closefrom: %s\n", msg); | ||
31 | exit(1); | ||
32 | } | ||
33 | |||
34 | int | ||
35 | main(void) | ||
36 | { | ||
37 | int i, max, fds[NUM_OPENS]; | ||
38 | char buf[512]; | ||
39 | |||
40 | for (i = 0; i < NUM_OPENS; i++) | ||
41 | if ((fds[i] = open("/dev/null", "r")) == -1) | ||
42 | exit(0); /* can't test */ | ||
43 | max = i - 1; | ||
44 | |||
45 | /* should close last fd only */ | ||
46 | closefrom(fds[max]); | ||
47 | if (close(fds[max]) != -1) | ||
48 | fail("failed to close highest fd"); | ||
49 | |||
50 | /* make sure we can still use remaining descriptors */ | ||
51 | for (i = 0; i < max; i++) | ||
52 | if (read(fds[i], buf, sizeof(buf)) == -1) | ||
53 | fail("closed descriptors it should not have"); | ||
54 | |||
55 | /* should close all fds */ | ||
56 | closefrom(fds[0]); | ||
57 | for (i = 0; i < NUM_OPENS; i++) | ||
58 | if (close(fds[i]) != -1) | ||
59 | fail("failed to close from lowest fd"); | ||
60 | } | ||
diff --git a/openbsd-compat/regress/snprintftest.c b/openbsd-compat/regress/snprintftest.c new file mode 100644 index 000000000..4ca63e180 --- /dev/null +++ b/openbsd-compat/regress/snprintftest.c | |||
@@ -0,0 +1,73 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2005 Darren Tucker | ||
3 | * Copyright (c) 2005 Damien Miller | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | #define BUFSZ 2048 | ||
19 | |||
20 | #include <sys/types.h> | ||
21 | #include <stdlib.h> | ||
22 | #include <stdio.h> | ||
23 | #include <stdarg.h> | ||
24 | #include <string.h> | ||
25 | |||
26 | static int failed = 0; | ||
27 | |||
28 | static void | ||
29 | fail(const char *m) | ||
30 | { | ||
31 | fprintf(stderr, "snprintftest: %s\n", m); | ||
32 | failed = 1; | ||
33 | } | ||
34 | |||
35 | int x_snprintf(char *str, size_t count, const char *fmt, ...) | ||
36 | { | ||
37 | size_t ret; | ||
38 | va_list ap; | ||
39 | |||
40 | va_start(ap, fmt); | ||
41 | ret = vsnprintf(str, count, fmt, ap); | ||
42 | va_end(ap); | ||
43 | return ret; | ||
44 | } | ||
45 | |||
46 | int | ||
47 | main(void) | ||
48 | { | ||
49 | char b[5]; | ||
50 | char *src; | ||
51 | |||
52 | snprintf(b,5,"123456789"); | ||
53 | if (b[4] != '\0') | ||
54 | fail("snprintf does not correctly terminate long strings"); | ||
55 | |||
56 | /* check for read overrun on unterminated string */ | ||
57 | if ((src = malloc(BUFSZ)) == NULL) { | ||
58 | fail("malloc failed"); | ||
59 | } else { | ||
60 | memset(src, 'a', BUFSZ); | ||
61 | snprintf(b, sizeof(b), "%.*s", 1, src); | ||
62 | if (strcmp(b, "a") != 0) | ||
63 | fail("failed with length limit '%%.s'"); | ||
64 | } | ||
65 | |||
66 | /* check that snprintf and vsnprintf return sane values */ | ||
67 | if (snprintf(b, 1, "%s %d", "hello", 12345) != 11) | ||
68 | fail("snprintf does not return required length"); | ||
69 | if (x_snprintf(b, 1, "%s %d", "hello", 12345) != 11) | ||
70 | fail("vsnprintf does not return required length"); | ||
71 | |||
72 | return failed; | ||
73 | } | ||
diff --git a/openbsd-compat/regress/strduptest.c b/openbsd-compat/regress/strduptest.c new file mode 100644 index 000000000..7f6d779be --- /dev/null +++ b/openbsd-compat/regress/strduptest.c | |||
@@ -0,0 +1,45 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2005 Darren Tucker | ||
3 | * | ||
4 | * Permission to use, copy, modify, and distribute this software for any | ||
5 | * purpose with or without fee is hereby granted, provided that the above | ||
6 | * copyright notice and this permission notice appear in all copies. | ||
7 | * | ||
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
15 | */ | ||
16 | |||
17 | #include <stdlib.h> | ||
18 | #include <string.h> | ||
19 | |||
20 | static int fail = 0; | ||
21 | |||
22 | void | ||
23 | test(const char *a) | ||
24 | { | ||
25 | char *b; | ||
26 | |||
27 | b = strdup(a); | ||
28 | if (b == 0) { | ||
29 | fail = 1; | ||
30 | return; | ||
31 | } | ||
32 | if (strcmp(a, b) != 0) | ||
33 | fail = 1; | ||
34 | free(b); | ||
35 | } | ||
36 | |||
37 | int | ||
38 | main(void) | ||
39 | { | ||
40 | test(""); | ||
41 | test("a"); | ||
42 | test("\0"); | ||
43 | test("abcdefghijklmnopqrstuvwxyz"); | ||
44 | return fail; | ||
45 | } | ||
diff --git a/openbsd-compat/regress/strtonumtest.c b/openbsd-compat/regress/strtonumtest.c new file mode 100644 index 000000000..cb8585129 --- /dev/null +++ b/openbsd-compat/regress/strtonumtest.c | |||
@@ -0,0 +1,66 @@ | |||
1 | /* $OpenBSD: strtonumtest.c,v 1.1 2004/08/03 20:38:36 otto Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2004 Otto Moerbeek <otto@drijf.net> | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | /* OPENBSD ORIGINAL: regress/lib/libc/strtonum/strtonumtest.c */ | ||
19 | |||
20 | #include <limits.h> | ||
21 | #include <stdio.h> | ||
22 | #include <stdlib.h> | ||
23 | |||
24 | int fail; | ||
25 | |||
26 | void | ||
27 | test(const char *p, long long lb, long long ub, int ok) | ||
28 | { | ||
29 | long long val; | ||
30 | const char *q; | ||
31 | |||
32 | val = strtonum(p, lb, ub, &q); | ||
33 | if (ok && q != NULL) { | ||
34 | fprintf(stderr, "%s [%lld-%lld] ", p, lb, ub); | ||
35 | fprintf(stderr, "NUMBER NOT ACCEPTED %s\n", q); | ||
36 | fail = 1; | ||
37 | } else if (!ok && q == NULL) { | ||
38 | fprintf(stderr, "%s [%lld-%lld] %lld ", p, lb, ub, val); | ||
39 | fprintf(stderr, "NUMBER ACCEPTED\n"); | ||
40 | fail = 1; | ||
41 | } | ||
42 | } | ||
43 | |||
44 | int main(int argc, char *argv[]) | ||
45 | { | ||
46 | test("1", 0, 10, 1); | ||
47 | test("0", -2, 5, 1); | ||
48 | test("0", 2, 5, 0); | ||
49 | test("0", 2, LLONG_MAX, 0); | ||
50 | test("-2", 0, LLONG_MAX, 0); | ||
51 | test("0", -5, LLONG_MAX, 1); | ||
52 | test("-3", -3, LLONG_MAX, 1); | ||
53 | test("-9223372036854775808", LLONG_MIN, LLONG_MAX, 1); | ||
54 | test("9223372036854775807", LLONG_MIN, LLONG_MAX, 1); | ||
55 | test("-9223372036854775809", LLONG_MIN, LLONG_MAX, 0); | ||
56 | test("9223372036854775808", LLONG_MIN, LLONG_MAX, 0); | ||
57 | test("1000000000000000000000000", LLONG_MIN, LLONG_MAX, 0); | ||
58 | test("-1000000000000000000000000", LLONG_MIN, LLONG_MAX, 0); | ||
59 | test("-2", 10, -1, 0); | ||
60 | test("-2", -10, -1, 1); | ||
61 | test("-20", -10, -1, 0); | ||
62 | test("20", -10, -1, 0); | ||
63 | |||
64 | return (fail); | ||
65 | } | ||
66 | |||