diff options
author | Ray Lai <ray@cvs.openbsd.org> | 2007-05-17 03:02:33 +0000 |
---|---|---|
committer | Ray Lai <ray@cvs.openbsd.org> | 2007-05-17 03:02:33 +0000 |
commit | 4eb99b5c5b130039d18fd378cf72b69985e8e3c8 (patch) | |
tree | 1c86f3e382ad65655a734b7d04556b2dcaf42e5e /regress/lib/libc | |
parent | 5071a4fdc95bde981ef8f68d378b7435c8ad957d (diff) |
Add basename(3) and dirname(3) regression tests, from tbert.
OK otto@.
Diffstat (limited to 'regress/lib/libc')
-rw-r--r-- | regress/lib/libc/Makefile | 6 | ||||
-rw-r--r-- | regress/lib/libc/basename/Makefile | 3 | ||||
-rw-r--r-- | regress/lib/libc/basename/basename_test.c | 79 | ||||
-rw-r--r-- | regress/lib/libc/dirname/Makefile | 3 | ||||
-rw-r--r-- | regress/lib/libc/dirname/dirname_test.c | 82 |
5 files changed, 170 insertions, 3 deletions
diff --git a/regress/lib/libc/Makefile b/regress/lib/libc/Makefile index 04970c7e163..e3e29ddd692 100644 --- a/regress/lib/libc/Makefile +++ b/regress/lib/libc/Makefile @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile,v 1.24 2006/09/29 11:00:24 otto Exp $ +# $OpenBSD: Makefile,v 1.25 2007/05/17 03:02:32 ray Exp $ -SUBDIR+= _setjmp alloca atexit db getaddrinfo getcap getopt_long hsearch longjmp -SUBDIR+= locale malloc +SUBDIR+= _setjmp alloca atexit basename db dirname getaddrinfo +SUBDIR+= getcap getopt_long hsearch longjmp locale malloc SUBDIR+= netdb popen regex setjmp setjmp-signal sigreturn sigsetjmp SUBDIR+= sprintf strerror strtod strtonum telldir time vis diff --git a/regress/lib/libc/basename/Makefile b/regress/lib/libc/basename/Makefile new file mode 100644 index 00000000000..958b06fd11d --- /dev/null +++ b/regress/lib/libc/basename/Makefile @@ -0,0 +1,3 @@ +PROG=basename_test + +.include <bsd.regress.mk> diff --git a/regress/lib/libc/basename/basename_test.c b/regress/lib/libc/basename/basename_test.c new file mode 100644 index 00000000000..34e138c726e --- /dev/null +++ b/regress/lib/libc/basename/basename_test.c @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2007 Bret S. Lambert <blambert@gsipt.net> + * + * Public domain. + */ + +#include <sys/param.h> + +#include <libgen.h> +#include <stdio.h> +#include <string.h> +#include <limits.h> +#include <errno.h> + +int +main(void) +{ + char path[2 * MAXPATHLEN]; + const char *dir = "junk/"; + const char *fname = "file.name.ext"; + char *str; + int i; + + /* Test normal functioning */ + strlcpy(path, "/", sizeof(path)); + strlcat(path, dir, sizeof(path)); + strlcat(path, fname, sizeof(path)); + str = basename(path); + if (strcmp(str, fname) != 0) + goto fail; + + /* + * There are four states that require special handling: + * + * 1) path is NULL + * 2) path is the empty string + * 3) path is composed entirely of slashes + * 4) the resulting name is larger than MAXPATHLEN + * + * The first two cases require that a pointer + * to the string "." be returned. + * + * The third case requires that a pointer + * to the string "/" be returned. + * + * The final case requires that NULL be returned + * and errno * be set to ENAMETOOLONG. + */ + /* Case 1 */ + str = basename(NULL); + if (strcmp(str, ".") != 0) + goto fail; + + /* Case 2 */ + strlcpy(path, "", sizeof(path)); + str = basename(path); + if (strcmp(str, ".") != 0) + goto fail; + + /* Case 3 */ + for (i = 0; i < MAXPATHLEN - 1; i++) + strlcat(path, "/", sizeof(path)); /* path cleared above */ + str = basename(path); + if (strcmp(str, "/") != 0) + goto fail; + + /* Case 4 */ + strlcpy(path, "/", sizeof(path)); + strlcat(path, dir, sizeof(path)); + for (i = 0; i <= MAXPATHLEN; i += sizeof(fname)) + strlcat(path, fname, sizeof(path)); + str = basename(path); + if (str != NULL || errno != ENAMETOOLONG) + goto fail; + + return (0); +fail: + return (1); +} diff --git a/regress/lib/libc/dirname/Makefile b/regress/lib/libc/dirname/Makefile new file mode 100644 index 00000000000..fc152df9545 --- /dev/null +++ b/regress/lib/libc/dirname/Makefile @@ -0,0 +1,3 @@ +PROG=dirname_test + +.include <bsd.regress.mk> diff --git a/regress/lib/libc/dirname/dirname_test.c b/regress/lib/libc/dirname/dirname_test.c new file mode 100644 index 00000000000..7d0b0e221f6 --- /dev/null +++ b/regress/lib/libc/dirname/dirname_test.c @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2007 Bret S. Lambert <blambert@gsipt.net> + * + * Public domain. + */ + +#include <sys/param.h> + +#include <libgen.h> +#include <stdio.h> +#include <string.h> +#include <limits.h> +#include <errno.h> + +int +main(void) +{ + char path[2 * MAXPATHLEN]; + char dname[128]; + const char *dir = "junk"; + const char *fname = "/file.name.ext"; + char *str; + int i; + + /* Test normal functioning */ + strlcpy(path, "/", sizeof(path)); + strlcpy(dname, "/", sizeof(dname)); + strlcat(path, dir, sizeof(path)); + strlcat(dname, dir, sizeof(dname)); + strlcat(path, fname, sizeof(path)); + str = dirname(path); + if (strcmp(str, dname) != 0) + goto fail; + + /* + * There are four states that require special handling: + * + * 1) path is NULL + * 2) path is the empty string + * 3) path is composed entirely of slashes + * 4) the resulting name is larger than MAXPATHLEN + * + * The first two cases require that a pointer + * to the string "." be returned. + * + * The third case requires that a pointer + * to the string "/" be returned. + * + * The final case requires that NULL be returned + * and errno * be set to ENAMETOOLONG. + */ + /* Case 1 */ + str = dirname(NULL); + if (strcmp(str, ".") != 0) + goto fail; + + /* Case 2 */ + strlcpy(path, "", sizeof(path)); + str = dirname(path); + if (strcmp(str, ".") != 0) + goto fail; + + /* Case 3 */ + for (i = 0; i < MAXPATHLEN - 1; i++) + strlcat(path, "/", sizeof(path)); /* path cleared above */ + str = dirname(path); + if (strcmp(str, "/") != 0) + goto fail; + + /* Case 4 */ + strlcpy(path, "/", sizeof(path)); /* reset path */ + for (i = 0; i <= MAXPATHLEN; i += sizeof(dir)) + strlcat(path, dir, sizeof(path)); + strlcat(path, fname, sizeof(path)); + str = dirname(path); + if (str != NULL || errno != ENAMETOOLONG) + goto fail; + + return (0); +fail: + return (1); +} |