xref: /freebsd/contrib/netbsd-tests/lib/libc/gen/t_basedirname.c (revision 1ce4b357402c3cd0cba264a15c06b6fd00591088)
1 /*	$NetBSD: t_basedirname.c,v 1.2 2011/07/07 09:49:59 jruoho 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(basename_posix);
94 ATF_TC_HEAD(basename_posix, tc)
95 {
96 	atf_tc_set_md_var(tc, "descr", "Test basename(3) with POSIX examples");
97 }
98 
99 ATF_TC_BODY(basename_posix, tc)
100 {
101 	char testbuf[32], *base;
102 	int i;
103 
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) >=
107 			    sizeof(testbuf))
108 				atf_tc_skip("Testbuf too small!");
109 			strcpy(testbuf, test_basename_table[i].input);
110 			base = basename(testbuf);
111 		} else
112 			base = basename(NULL);
113 
114 		/*
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.
118 		 *
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).
122 		 */
123 		if (test_basename_table[i].input != NULL &&
124 		    strcmp(test_basename_table[i].input, testbuf) != 0) {
125 			fprintf(stderr,
126 			    "Input buffer for \"%s\" was modified\n",
127 			    test_basename_table[i].input);
128 			atf_tc_fail("Input buffer was modified.");
129 		}
130 
131 		/* Make sure the result is correct. */
132 		if (strcmp(test_basename_table[i].output, base) != 0) {
133 			fprintf(stderr,
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.");
139 		}
140 	}
141 }
142 
143 
144 ATF_TC(dirname_posix);
145 ATF_TC_HEAD(dirname_posix, tc)
146 {
147 	atf_tc_set_md_var(tc, "descr", "Test dirname(3) with POSIX examples");
148 }
149 
150 ATF_TC_BODY(dirname_posix, tc)
151 {
152 	char testbuf[32], *base;
153 	int i;
154 
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) >=
158 			    sizeof(testbuf))
159 				atf_tc_skip("Testbuf too small!");
160 			strcpy(testbuf, test_dirname_table[i].input);
161 			base = dirname(testbuf);
162 		} else
163 			base = dirname(NULL);
164 
165 		/*
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.
169 		 *
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).
173 		 */
174 		if (test_dirname_table[i].input != NULL &&
175 		    strcmp(test_dirname_table[i].input, testbuf) != 0) {
176 			fprintf(stderr,
177 			    "Input buffer for \"%s\" was modified\n",
178 			    test_dirname_table[i].input);
179 			atf_tc_fail("Input buffer was modified.");
180 		}
181 
182 		/* Make sure the result is correct. */
183 		if (strcmp(test_dirname_table[i].output, base) != 0) {
184 			fprintf(stderr,
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.");
190 		}
191 	}
192 }
193 
194 ATF_TP_ADD_TCS(tp)
195 {
196 	ATF_TP_ADD_TC(tp, basename_posix);
197 	ATF_TP_ADD_TC(tp, dirname_posix);
198 
199 	return atf_no_error();
200 }
201