xref: /netbsd/tests/lib/libc/gen/t_basedirname.c (revision 6550d01e)
1 /*	$NetBSD: t_basedirname.c,v 1.1 2010/12/22 23:45:44 pgoyette Exp $	*/
2 
3 /*
4  * Regression test for basename(3).
5  *
6  * Written by Jason R. Thorpe <thorpej@NetBSD.org>, Oct. 2002.
7  * Public domain.
8  */
9 
10 #include <atf-c.h>
11 
12 #include <assert.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <libgen.h>
17 
18 struct {
19 	const char *input;
20 	const char *output;
21 } test_basename_table[] = {
22 /*
23  * The following are taken from the "Sample Input and Output Strings
24  * for basename()" table in IEEE Std 1003.1-2001.
25  */
26 	{ "/usr/lib",		"lib" },
27 	{ "/usr/",		"usr" },
28 	{ "/",			"/" },
29 	{ "///",		"/" },
30 	{ "//usr//lib//",	"lib" },
31 /*
32  * IEEE Std 1003.1-2001:
33  *
34  *	If path is a null pointer or points to an empty string,
35  *	basename() shall return a pointer to the string "." .
36  */
37 	{ "",			"." },
38 	{ NULL,			"." },
39 /*
40  * IEEE Std 1003.1-2001:
41  *
42  *	If the string is exactly "//", it is implementation-defined
43  *	whether "/" or "//" is returned.
44  *
45  * The NetBSD implementation returns "/".
46  */
47 	{ "//",			"/" },
48 
49 	{ NULL,			NULL }
50 };
51 
52 struct {
53 	const char *input;
54 	const char *output;
55 } test_dirname_table[] = {
56 /*
57  * The following are taken from the "Sample Input and Output Strings
58  * for dirname()" table in IEEE Std 1003.1-2001.
59  */
60 	{ "/usr/lib",		"/usr" },
61 	{ "/usr/",		"/" },
62 	{ "usr",		"." },
63 	{ "/",			"/" },
64 	{ ".",			"." },
65 	{ "..",			"." },
66 /*
67  * IEEE Std 1003.1-2001:
68  *
69  *	If path is a null pointer or points to an empty string,
70  *	dirname() shall return a pointer to the string "." .
71  */
72 	{ "",			"." },
73 	{ NULL,			"." },
74 /*
75  * IEEE Std 1003.1-2001:
76  *
77  *	Since the meaning of the leading "//" is implementation-defined,
78  *	dirname("//foo") may return either "//" or "/" (but nothing else).
79  *
80  * The NetBSD implementation returns "/".
81  */
82 	{ "//foo",		"/" },
83 /*
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.
87  */
88 	{ "/usr///lib",		"/usr" },
89 
90 	{ NULL,			NULL }
91 };
92 
93 ATF_TC(t_basename);
94 
95 ATF_TC_HEAD(t_basename, tc)
96 {
97 	atf_tc_set_md_var(tc, "descr", "Test basename(3) with POSIX examples");
98 }
99 
100 ATF_TC_BODY(t_basename, tc)
101 {
102 	char testbuf[32], *base;
103 	int i;
104 
105 	for (i = 0; test_basename_table[i].output != NULL; i++) {
106 		if (test_basename_table[i].input != NULL) {
107 			if (strlen(test_basename_table[i].input) >=
108 			    sizeof(testbuf))
109 				atf_tc_skip("Testbuf too small!");
110 			strcpy(testbuf, test_basename_table[i].input);
111 			base = basename(testbuf);
112 		} else
113 			base = basename(NULL);
114 
115 		/*
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.
119 		 *
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).
123 		 */
124 		if (test_basename_table[i].input != NULL &&
125 		    strcmp(test_basename_table[i].input, testbuf) != 0) {
126 			fprintf(stderr,
127 			    "Input buffer for \"%s\" was modified\n",
128 			    test_basename_table[i].input);
129 			atf_tc_fail("Input buffer was modified.");
130 		}
131 
132 		/* Make sure the result is correct. */
133 		if (strcmp(test_basename_table[i].output, base) != 0) {
134 			fprintf(stderr,
135 			    "Input \"%s\", output \"%s\", expected \"%s\"\n",
136 			    test_basename_table[i].input ==
137 				NULL ? "(null)" : test_basename_table[i].input,
138 			    base, test_basename_table[i].output);
139 			atf_tc_fail("Output does not match expected value.");
140 		}
141 	}
142 }
143 
144 
145 ATF_TC(t_dirname);
146 
147 ATF_TC_HEAD(t_dirname, tc)
148 {
149 	atf_tc_set_md_var(tc, "descr", "Test dirname(3) with POSIX examples");
150 }
151 
152 ATF_TC_BODY(t_dirname, tc)
153 {
154 	char testbuf[32], *base;
155 	int i;
156 
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) >=
160 			    sizeof(testbuf))
161 				atf_tc_skip("Testbuf too small!");
162 			strcpy(testbuf, test_dirname_table[i].input);
163 			base = dirname(testbuf);
164 		} else
165 			base = dirname(NULL);
166 
167 		/*
168 		 * dirname(3) is allowed to modify the input buffer.
169 		 * However, that is considered hostile by some programs,
170 		 * and so we elect to consider this an error.
171 		 *
172 		 * This is not a problem, as dirname(3) is also allowed
173 		 * to return a pointer to a statically-allocated buffer
174 		 * (it is explicitly not required to be reentrant).
175 		 */
176 		if (test_dirname_table[i].input != NULL &&
177 		    strcmp(test_dirname_table[i].input, testbuf) != 0) {
178 			fprintf(stderr,
179 			    "Input buffer for \"%s\" was modified\n",
180 			    test_dirname_table[i].input);
181 			atf_tc_fail("Input buffer was modified.");
182 		}
183 
184 		/* Make sure the result is correct. */
185 		if (strcmp(test_dirname_table[i].output, base) != 0) {
186 			fprintf(stderr,
187 			    "Input \"%s\", output \"%s\", expected \"%s\"\n",
188 			    test_dirname_table[i].input ==
189 				NULL ? "(null)" : test_dirname_table[i].input,
190 			    base, test_dirname_table[i].output);
191 			atf_tc_fail("Output does not match expected value.");
192 		}
193 	}
194 }
195 
196 ATF_TP_ADD_TCS(tp)
197 {
198 	ATF_TP_ADD_TC(tp, t_basename);
199 	ATF_TP_ADD_TC(tp, t_dirname);
200 
201 	return atf_no_error();
202 }
203