xref: /netbsd-src/tests/lib/libc/gen/t_dir.c (revision 036399b6f4d849845f669018dc48192aecd24246)
1*036399b6Sgson /* $NetBSD: t_dir.c,v 1.11 2018/06/19 09:20:46 gson Exp $ */
266bfd224Spgoyette 
366bfd224Spgoyette /*-
466bfd224Spgoyette  * Copyright (c) 2010 The NetBSD Foundation, Inc.
566bfd224Spgoyette  * All rights reserved.
666bfd224Spgoyette  *
766bfd224Spgoyette  * Redistribution and use in source and binary forms, with or without
866bfd224Spgoyette  * modification, are permitted provided that the following conditions
966bfd224Spgoyette  * are met:
1066bfd224Spgoyette  * 1. Redistributions of source code must retain the above copyright
1166bfd224Spgoyette  *    notice, this list of conditions and the following disclaimer.
1266bfd224Spgoyette  * 2. Redistributions in binary form must reproduce the above copyright
1366bfd224Spgoyette  *    notice, this list of conditions and the following disclaimer in the
1466bfd224Spgoyette  *    documentation and/or other materials provided with the distribution.
1566bfd224Spgoyette  *
1666bfd224Spgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1766bfd224Spgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1866bfd224Spgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1966bfd224Spgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2066bfd224Spgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2166bfd224Spgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2266bfd224Spgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2366bfd224Spgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2466bfd224Spgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2566bfd224Spgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2666bfd224Spgoyette  * POSSIBILITY OF SUCH DAMAGE.
2766bfd224Spgoyette  */
2866bfd224Spgoyette 
29d78006e8Schristos #include <sys/stat.h>
3066bfd224Spgoyette #include <assert.h>
31d78006e8Schristos #include <atf-c.h>
3266bfd224Spgoyette #include <dirent.h>
3366bfd224Spgoyette #include <err.h>
34d78006e8Schristos #include <errno.h>
3566bfd224Spgoyette #include <fcntl.h>
3666bfd224Spgoyette #include <stdio.h>
3766bfd224Spgoyette #include <stdlib.h>
3866bfd224Spgoyette #include <string.h>
3966bfd224Spgoyette #include <unistd.h>
4066bfd224Spgoyette 
41d78006e8Schristos 
4266bfd224Spgoyette 
439a514225Sjruoho ATF_TC(seekdir_basic);
ATF_TC_HEAD(seekdir_basic,tc)449a514225Sjruoho ATF_TC_HEAD(seekdir_basic, tc)
4566bfd224Spgoyette {
4666bfd224Spgoyette 
4772ed5c06Sjruoho 	atf_tc_set_md_var(tc, "descr", "Check telldir(3) and seekdir(3) "
4872ed5c06Sjruoho 	    "for correct behavior (PR lib/24324)");
4966bfd224Spgoyette }
5066bfd224Spgoyette 
ATF_TC_BODY(seekdir_basic,tc)519a514225Sjruoho ATF_TC_BODY(seekdir_basic, tc)
5266bfd224Spgoyette {
5366bfd224Spgoyette 	DIR *dp;
5466bfd224Spgoyette 	char *wasname;
5566bfd224Spgoyette 	struct dirent *entry;
5666bfd224Spgoyette 	long here;
5766bfd224Spgoyette 
58d78006e8Schristos #define	CREAT(x, m)	do {						\
59d78006e8Schristos 		int _creat_fd;						\
602b582fd0Schristos 		ATF_REQUIRE_MSG((_creat_fd = creat((x), (m))) != -1,	\
61d78006e8Schristos 		    "creat(%s, %x) failed: %s", (x), (m),		\
62d78006e8Schristos 		    strerror(errno));					\
63d78006e8Schristos 		(void)close(_creat_fd);					\
64*036399b6Sgson 	} while (0)
65d78006e8Schristos 
66d78006e8Schristos 	ATF_REQUIRE_MSG(mkdir("t", 0755) == 0,
67d78006e8Schristos 	    "mkdir failed: %s", strerror(errno));
68d78006e8Schristos 	CREAT("t/a", 0600);
69d78006e8Schristos 	CREAT("t/b", 0600);
70d78006e8Schristos 	CREAT("t/c", 0600);
7166bfd224Spgoyette 
7266bfd224Spgoyette 	dp = opendir("t");
7366bfd224Spgoyette 	if ( dp == NULL)
7466bfd224Spgoyette 		atf_tc_fail("Could not open temp directory.");
7566bfd224Spgoyette 
7666bfd224Spgoyette 	/* skip two for . and .. */
7766bfd224Spgoyette 	entry = readdir(dp);
78af5b1487Schristos 	ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
79af5b1487Schristos 	    ".", strerror(errno));
80af5b1487Schristos 
8166bfd224Spgoyette 	entry = readdir(dp);
82af5b1487Schristos 	ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
83af5b1487Schristos 	    "..", strerror(errno));
8466bfd224Spgoyette 
8566bfd224Spgoyette 	/* get first entry */
8666bfd224Spgoyette 	entry = readdir(dp);
87af5b1487Schristos 	ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
88af5b1487Schristos 	    "first", strerror(errno));
89af5b1487Schristos 
9066bfd224Spgoyette 	here = telldir(dp);
91af5b1487Schristos 	ATF_REQUIRE_MSG(here != -1, "telldir failed: %s", strerror(errno));
9266bfd224Spgoyette 
9366bfd224Spgoyette 	/* get second entry */
9466bfd224Spgoyette 	entry = readdir(dp);
95af5b1487Schristos 	ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
96af5b1487Schristos 	    "second", strerror(errno));
97af5b1487Schristos 
9866bfd224Spgoyette 	wasname = strdup(entry->d_name);
992a18cea9Schristos 	if (wasname == NULL)
1002a18cea9Schristos 		atf_tc_fail("cannot allocate memory");
10166bfd224Spgoyette 
10266bfd224Spgoyette 	/* get third entry */
10366bfd224Spgoyette 	entry = readdir(dp);
104af5b1487Schristos 	ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
105af5b1487Schristos 	    "third", strerror(errno));
10666bfd224Spgoyette 
10766bfd224Spgoyette 	/* try to return to the position after the first entry */
10866bfd224Spgoyette 	seekdir(dp, here);
10966bfd224Spgoyette 	entry = readdir(dp);
110af5b1487Schristos 	ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
111af5b1487Schristos 	    "first[1]", strerror(errno));
11266bfd224Spgoyette 	if (strcmp(entry->d_name, wasname) != 0)
11366bfd224Spgoyette 		atf_tc_fail("1st seekdir found wrong name");
11466bfd224Spgoyette 
11566bfd224Spgoyette 	/* try again, and throw in a telldir() for good measure */
11666bfd224Spgoyette 	seekdir(dp, here);
11766bfd224Spgoyette 	here = telldir(dp);
11866bfd224Spgoyette 	entry = readdir(dp);
119af5b1487Schristos 	ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
120af5b1487Schristos 	    "second[1]", strerror(errno));
12166bfd224Spgoyette 	if (strcmp(entry->d_name, wasname) != 0)
12266bfd224Spgoyette 		atf_tc_fail("2nd seekdir found wrong name");
12366bfd224Spgoyette 
12466bfd224Spgoyette 	/* One more time, to make sure that telldir() doesn't affect result */
12566bfd224Spgoyette 	seekdir(dp, here);
12666bfd224Spgoyette 	entry = readdir(dp);
127af5b1487Schristos 	ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
128af5b1487Schristos 	    "third[1]", strerror(errno));
12966bfd224Spgoyette 
13066bfd224Spgoyette 	if (strcmp(entry->d_name, wasname) != 0)
13166bfd224Spgoyette 		atf_tc_fail("3rd seekdir found wrong name");
13266bfd224Spgoyette 
13366bfd224Spgoyette 	closedir(dp);
134d78006e8Schristos 	free(wasname);
13566bfd224Spgoyette }
13666bfd224Spgoyette 
13766bfd224Spgoyette ATF_TC(telldir_leak);
ATF_TC_HEAD(telldir_leak,tc)13866bfd224Spgoyette ATF_TC_HEAD(telldir_leak, tc)
13966bfd224Spgoyette {
14066bfd224Spgoyette 
14166bfd224Spgoyette 	atf_tc_set_md_var(tc, "descr",
14272ed5c06Sjruoho 	    "Check telldir(3) for memory leakage (PR lib/24324)");
14366bfd224Spgoyette }
14466bfd224Spgoyette 
ATF_TC_BODY(telldir_leak,tc)14566bfd224Spgoyette ATF_TC_BODY(telldir_leak, tc)
14666bfd224Spgoyette {
14766bfd224Spgoyette 	DIR *dp;
14866bfd224Spgoyette 	char *memused;
14966bfd224Spgoyette 	int i;
15066bfd224Spgoyette 	int oktouse = 4096;
15166bfd224Spgoyette 
15266bfd224Spgoyette 	dp = opendir(".");
15366bfd224Spgoyette 	if (dp == NULL)
15466bfd224Spgoyette 		atf_tc_fail("Could not open current directory");
15566bfd224Spgoyette 
156d56fcfc9Schristos 	(void)telldir(dp);
15766bfd224Spgoyette 	memused = sbrk(0);
15866bfd224Spgoyette 	closedir(dp);
15966bfd224Spgoyette 
16066bfd224Spgoyette 	for (i = 0; i < 1000; i++) {
16166bfd224Spgoyette 		dp = opendir(".");
16266bfd224Spgoyette 		if (dp == NULL)
16366bfd224Spgoyette 			atf_tc_fail("Could not open current directory");
16466bfd224Spgoyette 
165d56fcfc9Schristos 		(void)telldir(dp);
16666bfd224Spgoyette 		closedir(dp);
16766bfd224Spgoyette 
1682a18cea9Schristos 		if ((char *)sbrk(0) - memused > oktouse) {
16966bfd224Spgoyette 			(void)printf("Used %td extra bytes for %d telldir "
1702a18cea9Schristos 			    "calls", ((char *)sbrk(0) - memused), i);
1712a18cea9Schristos 			oktouse = (char *)sbrk(0) - memused;
17266bfd224Spgoyette 		}
17366bfd224Spgoyette 	}
17466bfd224Spgoyette 	if (oktouse > 4096) {
1752a18cea9Schristos 		atf_tc_fail("Failure: leaked %d bytes", oktouse);
17666bfd224Spgoyette 	} else {
17766bfd224Spgoyette 		(void)printf("OK: used %td bytes\n", (char *)(sbrk(0))-memused);
17866bfd224Spgoyette 	}
17966bfd224Spgoyette }
18066bfd224Spgoyette 
ATF_TP_ADD_TCS(tp)18166bfd224Spgoyette ATF_TP_ADD_TCS(tp)
18266bfd224Spgoyette {
18366bfd224Spgoyette 
1849a514225Sjruoho 	ATF_TP_ADD_TC(tp, seekdir_basic);
18566bfd224Spgoyette 	ATF_TP_ADD_TC(tp, telldir_leak);
18666bfd224Spgoyette 
18766bfd224Spgoyette 	return atf_no_error();
18866bfd224Spgoyette }
189