t_dir.c revision 312091
1/* $NetBSD: t_dir.c,v 1.6 2013/10/19 17:45:00 christos Exp $ */ 2 3/*- 4 * Copyright (c) 2010 The NetBSD Foundation, Inc. 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#include <atf-c.h> 30 31#include <assert.h> 32#include <dirent.h> 33#include <err.h> 34#include <fcntl.h> 35#include <stdio.h> 36#include <stdlib.h> 37#include <string.h> 38#include <unistd.h> 39 40#include <sys/stat.h> 41 42#ifdef __FreeBSD__ 43#include <errno.h> 44#endif 45 46ATF_TC(seekdir_basic); 47ATF_TC_HEAD(seekdir_basic, tc) 48{ 49 50 atf_tc_set_md_var(tc, "descr", "Check telldir(3) and seekdir(3) " 51 "for correct behavior (PR lib/24324)"); 52} 53 54ATF_TC_BODY(seekdir_basic, tc) 55{ 56 DIR *dp; 57 char *wasname; 58 struct dirent *entry; 59 long here; 60 61#ifdef __FreeBSD__ 62#define CREAT(x, m) do { \ 63 int _creat_fd; \ 64 ATF_REQUIRE_MSG((_creat_fd = creat((x), (m))) != -1, \ 65 "creat(%s, %x) failed: %s", (x), (m), \ 66 strerror(errno)); \ 67 (void)close(_creat_fd); \ 68 } while(0); 69 70 ATF_REQUIRE_MSG(mkdir("t", 0755) == 0, 71 "mkdir failed: %s", strerror(errno)); 72 CREAT("t/a", 0600); 73 CREAT("t/b", 0600); 74 CREAT("t/c", 0600); 75#else 76 mkdir("t", 0755); 77 creat("t/a", 0600); 78 creat("t/b", 0600); 79 creat("t/c", 0600); 80#endif 81 82 dp = opendir("t"); 83 if ( dp == NULL) 84 atf_tc_fail("Could not open temp directory."); 85 86 /* skip two for . and .. */ 87 entry = readdir(dp); 88 entry = readdir(dp); 89 90 /* get first entry */ 91 entry = readdir(dp); 92 here = telldir(dp); 93#ifdef __FreeBSD__ 94 ATF_REQUIRE_MSG(here != -1, 95 "telldir failed: %s", strerror(errno)); 96#endif 97 98 /* get second entry */ 99 entry = readdir(dp); 100#ifdef __FreeBSD__ 101 ATF_REQUIRE_MSG(entry != NULL, 102 "readdir failed: %s", strerror(errno)); 103#endif 104 wasname = strdup(entry->d_name); 105 if (wasname == NULL) 106 atf_tc_fail("cannot allocate memory"); 107 108 /* get third entry */ 109 entry = readdir(dp); 110 111 /* try to return to the position after the first entry */ 112 seekdir(dp, here); 113 entry = readdir(dp); 114 115 if (entry == NULL) 116 atf_tc_fail("entry 1 not found"); 117 if (strcmp(entry->d_name, wasname) != 0) 118 atf_tc_fail("1st seekdir found wrong name"); 119 120 /* try again, and throw in a telldir() for good measure */ 121 seekdir(dp, here); 122 here = telldir(dp); 123 entry = readdir(dp); 124 125 if (entry == NULL) 126 atf_tc_fail("entry 2 not found"); 127 if (strcmp(entry->d_name, wasname) != 0) 128 atf_tc_fail("2nd seekdir found wrong name"); 129 130 /* One more time, to make sure that telldir() doesn't affect result */ 131 seekdir(dp, here); 132 entry = readdir(dp); 133 134 if (entry == NULL) 135 atf_tc_fail("entry 3 not found"); 136 if (strcmp(entry->d_name, wasname) != 0) 137 atf_tc_fail("3rd seekdir found wrong name"); 138 139 closedir(dp); 140#ifdef __FreeBSD__ 141 free(wasname); 142#endif 143} 144 145ATF_TC(telldir_leak); 146ATF_TC_HEAD(telldir_leak, tc) 147{ 148 149 atf_tc_set_md_var(tc, "descr", 150 "Check telldir(3) for memory leakage (PR lib/24324)"); 151} 152 153ATF_TC_BODY(telldir_leak, tc) 154{ 155 DIR *dp; 156 char *memused; 157 int i; 158 int oktouse = 4096; 159 160 dp = opendir("."); 161 if (dp == NULL) 162 atf_tc_fail("Could not open current directory"); 163 164 (void)telldir(dp); 165 memused = sbrk(0); 166 closedir(dp); 167 168 for (i = 0; i < 1000; i++) { 169 dp = opendir("."); 170 if (dp == NULL) 171 atf_tc_fail("Could not open current directory"); 172 173 (void)telldir(dp); 174 closedir(dp); 175 176 if ((char *)sbrk(0) - memused > oktouse) { 177 (void)printf("Used %td extra bytes for %d telldir " 178 "calls", ((char *)sbrk(0) - memused), i); 179 oktouse = (char *)sbrk(0) - memused; 180 } 181 } 182 if (oktouse > 4096) { 183 atf_tc_fail("Failure: leaked %d bytes", oktouse); 184 } else { 185 (void)printf("OK: used %td bytes\n", (char *)(sbrk(0))-memused); 186 } 187} 188 189ATF_TP_ADD_TCS(tp) 190{ 191 192 ATF_TP_ADD_TC(tp, seekdir_basic); 193 ATF_TP_ADD_TC(tp, telldir_leak); 194 195 return atf_no_error(); 196} 197