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
);
115 * basename(3) is allowed to modify the input buffer.
116 * However, that is considered hostile by some programs,
117 * and so we elect to consider this an error.
119 * This is not a problem, as basename(3) is also allowed
120 * to return a pointer to a statically-allocated buffer
121 * (it is explicitly not required to be reentrant).
123 if (test_basename_table
[i
].input
!= NULL
&&
124 strcmp(test_basename_table
[i
].input
, testbuf
) != 0) {
126 "Input buffer for \"%s\" was modified\n",
127 test_basename_table
[i
].input
);
128 atf_tc_fail("Input buffer was modified.");
131 /* Make sure the result is correct. */
132 if (strcmp(test_basename_table
[i
].output
, base
) != 0) {
134 "Input \"%s\", output \"%s\", expected \"%s\"\n",
135 test_basename_table
[i
].input
==
136 NULL
? "(null)" : test_basename_table
[i
].input
,
137 base
, test_basename_table
[i
].output
);
138 atf_tc_fail("Output does not match expected value.");
144 ATF_TC(dirname_posix
);
145 ATF_TC_HEAD(dirname_posix
, tc
)
147 atf_tc_set_md_var(tc
, "descr", "Test dirname(3) with POSIX examples");
150 ATF_TC_BODY(dirname_posix
, tc
)
152 char testbuf
[32], *base
;
155 for (i
= 0; test_dirname_table
[i
].output
!= NULL
; i
++) {
156 if (test_dirname_table
[i
].input
!= NULL
) {
157 if (strlen(test_dirname_table
[i
].input
) >=
159 atf_tc_skip("Testbuf too small!");
160 strcpy(testbuf
, test_dirname_table
[i
].input
);
161 base
= dirname(testbuf
);
163 base
= dirname(NULL
);
166 * dirname(3) is allowed to modify the input buffer.
167 * However, that is considered hostile by some programs,
168 * and so we elect to consider this an error.
170 * This is not a problem, as dirname(3) is also allowed
171 * to return a pointer to a statically-allocated buffer
172 * (it is explicitly not required to be reentrant).
174 if (test_dirname_table
[i
].input
!= NULL
&&
175 strcmp(test_dirname_table
[i
].input
, testbuf
) != 0) {
177 "Input buffer for \"%s\" was modified\n",
178 test_dirname_table
[i
].input
);
179 atf_tc_fail("Input buffer was modified.");
182 /* Make sure the result is correct. */
183 if (strcmp(test_dirname_table
[i
].output
, base
) != 0) {
185 "Input \"%s\", output \"%s\", expected \"%s\"\n",
186 test_dirname_table
[i
].input
==
187 NULL
? "(null)" : test_dirname_table
[i
].input
,
188 base
, test_dirname_table
[i
].output
);
189 atf_tc_fail("Output does not match expected value.");
196 ATF_TP_ADD_TC(tp
, basename_posix
);
197 ATF_TP_ADD_TC(tp
, dirname_posix
);
199 return atf_no_error();