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