fmemopen2_test.c revision 246120
1/*-
2Copyright (C) 2013 Pietro Cerutti <gahr@FreeBSD.org>
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions
6are met:
71. Redistributions of source code must retain the above copyright
8   notice, this list of conditions and the following disclaimer.
92. 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
13THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23SUCH 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: head/tools/regression/lib/libc/stdio/test-fmemopen.c 246120 2013-01-30 14:59:26Z gahr $");
33
34#include <assert.h>
35#include <errno.h>
36#include <stdio.h>
37#include <string.h>
38#include <strings.h>
39
40void
41test_preexisting ()
42{
43	/*
44	 * use a pre-existing buffer
45	 */
46
47	char buf[512];
48	char buf2[512];
49	char str[]  = "Test writing some stuff";
50	char str2[] = "AAAAAAAAA";
51	char str3[] = "AAAA writing some stuff";
52	FILE *fp;
53	size_t nofw, nofr;
54	int rc;
55
56	/* open a FILE * using fmemopen */
57	fp = fmemopen (buf, sizeof buf, "w");
58	assert (fp != NULL);
59
60	/* write to the buffer */
61	nofw = fwrite (str, 1, sizeof str, fp);
62	assert (nofw == sizeof str);
63
64	/* close the FILE * */
65	rc = fclose (fp);
66	assert (rc == 0);
67
68	/* re-open the FILE * to read back the data */
69	fp = fmemopen (buf, sizeof buf, "r");
70	assert (fp != NULL);
71
72	/* read from the buffer */
73	bzero (buf2, sizeof buf2);
74	nofr = fread (buf2, 1, sizeof buf2, fp);
75	assert (nofr == sizeof buf2);
76
77	/* since a write on a FILE * retrieved by fmemopen
78	 * will add a '\0' (if there's space), we can check
79	 * the strings for equality */
80	assert (strcmp(str, buf2) == 0);
81
82	/* close the FILE * */
83	rc = fclose (fp);
84	assert (rc == 0);
85
86	/* now open a FILE * on the first 4 bytes of the string */
87	fp = fmemopen (str, 4, "w");
88	assert (fp != NULL);
89
90	/* try to write more bytes than we shoud, we'll get a short count (4) */
91	nofw = fwrite (str2, 1, sizeof str2, fp);
92	assert (nofw == 4);
93
94	/* close the FILE * */
95	rc = fclose (fp);
96
97	/* check that the string was not modified after the first 4 bytes */
98	assert (strcmp (str, str3) == 0);
99}
100
101void
102test_autoalloc ()
103{
104	/*
105	 * let fmemopen allocate the buffer
106	 */
107
108	char str[] = "A quick test";
109	FILE *fp;
110	long pos;
111	size_t nofw, nofr, i;
112	int rc;
113
114	/* open a FILE * using fmemopen */
115	fp = fmemopen (NULL, 512, "w");
116	assert (fp != NULL);
117
118	/* fill the buffer */
119	for (i = 0; i < 512; i++) {
120		nofw = fwrite ("a", 1, 1, fp);
121		assert (nofw == 1);
122	}
123
124	/* get the current position into the stream */
125	pos = ftell (fp);
126	assert (pos == 512);
127
128	/* try to write past the end, we should get a short object count (0) */
129	nofw = fwrite ("a", 1, 1, fp);
130	assert (nofw == 0);
131
132	/* close the FILE * */
133	rc = fclose (fp);
134	assert (rc == 0);
135}
136
137int
138main (void)
139{
140	test_autoalloc   ();
141	test_preexisting ();
142	return (0);
143}
144