cmake: Add X11 include path for tools
[platform/upstream/dbus.git] / test / message.c
1 /* Targeted unit tests for OOM paths in DBusMessage
2  *
3  * Copyright © 2017 Collabora Ltd.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation files
7  * (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include <config.h>
27
28 #include <string.h>
29
30 #include <glib.h>
31
32 #include <dbus/dbus.h>
33 #include "dbus/dbus-internals.h"
34 #include "dbus/dbus-pipe.h"
35 #include "test-utils-glib.h"
36
37 /* Return TRUE if the right thing happens, but the right thing might include
38  * OOM. */
39 static dbus_bool_t
40 test_array (void *contained_signature)
41 {
42   DBusMessage *m;
43   DBusMessageIter iter;
44   DBusMessageIter arr_iter;
45   dbus_bool_t arr_iter_open = FALSE;
46   DBusMessageIter inner_iter;
47   dbus_bool_t inner_iter_open = FALSE;
48
49   m = dbus_message_new_signal ("/", "a.b", "c");
50
51   if (m == NULL)
52     goto out;
53
54   dbus_message_iter_init_append (m, &iter);
55
56   /* open_container only opens the container if it succeeds */
57   if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
58                                          contained_signature,
59                                          &arr_iter))
60     goto out;
61
62   arr_iter_open = TRUE;
63
64   if (g_strcmp0 (contained_signature, "ai") == 0)
65     {
66       /* open_container only opens the container if it succeeds */
67       if (!dbus_message_iter_open_container (&arr_iter, DBUS_TYPE_ARRAY, "i",
68                                              &inner_iter))
69         goto out;
70
71       /* We do not set inner_iter_open to TRUE here because we would
72        * immediately set it to FALSE again */
73
74       /* close_container closes the container, even when it fails */
75       if (!dbus_message_iter_close_container (&arr_iter, &inner_iter))
76         goto out;
77     }
78   else if (g_strcmp0 (contained_signature, "{ss}") == 0)
79     {
80       const char *s = "hello";
81
82       /* open_container only opens the container if it succeeds */
83       if (!dbus_message_iter_open_container (&arr_iter, DBUS_TYPE_DICT_ENTRY,
84                                              NULL, &inner_iter))
85         goto out;
86
87       inner_iter_open = TRUE;
88
89       if (!dbus_message_iter_append_basic (&inner_iter, DBUS_TYPE_STRING, &s))
90         goto out;
91
92       if (!dbus_message_iter_append_basic (&inner_iter, DBUS_TYPE_STRING, &s))
93         goto out;
94
95       /* close_container closes the container, even when it fails */
96       inner_iter_open = FALSE;
97
98       if (!dbus_message_iter_close_container (&arr_iter, &inner_iter))
99         goto out;
100     }
101   else if (g_strcmp0 (contained_signature, "v") == 0)
102     {
103       dbus_bool_t yes = TRUE;
104
105       /* open_container only opens the container if it succeeds */
106       if (!dbus_message_iter_open_container (&arr_iter, DBUS_TYPE_VARIANT,
107                                              "b", &inner_iter))
108         goto out;
109
110       inner_iter_open = TRUE;
111
112       if (!dbus_message_iter_append_basic (&inner_iter, DBUS_TYPE_BOOLEAN,
113                                            &yes))
114         goto out;
115
116       /* close_container closes the container, even when it fails */
117       inner_iter_open = FALSE;
118
119       if (!dbus_message_iter_close_container (&arr_iter, &inner_iter))
120         goto out;
121     }
122   else
123     {
124       g_assert_not_reached ();
125     }
126
127   /* close_container closes the container, even when it fails */
128   arr_iter_open = FALSE;
129
130   if (!dbus_message_iter_close_container (&iter, &arr_iter))
131     goto out;
132
133 out:
134   if (inner_iter_open)
135     dbus_message_iter_abandon_container (&arr_iter, &inner_iter);
136
137   if (arr_iter_open)
138     dbus_message_iter_abandon_container (&iter, &arr_iter);
139
140   if (m != NULL)
141     dbus_message_unref (m);
142
143   dbus_shutdown ();
144   g_assert_cmpint (_dbus_get_malloc_blocks_outstanding (), ==, 0);
145
146   return !g_test_failed ();
147 }
148
149 /* Return TRUE if the right thing happens, but the right thing might include
150  * OOM or inability to pass fds. */
151 static dbus_bool_t
152 test_fd (void *ignored)
153 {
154   DBusMessage *m = NULL;
155   DBusPipe pipe;
156
157   _dbus_pipe_init_stdout (&pipe);
158
159   m = dbus_message_new_signal ("/", "a.b", "c");
160
161   if (m == NULL)
162     goto out;
163
164   if (!dbus_message_append_args (m,
165                                  DBUS_TYPE_UNIX_FD, &pipe.fd,
166                                  DBUS_TYPE_INVALID))
167     goto out;
168
169 out:
170   if (m != NULL)
171     dbus_message_unref (m);
172
173   dbus_shutdown ();
174   g_assert_cmpint (_dbus_get_malloc_blocks_outstanding (), ==, 0);
175
176   return !g_test_failed ();
177 }
178
179 /* Similar to test_array(), but making use of
180  * dbus_message_iter_abandon_container_if_open().
181  *
182  * Return TRUE if the right thing happens, but the right thing might include
183  * OOM. */
184 static dbus_bool_t
185 test_zero_iter (void *ignored)
186 {
187   DBusMessage *m;
188   DBusMessageIter iter = DBUS_MESSAGE_ITER_INIT_CLOSED;
189   DBusMessageIter arr_iter = DBUS_MESSAGE_ITER_INIT_CLOSED;
190   DBusMessageIter inner_iter;
191   dbus_int32_t fortytwo = 42;
192   dbus_bool_t message_should_be_complete = FALSE;
193
194   /* This one was left uninitialized, just so we could exercise this
195    * function */
196   dbus_message_iter_init_closed (&inner_iter);
197
198   m = dbus_message_new_signal ("/", "a.b", "c");
199
200   if (m == NULL)
201     goto out;
202
203   dbus_message_iter_init_append (m, &iter);
204
205   if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
206                                          "ai", &arr_iter))
207     goto out;
208
209   if (!dbus_message_iter_open_container (&arr_iter, DBUS_TYPE_ARRAY, "i",
210                                          &inner_iter))
211     goto out;
212
213   if (!dbus_message_iter_append_basic (&inner_iter, DBUS_TYPE_INT32, &fortytwo))
214     goto out;
215
216   if (!dbus_message_iter_close_container (&arr_iter, &inner_iter))
217     goto out;
218
219   if (!dbus_message_iter_close_container (&iter, &arr_iter))
220     goto out;
221
222   message_should_be_complete = TRUE;
223
224 out:
225   dbus_message_iter_abandon_container_if_open (&arr_iter, &inner_iter);
226   dbus_message_iter_abandon_container_if_open (&iter, &arr_iter);
227   /* Redundant calls are OK */
228   dbus_message_iter_abandon_container_if_open (&iter, &arr_iter);
229
230   /* dbus_message_iter_abandon_container_if_open does not leave the message
231    * in what seems to be consistently documented as a "hosed" state */
232   if (message_should_be_complete)
233     {
234       DBusBasicValue read_back;
235
236       _DBUS_ZERO (read_back);
237       dbus_message_iter_init (m, &iter);
238       dbus_message_iter_recurse (&iter, &arr_iter);
239       dbus_message_iter_recurse (&arr_iter, &inner_iter);
240       dbus_message_iter_get_basic (&inner_iter, &read_back);
241       g_assert_cmpint (read_back.i32, ==, 42);
242     }
243
244   if (m != NULL)
245     dbus_message_unref (m);
246
247   dbus_shutdown ();
248   g_assert_cmpint (_dbus_get_malloc_blocks_outstanding (), ==, 0);
249
250   return !g_test_failed ();
251 }
252
253 typedef struct
254 {
255   const gchar *name;
256   DBusTestMemoryFunction function;
257   const void *data;
258 } OOMTestCase;
259
260 static void
261 test_oom_wrapper (gconstpointer data)
262 {
263   const OOMTestCase *test = data;
264
265   if (!_dbus_test_oom_handling (test->name, test->function,
266                                 (void *) test->data))
267     {
268       g_test_message ("OOM test failed");
269       g_test_fail ();
270     }
271 }
272
273 static GQueue *test_cases_to_free = NULL;
274
275 static void
276 add_oom_test (const gchar *name,
277               DBusTestMemoryFunction function,
278               const void *data)
279 {
280   /* By using GLib memory allocation here, we avoid being affected by
281    * dbus_shutdown() or contributing to
282    * _dbus_get_malloc_blocks_outstanding() */
283   OOMTestCase *test_case = g_new0 (OOMTestCase, 1);
284
285   test_case->name = name;
286   test_case->function = function;
287   test_case->data = data;
288   g_test_add_data_func (name, test_case, test_oom_wrapper);
289   g_queue_push_tail (test_cases_to_free, test_case);
290 }
291
292 int
293 main (int argc,
294       char **argv)
295 {
296   int ret;
297
298   test_init (&argc, &argv);
299
300   test_cases_to_free = g_queue_new ();
301   add_oom_test ("/message/array/array", test_array, "ai");
302   add_oom_test ("/message/array/dict", test_array, "{ss}");
303   add_oom_test ("/message/array/variant", test_array, "v");
304   add_oom_test ("/message/fd", test_fd, NULL);
305   add_oom_test ("/message/zero-iter", test_zero_iter, NULL);
306
307   ret = g_test_run ();
308
309   g_queue_free_full (test_cases_to_free, g_free);
310   return ret;
311 }