diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2014-12-02 20:23:06 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2014-12-02 20:23:06 +0000 |
commit | 630c11bd7ee3d0c52e6e143f3f52d207eb2dee7b (patch) | |
tree | 281b53de5433fef627c5ac4049b8e7ca54e3ff2f /regress/lib/libc | |
parent | eed9dcb825905471e4cc4a3f1e2570d610554c79 (diff) |
Add simple strlcpy regress
Diffstat (limited to 'regress/lib/libc')
-rw-r--r-- | regress/lib/libc/Makefile | 6 | ||||
-rw-r--r-- | regress/lib/libc/strlcpy/Makefile | 5 | ||||
-rw-r--r-- | regress/lib/libc/strlcpy/strlcpytest.c | 115 |
3 files changed, 123 insertions, 3 deletions
diff --git a/regress/lib/libc/Makefile b/regress/lib/libc/Makefile index 16bd49dcd6e..0c81c5cea56 100644 --- a/regress/lib/libc/Makefile +++ b/regress/lib/libc/Makefile @@ -1,12 +1,12 @@ -# $OpenBSD: Makefile,v 1.45 2014/12/02 17:48:34 millert Exp $ +# $OpenBSD: Makefile,v 1.46 2014/12/02 20:23:05 millert Exp $ SUBDIR+= _setjmp alloca arc4random-fork SUBDIR+= atexit basename cephes cxa-atexit db dirname env SUBDIR+= explicit_bzero fmemopen fnmatch fpclassify getcap getopt_long glob SUBDIR+= hsearch longjmp locale malloc mkstemp modf netdb open_memstream SUBDIR+= orientation popen printf -SUBDIR+= regex setjmp setjmp-signal sigreturn sigsetjmp sprintf -SUBDIR+= stdio_threading stpncpy strerror strlcat strnlen strtod strtol strtonum +SUBDIR+= regex setjmp setjmp-signal sigreturn sigsetjmp sprintf stdio_threading +SUBDIR+= stpncpy strerror strlcat strlcpy strnlen strtod strtol strtonum SUBDIR+= telldir time timingsafe vis .if defined(REGRESS_FULL) || make(clean) || make(cleandir) || make(obj) diff --git a/regress/lib/libc/strlcpy/Makefile b/regress/lib/libc/strlcpy/Makefile new file mode 100644 index 00000000000..921354432b4 --- /dev/null +++ b/regress/lib/libc/strlcpy/Makefile @@ -0,0 +1,5 @@ +# $OpenBSD: Makefile,v 1.1 2014/12/02 20:23:05 millert Exp $ + +PROG= strlcpytest + +.include <bsd.regress.mk> diff --git a/regress/lib/libc/strlcpy/strlcpytest.c b/regress/lib/libc/strlcpy/strlcpytest.c new file mode 100644 index 00000000000..f2760c2f99f --- /dev/null +++ b/regress/lib/libc/strlcpy/strlcpytest.c @@ -0,0 +1,115 @@ +/* $OpenBSD: strlcpytest.c,v 1.1 2014/12/02 20:23:05 millert Exp $ */ + +/* + * Copyright (c) 2014 Todd C. Miller <Todd.Miller@courtesan.com> + * + * 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 <sys/types.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +int main(int argc, char *argv[]) +{ + char *buf, *buf2, *cp, *ep; + int failures = 0; + size_t len, bufsize; + + /* Enable malloc security options. */ + setenv("MALLOC_OPTIONS", "S", 0); + + bufsize = getpagesize(); /* trigger guard pages easily */ + buf = malloc(bufsize); + buf2 = malloc(bufsize); + if (buf == NULL || buf2 == NULL) { + fprintf(stderr, "unable to allocate memory\n"); + return 1; + } + memset(buf, 'a', bufsize); + ep = buf + bufsize; + + /* Test copying to a zero-length NULL buffer. */ + len = strlcpy(NULL, "abcd", 0); + if (len != 4) { + fprintf(stderr, "strlcpy: failed NULL buffer test (1a)"); + failures++; + } + + /* Test copying small string to a large buffer. */ + len = strlcpy(buf, "abcd", bufsize); + if (len != 4) { + fprintf(stderr, "strlcpy: failed large buffer test (2a)"); + failures++; + } + /* Make sure we only wrote where expected. */ + if (memcmp(buf, "abcd", sizeof("abcd")) != 0) { + fprintf(stderr, "strlcpy: failed large buffer test (2b)"); + failures++; + } + for (cp = buf + len + 1; cp < ep; cp++) { + if (*cp != 'a') { + fprintf(stderr, "strlcpy: failed large buffer test (2c)"); + failures++; + break; + } + } + + /* Test copying large string to a small buffer. */ + memset(buf, 'a', bufsize); + memset(buf2, 'x', bufsize - 1); + buf2[bufsize - 1] = '\0'; + len = strlcpy(buf, buf2, bufsize / 2); + if (len != bufsize - 1) { + fprintf(stderr, "strlcpy: failed small buffer test (3a)"); + failures++; + } + /* Make sure we only wrote where expected. */ + len = (bufsize / 2) - 1; + if (memcmp(buf, buf2, len) != 0 || buf[len] != '\0') { + fprintf(stderr, "strlcpy: failed small buffer test (3b)"); + failures++; + } + for (cp = buf + len + 1; cp < ep; cp++) { + if (*cp != 'a') { + fprintf(stderr, "strlcpy: failed small buffer test (3c)"); + failures++; + break; + } + } + + /* Test copying to a 1-byte buffer. */ + memset(buf, 'a', bufsize); + len = strlcpy(buf, "abcd", 1); + if (len != 4) { + fprintf(stderr, "strlcpy: failed 1-byte buffer test (4a)"); + failures++; + } + /* Make sure we only wrote where expected. */ + if (buf[0] != '\0') { + fprintf(stderr, "strlcpy: failed 1-byte buffer test (4b)"); + failures++; + } + for (cp = buf + 1; cp < ep; cp++) { + if (*cp != 'a') { + fprintf(stderr, "strlcpy: failed 1-byte buffer test (4c)"); + failures++; + break; + } + } + + return failures; +} |