Blame gnulib-tests/test-open.h

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