Blame gnulib-tests/test-open.h

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