1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2017 Spectra Logic Corporation
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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /*
30 * Test cases for operations on DIR objects:
31 * opendir, readdir, seekdir, telldir, closedir, etc
32 */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <dirent.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41
42 #include <atf-c.h>
43
44 ATF_TC(telldir_after_seekdir);
ATF_TC_HEAD(telldir_after_seekdir,tc)45 ATF_TC_HEAD(telldir_after_seekdir, tc)
46 {
47
48 atf_tc_set_md_var(tc, "descr", "Calling telldir(3) after seekdir(3) "
49 "should return the argument passed to seekdir.");
50 }
ATF_TC_BODY(telldir_after_seekdir,tc)51 ATF_TC_BODY(telldir_after_seekdir, tc)
52 {
53 const int NUMFILES = 1000;
54 char template[] = "dXXXXXX";
55 char *tmpdir;
56 int i, dirfd;
57 DIR *dirp;
58 struct dirent *de;
59 long beginning, middle, end, td;
60
61 /* Create a temporary directory */
62 tmpdir = mkdtemp(template);
63 ATF_REQUIRE_MSG(tmpdir != NULL, "mkdtemp failed");
64 dirfd = open(tmpdir, O_RDONLY | O_DIRECTORY);
65 ATF_REQUIRE(dirfd > 0);
66
67 /*
68 * Fill it with files. Must be > 128 to ensure that the directory
69 * can't fit within a single page
70 */
71 for (i = 0; i < NUMFILES; i = i+1) {
72 int fd;
73 char filename[16];
74
75 snprintf(filename, sizeof(filename), "%d", i);
76 fd = openat(dirfd, filename, O_WRONLY | O_CREAT, 0600);
77 ATF_REQUIRE(fd > 0);
78 close(fd);
79 }
80
81 /* Get some directory bookmarks in various locations */
82 dirp = fdopendir(dirfd);
83 ATF_REQUIRE_MSG(dirfd >= 0, "fdopendir failed");
84 beginning = telldir(dirp);
85 for (i = 0; i < NUMFILES / 2; i = i+1) {
86 de = readdir(dirp);
87 ATF_REQUIRE_MSG(de != NULL, "readdir failed");
88 }
89 middle = telldir(dirp);
90 for (; i < NUMFILES - 1; i = i+1) {
91 de = readdir(dirp);
92 ATF_REQUIRE_MSG(de != NULL, "readdir failed");
93 }
94 end = telldir(dirp);
95
96 /*
97 * Seekdir to each bookmark, check the telldir after seekdir condition,
98 * and check that the bookmark is valid by reading another directory
99 * entry.
100 */
101
102 seekdir(dirp, beginning);
103 td = telldir(dirp);
104 ATF_CHECK_EQ(beginning, td);
105 ATF_REQUIRE_MSG(NULL != readdir(dirp), "invalid directory index");
106
107 seekdir(dirp, middle);
108 td = telldir(dirp);
109 ATF_CHECK_EQ(middle, td);
110 ATF_REQUIRE_MSG(NULL != readdir(dirp), "invalid directory index");
111
112 seekdir(dirp, end);
113 td = telldir(dirp);
114 ATF_CHECK_EQ(end, td);
115 ATF_REQUIRE_MSG(NULL != readdir(dirp), "invalid directory index");
116
117 closedir(dirp);
118 }
119
120 ATF_TC(telldir_at_end_of_block);
ATF_TC_HEAD(telldir_at_end_of_block,tc)121 ATF_TC_HEAD(telldir_at_end_of_block, tc)
122 {
123
124 atf_tc_set_md_var(tc, "descr", "Calling telldir(3) after readdir(3) read the last entry in the block should return a valid location");
125 }
ATF_TC_BODY(telldir_at_end_of_block,tc)126 ATF_TC_BODY(telldir_at_end_of_block, tc)
127 {
128 /* For UFS and ZFS, blocks roll over at 128 directory entries. */
129 const int NUMFILES = 129;
130 char template[] = "dXXXXXX";
131 char *tmpdir;
132 int i, dirfd;
133 DIR *dirp;
134 struct dirent *de;
135 long td;
136 char last_filename[16];
137
138 /* Create a temporary directory */
139 tmpdir = mkdtemp(template);
140 ATF_REQUIRE_MSG(tmpdir != NULL, "mkdtemp failed");
141 dirfd = open(tmpdir, O_RDONLY | O_DIRECTORY);
142 ATF_REQUIRE(dirfd > 0);
143
144 /*
145 * Fill it with files. Must be > 128 to ensure that the directory
146 * can't fit within a single page. The "-2" accounts for "." and ".."
147 */
148 for (i = 0; i < NUMFILES - 2; i = i+1) {
149 int fd;
150 char filename[16];
151
152 snprintf(filename, sizeof(filename), "%d", i);
153 fd = openat(dirfd, filename, O_WRONLY | O_CREAT, 0600);
154 ATF_REQUIRE(fd > 0);
155 close(fd);
156 }
157
158 /* Read all entries within the first page */
159 dirp = fdopendir(dirfd);
160 ATF_REQUIRE_MSG(dirfd >= 0, "fdopendir failed");
161 for (i = 0; i < NUMFILES - 1; i = i + 1)
162 ATF_REQUIRE_MSG(readdir(dirp) != NULL, "readdir failed");
163
164 /* Call telldir at the end of a page */
165 td = telldir(dirp);
166
167 /* Read the last entry */
168 de = readdir(dirp);
169 ATF_REQUIRE_MSG(de != NULL, "readdir failed");
170 strlcpy(last_filename, de->d_name, sizeof(last_filename));
171
172 /* Seek back to the bookmark. readdir() should return the last entry */
173 seekdir(dirp, td);
174 de = readdir(dirp);
175 ATF_REQUIRE_STREQ_MSG(last_filename, de->d_name,
176 "seekdir went to the wrong directory position");
177
178 closedir(dirp);
179 }
180
181
ATF_TP_ADD_TCS(tp)182 ATF_TP_ADD_TCS(tp)
183 {
184
185 ATF_TP_ADD_TC(tp, telldir_after_seekdir);
186 ATF_TP_ADD_TC(tp, telldir_at_end_of_block);
187
188 return atf_no_error();
189 }
190