t_dir.c revision 1.7 1 1.7 christos /* $NetBSD: t_dir.c,v 1.7 2017/01/10 15:19:00 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.7 christos #include <sys/stat.h>
30 1.7 christos #include <assert.h>
31 1.1 pgoyette #include <atf-c.h>
32 1.1 pgoyette #include <dirent.h>
33 1.1 pgoyette #include <err.h>
34 1.7 christos #include <errno.h>
35 1.1 pgoyette #include <fcntl.h>
36 1.1 pgoyette #include <stdio.h>
37 1.1 pgoyette #include <stdlib.h>
38 1.1 pgoyette #include <string.h>
39 1.1 pgoyette #include <unistd.h>
40 1.1 pgoyette
41 1.7 christos
42 1.1 pgoyette
43 1.4 jruoho ATF_TC(seekdir_basic);
44 1.4 jruoho ATF_TC_HEAD(seekdir_basic, tc)
45 1.1 pgoyette {
46 1.1 pgoyette
47 1.5 jruoho atf_tc_set_md_var(tc, "descr", "Check telldir(3) and seekdir(3) "
48 1.5 jruoho "for correct behavior (PR lib/24324)");
49 1.1 pgoyette }
50 1.1 pgoyette
51 1.4 jruoho ATF_TC_BODY(seekdir_basic, 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.7 christos #define CREAT(x, m) do { \
59 1.7 christos int _creat_fd; \
60 1.7 christos ATF_REQUIRE_MSG((_creat_fd = creat((x), (m))), \
61 1.7 christos "creat(%s, %x) failed: %s", (x), (m), \
62 1.7 christos strerror(errno)); \
63 1.7 christos (void)close(_creat_fd); \
64 1.7 christos } while(0);
65 1.7 christos
66 1.7 christos ATF_REQUIRE_MSG(mkdir("t", 0755) == 0,
67 1.7 christos "mkdir failed: %s", strerror(errno));
68 1.7 christos CREAT("t/a", 0600);
69 1.7 christos CREAT("t/b", 0600);
70 1.7 christos CREAT("t/c", 0600);
71 1.1 pgoyette
72 1.1 pgoyette dp = opendir("t");
73 1.1 pgoyette if ( dp == NULL)
74 1.1 pgoyette atf_tc_fail("Could not open temp directory.");
75 1.1 pgoyette
76 1.1 pgoyette /* skip two for . and .. */
77 1.1 pgoyette entry = readdir(dp);
78 1.1 pgoyette entry = readdir(dp);
79 1.1 pgoyette
80 1.1 pgoyette /* get first entry */
81 1.1 pgoyette entry = readdir(dp);
82 1.1 pgoyette here = telldir(dp);
83 1.7 christos ATF_REQUIRE_MSG(here != -1,
84 1.7 christos "telldir failed: %s", strerror(errno));
85 1.1 pgoyette
86 1.1 pgoyette /* get second entry */
87 1.1 pgoyette entry = readdir(dp);
88 1.1 pgoyette wasname = strdup(entry->d_name);
89 1.3 christos if (wasname == NULL)
90 1.3 christos atf_tc_fail("cannot allocate memory");
91 1.1 pgoyette
92 1.1 pgoyette /* get third entry */
93 1.1 pgoyette entry = readdir(dp);
94 1.1 pgoyette
95 1.1 pgoyette /* try to return to the position after the first entry */
96 1.1 pgoyette seekdir(dp, here);
97 1.1 pgoyette entry = readdir(dp);
98 1.1 pgoyette
99 1.3 christos if (entry == NULL)
100 1.3 christos atf_tc_fail("entry 1 not found");
101 1.1 pgoyette if (strcmp(entry->d_name, wasname) != 0)
102 1.1 pgoyette atf_tc_fail("1st seekdir found wrong name");
103 1.1 pgoyette
104 1.1 pgoyette /* try again, and throw in a telldir() for good measure */
105 1.1 pgoyette seekdir(dp, here);
106 1.1 pgoyette here = telldir(dp);
107 1.1 pgoyette entry = readdir(dp);
108 1.1 pgoyette
109 1.3 christos if (entry == NULL)
110 1.3 christos atf_tc_fail("entry 2 not found");
111 1.1 pgoyette if (strcmp(entry->d_name, wasname) != 0)
112 1.1 pgoyette atf_tc_fail("2nd seekdir found wrong name");
113 1.1 pgoyette
114 1.1 pgoyette /* One more time, to make sure that telldir() doesn't affect result */
115 1.1 pgoyette seekdir(dp, here);
116 1.1 pgoyette entry = readdir(dp);
117 1.1 pgoyette
118 1.3 christos if (entry == NULL)
119 1.3 christos atf_tc_fail("entry 3 not found");
120 1.1 pgoyette if (strcmp(entry->d_name, wasname) != 0)
121 1.1 pgoyette atf_tc_fail("3rd seekdir found wrong name");
122 1.1 pgoyette
123 1.1 pgoyette closedir(dp);
124 1.7 christos free(wasname);
125 1.1 pgoyette }
126 1.1 pgoyette
127 1.1 pgoyette ATF_TC(telldir_leak);
128 1.1 pgoyette ATF_TC_HEAD(telldir_leak, tc)
129 1.1 pgoyette {
130 1.1 pgoyette
131 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
132 1.5 jruoho "Check telldir(3) for memory leakage (PR lib/24324)");
133 1.1 pgoyette }
134 1.1 pgoyette
135 1.1 pgoyette ATF_TC_BODY(telldir_leak, tc)
136 1.1 pgoyette {
137 1.1 pgoyette DIR *dp;
138 1.1 pgoyette char *memused;
139 1.1 pgoyette int i;
140 1.1 pgoyette int oktouse = 4096;
141 1.1 pgoyette
142 1.1 pgoyette dp = opendir(".");
143 1.1 pgoyette if (dp == NULL)
144 1.1 pgoyette atf_tc_fail("Could not open current directory");
145 1.1 pgoyette
146 1.6 christos (void)telldir(dp);
147 1.1 pgoyette memused = sbrk(0);
148 1.1 pgoyette closedir(dp);
149 1.1 pgoyette
150 1.3 christos for (i = 0; i < 1000; i++) {
151 1.1 pgoyette dp = opendir(".");
152 1.1 pgoyette if (dp == NULL)
153 1.1 pgoyette atf_tc_fail("Could not open current directory");
154 1.1 pgoyette
155 1.6 christos (void)telldir(dp);
156 1.1 pgoyette closedir(dp);
157 1.1 pgoyette
158 1.3 christos if ((char *)sbrk(0) - memused > oktouse) {
159 1.1 pgoyette (void)printf("Used %td extra bytes for %d telldir "
160 1.3 christos "calls", ((char *)sbrk(0) - memused), i);
161 1.3 christos oktouse = (char *)sbrk(0) - memused;
162 1.1 pgoyette }
163 1.1 pgoyette }
164 1.1 pgoyette if (oktouse > 4096) {
165 1.3 christos atf_tc_fail("Failure: leaked %d bytes", oktouse);
166 1.1 pgoyette } else {
167 1.1 pgoyette (void)printf("OK: used %td bytes\n", (char *)(sbrk(0))-memused);
168 1.1 pgoyette }
169 1.1 pgoyette }
170 1.1 pgoyette
171 1.1 pgoyette ATF_TP_ADD_TCS(tp)
172 1.1 pgoyette {
173 1.1 pgoyette
174 1.4 jruoho ATF_TP_ADD_TC(tp, seekdir_basic);
175 1.1 pgoyette ATF_TP_ADD_TC(tp, telldir_leak);
176 1.1 pgoyette
177 1.1 pgoyette return atf_no_error();
178 1.1 pgoyette }
179