freopen_test.c revision 290537
1264737Sjilles/*- 2264737Sjilles * Copyright (c) 2014 Jilles Tjoelker 3264737Sjilles * All rights reserved. 4264737Sjilles * 5264737Sjilles * Redistribution and use in source and binary forms, with or without 6264737Sjilles * modification, are permitted provided that the following conditions 7264737Sjilles * are met: 8264737Sjilles * 1. Redistributions of source code must retain the above copyright 9264737Sjilles * notice, this list of conditions and the following disclaimer. 10264737Sjilles * 2. Redistributions in binary form must reproduce the above copyright 11264737Sjilles * notice, this list of conditions and the following disclaimer in the 12264737Sjilles * documentation and/or other materials provided with the distribution. 13264737Sjilles * 14264737Sjilles * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15264737Sjilles * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16264737Sjilles * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17264737Sjilles * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18264737Sjilles * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19264737Sjilles * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20264737Sjilles * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21264737Sjilles * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22264737Sjilles * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23264737Sjilles * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24264737Sjilles * SUCH DAMAGE. 25264737Sjilles */ 26264737Sjilles 27264737Sjilles#include <sys/cdefs.h> 28264737Sjilles__FBSDID("$FreeBSD: head/lib/libc/tests/stdio/freopen_test.c 290537 2015-11-08 06:37:50Z ngie $"); 29264737Sjilles 30290537Sngie#include <errno.h> 31290537Sngie#include <paths.h> 32290537Sngie#include <stdbool.h> 33290537Sngie#include <stdio.h> 34290537Sngie#include <string.h> 35264737Sjilles 36290537Sngie#include <atf-c.h> 37264737Sjilles 38264737Sjillesstatic void 39264737Sjillesruntest(const char *fname1, const char *mode1, const char *fname2, 40264737Sjilles const char *mode2, bool success) 41264737Sjilles{ 42264737Sjilles FILE *fp1, *fp2; 43264737Sjilles const char *fname2_print; 44264737Sjilles 45264737Sjilles fname2_print = fname2 != NULL ? fname2 : "<NULL>"; 46264737Sjilles fp1 = fopen(fname1, mode1); 47290537Sngie ATF_REQUIRE_MSG(fp1 != NULL, 48290537Sngie "fopen(\"%s\", \"%s\") failed; errno=%d", fname1, mode1, errno); 49264737Sjilles fp2 = freopen(fname2, mode2, fp1); 50264737Sjilles if (fp2 == NULL) { 51264737Sjilles fclose(fp1); 52290537Sngie ATF_REQUIRE_MSG(success == false, 53290537Sngie "freopen(\"%s\", \"%s\", fopen(\"%s\", \"%s\")) succeeded " 54290537Sngie "unexpectedly", fname2_print, mode2, fname1, mode1); 55264737Sjilles return; 56264737Sjilles } 57290537Sngie ATF_REQUIRE_MSG(success == true, 58290537Sngie "freopen(\"%s\", \"%s\", fopen(\"%s\", \"%s\")) failed: %d", 59290537Sngie fname2_print, mode2, fname1, mode1, errno); 60264737Sjilles fclose(fp2); 61264737Sjilles} 62264737Sjilles 63290537SngieATF_TC_WITHOUT_HEAD(null__r__r__test); 64290537SngieATF_TC_BODY(null__r__r__test, tc) 65264737Sjilles{ 66290537Sngie 67290537Sngie runtest(_PATH_DEVNULL, "r", NULL, "r", true); 68290537Sngie} 69290537Sngie 70290537SngieATF_TC_WITHOUT_HEAD(null__w__r__test); 71290537SngieATF_TC_BODY(null__w__r__test, tc) 72290537Sngie{ 73290537Sngie 74290537Sngie runtest(_PATH_DEVNULL, "w", NULL, "r", false); 75290537Sngie} 76290537Sngie 77290537SngieATF_TC_WITHOUT_HEAD(null__r_append__r__test); 78290537SngieATF_TC_BODY(null__r_append__r__test, tc) 79290537Sngie{ 80290537Sngie 81290537Sngie runtest(_PATH_DEVNULL, "r+", NULL, "r", true); 82290537Sngie} 83290537Sngie 84290537SngieATF_TC_WITHOUT_HEAD(null__r__w__test); 85290537SngieATF_TC_BODY(null__r__w__test, tc) 86290537Sngie{ 87290537Sngie 88290537Sngie runtest(_PATH_DEVNULL, "r", NULL, "w", false); 89290537Sngie} 90290537Sngie 91290537SngieATF_TC_WITHOUT_HEAD(null__w__w__test); 92290537SngieATF_TC_BODY(null__w__w__test, tc) 93290537Sngie{ 94290537Sngie 95290537Sngie runtest(_PATH_DEVNULL, "w", NULL, "w", true); 96290537Sngie} 97290537Sngie 98290537SngieATF_TC_WITHOUT_HEAD(null__r_append__w__test); 99290537SngieATF_TC_BODY(null__r_append__w__test, tc) 100290537Sngie{ 101290537Sngie 102290537Sngie runtest(_PATH_DEVNULL, "r+", NULL, "w", true); 103290537Sngie} 104290537Sngie 105290537SngieATF_TC_WITHOUT_HEAD(null__r__a__test); 106290537SngieATF_TC_BODY(null__r__a__test, tc) 107290537Sngie{ 108290537Sngie 109290537Sngie runtest(_PATH_DEVNULL, "r", NULL, "a", false); 110290537Sngie} 111290537Sngie 112290537SngieATF_TC_WITHOUT_HEAD(null__w__a__test); 113290537SngieATF_TC_BODY(null__w__a__test, tc) 114290537Sngie{ 115290537Sngie 116290537Sngie runtest(_PATH_DEVNULL, "w", NULL, "a", true); 117290537Sngie} 118290537Sngie 119290537SngieATF_TC_WITHOUT_HEAD(null__r_append__a__test); 120290537SngieATF_TC_BODY(null__r_append__a__test, tc) 121290537Sngie{ 122290537Sngie 123290537Sngie runtest(_PATH_DEVNULL, "r+", NULL, "a", true); 124290537Sngie} 125290537Sngie 126290537SngieATF_TC_WITHOUT_HEAD(null__r__r_append__test); 127290537SngieATF_TC_BODY(null__r__r_append__test, tc) 128290537Sngie{ 129290537Sngie 130290537Sngie runtest(_PATH_DEVNULL, "r", NULL, "r+", false); 131290537Sngie} 132290537Sngie 133290537SngieATF_TC_WITHOUT_HEAD(null__w__r_append__test); 134290537SngieATF_TC_BODY(null__w__r_append__test, tc) 135290537Sngie{ 136290537Sngie 137290537Sngie runtest(_PATH_DEVNULL, "w", NULL, "r+", false); 138290537Sngie} 139290537Sngie 140290537SngieATF_TC_WITHOUT_HEAD(null__r_append__r_append__test); 141290537SngieATF_TC_BODY(null__r_append__r_append__test, tc) 142290537Sngie{ 143290537Sngie 144290537Sngie runtest(_PATH_DEVNULL, "r+", NULL, "r+", true); 145290537Sngie} 146290537Sngie 147290537SngieATF_TC_WITHOUT_HEAD(null__r__w_append__test); 148290537SngieATF_TC_BODY(null__r__w_append__test, tc) 149290537Sngie{ 150290537Sngie 151290537Sngie runtest(_PATH_DEVNULL, "r", NULL, "w+", false); 152290537Sngie} 153290537Sngie 154290537SngieATF_TC_WITHOUT_HEAD(null__w__w_append__test); 155290537SngieATF_TC_BODY(null__w__w_append__test, tc) 156290537Sngie{ 157290537Sngie 158290537Sngie runtest(_PATH_DEVNULL, "w", NULL, "w+", false); 159290537Sngie} 160290537Sngie 161290537SngieATF_TC_WITHOUT_HEAD(null__r_append__w_append__test); 162290537SngieATF_TC_BODY(null__r_append__w_append__test, tc) 163290537Sngie{ 164290537Sngie 165290537Sngie runtest(_PATH_DEVNULL, "r+", NULL, "w+", true); 166290537Sngie} 167290537Sngie 168290537SngieATF_TC_WITHOUT_HEAD(sh__r__r__test); 169290537SngieATF_TC_BODY(sh__r__r__test, tc) 170290537Sngie{ 171290537Sngie 172264737Sjilles runtest("/bin/sh", "r", NULL, "r", true); 173290537Sngie} 174290537Sngie 175290537SngieATF_TC_WITHOUT_HEAD(sh__sh__r__r__test); 176290537SngieATF_TC_BODY(sh__sh__r__r__test, tc) 177290537Sngie{ 178290537Sngie 179264737Sjilles runtest("/bin/sh", "r", "/bin/sh", "r", true); 180290537Sngie} 181264737Sjilles 182290537SngieATF_TC_WITHOUT_HEAD(sh__null__r__r__test); 183290537SngieATF_TC_BODY(sh__null__r__r__test, tc) 184290537Sngie{ 185290537Sngie 186290537Sngie runtest("/bin/sh", "r", _PATH_DEVNULL, "r", true); 187264737Sjilles} 188264737Sjilles 189290537SngieATF_TC_WITHOUT_HEAD(sh__null__r__w__test); 190290537SngieATF_TC_BODY(sh__null__r__w__test, tc) 191290537Sngie{ 192290537Sngie 193290537Sngie runtest("/bin/sh", "r", _PATH_DEVNULL, "w", true); 194290537Sngie} 195290537Sngie 196290537SngieATF_TP_ADD_TCS(tp) 197290537Sngie{ 198290537Sngie 199290537Sngie ATF_TP_ADD_TC(tp, null__r__r__test); 200290537Sngie ATF_TP_ADD_TC(tp, null__w__r__test); 201290537Sngie ATF_TP_ADD_TC(tp, null__r_append__r__test); 202290537Sngie ATF_TP_ADD_TC(tp, null__r__w__test); 203290537Sngie ATF_TP_ADD_TC(tp, null__w__w__test); 204290537Sngie ATF_TP_ADD_TC(tp, null__r_append__w__test); 205290537Sngie ATF_TP_ADD_TC(tp, null__r__a__test); 206290537Sngie ATF_TP_ADD_TC(tp, null__w__a__test); 207290537Sngie ATF_TP_ADD_TC(tp, null__r_append__a__test); 208290537Sngie ATF_TP_ADD_TC(tp, null__r__r_append__test); 209290537Sngie ATF_TP_ADD_TC(tp, null__w__r_append__test); 210290537Sngie ATF_TP_ADD_TC(tp, null__r_append__r_append__test); 211290537Sngie ATF_TP_ADD_TC(tp, null__r__w_append__test); 212290537Sngie ATF_TP_ADD_TC(tp, null__w__w_append__test); 213290537Sngie ATF_TP_ADD_TC(tp, null__r_append__w_append__test); 214290537Sngie ATF_TP_ADD_TC(tp, sh__r__r__test); 215290537Sngie ATF_TP_ADD_TC(tp, sh__sh__r__r__test); 216290537Sngie ATF_TP_ADD_TC(tp, sh__null__r__r__test); 217290537Sngie ATF_TP_ADD_TC(tp, sh__null__r__w__test); 218290537Sngie 219290537Sngie return (atf_no_error()); 220290537Sngie} 221290537Sngie 222290537Sngie/* 223290537Sngie vim:ts=8:cin:sw=8 224290537Sngie */ 225