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