LCOV - code coverage report
Current view: top level - gltests - test-dup2.c (source / functions) Hit Total Coverage
Test: GNU SASL Lines: 66 66 100.0 %
Date: 2010-09-27 Functions: 4 4 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 45 90 50.0 %

           Branch data     Line data    Source code
       1                 :            : /* Test duplicating file descriptors.
       2                 :            :    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
       3                 :            : 
       4                 :            :    This program is free software: you can redistribute it and/or modify
       5                 :            :    it under the terms of the GNU General Public License as published by
       6                 :            :    the Free Software Foundation; either version 3 of the License, or
       7                 :            :    (at your option) any later version.
       8                 :            : 
       9                 :            :    This program is distributed in the hope that it will be useful,
      10                 :            :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      11                 :            :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12                 :            :    GNU General Public License for more details.
      13                 :            : 
      14                 :            :    You should have received a copy of the GNU General Public License
      15                 :            :    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
      16                 :            : 
      17                 :            : /* Written by Eric Blake <ebb9@byu.net>, 2009.  */
      18                 :            : 
      19                 :            : #include <config.h>
      20                 :            : 
      21                 :            : #include <unistd.h>
      22                 :            : 
      23                 :            : #include "signature.h"
      24                 :            : SIGNATURE_CHECK (dup2, int, (int, int));
      25                 :            : 
      26                 :            : #include <errno.h>
      27                 :            : #include <fcntl.h>
      28                 :            : 
      29                 :            : #include "binary-io.h"
      30                 :            : 
      31                 :            : #if GNULIB_TEST_CLOEXEC
      32                 :            : # include "cloexec.h"
      33                 :            : #endif
      34                 :            : 
      35                 :            : #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
      36                 :            : /* Get declarations of the Win32 API functions.  */
      37                 :            : # define WIN32_LEAN_AND_MEAN
      38                 :            : # include <windows.h>
      39                 :            : #endif
      40                 :            : 
      41                 :            : #include "macros.h"
      42                 :            : 
      43                 :            : /* Return non-zero if FD is open.  */
      44                 :            : static int
      45                 :         10 : is_open (int fd)
      46                 :            : {
      47                 :            : #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
      48                 :            :   /* On Win32, the initial state of unassigned standard file
      49                 :            :      descriptors is that they are open but point to an
      50                 :            :      INVALID_HANDLE_VALUE, and there is no fcntl.  */
      51                 :            :   return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
      52                 :            : #else
      53                 :            : # ifndef F_GETFL
      54                 :            : #  error Please port fcntl to your platform
      55                 :            : # endif
      56                 :         10 :   return 0 <= fcntl (fd, F_GETFL);
      57                 :            : #endif
      58                 :            : }
      59                 :            : 
      60                 :            : #if GNULIB_TEST_CLOEXEC
      61                 :            : /* Return non-zero if FD is open and inheritable across exec/spawn.  */
      62                 :            : static int
      63                 :            : is_inheritable (int fd)
      64                 :            : {
      65                 :            : # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
      66                 :            :   /* On Win32, the initial state of unassigned standard file
      67                 :            :      descriptors is that they are open but point to an
      68                 :            :      INVALID_HANDLE_VALUE, and there is no fcntl.  */
      69                 :            :   HANDLE h = (HANDLE) _get_osfhandle (fd);
      70                 :            :   DWORD flags;
      71                 :            :   if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
      72                 :            :     return 0;
      73                 :            :   return (flags & HANDLE_FLAG_INHERIT) != 0;
      74                 :            : # else
      75                 :            : #  ifndef F_GETFD
      76                 :            : #   error Please port fcntl to your platform
      77                 :            : #  endif
      78                 :            :   int i = fcntl (fd, F_GETFD);
      79                 :            :   return 0 <= i && (i & FD_CLOEXEC) == 0;
      80                 :            : # endif
      81                 :            : }
      82                 :            : #endif /* GNULIB_TEST_CLOEXEC */
      83                 :            : 
      84                 :            : #if !O_BINARY
      85                 :            : # define setmode(f,m) zero ()
      86                 :         10 : static int zero (void) { return 0; }
      87                 :            : #endif
      88                 :            : 
      89                 :            : /* Return non-zero if FD is open in the given MODE, which is either
      90                 :            :    O_TEXT or O_BINARY.  */
      91                 :            : static int
      92                 :          4 : is_mode (int fd, int mode)
      93                 :            : {
      94                 :          4 :   int value = setmode (fd, O_BINARY);
      95                 :          4 :   setmode (fd, value);
      96                 :          4 :   return mode == value;
      97                 :            : }
      98                 :            : 
      99                 :            : int
     100                 :          1 : main (void)
     101                 :            : {
     102                 :          1 :   const char *file = "test-dup2.tmp";
     103                 :            :   char buffer[1];
     104                 :          1 :   int fd = open (file, O_CREAT | O_TRUNC | O_RDWR, 0600);
     105                 :            : 
     106                 :            :   /* Assume std descriptors were provided by invoker.  */
     107         [ -  + ]:          1 :   ASSERT (STDERR_FILENO < fd);
     108         [ -  + ]:          1 :   ASSERT (is_open (fd));
     109                 :            :   /* Ignore any other fd's leaked into this process.  */
     110                 :          1 :   close (fd + 1);
     111                 :          1 :   close (fd + 2);
     112         [ -  + ]:          1 :   ASSERT (!is_open (fd + 1));
     113         [ -  + ]:          1 :   ASSERT (!is_open (fd + 2));
     114                 :            : 
     115                 :            :   /* Assigning to self must be a no-op.  */
     116         [ -  + ]:          1 :   ASSERT (dup2 (fd, fd) == fd);
     117         [ -  + ]:          1 :   ASSERT (is_open (fd));
     118                 :            : 
     119                 :            :   /* The source must be valid.  */
     120                 :          1 :   errno = 0;
     121         [ -  + ]:          1 :   ASSERT (dup2 (-1, fd) == -1);
     122         [ -  + ]:          1 :   ASSERT (errno == EBADF);
     123                 :          1 :   errno = 0;
     124         [ -  + ]:          1 :   ASSERT (dup2 (AT_FDCWD, fd) == -1);
     125         [ -  + ]:          1 :   ASSERT (errno == EBADF);
     126         [ -  + ]:          1 :   ASSERT (is_open (fd));
     127                 :            : 
     128                 :            :   /* If the source is not open, then the destination is unaffected.  */
     129                 :          1 :   errno = 0;
     130         [ -  + ]:          1 :   ASSERT (dup2 (fd + 1, fd + 1) == -1);
     131         [ -  + ]:          1 :   ASSERT (errno == EBADF);
     132         [ -  + ]:          1 :   ASSERT (!is_open (fd + 1));
     133                 :          1 :   errno = 0;
     134         [ -  + ]:          1 :   ASSERT (dup2 (fd + 1, fd) == -1);
     135         [ -  + ]:          1 :   ASSERT (errno == EBADF);
     136         [ -  + ]:          1 :   ASSERT (is_open (fd));
     137                 :            : 
     138                 :            :   /* The destination must be valid.  */
     139                 :          1 :   errno = 0;
     140         [ -  + ]:          1 :   ASSERT (dup2 (fd, -2) == -1);
     141         [ -  + ]:          1 :   ASSERT (errno == EBADF);
     142                 :          1 :   errno = 0;
     143         [ -  + ]:          1 :   ASSERT (dup2 (fd, 10000000) == -1);
     144         [ -  + ]:          1 :   ASSERT (errno == EBADF);
     145                 :            : 
     146                 :            :   /* Using dup2 can skip fds.  */
     147         [ -  + ]:          1 :   ASSERT (dup2 (fd, fd + 2) == fd + 2);
     148         [ -  + ]:          1 :   ASSERT (is_open (fd));
     149         [ -  + ]:          1 :   ASSERT (!is_open (fd + 1));
     150         [ -  + ]:          1 :   ASSERT (is_open (fd + 2));
     151                 :            : 
     152                 :            :   /* Verify that dup2 closes the previous occupant of a fd.  */
     153         [ -  + ]:          1 :   ASSERT (open ("/dev/null", O_WRONLY, 0600) == fd + 1);
     154         [ -  + ]:          1 :   ASSERT (dup2 (fd + 1, fd) == fd);
     155         [ -  + ]:          1 :   ASSERT (close (fd + 1) == 0);
     156         [ -  + ]:          1 :   ASSERT (write (fd, "1", 1) == 1);
     157         [ -  + ]:          1 :   ASSERT (dup2 (fd + 2, fd) == fd);
     158         [ -  + ]:          1 :   ASSERT (lseek (fd, 0, SEEK_END) == 0);
     159         [ -  + ]:          1 :   ASSERT (write (fd + 2, "2", 1) == 1);
     160         [ -  + ]:          1 :   ASSERT (lseek (fd, 0, SEEK_SET) == 0);
     161         [ -  + ]:          1 :   ASSERT (read (fd, buffer, 1) == 1);
     162         [ -  + ]:          1 :   ASSERT (*buffer == '2');
     163                 :            : 
     164                 :            : #if GNULIB_TEST_CLOEXEC
     165                 :            :   /* Any new fd created by dup2 must not be cloexec.  */
     166                 :            :   ASSERT (close (fd + 2) == 0);
     167                 :            :   ASSERT (dup_cloexec (fd) == fd + 1);
     168                 :            :   ASSERT (!is_inheritable (fd + 1));
     169                 :            :   ASSERT (dup2 (fd + 1, fd + 1) == fd + 1);
     170                 :            :   ASSERT (!is_inheritable (fd + 1));
     171                 :            :   ASSERT (dup2 (fd + 1, fd + 2) == fd + 2);
     172                 :            :   ASSERT (is_inheritable (fd + 2));
     173                 :            : #endif
     174                 :            : 
     175                 :            :   /* On systems that distinguish between text and binary mode, dup2
     176                 :            :      reuses the mode of the source.  */
     177                 :          1 :   setmode (fd, O_BINARY);
     178         [ -  + ]:          1 :   ASSERT (is_mode (fd, O_BINARY));
     179         [ -  + ]:          1 :   ASSERT (dup2 (fd, fd + 1) == fd + 1);
     180         [ -  + ]:          1 :   ASSERT (is_mode (fd + 1, O_BINARY));
     181                 :          1 :   setmode (fd, O_TEXT);
     182         [ -  + ]:          1 :   ASSERT (is_mode (fd, O_TEXT));
     183         [ -  + ]:          1 :   ASSERT (dup2 (fd, fd + 1) == fd + 1);
     184         [ -  + ]:          1 :   ASSERT (is_mode (fd + 1, O_TEXT));
     185                 :            : 
     186                 :            :   /* Clean up.  */
     187         [ -  + ]:          1 :   ASSERT (close (fd + 2) == 0);
     188         [ -  + ]:          1 :   ASSERT (close (fd + 1) == 0);
     189         [ -  + ]:          1 :   ASSERT (close (fd) == 0);
     190         [ -  + ]:          1 :   ASSERT (unlink (file) == 0);
     191                 :            : 
     192                 :          1 :   return 0;
     193                 :            : }

Generated by: LCOV version 1.8