xref: /netbsd-src/tests/lib/libc/gen/t_dir.c (revision cb861154c176d3dcc8ff846f449e3c16a5f5edb5)
1 /* $NetBSD: t_dir.c,v 1.2 2011/04/07 18:14:08 jruoho 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 <atf-c.h>
30 
31 #include <assert.h>
32 #include <dirent.h>
33 #include <err.h>
34 #include <fcntl.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #include <sys/stat.h>
41 
42 ATF_TC(seekdir);
43 
44 ATF_TC_HEAD(seekdir, tc)
45 {
46 
47 	atf_tc_set_md_var(tc, "descr",
48 	    "Check telldir(3) and seekdir(3) for correct behavior (PR/24324)");
49 }
50 
51 ATF_TC_BODY(seekdir, tc)
52 {
53 	DIR *dp;
54 	char *wasname;
55 	struct dirent *entry;
56 	long here;
57 
58 	mkdir("t", 0755);
59 	creat("t/a", 0600);
60 	creat("t/b", 0600);
61 	creat("t/c", 0600);
62 
63 	dp = opendir("t");
64 	if ( dp == NULL)
65 		atf_tc_fail("Could not open temp directory.");
66 
67 	/* skip two for . and .. */
68 	entry = readdir(dp);
69 	entry = readdir(dp);
70 
71 	/* get first entry */
72 	entry = readdir(dp);
73 	here = telldir(dp);
74 
75 	/* get second entry */
76 	entry = readdir(dp);
77 	wasname = strdup(entry->d_name);
78 
79 	/* get third entry */
80 	entry = readdir(dp);
81 
82 	/* try to return to the position after the first entry */
83 	seekdir(dp, here);
84 	entry = readdir(dp);
85 
86 	if (strcmp(entry->d_name, wasname) != 0)
87 		atf_tc_fail("1st seekdir found wrong name");
88 
89 	/* try again, and throw in a telldir() for good measure */
90 	seekdir(dp, here);
91 	here = telldir(dp);
92 	entry = readdir(dp);
93 
94 	if (strcmp(entry->d_name, wasname) != 0)
95 		atf_tc_fail("2nd seekdir found wrong name");
96 
97 	/* One more time, to make sure that telldir() doesn't affect result */
98 	seekdir(dp, here);
99 	entry = readdir(dp);
100 
101 	if (strcmp(entry->d_name, wasname) != 0)
102 		atf_tc_fail("3rd seekdir found wrong name");
103 
104 	closedir(dp);
105 }
106 
107 ATF_TC(telldir_leak);
108 
109 ATF_TC_HEAD(telldir_leak, tc)
110 {
111 
112 	atf_tc_set_md_var(tc, "descr",
113 	    "Check telldir(3) for memory leakage (PR/24324)");
114 }
115 
116 ATF_TC_BODY(telldir_leak, tc)
117 {
118 	DIR *dp;
119 	long loc;
120 	char *memused;
121 	int i;
122 	int oktouse = 4096;
123 
124 	dp = opendir(".");
125 	if (dp == NULL)
126 		atf_tc_fail("Could not open current directory");
127 
128 	loc = telldir(dp);
129 	memused = sbrk(0);
130 	closedir(dp);
131 
132 	for (i=0; i<1000; i++) {
133 		dp = opendir(".");
134 		if (dp == NULL)
135 			atf_tc_fail("Could not open current directory");
136 
137 		loc = telldir(dp);
138 		closedir(dp);
139 
140 		if ((char *)(sbrk(0)) - memused > oktouse) {
141 			(void)printf("Used %td extra bytes for %d telldir "
142 			    "calls", ((char *)(sbrk(0)) - memused), i);
143 			oktouse = (char *)(sbrk(0)) - memused;
144 		}
145 	}
146 	if (oktouse > 4096) {
147 		atf_tc_fail("Failure: leaked %td bytes", oktouse);
148 	} else {
149 		(void)printf("OK: used %td bytes\n", (char *)(sbrk(0))-memused);
150 	}
151 }
152 
153 ATF_TP_ADD_TCS(tp)
154 {
155 
156 	ATF_TP_ADD_TC(tp, seekdir);
157 	ATF_TP_ADD_TC(tp, telldir_leak);
158 
159 	return atf_no_error();
160 }
161