xref: /freebsd/lib/libc/tests/gen/dir2_test.c (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
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 <dirent.h>
35 #include <fcntl.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 
39 #include <atf-c.h>
40 
41 ATF_TC(telldir_after_seekdir);
42 ATF_TC_HEAD(telldir_after_seekdir, tc)
43 {
44 
45 	atf_tc_set_md_var(tc, "descr", "Calling telldir(3) after seekdir(3) "
46 	    "should return the argument passed to seekdir.");
47 }
48 ATF_TC_BODY(telldir_after_seekdir, tc)
49 {
50 	const int NUMFILES = 1000;
51 	char template[] = "dXXXXXX";
52 	char *tmpdir;
53 	int i, dirfd;
54 	DIR *dirp;
55 	struct dirent *de;
56 	long beginning, middle, end, td;
57 
58 	/* Create a temporary directory */
59 	tmpdir = mkdtemp(template);
60 	ATF_REQUIRE_MSG(tmpdir != NULL, "mkdtemp failed");
61 	dirfd = open(tmpdir, O_RDONLY | O_DIRECTORY);
62 	ATF_REQUIRE(dirfd > 0);
63 
64 	/*
65 	 * Fill it with files.  Must be > 128 to ensure that the directory
66 	 * can't fit within a single page
67 	 */
68 	for (i = 0; i < NUMFILES; i = i+1) {
69 		int fd;
70 		char filename[16];
71 
72 		snprintf(filename, sizeof(filename), "%d", i);
73 		fd = openat(dirfd, filename, O_WRONLY | O_CREAT, 0600);
74 		ATF_REQUIRE(fd > 0);
75 		close(fd);
76 	}
77 
78 	/* Get some directory bookmarks in various locations */
79 	dirp = fdopendir(dirfd);
80 	ATF_REQUIRE_MSG(dirfd >= 0, "fdopendir failed");
81 	beginning = telldir(dirp);
82 	for (i = 0; i < NUMFILES / 2; i = i+1) {
83 		de = readdir(dirp);
84 		ATF_REQUIRE_MSG(de != NULL, "readdir failed");
85 	}
86 	middle = telldir(dirp);
87 	for (; i < NUMFILES - 1; i = i+1) {
88 		de = readdir(dirp);
89 		ATF_REQUIRE_MSG(de != NULL, "readdir failed");
90 	}
91 	end = telldir(dirp);
92 
93 	/*
94 	 * Seekdir to each bookmark, check the telldir after seekdir condition,
95 	 * and check that the bookmark is valid by reading another directory
96 	 * entry.
97 	 */
98 
99 	seekdir(dirp, beginning);
100 	td = telldir(dirp);
101 	ATF_CHECK_EQ(beginning, td);
102 	ATF_REQUIRE_MSG(NULL != readdir(dirp), "invalid directory index");
103 
104 	seekdir(dirp, middle);
105 	td = telldir(dirp);
106 	ATF_CHECK_EQ(middle, td);
107 	ATF_REQUIRE_MSG(NULL != readdir(dirp), "invalid directory index");
108 
109 	seekdir(dirp, end);
110 	td = telldir(dirp);
111 	ATF_CHECK_EQ(end, td);
112 	ATF_REQUIRE_MSG(NULL != readdir(dirp), "invalid directory index");
113 
114 	closedir(dirp);
115 }
116 
117 ATF_TC(telldir_at_end_of_block);
118 ATF_TC_HEAD(telldir_at_end_of_block, tc)
119 {
120 
121 	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");
122 }
123 ATF_TC_BODY(telldir_at_end_of_block, tc)
124 {
125 	/* For UFS and ZFS, blocks roll over at 128 directory entries.  */
126 	const int NUMFILES = 129;
127 	char template[] = "dXXXXXX";
128 	char *tmpdir;
129 	int i, dirfd;
130 	DIR *dirp;
131 	struct dirent *de;
132 	long td;
133 	char last_filename[16];
134 
135 	/* Create a temporary directory */
136 	tmpdir = mkdtemp(template);
137 	ATF_REQUIRE_MSG(tmpdir != NULL, "mkdtemp failed");
138 	dirfd = open(tmpdir, O_RDONLY | O_DIRECTORY);
139 	ATF_REQUIRE(dirfd > 0);
140 
141 	/*
142 	 * Fill it with files.  Must be > 128 to ensure that the directory
143 	 * can't fit within a single page.  The "-2" accounts for "." and ".."
144 	 */
145 	for (i = 0; i < NUMFILES - 2; i = i+1) {
146 		int fd;
147 		char filename[16];
148 
149 		snprintf(filename, sizeof(filename), "%d", i);
150 		fd = openat(dirfd, filename, O_WRONLY | O_CREAT, 0600);
151 		ATF_REQUIRE(fd > 0);
152 		close(fd);
153 	}
154 
155 	/* Read all entries within the first page */
156 	dirp = fdopendir(dirfd);
157 	ATF_REQUIRE_MSG(dirfd >= 0, "fdopendir failed");
158 	for (i = 0; i < NUMFILES - 1; i = i + 1)
159 		ATF_REQUIRE_MSG(readdir(dirp) != NULL, "readdir failed");
160 
161 	/* Call telldir at the end of a page */
162 	td = telldir(dirp);
163 
164 	/* Read the last entry */
165 	de = readdir(dirp);
166 	ATF_REQUIRE_MSG(de != NULL, "readdir failed");
167 	strlcpy(last_filename, de->d_name, sizeof(last_filename));
168 
169 	/* Seek back to the bookmark. readdir() should return the last entry */
170 	seekdir(dirp, td);
171 	de = readdir(dirp);
172 	ATF_REQUIRE_STREQ_MSG(last_filename, de->d_name,
173 			"seekdir went to the wrong directory position");
174 
175 	closedir(dirp);
176 }
177 
178 
179 ATF_TP_ADD_TCS(tp)
180 {
181 
182 	ATF_TP_ADD_TC(tp, telldir_after_seekdir);
183 	ATF_TP_ADD_TC(tp, telldir_at_end_of_block);
184 
185 	return atf_no_error();
186 }
187