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

rpm-build 0fba15
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
rpm-build 0fba15
 *
rpm-build 0fba15
 * Copyright (C) 2017 Red Hat, Inc.
rpm-build 0fba15
 *
rpm-build 0fba15
 * This library is free software; you can redistribute it and/or
rpm-build 0fba15
 * modify it under the terms of the GNU Lesser General Public
rpm-build 0fba15
 * License as published by the Free Software Foundation; either
rpm-build 0fba15
 * version 2 of the License, or (at your option) any later version.
rpm-build 0fba15
 *
rpm-build 0fba15
 * This library is distributed in the hope that it will be useful,
rpm-build 0fba15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build 0fba15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build 0fba15
 * Lesser General Public License for more details.
rpm-build 0fba15
 *
rpm-build 0fba15
 * You should have received a copy of the GNU Lesser General Public
rpm-build 0fba15
 * License along with this library; if not, write to the
rpm-build 0fba15
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
rpm-build 0fba15
 * Boston, MA 02111-1307, USA.
rpm-build 0fba15
 */
rpm-build 0fba15
rpm-build 0fba15
#include "config.h"
rpm-build 0fba15
#include "libglnx.h"
rpm-build 0fba15
#include <glib.h>
rpm-build 0fba15
#include <stdlib.h>
rpm-build 0fba15
#include <gio/gio.h>
rpm-build 0fba15
#include <string.h>
rpm-build 0fba15
rpm-build 0fba15
static void
rpm-build 0fba15
test_error_throw (void)
rpm-build 0fba15
{
rpm-build 0fba15
  g_autoptr(GError) error = NULL;
rpm-build 0fba15
rpm-build 0fba15
  g_assert (!glnx_throw (&error, "foo: %s %d", "hello", 42));
rpm-build 0fba15
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
rpm-build 0fba15
  g_assert_cmpstr (error->message, ==, "foo: hello 42");
rpm-build 0fba15
  g_clear_error (&error);
rpm-build 0fba15
rpm-build 0fba15
  gpointer dummy = glnx_null_throw (&error, "literal foo");
rpm-build 0fba15
  g_assert (dummy == NULL);
rpm-build 0fba15
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
rpm-build 0fba15
  g_assert_cmpstr (error->message, ==, "literal foo");
rpm-build 0fba15
  g_clear_error (&error);
rpm-build 0fba15
rpm-build 0fba15
  gpointer dummy2 = glnx_null_throw (&error, "foo: %s %d", "hola", 24);
rpm-build 0fba15
  g_assert (dummy2 == NULL);
rpm-build 0fba15
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
rpm-build 0fba15
  g_assert_cmpstr (error->message, ==, "foo: hola 24");
rpm-build 0fba15
  g_clear_error (&error);
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
static void
rpm-build 0fba15
test_error_errno (void)
rpm-build 0fba15
{
rpm-build 0fba15
  g_autoptr(GError) error = NULL;
rpm-build 0fba15
  const char noent_path[] = "/enoent-this-should-not-exist";
rpm-build 0fba15
  int fd;
rpm-build 0fba15
rpm-build 0fba15
  fd = open (noent_path, O_RDONLY);
rpm-build 0fba15
  if (fd < 0)
rpm-build 0fba15
    {
rpm-build 0fba15
      g_assert (!glnx_throw_errno (&error));
rpm-build 0fba15
      g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
rpm-build 0fba15
      g_assert (!glnx_prefix_error (&error, "myprefix"));
rpm-build 0fba15
      g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
rpm-build 0fba15
      g_assert (g_str_has_prefix (error->message, "myprefix: "));
rpm-build 0fba15
      g_clear_error (&error);
rpm-build 0fba15
    }
rpm-build 0fba15
  else
rpm-build 0fba15
    g_assert_cmpint (fd, ==, -1);
rpm-build 0fba15
rpm-build 0fba15
  fd = open (noent_path, O_RDONLY);
rpm-build 0fba15
  if (fd < 0)
rpm-build 0fba15
    {
rpm-build 0fba15
      gpointer dummy = glnx_null_throw_errno (&error);
rpm-build 0fba15
      g_assert (dummy == NULL);
rpm-build 0fba15
      g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
rpm-build 0fba15
      dummy = glnx_prefix_error_null (&error, "myprefix");
rpm-build 0fba15
      g_assert (dummy == NULL);
rpm-build 0fba15
      g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
rpm-build 0fba15
      g_assert (g_str_has_prefix (error->message, "myprefix: "));
rpm-build 0fba15
      g_clear_error (&error);
rpm-build 0fba15
    }
rpm-build 0fba15
  else
rpm-build 0fba15
    g_assert_cmpint (fd, ==, -1);
rpm-build 0fba15
rpm-build 0fba15
  fd = open (noent_path, O_RDONLY);
rpm-build 0fba15
  if (fd < 0)
rpm-build 0fba15
    {
rpm-build 0fba15
      g_autofree char *expected_prefix = g_strdup_printf ("Failed to open %s", noent_path);
rpm-build 0fba15
      g_assert (!glnx_throw_errno_prefix (&error, "Failed to open %s", noent_path));
rpm-build 0fba15
      g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
rpm-build 0fba15
      g_assert (g_str_has_prefix (error->message, expected_prefix));
rpm-build 0fba15
      g_clear_error (&error);
rpm-build 0fba15
      /* And test the legacy wrapper */
rpm-build 0fba15
      glnx_set_prefix_error_from_errno (&error, "Failed to open %s", noent_path);
rpm-build 0fba15
      g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
rpm-build 0fba15
      g_assert (g_str_has_prefix (error->message, expected_prefix));
rpm-build 0fba15
      g_clear_error (&error);
rpm-build 0fba15
    }
rpm-build 0fba15
  else
rpm-build 0fba15
    g_assert_cmpint (fd, ==, -1);
rpm-build 0fba15
rpm-build 0fba15
  fd = open (noent_path, O_RDONLY);
rpm-build 0fba15
  if (fd < 0)
rpm-build 0fba15
    {
rpm-build 0fba15
      gpointer dummy = glnx_null_throw_errno_prefix (&error, "Failed to open file");
rpm-build 0fba15
      g_assert (dummy == NULL);
rpm-build 0fba15
      g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
rpm-build 0fba15
      g_assert (g_str_has_prefix (error->message, "Failed to open file"));
rpm-build 0fba15
      g_clear_error (&error);
rpm-build 0fba15
    }
rpm-build 0fba15
  else
rpm-build 0fba15
    g_assert_cmpint (fd, ==, -1);
rpm-build 0fba15
rpm-build 0fba15
  fd = open (noent_path, O_RDONLY);
rpm-build 0fba15
  if (fd < 0)
rpm-build 0fba15
    {
rpm-build 0fba15
      gpointer dummy = glnx_null_throw_errno_prefix (&error, "Failed to open %s", noent_path);
rpm-build 0fba15
      g_assert (dummy == NULL);
rpm-build 0fba15
      g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
rpm-build 0fba15
      g_assert (g_str_has_prefix (error->message, glnx_strjoina ("Failed to open ", noent_path)));
rpm-build 0fba15
      g_clear_error (&error);
rpm-build 0fba15
    }
rpm-build 0fba15
  else
rpm-build 0fba15
    g_assert_cmpint (fd, ==, -1);
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
static void
rpm-build 0fba15
test_error_auto_nothrow (GError **error)
rpm-build 0fba15
{
rpm-build 0fba15
  GLNX_AUTO_PREFIX_ERROR("foo", error);
rpm-build 0fba15
  /* Side effect to avoid otherwise empty function */
rpm-build 0fba15
  g_assert_no_error (*error);
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
static void
rpm-build 0fba15
test_error_auto_throw (GError **error)
rpm-build 0fba15
{
rpm-build 0fba15
  GLNX_AUTO_PREFIX_ERROR("foo", error);
rpm-build 0fba15
  (void) glnx_throw (error, "oops");
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
static void
rpm-build 0fba15
test_error_auto_throw_recurse (GError **error)
rpm-build 0fba15
{
rpm-build 0fba15
  GLNX_AUTO_PREFIX_ERROR("foo", error);
rpm-build 0fba15
rpm-build 0fba15
  if (TRUE)
rpm-build 0fba15
    {
rpm-build 0fba15
      GLNX_AUTO_PREFIX_ERROR("bar", error);
rpm-build 0fba15
      (void) glnx_throw (error, "oops");
rpm-build 0fba15
    }
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
static void
rpm-build 0fba15
test_error_auto (void)
rpm-build 0fba15
{
rpm-build 0fba15
  g_autoptr(GError) error = NULL;
rpm-build 0fba15
  test_error_auto_nothrow (&error);
rpm-build 0fba15
  g_assert_no_error (error);
rpm-build 0fba15
  test_error_auto_throw (&error);
rpm-build 0fba15
  g_assert_nonnull (error);
rpm-build 0fba15
  g_assert_cmpstr (error->message, ==, "foo: oops");
rpm-build 0fba15
  g_clear_error (&error);
rpm-build 0fba15
  test_error_auto_throw_recurse (&error);
rpm-build 0fba15
  g_assert_nonnull (error);
rpm-build 0fba15
  g_assert_cmpstr (error->message, ==, "foo: bar: oops");
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
int main (int argc, char **argv)
rpm-build 0fba15
{
rpm-build 0fba15
  int ret;
rpm-build 0fba15
rpm-build 0fba15
  g_test_init (&argc, &argv, NULL);
rpm-build 0fba15
rpm-build 0fba15
  g_test_add_func ("/error-throw", test_error_throw);
rpm-build 0fba15
  g_test_add_func ("/error-errno", test_error_errno);
rpm-build 0fba15
  g_test_add_func ("/error-auto", test_error_auto);
rpm-build 0fba15
rpm-build 0fba15
  ret = g_test_run();
rpm-build 0fba15
rpm-build 0fba15
  return ret;
rpm-build 0fba15
}