]>
Commit | Line | Data |
---|---|---|
fdb7d390 AZ |
1 | /* fmemopen tests for append and read mode. |
2 | Copyright (C) 2015 Free Software Foundation, Inc. | |
3 | This file is part of the GNU C Library. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; if not, see | |
17 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #include <assert.h> | |
20 | #include <stdio.h> | |
21 | #include <string.h> | |
22 | #include <sys/types.h> | |
23 | ||
24 | static void | |
25 | print_buffer (const char *s, size_t n) | |
26 | { | |
27 | size_t i; | |
28 | for (i=0; i<n; ++i) | |
29 | printf ("0x%02X (%c), ", s[i], s[i]); | |
30 | } | |
31 | ||
32 | /* This test check append mode initial position (a/a+) based on POSIX defition | |
33 | (BZ#6544 and BZ#13151). */ | |
34 | static int | |
35 | do_test_write_append (const char *mode) | |
36 | { | |
37 | char buf[32] = "testing buffer"; | |
38 | char exp[32] = "testing bufferXX"; | |
39 | ||
40 | FILE *fp = fmemopen (buf, sizeof (buf), mode); | |
41 | ||
42 | fflush (fp); | |
43 | fprintf (fp, "X"); | |
44 | fseek (fp, 0, SEEK_SET); | |
45 | fprintf (fp, "X"); | |
46 | fclose (fp); | |
47 | ||
48 | if (strcmp (buf, exp) != 0) | |
49 | { | |
50 | printf ("%s: check failed: %s != %s\n", __FUNCTION__, buf, exp); | |
51 | return 1; | |
52 | } | |
53 | ||
54 | return 0; | |
55 | } | |
56 | ||
57 | /* This test check append mode initial position (a/a+) based on POSIX defition | |
58 | (BZ#6544 and BZ#13151) for buffer without null byte end. */ | |
59 | static int | |
60 | do_test_write_append_without_null (const char *mode) | |
61 | { | |
62 | char buf[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 }; | |
63 | char exp[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 }; | |
64 | ||
65 | /* If '\0' is not found in buffer, POSIX states that SEEK_SET should be | |
66 | the size argument. */ | |
67 | FILE *fp = fmemopen (buf, sizeof (buf) - 2, "a"); | |
68 | ||
69 | fflush (fp); | |
70 | fputc (0x70, fp); | |
71 | fseek (fp, 0, SEEK_SET); | |
72 | fputc (0x70, fp); | |
73 | fputc (0x70, fp); | |
74 | fclose (fp); | |
75 | ||
76 | /* POSIX also states that a write operation on the stream shall not advance | |
77 | the current buffer size beyond the size given in fmemopen, so the string | |
78 | should be same. */ | |
79 | if (memcmp (buf, exp, sizeof (buf)) != 0) | |
80 | { | |
81 | printf ("%s: check failed: ", __FUNCTION__); | |
82 | print_buffer (buf, sizeof (buf)); | |
83 | printf ("!= "); | |
84 | print_buffer (exp, sizeof (exp)); | |
85 | printf ("\n"); | |
86 | return 1; | |
87 | } | |
88 | ||
89 | return 0; | |
90 | } | |
91 | ||
92 | /* This test check for initial position and feek value for fmemopen objects | |
93 | opened with append mode. */ | |
94 | static int | |
95 | do_test_read_append (void) | |
96 | { | |
97 | char buf[32] = "testing buffer"; | |
98 | size_t buflen = strlen (buf); | |
99 | long fpos; | |
100 | ||
101 | /* POSIX defines for 'a+' the initial position is the first null byte. */ | |
102 | FILE *fp = fmemopen (buf, sizeof (buf), "a+"); | |
103 | ||
104 | fpos = ftell (fp); | |
105 | if (fpos != buflen) | |
106 | { | |
107 | printf ("%s: ftell|SEEK_SET (fp) %li != strlen (%s) %zu\n", | |
108 | __FUNCTION__, fpos, buf, buflen); | |
109 | fclose (fp); | |
110 | return 1; | |
111 | } | |
112 | ||
113 | fseek (fp, 0, SEEK_END); | |
114 | ||
115 | if (fpos != buflen) | |
116 | { | |
117 | printf ("%s: ftell|SEEK_END (fp) %li != strlen (%s) %zu\n", | |
118 | __FUNCTION__, fpos, buf, buflen); | |
119 | fclose (fp); | |
120 | return 1; | |
121 | } | |
122 | fclose (fp); | |
123 | ||
124 | /* Check if attempting to read past the current size, defined as strlen (buf) | |
125 | yield an EOF. */ | |
126 | fp = fmemopen (buf, sizeof (buf), "a+"); | |
127 | if (getc(fp) != EOF) | |
128 | { | |
129 | printf ("%s: getc(fp) != EOF\n", __FUNCTION__); | |
130 | fclose (fp); | |
131 | return -1; | |
132 | } | |
133 | ||
134 | fclose (fp); | |
135 | ||
136 | return 0; | |
137 | } | |
138 | ||
139 | /* This test check for fseek (SEEK_END) using negative offsets (BZ#14292). The | |
140 | starting position of descriptor is different base on the opening mode. */ | |
141 | static int | |
142 | do_test_read_seek_neg (const char *mode, const char *expected) | |
143 | { | |
144 | char buf[] = "abcdefghijklmnopqrstuvxz0123456789"; | |
145 | char tmp[10]; | |
146 | size_t tmps = sizeof (tmps); | |
147 | long offset = -11; | |
148 | ||
149 | FILE *fp = fmemopen (buf, sizeof (buf), mode); | |
150 | fseek (fp, offset, SEEK_END); | |
151 | fread (tmp, tmps, 1, fp); | |
152 | ||
153 | if (memcmp (tmp, expected, tmps) != 0) | |
154 | { | |
155 | printf ("%s: fmemopen(%s) - fseek (fp, %li, SEEK_END):\n", | |
156 | __FUNCTION__, mode, offset); | |
157 | printf (" returned: "); | |
158 | print_buffer (tmp, tmps); | |
159 | printf ("\n"); | |
160 | printf (" expected: "); | |
161 | print_buffer (expected, tmps); | |
162 | printf ("\n"); | |
163 | return 1; | |
164 | } | |
165 | ||
166 | fclose (fp); | |
167 | ||
168 | return 0; | |
169 | } | |
170 | ||
171 | static int | |
172 | do_test_read_seek_negative (void) | |
173 | { | |
174 | int ret = 0; | |
175 | ||
176 | /* 'r' and 'w' modes defines the initial position at the buffer start and | |
177 | seek with SEEK_END shall seek relative to its size give in fmemopen | |
178 | call. The expected tmp result is 0 to 9 *without* the ending null */ | |
179 | ret += do_test_read_seek_neg ("r", "0123456789"); | |
180 | /* 'a+' mode sets the initial position at the first null byte in buffer and | |
181 | SEEK_END shall seek relative to its size as well. The expected result is | |
182 | z012345678, since SEEK_END plus a+ start at '\0', not size. */ | |
183 | ret += do_test_read_seek_neg ("a+", "z012345678"); | |
184 | ||
185 | return ret; | |
186 | } | |
187 | ||
188 | static int | |
189 | do_test (void) | |
190 | { | |
191 | int ret = 0; | |
192 | ||
193 | ret += do_test_write_append ("a"); | |
194 | ret += do_test_write_append_without_null ("a"); | |
195 | ret += do_test_write_append ("a+"); | |
196 | ret += do_test_write_append_without_null ("a+"); | |
197 | ||
198 | ret += do_test_read_append (); | |
199 | ||
200 | ret += do_test_read_seek_negative (); | |
201 | ||
202 | return ret; | |
203 | } | |
204 | ||
205 | #define TEST_FUNCTION do_test () | |
206 | #include "../test-skeleton.c" |