summaryrefslogtreecommitdiffstats
path: root/contrib/netbsd-tests/lib/libc/gen/t_basedirname.c
blob: 6c82cb471c27f9f64a87f4a590aeb61855f3c139 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*	$NetBSD: t_basedirname.c,v 1.2 2011/07/07 09:49:59 jruoho Exp $	*/

/*
 * Regression test for basename(3).
 *
 * Written by Jason R. Thorpe <thorpej@NetBSD.org>, Oct. 2002.
 * Public domain.
 */

#include <atf-c.h>

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>

struct {
	const char *input;
	const char *output;
} test_basename_table[] = {
/*
 * The following are taken from the "Sample Input and Output Strings
 * for basename()" table in IEEE Std 1003.1-2001.
 */
	{ "/usr/lib",		"lib" },
	{ "/usr/",		"usr" },
	{ "/",			"/" },
	{ "///",		"/" },
	{ "//usr//lib//",	"lib" },
/*
 * IEEE Std 1003.1-2001:
 *
 *	If path is a null pointer or points to an empty string,
 *	basename() shall return a pointer to the string "." .
 */
	{ "",			"." },
	{ NULL,			"." },
/*
 * IEEE Std 1003.1-2001:
 *
 *	If the string is exactly "//", it is implementation-defined
 *	whether "/" or "//" is returned.
 *
 * The NetBSD implementation returns "/".
 */
	{ "//",			"/" },

	{ NULL,			NULL }
};

struct {
	const char *input;
	const char *output;
} test_dirname_table[] = {
/*
 * The following are taken from the "Sample Input and Output Strings
 * for dirname()" table in IEEE Std 1003.1-2001.
 */
	{ "/usr/lib",		"/usr" },
	{ "/usr/",		"/" },
	{ "usr",		"." },
	{ "/",			"/" },
	{ ".",			"." },
	{ "..",			"." },
/*
 * IEEE Std 1003.1-2001:
 *
 *	If path is a null pointer or points to an empty string,
 *	dirname() shall return a pointer to the string "." .
 */
	{ "",			"." },
	{ NULL,			"." },
/*
 * IEEE Std 1003.1-2001:
 *
 *	Since the meaning of the leading "//" is implementation-defined,
 *	dirname("//foo") may return either "//" or "/" (but nothing else).
 *
 * The NetBSD implementation returns "/".
 */
	{ "//foo",		"/" },
/*
 * Make sure the trailing slashes after the directory name component
 * get trimmed.  The Std does not talk about this, but this is what
 * Solaris 8's dirname(3) does.
 */
	{ "/usr///lib",		"/usr" },

	{ NULL,			NULL }
};

ATF_TC(basename_posix);
ATF_TC_HEAD(basename_posix, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test basename(3) with POSIX examples");
}

ATF_TC_BODY(basename_posix, tc)
{
	char testbuf[32], *base;
	int i;

	for (i = 0; test_basename_table[i].output != NULL; i++) {
		if (test_basename_table[i].input != NULL) {
			if (strlen(test_basename_table[i].input) >=
			    sizeof(testbuf))
				atf_tc_skip("Testbuf too small!");
			strcpy(testbuf, test_basename_table[i].input);
			base = basename(testbuf);
		} else
			base = basename(NULL);

		/*
		 * basename(3) is allowed to modify the input buffer.
		 * However, that is considered hostile by some programs,
		 * and so we elect to consider this an error.
		 *
		 * This is not a problem, as basename(3) is also allowed
		 * to return a pointer to a statically-allocated buffer
		 * (it is explicitly not required to be reentrant).
		 */
		if (test_basename_table[i].input != NULL &&
		    strcmp(test_basename_table[i].input, testbuf) != 0) {
			fprintf(stderr,
			    "Input buffer for \"%s\" was modified\n",
			    test_basename_table[i].input);
			atf_tc_fail("Input buffer was modified.");
		}

		/* Make sure the result is correct. */
		if (strcmp(test_basename_table[i].output, base) != 0) {
			fprintf(stderr,
			    "Input \"%s\", output \"%s\", expected \"%s\"\n",
			    test_basename_table[i].input ==
				NULL ? "(null)" : test_basename_table[i].input,
			    base, test_basename_table[i].output);
			atf_tc_fail("Output does not match expected value.");
		}
	}
}


ATF_TC(dirname_posix);
ATF_TC_HEAD(dirname_posix, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test dirname(3) with POSIX examples");
}

ATF_TC_BODY(dirname_posix, tc)
{
	char testbuf[32], *base;
	int i;

	for (i = 0; test_dirname_table[i].output != NULL; i++) {
		if (test_dirname_table[i].input != NULL) {
			if (strlen(test_dirname_table[i].input) >=
			    sizeof(testbuf))
				atf_tc_skip("Testbuf too small!");
			strcpy(testbuf, test_dirname_table[i].input);
			base = dirname(testbuf);
		} else
			base = dirname(NULL);

		/*
		 * dirname(3) is allowed to modify the input buffer.
		 * However, that is considered hostile by some programs,
		 * and so we elect to consider this an error.
		 *
		 * This is not a problem, as dirname(3) is also allowed
		 * to return a pointer to a statically-allocated buffer
		 * (it is explicitly not required to be reentrant).
		 */
		if (test_dirname_table[i].input != NULL &&
		    strcmp(test_dirname_table[i].input, testbuf) != 0) {
			fprintf(stderr,
			    "Input buffer for \"%s\" was modified\n",
			    test_dirname_table[i].input);
			atf_tc_fail("Input buffer was modified.");
		}

		/* Make sure the result is correct. */
		if (strcmp(test_dirname_table[i].output, base) != 0) {
			fprintf(stderr,
			    "Input \"%s\", output \"%s\", expected \"%s\"\n",
			    test_dirname_table[i].input ==
				NULL ? "(null)" : test_dirname_table[i].input,
			    base, test_dirname_table[i].output);
			atf_tc_fail("Output does not match expected value.");
		}
	}
}

ATF_TP_ADD_TCS(tp)
{
	ATF_TP_ADD_TC(tp, basename_posix);
	ATF_TP_ADD_TC(tp, dirname_posix);

	return atf_no_error();
}
OpenPOWER on IntegriCloud