Tests for module 'fmodl-ieee'.
[gnulib.git] / tests / test-fcntl.c
index a181da3..78688ab 100644 (file)
@@ -1,5 +1,5 @@
 /* Test of fcntl(2).
-   Copyright (C) 2009, 2010 Free Software Foundation, Inc.
+   Copyright (C) 2009-2012 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -31,9 +31,11 @@ SIGNATURE_CHECK (fcntl, int, (int, int, ...));
 #include <unistd.h>
 
 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
-/* Get declarations of the Win32 API functions.  */
+/* Get declarations of the native Windows API functions.  */
 # define WIN32_LEAN_AND_MEAN
 # include <windows.h>
+/* Get _get_osfhandle.  */
+# include "msvc-nothrow.h"
 #endif
 
 #include "binary-io.h"
@@ -49,7 +51,7 @@ static bool
 is_open (int fd)
 {
 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
-  /* On Win32, the initial state of unassigned standard file
+  /* On native Windows, the initial state of unassigned standard file
      descriptors is that they are open but point to an
      INVALID_HANDLE_VALUE, and there is no fcntl.  */
   return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
@@ -66,7 +68,7 @@ static bool
 is_inheritable (int fd)
 {
 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
-  /* On Win32, the initial state of unassigned standard file
+  /* On native Windows, the initial state of unassigned standard file
      descriptors is that they are open but point to an
      INVALID_HANDLE_VALUE, and there is no fcntl.  */
   HANDLE h = (HANDLE) _get_osfhandle (fd);
@@ -312,7 +314,7 @@ main (void)
   ASSERT (is_mode (fd + 2, O_TEXT));
   ASSERT (close (fd + 2) == 0);
 
-  /* Test F_GETFD.  */
+  /* Test F_GETFD on invalid file descriptors.  */
   errno = 0;
   ASSERT (fcntl (-1, F_GETFD) == -1);
   ASSERT (errno == EBADF);
@@ -322,6 +324,8 @@ main (void)
   errno = 0;
   ASSERT (fcntl (10000000, F_GETFD) == -1);
   ASSERT (errno == EBADF);
+
+  /* Test F_GETFD, the FD_CLOEXEC bit.  */
   {
     int result = fcntl (fd, F_GETFD);
     ASSERT (0 <= result);
@@ -333,6 +337,71 @@ main (void)
     ASSERT (close (fd + 1) == 0);
   }
 
+#ifdef F_SETFD
+  /* Test F_SETFD on invalid file descriptors.  */
+  errno = 0;
+  ASSERT (fcntl (-1, F_SETFD, 0) == -1);
+  ASSERT (errno == EBADF);
+  errno = 0;
+  ASSERT (fcntl (fd + 1, F_SETFD, 0) == -1);
+  ASSERT (errno == EBADF);
+  errno = 0;
+  ASSERT (fcntl (10000000, F_SETFD, 0) == -1);
+  ASSERT (errno == EBADF);
+#endif
+
+#ifdef F_GETFL
+  /* Test F_GETFL on invalid file descriptors.  */
+  errno = 0;
+  ASSERT (fcntl (-1, F_GETFL) == -1);
+  ASSERT (errno == EBADF);
+  errno = 0;
+  ASSERT (fcntl (fd + 1, F_GETFL) == -1);
+  ASSERT (errno == EBADF);
+  errno = 0;
+  ASSERT (fcntl (10000000, F_GETFL) == -1);
+  ASSERT (errno == EBADF);
+#endif
+
+#ifdef F_SETFL
+  /* Test F_SETFL on invalid file descriptors.  */
+  errno = 0;
+  ASSERT (fcntl (-1, F_SETFL, 0) == -1);
+  ASSERT (errno == EBADF);
+  errno = 0;
+  ASSERT (fcntl (fd + 1, F_SETFL, 0) == -1);
+  ASSERT (errno == EBADF);
+  errno = 0;
+  ASSERT (fcntl (10000000, F_SETFL, 0) == -1);
+  ASSERT (errno == EBADF);
+#endif
+
+#ifdef F_GETOWN
+  /* Test F_GETOWN on invalid file descriptors.  */
+  errno = 0;
+  ASSERT (fcntl (-1, F_GETOWN) == -1);
+  ASSERT (errno == EBADF);
+  errno = 0;
+  ASSERT (fcntl (fd + 1, F_GETOWN) == -1);
+  ASSERT (errno == EBADF);
+  errno = 0;
+  ASSERT (fcntl (10000000, F_GETOWN) == -1);
+  ASSERT (errno == EBADF);
+#endif
+
+#ifdef F_SETOWN
+  /* Test F_SETFL on invalid file descriptors.  */
+  errno = 0;
+  ASSERT (fcntl (-1, F_SETOWN, 0) == -1);
+  ASSERT (errno == EBADF);
+  errno = 0;
+  ASSERT (fcntl (fd + 1, F_SETOWN, 0) == -1);
+  ASSERT (errno == EBADF);
+  errno = 0;
+  ASSERT (fcntl (10000000, F_SETOWN, 0) == -1);
+  ASSERT (errno == EBADF);
+#endif
+
   /* Cleanup.  */
   ASSERT (close (fd) == 0);
   ASSERT (unlink (file) == 0);