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 <fcntl.h> 30 #include <paths.h> 31 #include <stdbool.h> 32 #include <stdio.h> 33 #include <string.h> 34 #include <unistd.h> 35 36 #include <atf-c.h> 37 38 static void 39 runtest(const char *fname, int intmode, const char *strmode, bool success) 40 { 41 FILE *fp; 42 int fd; 43 44 fd = open(fname, intmode); 45 ATF_REQUIRE_MSG(fd != -1, 46 "open(\"%s\", %#x) failed; errno=%d", fname, intmode, errno); 47 48 fp = fdopen(fd, strmode); 49 if (fp == NULL) { 50 close(fd); 51 ATF_REQUIRE_MSG(success == false, 52 "fdopen(open(\"%s\", %#x), \"%s\") succeeded unexpectedly", 53 fname, intmode, strmode); 54 return; 55 } 56 ATF_REQUIRE_MSG(success == true, 57 "fdopen(open(\"%s\", %#x), \"%s\") failed; errno=%d", 58 fname, intmode, strmode, errno); 59 fclose(fp); 60 } 61 62 ATF_TC_WITHOUT_HEAD(null__O_RDONLY__r_test); 63 ATF_TC_BODY(null__O_RDONLY__r_test, tc) 64 { 65 66 runtest(_PATH_DEVNULL, O_RDONLY, "r", true); 67 } 68 69 ATF_TC_WITHOUT_HEAD(null__O_WRONLY__r_test); 70 ATF_TC_BODY(null__O_WRONLY__r_test, tc) 71 { 72 73 runtest(_PATH_DEVNULL, O_WRONLY, "r", false); 74 } 75 76 ATF_TC_WITHOUT_HEAD(null__O_RDWR__r_test); 77 ATF_TC_BODY(null__O_RDWR__r_test, tc) 78 { 79 80 runtest(_PATH_DEVNULL, O_RDWR, "r", true); 81 } 82 83 ATF_TC_WITHOUT_HEAD(null__O_RDONLY__w_test); 84 ATF_TC_BODY(null__O_RDONLY__w_test, tc) 85 { 86 87 runtest(_PATH_DEVNULL, O_RDONLY, "w", false); 88 } 89 90 ATF_TC_WITHOUT_HEAD(null__O_WRONLY__w_test); 91 ATF_TC_BODY(null__O_WRONLY__w_test, tc) 92 { 93 94 runtest(_PATH_DEVNULL, O_WRONLY, "w", true); 95 } 96 97 ATF_TC_WITHOUT_HEAD(null__O_RDWR__w_test); 98 ATF_TC_BODY(null__O_RDWR__w_test, tc) 99 { 100 101 runtest(_PATH_DEVNULL, O_RDWR, "w", true); 102 } 103 104 ATF_TC_WITHOUT_HEAD(null__O_RDONLY__a_test); 105 ATF_TC_BODY(null__O_RDONLY__a_test, tc) 106 { 107 108 runtest(_PATH_DEVNULL, O_RDONLY, "a", false); 109 } 110 111 ATF_TC_WITHOUT_HEAD(null__O_WRONLY__a_test); 112 ATF_TC_BODY(null__O_WRONLY__a_test, tc) 113 { 114 115 runtest(_PATH_DEVNULL, O_WRONLY, "a", true); 116 } 117 118 ATF_TC_WITHOUT_HEAD(null__O_RDWR__test); 119 ATF_TC_BODY(null__O_RDWR__test, tc) 120 { 121 122 runtest(_PATH_DEVNULL, O_RDWR, "a", true); 123 } 124 125 ATF_TC_WITHOUT_HEAD(null__O_RDONLY__r_append); 126 ATF_TC_BODY(null__O_RDONLY__r_append, tc) 127 { 128 129 runtest(_PATH_DEVNULL, O_RDONLY, "r+", false); 130 } 131 132 ATF_TC_WITHOUT_HEAD(null__O_WRONLY__r_append); 133 ATF_TC_BODY(null__O_WRONLY__r_append, tc) 134 { 135 136 runtest(_PATH_DEVNULL, O_WRONLY, "r+", false); 137 } 138 139 ATF_TC_WITHOUT_HEAD(null__O_RDWR__r_append); 140 ATF_TC_BODY(null__O_RDWR__r_append, tc) 141 { 142 143 runtest(_PATH_DEVNULL, O_RDWR, "r+", true); 144 } 145 146 ATF_TC_WITHOUT_HEAD(null__O_RDONLY__w_append); 147 ATF_TC_BODY(null__O_RDONLY__w_append, tc) 148 { 149 150 runtest(_PATH_DEVNULL, O_RDONLY, "w+", false); 151 } 152 153 ATF_TC_WITHOUT_HEAD(null__O_WRONLY__w_append); 154 ATF_TC_BODY(null__O_WRONLY__w_append, tc) 155 { 156 157 runtest(_PATH_DEVNULL, O_WRONLY, "w+", false); 158 } 159 160 ATF_TC_WITHOUT_HEAD(null__O_RDWR__w_append); 161 ATF_TC_BODY(null__O_RDWR__w_append, tc) 162 { 163 164 runtest(_PATH_DEVNULL, O_RDWR, "w+", true); 165 } 166 167 ATF_TC_WITHOUT_HEAD(sh__O_EXEC__r); 168 ATF_TC_BODY(sh__O_EXEC__r, tc) 169 { 170 171 runtest("/bin/sh", O_EXEC, "r", false); 172 } 173 174 ATF_TC_WITHOUT_HEAD(sh__O_EXEC__w); 175 ATF_TC_BODY(sh__O_EXEC__w, tc) 176 { 177 178 runtest("/bin/sh", O_EXEC, "w", false); 179 } 180 181 ATF_TC_WITHOUT_HEAD(sh__O_EXEC__r_append); 182 ATF_TC_BODY(sh__O_EXEC__r_append, tc) 183 { 184 185 runtest("/bin/sh", O_EXEC, "r+", false); 186 } 187 188 ATF_TC_WITHOUT_HEAD(sh__O_EXEC__w_append); 189 ATF_TC_BODY(sh__O_EXEC__w_append, tc) 190 { 191 192 runtest("/bin/sh", O_EXEC, "w+", false); 193 } 194 195 ATF_TP_ADD_TCS(tp) 196 { 197 198 ATF_TP_ADD_TC(tp, null__O_RDONLY__r_test); 199 ATF_TP_ADD_TC(tp, null__O_WRONLY__r_test); 200 ATF_TP_ADD_TC(tp, null__O_RDWR__r_test); 201 ATF_TP_ADD_TC(tp, null__O_RDONLY__w_test); 202 ATF_TP_ADD_TC(tp, null__O_WRONLY__w_test); 203 ATF_TP_ADD_TC(tp, null__O_RDWR__w_test); 204 ATF_TP_ADD_TC(tp, null__O_RDONLY__a_test); 205 ATF_TP_ADD_TC(tp, null__O_WRONLY__a_test); 206 ATF_TP_ADD_TC(tp, null__O_RDWR__test); 207 ATF_TP_ADD_TC(tp, null__O_RDONLY__r_append); 208 ATF_TP_ADD_TC(tp, null__O_WRONLY__r_append); 209 ATF_TP_ADD_TC(tp, null__O_RDWR__r_append); 210 ATF_TP_ADD_TC(tp, null__O_RDONLY__w_append); 211 ATF_TP_ADD_TC(tp, null__O_WRONLY__w_append); 212 ATF_TP_ADD_TC(tp, null__O_RDWR__w_append); 213 ATF_TP_ADD_TC(tp, sh__O_EXEC__r); 214 ATF_TP_ADD_TC(tp, sh__O_EXEC__w); 215 ATF_TP_ADD_TC(tp, sh__O_EXEC__r_append); 216 ATF_TP_ADD_TC(tp, sh__O_EXEC__w_append); 217 218 return (atf_no_error()); 219 } 220 221 /* 222 vim:ts=8:cin:sw=8 223 */ 224