Blame stdio-common/tst-fmemopen2.c

Packit 6c4009
/* fmemopen tests.
Packit 6c4009
   Copyright (C) 2015-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
Packit 6c4009
/* Check fmemopen with user provided buffer open for write.  */
Packit 6c4009
static int
Packit 6c4009
do_test_with_buffer (void)
Packit 6c4009
{
Packit 6c4009
  int result = 0;
Packit 6c4009
  char buf[100];
Packit 6c4009
  const size_t nbuf = sizeof (buf);
Packit 6c4009
Packit 6c4009
  FILE *fp = fmemopen (buf, nbuf, "w");
Packit 6c4009
  if (fp == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: fmemopen failed (%s)\n", __FUNCTION__);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Default write operation, check if file position is correct after it.  */
Packit 6c4009
  static const char str[] = "hello world";
Packit 6c4009
  const size_t nstr = sizeof (str) - 1;
Packit 6c4009
  fputs (str, fp);
Packit 6c4009
  off_t o = ftello (fp);
Packit 6c4009
  if (o != nstr)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: first ftello returned %jd, expected %zu\n",
Packit 6c4009
	      (intmax_t)o, nstr);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Rewind stream and seek tests, the position size should be equal to
Packit 6c4009
     buffer size provided in open function.  */
Packit 6c4009
  rewind (fp);
Packit 6c4009
  o = ftello (fp);
Packit 6c4009
  if (o != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: second ftello returned %jd, expected 0\n",
Packit 6c4009
	      (intmax_t)o);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  if (fseeko (fp, 0, SEEK_END) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: fseeko failed\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  o = ftello (fp);
Packit 6c4009
  if (o != nstr)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: third ftello returned %jd, expected %zu\n",
Packit 6c4009
	      (intmax_t)o, nstr);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Rewind the stream and recheck by using a shorter string.  */
Packit 6c4009
  rewind (fp);
Packit 6c4009
  static const char str2[] = "just hello";
Packit 6c4009
  const size_t nstr2 = sizeof (str2) - 1;
Packit 6c4009
  assert (nstr2 < nstr);
Packit 6c4009
  fputs (str2, fp);
Packit 6c4009
  o = ftello (fp);
Packit 6c4009
  if (o != nstr2)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: fourth ftello returned %jd, expected %zu\n",
Packit 6c4009
	      (intmax_t)o, nstr2);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  fclose (fp);
Packit 6c4009
Packit 6c4009
  /* Again, but now with a larger string.  */
Packit 6c4009
  static const char str3[] = "just hellod";
Packit 6c4009
  if (strcmp (buf, str3) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: final string is \"%s\", expected \"%s\"\n",
Packit 6c4009
              buf, str3);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Check fmemopen without user provided buffer open for write.  */
Packit 6c4009
static int
Packit 6c4009
do_test_without_buffer (void)
Packit 6c4009
{
Packit 6c4009
  int result = 0;
Packit 6c4009
  const size_t nbuf = 100;
Packit 6c4009
Packit 6c4009
  FILE *fp = fmemopen (NULL, nbuf, "w");
Packit 6c4009
  if (fp == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: fmemopen failed (%s)\n", __FUNCTION__);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  static const char str[] = "hello world";
Packit 6c4009
  const size_t nstr = sizeof (str) - 1;
Packit 6c4009
Packit 6c4009
  /* Default write operation, check if file position is correct after it.  */
Packit 6c4009
  fputs (str, fp);
Packit 6c4009
  off_t o = ftello (fp);
Packit 6c4009
  if (o != nstr)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: first ftello returned %jd, expected %zu\n",
Packit 6c4009
	      (intmax_t) o, nstr);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  if (fseeko (fp, 0, SEEK_END) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: fseeko failed\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  o = ftello (fp);
Packit 6c4009
  if (o != nstr)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: second ftello returned %jd, expected %zu\n",
Packit 6c4009
	      (intmax_t) o, nbuf);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Rewind the stream and recheck by using a shorter string.  */
Packit 6c4009
  rewind (fp);
Packit 6c4009
  static const char str2[] = "just hello";
Packit 6c4009
  const size_t nstr2 = sizeof (str2) - 1;
Packit 6c4009
  assert (nstr2 < nstr);
Packit 6c4009
  fputs (str2, fp);
Packit 6c4009
  o = ftello (fp);
Packit 6c4009
  if (o != nstr2)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: third ftello returned %jd, expected %zu\n",
Packit 6c4009
	      (intmax_t) o, nstr2);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  fclose (fp);
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Check fmemopen with a buffer lenght of zero.  */
Packit 6c4009
static int
Packit 6c4009
do_test_length_zero (void)
Packit 6c4009
{
Packit 6c4009
  int result = 0;
Packit 6c4009
  FILE *fp;
Packit 6c4009
#define BUFCONTENTS "testing buffer"
Packit 6c4009
  char buf[100] = BUFCONTENTS;
Packit 6c4009
  const size_t nbuf = 0;
Packit 6c4009
  int r;
Packit 6c4009
Packit 6c4009
  fp = fmemopen (buf, nbuf, "r");
Packit 6c4009
  if (fp == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: fmemopen failed (%s)\n", __FUNCTION__);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Reading any data on a zero-length buffer should return EOF.  */
Packit 6c4009
  if ((r = fgetc (fp)) != EOF)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: fgetc on a zero-length returned: %d\n", r);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  off_t o = ftello (fp);
Packit 6c4009
  if (o != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: first ftello returned %jd, expected 0\n",
Packit 6c4009
	      (intmax_t) o);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  fclose (fp);
Packit 6c4009
Packit 6c4009
  /* Writing any data shall start at current position and shall not pass
Packit 6c4009
     current buffer size beyond the size in fmemopen call.  */
Packit 6c4009
  fp = fmemopen (buf, nbuf, "w");
Packit 6c4009
  if (fp == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: second fmemopen failed (%s)\n", __FUNCTION__);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  static const char str[] = "hello world";
Packit 6c4009
  /* Because of buffering, the fputs call itself will not fail. However the
Packit 6c4009
     final buffer should be not changed because length 0 was passed to the
Packit 6c4009
     fmemopen call.  */
Packit 6c4009
  fputs (str, fp);
Packit 6c4009
  r = 0;
Packit 6c4009
  errno = 0;
Packit 6c4009
  if (fflush (fp) != EOF)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: fflush did not return EOF\n");
Packit 6c4009
      fclose (fp);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (errno != ENOSPC)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: errno is %i (expected ENOSPC)\n", errno);
Packit 6c4009
      fclose (fp);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fclose (fp);
Packit 6c4009
Packit 6c4009
  if (strcmp (buf, BUFCONTENTS) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: strcmp (%s, %s) failed\n", buf, BUFCONTENTS);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Different than 'w' mode, 'w+' truncates the buffer.  */
Packit 6c4009
  fp = fmemopen (buf, nbuf, "w+");
Packit 6c4009
  if (fp == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: third fmemopen failed (%s)\n", __FUNCTION__);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fclose (fp);
Packit 6c4009
Packit 6c4009
  if (strcmp (buf, "") != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: strcmp (%s, \"\") failed\n", buf);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  int ret = 0;
Packit 6c4009
Packit 6c4009
  ret += do_test_with_buffer ();
Packit 6c4009
  ret += do_test_without_buffer ();
Packit 6c4009
  ret += do_test_length_zero ();
Packit 6c4009
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"