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