Blame gnulib-tests/test-fread.c

Packit Service fdd496
/* Test of fread() function.
Packit Service fdd496
   Copyright (C) 2011-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, or (at your option)
Packit Service fdd496
   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
#include <config.h>
Packit Service fdd496
Packit Service fdd496
#include <stdio.h>
Packit Service fdd496
Packit Service fdd496
#include "signature.h"
Packit Service fdd496
SIGNATURE_CHECK (fread, size_t, (void *, size_t, size_t, FILE *));
Packit Service fdd496
Packit Service fdd496
#include <errno.h>
Packit Service fdd496
#include <fcntl.h>
Packit Service fdd496
#include <unistd.h>
Packit Service fdd496
Packit Service fdd496
#if HAVE_MSVC_INVALID_PARAMETER_HANDLER
Packit Service fdd496
# include "msvc-inval.h"
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
#include "macros.h"
Packit Service fdd496
Packit Service fdd496
int
Packit Service fdd496
main (int argc, char **argv)
Packit Service fdd496
{
Packit Service fdd496
  const char *filename = "test-fread.txt";
Packit Service fdd496
Packit Service fdd496
  /* We don't have an fread() function that installs an invalid parameter
Packit Service fdd496
     handler so far.  So install that handler here, explicitly.  */
Packit Service fdd496
#if HAVE_MSVC_INVALID_PARAMETER_HANDLER \
Packit Service fdd496
    && MSVC_INVALID_PARAMETER_HANDLING == DEFAULT_HANDLING
Packit Service fdd496
  gl_msvc_inval_ensure_handler ();
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
  /* Prepare a file.  */
Packit Service fdd496
  {
Packit Service fdd496
    const char text[] = "hello world";
Packit Service fdd496
    int fd = open (filename, O_RDWR | O_CREAT | O_TRUNC, 0600);
Packit Service fdd496
    ASSERT (fd >= 0);
Packit Service fdd496
    ASSERT (write (fd, text, sizeof (text)) == sizeof (text));
Packit Service fdd496
    ASSERT (close (fd) == 0);
Packit Service fdd496
  }
Packit Service fdd496
Packit Service fdd496
  /* Test that fread() sets errno if someone else closes the stream
Packit Service fdd496
     fd behind the back of stdio.  */
Packit Service fdd496
  {
Packit Service fdd496
    FILE *fp = fopen (filename, "r");
Packit Service fdd496
    char buf[5];
Packit Service fdd496
    ASSERT (fp != NULL);
Packit Service fdd496
    ASSERT (close (fileno (fp)) == 0);
Packit Service fdd496
    errno = 0;
Packit Service fdd496
    ASSERT (fread (buf, 1, sizeof (buf), fp) == 0);
Packit Service fdd496
    ASSERT (errno == EBADF);
Packit Service fdd496
    ASSERT (ferror (fp));
Packit Service fdd496
    fclose (fp);
Packit Service fdd496
  }
Packit Service fdd496
Packit Service fdd496
  /* Test that fread() sets errno if the stream was constructed with
Packit Service fdd496
     an invalid file descriptor.  */
Packit Service fdd496
  {
Packit Service fdd496
    FILE *fp = fdopen (-1, "r");
Packit Service fdd496
    if (fp != NULL)
Packit Service fdd496
      {
Packit Service fdd496
        char buf[1];
Packit Service fdd496
        errno = 0;
Packit Service fdd496
        ASSERT (fread (buf, 1, 1, fp) == 0);
Packit Service fdd496
        ASSERT (errno == EBADF);
Packit Service fdd496
        ASSERT (ferror (fp));
Packit Service fdd496
        fclose (fp);
Packit Service fdd496
      }
Packit Service fdd496
  }
Packit Service fdd496
  {
Packit Service fdd496
    FILE *fp;
Packit Service fdd496
    close (99);
Packit Service fdd496
    fp = fdopen (99, "r");
Packit Service fdd496
    if (fp != NULL)
Packit Service fdd496
      {
Packit Service fdd496
        char buf[1];
Packit Service fdd496
        errno = 0;
Packit Service fdd496
        ASSERT (fread (buf, 1, 1, fp) == 0);
Packit Service fdd496
        ASSERT (errno == EBADF);
Packit Service fdd496
        ASSERT (ferror (fp));
Packit Service fdd496
        fclose (fp);
Packit Service fdd496
      }
Packit Service fdd496
  }
Packit Service fdd496
Packit Service fdd496
  /* Clean up.  */
Packit Service fdd496
  unlink (filename);
Packit Service fdd496
Packit Service fdd496
  return 0;
Packit Service fdd496
}