Remove g_type_init() calls
[platform/upstream/glib.git] / gio / tests / gdbus-serialization.c
index 12abb77..e94fc4d 100644 (file)
@@ -20,6 +20,7 @@
  * Author: David Zeuthen <davidz@redhat.com>
  */
 
+#include <locale.h>
 #include <gio/gio.h>
 
 #include <string.h>
@@ -606,8 +607,8 @@ check_serialization (GVariant *value,
                                                         blob_size,
                                                         G_DBUS_CAPABILITY_FLAGS_NONE,
                                                         &error);
-      g_assert (recovered_message != NULL);
       g_assert_no_error (error);
+      g_assert (recovered_message != NULL);
 
       if (value == NULL)
         {
@@ -735,15 +736,30 @@ message_serialize_complex (void)
 /* ---------------------------------------------------------------------------------------------------- */
 
 static void
+replace (char       *blob,
+         gsize       len,
+        const char *before,
+        const char *after)
+{
+  gsize i;
+  gsize slen = strlen (before) + 1;
+
+  g_assert_cmpuint (strlen (before), ==, strlen (after));
+  g_assert_cmpuint (len, >=, slen);
+
+  for (i = 0; i < (len - slen + 1); i++)
+    {
+      if (memcmp (blob + i, before, slen) == 0)
+        memcpy (blob + i, after, slen);
+    }
+}
+
+static void
 message_serialize_invalid (void)
 {
   guint n;
 
-  /* Here we're relying on libdbus-1's DBusMessage type not checking
-   * anything. If that were to change, we'd need to do our own
-   * thing.
-   *
-   * Other things we could check (note that GDBus _does_ check for all
+  /* Other things we could check (note that GDBus _does_ check for all
    * these things - we just don't have test-suit coverage for it)
    *
    *  - array exceeding 64 MiB (2^26 bytes) - unfortunately libdbus-1 checks
@@ -768,9 +784,13 @@ message_serialize_invalid (void)
       DBusMessage *dbus_message;
       char *blob;
       int blob_len;
+      /* these are in pairs with matching length */
+      const gchar *valid_utf8_str = "this is valid...";
       const gchar *invalid_utf8_str = "this is invalid\xff";
-      const gchar *invalid_object_path = "/this/is/not a valid object path";
+      const gchar *valid_signature = "a{sv}a{sv}a{sv}aiai";
       const gchar *invalid_signature = "not valid signature";
+      const gchar *valid_object_path = "/this/is/a/valid/dbus/object/path";
+      const gchar *invalid_object_path = "/this/is/not a valid object path!";
 
       dbus_message = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
       dbus_message_set_serial (dbus_message, 0x41);
@@ -781,21 +801,21 @@ message_serialize_invalid (void)
         case 0:
           /* invalid UTF-8 */
           dbus_message_append_args (dbus_message,
-                                    DBUS_TYPE_STRING, &invalid_utf8_str,
+                                    DBUS_TYPE_STRING, &valid_utf8_str,
                                     DBUS_TYPE_INVALID);
           break;
 
         case 1:
           /* invalid object path */
           dbus_message_append_args (dbus_message,
-                                    DBUS_TYPE_OBJECT_PATH, &invalid_object_path,
+                                    DBUS_TYPE_OBJECT_PATH, &valid_object_path,
                                     DBUS_TYPE_INVALID);
           break;
 
         case 2:
           /* invalid signature */
           dbus_message_append_args (dbus_message,
-                                    DBUS_TYPE_SIGNATURE, &invalid_signature,
+                                    DBUS_TYPE_SIGNATURE, &valid_signature,
                                     DBUS_TYPE_INVALID);
           break;
 
@@ -804,6 +824,11 @@ message_serialize_invalid (void)
           break;
         }
       dbus_message_marshal (dbus_message, &blob, &blob_len);
+      /* hack up the message to be invalid by replacing each valid string
+       * with its invalid counterpart */
+      replace (blob, blob_len, valid_utf8_str, invalid_utf8_str);
+      replace (blob, blob_len, valid_object_path, invalid_object_path);
+      replace (blob, blob_len, valid_signature, invalid_signature);
 
       error = NULL;
       message = g_dbus_message_new_from_blob ((guchar *) blob,
@@ -821,16 +846,227 @@ message_serialize_invalid (void)
 
 /* ---------------------------------------------------------------------------------------------------- */
 
+static void
+message_serialize_header_checks (void)
+{
+  GDBusMessage *message;
+  GDBusMessage *reply;
+  GError *error;
+  guchar *blob;
+  gsize blob_size;
+
+  /*
+   * check we can't serialize messages with INVALID type
+   */
+  message = g_dbus_message_new ();
+  error = NULL;
+  blob = g_dbus_message_to_blob (message, &blob_size, G_DBUS_CAPABILITY_FLAGS_NONE, &error);
+  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
+  g_assert_cmpstr (error->message, ==, "Cannot serialize message: type is INVALID");
+  g_error_free (error);
+  g_assert (blob == NULL);
+  g_object_unref (message);
+
+  /*
+   * check we can't serialize signal messages with INTERFACE, PATH or MEMBER unset / set to reserved value
+   */
+  message = g_dbus_message_new_signal ("/the/path", "The.Interface", "TheMember");
+  /* ----- */
+  /* interface NULL => error */
+  g_dbus_message_set_interface (message, NULL);
+  error = NULL;
+  blob = g_dbus_message_to_blob (message, &blob_size, G_DBUS_CAPABILITY_FLAGS_NONE, &error);
+  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
+  g_assert_cmpstr (error->message, ==, "Cannot serialize message: SIGNAL message: PATH, INTERFACE or MEMBER header field is missing");
+  g_error_free (error);
+  g_assert (blob == NULL);
+  /* interface reserved value => error */
+  g_dbus_message_set_interface (message, "org.freedesktop.DBus.Local");
+  error = NULL;
+  blob = g_dbus_message_to_blob (message, &blob_size, G_DBUS_CAPABILITY_FLAGS_NONE, &error);
+  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
+  g_assert_cmpstr (error->message, ==, "Cannot serialize message: SIGNAL message: The INTERFACE header field is using the reserved value org.freedesktop.DBus.Local");
+  g_error_free (error);
+  g_assert (blob == NULL);
+  /* reset interface */
+  g_dbus_message_set_interface (message, "The.Interface");
+  /* ----- */
+  /* path NULL => error */
+  g_dbus_message_set_path (message, NULL);
+  error = NULL;
+  blob = g_dbus_message_to_blob (message, &blob_size, G_DBUS_CAPABILITY_FLAGS_NONE, &error);
+  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
+  g_assert_cmpstr (error->message, ==, "Cannot serialize message: SIGNAL message: PATH, INTERFACE or MEMBER header field is missing");
+  g_error_free (error);
+  g_assert (blob == NULL);
+  /* path reserved value => error */
+  g_dbus_message_set_path (message, "/org/freedesktop/DBus/Local");
+  error = NULL;
+  blob = g_dbus_message_to_blob (message, &blob_size, G_DBUS_CAPABILITY_FLAGS_NONE, &error);
+  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
+  g_assert_cmpstr (error->message, ==, "Cannot serialize message: SIGNAL message: The PATH header field is using the reserved value /org/freedesktop/DBus/Local");
+  g_error_free (error);
+  g_assert (blob == NULL);
+  /* reset path */
+  g_dbus_message_set_path (message, "/the/path");
+  /* ----- */
+  /* member NULL => error */
+  g_dbus_message_set_member (message, NULL);
+  error = NULL;
+  blob = g_dbus_message_to_blob (message, &blob_size, G_DBUS_CAPABILITY_FLAGS_NONE, &error);
+  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
+  g_assert_cmpstr (error->message, ==, "Cannot serialize message: SIGNAL message: PATH, INTERFACE or MEMBER header field is missing");
+  g_error_free (error);
+  g_assert (blob == NULL);
+  /* reset member */
+  g_dbus_message_set_member (message, "TheMember");
+  /* ----- */
+  /* done */
+  g_object_unref (message);
+
+  /*
+   * check that we can't serialize method call messages with PATH or MEMBER unset
+   */
+  message = g_dbus_message_new_method_call (NULL, "/the/path", NULL, "TheMember");
+  /* ----- */
+  /* path NULL => error */
+  g_dbus_message_set_path (message, NULL);
+  error = NULL;
+  blob = g_dbus_message_to_blob (message, &blob_size, G_DBUS_CAPABILITY_FLAGS_NONE, &error);
+  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
+  g_assert_cmpstr (error->message, ==, "Cannot serialize message: METHOD_CALL message: PATH or MEMBER header field is missing");
+  g_error_free (error);
+  g_assert (blob == NULL);
+  /* reset path */
+  g_dbus_message_set_path (message, "/the/path");
+  /* ----- */
+  /* member NULL => error */
+  g_dbus_message_set_member (message, NULL);
+  error = NULL;
+  blob = g_dbus_message_to_blob (message, &blob_size, G_DBUS_CAPABILITY_FLAGS_NONE, &error);
+  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
+  g_assert_cmpstr (error->message, ==, "Cannot serialize message: METHOD_CALL message: PATH or MEMBER header field is missing");
+  g_error_free (error);
+  g_assert (blob == NULL);
+  /* reset member */
+  g_dbus_message_set_member (message, "TheMember");
+  /* ----- */
+  /* done */
+  g_object_unref (message);
+
+  /*
+   * check that we can't serialize method reply messages with REPLY_SERIAL unset
+   */
+  message = g_dbus_message_new_method_call (NULL, "/the/path", NULL, "TheMember");
+  g_dbus_message_set_serial (message, 42);
+  /* method reply */
+  reply = g_dbus_message_new_method_reply (message);
+  g_assert_cmpint (g_dbus_message_get_reply_serial (reply), ==, 42);
+  g_dbus_message_set_header (reply, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL, NULL);
+  error = NULL;
+  blob = g_dbus_message_to_blob (reply, &blob_size, G_DBUS_CAPABILITY_FLAGS_NONE, &error);
+  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
+  g_assert_cmpstr (error->message, ==, "Cannot serialize message: METHOD_RETURN message: REPLY_SERIAL header field is missing");
+  g_error_free (error);
+  g_assert (blob == NULL);
+  g_object_unref (reply);
+  /* method error - first nuke ERROR_NAME, then REPLY_SERIAL */
+  reply = g_dbus_message_new_method_error (message, "Some.Error.Name", "the message");
+  g_assert_cmpint (g_dbus_message_get_reply_serial (reply), ==, 42);
+  /* nuke ERROR_NAME */
+  g_dbus_message_set_error_name (reply, NULL);
+  error = NULL;
+  blob = g_dbus_message_to_blob (reply, &blob_size, G_DBUS_CAPABILITY_FLAGS_NONE, &error);
+  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
+  g_assert_cmpstr (error->message, ==, "Cannot serialize message: ERROR message: REPLY_SERIAL or ERROR_NAME header field is missing");
+  g_error_free (error);
+  g_assert (blob == NULL);
+  /* reset ERROR_NAME */
+  g_dbus_message_set_error_name (reply, "Some.Error.Name");
+  /* nuke REPLY_SERIAL */
+  g_dbus_message_set_header (reply, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL, NULL);
+  error = NULL;
+  blob = g_dbus_message_to_blob (reply, &blob_size, G_DBUS_CAPABILITY_FLAGS_NONE, &error);
+  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
+  g_assert_cmpstr (error->message, ==, "Cannot serialize message: ERROR message: REPLY_SERIAL or ERROR_NAME header field is missing");
+  g_error_free (error);
+  g_assert (blob == NULL);
+  g_object_unref (reply);
+  g_object_unref (message);
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
+static void
+message_parse_empty_arrays_of_arrays (void)
+{
+  GVariant *body;
+  GError *error = NULL;
+
+  g_test_bug ("673612");
+  /* These three-element array of empty arrays were previously read back as a
+   * two-element array of empty arrays, due to sometimes erroneously skipping
+   * four bytes to align for the eight-byte-aligned grandchild types (x and
+   * dict_entry).
+   */
+  body = g_variant_parse (G_VARIANT_TYPE ("(aaax)"),
+      "([@aax [], [], []],)", NULL, NULL, &error);
+  g_assert_no_error (error);
+  check_serialization (body,
+      "value 0:   array:\n"
+      "    array:\n"
+      "    array:\n"
+      "    array:\n");
+
+  body = g_variant_parse (G_VARIANT_TYPE ("(aaa{uu})"),
+      "([@aa{uu} [], [], []],)", NULL, NULL, &error);
+  g_assert_no_error (error);
+  check_serialization (body,
+      "value 0:   array:\n"
+      "    array:\n"
+      "    array:\n"
+      "    array:\n");
+
+  /* Due to the same bug, g_dbus_message_new_from_blob() would fail for this
+   * message because it would try to read past the end of the string. Hence,
+   * sending this to an application would make it fall off the bus. */
+  body = g_variant_parse (G_VARIANT_TYPE ("(a(aa{sv}as))"),
+      "([ ([], []),"
+      "   ([], []),"
+      "   ([], [])],)", NULL, NULL, &error);
+  g_assert_no_error (error);
+  check_serialization (body,
+      "value 0:   array:\n"
+      "    struct:\n"
+      "      array:\n"
+      "      array:\n"
+      "    struct:\n"
+      "      array:\n"
+      "      array:\n"
+      "    struct:\n"
+      "      array:\n"
+      "      array:\n");
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
 int
 main (int   argc,
       char *argv[])
 {
-  g_type_init ();
+  setlocale (LC_ALL, "C");
+
   g_test_init (&argc, &argv, NULL);
+  g_test_bug_base ("https://bugzilla.gnome.org/show_bug.cgi?id=");
 
   g_test_add_func ("/gdbus/message-serialize-basic", message_serialize_basic);
   g_test_add_func ("/gdbus/message-serialize-complex", message_serialize_complex);
   g_test_add_func ("/gdbus/message-serialize-invalid", message_serialize_invalid);
+  g_test_add_func ("/gdbus/message-serialize-header-checks", message_serialize_header_checks);
+
+  g_test_add_func ("/gdbus/message-parse-empty-arrays-of-arrays",
+      message_parse_empty_arrays_of_arrays);
+
   return g_test_run();
 }