root/maint/gnulib/tests/test-mkfifoat.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. test_mknodat
  2. do_mkfifoat
  3. do_mknodat
  4. main

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

/* [previous][next][first][last][top][bottom][index][help] */