1 /* $NetBSD: t_basedirname.c,v 1.2 2011/07/07 09:49:59 jruoho Exp $ */
4 * Regression test for basename(3).
6 * Written by Jason R. Thorpe <thorpej@NetBSD.org>, Oct. 2002.
21 } test_basename_table[] = {
23 * The following are taken from the "Sample Input and Output Strings
24 * for basename()" table in IEEE Std 1003.1-2001.
26 { "/usr/lib", "lib" },
30 { "//usr//lib//", "lib" },
32 * IEEE Std 1003.1-2001:
34 * If path is a null pointer or points to an empty string,
35 * basename() shall return a pointer to the string "." .
40 * IEEE Std 1003.1-2001:
42 * If the string is exactly "//", it is implementation-defined
43 * whether "/" or "//" is returned.
45 * The NetBSD implementation returns "/".
55 } test_dirname_table[] = {
57 * The following are taken from the "Sample Input and Output Strings
58 * for dirname()" table in IEEE Std 1003.1-2001.
60 { "/usr/lib", "/usr" },
67 * IEEE Std 1003.1-2001:
69 * If path is a null pointer or points to an empty string,
70 * dirname() shall return a pointer to the string "." .
75 * IEEE Std 1003.1-2001:
77 * Since the meaning of the leading "//" is implementation-defined,
78 * dirname("//foo") may return either "//" or "/" (but nothing else).
80 * The NetBSD implementation returns "/".
84 * Make sure the trailing slashes after the directory name component
85 * get trimmed. The Std does not talk about this, but this is what
86 * Solaris 8's dirname(3) does.
88 { "/usr///lib", "/usr" },
93 ATF_TC(basename_posix);
94 ATF_TC_HEAD(basename_posix, tc)
96 atf_tc_set_md_var(tc, "descr", "Test basename(3) with POSIX examples");
99 ATF_TC_BODY(basename_posix, tc)
101 char testbuf[32], *base;
104 for (i = 0; test_basename_table[i].output != NULL; i++) {
105 if (test_basename_table[i].input != NULL) {
106 if (strlen(test_basename_table[i].input) >=
108 atf_tc_skip("Testbuf too small!");
109 strcpy(testbuf, test_basename_table[i].input);
110 base = basename(testbuf);
112 base = basename(NULL);
116 * basename(3) is allowed to modify the input buffer.
117 * However, that is considered hostile by some programs,
118 * and so we elect to consider this an error.
120 * This is not a problem, as basename(3) is also allowed
121 * to return a pointer to a statically-allocated buffer
122 * (it is explicitly not required to be reentrant).
124 if (test_basename_table[i].input != NULL &&
125 strcmp(test_basename_table[i].input, testbuf) != 0) {
127 "Input buffer for \"%s\" was modified\n",
128 test_basename_table[i].input);
129 atf_tc_fail("Input buffer was modified.");
133 /* Make sure the result is correct. */
134 if (strcmp(test_basename_table[i].output, base) != 0) {
136 "Input \"%s\", output \"%s\", expected \"%s\"\n",
137 test_basename_table[i].input ==
138 NULL ? "(null)" : test_basename_table[i].input,
139 base, test_basename_table[i].output);
140 atf_tc_fail("Output does not match expected value.");
146 ATF_TC(dirname_posix);
147 ATF_TC_HEAD(dirname_posix, tc)
149 atf_tc_set_md_var(tc, "descr", "Test dirname(3) with POSIX examples");
152 ATF_TC_BODY(dirname_posix, tc)
154 char testbuf[32], *base;
157 for (i = 0; test_dirname_table[i].output != NULL; i++) {
158 if (test_dirname_table[i].input != NULL) {
159 if (strlen(test_dirname_table[i].input) >=
161 atf_tc_skip("Testbuf too small!");
162 strcpy(testbuf, test_dirname_table[i].input);
163 base = dirname(testbuf);
165 base = dirname(NULL);
169 * dirname(3) is allowed to modify the input buffer.
170 * However, that is considered hostile by some programs,
171 * and so we elect to consider this an error.
173 * This is not a problem, as dirname(3) is also allowed
174 * to return a pointer to a statically-allocated buffer
175 * (it is explicitly not required to be reentrant).
177 if (test_dirname_table[i].input != NULL &&
178 strcmp(test_dirname_table[i].input, testbuf) != 0) {
180 "Input buffer for \"%s\" was modified\n",
181 test_dirname_table[i].input);
182 atf_tc_fail("Input buffer was modified.");
186 /* Make sure the result is correct. */
187 if (strcmp(test_dirname_table[i].output, base) != 0) {
189 "Input \"%s\", output \"%s\", expected \"%s\"\n",
190 test_dirname_table[i].input ==
191 NULL ? "(null)" : test_dirname_table[i].input,
192 base, test_dirname_table[i].output);
193 atf_tc_fail("Output does not match expected value.");
200 ATF_TP_ADD_TC(tp, basename_posix);
201 ATF_TP_ADD_TC(tp, dirname_posix);
203 return atf_no_error();