xref: /freebsd/lib/libc/tests/stdio/fmemopen2_test.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
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 <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <errno.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <strings.h>
38 
39 #include <atf-c.h>
40 
41 ATF_TC_WITHOUT_HEAD(test_preexisting);
42 ATF_TC_BODY(test_preexisting, tc)
43 {
44 	/* Use a pre-existing buffer. */
45 	char buf[512];
46 	char buf2[512];
47 	char str[]  = "Test writing some stuff";
48 	char str2[] = "AAAAAAAAA";
49 	char str3[] = "AAAA writing some stuff";
50 	FILE *fp;
51 	size_t nofw, nofr;
52 	int rc;
53 
54 	/* Open a FILE * using fmemopen. */
55 	fp = fmemopen(buf, sizeof(buf), "w");
56 	ATF_REQUIRE(fp != NULL);
57 
58 	/* Write to the buffer. */
59 	nofw = fwrite(str, 1, sizeof(str), fp);
60 	ATF_REQUIRE(nofw == sizeof(str));
61 
62 	/* Close the FILE *. */
63 	rc = fclose(fp);
64 	ATF_REQUIRE(rc == 0);
65 
66 	/* Re-open the FILE * to read back the data. */
67 	fp = fmemopen(buf, sizeof(buf), "r");
68 	ATF_REQUIRE(fp != NULL);
69 
70 	/* Read from the buffer. */
71 	bzero(buf2, sizeof(buf2));
72 	nofr = fread(buf2, 1, sizeof(buf2), fp);
73 	ATF_REQUIRE(nofr == sizeof(buf2));
74 
75 	/*
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.
79 	 */
80 	ATF_REQUIRE(strcmp(str, buf2) == 0);
81 
82 	/* Close the FILE *. */
83 	rc = fclose(fp);
84 	ATF_REQUIRE(rc == 0);
85 
86 	/* Now open a FILE * on the first 4 bytes of the string. */
87 	fp = fmemopen(str, 4, "w");
88 	ATF_REQUIRE(fp != NULL);
89 
90 	/*
91 	 * Try to write more bytes than we shoud, we'll get a short count (4).
92 	 */
93 	nofw = fwrite(str2, 1, sizeof(str2), fp);
94 	ATF_REQUIRE(nofw == 4);
95 
96 	/* Close the FILE *. */
97 	rc = fclose(fp);
98 	ATF_REQUIRE(rc == 0);
99 
100 	/* Check that the string was not modified after the first 4 bytes. */
101 	ATF_REQUIRE(strcmp(str, str3) == 0);
102 }
103 
104 ATF_TC_WITHOUT_HEAD(test_autoalloc);
105 ATF_TC_BODY(test_autoalloc, tc)
106 {
107 	/* Let fmemopen allocate the buffer. */
108 	FILE *fp;
109 	long pos;
110 	size_t nofw, i;
111 	int rc;
112 
113 	/* Open a FILE * using fmemopen. */
114 	fp = fmemopen(NULL, 512, "w+");
115 	ATF_REQUIRE(fp != NULL);
116 
117 	/* fill the buffer */
118 	for (i = 0; i < 512; i++) {
119 		nofw = fwrite("a", 1, 1, fp);
120 		ATF_REQUIRE(nofw == 1);
121 	}
122 
123 	/* Get the current position into the stream. */
124 	pos = ftell(fp);
125 	ATF_REQUIRE(pos == 512);
126 
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);
130 
131 	/* Close the FILE *. */
132 	rc = fclose(fp);
133 	ATF_REQUIRE(rc == 0);
134 
135 	/* Open a FILE * using a wrong mode */
136 	fp = fmemopen(NULL, 512, "r");
137 	ATF_REQUIRE(fp == NULL);
138 
139 	fp = fmemopen(NULL, 512, "w");
140 	ATF_REQUIRE(fp == NULL);
141 }
142 
143 ATF_TC_WITHOUT_HEAD(test_data_length);
144 ATF_TC_BODY(test_data_length, tc)
145 {
146 	/*
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.
150 	 */
151 	FILE *fp;
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];
156 	long pos;
157 	size_t nofw, nofr;
158 	int rc;
159 
160 	/* Open a FILE * for updating our buffer. */
161 	fp = fmemopen(buf, sizeof(buf), "w+");
162 	ATF_REQUIRE(fp != NULL);
163 
164 	/* Write our string into the buffer. */
165 	nofw = fwrite(str, 1, sizeof(str), fp);
166 	ATF_REQUIRE(nofw == sizeof(str));
167 
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);
171 	pos = ftell(fp);
172 	ATF_REQUIRE(pos == sizeof(str));
173 
174 	/* Close the FILE *. */
175 	rc = fclose(fp);
176 	ATF_REQUIRE(rc == 0);
177 
178 	/* Reopen the buffer for appending. */
179 	fp = fmemopen(buf, sizeof(buf), "a+");
180 	ATF_REQUIRE(fp != NULL);
181 
182 	/* We should now be writing after the first string. */
183 	nofw = fwrite(str2, 1, sizeof(str2), fp);
184 	ATF_REQUIRE(nofw == sizeof(str2));
185 
186 	/* Rewind the FILE *. */
187 	rc = fseek(fp, 0, SEEK_SET);
188 	ATF_REQUIRE(rc == 0);
189 
190 	/* Make sure we're at the beginning. */
191 	pos = ftell(fp);
192 	ATF_REQUIRE(pos == 0);
193 
194 	/* Read the whole buffer. */
195 	nofr = fread(str3, 1, sizeof(buf), fp);
196 	ATF_REQUIRE(nofr == sizeof(str3));
197 
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);
201 
202 	/* Close the FILE *. */
203 	rc = fclose(fp);
204 	ATF_REQUIRE(rc == 0);
205 }
206 
207 ATF_TC_WITHOUT_HEAD(test_binary);
208 ATF_TC_BODY(test_binary, tc)
209 {
210 	/*
211 	 * Make sure that NULL bytes are never appended when opening a buffer
212 	 * in binary mode.
213 	 */
214 
215 	FILE *fp;
216 	char buf[20];
217 	char str[] = "Test";
218 	size_t nofw;
219 	int rc, i;
220 
221 	/* Pre-fill the buffer. */
222 	memset(buf, 'A', sizeof(buf));
223 
224 	/* Open a FILE * in binary mode. */
225 	fp = fmemopen(buf, sizeof(buf), "w+b");
226 	ATF_REQUIRE(fp != NULL);
227 
228 	/* Write some data into it. */
229 	nofw = fwrite(str, 1, strlen(str), fp);
230 	ATF_REQUIRE(nofw == strlen(str));
231 
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');
235 
236 	/* Close the FILE *. */
237 	rc = fclose(fp);
238 	ATF_REQUIRE(rc == 0);
239 }
240 
241 ATF_TC_WITHOUT_HEAD(test_append_binary_pos);
242 ATF_TC_BODY(test_append_binary_pos, tc)
243 {
244 	/*
245 	 * For compatibility with other implementations (glibc), we set the
246 	 * position to 0 when opening an automatically allocated binary stream
247 	 * for appending.
248 	 */
249 
250 	FILE *fp;
251 
252 	fp = fmemopen(NULL, 16, "ab+");
253 	ATF_REQUIRE(fp != NULL);
254 	ATF_REQUIRE(ftell(fp) == 0L);
255 	fclose(fp);
256 
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));
262 	fclose(fp);
263 }
264 
265 ATF_TC_WITHOUT_HEAD(test_size_0);
266 ATF_TC_BODY(test_size_0, tc)
267 {
268 	/* POSIX mandates that we return EINVAL if size is 0. */
269 
270 	FILE *fp;
271 
272 	fp = fmemopen(NULL, 0, "r+");
273 	ATF_REQUIRE(fp == NULL);
274 	ATF_REQUIRE(errno == EINVAL);
275 }
276 
277 ATF_TP_ADD_TCS(tp)
278 {
279 
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);
286 
287 	return (atf_no_error());
288 }
289