aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/tests/gen/dir2_test.c
blob: 5c9604856f114f73113501f1e0d9e29b85743802 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 * 
 * Copyright (c) 2017 Spectra Logic Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * Test cases for operations on DIR objects:
 * opendir, readdir, seekdir, telldir, closedir, etc
 */

#include <sys/cdefs.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

#include <atf-c.h>

ATF_TC(telldir_after_seekdir);
ATF_TC_HEAD(telldir_after_seekdir, tc)
{

	atf_tc_set_md_var(tc, "descr", "Calling telldir(3) after seekdir(3) "
	    "should return the argument passed to seekdir.");
}
ATF_TC_BODY(telldir_after_seekdir, tc)
{
	const int NUMFILES = 1000;
	char template[] = "dXXXXXX";
	char *tmpdir;
	int i, dirfd;
	DIR *dirp;
	struct dirent *de;
	long beginning, middle, end, td;
	
	/* Create a temporary directory */
	tmpdir = mkdtemp(template);
	ATF_REQUIRE_MSG(tmpdir != NULL, "mkdtemp failed");
	dirfd = open(tmpdir, O_RDONLY | O_DIRECTORY);
	ATF_REQUIRE(dirfd > 0);

	/* 
	 * Fill it with files.  Must be > 128 to ensure that the directory
	 * can't fit within a single page
	 */
	for (i = 0; i < NUMFILES; i = i+1) {
		int fd;
		char filename[16];

		snprintf(filename, sizeof(filename), "%d", i);
		fd = openat(dirfd, filename, O_WRONLY | O_CREAT, 0600);
		ATF_REQUIRE(fd > 0);
		close(fd);
	}

	/* Get some directory bookmarks in various locations */
	dirp = fdopendir(dirfd);
	ATF_REQUIRE_MSG(dirfd >= 0, "fdopendir failed");
	beginning = telldir(dirp);
	for (i = 0; i < NUMFILES / 2; i = i+1) {
		de = readdir(dirp);
		ATF_REQUIRE_MSG(de != NULL, "readdir failed");
	}
	middle = telldir(dirp);
	for (; i < NUMFILES - 1; i = i+1) {
		de = readdir(dirp);
		ATF_REQUIRE_MSG(de != NULL, "readdir failed");
	}
	end = telldir(dirp);

	/*
	 * Seekdir to each bookmark, check the telldir after seekdir condition,
	 * and check that the bookmark is valid by reading another directory
	 * entry.
	 */

	seekdir(dirp, beginning);
	td = telldir(dirp);
	ATF_CHECK_EQ(beginning, td);
	ATF_REQUIRE_MSG(NULL != readdir(dirp), "invalid directory index");

	seekdir(dirp, middle);
	td = telldir(dirp);
	ATF_CHECK_EQ(middle, td);
	ATF_REQUIRE_MSG(NULL != readdir(dirp), "invalid directory index");

	seekdir(dirp, end);
	td = telldir(dirp);
	ATF_CHECK_EQ(end, td);
	ATF_REQUIRE_MSG(NULL != readdir(dirp), "invalid directory index");

	closedir(dirp);
}	

ATF_TC(telldir_at_end_of_block);
ATF_TC_HEAD(telldir_at_end_of_block, tc)
{

	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");
}
ATF_TC_BODY(telldir_at_end_of_block, tc)
{
	/* For UFS and ZFS, blocks roll over at 128 directory entries.  */
	const int NUMFILES = 129;
	char template[] = "dXXXXXX";
	char *tmpdir;
	int i, dirfd;
	DIR *dirp;
	struct dirent *de;
	long td;
	char last_filename[16];

	/* Create a temporary directory */
	tmpdir = mkdtemp(template);
	ATF_REQUIRE_MSG(tmpdir != NULL, "mkdtemp failed");
	dirfd = open(tmpdir, O_RDONLY | O_DIRECTORY);
	ATF_REQUIRE(dirfd > 0);

	/* 
	 * Fill it with files.  Must be > 128 to ensure that the directory
	 * can't fit within a single page.  The "-2" accounts for "." and ".."
	 */
	for (i = 0; i < NUMFILES - 2; i = i+1) {
		int fd;
		char filename[16];

		snprintf(filename, sizeof(filename), "%d", i);
		fd = openat(dirfd, filename, O_WRONLY | O_CREAT, 0600);
		ATF_REQUIRE(fd > 0);
		close(fd);
	}

	/* Read all entries within the first page */
	dirp = fdopendir(dirfd);
	ATF_REQUIRE_MSG(dirfd >= 0, "fdopendir failed");
	for (i = 0; i < NUMFILES - 1; i = i + 1)
		ATF_REQUIRE_MSG(readdir(dirp) != NULL, "readdir failed");

	/* Call telldir at the end of a page */
	td = telldir(dirp);

	/* Read the last entry */
	de = readdir(dirp);
	ATF_REQUIRE_MSG(de != NULL, "readdir failed");
	strlcpy(last_filename, de->d_name, sizeof(last_filename));

	/* Seek back to the bookmark. readdir() should return the last entry */
	seekdir(dirp, td);
	de = readdir(dirp);
	ATF_REQUIRE_STREQ_MSG(last_filename, de->d_name,
			"seekdir went to the wrong directory position");

	closedir(dirp);
}
	

ATF_TP_ADD_TCS(tp)
{

	ATF_TP_ADD_TC(tp, telldir_after_seekdir);
	ATF_TP_ADD_TC(tp, telldir_at_end_of_block);

	return atf_no_error();
}