xref: /freebsd/contrib/netbsd-tests/lib/libc/stdio/t_popen.c (revision 9268022b74279434ed6300244e3f977e56a8ceb5)
1 /*	$NetBSD: t_popen.c,v 1.4 2013/02/15 23:27:19 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Matthias Scheler.
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 <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1999\
35  The NetBSD Foundation, Inc.  All rights reserved.");
36 #endif /* not lint */
37 
38 #ifndef lint
39 __RCSID("$NetBSD: t_popen.c,v 1.4 2013/02/15 23:27:19 christos Exp $");
40 #endif /* not lint */
41 
42 #include <atf-c.h>
43 
44 #include <sys/param.h>
45 
46 #include <errno.h>
47 #include <err.h>
48 #include <paths.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <time.h>
52 #include <unistd.h>
53 
54 #define _PATH_CAT	"/bin/cat"
55 #define BUFSIZE		(640*1024)
56 			/* 640KB ought to be enough for everyone. */
57 #define DATAFILE	"popen.data"
58 
59 #define TEST_ERROR(a)						\
60 	do							\
61 	{							\
62 		warn(a);					\
63 		atf_tc_fail("Check stderr for error details.");	\
64 	} while ( /*CONSTCOND*/ 0 )
65 
66 ATF_TC_WITH_CLEANUP(popen_zeropad);
ATF_TC_HEAD(popen_zeropad,tc)67 ATF_TC_HEAD(popen_zeropad, tc)
68 {
69 
70 	atf_tc_set_md_var(tc, "descr", "output format zero padding");
71 }
72 
ATF_TC_BODY(popen_zeropad,tc)73 ATF_TC_BODY(popen_zeropad, tc)
74 {
75 	char *buffer, command[MAXPATHLEN];
76 	int idx, in;
77 	FILE *my_pipe;
78 
79 	if ((buffer = malloc(BUFSIZE)) == NULL)
80 		atf_tc_skip("Unable to allocate buffer.");
81 
82 	srand ((unsigned int)time(NULL));
83 
84 	for (idx = 0; idx < BUFSIZE; idx++)
85 		buffer[idx]=(char)rand();
86 
87 	(void)snprintf(command, sizeof(command), "%s >%s",
88 	    _PATH_CAT, DATAFILE);
89 
90 	if ((my_pipe = popen(command, "w")) == NULL)
91 		TEST_ERROR("popen write");
92 
93 	if (fwrite(buffer, sizeof(char), BUFSIZE, my_pipe) != BUFSIZE)
94 		TEST_ERROR("fwrite");
95 
96 	if (pclose(my_pipe) == -1)
97 		TEST_ERROR("pclose");
98 
99 	(void)snprintf(command, sizeof(command), "%s %s", _PATH_CAT, DATAFILE);
100 
101 	if ((my_pipe = popen(command, "r")) == NULL)
102 		TEST_ERROR("popen read");
103 
104 	idx = 0;
105 	while ((in = fgetc(my_pipe)) != EOF)
106 		if (idx == BUFSIZE) {
107 			errno = EFBIG;
108 			TEST_ERROR("read");
109 		}
110 		else if ((char)in != buffer[idx++]) {
111 		    	errno = EINVAL;
112 			TEST_ERROR("read");
113 		}
114 
115 	if (idx < BUFSIZE) {
116 		errno = EIO;
117 		TEST_ERROR("read");
118 	}
119 
120 	if (pclose(my_pipe) == -1)
121 		TEST_ERROR("pclose");
122 }
123 
ATF_TC_CLEANUP(popen_zeropad,tc)124 ATF_TC_CLEANUP(popen_zeropad, tc)
125 {
126 	(void)unlink(DATAFILE);
127 }
128 
ATF_TP_ADD_TCS(tp)129 ATF_TP_ADD_TCS(tp)
130 {
131 
132 	ATF_TP_ADD_TC(tp, popen_zeropad);
133 
134 	return atf_no_error();
135 }
136