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 <sys/cdefs.h>
29 #include <err.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <wchar.h>
37 
38 #include <atf-c.h>
39 
40 static wchar_t *buf;
41 static size_t len;
42 
43 static void
44 assert_stream(const wchar_t *contents)
45 {
46 	if (wcslen(contents) != len)
47 		printf("bad length %zd for \"%ls\"\n", len, contents);
48 	else if (wcsncmp(buf, contents, wcslen(contents)) != 0)
49 		printf("bad buffer \"%ls\" for \"%ls\"\n", buf, contents);
50 }
51 
52 ATF_TC_WITHOUT_HEAD(open_group_test);
53 ATF_TC_BODY(open_group_test, tc)
54 {
55 	FILE *fp;
56 	off_t eob;
57 
58 	fp = open_wmemstream(&buf, &len);
59 	ATF_REQUIRE_MSG(fp != NULL, "open_wmemstream failed");
60 
61 	fwprintf(fp, L"hello my world");
62 	fflush(fp);
63 	assert_stream(L"hello my world");
64 	eob = ftello(fp);
65 	rewind(fp);
66 	fwprintf(fp, L"good-bye");
67 	fseeko(fp, eob, SEEK_SET);
68 	fclose(fp);
69 	assert_stream(L"good-bye world");
70 	free(buf);
71 }
72 
73 ATF_TC_WITHOUT_HEAD(simple_tests);
74 ATF_TC_BODY(simple_tests, tc)
75 {
76 	static const wchar_t zerobuf[] =
77 	    { L'f', L'o', L'o', 0, 0, 0, 0, L'b', L'a', L'r', 0 };
78 	wchar_t c;
79 	FILE *fp;
80 
81 	fp = open_wmemstream(&buf, NULL);
82 	ATF_REQUIRE_MSG(fp == NULL, "open_wmemstream did not fail");
83 	ATF_REQUIRE_MSG(errno == EINVAL,
84 	    "open_wmemstream didn't fail with EINVAL");
85 	fp = open_wmemstream(NULL, &len);
86 	ATF_REQUIRE_MSG(fp == NULL, "open_wmemstream did not fail");
87 	ATF_REQUIRE_MSG(errno == EINVAL,
88 	    "open_wmemstream didn't fail with EINVAL");
89 	fp = open_wmemstream(&buf, &len);
90 	ATF_REQUIRE_MSG(fp != NULL, "open_memstream failed; errno=%d", errno);
91 	fflush(fp);
92 	assert_stream(L"");
93 	if (fwide(fp, 0) <= 0)
94 		printf("stream is not wide-oriented\n");
95 
96 	fwprintf(fp, L"fo");
97 	fflush(fp);
98 	assert_stream(L"fo");
99 	fputwc(L'o', fp);
100 	fflush(fp);
101 	assert_stream(L"foo");
102 	rewind(fp);
103 	fflush(fp);
104 	assert_stream(L"");
105 	fseek(fp, 0, SEEK_END);
106 	fflush(fp);
107 	assert_stream(L"foo");
108 
109 	/*
110 	 * Test seeking out past the current end.  Should zero-fill the
111 	 * intermediate area.
112 	 */
113 	fseek(fp, 4, SEEK_END);
114 	fwprintf(fp, L"bar");
115 	fflush(fp);
116 
117 	/*
118 	 * Can't use assert_stream() here since this should contain
119 	 * embedded null characters.
120 	 */
121 	if (len != 10)
122 		printf("bad length %zd for zero-fill test\n", len);
123 	else if (memcmp(buf, zerobuf, sizeof(zerobuf)) != 0)
124 		printf("bad buffer for zero-fill test\n");
125 
126 	fseek(fp, 3, SEEK_SET);
127 	fwprintf(fp, L" in ");
128 	fflush(fp);
129 	assert_stream(L"foo in ");
130 	fseek(fp, 0, SEEK_END);
131 	fflush(fp);
132 	assert_stream(L"foo in bar");
133 
134 	rewind(fp);
135 	if (fread(&c, sizeof(c), 1, fp) != 0)
136 		printf("fread did not fail\n");
137 	else if (!ferror(fp))
138 		printf("error indicator not set after fread\n");
139 	else
140 		clearerr(fp);
141 
142 	fseek(fp, 4, SEEK_SET);
143 	fwprintf(fp, L"bar baz");
144 	fclose(fp);
145 	assert_stream(L"foo bar baz");
146 	free(buf);
147 }
148 
149 ATF_TC_WITHOUT_HEAD(seek_tests);
150 ATF_TC_BODY(seek_tests, tc)
151 {
152 	FILE *fp;
153 
154 	fp = open_wmemstream(&buf, &len);
155 	ATF_REQUIRE_MSG(fp != NULL, "open_wmemstream failed; errno=%d", errno);
156 
157 #define SEEK_FAIL(offset, whence, error) do {			\
158 	errno = 0;						\
159 	ATF_REQUIRE_MSG(fseeko(fp, (offset), (whence)) != 0,	\
160 	    "fseeko(%s, %s) did not fail, set pos to %jd",	\
161 	    __STRING(offset), __STRING(whence),			\
162 	    (intmax_t)ftello(fp));				\
163 	ATF_REQUIRE_MSG(errno == (error),			\
164 	    "fseeko(%s, %s) failed with %d rather than %s",	\
165 	    __STRING(offset), __STRING(whence),	errno,		\
166 	    __STRING(error));					\
167 } while (0)
168 
169 #define SEEK_OK(offset, whence, result) do {			\
170 	ATF_REQUIRE_MSG(fseeko(fp, (offset), (whence)) == 0,	\
171 	    "fseeko(%s, %s) failed: %s",			\
172 	    __STRING(offset), __STRING(whence), strerror(errno)); \
173 	ATF_REQUIRE_MSG(ftello(fp) == (result),			\
174 	    "fseeko(%s, %s) seeked to %jd rather than %s",	\
175 	    __STRING(offset), __STRING(whence),			\
176 	    (intmax_t)ftello(fp), __STRING(result));		\
177 } while (0)
178 
179 	SEEK_FAIL(-1, SEEK_SET, EINVAL);
180 	SEEK_FAIL(-1, SEEK_CUR, EINVAL);
181 	SEEK_FAIL(-1, SEEK_END, EINVAL);
182 	fwprintf(fp, L"foo");
183 	SEEK_OK(-1, SEEK_CUR, 2);
184 	SEEK_OK(0, SEEK_SET, 0);
185 	SEEK_OK(-1, SEEK_END, 2);
186 	SEEK_OK(OFF_MAX - 1, SEEK_SET, OFF_MAX - 1);
187 	SEEK_FAIL(2, SEEK_CUR, EOVERFLOW);
188 	fclose(fp);
189 }
190 
191 ATF_TP_ADD_TCS(tp)
192 {
193 
194 	ATF_TP_ADD_TC(tp, open_group_test);
195 	ATF_TP_ADD_TC(tp, simple_tests);
196 	ATF_TP_ADD_TC(tp, seek_tests);
197 
198 	return (atf_no_error());
199 }
200