1 /* $NetBSD: t_popen.c,v 1.2 2011/06/11 18:03:18 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.2 2011/06/11 18:03:18 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); 67 68 ATF_TC_HEAD(popen, tc) 69 { 70 71 atf_tc_set_md_var(tc, "descr", "output format zero padding"); 72 } 73 74 ATF_TC_BODY(popen, tc) 75 { 76 char *buffer, command[MAXPATHLEN]; 77 int index, in; 78 FILE *my_pipe; 79 80 if ((buffer = malloc(BUFSIZE*sizeof(char))) == NULL) 81 atf_tc_skip("Unable to allocate buffer."); 82 83 srand ((unsigned int)time(NULL)); 84 for (index=0; index<BUFSIZE; index++) 85 buffer[index]=(char)rand(); 86 87 (void)snprintf(command, sizeof(command), "%s >%s", _PATH_CAT, DATAFILE); 88 if ((my_pipe = popen(command, "w")) == NULL) 89 TEST_ERROR("popen write"); 90 91 if (fwrite(buffer, sizeof(char), BUFSIZE, my_pipe) != BUFSIZE) 92 TEST_ERROR("fwrite"); 93 94 if (pclose(my_pipe) == -1) 95 TEST_ERROR("pclose"); 96 97 (void)snprintf(command, sizeof(command), "%s %s", _PATH_CAT, DATAFILE); 98 if ((my_pipe = popen(command, "r")) == NULL) 99 TEST_ERROR("popen read"); 100 101 index = 0; 102 while ((in = fgetc(my_pipe)) != EOF) 103 if (index == BUFSIZE) { 104 errno = EFBIG; 105 TEST_ERROR("read"); 106 } 107 else if ((char)in != buffer[index++]) { 108 errno = EINVAL; 109 TEST_ERROR("read"); 110 } 111 112 if (index < BUFSIZE) { 113 errno = EIO; 114 TEST_ERROR("read"); 115 } 116 117 if (pclose(my_pipe) == -1) 118 TEST_ERROR("pclose"); 119 } 120 121 ATF_TC_CLEANUP(popen, tc) 122 { 123 (void)unlink(DATAFILE); 124 } 125 126 ATF_TP_ADD_TCS(tp) 127 { 128 ATF_TP_ADD_TC(tp, popen); 129 130 return atf_no_error(); 131 } 132