1 /*- 2 Copyright (C) 2013 Pietro Cerutti <gahr@FreeBSD.org> 3 4 Redistribution and use in source and binary forms, with or without 5 modification, are permitted provided that the following conditions 6 are met: 7 1. Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. 9 2. Redistributions in binary form must reproduce the above copyright 10 notice, this list of conditions and the following disclaimer in the 11 documentation and/or other materials provided with the distribution. 12 13 THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 17 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 SUCH DAMAGE. 24 */ 25 26 /* 27 * Test basic FILE * functions (fread, fwrite, fseek, fclose) against 28 * a FILE * retrieved using fmemopen() 29 */ 30 31 #include <errno.h> 32 #include <stdio.h> 33 #include <string.h> 34 #include <strings.h> 35 36 #include <atf-c.h> 37 38 ATF_TC_WITHOUT_HEAD(test_preexisting); 39 ATF_TC_BODY(test_preexisting, tc) 40 { 41 /* Use a pre-existing buffer. */ 42 char buf[512]; 43 char buf2[512]; 44 char str[] = "Test writing some stuff"; 45 char str2[] = "AAAAAAAAA"; 46 char str3[] = "AAAA writing some stuff"; 47 FILE *fp; 48 size_t nofw, nofr; 49 int rc; 50 51 /* Open a FILE * using fmemopen. */ 52 fp = fmemopen(buf, sizeof(buf), "w"); 53 ATF_REQUIRE(fp != NULL); 54 55 /* Write to the buffer. */ 56 nofw = fwrite(str, 1, sizeof(str), fp); 57 ATF_REQUIRE(nofw == sizeof(str)); 58 59 /* Close the FILE *. */ 60 rc = fclose(fp); 61 ATF_REQUIRE(rc == 0); 62 63 /* Re-open the FILE * to read back the data. */ 64 fp = fmemopen(buf, sizeof(buf), "r"); 65 ATF_REQUIRE(fp != NULL); 66 67 /* Read from the buffer. */ 68 bzero(buf2, sizeof(buf2)); 69 nofr = fread(buf2, 1, sizeof(buf2), fp); 70 ATF_REQUIRE(nofr == sizeof(buf2)); 71 72 /* 73 * Since a write on a FILE * retrieved by fmemopen 74 * will add a '\0' (if there's space), we can check 75 * the strings for equality. 76 */ 77 ATF_REQUIRE(strcmp(str, buf2) == 0); 78 79 /* Close the FILE *. */ 80 rc = fclose(fp); 81 ATF_REQUIRE(rc == 0); 82 83 /* Now open a FILE * on the first 4 bytes of the string. */ 84 fp = fmemopen(str, 4, "w"); 85 ATF_REQUIRE(fp != NULL); 86 87 /* 88 * Try to write more bytes than we shoud, we'll get a short count (4). 89 */ 90 nofw = fwrite(str2, 1, sizeof(str2), fp); 91 ATF_REQUIRE(nofw == 4); 92 93 /* Close the FILE *. */ 94 rc = fclose(fp); 95 ATF_REQUIRE(rc == 0); 96 97 /* Check that the string was not modified after the first 4 bytes. */ 98 ATF_REQUIRE(strcmp(str, str3) == 0); 99 } 100 101 ATF_TC_WITHOUT_HEAD(test_autoalloc); 102 ATF_TC_BODY(test_autoalloc, tc) 103 { 104 /* Let fmemopen allocate the buffer. */ 105 FILE *fp; 106 long pos; 107 size_t nofw, i; 108 int rc; 109 110 /* Open a FILE * using fmemopen. */ 111 fp = fmemopen(NULL, 512, "w+"); 112 ATF_REQUIRE(fp != NULL); 113 114 /* fill the buffer */ 115 for (i = 0; i < 512; i++) { 116 nofw = fwrite("a", 1, 1, fp); 117 ATF_REQUIRE(nofw == 1); 118 } 119 120 /* Get the current position into the stream. */ 121 pos = ftell(fp); 122 ATF_REQUIRE(pos == 512); 123 124 /* Try to write past the end, we should get a short object count (0) */ 125 nofw = fwrite("a", 1, 1, fp); 126 ATF_REQUIRE(nofw == 0); 127 128 /* Close the FILE *. */ 129 rc = fclose(fp); 130 ATF_REQUIRE(rc == 0); 131 132 /* Open a FILE * using a wrong mode */ 133 fp = fmemopen(NULL, 512, "r"); 134 ATF_REQUIRE(fp == NULL); 135 136 fp = fmemopen(NULL, 512, "w"); 137 ATF_REQUIRE(fp == NULL); 138 } 139 140 ATF_TC_WITHOUT_HEAD(test_data_length); 141 ATF_TC_BODY(test_data_length, tc) 142 { 143 /* 144 * Here we test that a read operation doesn't go past the end of the 145 * data actually written, and that a SEEK_END seeks from the end of the 146 * data, not of the whole buffer. 147 */ 148 FILE *fp; 149 char buf[512] = {'\0'}; 150 char str[] = "Test data length. "; 151 char str2[] = "Do we have two sentences?"; 152 char str3[sizeof(str) + sizeof(str2) -1]; 153 long pos; 154 size_t nofw, nofr; 155 int rc; 156 157 /* Open a FILE * for updating our buffer. */ 158 fp = fmemopen(buf, sizeof(buf), "w+"); 159 ATF_REQUIRE(fp != NULL); 160 161 /* Write our string into the buffer. */ 162 nofw = fwrite(str, 1, sizeof(str), fp); 163 ATF_REQUIRE(nofw == sizeof(str)); 164 165 /* Now seek to the end and check that ftell gives us sizeof(str). */ 166 rc = fseek(fp, 0, SEEK_END); 167 ATF_REQUIRE(rc == 0); 168 pos = ftell(fp); 169 ATF_REQUIRE(pos == sizeof(str)); 170 171 /* Close the FILE *. */ 172 rc = fclose(fp); 173 ATF_REQUIRE(rc == 0); 174 175 /* Reopen the buffer for appending. */ 176 fp = fmemopen(buf, sizeof(buf), "a+"); 177 ATF_REQUIRE(fp != NULL); 178 179 /* We should now be writing after the first string. */ 180 nofw = fwrite(str2, 1, sizeof(str2), fp); 181 ATF_REQUIRE(nofw == sizeof(str2)); 182 183 /* Rewind the FILE *. */ 184 rc = fseek(fp, 0, SEEK_SET); 185 ATF_REQUIRE(rc == 0); 186 187 /* Make sure we're at the beginning. */ 188 pos = ftell(fp); 189 ATF_REQUIRE(pos == 0); 190 191 /* Read the whole buffer. */ 192 nofr = fread(str3, 1, sizeof(buf), fp); 193 ATF_REQUIRE(nofr == sizeof(str3)); 194 195 /* Make sure the two strings are there. */ 196 ATF_REQUIRE(strncmp(str3, str, sizeof(str) - 1) == 0); 197 ATF_REQUIRE(strncmp(str3 + sizeof(str) - 1, str2, sizeof(str2)) == 0); 198 199 /* Close the FILE *. */ 200 rc = fclose(fp); 201 ATF_REQUIRE(rc == 0); 202 } 203 204 ATF_TC_WITHOUT_HEAD(test_binary); 205 ATF_TC_BODY(test_binary, tc) 206 { 207 /* 208 * Make sure that NULL bytes are never appended when opening a buffer 209 * in binary mode. 210 */ 211 212 FILE *fp; 213 char buf[20]; 214 char str[] = "Test"; 215 size_t nofw; 216 int rc, i; 217 218 /* Pre-fill the buffer. */ 219 memset(buf, 'A', sizeof(buf)); 220 221 /* Open a FILE * in binary mode. */ 222 fp = fmemopen(buf, sizeof(buf), "w+b"); 223 ATF_REQUIRE(fp != NULL); 224 225 /* Write some data into it. */ 226 nofw = fwrite(str, 1, strlen(str), fp); 227 ATF_REQUIRE(nofw == strlen(str)); 228 229 /* Make sure that the buffer doesn't contain any NULL bytes. */ 230 for (i = 0; i < sizeof(buf); i++) 231 ATF_REQUIRE(buf[i] != '\0'); 232 233 /* Close the FILE *. */ 234 rc = fclose(fp); 235 ATF_REQUIRE(rc == 0); 236 } 237 238 ATF_TC_WITHOUT_HEAD(test_append_binary_pos); 239 ATF_TC_BODY(test_append_binary_pos, tc) 240 { 241 /* 242 * For compatibility with other implementations (glibc), we set the 243 * position to 0 when opening an automatically allocated binary stream 244 * for appending. 245 */ 246 247 FILE *fp; 248 249 fp = fmemopen(NULL, 16, "ab+"); 250 ATF_REQUIRE(fp != NULL); 251 ATF_REQUIRE(ftell(fp) == 0L); 252 fclose(fp); 253 254 /* Make sure that a pre-allocated buffer behaves correctly. */ 255 char buf[] = "Hello"; 256 fp = fmemopen(buf, sizeof(buf), "ab+"); 257 ATF_REQUIRE(fp != NULL); 258 ATF_REQUIRE(ftell(fp) == strlen(buf)); 259 fclose(fp); 260 } 261 262 ATF_TC_WITHOUT_HEAD(test_size_0); 263 ATF_TC_BODY(test_size_0, tc) 264 { 265 /* POSIX mandates that we return EINVAL if size is 0. */ 266 267 FILE *fp; 268 269 fp = fmemopen(NULL, 0, "r+"); 270 ATF_REQUIRE(fp == NULL); 271 ATF_REQUIRE(errno == EINVAL); 272 } 273 274 ATF_TP_ADD_TCS(tp) 275 { 276 277 ATF_TP_ADD_TC(tp, test_autoalloc); 278 ATF_TP_ADD_TC(tp, test_preexisting); 279 ATF_TP_ADD_TC(tp, test_data_length); 280 ATF_TP_ADD_TC(tp, test_binary); 281 ATF_TP_ADD_TC(tp, test_append_binary_pos); 282 ATF_TP_ADD_TC(tp, test_size_0); 283 284 return (atf_no_error()); 285 } 286