1 /* fmemopen tests for append and read mode.
2 Copyright (C) 2015-2023 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 <https://www.gnu.org/licenses/>. */
22 #include <sys/types.h>
24 #include <support/xstdio.h>
27 print_buffer (const char *s
, size_t n
)
33 printf ("0x%02X (%c)", s
[i
], s
[i
]);
39 /* This test check append mode initial position (a/a+) based on POSIX definition
40 (BZ#6544 and BZ#13151). */
42 do_test_write_append (const char *mode
)
44 char buf
[32] = "testing buffer";
45 char exp
[32] = "testing bufferXX";
47 FILE *fp
= fmemopen (buf
, sizeof (buf
), mode
);
51 fseek (fp
, 0, SEEK_SET
);
55 if (strcmp (buf
, exp
) != 0)
57 printf ("%s: check failed: %s != %s\n", __FUNCTION__
, buf
, exp
);
64 /* This test check append mode initial position (a/a+) based on POSIX definition
65 (BZ#6544 and BZ#13151) for buffer without null byte end. */
67 do_test_write_append_without_null (const char *mode
)
69 char buf
[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
70 char exp
[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
72 /* If '\0' is not found in buffer, POSIX states that SEEK_SET should be
74 FILE *fp
= fmemopen (buf
, sizeof (buf
) - 2, "a");
78 fseek (fp
, 0, SEEK_SET
);
83 /* POSIX also states that a write operation on the stream shall not advance
84 the current buffer size beyond the size given in fmemopen, so the string
86 if (memcmp (buf
, exp
, sizeof (buf
)) != 0)
88 printf ("%s: check failed: ", __FUNCTION__
);
89 print_buffer (buf
, sizeof (buf
));
91 print_buffer (exp
, sizeof (exp
));
99 /* This test check for initial position and seek value for fmemopen objects
100 opened with append mode. */
102 do_test_read_append (void)
104 char buf
[32] = "testing buffer";
105 size_t buflen
= strlen (buf
);
108 /* POSIX defines for 'a+' the initial position is the first null byte. */
109 FILE *fp
= fmemopen (buf
, sizeof (buf
), "a+");
114 printf ("%s: ftell|SEEK_SET (fp) %li != strlen (%s) %zu\n",
115 __FUNCTION__
, fpos
, buf
, buflen
);
120 fseek (fp
, 0, SEEK_END
);
124 printf ("%s: ftell|SEEK_END (fp) %li != strlen (%s) %zu\n",
125 __FUNCTION__
, fpos
, buf
, buflen
);
131 /* Check if attempting to read past the current size, defined as strlen (buf)
133 fp
= fmemopen (buf
, sizeof (buf
), "a+");
136 printf ("%s: getc(fp) != EOF\n", __FUNCTION__
);
146 /* This test check for fseek (SEEK_END) using negative offsets (BZ#14292). The
147 starting position of descriptor is different base on the opening mode. */
149 do_test_read_seek_neg (const char *mode
, const char *expected
)
151 char buf
[] = "abcdefghijklmnopqrstuvxz0123456789";
153 size_t tmps
= sizeof (tmps
);
156 FILE *fp
= fmemopen (buf
, sizeof (buf
), mode
);
157 fseek (fp
, offset
, SEEK_END
);
158 xfread (tmp
, tmps
, 1, fp
);
160 if (memcmp (tmp
, expected
, tmps
) != 0)
162 printf ("%s: fmemopen(%s) - fseek (fp, %li, SEEK_END):\n",
163 __FUNCTION__
, mode
, offset
);
164 printf (" returned: ");
165 print_buffer (tmp
, tmps
);
167 printf (" expected: ");
168 print_buffer (expected
, tmps
);
179 do_test_read_seek_negative (void)
183 /* 'r' and 'w' modes defines the initial position at the buffer start and
184 seek with SEEK_END shall seek relative to its size give in fmemopen
185 call. The expected tmp result is 0 to 9 *without* the ending null */
186 ret
+= do_test_read_seek_neg ("r", "0123456789");
187 /* 'a+' mode sets the initial position at the first null byte in buffer and
188 SEEK_END shall seek relative to its size as well. The expected result is
189 z012345678, since SEEK_END plus a+ start at '\0', not size. */
190 ret
+= do_test_read_seek_neg ("a+", "z012345678");
196 do_test_write_append_2 (const char *str
)
199 size_t strn
= strlen (str
);
202 FILE *fp
= fmemopen (buf
, sizeof (buf
), "a+");
203 size_t r
= ftell (fp
);
204 size_t e
= strlen (buf
);
207 printf ("%s: ftell returned %zu, expected %zu\n", __FUNCTION__
, r
, e
);
211 if (fseek (fp
, 0, SEEK_SET
) == -1)
213 printf ("%s: fseek returned -1\n", __FUNCTION__
);
218 for (int i
=0; i
<strn
; ++i
)
220 if ((gr
= getc (fp
)) != str
[i
])
222 printf ("%s: getc failed returned %d, expected %d\n", __FUNCTION__
,
227 if ((gr
= getc (fp
)) != EOF
)
229 printf ("%s: getc failed returned %d, expected EOF\n", __FUNCTION__
,
234 if (fseek (fp
, e
+1, SEEK_SET
) == -1)
236 printf ("%s: fseek returned -1\n", __FUNCTION__
);
240 if ((r
= ftell (fp
)) != e
+1)
242 printf ("%s: ftell returned %zu, expected %zu\n", __FUNCTION__
, r
, e
+1);
246 if ((gr
= getc (fp
)) != EOF
)
248 printf ("%s: getc failed returned %i\n", __FUNCTION__
, gr
);
252 /* Check if internal position is not changed with a getc returning EOF. */
253 if ((r
= ftell (fp
)) != e
+1)
255 printf ("%s: ftell returned %zu, expected %zu\n", __FUNCTION__
, r
, e
+1);
259 if (fseek (fp
, 0, SEEK_CUR
) == -1)
261 printf ("%s: fseek returned -1\n", __FUNCTION__
);
265 /* This should be overwritten by fprintf + fflush. */
268 if ((r
= fprintf (fp
, "%d", 101)) != 3)
270 printf ("%s: fprintf returned %zu, expected %d\n", __FUNCTION__
, r
, 3);
276 /* Check if internal position is changed by 3 (strlen of '101'). */
277 if ((r
= ftell (fp
)) != e
+3)
279 printf ("%s: ftell returned %zu, expected %zu\n", __FUNCTION__
, r
, e
+3);
284 sprintf (exp
, "%s%d", str
, 101);
285 if (memcmp (buf
, exp
, strlen (exp
)) != 0)
287 printf ("%s: check failed:", __FUNCTION__
);
288 printf ("\nexpected: ");
289 print_buffer (buf
, sizeof (buf
));
290 printf ("\nbuffer: ");
291 print_buffer (exp
, sizeof (exp
));
306 ret
+= do_test_write_append ("a");
307 ret
+= do_test_write_append_without_null ("a");
308 ret
+= do_test_write_append ("a+");
309 ret
+= do_test_write_append_without_null ("a+");
311 ret
+= do_test_read_append ();
313 ret
+= do_test_read_seek_negative ();
315 /* First test plus addend will fit in the define buffer of size 10. */
316 ret
+= do_test_write_append_2 ("test");
317 /* The second test will also fit, but not the final '\0'. */
318 ret
+= do_test_write_append_2 ("testing");
323 #define TEST_FUNCTION do_test ()
324 #include "../test-skeleton.c"