2 Copyright (C) 2013 Pietro Cerutti <gahr@FreeBSD.org>
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
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.
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
27 * Test basic FILE * functions (fread, fwrite, fseek, fclose) against
28 * a FILE * retrieved using fmemopen()
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
41 ATF_TC_WITHOUT_HEAD(test_preexisting);
42 ATF_TC_BODY(test_preexisting, tc)
44 /* Use a pre-existing buffer. */
47 char str[] = "Test writing some stuff";
48 char str2[] = "AAAAAAAAA";
49 char str3[] = "AAAA writing some stuff";
54 /* Open a FILE * using fmemopen. */
55 fp = fmemopen(buf, sizeof(buf), "w");
56 ATF_REQUIRE(fp != NULL);
58 /* Write to the buffer. */
59 nofw = fwrite(str, 1, sizeof(str), fp);
60 ATF_REQUIRE(nofw == sizeof(str));
62 /* Close the FILE *. */
66 /* Re-open the FILE * to read back the data. */
67 fp = fmemopen(buf, sizeof(buf), "r");
68 ATF_REQUIRE(fp != NULL);
70 /* Read from the buffer. */
71 bzero(buf2, sizeof(buf2));
72 nofr = fread(buf2, 1, sizeof(buf2), fp);
73 ATF_REQUIRE(nofr == sizeof(buf2));
76 * Since a write on a FILE * retrieved by fmemopen
77 * will add a '\0' (if there's space), we can check
78 * the strings for equality.
80 ATF_REQUIRE(strcmp(str, buf2) == 0);
82 /* Close the FILE *. */
86 /* Now open a FILE * on the first 4 bytes of the string. */
87 fp = fmemopen(str, 4, "w");
88 ATF_REQUIRE(fp != NULL);
91 * Try to write more bytes than we shoud, we'll get a short count (4).
93 nofw = fwrite(str2, 1, sizeof(str2), fp);
94 ATF_REQUIRE(nofw == 4);
96 /* Close the FILE *. */
100 /* Check that the string was not modified after the first 4 bytes. */
101 ATF_REQUIRE(strcmp(str, str3) == 0);
104 ATF_TC_WITHOUT_HEAD(test_autoalloc);
105 ATF_TC_BODY(test_autoalloc, tc)
107 /* Let fmemopen allocate the buffer. */
113 /* Open a FILE * using fmemopen. */
114 fp = fmemopen(NULL, 512, "w+");
115 ATF_REQUIRE(fp != NULL);
117 /* fill the buffer */
118 for (i = 0; i < 512; i++) {
119 nofw = fwrite("a", 1, 1, fp);
120 ATF_REQUIRE(nofw == 1);
123 /* Get the current position into the stream. */
125 ATF_REQUIRE(pos == 512);
127 /* Try to write past the end, we should get a short object count (0) */
128 nofw = fwrite("a", 1, 1, fp);
129 ATF_REQUIRE(nofw == 0);
131 /* Close the FILE *. */
133 ATF_REQUIRE(rc == 0);
135 /* Open a FILE * using a wrong mode */
136 fp = fmemopen(NULL, 512, "r");
137 ATF_REQUIRE(fp == NULL);
139 fp = fmemopen(NULL, 512, "w");
140 ATF_REQUIRE(fp == NULL);
143 ATF_TC_WITHOUT_HEAD(test_data_length);
144 ATF_TC_BODY(test_data_length, tc)
147 * Here we test that a read operation doesn't go past the end of the
148 * data actually written, and that a SEEK_END seeks from the end of the
149 * data, not of the whole buffer.
152 char buf[512] = {'\0'};
153 char str[] = "Test data length. ";
154 char str2[] = "Do we have two sentences?";
155 char str3[sizeof(str) + sizeof(str2) -1];
160 /* Open a FILE * for updating our buffer. */
161 fp = fmemopen(buf, sizeof(buf), "w+");
162 ATF_REQUIRE(fp != NULL);
164 /* Write our string into the buffer. */
165 nofw = fwrite(str, 1, sizeof(str), fp);
166 ATF_REQUIRE(nofw == sizeof(str));
168 /* Now seek to the end and check that ftell gives us sizeof(str). */
169 rc = fseek(fp, 0, SEEK_END);
170 ATF_REQUIRE(rc == 0);
172 ATF_REQUIRE(pos == sizeof(str));
174 /* Close the FILE *. */
176 ATF_REQUIRE(rc == 0);
178 /* Reopen the buffer for appending. */
179 fp = fmemopen(buf, sizeof(buf), "a+");
180 ATF_REQUIRE(fp != NULL);
182 /* We should now be writing after the first string. */
183 nofw = fwrite(str2, 1, sizeof(str2), fp);
184 ATF_REQUIRE(nofw == sizeof(str2));
186 /* Rewind the FILE *. */
187 rc = fseek(fp, 0, SEEK_SET);
188 ATF_REQUIRE(rc == 0);
190 /* Make sure we're at the beginning. */
192 ATF_REQUIRE(pos == 0);
194 /* Read the whole buffer. */
195 nofr = fread(str3, 1, sizeof(buf), fp);
196 ATF_REQUIRE(nofr == sizeof(str3));
198 /* Make sure the two strings are there. */
199 ATF_REQUIRE(strncmp(str3, str, sizeof(str) - 1) == 0);
200 ATF_REQUIRE(strncmp(str3 + sizeof(str) - 1, str2, sizeof(str2)) == 0);
202 /* Close the FILE *. */
204 ATF_REQUIRE(rc == 0);
207 ATF_TC_WITHOUT_HEAD(test_binary);
208 ATF_TC_BODY(test_binary, tc)
211 * Make sure that NULL bytes are never appended when opening a buffer
221 /* Pre-fill the buffer. */
222 memset(buf, 'A', sizeof(buf));
224 /* Open a FILE * in binary mode. */
225 fp = fmemopen(buf, sizeof(buf), "w+b");
226 ATF_REQUIRE(fp != NULL);
228 /* Write some data into it. */
229 nofw = fwrite(str, 1, strlen(str), fp);
230 ATF_REQUIRE(nofw == strlen(str));
232 /* Make sure that the buffer doesn't contain any NULL bytes. */
233 for (i = 0; i < sizeof(buf); i++)
234 ATF_REQUIRE(buf[i] != '\0');
236 /* Close the FILE *. */
238 ATF_REQUIRE(rc == 0);
241 ATF_TC_WITHOUT_HEAD(test_append_binary_pos);
242 ATF_TC_BODY(test_append_binary_pos, tc)
245 * For compatibility with other implementations (glibc), we set the
246 * position to 0 when opening an automatically allocated binary stream
252 fp = fmemopen(NULL, 16, "ab+");
253 ATF_REQUIRE(fp != NULL);
254 ATF_REQUIRE(ftell(fp) == 0L);
257 /* Make sure that a pre-allocated buffer behaves correctly. */
258 char buf[] = "Hello";
259 fp = fmemopen(buf, sizeof(buf), "ab+");
260 ATF_REQUIRE(fp != NULL);
261 ATF_REQUIRE(ftell(fp) == strlen(buf));
265 ATF_TC_WITHOUT_HEAD(test_size_0);
266 ATF_TC_BODY(test_size_0, tc)
268 /* POSIX mandates that we return EINVAL if size is 0. */
272 fp = fmemopen(NULL, 0, "r+");
273 ATF_REQUIRE(fp == NULL);
274 ATF_REQUIRE(errno == EINVAL);
280 ATF_TP_ADD_TC(tp, test_autoalloc);
281 ATF_TP_ADD_TC(tp, test_preexisting);
282 ATF_TP_ADD_TC(tp, test_data_length);
283 ATF_TP_ADD_TC(tp, test_binary);
284 ATF_TP_ADD_TC(tp, test_append_binary_pos);
285 ATF_TP_ADD_TC(tp, test_size_0);
287 return (atf_no_error());