xref: /freebsd/lib/libc/tests/stdio/fmemopen2_test.c (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
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 	ATF_REQUIRE(errno == EINVAL);
136 
137 	fp = fmemopen(NULL, 512, "w");
138 	ATF_REQUIRE(fp == NULL);
139 	ATF_REQUIRE(errno == EINVAL);
140 }
141 
142 ATF_TC_WITHOUT_HEAD(test_data_length);
143 ATF_TC_BODY(test_data_length, tc)
144 {
145 	/*
146 	 * Here we test that a read operation doesn't go past the end of the
147 	 * data actually written, and that a SEEK_END seeks from the end of the
148 	 * data, not of the whole buffer.
149 	 */
150 	FILE *fp;
151 	char buf[512] = {'\0'};
152 	char str[]  = "Test data length. ";
153 	char str2[] = "Do we have two sentences?";
154 	char str3[sizeof(str) + sizeof(str2) -1];
155 	long pos;
156 	size_t nofw, nofr;
157 	int rc;
158 
159 	/* Open a FILE * for updating our buffer. */
160 	fp = fmemopen(buf, sizeof(buf), "w+");
161 	ATF_REQUIRE(fp != NULL);
162 
163 	/* Write our string into the buffer. */
164 	nofw = fwrite(str, 1, sizeof(str), fp);
165 	ATF_REQUIRE(nofw == sizeof(str));
166 
167 	/* Now seek to the end and check that ftell gives us sizeof(str). */
168 	rc = fseek(fp, 0, SEEK_END);
169 	ATF_REQUIRE(rc == 0);
170 	pos = ftell(fp);
171 	ATF_REQUIRE(pos == sizeof(str));
172 
173 	/* Close the FILE *. */
174 	rc = fclose(fp);
175 	ATF_REQUIRE(rc == 0);
176 
177 	/* Reopen the buffer for appending. */
178 	fp = fmemopen(buf, sizeof(buf), "a+");
179 	ATF_REQUIRE(fp != NULL);
180 
181 	/* We should now be writing after the first string. */
182 	nofw = fwrite(str2, 1, sizeof(str2), fp);
183 	ATF_REQUIRE(nofw == sizeof(str2));
184 
185 	/* Rewind the FILE *. */
186 	rc = fseek(fp, 0, SEEK_SET);
187 	ATF_REQUIRE(rc == 0);
188 
189 	/* Make sure we're at the beginning. */
190 	pos = ftell(fp);
191 	ATF_REQUIRE(pos == 0);
192 
193 	/* Read the whole buffer. */
194 	nofr = fread(str3, 1, sizeof(buf), fp);
195 	ATF_REQUIRE(nofr == sizeof(str3));
196 
197 	/* Make sure the two strings are there. */
198 	ATF_REQUIRE(strncmp(str3, str, sizeof(str) - 1) == 0);
199 	ATF_REQUIRE(strncmp(str3 + sizeof(str) - 1, str2, sizeof(str2)) == 0);
200 
201 	/* Close the FILE *. */
202 	rc = fclose(fp);
203 	ATF_REQUIRE(rc == 0);
204 }
205 
206 ATF_TC_WITHOUT_HEAD(test_binary);
207 ATF_TC_BODY(test_binary, tc)
208 {
209 	/*
210 	 * Make sure that NULL bytes are never appended when opening a buffer
211 	 * in binary mode.
212 	 */
213 
214 	FILE *fp;
215 	char buf[20];
216 	char str[] = "Test";
217 	size_t nofw;
218 	int rc, i;
219 
220 	/* Pre-fill the buffer. */
221 	memset(buf, 'A', sizeof(buf));
222 
223 	/* Open a FILE * in binary mode. */
224 	fp = fmemopen(buf, sizeof(buf), "w+b");
225 	ATF_REQUIRE(fp != NULL);
226 
227 	/* Write some data into it. */
228 	nofw = fwrite(str, 1, strlen(str), fp);
229 	ATF_REQUIRE(nofw == strlen(str));
230 
231 	/* Make sure that the buffer doesn't contain any NULL bytes. */
232 	for (i = 0; i < sizeof(buf); i++)
233 		ATF_REQUIRE(buf[i] != '\0');
234 
235 	/* Close the FILE *. */
236 	rc = fclose(fp);
237 	ATF_REQUIRE(rc == 0);
238 }
239 
240 ATF_TC_WITHOUT_HEAD(test_append_binary_pos);
241 ATF_TC_BODY(test_append_binary_pos, tc)
242 {
243 	/*
244 	 * For compatibility with other implementations (glibc), we set the
245 	 * position to 0 when opening an automatically allocated binary stream
246 	 * for appending.
247 	 */
248 
249 	FILE *fp;
250 
251 	fp = fmemopen(NULL, 16, "ab+");
252 	ATF_REQUIRE(fp != NULL);
253 	ATF_REQUIRE(ftell(fp) == 0L);
254 	fclose(fp);
255 
256 	/* Make sure that a pre-allocated buffer behaves correctly. */
257 	char buf[] = "Hello";
258 	fp = fmemopen(buf, sizeof(buf), "ab+");
259 	ATF_REQUIRE(fp != NULL);
260 	ATF_REQUIRE(ftell(fp) == strlen(buf));
261 	fclose(fp);
262 }
263 
264 ATF_TC_WITHOUT_HEAD(test_size_0);
265 ATF_TC_BODY(test_size_0, tc)
266 {
267 	/* POSIX mandates that we return EINVAL if size is 0. */
268 
269 	FILE *fp;
270 
271 	fp = fmemopen(NULL, 0, "r+");
272 	ATF_REQUIRE(fp == NULL);
273 	ATF_REQUIRE(errno == EINVAL);
274 }
275 
276 /*
277  * PR281953 - ensure we cannot write in read-only only mode, and cannot read in
278  * write-only mode.
279  */
280 ATF_TC_WITHOUT_HEAD(test_rdonly_wronly);
281 ATF_TC_BODY(test_rdonly_wronly, tc)
282 {
283 	FILE *fp;
284 	char buf[16];
285 	char buf_orig[16] = "input data";
286 	char buf_write[16] = "write";
287 	size_t sz;
288 
289 	memcpy(buf, buf_orig, sizeof(buf));
290 	fp = fmemopen(buf, sizeof(buf), "r");
291 	ATF_REQUIRE(fp != NULL);
292 	sz = fwrite(buf_write, 1, strlen(buf_write), fp);
293 	ATF_REQUIRE(sz == 0);
294 	ATF_REQUIRE(errno == EBADF);
295 	ATF_REQUIRE(memcmp(buf, buf_orig, sizeof(buf)) == 0);
296 	fclose(fp);
297 
298 	fp = fmemopen(buf_orig, sizeof(buf), "w");
299 	ATF_REQUIRE(fp != NULL);
300 	sz = fread(buf, sizeof(buf), 1, fp);
301 	ATF_REQUIRE(sz == 0);
302 	ATF_REQUIRE(errno == EBADF);
303 	fclose(fp);
304 }
305 
306 ATF_TP_ADD_TCS(tp)
307 {
308 
309 	ATF_TP_ADD_TC(tp, test_autoalloc);
310 	ATF_TP_ADD_TC(tp, test_preexisting);
311 	ATF_TP_ADD_TC(tp, test_data_length);
312 	ATF_TP_ADD_TC(tp, test_binary);
313 	ATF_TP_ADD_TC(tp, test_append_binary_pos);
314 	ATF_TP_ADD_TC(tp, test_size_0);
315 	ATF_TP_ADD_TC(tp, test_rdonly_wronly);
316 
317 	return (atf_no_error());
318 }
319