summaryrefslogtreecommitdiff
path: root/tests/test-fflush.c
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2011-09-20 23:10:44 +0200
committerBruno Haible <bruno@clisp.org>2011-09-20 23:28:04 +0200
commit69d591ce1cc5e595806dfd63283d9b54eebb8433 (patch)
treef8df4f0479ed6498cb93860113245c427627456f /tests/test-fflush.c
parent089311a6ed4a67fff8ceceb14964268a11fc8a24 (diff)
downloadgnulib-69d591ce1cc5e595806dfd63283d9b54eebb8433.tar.gz
fflush tests: EBADF tests.
* tests/test-fflush.c: Include errno.h, macros.h. (main): Add tests for EBADF.
Diffstat (limited to 'tests/test-fflush.c')
-rw-r--r--tests/test-fflush.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/test-fflush.c b/tests/test-fflush.c
index a6c0e18abb..7c7c474a0a 100644
--- a/tests/test-fflush.c
+++ b/tests/test-fflush.c
@@ -26,8 +26,11 @@
#include "signature.h"
SIGNATURE_CHECK (fflush, int, (FILE *));
+#include <errno.h>
#include <unistd.h>
+#include "macros.h"
+
int
main (void)
{
@@ -46,6 +49,7 @@ main (void)
/* Test fflush. */
f = fopen ("test-fflush.txt", "r");
+ ASSERT (f != NULL);
fd = fileno (f);
if (!f || 0 > fd || fread (buffer, 1, 5, f) != 5)
{
@@ -141,6 +145,45 @@ main (void)
return 1;
}
fclose (f);
+
+ /* Test that fflush() sets errno if someone else closes the stream
+ fd behind the back of stdio. */
+ {
+ FILE *fp = fopen ("test-fflush.txt", "w");
+ ASSERT (fp != NULL);
+ fputc ('x', fp);
+ ASSERT (close (fileno (fp)) == 0);
+ errno = 0;
+ ASSERT (fflush (fp) == EOF);
+ ASSERT (errno == EBADF);
+ fclose (fp);
+ }
+
+ /* Test that fflush() sets errno if the stream was constructed with
+ an invalid file descriptor. */
+ {
+ FILE *fp = fdopen (-1, "w");
+ if (fp != NULL)
+ {
+ fputc ('x', fp);
+ errno = 0;
+ ASSERT (fflush (fp) == EOF);
+ ASSERT (errno == EBADF);
+ }
+ }
+ {
+ FILE *fp = fdopen (99, "w");
+ if (fp != NULL)
+ {
+ fputc ('x', fp);
+ errno = 0;
+ ASSERT (fflush (fp) == EOF);
+ ASSERT (errno == EBADF);
+ }
+ }
+
+ /* Clean up. */
unlink ("test-fflush.txt");
+
return 0;
}