t_dir.c revision 1.1
1/* $NetBSD: t_dir.c,v 1.1 2010/12/28 12:46:15 pgoyette Exp $ */ 2 3/*- 4 * Copyright (c) 2010 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#include <atf-c.h> 33 34#include <assert.h> 35#include <dirent.h> 36#include <err.h> 37#include <fcntl.h> 38#include <stdio.h> 39#include <stdlib.h> 40#include <string.h> 41#include <unistd.h> 42 43#include <sys/stat.h> 44 45ATF_TC(seekdir); 46 47ATF_TC_HEAD(seekdir, tc) 48{ 49 50 atf_tc_set_md_var(tc, "descr", 51 "Check telldir(3) and seekdir(3) for correct behavior (PR/24324)"); 52} 53 54ATF_TC_BODY(seekdir, tc) 55{ 56 DIR *dp; 57 char *wasname; 58 struct dirent *entry; 59 long here; 60 61 mkdir("t", 0755); 62 creat("t/a", 0600); 63 creat("t/b", 0600); 64 creat("t/c", 0600); 65 66 dp = opendir("t"); 67 if ( dp == NULL) 68 atf_tc_fail("Could not open temp directory."); 69 70 /* skip two for . and .. */ 71 entry = readdir(dp); 72 entry = readdir(dp); 73 74 /* get first entry */ 75 entry = readdir(dp); 76 here = telldir(dp); 77 78 /* get second entry */ 79 entry = readdir(dp); 80 wasname = strdup(entry->d_name); 81 82 /* get third entry */ 83 entry = readdir(dp); 84 85 /* try to return to the position after the first entry */ 86 seekdir(dp, here); 87 entry = readdir(dp); 88 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 (strcmp(entry->d_name, wasname) != 0) 98 atf_tc_fail("2nd seekdir found wrong name"); 99 100 /* One more time, to make sure that telldir() doesn't affect result */ 101 seekdir(dp, here); 102 entry = readdir(dp); 103 104 if (strcmp(entry->d_name, wasname) != 0) 105 atf_tc_fail("3rd seekdir found wrong name"); 106 107 closedir(dp); 108} 109 110ATF_TC(telldir_leak); 111 112ATF_TC_HEAD(telldir_leak, tc) 113{ 114 115 atf_tc_set_md_var(tc, "descr", 116 "Check telldir(3) for memory leakage (PR/24324)"); 117} 118 119ATF_TC_BODY(telldir_leak, tc) 120{ 121 DIR *dp; 122 long loc; 123 char *memused; 124 int i; 125 int oktouse = 4096; 126 127 dp = opendir("."); 128 if (dp == NULL) 129 atf_tc_fail("Could not open current directory"); 130 131 loc = telldir(dp); 132 memused = sbrk(0); 133 closedir(dp); 134 135 for (i=0; i<1000; i++) { 136 dp = opendir("."); 137 if (dp == NULL) 138 atf_tc_fail("Could not open current directory"); 139 140 loc = telldir(dp); 141 closedir(dp); 142 143 if ((char *)(sbrk(0)) - memused > oktouse) { 144 (void)printf("Used %td extra bytes for %d telldir " 145 "calls", ((char *)(sbrk(0)) - memused), i); 146 oktouse = (char *)(sbrk(0)) - memused; 147 } 148 } 149 if (oktouse > 4096) { 150 atf_tc_fail("Failure: leaked %td bytes", oktouse); 151 } else { 152 (void)printf("OK: used %td bytes\n", (char *)(sbrk(0))-memused); 153 } 154} 155 156ATF_TP_ADD_TCS(tp) 157{ 158 159 ATF_TP_ADD_TC(tp, seekdir); 160 ATF_TP_ADD_TC(tp, telldir_leak); 161 162 return atf_no_error(); 163} 164