xref: /freebsd/contrib/netbsd-tests/lib/libc/gen/t_dir.c (revision 8a6fe8ce60ab99778558c4951d23615a0141daf0)
1 /* $NetBSD: t_dir.c,v 1.8 2017/01/11 07:26:17 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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.
27  */
28 
29 #include <sys/stat.h>
30 #include <assert.h>
31 #include <atf-c.h>
32 #include <dirent.h>
33 #include <err.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 
42 
43 ATF_TC(seekdir_basic);
44 ATF_TC_HEAD(seekdir_basic, tc)
45 {
46 
47 	atf_tc_set_md_var(tc, "descr", "Check telldir(3) and seekdir(3) "
48 	    "for correct behavior (PR lib/24324)");
49 }
50 
51 ATF_TC_BODY(seekdir_basic, tc)
52 {
53 	DIR *dp;
54 	char *wasname;
55 	struct dirent *entry;
56 	long here;
57 
58 #define	CREAT(x, m)	do {						\
59 		int _creat_fd;						\
60 		ATF_REQUIRE_MSG((_creat_fd = creat((x), (m))) != -1,	\
61 		    "creat(%s, %x) failed: %s", (x), (m),		\
62 		    strerror(errno));					\
63 		(void)close(_creat_fd);					\
64 	} while(0);
65 
66 	ATF_REQUIRE_MSG(mkdir("t", 0755) == 0,
67 	    "mkdir failed: %s", strerror(errno));
68 	CREAT("t/a", 0600);
69 	CREAT("t/b", 0600);
70 	CREAT("t/c", 0600);
71 
72 	dp = opendir("t");
73 	if ( dp == NULL)
74 		atf_tc_fail("Could not open temp directory.");
75 
76 	/* skip two for . and .. */
77 	entry = readdir(dp);
78 	entry = readdir(dp);
79 
80 	/* get first entry */
81 	entry = readdir(dp);
82 	here = telldir(dp);
83 	ATF_REQUIRE_MSG(here != -1,
84 	    "telldir failed: %s", strerror(errno));
85 
86 	/* get second entry */
87 	entry = readdir(dp);
88 #ifdef	__FreeBSD__
89 	ATF_REQUIRE_MSG(entry != NULL,
90 	    "readdir failed: %s", strerror(errno));
91 #endif
92 	wasname = strdup(entry->d_name);
93 	if (wasname == NULL)
94 		atf_tc_fail("cannot allocate memory");
95 
96 	/* get third entry */
97 	entry = readdir(dp);
98 
99 	/* try to return to the position after the first entry */
100 	seekdir(dp, here);
101 	entry = readdir(dp);
102 
103 	if (entry == NULL)
104 		atf_tc_fail("entry 1 not found");
105 	if (strcmp(entry->d_name, wasname) != 0)
106 		atf_tc_fail("1st seekdir found wrong name");
107 
108 	/* try again, and throw in a telldir() for good measure */
109 	seekdir(dp, here);
110 	here = telldir(dp);
111 	entry = readdir(dp);
112 
113 	if (entry == NULL)
114 		atf_tc_fail("entry 2 not found");
115 	if (strcmp(entry->d_name, wasname) != 0)
116 		atf_tc_fail("2nd seekdir found wrong name");
117 
118 	/* One more time, to make sure that telldir() doesn't affect result */
119 	seekdir(dp, here);
120 	entry = readdir(dp);
121 
122 	if (entry == NULL)
123 		atf_tc_fail("entry 3 not found");
124 	if (strcmp(entry->d_name, wasname) != 0)
125 		atf_tc_fail("3rd seekdir found wrong name");
126 
127 	closedir(dp);
128 	free(wasname);
129 }
130 
131 /* There is no sbrk on AArch64 and RISC-V */
132 #if !defined(__aarch64__) && !defined(__riscv__)
133 ATF_TC(telldir_leak);
134 ATF_TC_HEAD(telldir_leak, tc)
135 {
136 
137 	atf_tc_set_md_var(tc, "descr",
138 	    "Check telldir(3) for memory leakage (PR lib/24324)");
139 }
140 
141 ATF_TC_BODY(telldir_leak, tc)
142 {
143 	DIR *dp;
144 	char *memused;
145 	int i;
146 	int oktouse = 4096;
147 
148 	dp = opendir(".");
149 	if (dp == NULL)
150 		atf_tc_fail("Could not open current directory");
151 
152 	(void)telldir(dp);
153 	memused = sbrk(0);
154 	closedir(dp);
155 
156 	for (i = 0; i < 1000; i++) {
157 		dp = opendir(".");
158 		if (dp == NULL)
159 			atf_tc_fail("Could not open current directory");
160 
161 		(void)telldir(dp);
162 		closedir(dp);
163 
164 		if ((char *)sbrk(0) - memused > oktouse) {
165 			(void)printf("Used %td extra bytes for %d telldir "
166 			    "calls", ((char *)sbrk(0) - memused), i);
167 			oktouse = (char *)sbrk(0) - memused;
168 		}
169 	}
170 	if (oktouse > 4096) {
171 		atf_tc_fail("Failure: leaked %d bytes", oktouse);
172 	} else {
173 		(void)printf("OK: used %td bytes\n", (char *)(sbrk(0))-memused);
174 	}
175 }
176 #endif
177 
178 ATF_TP_ADD_TCS(tp)
179 {
180 
181 	ATF_TP_ADD_TC(tp, seekdir_basic);
182 #if !defined(__aarch64__) && !defined(__riscv__)
183 	ATF_TP_ADD_TC(tp, telldir_leak);
184 #endif
185 
186 	return atf_no_error();
187 }
188