1272343Sngie/* $NetBSD: t_popen.c,v 1.4 2013/02/15 23:27:19 christos Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 1999 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * This code is derived from software contributed to The NetBSD Foundation 8272343Sngie * by Matthias Scheler. 9272343Sngie * 10272343Sngie * Redistribution and use in source and binary forms, with or without 11272343Sngie * modification, are permitted provided that the following conditions 12272343Sngie * are met: 13272343Sngie * 1. Redistributions of source code must retain the above copyright 14272343Sngie * notice, this list of conditions and the following disclaimer. 15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 16272343Sngie * notice, this list of conditions and the following disclaimer in the 17272343Sngie * documentation and/or other materials provided with the distribution. 18272343Sngie * 19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29272343Sngie * POSSIBILITY OF SUCH DAMAGE. 30272343Sngie */ 31272343Sngie 32272343Sngie#include <sys/cdefs.h> 33272343Sngie#ifndef lint 34272343Sngie__COPYRIGHT("@(#) Copyright (c) 1999\ 35272343Sngie The NetBSD Foundation, Inc. All rights reserved."); 36272343Sngie#endif /* not lint */ 37272343Sngie 38272343Sngie#ifndef lint 39272343Sngie__RCSID("$NetBSD: t_popen.c,v 1.4 2013/02/15 23:27:19 christos Exp $"); 40272343Sngie#endif /* not lint */ 41272343Sngie 42272343Sngie#include <atf-c.h> 43272343Sngie 44272343Sngie#include <sys/param.h> 45272343Sngie 46272343Sngie#include <errno.h> 47272343Sngie#include <err.h> 48272343Sngie#include <paths.h> 49272343Sngie#include <stdio.h> 50272343Sngie#include <stdlib.h> 51272343Sngie#include <time.h> 52272343Sngie#include <unistd.h> 53272343Sngie 54272343Sngie#define _PATH_CAT "/bin/cat" 55272343Sngie#define BUFSIZE (640*1024) 56272343Sngie /* 640KB ought to be enough for everyone. */ 57272343Sngie#define DATAFILE "popen.data" 58272343Sngie 59272343Sngie#define TEST_ERROR(a) \ 60272343Sngie do \ 61272343Sngie { \ 62272343Sngie warn(a); \ 63272343Sngie atf_tc_fail("Check stderr for error details."); \ 64272343Sngie } while ( /*CONSTCOND*/ 0 ) 65272343Sngie 66272343SngieATF_TC_WITH_CLEANUP(popen_zeropad); 67272343SngieATF_TC_HEAD(popen_zeropad, tc) 68272343Sngie{ 69272343Sngie 70272343Sngie atf_tc_set_md_var(tc, "descr", "output format zero padding"); 71272343Sngie} 72272343Sngie 73272343SngieATF_TC_BODY(popen_zeropad, tc) 74272343Sngie{ 75272343Sngie char *buffer, command[MAXPATHLEN]; 76272343Sngie int idx, in; 77272343Sngie FILE *my_pipe; 78272343Sngie 79272343Sngie if ((buffer = malloc(BUFSIZE)) == NULL) 80272343Sngie atf_tc_skip("Unable to allocate buffer."); 81272343Sngie 82272343Sngie srand ((unsigned int)time(NULL)); 83272343Sngie 84272343Sngie for (idx = 0; idx < BUFSIZE; idx++) 85272343Sngie buffer[idx]=(char)rand(); 86272343Sngie 87272343Sngie (void)snprintf(command, sizeof(command), "%s >%s", 88272343Sngie _PATH_CAT, DATAFILE); 89272343Sngie 90272343Sngie if ((my_pipe = popen(command, "w")) == NULL) 91272343Sngie TEST_ERROR("popen write"); 92272343Sngie 93272343Sngie if (fwrite(buffer, sizeof(char), BUFSIZE, my_pipe) != BUFSIZE) 94272343Sngie TEST_ERROR("fwrite"); 95272343Sngie 96272343Sngie if (pclose(my_pipe) == -1) 97272343Sngie TEST_ERROR("pclose"); 98272343Sngie 99272343Sngie (void)snprintf(command, sizeof(command), "%s %s", _PATH_CAT, DATAFILE); 100272343Sngie 101272343Sngie if ((my_pipe = popen(command, "r")) == NULL) 102272343Sngie TEST_ERROR("popen read"); 103272343Sngie 104272343Sngie idx = 0; 105272343Sngie while ((in = fgetc(my_pipe)) != EOF) 106272343Sngie if (idx == BUFSIZE) { 107272343Sngie errno = EFBIG; 108272343Sngie TEST_ERROR("read"); 109272343Sngie } 110272343Sngie else if ((char)in != buffer[idx++]) { 111272343Sngie errno = EINVAL; 112272343Sngie TEST_ERROR("read"); 113272343Sngie } 114272343Sngie 115272343Sngie if (idx < BUFSIZE) { 116272343Sngie errno = EIO; 117272343Sngie TEST_ERROR("read"); 118272343Sngie } 119272343Sngie 120272343Sngie if (pclose(my_pipe) == -1) 121272343Sngie TEST_ERROR("pclose"); 122272343Sngie} 123272343Sngie 124272343SngieATF_TC_CLEANUP(popen_zeropad, tc) 125272343Sngie{ 126272343Sngie (void)unlink(DATAFILE); 127272343Sngie} 128272343Sngie 129272343SngieATF_TP_ADD_TCS(tp) 130272343Sngie{ 131272343Sngie 132272343Sngie ATF_TP_ADD_TC(tp, popen_zeropad); 133272343Sngie 134272343Sngie return atf_no_error(); 135272343Sngie} 136