1 /* $NetBSD: t_popen.c,v 1.1 2010/12/23 15:27:44 pgoyette 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.1 2010/12/23 15:27:44 pgoyette Exp $"); 40 #endif /* not lint */ 41 42 #include <atf-c.h> 43 44 #include <sys/param.h> 45 46 #include <errno.h> 47 #include <paths.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <time.h> 51 #include <unistd.h> 52 53 #define _PATH_CAT "/bin/cat" 54 #define BUFSIZE (640*1024) 55 /* 640KB ought to be enough for everyone. */ 56 #define DATAFILE "popen.data" 57 58 #define TEST_ERROR(a) \ 59 do \ 60 { \ 61 perror(a); \ 62 atf_tc_fail("Check stderr for error details."); \ 63 } while ( /*CONSTCOND*/ 0 ) 64 65 ATF_TC_WITH_CLEANUP(popen); 66 67 ATF_TC_HEAD(popen, tc) 68 { 69 70 atf_tc_set_md_var(tc, "descr", "output format zero padding"); 71 } 72 73 ATF_TC_BODY(popen, tc) 74 { 75 char *buffer, command[MAXPATHLEN]; 76 int index, in; 77 FILE *my_pipe; 78 79 if ((buffer = malloc(BUFSIZE*sizeof(char))) == NULL) 80 atf_tc_skip("Unable to allocate buffer."); 81 82 srand ((unsigned int)time(NULL)); 83 for (index=0; index<BUFSIZE; index++) 84 buffer[index]=(char)rand(); 85 86 (void)snprintf(command, sizeof(command), "%s >%s", _PATH_CAT, DATAFILE); 87 if ((my_pipe = popen(command, "w")) == NULL) 88 TEST_ERROR("popen write"); 89 90 if (fwrite(buffer, sizeof(char), BUFSIZE, my_pipe) != BUFSIZE) 91 TEST_ERROR("fwrite"); 92 93 if (pclose(my_pipe) == -1) 94 TEST_ERROR("pclose"); 95 96 (void)snprintf(command, sizeof(command), "%s %s", _PATH_CAT, DATAFILE); 97 if ((my_pipe = popen(command, "r")) == NULL) 98 TEST_ERROR("popen read"); 99 100 index = 0; 101 while ((in = fgetc(my_pipe)) != EOF) 102 if (index == BUFSIZE) { 103 errno = EFBIG; 104 TEST_ERROR("read"); 105 } 106 else if ((char)in != buffer[index++]) { 107 errno = EINVAL; 108 TEST_ERROR("read"); 109 } 110 111 if (index < BUFSIZE) { 112 errno = EIO; 113 TEST_ERROR("read"); 114 } 115 116 if (pclose(my_pipe) == -1) 117 TEST_ERROR("pclose"); 118 } 119 120 ATF_TC_CLEANUP(popen, tc) 121 { 122 (void)unlink(DATAFILE); 123 } 124 125 ATF_TP_ADD_TCS(tp) 126 { 127 ATF_TP_ADD_TC(tp, popen); 128 129 return atf_no_error(); 130 } 131