981a9513c921e9ff950eaad275c97ba063982bcb
[platform/upstream/glib.git] / gio / tests / gdbus-message.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, 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
23 #include <locale.h>
24 #include <gio/gio.h>
25
26 /* ---------------------------------------------------------------------------------------------------- */
27
28 static void
29 on_notify_locked (GObject    *object,
30                   GParamSpec *pspec,
31                   gpointer    user_data)
32 {
33   gint *count = user_data;
34   *count += 1;
35 }
36
37 static void
38 message_lock (void)
39 {
40   GDBusMessage *m;
41   gint count;
42
43   count = 0;
44   m = g_dbus_message_new ();
45   g_signal_connect (m,
46                     "notify::locked",
47                     G_CALLBACK (on_notify_locked),
48                     &count);
49   g_assert (!g_dbus_message_get_locked (m));
50   g_dbus_message_lock (m);
51   g_assert (g_dbus_message_get_locked (m));
52   g_assert_cmpint (count, ==, 1);
53   g_dbus_message_lock (m);
54   g_assert (g_dbus_message_get_locked (m));
55   g_assert_cmpint (count, ==, 1);
56
57   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
58     g_dbus_message_set_serial (m, 42);
59   g_test_trap_assert_failed (); g_test_trap_assert_stderr ("*Attempted to modify a locked message*");
60
61   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
62     g_dbus_message_set_byte_order (m, G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN);
63   g_test_trap_assert_failed (); g_test_trap_assert_stderr ("*Attempted to modify a locked message*");
64
65   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
66     g_dbus_message_set_message_type (m, G_DBUS_MESSAGE_TYPE_METHOD_CALL);
67   g_test_trap_assert_failed (); g_test_trap_assert_stderr ("*Attempted to modify a locked message*");
68
69   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
70     g_dbus_message_set_flags (m, G_DBUS_MESSAGE_FLAGS_NONE);
71   g_test_trap_assert_failed (); g_test_trap_assert_stderr ("*Attempted to modify a locked message*");
72
73   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
74     g_dbus_message_set_body (m, NULL);
75   g_test_trap_assert_failed (); g_test_trap_assert_stderr ("*Attempted to modify a locked message*");
76
77   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
78     g_dbus_message_set_header (m, 0, NULL);
79   g_test_trap_assert_failed (); g_test_trap_assert_stderr ("*Attempted to modify a locked message*");
80
81   g_object_unref (m);
82 }
83
84 /* ---------------------------------------------------------------------------------------------------- */
85
86 static void
87 message_copy (void)
88 {
89   GDBusMessage *m;
90   GDBusMessage *copy;
91   GError *error;
92   guchar *m_headers;
93   guchar *copy_headers;
94   guint n;
95
96   m = g_dbus_message_new_method_call ("org.example.Name",
97                                       "/org/example/Object",
98                                       "org.example.Interface",
99                                       "Method");
100   g_dbus_message_set_serial (m, 42);
101   g_dbus_message_set_byte_order (m, G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN);
102
103   error = NULL;
104   copy = g_dbus_message_copy (m, &error);
105   g_assert_no_error (error);
106   g_assert (G_IS_DBUS_MESSAGE (copy));
107   g_assert (m != copy);
108   g_assert_cmpint (G_OBJECT (m)->ref_count, ==, 1);
109   g_assert_cmpint (G_OBJECT (copy)->ref_count, ==, 1);
110
111   g_assert_cmpint (g_dbus_message_get_serial (copy), ==, 0);
112   g_assert_cmpint (g_dbus_message_get_byte_order (copy), ==, g_dbus_message_get_byte_order (m));
113   g_assert_cmpint (g_dbus_message_get_flags (copy), ==, g_dbus_message_get_flags (m));
114   g_assert_cmpint (g_dbus_message_get_message_type (copy), ==, g_dbus_message_get_message_type (m));
115   m_headers = g_dbus_message_get_header_fields (m);
116   copy_headers = g_dbus_message_get_header_fields (copy);
117   g_assert (m_headers != NULL);
118   g_assert (copy_headers != NULL);
119   for (n = 0; m_headers[n] != 0; n++)
120     {
121       GVariant *m_val;
122       GVariant *copy_val;
123       m_val = g_dbus_message_get_header (m, m_headers[n]);
124       copy_val = g_dbus_message_get_header (m, m_headers[n]);
125       g_assert (m_val != NULL);
126       g_assert (copy_val != NULL);
127       g_assert (g_variant_equal (m_val, copy_val));
128     }
129   g_assert_cmpint (n, >, 0); /* make sure we actually compared headers etc. */
130   g_assert_cmpint (copy_headers[n], ==, 0);
131   g_free (m_headers);
132   g_free (copy_headers);
133
134   g_object_unref (copy);
135   g_object_unref (m);
136 }
137
138 /* ---------------------------------------------------------------------------------------------------- */
139
140 int
141 main (int   argc,
142       char *argv[])
143 {
144   setlocale (LC_ALL, "C");
145
146   g_type_init ();
147   g_test_init (&argc, &argv, NULL);
148
149   g_test_add_func ("/gdbus/message/lock", message_lock);
150   g_test_add_func ("/gdbus/message/copy", message_copy);
151   return g_test_run();
152 }
153