PageRenderTime 52ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/gio/tests/gdbus-error.c

https://bitbucket.org/weberw/glib
C | 269 lines | 157 code | 37 blank | 75 comment | 2 complexity | 0b59b294d898f19647b159560182736f MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-3.0
  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, write to the
  17. * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  18. * Boston, MA 02111-1307, USA.
  19. *
  20. * Author: David Zeuthen <davidz@redhat.com>
  21. */
  22. #include <gio/gio.h>
  23. #include <unistd.h>
  24. #include <string.h>
  25. /* ---------------------------------------------------------------------------------------------------- */
  26. /* Test that registered errors are properly mapped */
  27. /* ---------------------------------------------------------------------------------------------------- */
  28. static void
  29. check_registered_error (const gchar *given_dbus_error_name,
  30. GQuark error_domain,
  31. gint error_code)
  32. {
  33. GError *error;
  34. gchar *dbus_error_name;
  35. error = g_dbus_error_new_for_dbus_error (given_dbus_error_name, "test message");
  36. g_assert_error (error, error_domain, error_code);
  37. g_assert (g_dbus_error_is_remote_error (error));
  38. g_assert (g_dbus_error_strip_remote_error (error));
  39. g_assert_cmpstr (error->message, ==, "test message");
  40. dbus_error_name = g_dbus_error_get_remote_error (error);
  41. g_assert_cmpstr (dbus_error_name, ==, given_dbus_error_name);
  42. g_free (dbus_error_name);
  43. g_error_free (error);
  44. }
  45. static void
  46. test_registered_errors (void)
  47. {
  48. /* Here we check that we are able to map to GError and back for registered
  49. * errors.
  50. *
  51. * For example, if "org.freedesktop.DBus.Error.AddressInUse" is
  52. * associated with (G_DBUS_ERROR, G_DBUS_ERROR_DBUS_FAILED), check
  53. * that
  54. *
  55. * - Creating a GError for e.g. "org.freedesktop.DBus.Error.AddressInUse"
  56. * has (error_domain, code) == (G_DBUS_ERROR, G_DBUS_ERROR_DBUS_FAILED)
  57. *
  58. * - That it is possible to recover e.g. "org.freedesktop.DBus.Error.AddressInUse"
  59. * as the D-Bus error name when dealing with an error with (error_domain, code) ==
  60. * (G_DBUS_ERROR, G_DBUS_ERROR_DBUS_FAILED)
  61. *
  62. * We just check a couple of well-known errors.
  63. */
  64. check_registered_error ("org.freedesktop.DBus.Error.Failed",
  65. G_DBUS_ERROR,
  66. G_DBUS_ERROR_FAILED);
  67. check_registered_error ("org.freedesktop.DBus.Error.AddressInUse",
  68. G_DBUS_ERROR,
  69. G_DBUS_ERROR_ADDRESS_IN_USE);
  70. check_registered_error ("org.freedesktop.DBus.Error.UnknownMethod",
  71. G_DBUS_ERROR,
  72. G_DBUS_ERROR_UNKNOWN_METHOD);
  73. }
  74. /* ---------------------------------------------------------------------------------------------------- */
  75. static void
  76. check_unregistered_error (const gchar *given_dbus_error_name)
  77. {
  78. GError *error;
  79. gchar *dbus_error_name;
  80. error = g_dbus_error_new_for_dbus_error (given_dbus_error_name, "test message");
  81. g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
  82. g_assert (g_dbus_error_is_remote_error (error));
  83. dbus_error_name = g_dbus_error_get_remote_error (error);
  84. g_assert_cmpstr (dbus_error_name, ==, given_dbus_error_name);
  85. g_free (dbus_error_name);
  86. /* strip the message */
  87. g_assert (g_dbus_error_strip_remote_error (error));
  88. g_assert_cmpstr (error->message, ==, "test message");
  89. /* check that we can no longer recover the D-Bus error name */
  90. g_assert (g_dbus_error_get_remote_error (error) == NULL);
  91. g_error_free (error);
  92. }
  93. static void
  94. test_unregistered_errors (void)
  95. {
  96. /* Here we check that we are able to map to GError and back for unregistered
  97. * errors.
  98. *
  99. * For example, if "com.example.Error.Failed" is not registered, then check
  100. *
  101. * - Creating a GError for e.g. "com.example.Error.Failed" has (error_domain, code) ==
  102. * (G_IO_ERROR, G_IO_ERROR_DBUS_ERROR)
  103. *
  104. * - That it is possible to recover e.g. "com.example.Error.Failed" from that
  105. * GError.
  106. *
  107. * We just check a couple of random errors.
  108. */
  109. check_unregistered_error ("com.example.Error.Failed");
  110. check_unregistered_error ("foobar.buh");
  111. }
  112. /* ---------------------------------------------------------------------------------------------------- */
  113. static void
  114. check_transparent_gerror (GQuark error_domain,
  115. gint error_code)
  116. {
  117. GError *error;
  118. gchar *given_dbus_error_name;
  119. gchar *dbus_error_name;
  120. error = g_error_new (error_domain, error_code, "test message");
  121. given_dbus_error_name = g_dbus_error_encode_gerror (error);
  122. g_assert (g_str_has_prefix (given_dbus_error_name, "org.gtk.GDBus.UnmappedGError.Quark"));
  123. g_error_free (error);
  124. error = g_dbus_error_new_for_dbus_error (given_dbus_error_name, "test message");
  125. g_assert_error (error, error_domain, error_code);
  126. g_assert (g_dbus_error_is_remote_error (error));
  127. dbus_error_name = g_dbus_error_get_remote_error (error);
  128. g_assert_cmpstr (dbus_error_name, ==, given_dbus_error_name);
  129. g_free (dbus_error_name);
  130. g_free (given_dbus_error_name);
  131. /* strip the message */
  132. g_assert (g_dbus_error_strip_remote_error (error));
  133. g_assert_cmpstr (error->message, ==, "test message");
  134. /* check that we can no longer recover the D-Bus error name */
  135. g_assert (g_dbus_error_get_remote_error (error) == NULL);
  136. g_error_free (error);
  137. }
  138. static void
  139. test_transparent_gerror (void)
  140. {
  141. /* Here we check that we are able to transparent pass unregistered GError's
  142. * over the wire.
  143. *
  144. * For example, if G_IO_ERROR_FAILED is not registered, then check
  145. *
  146. * - g_dbus_error_encode_gerror() returns something of the form
  147. * org.gtk.GDBus.UnmappedGError.Quark_HEXENCODED_QUARK_NAME_.Code_ERROR_CODE
  148. *
  149. * - mapping back the D-Bus error name gives us G_IO_ERROR_FAILED
  150. *
  151. * - That it is possible to recover the D-Bus error name from the
  152. * GError.
  153. *
  154. * We just check a couple of random errors.
  155. */
  156. check_transparent_gerror (G_IO_ERROR, G_IO_ERROR_FAILED);
  157. check_transparent_gerror (G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE);
  158. }
  159. typedef enum
  160. {
  161. TEST_ERROR_FAILED,
  162. TEST_ERROR_BLA
  163. } TestError;
  164. GDBusErrorEntry test_error_entries[] =
  165. {
  166. { TEST_ERROR_FAILED, "org.gtk.test.Error.Failed" },
  167. { TEST_ERROR_BLA, "org.gtk.test.Error.Bla" }
  168. };
  169. static void
  170. test_register_error (void)
  171. {
  172. gsize test_error_quark = 0;
  173. gboolean res;
  174. gchar *msg;
  175. GError *error;
  176. g_dbus_error_register_error_domain ("test-error-quark",
  177. &test_error_quark,
  178. test_error_entries,
  179. G_N_ELEMENTS (test_error_entries));
  180. g_assert_cmpint (test_error_quark, !=, 0);
  181. error = g_dbus_error_new_for_dbus_error ("org.gtk.test.Error.Failed", "Failed");
  182. g_assert_error (error, test_error_quark, TEST_ERROR_FAILED);
  183. res = g_dbus_error_is_remote_error (error);
  184. msg = g_dbus_error_get_remote_error (error);
  185. g_assert (res);
  186. g_assert_cmpstr (msg, ==, "org.gtk.test.Error.Failed");
  187. res = g_dbus_error_strip_remote_error (error);
  188. g_assert (res);
  189. g_assert_cmpstr (error->message, ==, "Failed");
  190. g_clear_error (&error);
  191. g_free (msg);
  192. g_dbus_error_set_dbus_error (&error, "org.gtk.test.Error.Failed", "Failed again", "Prefix %d", 1);
  193. res = g_dbus_error_is_remote_error (error);
  194. msg = g_dbus_error_get_remote_error (error);
  195. g_assert (res);
  196. g_assert_cmpstr (msg, ==, "org.gtk.test.Error.Failed");
  197. res = g_dbus_error_strip_remote_error (error);
  198. g_assert (res);
  199. g_assert_cmpstr (error->message, ==, "Prefix 1: Failed again");
  200. g_clear_error (&error);
  201. g_free (msg);
  202. error = g_error_new_literal (G_IO_ERROR, G_IO_ERROR_NOT_EMPTY, "Not Empty");
  203. res = g_dbus_error_is_remote_error (error);
  204. msg = g_dbus_error_get_remote_error (error);
  205. g_assert (!res);
  206. g_assert_cmpstr (msg, ==, NULL);
  207. res = g_dbus_error_strip_remote_error (error);
  208. g_assert (!res);
  209. g_assert_cmpstr (error->message, ==, "Not Empty");
  210. g_clear_error (&error);
  211. error = g_error_new_literal (test_error_quark, TEST_ERROR_BLA, "Bla");
  212. msg = g_dbus_error_encode_gerror (error);
  213. g_assert_cmpstr (msg, ==, "org.gtk.test.Error.Bla");
  214. g_free (msg);
  215. g_clear_error (&error);
  216. res = g_dbus_error_unregister_error (test_error_quark,
  217. TEST_ERROR_BLA, "org.gtk.test.Error.Bla");
  218. g_assert (res);
  219. }
  220. /* ---------------------------------------------------------------------------------------------------- */
  221. int
  222. main (int argc,
  223. char *argv[])
  224. {
  225. g_type_init ();
  226. g_test_init (&argc, &argv, NULL);
  227. g_test_add_func ("/gdbus/registered-errors", test_registered_errors);
  228. g_test_add_func ("/gdbus/unregistered-errors", test_unregistered_errors);
  229. g_test_add_func ("/gdbus/transparent-gerror", test_transparent_gerror);
  230. g_test_add_func ("/gdbus/register-error", test_register_error);
  231. return g_test_run();
  232. }