diff options
Diffstat (limited to 'src/regress/lib/libc/basename/basename_test.c')
| -rw-r--r-- | src/regress/lib/libc/basename/basename_test.c | 79 |
1 files changed, 79 insertions, 0 deletions
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 | } | ||
