Blame io/tst-copy_file_range.c

Packit 6c4009
/* Tests for copy_file_range.
Packit 6c4009
   Copyright (C) 2017-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
#include <array_length.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <inttypes.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/support.h>
Packit 6c4009
#include <support/temp_file.h>
Packit 6c4009
#include <support/test-driver.h>
Packit 6c4009
#include <support/xunistd.h>
Packit 6c4009
Packit 6c4009
/* Boolean flags which indicate whether to use pointers with explicit
Packit 6c4009
   output flags.  */
Packit 6c4009
static int do_inoff;
Packit 6c4009
static int do_outoff;
Packit 6c4009
Packit 6c4009
/* Name and descriptors of the input files.  Files are truncated and
Packit 6c4009
   reopened (with O_RDWR) between tests.  */
Packit 6c4009
static char *infile;
Packit 6c4009
static int infd;
Packit 6c4009
static char *outfile;
Packit 6c4009
static int outfd;
Packit 6c4009
Packit 6c4009
/* Input and output offsets.  Set according to do_inoff and do_outoff
Packit 6c4009
   before the test.  The offsets themselves are always set to
Packit 6c4009
   zero.  */
Packit 6c4009
static off64_t inoff;
Packit 6c4009
static off64_t *pinoff;
Packit 6c4009
static off64_t outoff;
Packit 6c4009
static off64_t *poutoff;
Packit 6c4009
Packit Service ca691f
/* These are a collection of copy sizes used in tests.    */
Packit 6c4009
enum { maximum_size = 99999 };
Packit 6c4009
static const int typical_sizes[] =
Packit Service ca691f
  { 0, 1, 2, 3, 1024, 2048, 4096, 8191, 8192, 8193, maximum_size };
Packit 6c4009
Packit 6c4009
/* The random contents of this array can be used as a pattern to check
Packit 6c4009
   for correct write operations.  */
Packit 6c4009
static unsigned char random_data[maximum_size];
Packit 6c4009
Packit 6c4009
/* The size chosen by the test harness.  */
Packit 6c4009
static int current_size;
Packit 6c4009
Packit 6c4009
/* Perform a copy of a file.  */
Packit 6c4009
static void
Packit 6c4009
simple_file_copy (void)
Packit 6c4009
{
Packit 6c4009
  xwrite (infd, random_data, current_size);
Packit 6c4009
Packit 6c4009
  int length;
Packit 6c4009
  int in_skipped; /* Expected skipped bytes in input.  */
Packit 6c4009
  if (do_inoff)
Packit 6c4009
    {
Packit 6c4009
      xlseek (infd, 1, SEEK_SET);
Packit 6c4009
      inoff = 2;
Packit 6c4009
      length = current_size - 3;
Packit 6c4009
      in_skipped = 2;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      xlseek (infd, 3, SEEK_SET);
Packit 6c4009
      length = current_size - 5;
Packit 6c4009
      in_skipped = 3;
Packit 6c4009
    }
Packit 6c4009
  int out_skipped; /* Expected skipped bytes before the written data.  */
Packit 6c4009
  if (do_outoff)
Packit 6c4009
    {
Packit 6c4009
      xlseek (outfd, 4, SEEK_SET);
Packit 6c4009
      outoff = 5;
Packit 6c4009
      out_skipped = 5;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      xlseek (outfd, 6, SEEK_SET);
Packit 6c4009
      length = current_size - 6;
Packit 6c4009
      out_skipped = 6;
Packit 6c4009
    }
Packit 6c4009
  if (length < 0)
Packit 6c4009
    length = 0;
Packit 6c4009
Packit 6c4009
  TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff,
Packit 6c4009
                                 length, 0), length);
Packit 6c4009
  if (do_inoff)
Packit 6c4009
    {
Packit 6c4009
      TEST_COMPARE (inoff, 2 + length);
Packit 6c4009
      TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 1);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 3 + length);
Packit 6c4009
  if (do_outoff)
Packit 6c4009
    {
Packit 6c4009
      TEST_COMPARE (outoff, 5 + length);
Packit 6c4009
      TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 4);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 6 + length);
Packit 6c4009
Packit 6c4009
  struct stat64 st;
Packit 6c4009
  xfstat (outfd, &st);
Packit 6c4009
  if (length > 0)
Packit 6c4009
    TEST_COMPARE (st.st_size, out_skipped + length);
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* If we did not write anything, we also did not add any
Packit 6c4009
         padding.  */
Packit 6c4009
      TEST_COMPARE (st.st_size, 0);
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  xlseek (outfd, 0, SEEK_SET);
Packit 6c4009
  char *bytes = xmalloc (st.st_size);
Packit 6c4009
  TEST_COMPARE (read (outfd, bytes, st.st_size), st.st_size);
Packit 6c4009
  for (int i = 0; i < out_skipped; ++i)
Packit 6c4009
    TEST_COMPARE (bytes[i], 0);
Packit 6c4009
  TEST_VERIFY (memcmp (bytes + out_skipped, random_data + in_skipped,
Packit 6c4009
                       length) == 0);
Packit 6c4009
  free (bytes);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Test that a short input file results in a shortened copy.  */
Packit 6c4009
static void
Packit 6c4009
short_copy (void)
Packit 6c4009
{
Packit 6c4009
  if (current_size == 0)
Packit 6c4009
    /* Nothing to shorten.  */
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  /* Two subtests, one with offset 0 and current_size - 1 bytes, and
Packit 6c4009
     another one with current_size bytes, but offset 1.  */
Packit 6c4009
  for (int shift = 0; shift < 2; ++shift)
Packit 6c4009
    {
Packit 6c4009
      if (test_verbose > 0)
Packit 6c4009
        printf ("info:   shift=%d\n", shift);
Packit 6c4009
      xftruncate (infd, 0);
Packit 6c4009
      xlseek (infd, 0, SEEK_SET);
Packit 6c4009
      xwrite (infd, random_data, current_size - !shift);
Packit 6c4009
Packit 6c4009
      if (do_inoff)
Packit 6c4009
        {
Packit 6c4009
          inoff = shift;
Packit 6c4009
          xlseek (infd, 2, SEEK_SET);
Packit 6c4009
        }
Packit 6c4009
      else
Packit 6c4009
        {
Packit 6c4009
          inoff = 3;
Packit 6c4009
          xlseek (infd, shift, SEEK_SET);
Packit 6c4009
        }
Packit 6c4009
      ftruncate (outfd, 0);
Packit 6c4009
      xlseek (outfd, 0, SEEK_SET);
Packit 6c4009
      outoff = 0;
Packit 6c4009
Packit 6c4009
      /* First call copies current_size - 1 bytes.  */
Packit 6c4009
      TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff,
Packit 6c4009
                                     current_size, 0), current_size - 1);
Packit 6c4009
      char *buffer = xmalloc (current_size);
Packit 6c4009
      TEST_COMPARE (pread64 (outfd, buffer, current_size, 0),
Packit 6c4009
                    current_size - 1);
Packit 6c4009
      TEST_VERIFY (memcmp (buffer, random_data + shift, current_size - 1)
Packit 6c4009
                   == 0);
Packit 6c4009
      free (buffer);
Packit 6c4009
Packit 6c4009
      if (do_inoff)
Packit 6c4009
        {
Packit 6c4009
          TEST_COMPARE (inoff, current_size - 1 + shift);
Packit 6c4009
          TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 2);
Packit 6c4009
        }
Packit 6c4009
      else
Packit 6c4009
        TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), current_size - 1 + shift);
Packit 6c4009
      if (do_outoff)
Packit 6c4009
        {
Packit 6c4009
          TEST_COMPARE (outoff, current_size - 1);
Packit 6c4009
          TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 0);
Packit 6c4009
        }
Packit 6c4009
      else
Packit 6c4009
        TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), current_size - 1);
Packit 6c4009
Packit 6c4009
      /* First call copies zero bytes.  */
Packit 6c4009
      TEST_COMPARE (copy_file_range (infd, pinoff, outfd, poutoff,
Packit 6c4009
                                     current_size, 0), 0);
Packit 6c4009
      /* And the offsets are unchanged.  */
Packit 6c4009
      if (do_inoff)
Packit 6c4009
        {
Packit 6c4009
          TEST_COMPARE (inoff, current_size - 1 + shift);
Packit 6c4009
          TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 2);
Packit 6c4009
        }
Packit 6c4009
      else
Packit 6c4009
        TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), current_size - 1 + shift);
Packit 6c4009
      if (do_outoff)
Packit 6c4009
        {
Packit 6c4009
          TEST_COMPARE (outoff, current_size - 1);
Packit 6c4009
          TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), 0);
Packit 6c4009
        }
Packit 6c4009
      else
Packit 6c4009
        TEST_COMPARE (xlseek (outfd, 0, SEEK_CUR), current_size - 1);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* A named test function.  */
Packit 6c4009
struct test_case
Packit 6c4009
{
Packit 6c4009
  const char *name;
Packit 6c4009
  void (*func) (void);
Packit 6c4009
  bool sizes; /* If true, call the test with different current_size values.  */
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* The available test cases.  */
Packit 6c4009
static struct test_case tests[] =
Packit 6c4009
  {
Packit 6c4009
    { "simple_file_copy", simple_file_copy, .sizes = true },
Packit 6c4009
    { "short_copy", short_copy, .sizes = true },
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  for (unsigned char *p = random_data; p < array_end (random_data); ++p)
Packit 6c4009
    *p = rand () >> 24;
Packit 6c4009
Packit 6c4009
  infd = create_temp_file ("tst-copy_file_range-in-", &infile);
Packit Service ca691f
  outfd = create_temp_file ("tst-copy_file_range-out-", &outfile);
Packit 6c4009
  {
Packit Service ca691f
    ssize_t ret = copy_file_range (infd, NULL, outfd, NULL, 0, 0);
Packit Service ca691f
    if (ret != 0)
Packit 6c4009
      {
Packit Service ca691f
        if (errno == ENOSYS)
Packit Service ca691f
          FAIL_UNSUPPORTED ("copy_file_range is not support on this system");
Packit Service ca691f
        FAIL_EXIT1 ("copy_file_range probing call: %m");
Packit 6c4009
      }
Packit 6c4009
  }
Packit 6c4009
  xclose (infd);
Packit Service ca691f
  xclose (outfd);
Packit 6c4009
Packit 6c4009
  for (do_inoff = 0; do_inoff < 2; ++do_inoff)
Packit 6c4009
    for (do_outoff = 0; do_outoff < 2; ++do_outoff)
Packit 6c4009
      for (struct test_case *test = tests; test < array_end (tests); ++test)
Packit 6c4009
        for (const int *size = typical_sizes;
Packit 6c4009
             size < array_end (typical_sizes); ++size)
Packit 6c4009
          {
Packit 6c4009
            current_size = *size;
Packit 6c4009
            if (test_verbose > 0)
Packit 6c4009
              printf ("info: %s do_inoff=%d do_outoff=%d current_size=%d\n",
Packit 6c4009
                      test->name, do_inoff, do_outoff, current_size);
Packit 6c4009
Packit 6c4009
            inoff = 0;
Packit 6c4009
            if (do_inoff)
Packit 6c4009
              pinoff = &inof;;
Packit 6c4009
            else
Packit 6c4009
              pinoff = NULL;
Packit 6c4009
            outoff = 0;
Packit 6c4009
            if (do_outoff)
Packit 6c4009
              poutoff = &outoff;
Packit 6c4009
            else
Packit 6c4009
              poutoff = NULL;
Packit 6c4009
Packit 6c4009
            infd = xopen (infile, O_RDWR | O_LARGEFILE, 0);
Packit 6c4009
            xftruncate (infd, 0);
Packit 6c4009
            outfd = xopen (outfile, O_RDWR | O_LARGEFILE, 0);
Packit 6c4009
            xftruncate (outfd, 0);
Packit 6c4009
Packit 6c4009
            test->func ();
Packit 6c4009
Packit 6c4009
            xclose (infd);
Packit 6c4009
            xclose (outfd);
Packit 6c4009
Packit 6c4009
            if (!test->sizes)
Packit 6c4009
              /* Skip the other sizes unless they have been
Packit 6c4009
                 requested.  */
Packit 6c4009
              break;
Packit 6c4009
          }
Packit 6c4009
Packit 6c4009
  free (infile);
Packit 6c4009
  free (outfile);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>