diff options
author | millert <> | 2014-12-02 20:23:05 +0000 |
---|---|---|
committer | millert <> | 2014-12-02 20:23:05 +0000 |
commit | 8b49ba40f09f8e00300ae25e95d801db5e53af0a (patch) | |
tree | e5eceb0682ae43096a4d104b795aa5bddb70bc34 /src | |
parent | 6f5388d11ca7552025496dc1e465ad003e94f27e (diff) | |
download | openbsd-8b49ba40f09f8e00300ae25e95d801db5e53af0a.tar.gz openbsd-8b49ba40f09f8e00300ae25e95d801db5e53af0a.tar.bz2 openbsd-8b49ba40f09f8e00300ae25e95d801db5e53af0a.zip |
Add simple strlcpy regress
Diffstat (limited to 'src')
-rw-r--r-- | src/regress/lib/libc/Makefile | 6 | ||||
-rw-r--r-- | src/regress/lib/libc/strlcpy/Makefile | 5 | ||||
-rw-r--r-- | src/regress/lib/libc/strlcpy/strlcpytest.c | 115 |
3 files changed, 123 insertions, 3 deletions
diff --git a/src/regress/lib/libc/Makefile b/src/regress/lib/libc/Makefile index 16bd49dcd6..0c81c5cea5 100644 --- a/src/regress/lib/libc/Makefile +++ b/src/regress/lib/libc/Makefile | |||
@@ -1,12 +1,12 @@ | |||
1 | # $OpenBSD: Makefile,v 1.45 2014/12/02 17:48:34 millert Exp $ | 1 | # $OpenBSD: Makefile,v 1.46 2014/12/02 20:23:05 millert Exp $ |
2 | 2 | ||
3 | SUBDIR+= _setjmp alloca arc4random-fork | 3 | SUBDIR+= _setjmp alloca arc4random-fork |
4 | SUBDIR+= atexit basename cephes cxa-atexit db dirname env | 4 | SUBDIR+= atexit basename cephes cxa-atexit db dirname env |
5 | SUBDIR+= explicit_bzero fmemopen fnmatch fpclassify getcap getopt_long glob | 5 | SUBDIR+= explicit_bzero fmemopen fnmatch fpclassify getcap getopt_long glob |
6 | SUBDIR+= hsearch longjmp locale malloc mkstemp modf netdb open_memstream | 6 | SUBDIR+= hsearch longjmp locale malloc mkstemp modf netdb open_memstream |
7 | SUBDIR+= orientation popen printf | 7 | SUBDIR+= orientation popen printf |
8 | SUBDIR+= regex setjmp setjmp-signal sigreturn sigsetjmp sprintf | 8 | SUBDIR+= regex setjmp setjmp-signal sigreturn sigsetjmp sprintf stdio_threading |
9 | SUBDIR+= stdio_threading stpncpy strerror strlcat strnlen strtod strtol strtonum | 9 | SUBDIR+= stpncpy strerror strlcat strlcpy strnlen strtod strtol strtonum |
10 | SUBDIR+= telldir time timingsafe vis | 10 | SUBDIR+= telldir time timingsafe vis |
11 | 11 | ||
12 | .if defined(REGRESS_FULL) || make(clean) || make(cleandir) || make(obj) | 12 | .if defined(REGRESS_FULL) || make(clean) || make(cleandir) || make(obj) |
diff --git a/src/regress/lib/libc/strlcpy/Makefile b/src/regress/lib/libc/strlcpy/Makefile new file mode 100644 index 0000000000..921354432b --- /dev/null +++ b/src/regress/lib/libc/strlcpy/Makefile | |||
@@ -0,0 +1,5 @@ | |||
1 | # $OpenBSD: Makefile,v 1.1 2014/12/02 20:23:05 millert Exp $ | ||
2 | |||
3 | PROG= strlcpytest | ||
4 | |||
5 | .include <bsd.regress.mk> | ||
diff --git a/src/regress/lib/libc/strlcpy/strlcpytest.c b/src/regress/lib/libc/strlcpy/strlcpytest.c new file mode 100644 index 0000000000..f2760c2f99 --- /dev/null +++ b/src/regress/lib/libc/strlcpy/strlcpytest.c | |||
@@ -0,0 +1,115 @@ | |||
1 | /* $OpenBSD: strlcpytest.c,v 1.1 2014/12/02 20:23:05 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2014 Todd C. Miller <Todd.Miller@courtesan.com> | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
17 | */ | ||
18 | |||
19 | #include <sys/types.h> | ||
20 | |||
21 | #include <stdio.h> | ||
22 | #include <stdlib.h> | ||
23 | #include <string.h> | ||
24 | #include <unistd.h> | ||
25 | |||
26 | int main(int argc, char *argv[]) | ||
27 | { | ||
28 | char *buf, *buf2, *cp, *ep; | ||
29 | int failures = 0; | ||
30 | size_t len, bufsize; | ||
31 | |||
32 | /* Enable malloc security options. */ | ||
33 | setenv("MALLOC_OPTIONS", "S", 0); | ||
34 | |||
35 | bufsize = getpagesize(); /* trigger guard pages easily */ | ||
36 | buf = malloc(bufsize); | ||
37 | buf2 = malloc(bufsize); | ||
38 | if (buf == NULL || buf2 == NULL) { | ||
39 | fprintf(stderr, "unable to allocate memory\n"); | ||
40 | return 1; | ||
41 | } | ||
42 | memset(buf, 'a', bufsize); | ||
43 | ep = buf + bufsize; | ||
44 | |||
45 | /* Test copying to a zero-length NULL buffer. */ | ||
46 | len = strlcpy(NULL, "abcd", 0); | ||
47 | if (len != 4) { | ||
48 | fprintf(stderr, "strlcpy: failed NULL buffer test (1a)"); | ||
49 | failures++; | ||
50 | } | ||
51 | |||
52 | /* Test copying small string to a large buffer. */ | ||
53 | len = strlcpy(buf, "abcd", bufsize); | ||
54 | if (len != 4) { | ||
55 | fprintf(stderr, "strlcpy: failed large buffer test (2a)"); | ||
56 | failures++; | ||
57 | } | ||
58 | /* Make sure we only wrote where expected. */ | ||
59 | if (memcmp(buf, "abcd", sizeof("abcd")) != 0) { | ||
60 | fprintf(stderr, "strlcpy: failed large buffer test (2b)"); | ||
61 | failures++; | ||
62 | } | ||
63 | for (cp = buf + len + 1; cp < ep; cp++) { | ||
64 | if (*cp != 'a') { | ||
65 | fprintf(stderr, "strlcpy: failed large buffer test (2c)"); | ||
66 | failures++; | ||
67 | break; | ||
68 | } | ||
69 | } | ||
70 | |||
71 | /* Test copying large string to a small buffer. */ | ||
72 | memset(buf, 'a', bufsize); | ||
73 | memset(buf2, 'x', bufsize - 1); | ||
74 | buf2[bufsize - 1] = '\0'; | ||
75 | len = strlcpy(buf, buf2, bufsize / 2); | ||
76 | if (len != bufsize - 1) { | ||
77 | fprintf(stderr, "strlcpy: failed small buffer test (3a)"); | ||
78 | failures++; | ||
79 | } | ||
80 | /* Make sure we only wrote where expected. */ | ||
81 | len = (bufsize / 2) - 1; | ||
82 | if (memcmp(buf, buf2, len) != 0 || buf[len] != '\0') { | ||
83 | fprintf(stderr, "strlcpy: failed small buffer test (3b)"); | ||
84 | failures++; | ||
85 | } | ||
86 | for (cp = buf + len + 1; cp < ep; cp++) { | ||
87 | if (*cp != 'a') { | ||
88 | fprintf(stderr, "strlcpy: failed small buffer test (3c)"); | ||
89 | failures++; | ||
90 | break; | ||
91 | } | ||
92 | } | ||
93 | |||
94 | /* Test copying to a 1-byte buffer. */ | ||
95 | memset(buf, 'a', bufsize); | ||
96 | len = strlcpy(buf, "abcd", 1); | ||
97 | if (len != 4) { | ||
98 | fprintf(stderr, "strlcpy: failed 1-byte buffer test (4a)"); | ||
99 | failures++; | ||
100 | } | ||
101 | /* Make sure we only wrote where expected. */ | ||
102 | if (buf[0] != '\0') { | ||
103 | fprintf(stderr, "strlcpy: failed 1-byte buffer test (4b)"); | ||
104 | failures++; | ||
105 | } | ||
106 | for (cp = buf + 1; cp < ep; cp++) { | ||
107 | if (*cp != 'a') { | ||
108 | fprintf(stderr, "strlcpy: failed 1-byte buffer test (4c)"); | ||
109 | failures++; | ||
110 | break; | ||
111 | } | ||
112 | } | ||
113 | |||
114 | return failures; | ||
115 | } | ||