1 /*- 2 * Copyright (c) 2014 Jilles Tjoelker 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 #include <errno.h> 29 #include <paths.h> 30 #include <stdbool.h> 31 #include <stdio.h> 32 #include <string.h> 33 34 #include <atf-c.h> 35 36 static void 37 runtest(const char *fname1, const char *mode1, const char *fname2, 38 const char *mode2, bool success) 39 { 40 FILE *fp1, *fp2; 41 const char *fname2_print; 42 43 fname2_print = fname2 != NULL ? fname2 : "<NULL>"; 44 fp1 = fopen(fname1, mode1); 45 ATF_REQUIRE_MSG(fp1 != NULL, 46 "fopen(\"%s\", \"%s\") failed; errno=%d", fname1, mode1, errno); 47 fp2 = freopen(fname2, mode2, fp1); 48 if (fp2 == NULL) { 49 ATF_REQUIRE_MSG(success == false, 50 "freopen(\"%s\", \"%s\", fopen(\"%s\", \"%s\")) succeeded " 51 "unexpectedly", fname2_print, mode2, fname1, mode1); 52 return; 53 } 54 ATF_REQUIRE_MSG(success == true, 55 "freopen(\"%s\", \"%s\", fopen(\"%s\", \"%s\")) failed: %d", 56 fname2_print, mode2, fname1, mode1, errno); 57 fclose(fp2); 58 } 59 60 ATF_TC_WITHOUT_HEAD(null__r__r__test); 61 ATF_TC_BODY(null__r__r__test, tc) 62 { 63 64 runtest(_PATH_DEVNULL, "r", NULL, "r", true); 65 } 66 67 ATF_TC_WITHOUT_HEAD(null__w__r__test); 68 ATF_TC_BODY(null__w__r__test, tc) 69 { 70 71 runtest(_PATH_DEVNULL, "w", NULL, "r", false); 72 } 73 74 ATF_TC_WITHOUT_HEAD(null__r_append__r__test); 75 ATF_TC_BODY(null__r_append__r__test, tc) 76 { 77 78 runtest(_PATH_DEVNULL, "r+", NULL, "r", true); 79 } 80 81 ATF_TC_WITHOUT_HEAD(null__r__w__test); 82 ATF_TC_BODY(null__r__w__test, tc) 83 { 84 85 runtest(_PATH_DEVNULL, "r", NULL, "w", false); 86 } 87 88 ATF_TC_WITHOUT_HEAD(null__w__w__test); 89 ATF_TC_BODY(null__w__w__test, tc) 90 { 91 92 runtest(_PATH_DEVNULL, "w", NULL, "w", true); 93 } 94 95 ATF_TC_WITHOUT_HEAD(null__r_append__w__test); 96 ATF_TC_BODY(null__r_append__w__test, tc) 97 { 98 99 runtest(_PATH_DEVNULL, "r+", NULL, "w", true); 100 } 101 102 ATF_TC_WITHOUT_HEAD(null__r__a__test); 103 ATF_TC_BODY(null__r__a__test, tc) 104 { 105 106 runtest(_PATH_DEVNULL, "r", NULL, "a", false); 107 } 108 109 ATF_TC_WITHOUT_HEAD(null__w__a__test); 110 ATF_TC_BODY(null__w__a__test, tc) 111 { 112 113 runtest(_PATH_DEVNULL, "w", NULL, "a", true); 114 } 115 116 ATF_TC_WITHOUT_HEAD(null__r_append__a__test); 117 ATF_TC_BODY(null__r_append__a__test, tc) 118 { 119 120 runtest(_PATH_DEVNULL, "r+", NULL, "a", true); 121 } 122 123 ATF_TC_WITHOUT_HEAD(null__r__r_append__test); 124 ATF_TC_BODY(null__r__r_append__test, tc) 125 { 126 127 runtest(_PATH_DEVNULL, "r", NULL, "r+", false); 128 } 129 130 ATF_TC_WITHOUT_HEAD(null__w__r_append__test); 131 ATF_TC_BODY(null__w__r_append__test, tc) 132 { 133 134 runtest(_PATH_DEVNULL, "w", NULL, "r+", false); 135 } 136 137 ATF_TC_WITHOUT_HEAD(null__r_append__r_append__test); 138 ATF_TC_BODY(null__r_append__r_append__test, tc) 139 { 140 141 runtest(_PATH_DEVNULL, "r+", NULL, "r+", true); 142 } 143 144 ATF_TC_WITHOUT_HEAD(null__r__w_append__test); 145 ATF_TC_BODY(null__r__w_append__test, tc) 146 { 147 148 runtest(_PATH_DEVNULL, "r", NULL, "w+", false); 149 } 150 151 ATF_TC_WITHOUT_HEAD(null__w__w_append__test); 152 ATF_TC_BODY(null__w__w_append__test, tc) 153 { 154 155 runtest(_PATH_DEVNULL, "w", NULL, "w+", false); 156 } 157 158 ATF_TC_WITHOUT_HEAD(null__r_append__w_append__test); 159 ATF_TC_BODY(null__r_append__w_append__test, tc) 160 { 161 162 runtest(_PATH_DEVNULL, "r+", NULL, "w+", true); 163 } 164 165 ATF_TC_WITHOUT_HEAD(sh__r__r__test); 166 ATF_TC_BODY(sh__r__r__test, tc) 167 { 168 169 runtest("/bin/sh", "r", NULL, "r", true); 170 } 171 172 ATF_TC_WITHOUT_HEAD(sh__sh__r__r__test); 173 ATF_TC_BODY(sh__sh__r__r__test, tc) 174 { 175 176 runtest("/bin/sh", "r", "/bin/sh", "r", true); 177 } 178 179 ATF_TC_WITHOUT_HEAD(sh__null__r__r__test); 180 ATF_TC_BODY(sh__null__r__r__test, tc) 181 { 182 183 runtest("/bin/sh", "r", _PATH_DEVNULL, "r", true); 184 } 185 186 ATF_TC_WITHOUT_HEAD(sh__null__r__w__test); 187 ATF_TC_BODY(sh__null__r__w__test, tc) 188 { 189 190 runtest("/bin/sh", "r", _PATH_DEVNULL, "w", true); 191 } 192 193 ATF_TP_ADD_TCS(tp) 194 { 195 196 ATF_TP_ADD_TC(tp, null__r__r__test); 197 ATF_TP_ADD_TC(tp, null__w__r__test); 198 ATF_TP_ADD_TC(tp, null__r_append__r__test); 199 ATF_TP_ADD_TC(tp, null__r__w__test); 200 ATF_TP_ADD_TC(tp, null__w__w__test); 201 ATF_TP_ADD_TC(tp, null__r_append__w__test); 202 ATF_TP_ADD_TC(tp, null__r__a__test); 203 ATF_TP_ADD_TC(tp, null__w__a__test); 204 ATF_TP_ADD_TC(tp, null__r_append__a__test); 205 ATF_TP_ADD_TC(tp, null__r__r_append__test); 206 ATF_TP_ADD_TC(tp, null__w__r_append__test); 207 ATF_TP_ADD_TC(tp, null__r_append__r_append__test); 208 ATF_TP_ADD_TC(tp, null__r__w_append__test); 209 ATF_TP_ADD_TC(tp, null__w__w_append__test); 210 ATF_TP_ADD_TC(tp, null__r_append__w_append__test); 211 ATF_TP_ADD_TC(tp, sh__r__r__test); 212 ATF_TP_ADD_TC(tp, sh__sh__r__r__test); 213 ATF_TP_ADD_TC(tp, sh__null__r__r__test); 214 ATF_TP_ADD_TC(tp, sh__null__r__w__test); 215 216 return (atf_no_error()); 217 } 218 219 /* 220 vim:ts=8:cin:sw=8 221 */ 222