Blame libglnx/tests/test-libglnx-fdio.c

rpm-build c487f7
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
rpm-build c487f7
 *
rpm-build c487f7
 * Copyright (C) 2017 Red Hat, Inc.
rpm-build c487f7
 *
rpm-build c487f7
 * This library is free software; you can redistribute it and/or
rpm-build c487f7
 * modify it under the terms of the GNU Lesser General Public
rpm-build c487f7
 * License as published by the Free Software Foundation; either
rpm-build c487f7
 * version 2 of the License, or (at your option) any later version.
rpm-build c487f7
 *
rpm-build c487f7
 * This library is distributed in the hope that it will be useful,
rpm-build c487f7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build c487f7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build c487f7
 * Lesser General Public License for more details.
rpm-build c487f7
 *
rpm-build c487f7
 * You should have received a copy of the GNU Lesser General Public
rpm-build c487f7
 * License along with this library; if not, write to the
rpm-build c487f7
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
rpm-build c487f7
 * Boston, MA 02111-1307, USA.
rpm-build c487f7
 */
rpm-build c487f7
rpm-build c487f7
#include "config.h"
rpm-build c487f7
#include "libglnx.h"
rpm-build c487f7
#include <glib.h>
rpm-build c487f7
#include <stdlib.h>
rpm-build c487f7
#include <gio/gio.h>
rpm-build c487f7
#include <err.h>
rpm-build c487f7
#include <string.h>
rpm-build c487f7
rpm-build c487f7
#include "libglnx-testlib.h"
rpm-build c487f7
rpm-build c487f7
static gboolean
rpm-build c487f7
renameat_test_setup (int *out_srcfd, int *out_destfd,
rpm-build c487f7
                     GError **error)
rpm-build c487f7
{
rpm-build c487f7
  glnx_autofd int srcfd = -1;
rpm-build c487f7
  glnx_autofd int destfd = -1;
rpm-build c487f7
rpm-build c487f7
  (void) glnx_shutil_rm_rf_at (AT_FDCWD, "srcdir", NULL, NULL);
rpm-build c487f7
  if (mkdir ("srcdir", 0755) < 0)
rpm-build c487f7
    err (1, "mkdir");
rpm-build c487f7
  if (!glnx_opendirat (AT_FDCWD, "srcdir", TRUE, &srcfd, error))
rpm-build c487f7
    return FALSE;
rpm-build c487f7
  (void) glnx_shutil_rm_rf_at (AT_FDCWD, "destdir", NULL, NULL);
rpm-build c487f7
  if (mkdir ("destdir", 0755) < 0)
rpm-build c487f7
    err (1, "mkdir");
rpm-build c487f7
  if (!glnx_opendirat (AT_FDCWD, "destdir", TRUE, &destfd, error))
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  if (!glnx_file_replace_contents_at (srcfd, "foo", (guint8*)"foo contents", strlen ("foo contents"),
rpm-build c487f7
                                      GLNX_FILE_REPLACE_NODATASYNC, NULL, error))
rpm-build c487f7
    return FALSE;
rpm-build c487f7
  if (!glnx_file_replace_contents_at (destfd, "bar", (guint8*)"bar contents", strlen ("bar contents"),
rpm-build c487f7
                                      GLNX_FILE_REPLACE_NODATASYNC, NULL, error))
rpm-build c487f7
    return FALSE;
rpm-build c487f7
rpm-build c487f7
  *out_srcfd = srcfd; srcfd = -1;
rpm-build c487f7
  *out_destfd = destfd; destfd = -1;
rpm-build c487f7
  return TRUE;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
test_renameat2_noreplace (void)
rpm-build c487f7
{
rpm-build c487f7
  _GLNX_TEST_DECLARE_ERROR(local_error, error);
rpm-build c487f7
  glnx_autofd int srcfd = -1;
rpm-build c487f7
  glnx_autofd int destfd = -1;
rpm-build c487f7
  struct stat stbuf;
rpm-build c487f7
rpm-build c487f7
  if (!renameat_test_setup (&srcfd, &destfd, error))
rpm-build c487f7
    return;
rpm-build c487f7
rpm-build c487f7
  if (glnx_renameat2_noreplace (srcfd, "foo", destfd, "bar") == 0)
rpm-build c487f7
    g_assert_not_reached ();
rpm-build c487f7
  else
rpm-build c487f7
    {
rpm-build c487f7
      g_assert_cmpint (errno, ==, EEXIST);
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  if (glnx_renameat2_noreplace (srcfd, "foo", destfd, "baz") < 0)
rpm-build c487f7
    return (void)glnx_throw_errno_prefix (error, "renameat");
rpm-build c487f7
  if (!glnx_fstatat (destfd, "bar", &stbuf, AT_SYMLINK_NOFOLLOW, error))
rpm-build c487f7
    return;
rpm-build c487f7
rpm-build c487f7
  if (fstatat (srcfd, "foo", &stbuf, AT_SYMLINK_NOFOLLOW) == 0)
rpm-build c487f7
    g_assert_not_reached ();
rpm-build c487f7
  else
rpm-build c487f7
    g_assert_cmpint (errno, ==, ENOENT);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
test_renameat2_exchange (void)
rpm-build c487f7
{
rpm-build c487f7
  _GLNX_TEST_DECLARE_ERROR(local_error, error);
rpm-build c487f7
rpm-build c487f7
  glnx_autofd int srcfd = -1;
rpm-build c487f7
  glnx_autofd int destfd = -1;
rpm-build c487f7
  if (!renameat_test_setup (&srcfd, &destfd, error))
rpm-build c487f7
    return;
rpm-build c487f7
rpm-build c487f7
  if (glnx_renameat2_exchange (AT_FDCWD, "srcdir", AT_FDCWD, "destdir") < 0)
rpm-build c487f7
    return (void)glnx_throw_errno_prefix (error, "renameat");
rpm-build c487f7
rpm-build c487f7
  /* Ensure the dir fds are the same */
rpm-build c487f7
  struct stat stbuf;
rpm-build c487f7
  if (!glnx_fstatat (srcfd, "foo", &stbuf, AT_SYMLINK_NOFOLLOW, error))
rpm-build c487f7
    return;
rpm-build c487f7
  if (!glnx_fstatat (destfd, "bar", &stbuf, AT_SYMLINK_NOFOLLOW, error))
rpm-build c487f7
    return;
rpm-build c487f7
  /* But the dirs should be swapped */
rpm-build c487f7
  if (!glnx_fstatat (AT_FDCWD, "destdir/foo", &stbuf, AT_SYMLINK_NOFOLLOW, error))
rpm-build c487f7
    return;
rpm-build c487f7
  if (!glnx_fstatat (AT_FDCWD, "srcdir/bar", &stbuf, AT_SYMLINK_NOFOLLOW, error))
rpm-build c487f7
    return;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
test_tmpfile (void)
rpm-build c487f7
{
rpm-build c487f7
  _GLNX_TEST_DECLARE_ERROR(local_error, error);
rpm-build c487f7
rpm-build c487f7
  g_auto(GLnxTmpfile) tmpf = { 0, };
rpm-build c487f7
  if (!glnx_open_tmpfile_linkable_at (AT_FDCWD, ".", O_WRONLY|O_CLOEXEC, &tmpf, error))
rpm-build c487f7
    return;
rpm-build c487f7
  if (glnx_loop_write (tmpf.fd, "foo", strlen ("foo")) < 0)
rpm-build c487f7
    return (void)glnx_throw_errno_prefix (error, "write");
rpm-build c487f7
  if (glnx_link_tmpfile_at (&tmpf, GLNX_LINK_TMPFILE_NOREPLACE, AT_FDCWD, "foo", error))
rpm-build c487f7
    return;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
test_stdio_file (void)
rpm-build c487f7
{
rpm-build c487f7
  _GLNX_TEST_DECLARE_ERROR(local_error, error);
rpm-build c487f7
  g_auto(GLnxTmpfile) tmpf = { 0, };
rpm-build c487f7
  g_autoptr(FILE) f = NULL;
rpm-build c487f7
rpm-build c487f7
  if (!glnx_open_anonymous_tmpfile (O_RDWR|O_CLOEXEC, &tmpf, error))
rpm-build c487f7
    return;
rpm-build c487f7
  f = fdopen (tmpf.fd, "w");
rpm-build c487f7
  tmpf.fd = -1; /* Ownership was transferred via fdopen() */
rpm-build c487f7
  if (!f)
rpm-build c487f7
    return (void)glnx_throw_errno_prefix (error, "fdopen");
rpm-build c487f7
  if (fwrite ("hello", 1, strlen ("hello"), f) != strlen ("hello"))
rpm-build c487f7
    return (void)glnx_throw_errno_prefix (error, "fwrite");
rpm-build c487f7
  if (!glnx_stdio_file_flush (f, error))
rpm-build c487f7
    return;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
test_fstatat (void)
rpm-build c487f7
{
rpm-build c487f7
  _GLNX_TEST_DECLARE_ERROR(local_error, error);
rpm-build c487f7
  struct stat stbuf = { 0, };
rpm-build c487f7
rpm-build c487f7
  if (!glnx_fstatat_allow_noent (AT_FDCWD, ".", &stbuf, 0, error))
rpm-build c487f7
    return;
rpm-build c487f7
  g_assert_cmpint (errno, ==, 0);
rpm-build c487f7
  g_assert_no_error (local_error);
rpm-build c487f7
  g_assert (S_ISDIR (stbuf.st_mode));
rpm-build c487f7
  if (!glnx_fstatat_allow_noent (AT_FDCWD, "nosuchfile", &stbuf, 0, error))
rpm-build c487f7
    return;
rpm-build c487f7
  g_assert_cmpint (errno, ==, ENOENT);
rpm-build c487f7
  g_assert_no_error (local_error);
rpm-build c487f7
rpm-build c487f7
  /* test NULL parameter for stat */
rpm-build c487f7
  if (!glnx_fstatat_allow_noent (AT_FDCWD, ".", NULL, 0, error))
rpm-build c487f7
    return;
rpm-build c487f7
  g_assert_cmpint (errno, ==, 0);
rpm-build c487f7
  g_assert_no_error (local_error);
rpm-build c487f7
  if (!glnx_fstatat_allow_noent (AT_FDCWD, "nosuchfile", NULL, 0, error))
rpm-build c487f7
    return;
rpm-build c487f7
  g_assert_cmpint (errno, ==, ENOENT);
rpm-build c487f7
  g_assert_no_error (local_error);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
test_filecopy (void)
rpm-build c487f7
{
rpm-build c487f7
  _GLNX_TEST_DECLARE_ERROR(local_error, error);
rpm-build c487f7
  const char foo[] = "foo";
rpm-build c487f7
  struct stat stbuf;
rpm-build c487f7
rpm-build c487f7
  if (!glnx_ensure_dir (AT_FDCWD, "subdir", 0755, error))
rpm-build c487f7
    return;
rpm-build c487f7
rpm-build c487f7
  if (!glnx_file_replace_contents_at (AT_FDCWD, foo, (guint8*)foo, sizeof (foo),
rpm-build c487f7
                                      GLNX_FILE_REPLACE_NODATASYNC, NULL, error))
rpm-build c487f7
    return;
rpm-build c487f7
rpm-build c487f7
  /* Copy it into both the same dir and a subdir */
rpm-build c487f7
  if (!glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "bar",
rpm-build c487f7
                          GLNX_FILE_COPY_NOXATTRS, NULL, error))
rpm-build c487f7
    return;
rpm-build c487f7
  if (!glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "subdir/bar",
rpm-build c487f7
                          GLNX_FILE_COPY_NOXATTRS, NULL, error))
rpm-build c487f7
    return;
rpm-build c487f7
  if (!glnx_fstatat (AT_FDCWD, "subdir/bar", &stbuf, 0, error))
rpm-build c487f7
    return;
rpm-build c487f7
rpm-build c487f7
  if (glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "bar",
rpm-build c487f7
                         GLNX_FILE_COPY_NOXATTRS, NULL, error))
rpm-build c487f7
    g_assert_not_reached ();
rpm-build c487f7
  g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_EXISTS);
rpm-build c487f7
  g_clear_error (&local_error);
rpm-build c487f7
rpm-build c487f7
  if (!glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "bar",
rpm-build c487f7
                          GLNX_FILE_COPY_NOXATTRS | GLNX_FILE_COPY_OVERWRITE,
rpm-build c487f7
                          NULL, error))
rpm-build c487f7
    return;
rpm-build c487f7
rpm-build c487f7
  if (symlinkat ("nosuchtarget", AT_FDCWD, "link") < 0)
rpm-build c487f7
    return (void) glnx_throw_errno_prefix (error, "symlinkat");
rpm-build c487f7
rpm-build c487f7
  /* Shouldn't be able to overwrite a symlink without GLNX_FILE_COPY_OVERWRITE */
rpm-build c487f7
  if (glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "link",
rpm-build c487f7
                         GLNX_FILE_COPY_NOXATTRS,
rpm-build c487f7
                         NULL, error))
rpm-build c487f7
    g_assert_not_reached ();
rpm-build c487f7
  g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_EXISTS);
rpm-build c487f7
  g_clear_error (&local_error);
rpm-build c487f7
rpm-build c487f7
  /* Test overwriting symlink */
rpm-build c487f7
  if (!glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "link",
rpm-build c487f7
                          GLNX_FILE_COPY_NOXATTRS | GLNX_FILE_COPY_OVERWRITE,
rpm-build c487f7
                          NULL, error))
rpm-build c487f7
    return;
rpm-build c487f7
rpm-build c487f7
  if (!glnx_fstatat_allow_noent (AT_FDCWD, "nosuchtarget", &stbuf, AT_SYMLINK_NOFOLLOW, error))
rpm-build c487f7
    return;
rpm-build c487f7
  g_assert_cmpint (errno, ==, ENOENT);
rpm-build c487f7
  g_assert_no_error (local_error);
rpm-build c487f7
rpm-build c487f7
  if (!glnx_fstatat (AT_FDCWD, "link", &stbuf, AT_SYMLINK_NOFOLLOW, error))
rpm-build c487f7
    return;
rpm-build c487f7
  g_assert (S_ISREG (stbuf.st_mode));
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
int main (int argc, char **argv)
rpm-build c487f7
{
rpm-build c487f7
  int ret;
rpm-build c487f7
rpm-build c487f7
  g_test_init (&argc, &argv, NULL);
rpm-build c487f7
rpm-build c487f7
  g_test_add_func ("/tmpfile", test_tmpfile);
rpm-build c487f7
  g_test_add_func ("/stdio-file", test_stdio_file);
rpm-build c487f7
  g_test_add_func ("/filecopy", test_filecopy);
rpm-build c487f7
  g_test_add_func ("/renameat2-noreplace", test_renameat2_noreplace);
rpm-build c487f7
  g_test_add_func ("/renameat2-exchange", test_renameat2_exchange);
rpm-build c487f7
  g_test_add_func ("/fstat", test_fstatat);
rpm-build c487f7
rpm-build c487f7
  ret = g_test_run();
rpm-build c487f7
rpm-build c487f7
  return ret;
rpm-build c487f7
}