Blame gl/tests/test-fopen.h

Packit Service 4684c1
/* Test of opening a file stream.
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
/* Include <config.h> and a form of <stdio.h> first.  */
Packit Service 4684c1
Packit Service 4684c1
#include <errno.h>
Packit Service 4684c1
#include <unistd.h>
Packit Service 4684c1
Packit Service 4684c1
#include "macros.h"
Packit Service 4684c1
Packit Service 4684c1
/* Test fopen.  Assumes BASE is defined.  */
Packit Service 4684c1
Packit Service 4684c1
static int
Packit Service 4684c1
test_fopen (void)
Packit Service 4684c1
{
Packit Service 4684c1
  FILE *f;
Packit Service 4684c1
  /* Remove anything from prior partial run.  */
Packit Service 4684c1
  unlink (BASE "file");
Packit Service 4684c1
Packit Service 4684c1
  /* Read requires existing file.  */
Packit Service 4684c1
  errno = 0;
Packit Service 4684c1
  ASSERT (fopen (BASE "file", "r") == NULL);
Packit Service 4684c1
  ASSERT (errno == ENOENT);
Packit Service 4684c1
Packit Service 4684c1
  /* Write can create a file.  */
Packit Service 4684c1
  f = fopen (BASE "file", "w");
Packit Service 4684c1
  ASSERT (f);
Packit Service 4684c1
  ASSERT (fclose (f) == 0);
Packit Service 4684c1
Packit Service 4684c1
  /* Trailing slash is invalid on non-directory.  */
Packit Service 4684c1
  errno = 0;
Packit Service 4684c1
  ASSERT (fopen (BASE "file/", "r") == NULL);
Packit Service 4684c1
  ASSERT (errno == ENOTDIR || errno == EISDIR || errno == EINVAL);
Packit Service 4684c1
Packit Service 4684c1
  errno = 0;
Packit Service 4684c1
  ASSERT (fopen (BASE "file/", "r+") == NULL);
Packit Service 4684c1
  ASSERT (errno == ENOTDIR || errno == EISDIR || errno == EINVAL);
Packit Service 4684c1
Packit Service 4684c1
  /* Cannot create a directory.  */
Packit Service 4684c1
  errno = 0;
Packit Service 4684c1
  ASSERT (fopen ("nonexist.ent/", "w") == NULL);
Packit Service 4684c1
  ASSERT (errno == ENOTDIR || errno == EISDIR || errno == ENOENT
Packit Service 4684c1
          || errno == EINVAL);
Packit Service 4684c1
Packit Service 4684c1
  /* Directories cannot be opened for writing.  */
Packit Service 4684c1
  errno = 0;
Packit Service 4684c1
  ASSERT (fopen (".", "w") == NULL);
Packit Service 4684c1
  ASSERT (errno == EISDIR || errno == EINVAL || errno == EACCES);
Packit Service 4684c1
Packit Service 4684c1
  errno = 0;
Packit Service 4684c1
  ASSERT (fopen ("./", "w") == NULL);
Packit Service 4684c1
  ASSERT (errno == EISDIR || errno == EINVAL || errno == EACCES);
Packit Service 4684c1
Packit Service 4684c1
  errno = 0;
Packit Service 4684c1
  ASSERT (fopen (".", "r+") == NULL);
Packit Service 4684c1
  ASSERT (errno == EISDIR || errno == EINVAL || errno == EACCES);
Packit Service 4684c1
Packit Service 4684c1
  errno = 0;
Packit Service 4684c1
  ASSERT (fopen ("./", "r+") == NULL);
Packit Service 4684c1
  ASSERT (errno == EISDIR || errno == EINVAL || errno == EACCES);
Packit Service 4684c1
Packit Service 4684c1
  /* /dev/null must exist, and be writable.  */
Packit Service 4684c1
  f = fopen ("/dev/null", "r");
Packit Service 4684c1
  ASSERT (f);
Packit Service 4684c1
  ASSERT (fclose (f) == 0);
Packit Service 4684c1
  f = fopen ("/dev/null", "w");
Packit Service 4684c1
  ASSERT (f);
Packit Service 4684c1
  ASSERT (fclose (f) == 0);
Packit Service 4684c1
Packit Service 4684c1
  /* Cleanup.  */
Packit Service 4684c1
  ASSERT (unlink (BASE "file") == 0);
Packit Service 4684c1
Packit Service 4684c1
  return 0;
Packit Service 4684c1
}