xref: /minix3/tests/lib/libc/gen/t_dir.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /* $NetBSD: t_dir.c,v 1.6 2013/10/19 17:45:00 christos Exp $ */
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc  * Copyright (c) 2010 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc  * are met:
1011be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc  *
1611be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1711be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1811be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1911be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2011be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2111be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2211be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2311be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2411be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2511be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2611be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
2711be35a1SLionel Sambuc  */
2811be35a1SLionel Sambuc 
2911be35a1SLionel Sambuc #include <atf-c.h>
3011be35a1SLionel Sambuc 
3111be35a1SLionel Sambuc #include <assert.h>
3211be35a1SLionel Sambuc #include <dirent.h>
3311be35a1SLionel Sambuc #include <err.h>
3411be35a1SLionel Sambuc #include <fcntl.h>
3511be35a1SLionel Sambuc #include <stdio.h>
3611be35a1SLionel Sambuc #include <stdlib.h>
3711be35a1SLionel Sambuc #include <string.h>
3811be35a1SLionel Sambuc #include <unistd.h>
3911be35a1SLionel Sambuc 
4011be35a1SLionel Sambuc #include <sys/stat.h>
4111be35a1SLionel Sambuc 
4211be35a1SLionel Sambuc ATF_TC(seekdir_basic);
ATF_TC_HEAD(seekdir_basic,tc)4311be35a1SLionel Sambuc ATF_TC_HEAD(seekdir_basic, tc)
4411be35a1SLionel Sambuc {
4511be35a1SLionel Sambuc 
4611be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Check telldir(3) and seekdir(3) "
4711be35a1SLionel Sambuc 	    "for correct behavior (PR lib/24324)");
4811be35a1SLionel Sambuc }
4911be35a1SLionel Sambuc 
ATF_TC_BODY(seekdir_basic,tc)5011be35a1SLionel Sambuc ATF_TC_BODY(seekdir_basic, tc)
5111be35a1SLionel Sambuc {
5211be35a1SLionel Sambuc 	DIR *dp;
5311be35a1SLionel Sambuc 	char *wasname;
5411be35a1SLionel Sambuc 	struct dirent *entry;
5511be35a1SLionel Sambuc 	long here;
5611be35a1SLionel Sambuc 
5711be35a1SLionel Sambuc 	mkdir("t", 0755);
5811be35a1SLionel Sambuc 	creat("t/a", 0600);
5911be35a1SLionel Sambuc 	creat("t/b", 0600);
6011be35a1SLionel Sambuc 	creat("t/c", 0600);
6111be35a1SLionel Sambuc 
6211be35a1SLionel Sambuc 	dp = opendir("t");
6311be35a1SLionel Sambuc 	if ( dp == NULL)
6411be35a1SLionel Sambuc 		atf_tc_fail("Could not open temp directory.");
6511be35a1SLionel Sambuc 
6611be35a1SLionel Sambuc 	/* skip two for . and .. */
6711be35a1SLionel Sambuc 	entry = readdir(dp);
6811be35a1SLionel Sambuc 	entry = readdir(dp);
6911be35a1SLionel Sambuc 
7011be35a1SLionel Sambuc 	/* get first entry */
7111be35a1SLionel Sambuc 	entry = readdir(dp);
7211be35a1SLionel Sambuc 	here = telldir(dp);
7311be35a1SLionel Sambuc 
7411be35a1SLionel Sambuc 	/* get second entry */
7511be35a1SLionel Sambuc 	entry = readdir(dp);
7611be35a1SLionel Sambuc 	wasname = strdup(entry->d_name);
7711be35a1SLionel Sambuc 	if (wasname == NULL)
7811be35a1SLionel Sambuc 		atf_tc_fail("cannot allocate memory");
7911be35a1SLionel Sambuc 
8011be35a1SLionel Sambuc 	/* get third entry */
8111be35a1SLionel Sambuc 	entry = readdir(dp);
8211be35a1SLionel Sambuc 
8311be35a1SLionel Sambuc 	/* try to return to the position after the first entry */
8411be35a1SLionel Sambuc 	seekdir(dp, here);
8511be35a1SLionel Sambuc 	entry = readdir(dp);
8611be35a1SLionel Sambuc 
8711be35a1SLionel Sambuc 	if (entry == NULL)
8811be35a1SLionel Sambuc 		atf_tc_fail("entry 1 not found");
8911be35a1SLionel Sambuc 	if (strcmp(entry->d_name, wasname) != 0)
9011be35a1SLionel Sambuc 		atf_tc_fail("1st seekdir found wrong name");
9111be35a1SLionel Sambuc 
9211be35a1SLionel Sambuc 	/* try again, and throw in a telldir() for good measure */
9311be35a1SLionel Sambuc 	seekdir(dp, here);
9411be35a1SLionel Sambuc 	here = telldir(dp);
9511be35a1SLionel Sambuc 	entry = readdir(dp);
9611be35a1SLionel Sambuc 
9711be35a1SLionel Sambuc 	if (entry == NULL)
9811be35a1SLionel Sambuc 		atf_tc_fail("entry 2 not found");
9911be35a1SLionel Sambuc 	if (strcmp(entry->d_name, wasname) != 0)
10011be35a1SLionel Sambuc 		atf_tc_fail("2nd seekdir found wrong name");
10111be35a1SLionel Sambuc 
10211be35a1SLionel Sambuc 	/* One more time, to make sure that telldir() doesn't affect result */
10311be35a1SLionel Sambuc 	seekdir(dp, here);
10411be35a1SLionel Sambuc 	entry = readdir(dp);
10511be35a1SLionel Sambuc 
10611be35a1SLionel Sambuc 	if (entry == NULL)
10711be35a1SLionel Sambuc 		atf_tc_fail("entry 3 not found");
10811be35a1SLionel Sambuc 	if (strcmp(entry->d_name, wasname) != 0)
10911be35a1SLionel Sambuc 		atf_tc_fail("3rd seekdir found wrong name");
11011be35a1SLionel Sambuc 
11111be35a1SLionel Sambuc 	closedir(dp);
11211be35a1SLionel Sambuc }
11311be35a1SLionel Sambuc 
11411be35a1SLionel Sambuc ATF_TC(telldir_leak);
ATF_TC_HEAD(telldir_leak,tc)11511be35a1SLionel Sambuc ATF_TC_HEAD(telldir_leak, tc)
11611be35a1SLionel Sambuc {
11711be35a1SLionel Sambuc 
11811be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
11911be35a1SLionel Sambuc 	    "Check telldir(3) for memory leakage (PR lib/24324)");
12011be35a1SLionel Sambuc }
12111be35a1SLionel Sambuc 
ATF_TC_BODY(telldir_leak,tc)12211be35a1SLionel Sambuc ATF_TC_BODY(telldir_leak, tc)
12311be35a1SLionel Sambuc {
12411be35a1SLionel Sambuc 	DIR *dp;
12511be35a1SLionel Sambuc 	char *memused;
12611be35a1SLionel Sambuc 	int i;
12711be35a1SLionel Sambuc 	int oktouse = 4096;
12811be35a1SLionel Sambuc 
12911be35a1SLionel Sambuc 	dp = opendir(".");
13011be35a1SLionel Sambuc 	if (dp == NULL)
13111be35a1SLionel Sambuc 		atf_tc_fail("Could not open current directory");
13211be35a1SLionel Sambuc 
133*84d9c625SLionel Sambuc 	(void)telldir(dp);
13411be35a1SLionel Sambuc 	memused = sbrk(0);
13511be35a1SLionel Sambuc 	closedir(dp);
13611be35a1SLionel Sambuc 
13711be35a1SLionel Sambuc 	for (i = 0; i < 1000; i++) {
13811be35a1SLionel Sambuc 		dp = opendir(".");
13911be35a1SLionel Sambuc 		if (dp == NULL)
14011be35a1SLionel Sambuc 			atf_tc_fail("Could not open current directory");
14111be35a1SLionel Sambuc 
142*84d9c625SLionel Sambuc 		(void)telldir(dp);
14311be35a1SLionel Sambuc 		closedir(dp);
14411be35a1SLionel Sambuc 
14511be35a1SLionel Sambuc 		if ((char *)sbrk(0) - memused > oktouse) {
14611be35a1SLionel Sambuc 			(void)printf("Used %td extra bytes for %d telldir "
14711be35a1SLionel Sambuc 			    "calls", ((char *)sbrk(0) - memused), i);
14811be35a1SLionel Sambuc 			oktouse = (char *)sbrk(0) - memused;
14911be35a1SLionel Sambuc 		}
15011be35a1SLionel Sambuc 	}
15111be35a1SLionel Sambuc 	if (oktouse > 4096) {
15211be35a1SLionel Sambuc 		atf_tc_fail("Failure: leaked %d bytes", oktouse);
15311be35a1SLionel Sambuc 	} else {
15411be35a1SLionel Sambuc 		(void)printf("OK: used %td bytes\n", (char *)(sbrk(0))-memused);
15511be35a1SLionel Sambuc 	}
15611be35a1SLionel Sambuc }
15711be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)15811be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
15911be35a1SLionel Sambuc {
16011be35a1SLionel Sambuc 
16111be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, seekdir_basic);
16211be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, telldir_leak);
16311be35a1SLionel Sambuc 
16411be35a1SLionel Sambuc 	return atf_no_error();
16511be35a1SLionel Sambuc }
166