2011-04-15 13:32:48 +01:00
|
|
|
/* Regression test for being disconnected by a corrupt message (fd.o #15578)
|
|
|
|
|
*
|
|
|
|
|
* Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
|
|
|
|
|
* Copyright © 2010-2011 Nokia Corporation
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
|
* obtaining a copy of this software and associated documentation files
|
|
|
|
|
* (the "Software"), to deal in the Software without restriction,
|
|
|
|
|
* including without limitation the rights to use, copy, modify, merge,
|
|
|
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
|
|
|
* subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
#include <gio/gio.h>
|
|
|
|
|
|
|
|
|
|
#include <dbus/dbus.h>
|
Tests: allow dbus-glib to be replaced with use of libdbus-internal
We only use dbus-glib for its main loop; within dbus, DBusLoop is
available as an alternative, although it isn't thread-safe and
isn't public API.
For tests that otherwise only use libdbus public API, it's desirable to
be able to avoid DBusLoop, so we can run them against an installed
libdbus as an integration test. However, if we don't have dbus-glib,
we're going to have to use an in-tree main loop, which might as well
be DBusLoop.
The major disadvantage of using dbus-glib is that it isn't safe to
link both dbus-1 and dbus-internal at the same time. This is awkward
for a future test case that wants to use _dbus_getsid() in dbus-daemon.c,
but only on Windows (fd.o #54445). If we use the same API wrapper around
both dbus-glib and DBusLoop, we can compile that test against dbus-glib
or against DBusLoop, depending on the platform.
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=68852
Reviewed-by: Ralf Habacker <ralf.habacker@freenet.de>
2013-09-02 16:32:31 +01:00
|
|
|
|
2015-02-03 14:45:35 +00:00
|
|
|
#include "test-utils-glib.h"
|
2011-04-15 13:32:48 +01:00
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
DBusError e;
|
Tests: allow dbus-glib to be replaced with use of libdbus-internal
We only use dbus-glib for its main loop; within dbus, DBusLoop is
available as an alternative, although it isn't thread-safe and
isn't public API.
For tests that otherwise only use libdbus public API, it's desirable to
be able to avoid DBusLoop, so we can run them against an installed
libdbus as an integration test. However, if we don't have dbus-glib,
we're going to have to use an in-tree main loop, which might as well
be DBusLoop.
The major disadvantage of using dbus-glib is that it isn't safe to
link both dbus-1 and dbus-internal at the same time. This is awkward
for a future test case that wants to use _dbus_getsid() in dbus-daemon.c,
but only on Windows (fd.o #54445). If we use the same API wrapper around
both dbus-glib and DBusLoop, we can compile that test against dbus-glib
or against DBusLoop, depending on the platform.
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=68852
Reviewed-by: Ralf Habacker <ralf.habacker@freenet.de>
2013-09-02 16:32:31 +01:00
|
|
|
TestMainContext *ctx;
|
test: Skip TCP tests if getaddrinfo doesn't work
For example, this can be the case in bubblewrap or Debian pbuilder after
unsharing the network namespace:
bwrap \
--bind / / \
--dev-bind /dev /dev \
--bind /dev/shm /dev/shm \
--bind /dev/pts /dev/pts \
--unshare-net \
${builddir}/test/test-loopback --tap
...
ok 1 /connect/tcp # SKIP Name resolution does not work here:
getaddrinfo("127.0.0.1", "0", {flags=ADDRCONFIG, family=INET,
socktype=STREAM, protocol=TCP}): Name or service not known
On some systems this can be circumvented by using nss_wrapper from
<https://cwrap.org/nss_wrapper.html>:
cat > hosts <<EOF
127.0.0.1 localhost
EOF
bwrap \
... \
env \
LD_PRELOAD=libnss_wrapper.so \
NSS_WRAPPER_HOSTS=$(pwd)/hosts \
${builddir}/test/test-loopback --tap
...
# listening at tcp:host=127.0.0.1,port=39219,family=ipv4,guid=...
but for systems where that does't work, we should be prepared to skip
the affected tests.
Signed-off-by: Simon McVittie <smcv@collabora.com>
Reviewed-by: Philip Withnall <withnall@endlessm.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=106812
2018-06-04 16:27:50 +01:00
|
|
|
gboolean skip;
|
2011-04-15 13:32:48 +01:00
|
|
|
|
|
|
|
|
DBusServer *server;
|
|
|
|
|
DBusConnection *server_conn;
|
|
|
|
|
/* queue of DBusMessage */
|
|
|
|
|
GQueue client_messages;
|
|
|
|
|
|
|
|
|
|
DBusConnection *client_conn;
|
|
|
|
|
} Fixture;
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
assert_no_error (const DBusError *e)
|
|
|
|
|
{
|
|
|
|
|
if (G_UNLIKELY (dbus_error_is_set (e)))
|
|
|
|
|
g_error ("expected success but got error: %s: %s", e->name, e->message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static DBusHandlerResult
|
|
|
|
|
client_message_cb (DBusConnection *client_conn,
|
|
|
|
|
DBusMessage *message,
|
|
|
|
|
void *data)
|
|
|
|
|
{
|
|
|
|
|
Fixture *f = data;
|
|
|
|
|
|
|
|
|
|
g_assert (client_conn == f->client_conn);
|
|
|
|
|
g_queue_push_tail (&f->client_messages, dbus_message_ref (message));
|
|
|
|
|
|
|
|
|
|
return DBUS_HANDLER_RESULT_HANDLED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
new_conn_cb (DBusServer *server,
|
|
|
|
|
DBusConnection *server_conn,
|
|
|
|
|
void *data)
|
|
|
|
|
{
|
|
|
|
|
Fixture *f = data;
|
|
|
|
|
|
|
|
|
|
g_assert (f->server_conn == NULL);
|
|
|
|
|
f->server_conn = dbus_connection_ref (server_conn);
|
Tests: allow dbus-glib to be replaced with use of libdbus-internal
We only use dbus-glib for its main loop; within dbus, DBusLoop is
available as an alternative, although it isn't thread-safe and
isn't public API.
For tests that otherwise only use libdbus public API, it's desirable to
be able to avoid DBusLoop, so we can run them against an installed
libdbus as an integration test. However, if we don't have dbus-glib,
we're going to have to use an in-tree main loop, which might as well
be DBusLoop.
The major disadvantage of using dbus-glib is that it isn't safe to
link both dbus-1 and dbus-internal at the same time. This is awkward
for a future test case that wants to use _dbus_getsid() in dbus-daemon.c,
but only on Windows (fd.o #54445). If we use the same API wrapper around
both dbus-glib and DBusLoop, we can compile that test against dbus-glib
or against DBusLoop, depending on the platform.
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=68852
Reviewed-by: Ralf Habacker <ralf.habacker@freenet.de>
2013-09-02 16:32:31 +01:00
|
|
|
test_connection_setup (f->ctx, server_conn);
|
2011-04-15 13:32:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
setup (Fixture *f,
|
|
|
|
|
gconstpointer addr)
|
|
|
|
|
{
|
Tests: allow dbus-glib to be replaced with use of libdbus-internal
We only use dbus-glib for its main loop; within dbus, DBusLoop is
available as an alternative, although it isn't thread-safe and
isn't public API.
For tests that otherwise only use libdbus public API, it's desirable to
be able to avoid DBusLoop, so we can run them against an installed
libdbus as an integration test. However, if we don't have dbus-glib,
we're going to have to use an in-tree main loop, which might as well
be DBusLoop.
The major disadvantage of using dbus-glib is that it isn't safe to
link both dbus-1 and dbus-internal at the same time. This is awkward
for a future test case that wants to use _dbus_getsid() in dbus-daemon.c,
but only on Windows (fd.o #54445). If we use the same API wrapper around
both dbus-glib and DBusLoop, we can compile that test against dbus-glib
or against DBusLoop, depending on the platform.
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=68852
Reviewed-by: Ralf Habacker <ralf.habacker@freenet.de>
2013-09-02 16:32:31 +01:00
|
|
|
f->ctx = test_main_context_get ();
|
2011-04-15 13:32:48 +01:00
|
|
|
dbus_error_init (&f->e);
|
|
|
|
|
g_queue_init (&f->client_messages);
|
|
|
|
|
|
test: Skip TCP tests if getaddrinfo doesn't work
For example, this can be the case in bubblewrap or Debian pbuilder after
unsharing the network namespace:
bwrap \
--bind / / \
--dev-bind /dev /dev \
--bind /dev/shm /dev/shm \
--bind /dev/pts /dev/pts \
--unshare-net \
${builddir}/test/test-loopback --tap
...
ok 1 /connect/tcp # SKIP Name resolution does not work here:
getaddrinfo("127.0.0.1", "0", {flags=ADDRCONFIG, family=INET,
socktype=STREAM, protocol=TCP}): Name or service not known
On some systems this can be circumvented by using nss_wrapper from
<https://cwrap.org/nss_wrapper.html>:
cat > hosts <<EOF
127.0.0.1 localhost
EOF
bwrap \
... \
env \
LD_PRELOAD=libnss_wrapper.so \
NSS_WRAPPER_HOSTS=$(pwd)/hosts \
${builddir}/test/test-loopback --tap
...
# listening at tcp:host=127.0.0.1,port=39219,family=ipv4,guid=...
but for systems where that does't work, we should be prepared to skip
the affected tests.
Signed-off-by: Simon McVittie <smcv@collabora.com>
Reviewed-by: Philip Withnall <withnall@endlessm.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=106812
2018-06-04 16:27:50 +01:00
|
|
|
if ((g_str_has_prefix (addr, "tcp:") ||
|
|
|
|
|
g_str_has_prefix (addr, "nonce-tcp:")) &&
|
|
|
|
|
!test_check_tcp_works ())
|
|
|
|
|
{
|
|
|
|
|
f->skip = TRUE;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-15 13:32:48 +01:00
|
|
|
f->server = dbus_server_listen (addr, &f->e);
|
|
|
|
|
assert_no_error (&f->e);
|
|
|
|
|
g_assert (f->server != NULL);
|
|
|
|
|
|
|
|
|
|
dbus_server_set_new_connection_function (f->server,
|
|
|
|
|
new_conn_cb, f, NULL);
|
Tests: allow dbus-glib to be replaced with use of libdbus-internal
We only use dbus-glib for its main loop; within dbus, DBusLoop is
available as an alternative, although it isn't thread-safe and
isn't public API.
For tests that otherwise only use libdbus public API, it's desirable to
be able to avoid DBusLoop, so we can run them against an installed
libdbus as an integration test. However, if we don't have dbus-glib,
we're going to have to use an in-tree main loop, which might as well
be DBusLoop.
The major disadvantage of using dbus-glib is that it isn't safe to
link both dbus-1 and dbus-internal at the same time. This is awkward
for a future test case that wants to use _dbus_getsid() in dbus-daemon.c,
but only on Windows (fd.o #54445). If we use the same API wrapper around
both dbus-glib and DBusLoop, we can compile that test against dbus-glib
or against DBusLoop, depending on the platform.
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=68852
Reviewed-by: Ralf Habacker <ralf.habacker@freenet.de>
2013-09-02 16:32:31 +01:00
|
|
|
test_server_setup (f->ctx, f->server);
|
2011-04-15 13:32:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_connect (Fixture *f,
|
|
|
|
|
gconstpointer addr G_GNUC_UNUSED)
|
|
|
|
|
{
|
|
|
|
|
dbus_bool_t have_mem;
|
2017-04-05 11:36:12 +01:00
|
|
|
char *address = NULL;
|
2011-04-15 13:32:48 +01:00
|
|
|
|
test: Skip TCP tests if getaddrinfo doesn't work
For example, this can be the case in bubblewrap or Debian pbuilder after
unsharing the network namespace:
bwrap \
--bind / / \
--dev-bind /dev /dev \
--bind /dev/shm /dev/shm \
--bind /dev/pts /dev/pts \
--unshare-net \
${builddir}/test/test-loopback --tap
...
ok 1 /connect/tcp # SKIP Name resolution does not work here:
getaddrinfo("127.0.0.1", "0", {flags=ADDRCONFIG, family=INET,
socktype=STREAM, protocol=TCP}): Name or service not known
On some systems this can be circumvented by using nss_wrapper from
<https://cwrap.org/nss_wrapper.html>:
cat > hosts <<EOF
127.0.0.1 localhost
EOF
bwrap \
... \
env \
LD_PRELOAD=libnss_wrapper.so \
NSS_WRAPPER_HOSTS=$(pwd)/hosts \
${builddir}/test/test-loopback --tap
...
# listening at tcp:host=127.0.0.1,port=39219,family=ipv4,guid=...
but for systems where that does't work, we should be prepared to skip
the affected tests.
Signed-off-by: Simon McVittie <smcv@collabora.com>
Reviewed-by: Philip Withnall <withnall@endlessm.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=106812
2018-06-04 16:27:50 +01:00
|
|
|
if (f->skip)
|
|
|
|
|
return;
|
|
|
|
|
|
2011-04-15 13:32:48 +01:00
|
|
|
g_assert (f->server_conn == NULL);
|
|
|
|
|
|
2017-04-05 11:36:12 +01:00
|
|
|
address = dbus_server_get_address (f->server);
|
|
|
|
|
f->client_conn = dbus_connection_open_private (address, &f->e);
|
2011-04-15 13:32:48 +01:00
|
|
|
assert_no_error (&f->e);
|
|
|
|
|
g_assert (f->client_conn != NULL);
|
Tests: allow dbus-glib to be replaced with use of libdbus-internal
We only use dbus-glib for its main loop; within dbus, DBusLoop is
available as an alternative, although it isn't thread-safe and
isn't public API.
For tests that otherwise only use libdbus public API, it's desirable to
be able to avoid DBusLoop, so we can run them against an installed
libdbus as an integration test. However, if we don't have dbus-glib,
we're going to have to use an in-tree main loop, which might as well
be DBusLoop.
The major disadvantage of using dbus-glib is that it isn't safe to
link both dbus-1 and dbus-internal at the same time. This is awkward
for a future test case that wants to use _dbus_getsid() in dbus-daemon.c,
but only on Windows (fd.o #54445). If we use the same API wrapper around
both dbus-glib and DBusLoop, we can compile that test against dbus-glib
or against DBusLoop, depending on the platform.
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=68852
Reviewed-by: Ralf Habacker <ralf.habacker@freenet.de>
2013-09-02 16:32:31 +01:00
|
|
|
test_connection_setup (f->ctx, f->client_conn);
|
2017-04-05 11:36:12 +01:00
|
|
|
dbus_free (address);
|
2011-04-15 13:32:48 +01:00
|
|
|
|
|
|
|
|
while (f->server_conn == NULL)
|
|
|
|
|
{
|
2015-02-26 17:12:22 +00:00
|
|
|
test_progress ('.');
|
Tests: allow dbus-glib to be replaced with use of libdbus-internal
We only use dbus-glib for its main loop; within dbus, DBusLoop is
available as an alternative, although it isn't thread-safe and
isn't public API.
For tests that otherwise only use libdbus public API, it's desirable to
be able to avoid DBusLoop, so we can run them against an installed
libdbus as an integration test. However, if we don't have dbus-glib,
we're going to have to use an in-tree main loop, which might as well
be DBusLoop.
The major disadvantage of using dbus-glib is that it isn't safe to
link both dbus-1 and dbus-internal at the same time. This is awkward
for a future test case that wants to use _dbus_getsid() in dbus-daemon.c,
but only on Windows (fd.o #54445). If we use the same API wrapper around
both dbus-glib and DBusLoop, we can compile that test against dbus-glib
or against DBusLoop, depending on the platform.
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=68852
Reviewed-by: Ralf Habacker <ralf.habacker@freenet.de>
2013-09-02 16:32:31 +01:00
|
|
|
test_main_context_iterate (f->ctx, TRUE);
|
2011-04-15 13:32:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
have_mem = dbus_connection_add_filter (f->client_conn,
|
|
|
|
|
client_message_cb, f, NULL);
|
|
|
|
|
g_assert (have_mem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_message (Fixture *f,
|
|
|
|
|
gconstpointer addr)
|
|
|
|
|
{
|
|
|
|
|
dbus_bool_t have_mem;
|
|
|
|
|
dbus_uint32_t serial;
|
|
|
|
|
DBusMessage *outgoing, *incoming;
|
|
|
|
|
|
test: Skip TCP tests if getaddrinfo doesn't work
For example, this can be the case in bubblewrap or Debian pbuilder after
unsharing the network namespace:
bwrap \
--bind / / \
--dev-bind /dev /dev \
--bind /dev/shm /dev/shm \
--bind /dev/pts /dev/pts \
--unshare-net \
${builddir}/test/test-loopback --tap
...
ok 1 /connect/tcp # SKIP Name resolution does not work here:
getaddrinfo("127.0.0.1", "0", {flags=ADDRCONFIG, family=INET,
socktype=STREAM, protocol=TCP}): Name or service not known
On some systems this can be circumvented by using nss_wrapper from
<https://cwrap.org/nss_wrapper.html>:
cat > hosts <<EOF
127.0.0.1 localhost
EOF
bwrap \
... \
env \
LD_PRELOAD=libnss_wrapper.so \
NSS_WRAPPER_HOSTS=$(pwd)/hosts \
${builddir}/test/test-loopback --tap
...
# listening at tcp:host=127.0.0.1,port=39219,family=ipv4,guid=...
but for systems where that does't work, we should be prepared to skip
the affected tests.
Signed-off-by: Simon McVittie <smcv@collabora.com>
Reviewed-by: Philip Withnall <withnall@endlessm.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=106812
2018-06-04 16:27:50 +01:00
|
|
|
if (f->skip)
|
|
|
|
|
return;
|
|
|
|
|
|
2011-04-15 13:32:48 +01:00
|
|
|
test_connect (f, addr);
|
|
|
|
|
|
|
|
|
|
outgoing = dbus_message_new_signal ("/com/example/Hello",
|
|
|
|
|
"com.example.Hello", "Greeting");
|
|
|
|
|
g_assert (outgoing != NULL);
|
|
|
|
|
|
|
|
|
|
have_mem = dbus_connection_send (f->server_conn, outgoing, &serial);
|
|
|
|
|
g_assert (have_mem);
|
|
|
|
|
g_assert (serial != 0);
|
|
|
|
|
|
|
|
|
|
while (g_queue_is_empty (&f->client_messages))
|
|
|
|
|
{
|
2015-02-26 17:12:22 +00:00
|
|
|
test_progress ('.');
|
Tests: allow dbus-glib to be replaced with use of libdbus-internal
We only use dbus-glib for its main loop; within dbus, DBusLoop is
available as an alternative, although it isn't thread-safe and
isn't public API.
For tests that otherwise only use libdbus public API, it's desirable to
be able to avoid DBusLoop, so we can run them against an installed
libdbus as an integration test. However, if we don't have dbus-glib,
we're going to have to use an in-tree main loop, which might as well
be DBusLoop.
The major disadvantage of using dbus-glib is that it isn't safe to
link both dbus-1 and dbus-internal at the same time. This is awkward
for a future test case that wants to use _dbus_getsid() in dbus-daemon.c,
but only on Windows (fd.o #54445). If we use the same API wrapper around
both dbus-glib and DBusLoop, we can compile that test against dbus-glib
or against DBusLoop, depending on the platform.
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=68852
Reviewed-by: Ralf Habacker <ralf.habacker@freenet.de>
2013-09-02 16:32:31 +01:00
|
|
|
test_main_context_iterate (f->ctx, TRUE);
|
2011-04-15 13:32:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_assert_cmpuint (g_queue_get_length (&f->client_messages), ==, 1);
|
|
|
|
|
|
|
|
|
|
incoming = g_queue_pop_head (&f->client_messages);
|
|
|
|
|
|
|
|
|
|
g_assert (!dbus_message_contains_unix_fds (incoming));
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
|
|
|
|
|
"com.example.Hello");
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Greeting");
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_signature (incoming), ==, "");
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_path (incoming), ==, "/com/example/Hello");
|
|
|
|
|
g_assert_cmpuint (dbus_message_get_serial (incoming), ==, serial);
|
|
|
|
|
|
|
|
|
|
dbus_message_unref (incoming);
|
|
|
|
|
|
|
|
|
|
dbus_message_unref (outgoing);
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-15 18:27:27 +01:00
|
|
|
static void
|
|
|
|
|
send_n_bytes (GSocket *socket,
|
|
|
|
|
const gchar *blob,
|
|
|
|
|
gssize blob_len)
|
|
|
|
|
{
|
|
|
|
|
gssize len, total_sent;
|
|
|
|
|
GError *gerror = NULL;
|
|
|
|
|
|
|
|
|
|
total_sent = 0;
|
|
|
|
|
|
|
|
|
|
while (total_sent < blob_len)
|
|
|
|
|
{
|
|
|
|
|
len = g_socket_send (socket,
|
|
|
|
|
blob + total_sent,
|
|
|
|
|
blob_len - total_sent,
|
|
|
|
|
NULL, &gerror);
|
|
|
|
|
|
|
|
|
|
/* this is NULL-safe: a NULL error does not match */
|
|
|
|
|
if (g_error_matches (gerror, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
|
|
|
|
|
{
|
|
|
|
|
/* we could wait for G_IO_OUT, but life's too short; just sleep */
|
|
|
|
|
g_clear_error (&gerror);
|
|
|
|
|
g_usleep (G_USEC_PER_SEC / 10);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_assert_no_error (gerror);
|
|
|
|
|
g_assert (len >= 0);
|
|
|
|
|
total_sent += len;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-15 13:32:48 +01:00
|
|
|
/* Enough bytes for it to be obvious that this connection is broken */
|
|
|
|
|
#define CORRUPT_LEN 1024
|
|
|
|
|
|
|
|
|
|
/* All-zero is not a valid D-Bus message header - for a start, this is
|
|
|
|
|
* protocol version 1, not 0 */
|
|
|
|
|
static const gchar not_a_dbus_message[CORRUPT_LEN] = { 0 };
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_corrupt (Fixture *f,
|
|
|
|
|
gconstpointer addr)
|
|
|
|
|
{
|
|
|
|
|
GSocket *socket;
|
|
|
|
|
GError *gerror = NULL;
|
|
|
|
|
int fd;
|
|
|
|
|
DBusMessage *incoming;
|
|
|
|
|
|
test: Skip TCP tests if getaddrinfo doesn't work
For example, this can be the case in bubblewrap or Debian pbuilder after
unsharing the network namespace:
bwrap \
--bind / / \
--dev-bind /dev /dev \
--bind /dev/shm /dev/shm \
--bind /dev/pts /dev/pts \
--unshare-net \
${builddir}/test/test-loopback --tap
...
ok 1 /connect/tcp # SKIP Name resolution does not work here:
getaddrinfo("127.0.0.1", "0", {flags=ADDRCONFIG, family=INET,
socktype=STREAM, protocol=TCP}): Name or service not known
On some systems this can be circumvented by using nss_wrapper from
<https://cwrap.org/nss_wrapper.html>:
cat > hosts <<EOF
127.0.0.1 localhost
EOF
bwrap \
... \
env \
LD_PRELOAD=libnss_wrapper.so \
NSS_WRAPPER_HOSTS=$(pwd)/hosts \
${builddir}/test/test-loopback --tap
...
# listening at tcp:host=127.0.0.1,port=39219,family=ipv4,guid=...
but for systems where that does't work, we should be prepared to skip
the affected tests.
Signed-off-by: Simon McVittie <smcv@collabora.com>
Reviewed-by: Philip Withnall <withnall@endlessm.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=106812
2018-06-04 16:27:50 +01:00
|
|
|
if (f->skip)
|
|
|
|
|
return;
|
|
|
|
|
|
2011-04-15 13:32:48 +01:00
|
|
|
test_message (f, addr);
|
|
|
|
|
|
|
|
|
|
dbus_connection_flush (f->server_conn);
|
|
|
|
|
|
|
|
|
|
/* OK, now the connection is working, let's break it! Don't try this
|
|
|
|
|
* at home; splicing arbitrary bytes into the middle of the stream is
|
|
|
|
|
* specifically documented as not a valid thing to do. Who'd have thought? */
|
|
|
|
|
if (!dbus_connection_get_socket (f->server_conn, &fd))
|
|
|
|
|
g_error ("failed to steal fd from server connection");
|
|
|
|
|
|
|
|
|
|
socket = g_socket_new_from_fd (fd, &gerror);
|
|
|
|
|
g_assert_no_error (gerror);
|
|
|
|
|
g_assert (socket != NULL);
|
|
|
|
|
|
2011-09-15 18:27:27 +01:00
|
|
|
send_n_bytes (socket, not_a_dbus_message, CORRUPT_LEN);
|
2011-04-15 13:32:48 +01:00
|
|
|
|
|
|
|
|
/* Now spin on the client connection: the server just sent it complete
|
|
|
|
|
* rubbish, so it should disconnect */
|
|
|
|
|
while (g_queue_is_empty (&f->client_messages))
|
|
|
|
|
{
|
2015-02-26 17:12:22 +00:00
|
|
|
test_progress ('.');
|
Tests: allow dbus-glib to be replaced with use of libdbus-internal
We only use dbus-glib for its main loop; within dbus, DBusLoop is
available as an alternative, although it isn't thread-safe and
isn't public API.
For tests that otherwise only use libdbus public API, it's desirable to
be able to avoid DBusLoop, so we can run them against an installed
libdbus as an integration test. However, if we don't have dbus-glib,
we're going to have to use an in-tree main loop, which might as well
be DBusLoop.
The major disadvantage of using dbus-glib is that it isn't safe to
link both dbus-1 and dbus-internal at the same time. This is awkward
for a future test case that wants to use _dbus_getsid() in dbus-daemon.c,
but only on Windows (fd.o #54445). If we use the same API wrapper around
both dbus-glib and DBusLoop, we can compile that test against dbus-glib
or against DBusLoop, depending on the platform.
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=68852
Reviewed-by: Ralf Habacker <ralf.habacker@freenet.de>
2013-09-02 16:32:31 +01:00
|
|
|
test_main_context_iterate (f->ctx, TRUE);
|
2011-04-15 13:32:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
incoming = g_queue_pop_head (&f->client_messages);
|
|
|
|
|
|
|
|
|
|
g_assert (!dbus_message_contains_unix_fds (incoming));
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
|
|
|
|
|
"org.freedesktop.DBus.Local");
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Disconnected");
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_signature (incoming), ==, "");
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_path (incoming), ==,
|
|
|
|
|
"/org/freedesktop/DBus/Local");
|
|
|
|
|
|
|
|
|
|
dbus_message_unref (incoming);
|
2013-09-02 17:14:38 +01:00
|
|
|
|
|
|
|
|
/* Free the DBusConnection before the GSocket, because GSocket is
|
|
|
|
|
* going to close our fd. GSocket tolerates closing an already-closed
|
|
|
|
|
* fd, whereas DBusLoop + DBusSocketSetEpoll doesn't. On Unix
|
|
|
|
|
* we could use dup() but that isn't portable to Windows :-(
|
|
|
|
|
*/
|
2018-07-12 12:51:38 +01:00
|
|
|
test_connection_shutdown (f->ctx, f->server_conn);
|
2013-09-02 17:14:38 +01:00
|
|
|
dbus_connection_close (f->server_conn);
|
|
|
|
|
dbus_connection_unref (f->server_conn);
|
|
|
|
|
f->server_conn = NULL;
|
|
|
|
|
|
2011-09-15 18:27:27 +01:00
|
|
|
g_object_unref (socket);
|
2011-04-15 13:32:48 +01:00
|
|
|
}
|
|
|
|
|
|
2011-06-09 17:43:34 +01:00
|
|
|
static void
|
|
|
|
|
test_byte_order (Fixture *f,
|
|
|
|
|
gconstpointer addr)
|
|
|
|
|
{
|
|
|
|
|
GSocket *socket;
|
|
|
|
|
GError *gerror = NULL;
|
|
|
|
|
int fd;
|
|
|
|
|
char *blob;
|
|
|
|
|
const gchar *arg = not_a_dbus_message;
|
2011-09-15 18:27:27 +01:00
|
|
|
int blob_len;
|
2011-06-09 17:43:34 +01:00
|
|
|
DBusMessage *message;
|
|
|
|
|
dbus_bool_t mem;
|
|
|
|
|
|
test: Skip TCP tests if getaddrinfo doesn't work
For example, this can be the case in bubblewrap or Debian pbuilder after
unsharing the network namespace:
bwrap \
--bind / / \
--dev-bind /dev /dev \
--bind /dev/shm /dev/shm \
--bind /dev/pts /dev/pts \
--unshare-net \
${builddir}/test/test-loopback --tap
...
ok 1 /connect/tcp # SKIP Name resolution does not work here:
getaddrinfo("127.0.0.1", "0", {flags=ADDRCONFIG, family=INET,
socktype=STREAM, protocol=TCP}): Name or service not known
On some systems this can be circumvented by using nss_wrapper from
<https://cwrap.org/nss_wrapper.html>:
cat > hosts <<EOF
127.0.0.1 localhost
EOF
bwrap \
... \
env \
LD_PRELOAD=libnss_wrapper.so \
NSS_WRAPPER_HOSTS=$(pwd)/hosts \
${builddir}/test/test-loopback --tap
...
# listening at tcp:host=127.0.0.1,port=39219,family=ipv4,guid=...
but for systems where that does't work, we should be prepared to skip
the affected tests.
Signed-off-by: Simon McVittie <smcv@collabora.com>
Reviewed-by: Philip Withnall <withnall@endlessm.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=106812
2018-06-04 16:27:50 +01:00
|
|
|
if (f->skip)
|
|
|
|
|
return;
|
|
|
|
|
|
2011-06-09 17:43:34 +01:00
|
|
|
test_message (f, addr);
|
|
|
|
|
|
|
|
|
|
message = dbus_message_new_signal ("/", "a.b", "c");
|
|
|
|
|
g_assert (message != NULL);
|
|
|
|
|
/* Append 0xFF bytes, so that the length of the body when byte-swapped
|
|
|
|
|
* is 0xFF000000, which is invalid */
|
|
|
|
|
mem = dbus_message_append_args (message,
|
2017-04-05 11:35:27 +01:00
|
|
|
DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &arg, 0xFF,
|
2011-06-09 17:43:34 +01:00
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
|
g_assert (mem);
|
|
|
|
|
mem = dbus_message_marshal (message, &blob, &blob_len);
|
|
|
|
|
g_assert (mem);
|
|
|
|
|
g_assert_cmpuint (blob_len, >, 0xFF);
|
|
|
|
|
g_assert (blob != NULL);
|
|
|
|
|
|
|
|
|
|
dbus_message_unref (message);
|
|
|
|
|
|
|
|
|
|
/* Break the message by changing its claimed byte order, without actually
|
|
|
|
|
* byteswapping anything. We happen to know that byte order is the first
|
|
|
|
|
* byte. */
|
|
|
|
|
if (blob[0] == 'B')
|
|
|
|
|
blob[0] = 'l';
|
|
|
|
|
else
|
|
|
|
|
blob[0] = 'B';
|
|
|
|
|
|
|
|
|
|
/* OK, now the connection is working, let's break it */
|
|
|
|
|
|
|
|
|
|
dbus_connection_flush (f->server_conn);
|
|
|
|
|
|
|
|
|
|
if (!dbus_connection_get_socket (f->server_conn, &fd))
|
|
|
|
|
g_error ("failed to steal fd from server connection");
|
|
|
|
|
|
|
|
|
|
socket = g_socket_new_from_fd (fd, &gerror);
|
|
|
|
|
g_assert_no_error (gerror);
|
|
|
|
|
g_assert (socket != NULL);
|
|
|
|
|
|
2011-09-15 18:27:27 +01:00
|
|
|
send_n_bytes (socket, blob, blob_len);
|
2011-06-09 17:43:34 +01:00
|
|
|
|
|
|
|
|
dbus_free (blob);
|
|
|
|
|
|
|
|
|
|
/* Now spin on the client connection: the server just sent it a faulty
|
|
|
|
|
* message, so it should disconnect */
|
|
|
|
|
while (g_queue_is_empty (&f->client_messages))
|
|
|
|
|
{
|
2015-02-26 17:12:22 +00:00
|
|
|
test_progress ('.');
|
Tests: allow dbus-glib to be replaced with use of libdbus-internal
We only use dbus-glib for its main loop; within dbus, DBusLoop is
available as an alternative, although it isn't thread-safe and
isn't public API.
For tests that otherwise only use libdbus public API, it's desirable to
be able to avoid DBusLoop, so we can run them against an installed
libdbus as an integration test. However, if we don't have dbus-glib,
we're going to have to use an in-tree main loop, which might as well
be DBusLoop.
The major disadvantage of using dbus-glib is that it isn't safe to
link both dbus-1 and dbus-internal at the same time. This is awkward
for a future test case that wants to use _dbus_getsid() in dbus-daemon.c,
but only on Windows (fd.o #54445). If we use the same API wrapper around
both dbus-glib and DBusLoop, we can compile that test against dbus-glib
or against DBusLoop, depending on the platform.
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=68852
Reviewed-by: Ralf Habacker <ralf.habacker@freenet.de>
2013-09-02 16:32:31 +01:00
|
|
|
test_main_context_iterate (f->ctx, TRUE);
|
2011-06-09 17:43:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
message = g_queue_pop_head (&f->client_messages);
|
|
|
|
|
|
|
|
|
|
g_assert (!dbus_message_contains_unix_fds (message));
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_destination (message), ==, NULL);
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_error_name (message), ==, NULL);
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_interface (message), ==,
|
|
|
|
|
"org.freedesktop.DBus.Local");
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_member (message), ==, "Disconnected");
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_sender (message), ==, NULL);
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_signature (message), ==, "");
|
|
|
|
|
g_assert_cmpstr (dbus_message_get_path (message), ==,
|
|
|
|
|
"/org/freedesktop/DBus/Local");
|
|
|
|
|
|
|
|
|
|
dbus_message_unref (message);
|
2013-09-02 17:14:38 +01:00
|
|
|
|
|
|
|
|
/* Free the DBusConnection before the GSocket, as above. */
|
2018-07-12 12:51:38 +01:00
|
|
|
test_connection_shutdown (f->ctx, f->server_conn);
|
2013-09-02 17:14:38 +01:00
|
|
|
dbus_connection_close (f->server_conn);
|
|
|
|
|
dbus_connection_unref (f->server_conn);
|
|
|
|
|
f->server_conn = NULL;
|
|
|
|
|
|
2011-09-15 18:27:27 +01:00
|
|
|
g_object_unref (socket);
|
2011-06-09 17:43:34 +01:00
|
|
|
}
|
|
|
|
|
|
2011-04-15 13:32:48 +01:00
|
|
|
static void
|
|
|
|
|
teardown (Fixture *f,
|
|
|
|
|
gconstpointer addr G_GNUC_UNUSED)
|
|
|
|
|
{
|
|
|
|
|
if (f->client_conn != NULL)
|
|
|
|
|
{
|
2017-04-05 11:36:12 +01:00
|
|
|
test_connection_shutdown (f->ctx, f->client_conn);
|
2011-04-15 13:32:48 +01:00
|
|
|
dbus_connection_close (f->client_conn);
|
|
|
|
|
dbus_connection_unref (f->client_conn);
|
|
|
|
|
f->client_conn = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (f->server_conn != NULL)
|
|
|
|
|
{
|
2017-04-05 11:36:12 +01:00
|
|
|
test_connection_shutdown (f->ctx, f->server_conn);
|
2011-04-15 13:32:48 +01:00
|
|
|
dbus_connection_close (f->server_conn);
|
|
|
|
|
dbus_connection_unref (f->server_conn);
|
|
|
|
|
f->server_conn = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (f->server != NULL)
|
|
|
|
|
{
|
2018-07-11 16:22:49 +01:00
|
|
|
test_server_shutdown (f->ctx, f->server);
|
2011-04-15 13:32:48 +01:00
|
|
|
dbus_server_unref (f->server);
|
|
|
|
|
f->server = NULL;
|
|
|
|
|
}
|
Tests: allow dbus-glib to be replaced with use of libdbus-internal
We only use dbus-glib for its main loop; within dbus, DBusLoop is
available as an alternative, although it isn't thread-safe and
isn't public API.
For tests that otherwise only use libdbus public API, it's desirable to
be able to avoid DBusLoop, so we can run them against an installed
libdbus as an integration test. However, if we don't have dbus-glib,
we're going to have to use an in-tree main loop, which might as well
be DBusLoop.
The major disadvantage of using dbus-glib is that it isn't safe to
link both dbus-1 and dbus-internal at the same time. This is awkward
for a future test case that wants to use _dbus_getsid() in dbus-daemon.c,
but only on Windows (fd.o #54445). If we use the same API wrapper around
both dbus-glib and DBusLoop, we can compile that test against dbus-glib
or against DBusLoop, depending on the platform.
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=68852
Reviewed-by: Ralf Habacker <ralf.habacker@freenet.de>
2013-09-02 16:32:31 +01:00
|
|
|
|
|
|
|
|
test_main_context_unref (f->ctx);
|
2011-04-15 13:32:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main (int argc,
|
|
|
|
|
char **argv)
|
|
|
|
|
{
|
2018-07-11 16:40:12 +01:00
|
|
|
int ret;
|
|
|
|
|
|
2015-02-03 14:45:35 +00:00
|
|
|
test_init (&argc, &argv);
|
2011-04-15 13:32:48 +01:00
|
|
|
|
|
|
|
|
g_test_add ("/corrupt/tcp", Fixture, "tcp:host=127.0.0.1", setup,
|
|
|
|
|
test_corrupt, teardown);
|
|
|
|
|
|
|
|
|
|
#ifdef DBUS_UNIX
|
|
|
|
|
g_test_add ("/corrupt/unix", Fixture, "unix:tmpdir=/tmp", setup,
|
|
|
|
|
test_corrupt, teardown);
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-06-09 17:43:34 +01:00
|
|
|
g_test_add ("/corrupt/byte-order/tcp", Fixture, "tcp:host=127.0.0.1", setup,
|
|
|
|
|
test_byte_order, teardown);
|
|
|
|
|
|
2018-06-04 16:27:49 +01:00
|
|
|
#ifdef DBUS_UNIX
|
|
|
|
|
g_test_add ("/corrupt/byte-order/unix", Fixture, "unix:tmpdir=/tmp", setup,
|
|
|
|
|
test_byte_order, teardown);
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-07-11 16:40:12 +01:00
|
|
|
ret = g_test_run ();
|
|
|
|
|
dbus_shutdown ();
|
|
|
|
|
return ret;
|
2011-04-15 13:32:48 +01:00
|
|
|
}
|