Blame gl/tests/test-open.h

Packit Service 4684c1
/* Test of opening a file descriptor.
Packit Service 4684c1
   Copyright (C) 2007-2020 Free Software Foundation, Inc.
Packit Service 4684c1
Packit Service 4684c1
   This program is free software: you can redistribute it and/or modify
Packit Service 4684c1
   it under the terms of the GNU General Public License as published by
Packit Service 4684c1
   the Free Software Foundation; either version 3 of the License, or
Packit Service 4684c1
   (at your option) any later version.
Packit Service 4684c1
Packit Service 4684c1
   This program is distributed in the hope that it will be useful,
Packit Service 4684c1
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 4684c1
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 4684c1
   GNU General Public License for more details.
Packit Service 4684c1
Packit Service 4684c1
   You should have received a copy of the GNU General Public License
Packit Service 4684c1
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit Service 4684c1
Packit Service 4684c1
/* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
Packit Service 4684c1
Packit Service 4684c1
/* Make test_open always inline if we're using Fortify, which defines
Packit Service 4684c1
   __always_inline to do that.  Do nothing otherwise.  This works
Packit Service 4684c1
   around a glibc bug whereby 'open' cannot be used as a function
Packit Service 4684c1
   pointer when _FORTIFY_SOURCE is positive.  */
Packit Service 4684c1
Packit Service 4684c1
#if __GLIBC__ && defined __always_inline
Packit Service 4684c1
# define ALWAYS_INLINE __always_inline
Packit Service 4684c1
#else
Packit Service 4684c1
# define ALWAYS_INLINE
Packit Service 4684c1
#endif
Packit Service 4684c1
Packit Service 4684c1
/* This file is designed to test both open(n,buf[,mode]) and
Packit Service 4684c1
   openat(AT_FDCWD,n,buf[,mode]).  FUNC is the function to test.
Packit Service 4684c1
   Assumes that BASE and ASSERT are already defined, and that
Packit Service 4684c1
   appropriate headers are already included.  If PRINT, warn before
Packit Service 4684c1
   skipping symlink tests with status 77.  */
Packit Service 4684c1
Packit Service 4684c1
static ALWAYS_INLINE int
Packit Service 4684c1
test_open (int (*func) (char const *, int, ...), bool print)
Packit Service 4684c1
{
Packit Service 4684c1
  int fd;
Packit Service 4684c1
Packit Service 4684c1
  /* Remove anything from prior partial run.  */
Packit Service 4684c1
  unlink (BASE "file");
Packit Service 4684c1
  unlink (BASE "e.exe");
Packit Service 4684c1
  unlink (BASE "link");
Packit Service 4684c1
Packit Service 4684c1
  /* Cannot create directory.  */
Packit Service 4684c1
  errno = 0;
Packit Service 4684c1
  ASSERT (func ("nonexist.ent/", O_CREAT | O_RDONLY, 0600) == -1);
Packit Service 4684c1
  ASSERT (errno == ENOTDIR || errno == EISDIR || errno == ENOENT
Packit Service 4684c1
          || errno == EINVAL);
Packit Service 4684c1
Packit Service 4684c1
  /* Create a regular file.  */
Packit Service 4684c1
  fd = func (BASE "file", O_CREAT | O_RDONLY, 0600);
Packit Service 4684c1
  ASSERT (0 <= fd);
Packit Service 4684c1
  ASSERT (close (fd) == 0);
Packit Service 4684c1
Packit Service 4684c1
  /* Create an executable regular file.  */
Packit Service 4684c1
  fd = func (BASE "e.exe", O_CREAT | O_RDONLY, 0700);
Packit Service 4684c1
  ASSERT (0 <= fd);
Packit Service 4684c1
  ASSERT (close (fd) == 0);
Packit Service 4684c1
Packit Service 4684c1
  /* Trailing slash handling.  */
Packit Service 4684c1
  errno = 0;
Packit Service 4684c1
  ASSERT (func (BASE "file/", O_RDONLY) == -1);
Packit Service 4684c1
  ASSERT (errno == ENOTDIR || errno == EISDIR || errno == EINVAL);
Packit Service 4684c1
Packit Service 4684c1
  /* Directories cannot be opened for writing.  */
Packit Service 4684c1
  errno = 0;
Packit Service 4684c1
  ASSERT (func (".", O_WRONLY) == -1);
Packit Service 4684c1
  ASSERT (errno == EISDIR || errno == EACCES);
Packit Service 4684c1
Packit Service 4684c1
  /* /dev/null must exist, and be writable.  */
Packit Service 4684c1
  fd = func ("/dev/null", O_RDONLY);
Packit Service 4684c1
  ASSERT (0 <= fd);
Packit Service 4684c1
  {
Packit Service 4684c1
    char c;
Packit Service 4684c1
    ASSERT (read (fd, &c, 1) == 0);
Packit Service 4684c1
  }
Packit Service 4684c1
  ASSERT (close (fd) == 0);
Packit Service 4684c1
  fd = func ("/dev/null", O_WRONLY);
Packit Service 4684c1
  ASSERT (0 <= fd);
Packit Service 4684c1
  ASSERT (write (fd, "c", 1) == 1);
Packit Service 4684c1
  ASSERT (close (fd) == 0);
Packit Service 4684c1
Packit Service 4684c1
  /* Although O_NONBLOCK on regular files can be ignored, it must not
Packit Service 4684c1
     cause a failure.  */
Packit Service 4684c1
  fd = func (BASE "file", O_NONBLOCK | O_RDONLY);
Packit Service 4684c1
  ASSERT (0 <= fd);
Packit Service 4684c1
  ASSERT (close (fd) == 0);
Packit Service 4684c1
Packit Service 4684c1
  /* O_CLOEXEC must be honoured.  */
Packit Service 4684c1
  if (O_CLOEXEC)
Packit Service 4684c1
    {
Packit Service 4684c1
      /* Since the O_CLOEXEC handling goes through a special code path at its
Packit Service 4684c1
         first invocation, test it twice.  */
Packit Service 4684c1
      int i;
Packit Service 4684c1
Packit Service 4684c1
      for (i = 0; i < 2; i++)
Packit Service 4684c1
        {
Packit Service 4684c1
          int flags;
Packit Service 4684c1
Packit Service 4684c1
          fd = func (BASE "file", O_CLOEXEC | O_RDONLY);
Packit Service 4684c1
          ASSERT (0 <= fd);
Packit Service 4684c1
          flags = fcntl (fd, F_GETFD);
Packit Service 4684c1
          ASSERT (flags >= 0);
Packit Service 4684c1
          ASSERT ((flags & FD_CLOEXEC) != 0);
Packit Service 4684c1
          ASSERT (close (fd) == 0);
Packit Service 4684c1
        }
Packit Service 4684c1
    }
Packit Service 4684c1
Packit Service 4684c1
  /* Symlink handling, where supported.  */
Packit Service 4684c1
  if (symlink (BASE "file", BASE "link") != 0)
Packit Service 4684c1
    {
Packit Service 4684c1
      ASSERT (unlink (BASE "file") == 0);
Packit Service 4684c1
      if (print)
Packit Service 4684c1
        fputs ("skipping test: symlinks not supported on this file system\n",
Packit Service 4684c1
               stderr);
Packit Service 4684c1
      return 77;
Packit Service 4684c1
    }
Packit Service 4684c1
  errno = 0;
Packit Service 4684c1
  ASSERT (func (BASE "link/", O_RDONLY) == -1);
Packit Service 4684c1
  ASSERT (errno == ENOTDIR);
Packit Service 4684c1
  fd = func (BASE "link", O_RDONLY);
Packit Service 4684c1
  ASSERT (0 <= fd);
Packit Service 4684c1
  ASSERT (close (fd) == 0);
Packit Service 4684c1
Packit Service 4684c1
  /* Cleanup.  */
Packit Service 4684c1
  ASSERT (unlink (BASE "file") == 0);
Packit Service 4684c1
  ASSERT (unlink (BASE "e.exe") == 0);
Packit Service 4684c1
  ASSERT (unlink (BASE "link") == 0);
Packit Service 4684c1
Packit Service 4684c1
  return 0;
Packit Service 4684c1
}