view tests/test-renameat.c @ 12517:013b270a78b9

tests: don't require debug system() to pass When running a cross-compilation to mingw on a Linux host, but without a valid mingw rm executable, the debug-only system() call would fail. This is not fatal to the test, since the call is merely a debugging aid if a prior call to the test encountered a failure and left garbage; in general, the tests should pass and the system() call have nothing to clean in the first place. Also, the system() call only needs to happen once at startup, not once per iteration into the tests performed by .h files. * tests/test-lstat.h (test_lstat_func): Move debug cleanup... * tests/test-rmdir.h (test_rmdir_func): Likewise. * tests/test-unlink.h (test_unlink_func): Likewise. * tests/test-fstatat.c (main): ...into callers. * tests/test-lstat.c (main): Likewise. * tests/test-rmdir.c (main): Likewise. * tests/test-unlink.c (main): Likewise. * tests/test-unlinkat.c (main): Likewise. * tests/test-areadlink-with-size.c (main): Don't require a debug-only system call to pass, aiding cross-testing to mingw. * tests/test-areadlink.c (main): Likewise. * tests/test-areadlinkat-with-size.c (main): Likewise. * tests/test-areadlinkat.c (main): Likewise. * tests/test-canonicalize-lgpl.c (main): Likewise. * tests/test-canonicalize.c (main): Likewise. * tests/test-chown.c (main): Likewise. * tests/test-fchownat.c (main): Likewise. * tests/test-lchown.c (main): Likewise. * tests/test-fdutimensat.c (main): Likewise. * tests/test-futimens.c (main): Likewise. * tests/test-link.c (main): Likewise. * tests/test-linkat.c (main): Likewise. * tests/test-mkdir.c (main): Likewise. * tests/test-mkdirat.c (main): Likewise. * tests/test-mkfifo.c (main): Likewise. * tests/test-mkfifoat.c (main): Likewise. * tests/test-mknod.c (main): Likewise. * tests/test-readlink.c (main): Likewise. * tests/test-remove.c (main): Likewise. * tests/test-rename.c (main): Likewise. * tests/test-renameat.c (main): Likewise. * tests/test-symlink.c (main): Likewise. * tests/test-symlinkat.c (main): Likewise. * tests/test-utimens.c (main): Likewise. * tests/test-utimensat.c (main): Likewise. Signed-off-by: Eric Blake <ebb9@byu.net>
author Eric Blake <ebb9@byu.net>
date Tue, 29 Dec 2009 06:58:18 -0700
parents a48d3d749ca5
children c2cbabec01dd
line wrap: on
line source

/* Tests of renameat.
   Copyright (C) 2009 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

/* Written by Eric Blake <ebb9@byu.net>, 2009.  */

#include <config.h>

#include <stdio.h>

#include "signature.h"
SIGNATURE_CHECK (renameat, int, (int, char const *, int, char const *));

#include <fcntl.h>
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "filenamecat.h"
#include "xgetcwd.h"
#include "macros.h"

#define BASE "test-renameat.t"

#include "test-rename.h"

static int dfd1 = AT_FDCWD;
static int dfd2 = AT_FDCWD;

/* Wrapper to test renameat like rename.  */
static int
do_rename (char const *name1, char const *name2)
{
  return renameat (dfd1, name1, dfd2, name2);
}

int
main (void)
{
  int i;
  int dfd;
  char *cwd;
  int result;

  /* Clean up any trash from prior testsuite runs.  */
  system ("rm -rf " BASE "*");

  /* Test basic rename functionality, using current directory.  */
  result = test_rename (do_rename, false);
  dfd1 = open (".", O_RDONLY);
  ASSERT (0 <= dfd1);
  ASSERT (test_rename (do_rename, false) == result);
  dfd2 = dfd1;
  ASSERT (test_rename (do_rename, false) == result);
  dfd1 = AT_FDCWD;
  ASSERT (test_rename (do_rename, false) == result);
  ASSERT (close (dfd2) == 0);

  /* Create locations to manipulate.  */
  ASSERT (mkdir (BASE "sub1", 0700) == 0);
  ASSERT (mkdir (BASE "sub2", 0700) == 0);
  dfd = creat (BASE "00", 0600);
  ASSERT (0 <= dfd);
  ASSERT (close (dfd) == 0);
  cwd = xgetcwd ();

  dfd = open (BASE "sub1", O_RDONLY);
  ASSERT (0 <= dfd);
  ASSERT (chdir (BASE "sub2") == 0);

  /* There are 16 possible scenarios, based on whether an fd is
     AT_FDCWD or real, and whether a file is absolute or relative.

     To ensure that we test all of the code paths (rather than
     triggering early normalization optimizations), we use a loop to
     repeatedly rename a file in the parent directory, use an fd open
     on subdirectory 1, all while executing in subdirectory 2; all
     relative names are thus given with a leading "../".  Finally, the
     last scenario (two relative paths given, neither one AT_FDCWD)
     has two paths, based on whether the two fds are equivalent, so we
     do the other variant after the loop.  */
  for (i = 0; i < 16; i++)
    {
      int fd1 = (i & 8) ? dfd : AT_FDCWD;
      char *file1 = file_name_concat ((i & 4) ? ".." : cwd, BASE "xx", NULL);
      int fd2 = (i & 2) ? dfd : AT_FDCWD;
      char *file2 = file_name_concat ((i & 1) ? ".." : cwd, BASE "xx", NULL);

      ASSERT (sprintf (strchr (file1, '\0') - 2, "%02d", i) == 2);
      ASSERT (sprintf (strchr (file2, '\0') - 2, "%02d", i + 1) == 2);
      ASSERT (renameat (fd1, file1, fd2, file2) == 0);
      free (file1);
      free (file2);
    }
  dfd2 = open ("..", O_RDONLY);
  ASSERT (0 <= dfd2);
  ASSERT (renameat (dfd, "../" BASE "16", dfd2, BASE "17") == 0);
  ASSERT (close (dfd2) == 0);

  /* Now we change back to the parent directory, and set dfd to ".";
     using dfd in remaining tests will expose any bugs if emulation
     via /proc/self/fd doesn't check for empty names.  */
  ASSERT (chdir ("..") == 0);
  ASSERT (close (dfd) == 0);
  dfd = open (".", O_RDONLY);
  ASSERT (0 <= dfd);

  ASSERT (close (creat (BASE "sub2/file", 0600)) == 0);
  errno = 0;
  ASSERT (renameat (dfd, BASE "sub1", dfd, BASE "sub2") == -1);
  ASSERT (errno == EEXIST || errno == ENOTEMPTY);
  ASSERT (unlink (BASE "sub2/file") == 0);
  errno = 0;
  ASSERT (renameat (dfd, BASE "sub2", dfd, BASE "sub1/.") == -1);
  ASSERT (errno == EINVAL || errno == EISDIR || errno == EBUSY
          || errno == ENOTEMPTY);
  errno = 0;
  ASSERT (renameat (dfd, BASE "sub2/.", dfd, BASE "sub1") == -1);
  ASSERT (errno == EINVAL || errno == EBUSY);
  errno = 0;
  ASSERT (renameat (dfd, BASE "17", dfd, BASE "sub1") == -1);
  ASSERT (errno == EISDIR);
  errno = 0;
  ASSERT (renameat (dfd, BASE "nosuch", dfd, BASE "18") == -1);
  ASSERT (errno == ENOENT);
  errno = 0;
  ASSERT (renameat (dfd, "", dfd, BASE "17") == -1);
  ASSERT (errno == ENOENT);
  errno = 0;
  ASSERT (renameat (dfd, BASE "17", dfd, "") == -1);
  ASSERT (errno == ENOENT);
  errno = 0;
  ASSERT (renameat (dfd, BASE "sub2", dfd, BASE "17") == -1);
  ASSERT (errno == ENOTDIR);
  errno = 0;
  ASSERT (renameat (dfd, BASE "17/", dfd, BASE "18") == -1);
  ASSERT (errno == ENOTDIR);
  errno = 0;
  ASSERT (renameat (dfd, BASE "17", dfd, BASE "18/") == -1);
  ASSERT (errno == ENOTDIR || errno == ENOENT);

  /* Finally, make sure we can overwrite existing files.  */
  ASSERT (close (creat (BASE "sub2/file", 0600)) == 0);
  errno = 0;
  ASSERT (renameat (dfd, BASE "sub2", dfd, BASE "sub1") == 0);
  ASSERT (renameat (dfd, BASE "sub1/file", dfd, BASE "17") == 0);

  /* Cleanup.  */
  ASSERT (close (dfd) == 0);
  errno = 0;
  ASSERT (unlink (BASE "sub1/file") == -1);
  ASSERT (errno == ENOENT);
  ASSERT (unlink (BASE "17") == 0);
  ASSERT (rmdir (BASE "sub1") == 0);
  errno = 0;
  ASSERT (rmdir (BASE "sub2") == -1);
  ASSERT (errno == ENOENT);
  free (cwd);

  if (result)
    fputs ("skipping test: symlinks not supported on this file system\n",
           stderr);
  return result;
}