| 1 |
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2017 Red Hat, Inc. |
| 4 |
* SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the |
| 18 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 |
* Boston, MA 02111-1307, USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "libglnx-config.h" |
| 23 |
#include "libglnx.h" |
| 24 |
#include <glib.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <gio/gio.h> |
| 27 |
#include <string.h> |
| 28 |
|
| 29 |
static void |
| 30 |
test_error_throw (void) |
| 31 |
{ |
| 32 |
g_autoptr(GError) error = NULL; |
| 33 |
|
| 34 |
g_assert (!glnx_throw (&error, "foo: %s %d", "hello", 42)); |
| 35 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED); |
| 36 |
g_assert_cmpstr (error->message, ==, "foo: hello 42"); |
| 37 |
g_clear_error (&error); |
| 38 |
|
| 39 |
gpointer dummy = glnx_null_throw (&error, "literal foo"); |
| 40 |
g_assert (dummy == NULL); |
| 41 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED); |
| 42 |
g_assert_cmpstr (error->message, ==, "literal foo"); |
| 43 |
g_clear_error (&error); |
| 44 |
|
| 45 |
gpointer dummy2 = glnx_null_throw (&error, "foo: %s %d", "hola", 24); |
| 46 |
g_assert (dummy2 == NULL); |
| 47 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED); |
| 48 |
g_assert_cmpstr (error->message, ==, "foo: hola 24"); |
| 49 |
g_clear_error (&error); |
| 50 |
} |
| 51 |
|
| 52 |
static void |
| 53 |
test_error_errno (void) |
| 54 |
{ |
| 55 |
g_autoptr(GError) error = NULL; |
| 56 |
const char noent_path[] = "/enoent-this-should-not-exist"; |
| 57 |
int fd; |
| 58 |
|
| 59 |
fd = open (noent_path, O_RDONLY); |
| 60 |
if (fd < 0) |
| 61 |
{ |
| 62 |
g_assert (!glnx_throw_errno (&error)); |
| 63 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND); |
| 64 |
g_assert (!glnx_prefix_error (&error, "myprefix")); |
| 65 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND); |
| 66 |
g_assert (g_str_has_prefix (error->message, "myprefix: ")); |
| 67 |
g_clear_error (&error); |
| 68 |
} |
| 69 |
else |
| 70 |
g_assert_cmpint (fd, ==, -1); |
| 71 |
|
| 72 |
fd = open (noent_path, O_RDONLY); |
| 73 |
if (fd < 0) |
| 74 |
{ |
| 75 |
gpointer dummy = glnx_null_throw_errno (&error); |
| 76 |
g_assert (dummy == NULL); |
| 77 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND); |
| 78 |
dummy = glnx_prefix_error_null (&error, "myprefix"); |
| 79 |
g_assert (dummy == NULL); |
| 80 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND); |
| 81 |
g_assert (g_str_has_prefix (error->message, "myprefix: ")); |
| 82 |
g_clear_error (&error); |
| 83 |
} |
| 84 |
else |
| 85 |
g_assert_cmpint (fd, ==, -1); |
| 86 |
|
| 87 |
fd = open (noent_path, O_RDONLY); |
| 88 |
if (fd < 0) |
| 89 |
{ |
| 90 |
g_autofree char *expected_prefix = g_strdup_printf ("Failed to open %s", noent_path); |
| 91 |
g_assert (!glnx_throw_errno_prefix (&error, "Failed to open %s", noent_path)); |
| 92 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND); |
| 93 |
g_assert (g_str_has_prefix (error->message, expected_prefix)); |
| 94 |
g_clear_error (&error); |
| 95 |
/* And test the legacy wrapper */ |
| 96 |
glnx_set_prefix_error_from_errno (&error, "Failed to open %s", noent_path); |
| 97 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND); |
| 98 |
g_assert (g_str_has_prefix (error->message, expected_prefix)); |
| 99 |
g_clear_error (&error); |
| 100 |
} |
| 101 |
else |
| 102 |
g_assert_cmpint (fd, ==, -1); |
| 103 |
|
| 104 |
fd = open (noent_path, O_RDONLY); |
| 105 |
if (fd < 0) |
| 106 |
{ |
| 107 |
gpointer dummy = glnx_null_throw_errno_prefix (&error, "Failed to open file"); |
| 108 |
g_assert (dummy == NULL); |
| 109 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND); |
| 110 |
g_assert (g_str_has_prefix (error->message, "Failed to open file")); |
| 111 |
g_clear_error (&error); |
| 112 |
} |
| 113 |
else |
| 114 |
g_assert_cmpint (fd, ==, -1); |
| 115 |
|
| 116 |
fd = open (noent_path, O_RDONLY); |
| 117 |
if (fd < 0) |
| 118 |
{ |
| 119 |
gpointer dummy = glnx_null_throw_errno_prefix (&error, "Failed to open %s", noent_path); |
| 120 |
g_assert (dummy == NULL); |
| 121 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND); |
| 122 |
g_assert (g_str_has_prefix (error->message, glnx_strjoina ("Failed to open ", noent_path))); |
| 123 |
g_clear_error (&error); |
| 124 |
} |
| 125 |
else |
| 126 |
g_assert_cmpint (fd, ==, -1); |
| 127 |
} |
| 128 |
|
| 129 |
static void |
| 130 |
test_error_auto_nothrow (GError **error) |
| 131 |
{ |
| 132 |
GLNX_AUTO_PREFIX_ERROR("foo", error); |
| 133 |
/* Side effect to avoid otherwise empty function */ |
| 134 |
g_assert_no_error (*error); |
| 135 |
} |
| 136 |
|
| 137 |
static void |
| 138 |
test_error_auto_throw (GError **error) |
| 139 |
{ |
| 140 |
GLNX_AUTO_PREFIX_ERROR("foo", error); |
| 141 |
(void) glnx_throw (error, "oops"); |
| 142 |
} |
| 143 |
|
| 144 |
static void |
| 145 |
test_error_auto_throw_recurse (GError **error) |
| 146 |
{ |
| 147 |
GLNX_AUTO_PREFIX_ERROR("foo", error); |
| 148 |
|
| 149 |
if (TRUE) |
| 150 |
{ |
| 151 |
GLNX_AUTO_PREFIX_ERROR("bar", error); |
| 152 |
(void) glnx_throw (error, "oops"); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
static void |
| 157 |
test_error_auto (void) |
| 158 |
{ |
| 159 |
g_autoptr(GError) error = NULL; |
| 160 |
test_error_auto_nothrow (&error); |
| 161 |
g_assert_no_error (error); |
| 162 |
test_error_auto_throw (&error); |
| 163 |
g_assert_nonnull (error); |
| 164 |
g_assert_cmpstr (error->message, ==, "foo: oops"); |
| 165 |
g_clear_error (&error); |
| 166 |
test_error_auto_throw_recurse (&error); |
| 167 |
g_assert_nonnull (error); |
| 168 |
g_assert_cmpstr (error->message, ==, "foo: bar: oops"); |
| 169 |
} |
| 170 |
|
| 171 |
int main (int argc, char **argv) |
| 172 |
{ |
| 173 |
int ret; |
| 174 |
|
| 175 |
g_test_init (&argc, &argv, NULL); |
| 176 |
|
| 177 |
g_test_add_func ("/error-throw", test_error_throw); |
| 178 |
g_test_add_func ("/error-errno", test_error_errno); |
| 179 |
g_test_add_func ("/error-auto", test_error_auto); |
| 180 |
|
| 181 |
ret = g_test_run(); |
| 182 |
|
| 183 |
return ret; |
| 184 |
} |