xref: /freebsd/lib/libc/tests/stdio/open_memstream2_test.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*-
2  * Copyright (c) 2013 Hudson River Trading LLC
3  * Written by: John H. Baldwin <jhb@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <err.h>
29 #include <errno.h>
30 #include <limits.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <wchar.h>
36 
37 #include <atf-c.h>
38 
39 static char *buf;
40 static size_t len;
41 
42 static void
43 assert_stream(const char *contents)
44 {
45 	if (strlen(contents) != len)
46 		printf("bad length %zd for \"%s\"\n", len, contents);
47 	else if (strncmp(buf, contents, strlen(contents)) != 0)
48 		printf("bad buffer \"%s\" for \"%s\"\n", buf, contents);
49 }
50 
51 ATF_TC_WITHOUT_HEAD(open_group_test);
52 ATF_TC_BODY(open_group_test, tc)
53 {
54 	FILE *fp;
55 	off_t eob;
56 
57 	fp = open_memstream(&buf, &len);
58 	ATF_REQUIRE_MSG(fp != NULL, "open_memstream failed");
59 
60 	fprintf(fp, "hello my world");
61 	fflush(fp);
62 	assert_stream("hello my world");
63 	eob = ftello(fp);
64 	rewind(fp);
65 	fprintf(fp, "good-bye");
66 	fseeko(fp, eob, SEEK_SET);
67 	fclose(fp);
68 	assert_stream("good-bye world");
69 	free(buf);
70 }
71 
72 ATF_TC_WITHOUT_HEAD(simple_tests);
73 ATF_TC_BODY(simple_tests, tc)
74 {
75 	static const char zerobuf[] =
76 	    { 'f', 'o', 'o', 0, 0, 0, 0, 'b', 'a', 'r', 0 };
77 	char c;
78 	FILE *fp;
79 
80 	fp = open_memstream(&buf, NULL);
81 	ATF_REQUIRE_MSG(fp == NULL, "open_memstream did not fail");
82 	ATF_REQUIRE_MSG(errno == EINVAL,
83 	    "open_memstream didn't fail with EINVAL");
84 	fp = open_memstream(NULL, &len);
85 	ATF_REQUIRE_MSG(fp == NULL, "open_memstream did not fail");
86 	ATF_REQUIRE_MSG(errno == EINVAL,
87 	    "open_memstream didn't fail with EINVAL");
88 	fp = open_memstream(&buf, &len);
89 	ATF_REQUIRE_MSG(fp != NULL, "open_memstream failed; errno=%d", errno);
90 	fflush(fp);
91 	assert_stream("");
92 	if (fwide(fp, 0) >= 0)
93 		printf("stream is not byte-oriented\n");
94 
95 	fprintf(fp, "fo");
96 	fflush(fp);
97 	assert_stream("fo");
98 	fputc('o', fp);
99 	fflush(fp);
100 	assert_stream("foo");
101 	rewind(fp);
102 	fflush(fp);
103 	assert_stream("");
104 	fseek(fp, 0, SEEK_END);
105 	fflush(fp);
106 	assert_stream("foo");
107 
108 	/*
109 	 * Test seeking out past the current end.  Should zero-fill the
110 	 * intermediate area.
111 	 */
112 	fseek(fp, 4, SEEK_END);
113 	fprintf(fp, "bar");
114 	fflush(fp);
115 
116 	/*
117 	 * Can't use assert_stream() here since this should contain
118 	 * embedded null characters.
119 	 */
120 	if (len != 10)
121 		printf("bad length %zd for zero-fill test\n", len);
122 	else if (memcmp(buf, zerobuf, sizeof(zerobuf)) != 0)
123 		printf("bad buffer for zero-fill test\n");
124 
125 	fseek(fp, 3, SEEK_SET);
126 	fprintf(fp, " in ");
127 	fflush(fp);
128 	assert_stream("foo in ");
129 	fseek(fp, 0, SEEK_END);
130 	fflush(fp);
131 	assert_stream("foo in bar");
132 
133 	rewind(fp);
134 	if (fread(&c, sizeof(c), 1, fp) != 0)
135 		printf("fread did not fail\n");
136 	else if (!ferror(fp))
137 		printf("error indicator not set after fread\n");
138 	else
139 		clearerr(fp);
140 
141 	fseek(fp, 4, SEEK_SET);
142 	fprintf(fp, "bar baz");
143 	fclose(fp);
144 	assert_stream("foo bar baz");
145 	free(buf);
146 }
147 
148 ATF_TC_WITHOUT_HEAD(seek_tests);
149 ATF_TC_BODY(seek_tests, tc)
150 {
151 	FILE *fp;
152 
153 	fp = open_memstream(&buf, &len);
154 	ATF_REQUIRE_MSG(fp != NULL, "open_memstream failed: %d", errno);
155 
156 #define SEEK_FAIL(offset, whence, error) do {			\
157 	errno = 0;						\
158 	ATF_REQUIRE_MSG(fseeko(fp, (offset), (whence)) != 0,	\
159 	    "fseeko(%s, %s) did not fail, set pos to %jd",	\
160 	    __STRING(offset), __STRING(whence),			\
161 	    (intmax_t)ftello(fp));				\
162 	ATF_REQUIRE_MSG(errno == (error),			\
163 	    "fseeko(%s, %s) failed with %d rather than %s",	\
164 	    __STRING(offset), __STRING(whence),	errno,		\
165 	    __STRING(error));					\
166 } while (0)
167 
168 #define SEEK_OK(offset, whence, result) do {			\
169 	ATF_REQUIRE_MSG(fseeko(fp, (offset), (whence)) == 0,	\
170 	    "fseeko(%s, %s) failed: %s",			\
171 	    __STRING(offset), __STRING(whence), strerror(errno)); \
172 	ATF_REQUIRE_MSG(ftello(fp) == (result),			\
173 	    "fseeko(%s, %s) seeked to %jd rather than %s",	\
174 	    __STRING(offset), __STRING(whence),			\
175 	    (intmax_t)ftello(fp), __STRING(result));		\
176 } while (0)
177 
178 	SEEK_FAIL(-1, SEEK_SET, EINVAL);
179 	SEEK_FAIL(-1, SEEK_CUR, EINVAL);
180 	SEEK_FAIL(-1, SEEK_END, EINVAL);
181 	fprintf(fp, "foo");
182 	SEEK_OK(-1, SEEK_CUR, 2);
183 	SEEK_OK(0, SEEK_SET, 0);
184 	SEEK_OK(-1, SEEK_END, 2);
185 	SEEK_OK(OFF_MAX - 1, SEEK_SET, OFF_MAX - 1);
186 	SEEK_FAIL(2, SEEK_CUR, EOVERFLOW);
187 	fclose(fp);
188 }
189 
190 ATF_TP_ADD_TCS(tp)
191 {
192 
193 	ATF_TP_ADD_TC(tp, open_group_test);
194 	ATF_TP_ADD_TC(tp, simple_tests);
195 	ATF_TP_ADD_TC(tp, seek_tests);
196 
197 	return (atf_no_error());
198 }
199