1 /*- 2 * Copyright (c) 2013 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 <fcntl.h> 29 #include <paths.h> 30 #include <stdio.h> 31 #include <string.h> 32 33 #include <atf-c.h> 34 35 /* 36 * O_ACCMODE is currently defined incorrectly. This is what it should be. 37 * Various code depends on the incorrect value. 38 */ 39 #define CORRECT_O_ACCMODE (O_ACCMODE | O_EXEC) 40 41 static void 42 runtest(const char *fname, const char *mode) 43 { 44 FILE *fp; 45 int exp_fget_ret, fget_ret, fd, flags, wantedflags; 46 47 fp = fopen(fname, mode); 48 ATF_REQUIRE_MSG(fp != NULL, 49 "fopen(\"%s\", \"%s\") failed", fname, mode); 50 fd = fileno(fp); 51 ATF_REQUIRE_MSG(fd >= 0, "fileno() failed for fopen"); 52 exp_fget_ret = strchr(mode, 'e') != NULL ? FD_CLOEXEC : 0; 53 ATF_REQUIRE_MSG((fget_ret = fcntl(fd, F_GETFD)) == exp_fget_ret, 54 "fcntl(.., F_GETFD) didn't FD_CLOEXEC as expected %d != %d", 55 exp_fget_ret, fget_ret); 56 flags = fcntl(fd, F_GETFL); 57 if (strchr(mode, '+')) 58 wantedflags = O_RDWR | (*mode == 'a' ? O_APPEND : 0); 59 else if (*mode == 'r') 60 wantedflags = O_RDONLY; 61 else if (*mode == 'w') 62 wantedflags = O_WRONLY; 63 else if (*mode == 'a') 64 wantedflags = O_WRONLY | O_APPEND; 65 else 66 wantedflags = -1; 67 fclose(fp); 68 if (wantedflags == -1) 69 atf_tc_fail("unrecognized mode: %s", mode); 70 else if ((flags & (CORRECT_O_ACCMODE | O_APPEND)) != wantedflags) 71 atf_tc_fail("incorrect access mode: %s", mode); 72 } 73 74 ATF_TC_WITHOUT_HEAD(fopen_r_test); 75 ATF_TC_BODY(fopen_r_test, tc) 76 { 77 78 runtest(_PATH_DEVNULL, "r"); 79 } 80 81 ATF_TC_WITHOUT_HEAD(fopen_r_append_test); 82 ATF_TC_BODY(fopen_r_append_test, tc) 83 { 84 85 runtest(_PATH_DEVNULL, "r+"); 86 } 87 88 ATF_TC_WITHOUT_HEAD(fopen_w_test); 89 ATF_TC_BODY(fopen_w_test, tc) 90 { 91 92 runtest(_PATH_DEVNULL, "w"); 93 } 94 95 ATF_TC_WITHOUT_HEAD(fopen_w_append_test); 96 ATF_TC_BODY(fopen_w_append_test, tc) 97 { 98 99 runtest(_PATH_DEVNULL, "w+"); 100 } 101 102 ATF_TC_WITHOUT_HEAD(fopen_a_test); 103 ATF_TC_BODY(fopen_a_test, tc) 104 { 105 106 runtest(_PATH_DEVNULL, "a"); 107 } 108 109 ATF_TC_WITHOUT_HEAD(fopen_a_append_test); 110 ATF_TC_BODY(fopen_a_append_test, tc) 111 { 112 113 runtest(_PATH_DEVNULL, "a+"); 114 } 115 116 ATF_TC_WITHOUT_HEAD(fopen_re_test); 117 ATF_TC_BODY(fopen_re_test, tc) 118 { 119 120 runtest(_PATH_DEVNULL, "re"); 121 } 122 123 ATF_TC_WITHOUT_HEAD(fopen_r_append_e_test); 124 ATF_TC_BODY(fopen_r_append_e_test, tc) 125 { 126 127 runtest(_PATH_DEVNULL, "r+e"); 128 } 129 130 ATF_TC_WITHOUT_HEAD(fopen_we_test); 131 ATF_TC_BODY(fopen_we_test, tc) 132 { 133 134 runtest(_PATH_DEVNULL, "we"); 135 } 136 137 ATF_TC_WITHOUT_HEAD(fopen_w_append_e_test); 138 ATF_TC_BODY(fopen_w_append_e_test, tc) 139 { 140 141 runtest(_PATH_DEVNULL, "w+e"); 142 } 143 144 ATF_TC_WITHOUT_HEAD(fopen_ae_test); 145 ATF_TC_BODY(fopen_ae_test, tc) 146 { 147 148 runtest(_PATH_DEVNULL, "ae"); 149 } 150 151 ATF_TC_WITHOUT_HEAD(fopen_a_append_e_test); 152 ATF_TC_BODY(fopen_a_append_e_test, tc) 153 { 154 155 runtest(_PATH_DEVNULL, "a+e"); 156 } 157 158 ATF_TC_WITHOUT_HEAD(fopen_re_append_test); 159 ATF_TC_BODY(fopen_re_append_test, tc) 160 { 161 162 runtest(_PATH_DEVNULL, "re+"); 163 } 164 165 ATF_TC_WITHOUT_HEAD(fopen_we_append_test); 166 ATF_TC_BODY(fopen_we_append_test, tc) 167 { 168 169 runtest(_PATH_DEVNULL, "we+"); 170 } 171 172 ATF_TC_WITHOUT_HEAD(fopen_ae_append_test); 173 ATF_TC_BODY(fopen_ae_append_test, tc) 174 { 175 176 runtest(_PATH_DEVNULL, "ae+"); 177 } 178 179 ATF_TP_ADD_TCS(tp) 180 { 181 182 ATF_TP_ADD_TC(tp, fopen_r_test); 183 ATF_TP_ADD_TC(tp, fopen_r_append_test); 184 ATF_TP_ADD_TC(tp, fopen_w_test); 185 ATF_TP_ADD_TC(tp, fopen_w_append_test); 186 ATF_TP_ADD_TC(tp, fopen_a_test); 187 ATF_TP_ADD_TC(tp, fopen_a_append_test); 188 ATF_TP_ADD_TC(tp, fopen_re_test); 189 ATF_TP_ADD_TC(tp, fopen_r_append_e_test); 190 ATF_TP_ADD_TC(tp, fopen_we_test); 191 ATF_TP_ADD_TC(tp, fopen_w_append_e_test); 192 ATF_TP_ADD_TC(tp, fopen_ae_test); 193 ATF_TP_ADD_TC(tp, fopen_a_append_e_test); 194 ATF_TP_ADD_TC(tp, fopen_re_append_test); 195 ATF_TP_ADD_TC(tp, fopen_we_append_test); 196 ATF_TP_ADD_TC(tp, fopen_ae_append_test); 197 198 return (atf_no_error()); 199 } 200 201 /* 202 vim:ts=8:cin:sw=8 203 */ 204