t_dir.c revision 1.3 1 1.3 christos /* $NetBSD: t_dir.c,v 1.3 2011/06/11 18:03:18 christos Exp $ */
2 1.1 pgoyette
3 1.1 pgoyette /*-
4 1.1 pgoyette * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 1.1 pgoyette * All rights reserved.
6 1.1 pgoyette *
7 1.1 pgoyette * Redistribution and use in source and binary forms, with or without
8 1.1 pgoyette * modification, are permitted provided that the following conditions
9 1.1 pgoyette * are met:
10 1.1 pgoyette * 1. Redistributions of source code must retain the above copyright
11 1.1 pgoyette * notice, this list of conditions and the following disclaimer.
12 1.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 pgoyette * notice, this list of conditions and the following disclaimer in the
14 1.1 pgoyette * documentation and/or other materials provided with the distribution.
15 1.1 pgoyette *
16 1.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 pgoyette * POSSIBILITY OF SUCH DAMAGE.
27 1.1 pgoyette */
28 1.1 pgoyette
29 1.1 pgoyette #include <atf-c.h>
30 1.1 pgoyette
31 1.1 pgoyette #include <assert.h>
32 1.1 pgoyette #include <dirent.h>
33 1.1 pgoyette #include <err.h>
34 1.1 pgoyette #include <fcntl.h>
35 1.1 pgoyette #include <stdio.h>
36 1.1 pgoyette #include <stdlib.h>
37 1.1 pgoyette #include <string.h>
38 1.1 pgoyette #include <unistd.h>
39 1.1 pgoyette
40 1.1 pgoyette #include <sys/stat.h>
41 1.1 pgoyette
42 1.1 pgoyette ATF_TC(seekdir);
43 1.1 pgoyette
44 1.1 pgoyette ATF_TC_HEAD(seekdir, tc)
45 1.1 pgoyette {
46 1.1 pgoyette
47 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
48 1.1 pgoyette "Check telldir(3) and seekdir(3) for correct behavior (PR/24324)");
49 1.1 pgoyette }
50 1.1 pgoyette
51 1.1 pgoyette ATF_TC_BODY(seekdir, tc)
52 1.1 pgoyette {
53 1.1 pgoyette DIR *dp;
54 1.1 pgoyette char *wasname;
55 1.1 pgoyette struct dirent *entry;
56 1.1 pgoyette long here;
57 1.1 pgoyette
58 1.1 pgoyette mkdir("t", 0755);
59 1.1 pgoyette creat("t/a", 0600);
60 1.1 pgoyette creat("t/b", 0600);
61 1.1 pgoyette creat("t/c", 0600);
62 1.1 pgoyette
63 1.1 pgoyette dp = opendir("t");
64 1.1 pgoyette if ( dp == NULL)
65 1.1 pgoyette atf_tc_fail("Could not open temp directory.");
66 1.1 pgoyette
67 1.1 pgoyette /* skip two for . and .. */
68 1.1 pgoyette entry = readdir(dp);
69 1.1 pgoyette entry = readdir(dp);
70 1.1 pgoyette
71 1.1 pgoyette /* get first entry */
72 1.1 pgoyette entry = readdir(dp);
73 1.1 pgoyette here = telldir(dp);
74 1.1 pgoyette
75 1.1 pgoyette /* get second entry */
76 1.1 pgoyette entry = readdir(dp);
77 1.1 pgoyette wasname = strdup(entry->d_name);
78 1.3 christos if (wasname == NULL)
79 1.3 christos atf_tc_fail("cannot allocate memory");
80 1.1 pgoyette
81 1.1 pgoyette /* get third entry */
82 1.1 pgoyette entry = readdir(dp);
83 1.1 pgoyette
84 1.1 pgoyette /* try to return to the position after the first entry */
85 1.1 pgoyette seekdir(dp, here);
86 1.1 pgoyette entry = readdir(dp);
87 1.1 pgoyette
88 1.3 christos if (entry == NULL)
89 1.3 christos atf_tc_fail("entry 1 not found");
90 1.1 pgoyette if (strcmp(entry->d_name, wasname) != 0)
91 1.1 pgoyette atf_tc_fail("1st seekdir found wrong name");
92 1.1 pgoyette
93 1.1 pgoyette /* try again, and throw in a telldir() for good measure */
94 1.1 pgoyette seekdir(dp, here);
95 1.1 pgoyette here = telldir(dp);
96 1.1 pgoyette entry = readdir(dp);
97 1.1 pgoyette
98 1.3 christos if (entry == NULL)
99 1.3 christos atf_tc_fail("entry 2 not found");
100 1.1 pgoyette if (strcmp(entry->d_name, wasname) != 0)
101 1.1 pgoyette atf_tc_fail("2nd seekdir found wrong name");
102 1.1 pgoyette
103 1.1 pgoyette /* One more time, to make sure that telldir() doesn't affect result */
104 1.1 pgoyette seekdir(dp, here);
105 1.1 pgoyette entry = readdir(dp);
106 1.1 pgoyette
107 1.3 christos if (entry == NULL)
108 1.3 christos atf_tc_fail("entry 3 not found");
109 1.1 pgoyette if (strcmp(entry->d_name, wasname) != 0)
110 1.1 pgoyette atf_tc_fail("3rd seekdir found wrong name");
111 1.1 pgoyette
112 1.1 pgoyette closedir(dp);
113 1.1 pgoyette }
114 1.1 pgoyette
115 1.1 pgoyette ATF_TC(telldir_leak);
116 1.1 pgoyette
117 1.1 pgoyette ATF_TC_HEAD(telldir_leak, tc)
118 1.1 pgoyette {
119 1.1 pgoyette
120 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
121 1.1 pgoyette "Check telldir(3) for memory leakage (PR/24324)");
122 1.1 pgoyette }
123 1.1 pgoyette
124 1.1 pgoyette ATF_TC_BODY(telldir_leak, tc)
125 1.1 pgoyette {
126 1.1 pgoyette DIR *dp;
127 1.1 pgoyette long loc;
128 1.1 pgoyette char *memused;
129 1.1 pgoyette int i;
130 1.1 pgoyette int oktouse = 4096;
131 1.1 pgoyette
132 1.1 pgoyette dp = opendir(".");
133 1.1 pgoyette if (dp == NULL)
134 1.1 pgoyette atf_tc_fail("Could not open current directory");
135 1.1 pgoyette
136 1.1 pgoyette loc = telldir(dp);
137 1.1 pgoyette memused = sbrk(0);
138 1.1 pgoyette closedir(dp);
139 1.1 pgoyette
140 1.3 christos for (i = 0; i < 1000; i++) {
141 1.1 pgoyette dp = opendir(".");
142 1.1 pgoyette if (dp == NULL)
143 1.1 pgoyette atf_tc_fail("Could not open current directory");
144 1.1 pgoyette
145 1.1 pgoyette loc = telldir(dp);
146 1.1 pgoyette closedir(dp);
147 1.1 pgoyette
148 1.3 christos if ((char *)sbrk(0) - memused > oktouse) {
149 1.1 pgoyette (void)printf("Used %td extra bytes for %d telldir "
150 1.3 christos "calls", ((char *)sbrk(0) - memused), i);
151 1.3 christos oktouse = (char *)sbrk(0) - memused;
152 1.1 pgoyette }
153 1.1 pgoyette }
154 1.1 pgoyette if (oktouse > 4096) {
155 1.3 christos atf_tc_fail("Failure: leaked %d bytes", oktouse);
156 1.1 pgoyette } else {
157 1.1 pgoyette (void)printf("OK: used %td bytes\n", (char *)(sbrk(0))-memused);
158 1.1 pgoyette }
159 1.1 pgoyette }
160 1.1 pgoyette
161 1.1 pgoyette ATF_TP_ADD_TCS(tp)
162 1.1 pgoyette {
163 1.1 pgoyette
164 1.1 pgoyette ATF_TP_ADD_TC(tp, seekdir);
165 1.1 pgoyette ATF_TP_ADD_TC(tp, telldir_leak);
166 1.1 pgoyette
167 1.1 pgoyette return atf_no_error();
168 1.1 pgoyette }
169