(root)/
coreutils-9.4/
gnulib-tests/
test-mkfifoat.c
       1  /* Tests of mkfifoat and mknodat.
       2     Copyright (C) 2009-2023 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 <https://www.gnu.org/licenses/>.  */
      16  
      17  /* Written by Eric Blake <ebb9@byu.net>, 2009.  */
      18  
      19  #include <config.h>
      20  
      21  #include <sys/stat.h>
      22  
      23  #include "signature.h"
      24  SIGNATURE_CHECK (mkfifoat, int, (int, char const *, mode_t));
      25  SIGNATURE_CHECK (mknodat, int, (int, char const *, mode_t, dev_t));
      26  
      27  #include <fcntl.h>
      28  #include <errno.h>
      29  #include <stdio.h>
      30  #include <stdlib.h>
      31  #include <string.h>
      32  #include <unistd.h>
      33  
      34  #include "ignore-value.h"
      35  #include "macros.h"
      36  
      37  #define BASE "test-mkfifoat.t"
      38  
      39  #include "test-mkfifo.h"
      40  
      41  typedef int (*test_func) (int, char const *, mode_t);
      42  
      43  static int dfd = AT_FDCWD;
      44  
      45  /* Wrapper to test mknodat like mkfifoat.  */
      46  static int
      47  test_mknodat (int fd, char const *name, mode_t mode)
      48  {
      49    /* This is the only portable use of mknodat, per POSIX.  */
      50    return mknodat (fd, name, mode | S_IFIFO, 0);
      51  }
      52  
      53  /* Wrapper to test mkfifoat like mkfifo.  */
      54  static int
      55  do_mkfifoat (char const *name, mode_t mode)
      56  {
      57    return mkfifoat (dfd, name, mode);
      58  }
      59  
      60  /* Wrapper to test mknodat like mkfifo.  */
      61  static int
      62  do_mknodat (char const *name, mode_t mode)
      63  {
      64    return mknodat (dfd, name, mode | S_IFIFO, 0);
      65  }
      66  
      67  int
      68  main (void)
      69  {
      70    int i;
      71    test_func funcs[2] = { mkfifoat, test_mknodat };
      72    int result;
      73  
      74    /* Remove any leftovers from a previous partial run.  */
      75    ignore_value (system ("rm -rf " BASE "*"));
      76  
      77    /* Basic tests.  */
      78    result = test_mkfifo (do_mkfifoat, true);
      79    ASSERT (test_mkfifo (do_mknodat, false) == result);
      80    dfd = open (".", O_RDONLY);
      81    ASSERT (0 <= dfd);
      82    ASSERT (test_mkfifo (do_mkfifoat, false) == result);
      83    ASSERT (test_mkfifo (do_mknodat, false) == result);
      84  
      85    /* Test directory-relative handling of both functions.  */
      86    for (i = 0; i < 2; i++)
      87      {
      88        struct stat st;
      89        test_func func = funcs[i];
      90  
      91        /* Test behaviour for invalid file descriptors.  */
      92        {
      93          errno = 0;
      94          ASSERT (func (-1, "foo", 0600) == -1);
      95          ASSERT (errno == EBADF
      96                  || errno == ENOSYS /* seen on mingw */
      97                 );
      98        }
      99        {
     100          close (99);
     101          errno = 0;
     102          ASSERT (func (99, "foo", 0600) == -1);
     103          ASSERT (errno == EBADF
     104                  || errno == ENOSYS /* seen on mingw */
     105                 );
     106        }
     107  
     108        /* Create fifo while cwd is '.', then stat it from '..'.  */
     109        if (func (AT_FDCWD, BASE "fifo", 0600) != 0)
     110          ASSERT (errno == ENOSYS); /* seen on native Windows */
     111        else
     112          {
     113            errno = 0;
     114            ASSERT (func (dfd, BASE "fifo", 0600) == -1);
     115            ASSERT (errno == EEXIST);
     116            ASSERT (chdir ("..") == 0);
     117            errno = 0;
     118            ASSERT (fstatat (AT_FDCWD, BASE "fifo", &st, 0) == -1);
     119            ASSERT (errno == ENOENT);
     120            memset (&st, 0, sizeof st);
     121            ASSERT (fstatat (dfd, BASE "fifo", &st, 0) == 0);
     122            ASSERT (S_ISFIFO (st.st_mode));
     123            ASSERT (unlinkat (dfd, BASE "fifo", 0) == 0);
     124          }
     125  
     126        /* Create fifo while cwd is '..', then stat it from '.'.  */
     127        if (func (dfd, BASE "fifo", 0600) != 0)
     128          ASSERT (errno == ENOSYS); /* seen on native Windows */
     129        else
     130          {
     131            ASSERT (fchdir (dfd) == 0);
     132            errno = 0;
     133            ASSERT (func (AT_FDCWD, BASE "fifo", 0600) == -1);
     134            ASSERT (errno == EEXIST);
     135            memset (&st, 0, sizeof st);
     136            ASSERT (fstatat (AT_FDCWD, BASE "fifo", &st, AT_SYMLINK_NOFOLLOW)
     137                    == 0);
     138            ASSERT (S_ISFIFO (st.st_mode));
     139            memset (&st, 0, sizeof st);
     140            ASSERT (fstatat (dfd, BASE "fifo", &st, AT_SYMLINK_NOFOLLOW) == 0);
     141            ASSERT (S_ISFIFO (st.st_mode));
     142            ASSERT (unlink (BASE "fifo") == 0);
     143          }
     144      }
     145  
     146    ASSERT (close (dfd) == 0);
     147  
     148    return 0;
     149  }