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