summaryrefslogtreecommitdiffstats
path: root/lib/libc/tests/stdio/fmemopen2_test.c
blob: 6b6392dcde879d184d0d654ff6fc182acb73c2fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*-
Copyright (C) 2013 Pietro Cerutti <gahr@FreeBSD.org>

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/

/*
 * Test basic FILE * functions (fread, fwrite, fseek, fclose) against
 * a FILE * retrieved using fmemopen()
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>

#include <atf-c.h>

ATF_TC_WITHOUT_HEAD(test_preexisting);
ATF_TC_BODY(test_preexisting, tc)
{
	/* Use a pre-existing buffer. */
	char buf[512];
	char buf2[512];
	char str[]  = "Test writing some stuff";
	char str2[] = "AAAAAAAAA";
	char str3[] = "AAAA writing some stuff";
	FILE *fp;
	size_t nofw, nofr;
	int rc;

	/* Open a FILE * using fmemopen. */
	fp = fmemopen(buf, sizeof(buf), "w");
	ATF_REQUIRE(fp != NULL);

	/* Write to the buffer. */
	nofw = fwrite(str, 1, sizeof(str), fp);
	ATF_REQUIRE(nofw == sizeof(str));

	/* Close the FILE *. */
	rc = fclose(fp);
	ATF_REQUIRE(rc == 0);

	/* Re-open the FILE * to read back the data. */
	fp = fmemopen(buf, sizeof(buf), "r");
	ATF_REQUIRE(fp != NULL);

	/* Read from the buffer. */
	bzero(buf2, sizeof(buf2));
	nofr = fread(buf2, 1, sizeof(buf2), fp);
	ATF_REQUIRE(nofr == sizeof(buf2));

	/*
	 * Since a write on a FILE * retrieved by fmemopen
	 * will add a '\0' (if there's space), we can check
	 * the strings for equality.
	 */
	ATF_REQUIRE(strcmp(str, buf2) == 0);

	/* Close the FILE *. */
	rc = fclose(fp);
	ATF_REQUIRE(rc == 0);

	/* Now open a FILE * on the first 4 bytes of the string. */
	fp = fmemopen(str, 4, "w");
	ATF_REQUIRE(fp != NULL);

	/*
	 * Try to write more bytes than we shoud, we'll get a short count (4).
	 */
	nofw = fwrite(str2, 1, sizeof(str2), fp);
	ATF_REQUIRE(nofw == 4);

	/* Close the FILE *. */
	rc = fclose(fp);

	/* Check that the string was not modified after the first 4 bytes. */
	ATF_REQUIRE(strcmp(str, str3) == 0);
}

ATF_TC_WITHOUT_HEAD(test_autoalloc);
ATF_TC_BODY(test_autoalloc, tc)
{
	/* Let fmemopen allocate the buffer. */
	FILE *fp;
	long pos;
	size_t nofw, i;
	int rc;

	/* Open a FILE * using fmemopen. */
	fp = fmemopen(NULL, 512, "w+");
	ATF_REQUIRE(fp != NULL);

	/* fill the buffer */
	for (i = 0; i < 512; i++) {
		nofw = fwrite("a", 1, 1, fp);
		ATF_REQUIRE(nofw == 1);
	}

	/* Get the current position into the stream. */
	pos = ftell(fp);
	ATF_REQUIRE(pos == 512);

	/* Try to write past the end, we should get a short object count (0) */
	nofw = fwrite("a", 1, 1, fp);
	ATF_REQUIRE(nofw == 0);

	/* Close the FILE *. */
	rc = fclose(fp);
	ATF_REQUIRE(rc == 0);

	/* Open a FILE * using a wrong mode */
	fp = fmemopen(NULL, 512, "r");
	ATF_REQUIRE(fp == NULL);

	fp = fmemopen(NULL, 512, "w");
	ATF_REQUIRE(fp == NULL);
}

ATF_TC_WITHOUT_HEAD(test_data_length);
ATF_TC_BODY(test_data_length, tc)
{
	/*
	 * Here we test that a read operation doesn't go past the end of the
	 * data actually written, and that a SEEK_END seeks from the end of the
	 * data, not of the whole buffer.
	 */
	FILE *fp;
	char buf[512] = {'\0'};
	char str[]  = "Test data length. ";
	char str2[] = "Do we have two sentences?";
	char str3[sizeof(str) + sizeof(str2) -1];
	long pos;
	size_t nofw, nofr;
	int rc;

	/* Open a FILE * for updating our buffer. */
	fp = fmemopen(buf, sizeof(buf), "w+");
	ATF_REQUIRE(fp != NULL);

	/* Write our string into the buffer. */
	nofw = fwrite(str, 1, sizeof(str), fp);
	ATF_REQUIRE(nofw == sizeof(str));

	/* Now seek to the end and check that ftell gives us sizeof(str). */
	rc = fseek(fp, 0, SEEK_END);
	ATF_REQUIRE(rc == 0);
	pos = ftell(fp);
	ATF_REQUIRE(pos == sizeof(str));

	/* Close the FILE *. */
	rc = fclose(fp);
	ATF_REQUIRE(rc == 0);

	/* Reopen the buffer for appending. */
	fp = fmemopen(buf, sizeof(buf), "a+");
	ATF_REQUIRE(fp != NULL);

	/* We should now be writing after the first string. */
	nofw = fwrite(str2, 1, sizeof(str2), fp);
	ATF_REQUIRE(nofw == sizeof(str2));

	/* Rewind the FILE *. */
	rc = fseek(fp, 0, SEEK_SET);
	ATF_REQUIRE(rc == 0);

	/* Make sure we're at the beginning. */
	pos = ftell(fp);
	ATF_REQUIRE(pos == 0);

	/* Read the whole buffer. */
	nofr = fread(str3, 1, sizeof(buf), fp);
	ATF_REQUIRE(nofr == sizeof(str3));

	/* Make sure the two strings are there. */
	ATF_REQUIRE(strncmp(str3, str, sizeof(str) - 1) == 0);
	ATF_REQUIRE(strncmp(str3 + sizeof(str) - 1, str2, sizeof(str2)) == 0);

	/* Close the FILE *. */
	rc = fclose(fp);
	ATF_REQUIRE(rc == 0);
}

ATF_TC_WITHOUT_HEAD(test_binary);
ATF_TC_BODY(test_binary, tc)
{
	/*
	 * Make sure that NULL bytes are never appended when opening a buffer
	 * in binary mode.
	 */

	FILE *fp;
	char buf[20];
	char str[] = "Test";
	size_t nofw;
	int rc, i;

	/* Pre-fill the buffer. */
	memset(buf, 'A', sizeof(buf));

	/* Open a FILE * in binary mode. */
	fp = fmemopen(buf, sizeof(buf), "w+b");
	ATF_REQUIRE(fp != NULL);

	/* Write some data into it. */
	nofw = fwrite(str, 1, strlen(str), fp);
	ATF_REQUIRE(nofw == strlen(str));

	/* Make sure that the buffer doesn't contain any NULL bytes. */
	for (i = 0; i < sizeof(buf); i++)
		ATF_REQUIRE(buf[i] != '\0');

	/* Close the FILE *. */
	rc = fclose(fp);
	ATF_REQUIRE(rc == 0);
}

ATF_TC_WITHOUT_HEAD(test_append_binary_pos);
ATF_TC_BODY(test_append_binary_pos, tc)
{
	/*
	 * For compatibility with other implementations (glibc), we set the
	 * position to 0 when opening an automatically allocated binary stream
	 * for appending.
	 */

	FILE *fp;

	fp = fmemopen(NULL, 16, "ab+");
	ATF_REQUIRE(fp != NULL);
	ATF_REQUIRE(ftell(fp) == 0L);
	fclose(fp);

	/* Make sure that a pre-allocated buffer behaves correctly. */
	char buf[] = "Hello";
	fp = fmemopen(buf, sizeof(buf), "ab+");
	ATF_REQUIRE(fp != NULL);
	ATF_REQUIRE(ftell(fp) == strlen(buf));
	fclose(fp);
}

ATF_TC_WITHOUT_HEAD(test_size_0);
ATF_TC_BODY(test_size_0, tc)
{
	/* POSIX mandates that we return EINVAL if size is 0. */

	FILE *fp;

	fp = fmemopen(NULL, 0, "r+");
	ATF_REQUIRE(fp == NULL);
	ATF_REQUIRE(errno == EINVAL);
}

ATF_TP_ADD_TCS(tp)
{

	ATF_TP_ADD_TC(tp, test_autoalloc);
	ATF_TP_ADD_TC(tp, test_preexisting);
	ATF_TP_ADD_TC(tp, test_data_length);
	ATF_TP_ADD_TC(tp, test_binary);
	ATF_TP_ADD_TC(tp, test_append_binary_pos);
	ATF_TP_ADD_TC(tp, test_size_0);

	return (atf_no_error());
}
OpenPOWER on IntegriCloud