Always indirect branch to __libc_start_main via GOT
[glibc.git] / stdio-common / tst-fmemopen3.c
blob054b0697b3fec742f4e741a9b5d793096bed0181
1 /* fmemopen tests for append and read mode.
2 Copyright (C) 2015-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <assert.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <sys/types.h>
24 static void
25 print_buffer (const char *s, size_t n)
27 size_t i;
28 printf ("{");
29 for (i=0; i<n; ++i)
31 printf ("0x%02X (%c)", s[i], s[i]);
32 if (i != n)
33 printf (", ");
37 /* This test check append mode initial position (a/a+) based on POSIX defition
38 (BZ#6544 and BZ#13151). */
39 static int
40 do_test_write_append (const char *mode)
42 char buf[32] = "testing buffer";
43 char exp[32] = "testing bufferXX";
45 FILE *fp = fmemopen (buf, sizeof (buf), mode);
47 fflush (fp);
48 fprintf (fp, "X");
49 fseek (fp, 0, SEEK_SET);
50 fprintf (fp, "X");
51 fclose (fp);
53 if (strcmp (buf, exp) != 0)
55 printf ("%s: check failed: %s != %s\n", __FUNCTION__, buf, exp);
56 return 1;
59 return 0;
62 /* This test check append mode initial position (a/a+) based on POSIX defition
63 (BZ#6544 and BZ#13151) for buffer without null byte end. */
64 static int
65 do_test_write_append_without_null (const char *mode)
67 char buf[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
68 char exp[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
70 /* If '\0' is not found in buffer, POSIX states that SEEK_SET should be
71 the size argument. */
72 FILE *fp = fmemopen (buf, sizeof (buf) - 2, "a");
74 fflush (fp);
75 fputc (0x70, fp);
76 fseek (fp, 0, SEEK_SET);
77 fputc (0x70, fp);
78 fputc (0x70, fp);
79 fclose (fp);
81 /* POSIX also states that a write operation on the stream shall not advance
82 the current buffer size beyond the size given in fmemopen, so the string
83 should be same. */
84 if (memcmp (buf, exp, sizeof (buf)) != 0)
86 printf ("%s: check failed: ", __FUNCTION__);
87 print_buffer (buf, sizeof (buf));
88 printf ("!= ");
89 print_buffer (exp, sizeof (exp));
90 printf ("\n");
91 return 1;
94 return 0;
97 /* This test check for initial position and feek value for fmemopen objects
98 opened with append mode. */
99 static int
100 do_test_read_append (void)
102 char buf[32] = "testing buffer";
103 size_t buflen = strlen (buf);
104 long fpos;
106 /* POSIX defines for 'a+' the initial position is the first null byte. */
107 FILE *fp = fmemopen (buf, sizeof (buf), "a+");
109 fpos = ftell (fp);
110 if (fpos != buflen)
112 printf ("%s: ftell|SEEK_SET (fp) %li != strlen (%s) %zu\n",
113 __FUNCTION__, fpos, buf, buflen);
114 fclose (fp);
115 return 1;
118 fseek (fp, 0, SEEK_END);
120 if (fpos != buflen)
122 printf ("%s: ftell|SEEK_END (fp) %li != strlen (%s) %zu\n",
123 __FUNCTION__, fpos, buf, buflen);
124 fclose (fp);
125 return 1;
127 fclose (fp);
129 /* Check if attempting to read past the current size, defined as strlen (buf)
130 yield an EOF. */
131 fp = fmemopen (buf, sizeof (buf), "a+");
132 if (getc(fp) != EOF)
134 printf ("%s: getc(fp) != EOF\n", __FUNCTION__);
135 fclose (fp);
136 return -1;
139 fclose (fp);
141 return 0;
144 /* This test check for fseek (SEEK_END) using negative offsets (BZ#14292). The
145 starting position of descriptor is different base on the opening mode. */
146 static int
147 do_test_read_seek_neg (const char *mode, const char *expected)
149 char buf[] = "abcdefghijklmnopqrstuvxz0123456789";
150 char tmp[10];
151 size_t tmps = sizeof (tmps);
152 long offset = -11;
154 FILE *fp = fmemopen (buf, sizeof (buf), mode);
155 fseek (fp, offset, SEEK_END);
156 fread (tmp, tmps, 1, fp);
158 if (memcmp (tmp, expected, tmps) != 0)
160 printf ("%s: fmemopen(%s) - fseek (fp, %li, SEEK_END):\n",
161 __FUNCTION__, mode, offset);
162 printf (" returned: ");
163 print_buffer (tmp, tmps);
164 printf ("\n");
165 printf (" expected: ");
166 print_buffer (expected, tmps);
167 printf ("\n");
168 return 1;
171 fclose (fp);
173 return 0;
176 static int
177 do_test_read_seek_negative (void)
179 int ret = 0;
181 /* 'r' and 'w' modes defines the initial position at the buffer start and
182 seek with SEEK_END shall seek relative to its size give in fmemopen
183 call. The expected tmp result is 0 to 9 *without* the ending null */
184 ret += do_test_read_seek_neg ("r", "0123456789");
185 /* 'a+' mode sets the initial position at the first null byte in buffer and
186 SEEK_END shall seek relative to its size as well. The expected result is
187 z012345678, since SEEK_END plus a+ start at '\0', not size. */
188 ret += do_test_read_seek_neg ("a+", "z012345678");
190 return ret;
193 static int
194 do_test_write_append_2 (const char *str)
196 char buf[10];
197 size_t strn = strlen (str);
198 strcpy (buf, str);
200 FILE *fp = fmemopen (buf, sizeof (buf), "a+");
201 size_t r = ftell (fp);
202 size_t e = strlen (buf);
203 if (r != e)
205 printf ("%s: ftell returned %zu, expected %zu\n", __FUNCTION__, r, e);
206 return 1;
209 if (fseek (fp, 0, SEEK_SET) == -1)
211 printf ("%s: fseek returned -1\n", __FUNCTION__);
212 return 1;
215 int gr;
216 for (int i=0; i<strn; ++i)
218 if ((gr = getc (fp)) != str[i])
220 printf ("%s: getc failed returned %d, expected %d\n", __FUNCTION__,
221 gr, str[i]);
222 return 1;
225 if ((gr = getc (fp)) != EOF)
227 printf ("%s: getc failed returned %d, expected EOF\n", __FUNCTION__,
228 gr);
229 return 1;
232 if (fseek (fp, e+1, SEEK_SET) == -1)
234 printf ("%s: fseek returned -1\n", __FUNCTION__);
235 return 1;
238 if ((r = ftell (fp)) != e+1)
240 printf ("%s: ftell returned %zu, expected %zu\n", __FUNCTION__, r, e+1);
241 return 1;
244 if ((gr = getc (fp)) != EOF)
246 printf ("%s: getc failed returned %i\n", __FUNCTION__, gr);
247 return 1;
250 /* Check if internal position is not changed with a getc returning EOF. */
251 if ((r = ftell (fp)) != e+1)
253 printf ("%s: ftell returned %zu, expected %zu\n", __FUNCTION__, r, e+1);
254 return 1;
257 if (fseek (fp, 0, SEEK_CUR) == -1)
259 printf ("%s: fseek returned -1\n", __FUNCTION__);
260 return 1;
263 /* This should be overwritten by fprintf + fflush. */
264 buf[e+2] = 'X';
266 if ((r = fprintf (fp, "%d", 101)) != 3)
268 printf ("%s: fprintf returned %zu, expected %d\n", __FUNCTION__, r, 3);
269 return 1;
272 fflush (fp);
274 /* Check if internal position is changed by 3 (strlen of '101'). */
275 if ((r = ftell (fp)) != e+3)
277 printf ("%s: ftell returned %zu, expected %zu\n", __FUNCTION__, r, e+3);
278 return 1;
281 char exp[20];
282 sprintf (exp, "%s%d", str, 101);
283 if (memcmp (buf, exp, strlen (exp)) != 0)
285 printf ("%s: check failed:", __FUNCTION__);
286 printf ("\nexpected: ");
287 print_buffer (buf, sizeof (buf));
288 printf ("\nbuffer: ");
289 print_buffer (exp, sizeof (exp));
290 printf ("\n");
291 return 1;
294 fclose(fp);
296 return 0;
299 static int
300 do_test (void)
302 int ret = 0;
304 ret += do_test_write_append ("a");
305 ret += do_test_write_append_without_null ("a");
306 ret += do_test_write_append ("a+");
307 ret += do_test_write_append_without_null ("a+");
309 ret += do_test_read_append ();
311 ret += do_test_read_seek_negative ();
313 /* First test plus addend will fit in the define buffer of size 10. */
314 ret += do_test_write_append_2 ("test");
315 /* The second test will also fit, but not the final '\0'. */
316 ret += do_test_write_append_2 ("testing");
318 return ret;
321 #define TEST_FUNCTION do_test ()
322 #include "../test-skeleton.c"