gsubprocess: test environment a bit more
[platform/upstream/glib.git] / gio / tests / gdbus-error.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: David Zeuthen <davidz@redhat.com>
19  */
20
21 #include <gio/gio.h>
22 #include <unistd.h>
23 #include <string.h>
24
25 /* ---------------------------------------------------------------------------------------------------- */
26 /* Test that registered errors are properly mapped */
27 /* ---------------------------------------------------------------------------------------------------- */
28
29 static void
30 check_registered_error (const gchar *given_dbus_error_name,
31                         GQuark       error_domain,
32                         gint         error_code)
33 {
34   GError *error;
35   gchar *dbus_error_name;
36
37   error = g_dbus_error_new_for_dbus_error (given_dbus_error_name, "test message");
38   g_assert_error (error, error_domain, error_code);
39   g_assert (g_dbus_error_is_remote_error (error));
40   g_assert (g_dbus_error_strip_remote_error (error));
41   g_assert_cmpstr (error->message, ==, "test message");
42   dbus_error_name = g_dbus_error_get_remote_error (error);
43   g_assert_cmpstr (dbus_error_name, ==, given_dbus_error_name);
44   g_free (dbus_error_name);
45   g_error_free (error);
46 }
47
48 static void
49 test_registered_errors (void)
50 {
51   /* Here we check that we are able to map to GError and back for registered
52    * errors.
53    *
54    * For example, if "org.freedesktop.DBus.Error.AddressInUse" is
55    * associated with (G_DBUS_ERROR, G_DBUS_ERROR_DBUS_FAILED), check
56    * that
57    *
58    *  - Creating a GError for e.g. "org.freedesktop.DBus.Error.AddressInUse"
59    *    has (error_domain, code) == (G_DBUS_ERROR, G_DBUS_ERROR_DBUS_FAILED)
60    *
61    *  - That it is possible to recover e.g. "org.freedesktop.DBus.Error.AddressInUse"
62    *    as the D-Bus error name when dealing with an error with (error_domain, code) ==
63    *    (G_DBUS_ERROR, G_DBUS_ERROR_DBUS_FAILED)
64    *
65    * We just check a couple of well-known errors.
66    */
67   check_registered_error ("org.freedesktop.DBus.Error.Failed",
68                           G_DBUS_ERROR,
69                           G_DBUS_ERROR_FAILED);
70   check_registered_error ("org.freedesktop.DBus.Error.AddressInUse",
71                           G_DBUS_ERROR,
72                           G_DBUS_ERROR_ADDRESS_IN_USE);
73   check_registered_error ("org.freedesktop.DBus.Error.UnknownMethod",
74                           G_DBUS_ERROR,
75                           G_DBUS_ERROR_UNKNOWN_METHOD);
76 }
77
78 /* ---------------------------------------------------------------------------------------------------- */
79
80 static void
81 check_unregistered_error (const gchar *given_dbus_error_name)
82 {
83   GError *error;
84   gchar *dbus_error_name;
85
86   error = g_dbus_error_new_for_dbus_error (given_dbus_error_name, "test message");
87   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
88   g_assert (g_dbus_error_is_remote_error (error));
89   dbus_error_name = g_dbus_error_get_remote_error (error);
90   g_assert_cmpstr (dbus_error_name, ==, given_dbus_error_name);
91   g_free (dbus_error_name);
92
93   /* strip the message */
94   g_assert (g_dbus_error_strip_remote_error (error));
95   g_assert_cmpstr (error->message, ==, "test message");
96
97   /* check that we can no longer recover the D-Bus error name */
98   g_assert (g_dbus_error_get_remote_error (error) == NULL);
99
100   g_error_free (error);
101
102 }
103
104 static void
105 test_unregistered_errors (void)
106 {
107   /* Here we check that we are able to map to GError and back for unregistered
108    * errors.
109    *
110    * For example, if "com.example.Error.Failed" is not registered, then check
111    *
112    *  - Creating a GError for e.g. "com.example.Error.Failed" has (error_domain, code) ==
113    *    (G_IO_ERROR, G_IO_ERROR_DBUS_ERROR)
114    *
115    *  - That it is possible to recover e.g. "com.example.Error.Failed" from that
116    *    GError.
117    *
118    * We just check a couple of random errors.
119    */
120
121   check_unregistered_error ("com.example.Error.Failed");
122   check_unregistered_error ("foobar.buh");
123 }
124
125 /* ---------------------------------------------------------------------------------------------------- */
126
127 static void
128 check_transparent_gerror (GQuark error_domain,
129                           gint   error_code)
130 {
131   GError *error;
132   gchar *given_dbus_error_name;
133   gchar *dbus_error_name;
134
135   error = g_error_new (error_domain, error_code, "test message");
136   given_dbus_error_name = g_dbus_error_encode_gerror (error);
137   g_assert (g_str_has_prefix (given_dbus_error_name, "org.gtk.GDBus.UnmappedGError.Quark"));
138   g_error_free (error);
139
140   error = g_dbus_error_new_for_dbus_error (given_dbus_error_name, "test message");
141   g_assert_error (error, error_domain, error_code);
142   g_assert (g_dbus_error_is_remote_error (error));
143   dbus_error_name = g_dbus_error_get_remote_error (error);
144   g_assert_cmpstr (dbus_error_name, ==, given_dbus_error_name);
145   g_free (dbus_error_name);
146   g_free (given_dbus_error_name);
147
148   /* strip the message */
149   g_assert (g_dbus_error_strip_remote_error (error));
150   g_assert_cmpstr (error->message, ==, "test message");
151
152   /* check that we can no longer recover the D-Bus error name */
153   g_assert (g_dbus_error_get_remote_error (error) == NULL);
154
155   g_error_free (error);
156 }
157
158 static void
159 test_transparent_gerror (void)
160 {
161   /* Here we check that we are able to transparent pass unregistered GError's
162    * over the wire.
163    *
164    * For example, if G_IO_ERROR_FAILED is not registered, then check
165    *
166    *  - g_dbus_error_encode_gerror() returns something of the form
167    *    org.gtk.GDBus.UnmappedGError.Quark_HEXENCODED_QUARK_NAME_.Code_ERROR_CODE
168    *
169    *  - mapping back the D-Bus error name gives us G_IO_ERROR_FAILED
170    *
171    *  - That it is possible to recover the D-Bus error name from the
172    *    GError.
173    *
174    * We just check a couple of random errors.
175    */
176
177   check_transparent_gerror (G_IO_ERROR, G_IO_ERROR_FAILED);
178   check_transparent_gerror (G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE);
179 }
180
181 typedef enum
182 {
183   TEST_ERROR_FAILED,
184   TEST_ERROR_BLA
185 } TestError;
186
187 GDBusErrorEntry test_error_entries[] =
188 {
189   { TEST_ERROR_FAILED, "org.gtk.test.Error.Failed" },
190   { TEST_ERROR_BLA,    "org.gtk.test.Error.Bla"    }
191 };
192
193 static void
194 test_register_error (void)
195 {
196   gsize test_error_quark = 0;
197   gboolean res;
198   gchar *msg;
199   GError *error;
200
201   g_dbus_error_register_error_domain ("test-error-quark",
202                                       &test_error_quark,
203                                       test_error_entries,
204                                       G_N_ELEMENTS (test_error_entries));
205   g_assert_cmpint (test_error_quark, !=, 0);
206
207   error = g_dbus_error_new_for_dbus_error ("org.gtk.test.Error.Failed", "Failed");
208   g_assert_error (error, test_error_quark, TEST_ERROR_FAILED);
209   res = g_dbus_error_is_remote_error (error);
210   msg = g_dbus_error_get_remote_error (error);
211   g_assert (res);
212   g_assert_cmpstr (msg, ==, "org.gtk.test.Error.Failed");
213   res = g_dbus_error_strip_remote_error (error);
214   g_assert (res);
215   g_assert_cmpstr (error->message, ==, "Failed");
216   g_clear_error (&error);
217   g_free (msg);
218
219   g_dbus_error_set_dbus_error (&error, "org.gtk.test.Error.Failed", "Failed again", "Prefix %d", 1);
220   res = g_dbus_error_is_remote_error (error);
221   msg = g_dbus_error_get_remote_error (error);
222   g_assert (res);
223   g_assert_cmpstr (msg, ==, "org.gtk.test.Error.Failed");
224   res = g_dbus_error_strip_remote_error (error);
225   g_assert (res);
226   g_assert_cmpstr (error->message, ==, "Prefix 1: Failed again");
227   g_clear_error (&error);
228   g_free (msg);
229
230   error = g_error_new_literal (G_IO_ERROR, G_IO_ERROR_NOT_EMPTY, "Not Empty");
231   res = g_dbus_error_is_remote_error (error);
232   msg = g_dbus_error_get_remote_error (error);
233   g_assert (!res);
234   g_assert_cmpstr (msg, ==, NULL);
235   res = g_dbus_error_strip_remote_error (error);
236   g_assert (!res);
237   g_assert_cmpstr (error->message, ==, "Not Empty");
238   g_clear_error (&error);
239
240   error = g_error_new_literal (test_error_quark, TEST_ERROR_BLA, "Bla");
241   msg = g_dbus_error_encode_gerror (error);
242   g_assert_cmpstr (msg, ==, "org.gtk.test.Error.Bla");
243   g_free (msg);
244   g_clear_error (&error);
245
246   res = g_dbus_error_unregister_error (test_error_quark,
247                                        TEST_ERROR_BLA, "org.gtk.test.Error.Bla");
248   g_assert (res);
249 }
250
251
252 /* ---------------------------------------------------------------------------------------------------- */
253
254 int
255 main (int   argc,
256       char *argv[])
257 {
258   g_test_init (&argc, &argv, NULL);
259
260   g_test_add_func ("/gdbus/registered-errors", test_registered_errors);
261   g_test_add_func ("/gdbus/unregistered-errors", test_unregistered_errors);
262   g_test_add_func ("/gdbus/transparent-gerror", test_transparent_gerror);
263   g_test_add_func ("/gdbus/register-error", test_register_error);
264
265   return g_test_run();
266 }