1 /* Targeted unit tests for OOM paths in DBusMessage
3 * Copyright © 2017 Collabora Ltd.
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:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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
32 #include <dbus/dbus.h>
33 #include "dbus/dbus-internals.h"
34 #include "dbus/dbus-pipe.h"
35 #include "test-utils-glib.h"
37 /* Return TRUE if the right thing happens, but the right thing might include
40 test_array (void *contained_signature)
44 DBusMessageIter arr_iter;
45 dbus_bool_t arr_iter_open = FALSE;
46 DBusMessageIter inner_iter;
47 dbus_bool_t inner_iter_open = FALSE;
49 m = dbus_message_new_signal ("/", "a.b", "c");
54 dbus_message_iter_init_append (m, &iter);
56 /* open_container only opens the container if it succeeds */
57 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
64 if (g_strcmp0 (contained_signature, "ai") == 0)
66 /* open_container only opens the container if it succeeds */
67 if (!dbus_message_iter_open_container (&arr_iter, DBUS_TYPE_ARRAY, "i",
71 /* We do not set inner_iter_open to TRUE here because we would
72 * immediately set it to FALSE again */
74 /* close_container closes the container, even when it fails */
75 if (!dbus_message_iter_close_container (&arr_iter, &inner_iter))
78 else if (g_strcmp0 (contained_signature, "{ss}") == 0)
80 const char *s = "hello";
82 /* open_container only opens the container if it succeeds */
83 if (!dbus_message_iter_open_container (&arr_iter, DBUS_TYPE_DICT_ENTRY,
87 inner_iter_open = TRUE;
89 if (!dbus_message_iter_append_basic (&inner_iter, DBUS_TYPE_STRING, &s))
92 if (!dbus_message_iter_append_basic (&inner_iter, DBUS_TYPE_STRING, &s))
95 /* close_container closes the container, even when it fails */
96 inner_iter_open = FALSE;
98 if (!dbus_message_iter_close_container (&arr_iter, &inner_iter))
101 else if (g_strcmp0 (contained_signature, "v") == 0)
103 dbus_bool_t yes = TRUE;
105 /* open_container only opens the container if it succeeds */
106 if (!dbus_message_iter_open_container (&arr_iter, DBUS_TYPE_VARIANT,
110 inner_iter_open = TRUE;
112 if (!dbus_message_iter_append_basic (&inner_iter, DBUS_TYPE_BOOLEAN,
116 /* close_container closes the container, even when it fails */
117 inner_iter_open = FALSE;
119 if (!dbus_message_iter_close_container (&arr_iter, &inner_iter))
124 g_assert_not_reached ();
127 /* close_container closes the container, even when it fails */
128 arr_iter_open = FALSE;
130 if (!dbus_message_iter_close_container (&iter, &arr_iter))
135 dbus_message_iter_abandon_container (&arr_iter, &inner_iter);
138 dbus_message_iter_abandon_container (&iter, &arr_iter);
141 dbus_message_unref (m);
144 g_assert_cmpint (_dbus_get_malloc_blocks_outstanding (), ==, 0);
146 return !g_test_failed ();
149 /* Return TRUE if the right thing happens, but the right thing might include
150 * OOM or inability to pass fds. */
152 test_fd (void *ignored)
154 DBusMessage *m = NULL;
157 _dbus_pipe_init_stdout (&pipe);
159 m = dbus_message_new_signal ("/", "a.b", "c");
164 if (!dbus_message_append_args (m,
165 DBUS_TYPE_UNIX_FD, &pipe.fd,
171 dbus_message_unref (m);
174 g_assert_cmpint (_dbus_get_malloc_blocks_outstanding (), ==, 0);
176 return !g_test_failed ();
179 /* Similar to test_array(), but making use of
180 * dbus_message_iter_abandon_container_if_open().
182 * Return TRUE if the right thing happens, but the right thing might include
185 test_zero_iter (void *ignored)
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;
194 /* This one was left uninitialized, just so we could exercise this
196 dbus_message_iter_init_closed (&inner_iter);
198 m = dbus_message_new_signal ("/", "a.b", "c");
203 dbus_message_iter_init_append (m, &iter);
205 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
209 if (!dbus_message_iter_open_container (&arr_iter, DBUS_TYPE_ARRAY, "i",
213 if (!dbus_message_iter_append_basic (&inner_iter, DBUS_TYPE_INT32, &fortytwo))
216 if (!dbus_message_iter_close_container (&arr_iter, &inner_iter))
219 if (!dbus_message_iter_close_container (&iter, &arr_iter))
222 message_should_be_complete = TRUE;
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);
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)
234 DBusBasicValue read_back;
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);
245 dbus_message_unref (m);
248 g_assert_cmpint (_dbus_get_malloc_blocks_outstanding (), ==, 0);
250 return !g_test_failed ();
256 DBusTestMemoryFunction function;
261 test_oom_wrapper (gconstpointer data)
263 const OOMTestCase *test = data;
265 if (!_dbus_test_oom_handling (test->name, test->function,
266 (void *) test->data))
268 g_test_message ("OOM test failed");
273 static GQueue *test_cases_to_free = NULL;
276 add_oom_test (const gchar *name,
277 DBusTestMemoryFunction function,
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);
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);
298 test_init (&argc, &argv);
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);
309 g_queue_free_full (test_cases_to_free, g_free);