1 /* Targeted unit tests for OOM paths in DBusServer
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 "dbus/dbus-server-socket.h"
36 #include "test-utils-glib.h"
38 /* Return TRUE if the right thing happens, but the right thing might include
41 test_new_tcp (void *user_data)
43 DBusError error = DBUS_ERROR_INIT;
44 DBusServer *server = NULL;
45 dbus_bool_t use_nonce = FALSE;
46 const char *bind = "localhost";
47 const char *family = NULL;
48 dbus_bool_t result = FALSE;
50 if (user_data != NULL)
52 if (strcmp (user_data, "nonce") == 0)
55 if (strcmp (user_data, "star") == 0)
58 if (strcmp (user_data, "v4") == 0)
62 server = _dbus_server_new_for_tcp_socket ("localhost", bind,
74 test_assert_no_error (&error);
78 g_assert_cmpstr (error.name, ==, DBUS_ERROR_NO_MEMORY);
83 dbus_server_disconnect (server);
85 dbus_clear_server (&server);
86 dbus_error_free (&error);
94 DBusTestMemoryFunction function;
99 test_oom_wrapper (gconstpointer data)
101 const OOMTestCase *test = data;
103 if (!_dbus_test_oom_handling (test->name, test->function,
104 (void *) test->data))
106 g_test_message ("OOM test failed");
111 static GQueue *test_cases_to_free = NULL;
114 add_oom_test (const gchar *name,
115 DBusTestMemoryFunction function,
118 /* By using GLib memory allocation here, we avoid being affected by
119 * dbus_shutdown() or contributing to
120 * _dbus_get_malloc_blocks_outstanding() */
121 OOMTestCase *test_case = g_new0 (OOMTestCase, 1);
123 test_case->name = name;
124 test_case->function = function;
125 test_case->data = data;
126 g_test_add_data_func (name, test_case, test_oom_wrapper);
127 g_queue_push_tail (test_cases_to_free, test_case);
136 test_init (&argc, &argv);
138 test_cases_to_free = g_queue_new ();
139 add_oom_test ("/server/new-tcp", test_new_tcp, NULL);
140 add_oom_test ("/server/new-nonce-tcp", test_new_tcp, "nonce");
141 add_oom_test ("/server/new-tcp-star", test_new_tcp, "star");
142 add_oom_test ("/server/new-tcp-v4", test_new_tcp, "v4");
146 g_queue_free_full (test_cases_to_free, g_free);