summaryrefslogtreecommitdiff
path: root/openbsd-compat/regress
diff options
context:
space:
mode:
Diffstat (limited to 'openbsd-compat/regress')
-rw-r--r--openbsd-compat/regress/Makefile.in39
-rw-r--r--openbsd-compat/regress/snprintftest.c72
-rw-r--r--openbsd-compat/regress/strduptest.c42
-rw-r--r--openbsd-compat/regress/strtonumtest.c66
4 files changed, 219 insertions, 0 deletions
diff --git a/openbsd-compat/regress/Makefile.in b/openbsd-compat/regress/Makefile.in
new file mode 100644
index 000000000..51383a777
--- /dev/null
+++ b/openbsd-compat/regress/Makefile.in
@@ -0,0 +1,39 @@
1# $Id: Makefile.in,v 1.1 2006/02/19 11:50:20 dtucker Exp $
2
3sysconfdir=@sysconfdir@
4piddir=@piddir@
5srcdir=@srcdir@
6top_srcdir=@top_srcdir@
7
8VPATH=@srcdir@
9CC=@CC@
10LD=@LD@
11CFLAGS=@CFLAGS@
12CPPFLAGS=-I. -I.. -I$(srcdir) -I$(srcdir)/.. @CPPFLAGS@ @DEFS@
13LIBS=@LIBS@
14LDFLAGS=-L.. -lopenbsd-compat @LDFLAGS@
15
16LIBCOMPAT=../libopenbsd-compat.a
17TESTPROGS=strtonumtest strduptest snprintftest
18
19all: t-exec ${OTHERTESTS}
20
21t-exec: $(TESTPROGS)
22 @echo running compat regress tests
23 @for TEST in ""$?; do \
24 echo "run test $${TEST}" ... 1>&2; \
25 ./$${TEST} || exit $$? ; \
26 done
27 @echo finished compat regress tests
28
29strtonumtest: strtonumtest.c $(LIBCOMPAT)
30 $(CC) $(CFLAGS) $(CPPFLAGS) -o strtonumtest $< $(LDFLAGS)
31
32strduptest: strduptest.c $(LIBCOMPAT)
33 $(CC) $(CFLAGS) $(CPPFLAGS) -o strduptest $< $(LDFLAGS)
34
35clean:
36 rm -f *.o *.a core $(TESTPROGS) valid.out
37
38distclean: clean
39 rm -f Makefile *~
diff --git a/openbsd-compat/regress/snprintftest.c b/openbsd-compat/regress/snprintftest.c
new file mode 100644
index 000000000..e25bf223f
--- /dev/null
+++ b/openbsd-compat/regress/snprintftest.c
@@ -0,0 +1,72 @@
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
25static int failed = 0;
26
27static void
28fail(const char *m)
29{
30 fprintf(stderr, "%s", m);
31 failed = 1;
32}
33
34int x_snprintf(char *str, size_t count, const char *fmt, ...)
35{
36 size_t ret;
37 va_list ap;
38
39 va_start(ap, fmt);
40 ret = vsnprintf(str, count, fmt, ap);
41 va_end(ap);
42 return ret;
43}
44
45int
46main(void)
47{
48 char b[5];
49 char *src;
50
51 snprintf(b,5,"123456789");
52 if (b[4] != '\0')
53 fail("snprintf does not correctly terminate long strings");
54
55 /* check for read overrun on unterminated string */
56 if ((src = malloc(BUFSZ)) == NULL) {
57 fail("malloc failed");
58 } else {
59 memset(src, 'a', BUFSZ);
60 snprintf(b, sizeof(b), "%.*s", 1, src);
61 if (strcmp(b, "a") != 0)
62 fail("failed with length limit '%%.s'");
63 }
64
65 /* check that snprintf and vsnprintf return sane values */
66 if (snprintf(b, 1, "%s %d", "hello", 12345) != 11)
67 fail("snprintf does not return required length");
68 if (x_snprintf(b, 1, "%s %d", "hello", 12345) != 11)
69 fail("vsnprintf does not return required length");
70
71 return failed;
72}
diff --git a/openbsd-compat/regress/strduptest.c b/openbsd-compat/regress/strduptest.c
new file mode 100644
index 000000000..664a48ef4
--- /dev/null
+++ b/openbsd-compat/regress/strduptest.c
@@ -0,0 +1,42 @@
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
17static int fail = 0;
18
19void
20test(const char *a)
21{
22 char *b;
23
24 b = strdup(a);
25 if (b == 0) {
26 fail = 1;
27 return;
28 }
29 if (strcmp(a, b) != 0)
30 fail = 1;
31 free(b);
32}
33
34int
35main(void)
36{
37 test("");
38 test("a");
39 test("\0");
40 test("abcdefghijklmnopqrstuvwxyz");
41 return fail;
42}
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
24int fail;
25
26void
27test(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
44int 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