changeset 15720:3a20a51c1323

freopen tests: EBADF tests. * tests/test-freopen.c: Include errno.h, unistd.h. (main): Add tests for EBADF, commented out for the moment.
author Bruno Haible <bruno@clisp.org>
date Tue, 20 Sep 2011 23:13:46 +0200
parents 5adced00bbec
children 95514a6910a3
files ChangeLog tests/test-freopen.c
diffstat 2 files changed, 50 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2011-09-20  Bruno Haible  <bruno@clisp.org>
 
+	freopen tests: EBADF tests.
+	* tests/test-freopen.c: Include errno.h, unistd.h.
+	(main): Add tests for EBADF, commented out for the moment.
+
 	fclose tests: EBADF tests.
 	* tests/test-fclose.c (main): Add tests for EBADF.
 
--- a/tests/test-freopen.c
+++ b/tests/test-freopen.c
@@ -23,12 +23,58 @@
 #include "signature.h"
 SIGNATURE_CHECK (freopen, FILE *, (char const *, char const *, FILE *));
 
+#include <errno.h>
+#include <unistd.h>
+
 #include "macros.h"
 
 int
 main ()
 {
+  const char *filename = "test-freopen.txt";
+
   ASSERT (freopen ("/dev/null", "r", stdin) != NULL);
 
+#if 0 /* freopen (NULL, ...) is unsupported on most platforms.  */
+  /* Test that freopen() sets errno if someone else closes the stream
+     fd behind the back of stdio.  */
+  {
+    FILE *fp = fopen (filename, "w+");
+    ASSERT (fp != NULL);
+    ASSERT (close (fileno (fp)) == 0);
+    errno = 0;
+    ASSERT (freopen (NULL, "r", fp) == NULL);
+    perror("freopen");
+    ASSERT (errno == EBADF);
+    fclose (fp);
+  }
+
+  /* Test that freopen() sets errno if the stream was constructed with
+     an invalid file descriptor.  */
+  {
+    FILE *fp = fdopen (-1, "w+");
+    if (fp != NULL)
+      {
+        errno = 0;
+        ASSERT (freopen (NULL, "r", fp) == NULL);
+        ASSERT (errno == EBADF);
+        fclose (fp);
+      }
+  }
+  {
+    FILE *fp = fdopen (99, "w+");
+    if (fp != NULL)
+      {
+        errno = 0;
+        ASSERT (freopen (NULL, "r", fp) == NULL);
+        ASSERT (errno == EBADF);
+        fclose (fp);
+      }
+  }
+#endif
+
+  /* Clean up.  */
+  unlink (filename);
+
   return 0;
 }