Blame misc/tst-preadvwritev-common.c

Packit Service 82fcde
/* Common definitions for preadv and pwritev.
Packit Service 82fcde
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#include <array_length.h>
Packit Service 82fcde
#include <stdio.h>
Packit Service 82fcde
#include <stdint.h>
Packit Service 82fcde
#include <errno.h>
Packit Service 82fcde
#include <string.h>
Packit Service 82fcde
#include <sys/uio.h>
Packit Service 82fcde
#include <sys/stat.h>
Packit Service 82fcde
Packit Service 82fcde
#include <support/check.h>
Packit Service 82fcde
#include <support/temp_file.h>
Packit Service 82fcde
#include <support/xunistd.h>
Packit Service 82fcde
Packit Service 82fcde
static char *temp_filename;
Packit Service 82fcde
static int temp_fd;
Packit Service 82fcde
Packit Service 82fcde
static int do_test (void);
Packit Service 82fcde
Packit Service 82fcde
static void
Packit Service 82fcde
do_prepare (int argc, char **argv)
Packit Service 82fcde
{
Packit Service 82fcde
  temp_fd = create_temp_file ("tst-preadvwritev.", &temp_filename);
Packit Service 82fcde
  if (temp_fd == -1)
Packit Service 82fcde
    FAIL_EXIT1 ("cannot create temporary file");
Packit Service 82fcde
}
Packit Service 82fcde
#define PREPARE do_prepare
Packit Service 82fcde
Packit Service 82fcde
#ifndef PREADV
Packit Service 82fcde
# define PREADV(__fd, __iov, __iovcnt, __offset) \
Packit Service 82fcde
  preadv (__fd, __iov, __iovcnt, __offset)
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
#ifndef PWRITEV
Packit Service 82fcde
# define PWRITEV(__fd, __iov, __iovcnt, __offset) \
Packit Service 82fcde
  pwritev (__fd, __iov, __iovcnt, __offset)
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
static __attribute__ ((unused)) void
Packit Service 82fcde
do_test_without_offset (void)
Packit Service 82fcde
{
Packit Service 82fcde
  xftruncate (temp_fd, 0);
Packit Service 82fcde
Packit Service 82fcde
  xwrite (temp_fd, "123", 3);
Packit Service 82fcde
  xlseek (temp_fd, 2, SEEK_SET);
Packit Service 82fcde
  {
Packit Service 82fcde
    struct iovec iov[] =
Packit Service 82fcde
      {
Packit Service 82fcde
        { (void *) "abc", 3 },
Packit Service 82fcde
        { (void *) "xyzt", 4 },
Packit Service 82fcde
      };
Packit Service 82fcde
    TEST_COMPARE (PWRITEV (temp_fd, iov, array_length (iov), -1), 7);
Packit Service 82fcde
  }
Packit Service 82fcde
  TEST_COMPARE (xlseek (temp_fd, 0, SEEK_CUR), 9);
Packit Service 82fcde
Packit Service 82fcde
  xlseek (temp_fd, 1, SEEK_SET);
Packit Service 82fcde
  char buf1[3];
Packit Service 82fcde
  char buf2[2];
Packit Service 82fcde
  {
Packit Service 82fcde
    struct iovec iov[] =
Packit Service 82fcde
      {
Packit Service 82fcde
        { buf1, sizeof (buf1) },
Packit Service 82fcde
        { buf2, sizeof (buf2) },
Packit Service 82fcde
      };
Packit Service 82fcde
    TEST_COMPARE (PREADV (temp_fd, iov, array_length (iov), -1),
Packit Service 82fcde
                  sizeof (buf1) + sizeof (buf2));
Packit Service 82fcde
    TEST_COMPARE (memcmp ("2ab", buf1, sizeof (buf1)), 0);
Packit Service 82fcde
    TEST_COMPARE (memcmp ("cx", buf2, sizeof (buf2)), 0);
Packit Service 82fcde
    TEST_COMPARE (xlseek (temp_fd, 0, SEEK_CUR), 6);
Packit Service 82fcde
  }
Packit Service 82fcde
Packit Service 82fcde
  xftruncate (temp_fd, 0);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static int
Packit Service 82fcde
do_test_with_offset (off_t offset)
Packit Service 82fcde
{
Packit Service 82fcde
  struct iovec iov[2];
Packit Service 82fcde
  ssize_t ret;
Packit Service 82fcde
Packit Service 82fcde
  char buf1[32];
Packit Service 82fcde
  char buf2[64];
Packit Service 82fcde
Packit Service 82fcde
  memset (buf1, 0xf0, sizeof buf1);
Packit Service 82fcde
  memset (buf2, 0x0f, sizeof buf2);
Packit Service 82fcde
Packit Service 82fcde
  /* Write two buffer with 32 and 64 bytes respectively.  */
Packit Service 82fcde
  memset (iov, 0, sizeof iov);
Packit Service 82fcde
  iov[0].iov_base = buf1;
Packit Service 82fcde
  iov[0].iov_len = sizeof buf1;
Packit Service 82fcde
  iov[1].iov_base = buf2;
Packit Service 82fcde
  iov[1].iov_len = sizeof buf2;
Packit Service 82fcde
Packit Service 82fcde
  ret = PWRITEV (temp_fd, iov, 2, offset);
Packit Service 82fcde
  if (ret == -1)
Packit Service 82fcde
    FAIL_RET ("first pwritev returned -1");
Packit Service 82fcde
  if (ret != (sizeof buf1 + sizeof buf2))
Packit Service 82fcde
    FAIL_RET ("first pwritev returned an unexpected value");
Packit Service 82fcde
Packit Service 82fcde
  ret = PWRITEV (temp_fd, iov, 2, sizeof buf1 + sizeof buf2 + offset);
Packit Service 82fcde
  if (ret == -1)
Packit Service 82fcde
    FAIL_RET ("second pwritev returned -1");
Packit Service 82fcde
  if (ret != (sizeof buf1 + sizeof buf2))
Packit Service 82fcde
    FAIL_RET ("second pwritev returned an unexpected value");
Packit Service 82fcde
Packit Service 82fcde
  char buf3[32];
Packit Service 82fcde
  char buf4[64];
Packit Service 82fcde
Packit Service 82fcde
  memset (buf3, 0x0f, sizeof buf3);
Packit Service 82fcde
  memset (buf4, 0xf0, sizeof buf4);
Packit Service 82fcde
Packit Service 82fcde
  iov[0].iov_base = buf3;
Packit Service 82fcde
  iov[0].iov_len = sizeof buf3;
Packit Service 82fcde
  iov[1].iov_base = buf4;
Packit Service 82fcde
  iov[1].iov_len = sizeof buf4;
Packit Service 82fcde
Packit Service 82fcde
  /* Now read two buffer with 32 and 64 bytes respectively.  */
Packit Service 82fcde
  ret = PREADV (temp_fd, iov, 2, offset);
Packit Service 82fcde
  if (ret == -1)
Packit Service 82fcde
    FAIL_RET ("first preadv returned -1");
Packit Service 82fcde
  if (ret != (sizeof buf3 + sizeof buf4))
Packit Service 82fcde
    FAIL_RET ("first preadv returned an unexpected value");
Packit Service 82fcde
Packit Service 82fcde
  if (memcmp (buf1, buf3, sizeof buf1) != 0)
Packit Service 82fcde
    FAIL_RET ("first buffer from first preadv different than expected");
Packit Service 82fcde
  if (memcmp (buf2, buf4, sizeof buf2) != 0)
Packit Service 82fcde
    FAIL_RET ("second buffer from first preadv different than expected");
Packit Service 82fcde
Packit Service 82fcde
  ret = PREADV (temp_fd, iov, 2, sizeof buf3 + sizeof buf4 + offset);
Packit Service 82fcde
  if (ret == -1)
Packit Service 82fcde
    FAIL_RET ("second preadv returned -1");
Packit Service 82fcde
  if (ret != (sizeof buf3 + sizeof buf4))
Packit Service 82fcde
    FAIL_RET ("second preadv returned an unexpected value");
Packit Service 82fcde
Packit Service 82fcde
  /* And compare the buffers read and written to check if there are equal.  */
Packit Service 82fcde
  if (memcmp (buf1, buf3, sizeof buf1) != 0)
Packit Service 82fcde
    FAIL_RET ("first buffer from second preadv different than expected");
Packit Service 82fcde
  if (memcmp (buf2, buf4, sizeof buf2) != 0)
Packit Service 82fcde
    FAIL_RET ("second buffer from second preadv different than expected");
Packit Service 82fcde
Packit Service 82fcde
  return 0;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
#include <support/test-driver.c>