Blame tests/libostreetest.c

Packit Service 2a3f3d
/*
Packit Service 2a3f3d
 * Copyright (C) 2015 Red Hat, Inc.
Packit Service 2a3f3d
 *
Packit Service 2a3f3d
 * SPDX-License-Identifier: LGPL-2.0+
Packit Service 2a3f3d
 *
Packit Service 2a3f3d
 * This library is free software; you can redistribute it and/or
Packit Service 2a3f3d
 * modify it under the terms of the GNU Lesser General Public
Packit Service 2a3f3d
 * License as published by the Free Software Foundation; either
Packit Service 2a3f3d
 * version 2 of the License, or (at your option) any later version.
Packit Service 2a3f3d
 *
Packit Service 2a3f3d
 * This library is distributed in the hope that it will be useful,
Packit Service 2a3f3d
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 2a3f3d
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 2a3f3d
 * Lesser General Public License for more details.
Packit Service 2a3f3d
 *
Packit Service 2a3f3d
 * You should have received a copy of the GNU Lesser General Public
Packit Service 2a3f3d
 * License along with this library; if not, write to the
Packit Service 2a3f3d
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit Service 2a3f3d
 * Boston, MA 02111-1307, USA.
Packit Service 2a3f3d
 */
Packit Service 2a3f3d
Packit Service 2a3f3d
#include "config.h"
Packit Service 2a3f3d
#include <stdlib.h>
Packit Service 2a3f3d
#include <string.h>
Packit Service 2a3f3d
Packit Service 2a3f3d
#include "libglnx.h"
Packit Service 2a3f3d
#include "libostreetest.h"
Packit Service 2a3f3d
Packit Service 2a3f3d
/* This function hovers in a quantum superposition of horrifying and
Packit Service 2a3f3d
 * beautiful.  Future generations may interpret it as modern art.
Packit Service 2a3f3d
 */
Packit Service 2a3f3d
gboolean
Packit Service 2a3f3d
ot_test_run_libtest (const char *cmd, GError **error)
Packit Service 2a3f3d
{
Packit Service 2a3f3d
  const char *srcdir = g_getenv ("G_TEST_SRCDIR");
Packit Service 2a3f3d
  g_autoptr(GPtrArray) argv = g_ptr_array_new ();
Packit Service 2a3f3d
  g_autoptr(GString) cmdstr = g_string_new ("");
Packit Service 2a3f3d
Packit Service 2a3f3d
  g_ptr_array_add (argv, "bash");
Packit Service 2a3f3d
  g_ptr_array_add (argv, "-c");
Packit Service 2a3f3d
Packit Service 2a3f3d
  g_string_append (cmdstr, "set -xeuo pipefail; . ");
Packit Service 2a3f3d
  g_string_append (cmdstr, srcdir);
Packit Service 2a3f3d
  g_string_append (cmdstr, "/tests/libtest.sh; ");
Packit Service 2a3f3d
  g_string_append (cmdstr, cmd);
Packit Service 2a3f3d
Packit Service 2a3f3d
  g_ptr_array_add (argv, cmdstr->str);
Packit Service 2a3f3d
  g_ptr_array_add (argv, NULL);
Packit Service 2a3f3d
Packit Service 2a3f3d
  int estatus;
Packit Service 2a3f3d
  if (!g_spawn_sync (NULL, (char**)argv->pdata, NULL, G_SPAWN_SEARCH_PATH,
Packit Service 2a3f3d
                     NULL, NULL, NULL, NULL, &estatus, error))
Packit Service 2a3f3d
    return FALSE;
Packit Service 2a3f3d
  if (!g_spawn_check_exit_status (estatus, error))
Packit Service 2a3f3d
    return FALSE;
Packit Service 2a3f3d
Packit Service 2a3f3d
  return TRUE;
Packit Service 2a3f3d
}
Packit Service 2a3f3d
Packit Service 2a3f3d
OstreeRepo *
Packit Service 2a3f3d
ot_test_setup_repo (GCancellable *cancellable,
Packit Service 2a3f3d
                    GError **error)
Packit Service 2a3f3d
{
Packit Service 2a3f3d
  if (!ot_test_run_libtest ("setup_test_repository archive", error))
Packit Service 2a3f3d
    return NULL;
Packit Service 2a3f3d
Packit Service 2a3f3d
  g_autoptr(GFile) repo_path = g_file_new_for_path ("repo");
Packit Service 2a3f3d
  g_autoptr(OstreeRepo) ret_repo = ostree_repo_new (repo_path);
Packit Service 2a3f3d
  if (!ostree_repo_open (ret_repo, cancellable, error))
Packit Service 2a3f3d
    return NULL;
Packit Service 2a3f3d
Packit Service 2a3f3d
  return g_steal_pointer (&ret_repo);
Packit Service 2a3f3d
}
Packit Service 2a3f3d
Packit Service 2a3f3d
/* Determine whether we're able to relabel files. Needed for bare tests. */
Packit Service 2a3f3d
gboolean
Packit Service 2a3f3d
ot_check_relabeling (gboolean *can_relabel,
Packit Service 2a3f3d
                     GError  **error)
Packit Service 2a3f3d
{
Packit Service 2a3f3d
  g_auto(GLnxTmpfile) tmpf = { 0, };
Packit Service 2a3f3d
  if (!glnx_open_tmpfile_linkable_at (AT_FDCWD, ".", O_RDWR | O_CLOEXEC, &tmpf, error))
Packit Service 2a3f3d
    return FALSE;
Packit Service 2a3f3d
Packit Service 2a3f3d
  g_autoptr(GError) local_error = NULL;
Packit Service 2a3f3d
  g_autoptr(GBytes) bytes = glnx_fgetxattr_bytes (tmpf.fd, "security.selinux", &local_error);
Packit Service 2a3f3d
  if (!bytes)
Packit Service 2a3f3d
    {
Packit Service 2a3f3d
      /* libglnx preserves errno. The EOPNOTSUPP case can't be part of a
Packit Service 2a3f3d
       * 'case' statement because on most but not all architectures,
Packit Service 2a3f3d
       * it's numerically equal to ENOTSUP. */
Packit Service 2a3f3d
      if (G_IN_SET (errno, ENOTSUP, ENODATA) || errno == EOPNOTSUPP)
Packit Service 2a3f3d
        {
Packit Service 2a3f3d
          *can_relabel = FALSE;
Packit Service 2a3f3d
          return TRUE;
Packit Service 2a3f3d
        }
Packit Service 2a3f3d
      g_propagate_error (error, g_steal_pointer (&local_error));
Packit Service 2a3f3d
      return FALSE;
Packit Service 2a3f3d
    }
Packit Service 2a3f3d
Packit Service 2a3f3d
  gsize data_len;
Packit Service 2a3f3d
  const guint8 *data = g_bytes_get_data (bytes, &data_len);
Packit Service 2a3f3d
  if (fsetxattr (tmpf.fd, "security.selinux", data, data_len, 0) < 0)
Packit Service 2a3f3d
    {
Packit Service 2a3f3d
      if (errno == ENOTSUP || errno == EOPNOTSUPP)
Packit Service 2a3f3d
        {
Packit Service 2a3f3d
          *can_relabel = FALSE;
Packit Service 2a3f3d
          return TRUE;
Packit Service 2a3f3d
        }
Packit Service 2a3f3d
      return glnx_throw_errno_prefix (error, "fsetxattr");
Packit Service 2a3f3d
    }
Packit Service 2a3f3d
Packit Service 2a3f3d
  *can_relabel = TRUE;
Packit Service 2a3f3d
  return TRUE;
Packit Service 2a3f3d
}
Packit Service 2a3f3d
Packit Service 2a3f3d
/* Determine whether the filesystem supports getting/setting user xattrs. */
Packit Service 2a3f3d
gboolean
Packit Service 2a3f3d
ot_check_user_xattrs (gboolean *has_user_xattrs,
Packit Service 2a3f3d
                      GError  **error)
Packit Service 2a3f3d
{
Packit Service 2a3f3d
  g_auto(GLnxTmpfile) tmpf = { 0, };
Packit Service 2a3f3d
  if (!glnx_open_tmpfile_linkable_at (AT_FDCWD, ".", O_RDWR | O_CLOEXEC, &tmpf, error))
Packit Service 2a3f3d
    return FALSE;
Packit Service 2a3f3d
Packit Service 2a3f3d
  if (fsetxattr (tmpf.fd, "user.test", "novalue", strlen ("novalue"), 0) < 0)
Packit Service 2a3f3d
    {
Packit Service 2a3f3d
      if (errno == ENOTSUP || errno == EOPNOTSUPP)
Packit Service 2a3f3d
        {
Packit Service 2a3f3d
          *has_user_xattrs = FALSE;
Packit Service 2a3f3d
          return TRUE;
Packit Service 2a3f3d
        }
Packit Service 2a3f3d
      return glnx_throw_errno_prefix (error, "fsetxattr");
Packit Service 2a3f3d
    }
Packit Service 2a3f3d
Packit Service 2a3f3d
  *has_user_xattrs = TRUE;
Packit Service 2a3f3d
  return TRUE;
Packit Service 2a3f3d
}
Packit Service 2a3f3d
Packit Service 2a3f3d
OstreeSysroot *
Packit Service 2a3f3d
ot_test_setup_sysroot (GCancellable *cancellable,
Packit Service 2a3f3d
                       GError **error)
Packit Service 2a3f3d
{
Packit Service 2a3f3d
  if (!ot_test_run_libtest ("setup_os_repository \"archive\" \"syslinux\"", error))
Packit Service 2a3f3d
    return FALSE;
Packit Service 2a3f3d
Packit Service 2a3f3d
  g_autoptr(GString) buf = g_string_new ("mutable-deployments");
Packit Service 2a3f3d
Packit Service 2a3f3d
  gboolean can_relabel = FALSE;
Packit Service 2a3f3d
  if (!ot_check_relabeling (&can_relabel, error))
Packit Service 2a3f3d
    return FALSE;
Packit Service 2a3f3d
  if (!can_relabel)
Packit Service 2a3f3d
    {
Packit Service 2a3f3d
      g_print ("libostreetest: can't relabel, turning off xattrs\n");
Packit Service 2a3f3d
      g_string_append (buf, ",no-xattrs");
Packit Service 2a3f3d
    }
Packit Service 2a3f3d
Packit Service 2a3f3d
  /* Make sure deployments are mutable */
Packit Service 2a3f3d
  g_setenv ("OSTREE_SYSROOT_DEBUG", buf->str, TRUE);
Packit Service 2a3f3d
Packit Service 2a3f3d
  g_autoptr(GFile) sysroot_path = g_file_new_for_path ("sysroot");
Packit Service 2a3f3d
  return ostree_sysroot_new (sysroot_path);
Packit Service 2a3f3d
}