t_dir.c revision 1.5
1/* $NetBSD: t_dir.c,v 1.5 2012/03/18 07:14:08 jruoho 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 42ATF_TC(seekdir_basic); 43ATF_TC_HEAD(seekdir_basic, tc) 44{ 45 46 atf_tc_set_md_var(tc, "descr", "Check telldir(3) and seekdir(3) " 47 "for correct behavior (PR lib/24324)"); 48} 49 50ATF_TC_BODY(seekdir_basic, tc) 51{ 52 DIR *dp; 53 char *wasname; 54 struct dirent *entry; 55 long here; 56 57 mkdir("t", 0755); 58 creat("t/a", 0600); 59 creat("t/b", 0600); 60 creat("t/c", 0600); 61 62 dp = opendir("t"); 63 if ( dp == NULL) 64 atf_tc_fail("Could not open temp directory."); 65 66 /* skip two for . and .. */ 67 entry = readdir(dp); 68 entry = readdir(dp); 69 70 /* get first entry */ 71 entry = readdir(dp); 72 here = telldir(dp); 73 74 /* get second entry */ 75 entry = readdir(dp); 76 wasname = strdup(entry->d_name); 77 if (wasname == NULL) 78 atf_tc_fail("cannot allocate memory"); 79 80 /* get third entry */ 81 entry = readdir(dp); 82 83 /* try to return to the position after the first entry */ 84 seekdir(dp, here); 85 entry = readdir(dp); 86 87 if (entry == NULL) 88 atf_tc_fail("entry 1 not found"); 89 if (strcmp(entry->d_name, wasname) != 0) 90 atf_tc_fail("1st seekdir found wrong name"); 91 92 /* try again, and throw in a telldir() for good measure */ 93 seekdir(dp, here); 94 here = telldir(dp); 95 entry = readdir(dp); 96 97 if (entry == NULL) 98 atf_tc_fail("entry 2 not found"); 99 if (strcmp(entry->d_name, wasname) != 0) 100 atf_tc_fail("2nd seekdir found wrong name"); 101 102 /* One more time, to make sure that telldir() doesn't affect result */ 103 seekdir(dp, here); 104 entry = readdir(dp); 105 106 if (entry == NULL) 107 atf_tc_fail("entry 3 not found"); 108 if (strcmp(entry->d_name, wasname) != 0) 109 atf_tc_fail("3rd seekdir found wrong name"); 110 111 closedir(dp); 112} 113 114ATF_TC(telldir_leak); 115ATF_TC_HEAD(telldir_leak, tc) 116{ 117 118 atf_tc_set_md_var(tc, "descr", 119 "Check telldir(3) for memory leakage (PR lib/24324)"); 120} 121 122ATF_TC_BODY(telldir_leak, tc) 123{ 124 DIR *dp; 125 long loc; 126 char *memused; 127 int i; 128 int oktouse = 4096; 129 130 dp = opendir("."); 131 if (dp == NULL) 132 atf_tc_fail("Could not open current directory"); 133 134 loc = telldir(dp); 135 memused = sbrk(0); 136 closedir(dp); 137 138 for (i = 0; i < 1000; i++) { 139 dp = opendir("."); 140 if (dp == NULL) 141 atf_tc_fail("Could not open current directory"); 142 143 loc = telldir(dp); 144 closedir(dp); 145 146 if ((char *)sbrk(0) - memused > oktouse) { 147 (void)printf("Used %td extra bytes for %d telldir " 148 "calls", ((char *)sbrk(0) - memused), i); 149 oktouse = (char *)sbrk(0) - memused; 150 } 151 } 152 if (oktouse > 4096) { 153 atf_tc_fail("Failure: leaked %d bytes", oktouse); 154 } else { 155 (void)printf("OK: used %td bytes\n", (char *)(sbrk(0))-memused); 156 } 157} 158 159ATF_TP_ADD_TCS(tp) 160{ 161 162 ATF_TP_ADD_TC(tp, seekdir_basic); 163 ATF_TP_ADD_TC(tp, telldir_leak); 164 165 return atf_no_error(); 166} 167