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 char *buf;
41 static size_t len;
42
43 static void
assert_stream(const char * contents)44 assert_stream(const char *contents)
45 {
46 if (strlen(contents) != len)
47 printf("bad length %zd for \"%s\"\n", len, contents);
48 else if (strncmp(buf, contents, strlen(contents)) != 0)
49 printf("bad buffer \"%s\" for \"%s\"\n", buf, contents);
50 }
51
52 ATF_TC_WITHOUT_HEAD(open_group_test);
ATF_TC_BODY(open_group_test,tc)53 ATF_TC_BODY(open_group_test, tc)
54 {
55 FILE *fp;
56 off_t eob;
57
58 fp = open_memstream(&buf, &len);
59 ATF_REQUIRE_MSG(fp != NULL, "open_memstream failed");
60
61 fprintf(fp, "hello my world");
62 fflush(fp);
63 assert_stream("hello my world");
64 eob = ftello(fp);
65 rewind(fp);
66 fprintf(fp, "good-bye");
67 fseeko(fp, eob, SEEK_SET);
68 fclose(fp);
69 assert_stream("good-bye world");
70 free(buf);
71 }
72
73 ATF_TC_WITHOUT_HEAD(simple_tests);
ATF_TC_BODY(simple_tests,tc)74 ATF_TC_BODY(simple_tests, tc)
75 {
76 static const char zerobuf[] =
77 { 'f', 'o', 'o', 0, 0, 0, 0, 'b', 'a', 'r', 0 };
78 char c;
79 FILE *fp;
80
81 fp = open_memstream(&buf, NULL);
82 ATF_REQUIRE_MSG(fp == NULL, "open_memstream did not fail");
83 ATF_REQUIRE_MSG(errno == EINVAL,
84 "open_memstream didn't fail with EINVAL");
85 fp = open_memstream(NULL, &len);
86 ATF_REQUIRE_MSG(fp == NULL, "open_memstream did not fail");
87 ATF_REQUIRE_MSG(errno == EINVAL,
88 "open_memstream didn't fail with EINVAL");
89 fp = open_memstream(&buf, &len);
90 ATF_REQUIRE_MSG(fp != NULL, "open_memstream failed; errno=%d", errno);
91 fflush(fp);
92 assert_stream("");
93 if (fwide(fp, 0) >= 0)
94 printf("stream is not byte-oriented\n");
95
96 fprintf(fp, "fo");
97 fflush(fp);
98 assert_stream("fo");
99 fputc('o', fp);
100 fflush(fp);
101 assert_stream("foo");
102 rewind(fp);
103 fflush(fp);
104 assert_stream("");
105 fseek(fp, 0, SEEK_END);
106 fflush(fp);
107 assert_stream("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 fprintf(fp, "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 fprintf(fp, " in ");
128 fflush(fp);
129 assert_stream("foo in ");
130 fseek(fp, 0, SEEK_END);
131 fflush(fp);
132 assert_stream("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 fprintf(fp, "bar baz");
144 fclose(fp);
145 assert_stream("foo bar baz");
146 free(buf);
147 }
148
149 ATF_TC_WITHOUT_HEAD(seek_tests);
ATF_TC_BODY(seek_tests,tc)150 ATF_TC_BODY(seek_tests, tc)
151 {
152 FILE *fp;
153
154 fp = open_memstream(&buf, &len);
155 ATF_REQUIRE_MSG(fp != NULL, "open_memstream failed: %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 fprintf(fp, "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
ATF_TP_ADD_TCS(tp)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