From 9a364ffb85cbe4c22d7e8895aeebced0acd0ed7a Mon Sep 17 00:00:00 2001 From: ray <> Date: Thu, 17 May 2007 03:02:32 +0000 Subject: Add basename(3) and dirname(3) regression tests, from tbert. OK otto@. --- src/regress/lib/libc/Makefile | 6 +- src/regress/lib/libc/basename/Makefile | 3 + src/regress/lib/libc/basename/basename_test.c | 79 ++++++++++++++++++++++++++ src/regress/lib/libc/dirname/Makefile | 3 + src/regress/lib/libc/dirname/dirname_test.c | 82 +++++++++++++++++++++++++++ 5 files changed, 170 insertions(+), 3 deletions(-) create mode 100644 src/regress/lib/libc/basename/Makefile create mode 100644 src/regress/lib/libc/basename/basename_test.c create mode 100644 src/regress/lib/libc/dirname/Makefile create mode 100644 src/regress/lib/libc/dirname/dirname_test.c (limited to 'src') diff --git a/src/regress/lib/libc/Makefile b/src/regress/lib/libc/Makefile index 04970c7e16..e3e29ddd69 100644 --- a/src/regress/lib/libc/Makefile +++ b/src/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/src/regress/lib/libc/basename/Makefile b/src/regress/lib/libc/basename/Makefile new file mode 100644 index 0000000000..958b06fd11 --- /dev/null +++ b/src/regress/lib/libc/basename/Makefile @@ -0,0 +1,3 @@ +PROG=basename_test + +.include diff --git a/src/regress/lib/libc/basename/basename_test.c b/src/regress/lib/libc/basename/basename_test.c new file mode 100644 index 0000000000..34e138c726 --- /dev/null +++ b/src/regress/lib/libc/basename/basename_test.c @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2007 Bret S. Lambert + * + * Public domain. + */ + +#include + +#include +#include +#include +#include +#include + +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/src/regress/lib/libc/dirname/Makefile b/src/regress/lib/libc/dirname/Makefile new file mode 100644 index 0000000000..fc152df954 --- /dev/null +++ b/src/regress/lib/libc/dirname/Makefile @@ -0,0 +1,3 @@ +PROG=dirname_test + +.include diff --git a/src/regress/lib/libc/dirname/dirname_test.c b/src/regress/lib/libc/dirname/dirname_test.c new file mode 100644 index 0000000000..7d0b0e221f --- /dev/null +++ b/src/regress/lib/libc/dirname/dirname_test.c @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2007 Bret S. Lambert + * + * Public domain. + */ + +#include + +#include +#include +#include +#include +#include + +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); +} -- cgit v1.2.3-55-g6feb