2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2017 Spectra Logic Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
30 * Test cases for operations on DIR objects:
31 * opendir, readdir, seekdir, telldir, closedir, etc
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
44 ATF_TC(telldir_after_seekdir);
45 ATF_TC_HEAD(telldir_after_seekdir, tc)
48 atf_tc_set_md_var(tc, "descr", "Calling telldir(3) after seekdir(3) "
49 "should return the argument passed to seekdir.");
51 ATF_TC_BODY(telldir_after_seekdir, tc)
53 const int NUMFILES = 1000;
54 char template[] = "dXXXXXX";
59 long beginning, middle, end, td;
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);
68 * Fill it with files. Must be > 128 to ensure that the directory
69 * can't fit within a single page
71 for (i = 0; i < NUMFILES; i = i+1) {
75 snprintf(filename, sizeof(filename), "%d", i);
76 fd = openat(dirfd, filename, O_WRONLY | O_CREAT);
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) {
87 ATF_REQUIRE_MSG(de != NULL, "readdir failed");
89 middle = telldir(dirp);
90 for (; i < NUMFILES - 1; i = i+1) {
92 ATF_REQUIRE_MSG(de != NULL, "readdir failed");
97 * Seekdir to each bookmark, check the telldir after seekdir condition,
98 * and check that the bookmark is valid by reading another directory
102 seekdir(dirp, beginning);
104 ATF_CHECK_EQ(beginning, td);
105 ATF_REQUIRE_MSG(NULL != readdir(dirp), "invalid directory index");
107 seekdir(dirp, middle);
109 ATF_CHECK_EQ(middle, td);
110 ATF_REQUIRE_MSG(NULL != readdir(dirp), "invalid directory index");
114 ATF_CHECK_EQ(end, td);
115 ATF_REQUIRE_MSG(NULL != readdir(dirp), "invalid directory index");
120 ATF_TC(telldir_at_end_of_block);
121 ATF_TC_HEAD(telldir_at_end_of_block, tc)
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");
126 ATF_TC_BODY(telldir_at_end_of_block, tc)
128 /* For UFS and ZFS, blocks roll over at 128 directory entries. */
129 const int NUMFILES = 129;
130 char template[] = "dXXXXXX";
136 char last_filename[16];
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);
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 ".."
148 for (i = 0; i < NUMFILES - 2; i = i+1) {
152 snprintf(filename, sizeof(filename), "%d", i);
153 fd = openat(dirfd, filename, O_WRONLY | O_CREAT);
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");
164 /* Call telldir at the end of a page */
167 /* Read the last entry */
169 ATF_REQUIRE_MSG(de != NULL, "readdir failed");
170 strlcpy(last_filename, de->d_name, sizeof(last_filename));
172 /* Seek back to the bookmark. readdir() should return the last entry */
175 ATF_REQUIRE_STREQ_MSG(last_filename, de->d_name,
176 "seekdir went to the wrong directory position");
185 ATF_TP_ADD_TC(tp, telldir_after_seekdir);
186 ATF_TP_ADD_TC(tp, telldir_at_end_of_block);
188 return atf_no_error();