1 /* fmemopen tests for append and read mode.
2 Copyright (C) 2015-2017 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/>. */
22 #include <sys/types.h>
25 print_buffer (const char *s
, size_t n
)
31 printf ("0x%02X (%c)", s
[i
], s
[i
]);
37 /* This test check append mode initial position (a/a+) based on POSIX defition
38 (BZ#6544 and BZ#13151). */
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
);
49 fseek (fp
, 0, SEEK_SET
);
53 if (strcmp (buf
, exp
) != 0)
55 printf ("%s: check failed: %s != %s\n", __FUNCTION__
, buf
, exp
);
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. */
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
72 FILE *fp
= fmemopen (buf
, sizeof (buf
) - 2, "a");
76 fseek (fp
, 0, SEEK_SET
);
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
84 if (memcmp (buf
, exp
, sizeof (buf
)) != 0)
86 printf ("%s: check failed: ", __FUNCTION__
);
87 print_buffer (buf
, sizeof (buf
));
89 print_buffer (exp
, sizeof (exp
));
97 /* This test check for initial position and feek value for fmemopen objects
98 opened with append mode. */
100 do_test_read_append (void)
102 char buf
[32] = "testing buffer";
103 size_t buflen
= strlen (buf
);
106 /* POSIX defines for 'a+' the initial position is the first null byte. */
107 FILE *fp
= fmemopen (buf
, sizeof (buf
), "a+");
112 printf ("%s: ftell|SEEK_SET (fp) %li != strlen (%s) %zu\n",
113 __FUNCTION__
, fpos
, buf
, buflen
);
118 fseek (fp
, 0, SEEK_END
);
122 printf ("%s: ftell|SEEK_END (fp) %li != strlen (%s) %zu\n",
123 __FUNCTION__
, fpos
, buf
, buflen
);
129 /* Check if attempting to read past the current size, defined as strlen (buf)
131 fp
= fmemopen (buf
, sizeof (buf
), "a+");
134 printf ("%s: getc(fp) != EOF\n", __FUNCTION__
);
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. */
147 do_test_read_seek_neg (const char *mode
, const char *expected
)
149 char buf
[] = "abcdefghijklmnopqrstuvxz0123456789";
151 size_t tmps
= sizeof (tmps
);
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
);
165 printf (" expected: ");
166 print_buffer (expected
, tmps
);
177 do_test_read_seek_negative (void)
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");
194 do_test_write_append_2 (const char *str
)
197 size_t strn
= strlen (str
);
200 FILE *fp
= fmemopen (buf
, sizeof (buf
), "a+");
201 size_t r
= ftell (fp
);
202 size_t e
= strlen (buf
);
205 printf ("%s: ftell returned %zu, expected %zu\n", __FUNCTION__
, r
, e
);
209 if (fseek (fp
, 0, SEEK_SET
) == -1)
211 printf ("%s: fseek returned -1\n", __FUNCTION__
);
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__
,
225 if ((gr
= getc (fp
)) != EOF
)
227 printf ("%s: getc failed returned %d, expected EOF\n", __FUNCTION__
,
232 if (fseek (fp
, e
+1, SEEK_SET
) == -1)
234 printf ("%s: fseek returned -1\n", __FUNCTION__
);
238 if ((r
= ftell (fp
)) != e
+1)
240 printf ("%s: ftell returned %zu, expected %zu\n", __FUNCTION__
, r
, e
+1);
244 if ((gr
= getc (fp
)) != EOF
)
246 printf ("%s: getc failed returned %i\n", __FUNCTION__
, gr
);
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);
257 if (fseek (fp
, 0, SEEK_CUR
) == -1)
259 printf ("%s: fseek returned -1\n", __FUNCTION__
);
263 /* This should be overwritten by fprintf + fflush. */
266 if ((r
= fprintf (fp
, "%d", 101)) != 3)
268 printf ("%s: fprintf returned %zu, expected %d\n", __FUNCTION__
, r
, 3);
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);
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
));
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");
321 #define TEST_FUNCTION do_test ()
322 #include "../test-skeleton.c"