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