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