Remove building with NOCRYPTO option
[minix.git] / tests / lib / libc / gen / t_basedirname.c
blob6c82cb471c27f9f64a87f4a590aeb61855f3c139
1 /* $NetBSD: t_basedirname.c,v 1.2 2011/07/07 09:49:59 jruoho Exp $ */
3 /*
4 * Regression test for basename(3).
6 * Written by Jason R. Thorpe <thorpej@NetBSD.org>, Oct. 2002.
7 * Public domain.
8 */
10 #include <atf-c.h>
12 #include <assert.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <libgen.h>
18 struct {
19 const char *input;
20 const char *output;
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" },
27 { "/usr/", "usr" },
28 { "/", "/" },
29 { "///", "/" },
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 "." .
37 { "", "." },
38 { NULL, "." },
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 "/".
47 { "//", "/" },
49 { NULL, NULL }
52 struct {
53 const char *input;
54 const char *output;
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" },
61 { "/usr/", "/" },
62 { "usr", "." },
63 { "/", "/" },
64 { ".", "." },
65 { "..", "." },
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 "." .
72 { "", "." },
73 { NULL, "." },
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 "/".
82 { "//foo", "/" },
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" },
90 { NULL, NULL }
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;
102 int i;
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);
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) {
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.");
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.");
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;
153 int i;
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);
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) {
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.");
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.");
194 ATF_TP_ADD_TCS(tp)
196 ATF_TP_ADD_TC(tp, basename_posix);
197 ATF_TP_ADD_TC(tp, dirname_posix);
199 return atf_no_error();