1 /* $NetBSD: t_dir.c,v 1.10 2017/01/11 18:15:02 christos Exp $ */
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
43 ATF_TC(seekdir_basic);
44 ATF_TC_HEAD(seekdir_basic, tc)
47 atf_tc_set_md_var(tc, "descr", "Check telldir(3) and seekdir(3) "
48 "for correct behavior (PR lib/24324)");
51 ATF_TC_BODY(seekdir_basic, tc)
58 #define CREAT(x, m) do { \
60 ATF_REQUIRE_MSG((_creat_fd = creat((x), (m))) != -1, \
61 "creat(%s, %x) failed: %s", (x), (m), \
63 (void)close(_creat_fd); \
66 ATF_REQUIRE_MSG(mkdir("t", 0755) == 0,
67 "mkdir failed: %s", strerror(errno));
74 atf_tc_fail("Could not open temp directory.");
76 /* skip two for . and .. */
78 ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
79 ".", strerror(errno));
82 ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
83 "..", strerror(errno));
87 ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
88 "first", strerror(errno));
91 ATF_REQUIRE_MSG(here != -1, "telldir failed: %s", strerror(errno));
93 /* get second entry */
95 ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
96 "second", strerror(errno));
98 wasname = strdup(entry->d_name);
100 atf_tc_fail("cannot allocate memory");
102 /* get third entry */
104 ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
105 "third", strerror(errno));
107 /* try to return to the position after the first entry */
110 ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
111 "first[1]", strerror(errno));
112 if (strcmp(entry->d_name, wasname) != 0)
113 atf_tc_fail("1st seekdir found wrong name");
115 /* try again, and throw in a telldir() for good measure */
119 ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
120 "second[1]", strerror(errno));
121 if (strcmp(entry->d_name, wasname) != 0)
122 atf_tc_fail("2nd seekdir found wrong name");
124 /* One more time, to make sure that telldir() doesn't affect result */
127 ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
128 "third[1]", strerror(errno));
130 if (strcmp(entry->d_name, wasname) != 0)
131 atf_tc_fail("3rd seekdir found wrong name");
137 /* There is no sbrk on AArch64 and RISC-V */
138 #if !defined(__aarch64__) && !defined(__riscv)
139 ATF_TC(telldir_leak);
140 ATF_TC_HEAD(telldir_leak, tc)
143 atf_tc_set_md_var(tc, "descr",
144 "Check telldir(3) for memory leakage (PR lib/24324)");
147 ATF_TC_BODY(telldir_leak, tc)
156 atf_tc_fail("Could not open current directory");
162 for (i = 0; i < 1000; i++) {
165 atf_tc_fail("Could not open current directory");
170 if ((char *)sbrk(0) - memused > oktouse) {
171 (void)printf("Used %td extra bytes for %d telldir "
172 "calls", ((char *)sbrk(0) - memused), i);
173 oktouse = (char *)sbrk(0) - memused;
176 if (oktouse > 4096) {
177 atf_tc_fail("Failure: leaked %d bytes", oktouse);
179 (void)printf("OK: used %td bytes\n", (char *)(sbrk(0))-memused);
187 ATF_TP_ADD_TC(tp, seekdir_basic);
188 #if !defined(__aarch64__) && !defined(__riscv)
189 ATF_TP_ADD_TC(tp, telldir_leak);
192 return atf_no_error();