diff options
| author | ray <> | 2007-05-17 03:02:32 +0000 |
|---|---|---|
| committer | ray <> | 2007-05-17 03:02:32 +0000 |
| commit | 9a364ffb85cbe4c22d7e8895aeebced0acd0ed7a (patch) | |
| tree | 4b3e0d693c81e9708dde02d9bb9e3d83b03dd569 /src | |
| parent | 87de8e8e4ecf9054b2cc206a6c1fba1c14382b10 (diff) | |
| download | openbsd-9a364ffb85cbe4c22d7e8895aeebced0acd0ed7a.tar.gz openbsd-9a364ffb85cbe4c22d7e8895aeebced0acd0ed7a.tar.bz2 openbsd-9a364ffb85cbe4c22d7e8895aeebced0acd0ed7a.zip | |
Add basename(3) and dirname(3) regression tests, from tbert.
OK otto@.
Diffstat (limited to 'src')
| -rw-r--r-- | src/regress/lib/libc/Makefile | 6 | ||||
| -rw-r--r-- | src/regress/lib/libc/basename/Makefile | 3 | ||||
| -rw-r--r-- | src/regress/lib/libc/basename/basename_test.c | 79 | ||||
| -rw-r--r-- | src/regress/lib/libc/dirname/Makefile | 3 | ||||
| -rw-r--r-- | src/regress/lib/libc/dirname/dirname_test.c | 82 |
5 files changed, 170 insertions, 3 deletions
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 @@ | |||
| 1 | # $OpenBSD: Makefile,v 1.24 2006/09/29 11:00:24 otto Exp $ | 1 | # $OpenBSD: Makefile,v 1.25 2007/05/17 03:02:32 ray Exp $ |
| 2 | 2 | ||
| 3 | SUBDIR+= _setjmp alloca atexit db getaddrinfo getcap getopt_long hsearch longjmp | 3 | SUBDIR+= _setjmp alloca atexit basename db dirname getaddrinfo |
| 4 | SUBDIR+= locale malloc | 4 | SUBDIR+= getcap getopt_long hsearch longjmp locale malloc |
| 5 | SUBDIR+= netdb popen regex setjmp setjmp-signal sigreturn sigsetjmp | 5 | SUBDIR+= netdb popen regex setjmp setjmp-signal sigreturn sigsetjmp |
| 6 | SUBDIR+= sprintf strerror strtod strtonum telldir time vis | 6 | SUBDIR+= sprintf strerror strtod strtonum telldir time vis |
| 7 | 7 | ||
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 @@ | |||
| 1 | PROG=basename_test | ||
| 2 | |||
| 3 | .include <bsd.regress.mk> | ||
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 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2007 Bret S. Lambert <blambert@gsipt.net> | ||
| 3 | * | ||
| 4 | * Public domain. | ||
| 5 | */ | ||
| 6 | |||
| 7 | #include <sys/param.h> | ||
| 8 | |||
| 9 | #include <libgen.h> | ||
| 10 | #include <stdio.h> | ||
| 11 | #include <string.h> | ||
| 12 | #include <limits.h> | ||
| 13 | #include <errno.h> | ||
| 14 | |||
| 15 | int | ||
| 16 | main(void) | ||
| 17 | { | ||
| 18 | char path[2 * MAXPATHLEN]; | ||
| 19 | const char *dir = "junk/"; | ||
| 20 | const char *fname = "file.name.ext"; | ||
| 21 | char *str; | ||
| 22 | int i; | ||
| 23 | |||
| 24 | /* Test normal functioning */ | ||
| 25 | strlcpy(path, "/", sizeof(path)); | ||
| 26 | strlcat(path, dir, sizeof(path)); | ||
| 27 | strlcat(path, fname, sizeof(path)); | ||
| 28 | str = basename(path); | ||
| 29 | if (strcmp(str, fname) != 0) | ||
| 30 | goto fail; | ||
| 31 | |||
| 32 | /* | ||
| 33 | * There are four states that require special handling: | ||
| 34 | * | ||
| 35 | * 1) path is NULL | ||
| 36 | * 2) path is the empty string | ||
| 37 | * 3) path is composed entirely of slashes | ||
| 38 | * 4) the resulting name is larger than MAXPATHLEN | ||
| 39 | * | ||
| 40 | * The first two cases require that a pointer | ||
| 41 | * to the string "." be returned. | ||
| 42 | * | ||
| 43 | * The third case requires that a pointer | ||
| 44 | * to the string "/" be returned. | ||
| 45 | * | ||
| 46 | * The final case requires that NULL be returned | ||
| 47 | * and errno * be set to ENAMETOOLONG. | ||
| 48 | */ | ||
| 49 | /* Case 1 */ | ||
| 50 | str = basename(NULL); | ||
| 51 | if (strcmp(str, ".") != 0) | ||
| 52 | goto fail; | ||
| 53 | |||
| 54 | /* Case 2 */ | ||
| 55 | strlcpy(path, "", sizeof(path)); | ||
| 56 | str = basename(path); | ||
| 57 | if (strcmp(str, ".") != 0) | ||
| 58 | goto fail; | ||
| 59 | |||
| 60 | /* Case 3 */ | ||
| 61 | for (i = 0; i < MAXPATHLEN - 1; i++) | ||
| 62 | strlcat(path, "/", sizeof(path)); /* path cleared above */ | ||
| 63 | str = basename(path); | ||
| 64 | if (strcmp(str, "/") != 0) | ||
| 65 | goto fail; | ||
| 66 | |||
| 67 | /* Case 4 */ | ||
| 68 | strlcpy(path, "/", sizeof(path)); | ||
| 69 | strlcat(path, dir, sizeof(path)); | ||
| 70 | for (i = 0; i <= MAXPATHLEN; i += sizeof(fname)) | ||
| 71 | strlcat(path, fname, sizeof(path)); | ||
| 72 | str = basename(path); | ||
| 73 | if (str != NULL || errno != ENAMETOOLONG) | ||
| 74 | goto fail; | ||
| 75 | |||
| 76 | return (0); | ||
| 77 | fail: | ||
| 78 | return (1); | ||
| 79 | } | ||
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 @@ | |||
| 1 | PROG=dirname_test | ||
| 2 | |||
| 3 | .include <bsd.regress.mk> | ||
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 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2007 Bret S. Lambert <blambert@gsipt.net> | ||
| 3 | * | ||
| 4 | * Public domain. | ||
| 5 | */ | ||
| 6 | |||
| 7 | #include <sys/param.h> | ||
| 8 | |||
| 9 | #include <libgen.h> | ||
| 10 | #include <stdio.h> | ||
| 11 | #include <string.h> | ||
| 12 | #include <limits.h> | ||
| 13 | #include <errno.h> | ||
| 14 | |||
| 15 | int | ||
| 16 | main(void) | ||
| 17 | { | ||
| 18 | char path[2 * MAXPATHLEN]; | ||
| 19 | char dname[128]; | ||
| 20 | const char *dir = "junk"; | ||
| 21 | const char *fname = "/file.name.ext"; | ||
| 22 | char *str; | ||
| 23 | int i; | ||
| 24 | |||
| 25 | /* Test normal functioning */ | ||
| 26 | strlcpy(path, "/", sizeof(path)); | ||
| 27 | strlcpy(dname, "/", sizeof(dname)); | ||
| 28 | strlcat(path, dir, sizeof(path)); | ||
| 29 | strlcat(dname, dir, sizeof(dname)); | ||
| 30 | strlcat(path, fname, sizeof(path)); | ||
| 31 | str = dirname(path); | ||
| 32 | if (strcmp(str, dname) != 0) | ||
| 33 | goto fail; | ||
| 34 | |||
| 35 | /* | ||
| 36 | * There are four states that require special handling: | ||
| 37 | * | ||
| 38 | * 1) path is NULL | ||
| 39 | * 2) path is the empty string | ||
| 40 | * 3) path is composed entirely of slashes | ||
| 41 | * 4) the resulting name is larger than MAXPATHLEN | ||
| 42 | * | ||
| 43 | * The first two cases require that a pointer | ||
| 44 | * to the string "." be returned. | ||
| 45 | * | ||
| 46 | * The third case requires that a pointer | ||
| 47 | * to the string "/" be returned. | ||
| 48 | * | ||
| 49 | * The final case requires that NULL be returned | ||
| 50 | * and errno * be set to ENAMETOOLONG. | ||
| 51 | */ | ||
| 52 | /* Case 1 */ | ||
| 53 | str = dirname(NULL); | ||
| 54 | if (strcmp(str, ".") != 0) | ||
| 55 | goto fail; | ||
| 56 | |||
| 57 | /* Case 2 */ | ||
| 58 | strlcpy(path, "", sizeof(path)); | ||
| 59 | str = dirname(path); | ||
| 60 | if (strcmp(str, ".") != 0) | ||
| 61 | goto fail; | ||
| 62 | |||
| 63 | /* Case 3 */ | ||
| 64 | for (i = 0; i < MAXPATHLEN - 1; i++) | ||
| 65 | strlcat(path, "/", sizeof(path)); /* path cleared above */ | ||
| 66 | str = dirname(path); | ||
| 67 | if (strcmp(str, "/") != 0) | ||
| 68 | goto fail; | ||
| 69 | |||
| 70 | /* Case 4 */ | ||
| 71 | strlcpy(path, "/", sizeof(path)); /* reset path */ | ||
| 72 | for (i = 0; i <= MAXPATHLEN; i += sizeof(dir)) | ||
| 73 | strlcat(path, dir, sizeof(path)); | ||
| 74 | strlcat(path, fname, sizeof(path)); | ||
| 75 | str = dirname(path); | ||
| 76 | if (str != NULL || errno != ENAMETOOLONG) | ||
| 77 | goto fail; | ||
| 78 | |||
| 79 | return (0); | ||
| 80 | fail: | ||
| 81 | return (1); | ||
| 82 | } | ||
