2015-02-27 17:19:33 +01:00
|
|
|
/*
|
|
|
|
* QEMU I/O channel sockets test
|
|
|
|
*
|
2016-03-03 17:16:48 +01:00
|
|
|
* Copyright (c) 2015-2016 Red Hat, Inc.
|
2015-02-27 17:19:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2020-10-14 15:40:33 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2015-02-27 17:19:33 +01:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-02-08 19:08:51 +01:00
|
|
|
#include "qemu/osdep.h"
|
2015-02-27 17:19:33 +01:00
|
|
|
#include "io/channel-socket.h"
|
2016-02-03 15:00:28 +01:00
|
|
|
#include "io/channel-util.h"
|
2015-02-27 17:19:33 +01:00
|
|
|
#include "io-channel-helpers.h"
|
2017-12-22 12:45:24 +01:00
|
|
|
#include "socket-helpers.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 09:01:28 +01:00
|
|
|
#include "qapi/error.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2020-09-15 14:13:09 +02:00
|
|
|
#include "qemu/main-loop.h"
|
2015-02-27 17:19:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
static void test_io_channel_set_socket_bufs(QIOChannel *src,
|
|
|
|
QIOChannel *dst)
|
|
|
|
{
|
|
|
|
int buflen = 64 * 1024;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make the socket buffers small so that we see
|
|
|
|
* the effects of partial reads/writes
|
|
|
|
*/
|
|
|
|
setsockopt(((QIOChannelSocket *)src)->fd,
|
|
|
|
SOL_SOCKET, SO_SNDBUF,
|
|
|
|
(char *)&buflen,
|
|
|
|
sizeof(buflen));
|
|
|
|
|
|
|
|
setsockopt(((QIOChannelSocket *)dst)->fd,
|
|
|
|
SOL_SOCKET, SO_SNDBUF,
|
|
|
|
(char *)&buflen,
|
|
|
|
sizeof(buflen));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-26 09:36:41 +02:00
|
|
|
static void test_io_channel_setup_sync(SocketAddress *listen_addr,
|
|
|
|
SocketAddress *connect_addr,
|
2019-01-14 12:33:18 +01:00
|
|
|
QIOChannel **srv,
|
2015-02-27 17:19:33 +01:00
|
|
|
QIOChannel **src,
|
|
|
|
QIOChannel **dst)
|
|
|
|
{
|
|
|
|
QIOChannelSocket *lioc;
|
|
|
|
|
|
|
|
lioc = qio_channel_socket_new();
|
2019-08-19 15:29:58 +02:00
|
|
|
qio_channel_socket_listen_sync(lioc, listen_addr, 1, &error_abort);
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2017-04-26 09:36:41 +02:00
|
|
|
if (listen_addr->type == SOCKET_ADDRESS_TYPE_INET) {
|
|
|
|
SocketAddress *laddr = qio_channel_socket_get_local_address(
|
2015-02-27 17:19:33 +01:00
|
|
|
lioc, &error_abort);
|
|
|
|
|
2017-04-26 09:36:41 +02:00
|
|
|
g_free(connect_addr->u.inet.port);
|
|
|
|
connect_addr->u.inet.port = g_strdup(laddr->u.inet.port);
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2017-04-26 09:36:41 +02:00
|
|
|
qapi_free_SocketAddress(laddr);
|
2015-02-27 17:19:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*src = QIO_CHANNEL(qio_channel_socket_new());
|
|
|
|
qio_channel_socket_connect_sync(
|
|
|
|
QIO_CHANNEL_SOCKET(*src), connect_addr, &error_abort);
|
|
|
|
qio_channel_set_delay(*src, false);
|
|
|
|
|
2016-03-08 16:26:39 +01:00
|
|
|
qio_channel_wait(QIO_CHANNEL(lioc), G_IO_IN);
|
2015-02-27 17:19:33 +01:00
|
|
|
*dst = QIO_CHANNEL(qio_channel_socket_accept(lioc, &error_abort));
|
|
|
|
g_assert(*dst);
|
|
|
|
|
|
|
|
test_io_channel_set_socket_bufs(*src, *dst);
|
|
|
|
|
2019-01-14 12:33:18 +01:00
|
|
|
*srv = QIO_CHANNEL(lioc);
|
2015-02-27 17:19:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct TestIOChannelData {
|
|
|
|
bool err;
|
|
|
|
GMainLoop *loop;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-08-11 16:20:58 +02:00
|
|
|
static void test_io_channel_complete(QIOTask *task,
|
2015-02-27 17:19:33 +01:00
|
|
|
gpointer opaque)
|
|
|
|
{
|
|
|
|
struct TestIOChannelData *data = opaque;
|
2016-08-11 16:20:58 +02:00
|
|
|
data->err = qio_task_propagate_error(task, NULL);
|
2015-02-27 17:19:33 +01:00
|
|
|
g_main_loop_quit(data->loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-26 09:36:41 +02:00
|
|
|
static void test_io_channel_setup_async(SocketAddress *listen_addr,
|
|
|
|
SocketAddress *connect_addr,
|
2019-01-14 12:33:18 +01:00
|
|
|
QIOChannel **srv,
|
2015-02-27 17:19:33 +01:00
|
|
|
QIOChannel **src,
|
|
|
|
QIOChannel **dst)
|
|
|
|
{
|
|
|
|
QIOChannelSocket *lioc;
|
|
|
|
struct TestIOChannelData data;
|
|
|
|
|
|
|
|
data.loop = g_main_loop_new(g_main_context_default(),
|
|
|
|
TRUE);
|
|
|
|
|
|
|
|
lioc = qio_channel_socket_new();
|
|
|
|
qio_channel_socket_listen_async(
|
2019-08-20 09:40:39 +02:00
|
|
|
lioc, listen_addr, 1,
|
2018-03-05 07:43:23 +01:00
|
|
|
test_io_channel_complete, &data, NULL, NULL);
|
2015-02-27 17:19:33 +01:00
|
|
|
|
|
|
|
g_main_loop_run(data.loop);
|
|
|
|
g_main_context_iteration(g_main_context_default(), FALSE);
|
|
|
|
|
|
|
|
g_assert(!data.err);
|
|
|
|
|
2017-04-26 09:36:41 +02:00
|
|
|
if (listen_addr->type == SOCKET_ADDRESS_TYPE_INET) {
|
|
|
|
SocketAddress *laddr = qio_channel_socket_get_local_address(
|
2015-02-27 17:19:33 +01:00
|
|
|
lioc, &error_abort);
|
|
|
|
|
2017-04-26 09:36:41 +02:00
|
|
|
g_free(connect_addr->u.inet.port);
|
|
|
|
connect_addr->u.inet.port = g_strdup(laddr->u.inet.port);
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2017-04-26 09:36:41 +02:00
|
|
|
qapi_free_SocketAddress(laddr);
|
2015-02-27 17:19:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*src = QIO_CHANNEL(qio_channel_socket_new());
|
|
|
|
|
|
|
|
qio_channel_socket_connect_async(
|
|
|
|
QIO_CHANNEL_SOCKET(*src), connect_addr,
|
2018-03-05 07:43:23 +01:00
|
|
|
test_io_channel_complete, &data, NULL, NULL);
|
2015-02-27 17:19:33 +01:00
|
|
|
|
|
|
|
g_main_loop_run(data.loop);
|
|
|
|
g_main_context_iteration(g_main_context_default(), FALSE);
|
|
|
|
|
|
|
|
g_assert(!data.err);
|
|
|
|
|
2016-03-08 16:26:39 +01:00
|
|
|
qio_channel_wait(QIO_CHANNEL(lioc), G_IO_IN);
|
2015-02-27 17:19:33 +01:00
|
|
|
*dst = QIO_CHANNEL(qio_channel_socket_accept(lioc, &error_abort));
|
|
|
|
g_assert(*dst);
|
|
|
|
|
|
|
|
qio_channel_set_delay(*src, false);
|
|
|
|
test_io_channel_set_socket_bufs(*src, *dst);
|
|
|
|
|
2019-01-14 12:33:18 +01:00
|
|
|
*srv = QIO_CHANNEL(lioc);
|
2015-02-27 17:19:33 +01:00
|
|
|
|
|
|
|
g_main_loop_unref(data.loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-14 12:33:18 +01:00
|
|
|
static void test_io_channel_socket_path_exists(SocketAddress *addr,
|
|
|
|
bool expectExists)
|
|
|
|
{
|
|
|
|
if (addr->type != SOCKET_ADDRESS_TYPE_UNIX) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert(g_file_test(addr->u.q_unix.path,
|
|
|
|
G_FILE_TEST_EXISTS) == expectExists);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-27 17:19:33 +01:00
|
|
|
static void test_io_channel(bool async,
|
2017-04-26 09:36:41 +02:00
|
|
|
SocketAddress *listen_addr,
|
|
|
|
SocketAddress *connect_addr,
|
2015-12-21 13:04:21 +01:00
|
|
|
bool passFD)
|
2015-02-27 17:19:33 +01:00
|
|
|
{
|
2019-01-14 12:33:18 +01:00
|
|
|
QIOChannel *src, *dst, *srv;
|
2015-02-27 17:19:33 +01:00
|
|
|
QIOChannelTest *test;
|
|
|
|
if (async) {
|
2019-01-14 12:33:18 +01:00
|
|
|
test_io_channel_setup_async(listen_addr, connect_addr,
|
|
|
|
&srv, &src, &dst);
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2022-08-02 09:52:00 +02:00
|
|
|
#ifndef _WIN32
|
2015-12-21 13:04:21 +01:00
|
|
|
g_assert(!passFD ||
|
|
|
|
qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
|
|
|
|
g_assert(!passFD ||
|
|
|
|
qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
|
2022-08-02 09:52:00 +02:00
|
|
|
#endif
|
2017-04-03 13:05:21 +02:00
|
|
|
g_assert(qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_SHUTDOWN));
|
|
|
|
g_assert(qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_SHUTDOWN));
|
2015-12-21 13:04:21 +01:00
|
|
|
|
2019-01-14 12:33:18 +01:00
|
|
|
test_io_channel_socket_path_exists(listen_addr, true);
|
|
|
|
|
2015-02-27 17:19:33 +01:00
|
|
|
test = qio_channel_test_new();
|
|
|
|
qio_channel_test_run_threads(test, true, src, dst);
|
|
|
|
qio_channel_test_validate(test);
|
|
|
|
|
2019-01-14 12:33:18 +01:00
|
|
|
test_io_channel_socket_path_exists(listen_addr, true);
|
|
|
|
|
|
|
|
/* unref without close, to ensure finalize() cleans up */
|
|
|
|
|
2015-02-27 17:19:33 +01:00
|
|
|
object_unref(OBJECT(src));
|
|
|
|
object_unref(OBJECT(dst));
|
2019-01-14 12:33:18 +01:00
|
|
|
test_io_channel_socket_path_exists(listen_addr, true);
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2019-01-14 12:33:18 +01:00
|
|
|
object_unref(OBJECT(srv));
|
|
|
|
test_io_channel_socket_path_exists(listen_addr, false);
|
|
|
|
|
|
|
|
test_io_channel_setup_async(listen_addr, connect_addr,
|
|
|
|
&srv, &src, &dst);
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2022-08-02 09:52:00 +02:00
|
|
|
#ifndef _WIN32
|
2015-12-21 13:04:21 +01:00
|
|
|
g_assert(!passFD ||
|
|
|
|
qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
|
|
|
|
g_assert(!passFD ||
|
|
|
|
qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
|
2022-08-02 09:52:00 +02:00
|
|
|
#endif
|
2017-04-03 13:05:21 +02:00
|
|
|
g_assert(qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_SHUTDOWN));
|
|
|
|
g_assert(qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_SHUTDOWN));
|
2015-12-21 13:04:21 +01:00
|
|
|
|
2015-02-27 17:19:33 +01:00
|
|
|
test = qio_channel_test_new();
|
|
|
|
qio_channel_test_run_threads(test, false, src, dst);
|
|
|
|
qio_channel_test_validate(test);
|
|
|
|
|
2019-01-14 12:33:18 +01:00
|
|
|
/* close before unref, to ensure finalize copes with already closed */
|
|
|
|
|
|
|
|
qio_channel_close(src, &error_abort);
|
|
|
|
qio_channel_close(dst, &error_abort);
|
|
|
|
test_io_channel_socket_path_exists(listen_addr, true);
|
|
|
|
|
2015-02-27 17:19:33 +01:00
|
|
|
object_unref(OBJECT(src));
|
|
|
|
object_unref(OBJECT(dst));
|
2019-01-14 12:33:18 +01:00
|
|
|
test_io_channel_socket_path_exists(listen_addr, true);
|
|
|
|
|
|
|
|
qio_channel_close(srv, &error_abort);
|
|
|
|
test_io_channel_socket_path_exists(listen_addr, false);
|
|
|
|
|
|
|
|
object_unref(OBJECT(srv));
|
|
|
|
test_io_channel_socket_path_exists(listen_addr, false);
|
2015-02-27 17:19:33 +01:00
|
|
|
} else {
|
2019-01-14 12:33:18 +01:00
|
|
|
test_io_channel_setup_sync(listen_addr, connect_addr,
|
|
|
|
&srv, &src, &dst);
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2022-08-02 09:52:00 +02:00
|
|
|
#ifndef _WIN32
|
2015-12-21 13:04:21 +01:00
|
|
|
g_assert(!passFD ||
|
|
|
|
qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
|
|
|
|
g_assert(!passFD ||
|
|
|
|
qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
|
2022-08-02 09:52:00 +02:00
|
|
|
#endif
|
2017-04-03 13:05:21 +02:00
|
|
|
g_assert(qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_SHUTDOWN));
|
|
|
|
g_assert(qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_SHUTDOWN));
|
2015-12-21 13:04:21 +01:00
|
|
|
|
2019-01-14 12:33:18 +01:00
|
|
|
test_io_channel_socket_path_exists(listen_addr, true);
|
|
|
|
|
2015-02-27 17:19:33 +01:00
|
|
|
test = qio_channel_test_new();
|
|
|
|
qio_channel_test_run_threads(test, true, src, dst);
|
|
|
|
qio_channel_test_validate(test);
|
|
|
|
|
2019-01-14 12:33:18 +01:00
|
|
|
test_io_channel_socket_path_exists(listen_addr, true);
|
|
|
|
|
|
|
|
/* unref without close, to ensure finalize() cleans up */
|
|
|
|
|
2015-02-27 17:19:33 +01:00
|
|
|
object_unref(OBJECT(src));
|
|
|
|
object_unref(OBJECT(dst));
|
2019-01-14 12:33:18 +01:00
|
|
|
test_io_channel_socket_path_exists(listen_addr, true);
|
|
|
|
|
|
|
|
object_unref(OBJECT(srv));
|
|
|
|
test_io_channel_socket_path_exists(listen_addr, false);
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2019-01-14 12:33:18 +01:00
|
|
|
test_io_channel_setup_sync(listen_addr, connect_addr,
|
|
|
|
&srv, &src, &dst);
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2022-08-02 09:52:00 +02:00
|
|
|
#ifndef _WIN32
|
2015-12-21 13:04:21 +01:00
|
|
|
g_assert(!passFD ||
|
|
|
|
qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
|
|
|
|
g_assert(!passFD ||
|
|
|
|
qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
|
2022-08-02 09:52:00 +02:00
|
|
|
#endif
|
2017-04-03 13:05:21 +02:00
|
|
|
g_assert(qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_SHUTDOWN));
|
|
|
|
g_assert(qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_SHUTDOWN));
|
2015-12-21 13:04:21 +01:00
|
|
|
|
2015-02-27 17:19:33 +01:00
|
|
|
test = qio_channel_test_new();
|
|
|
|
qio_channel_test_run_threads(test, false, src, dst);
|
|
|
|
qio_channel_test_validate(test);
|
|
|
|
|
2019-01-14 12:33:18 +01:00
|
|
|
test_io_channel_socket_path_exists(listen_addr, true);
|
|
|
|
|
|
|
|
/* close before unref, to ensure finalize copes with already closed */
|
|
|
|
|
|
|
|
qio_channel_close(src, &error_abort);
|
|
|
|
qio_channel_close(dst, &error_abort);
|
|
|
|
test_io_channel_socket_path_exists(listen_addr, true);
|
|
|
|
|
2015-02-27 17:19:33 +01:00
|
|
|
object_unref(OBJECT(src));
|
|
|
|
object_unref(OBJECT(dst));
|
2019-01-14 12:33:18 +01:00
|
|
|
test_io_channel_socket_path_exists(listen_addr, true);
|
|
|
|
|
|
|
|
qio_channel_close(srv, &error_abort);
|
|
|
|
test_io_channel_socket_path_exists(listen_addr, false);
|
|
|
|
|
|
|
|
object_unref(OBJECT(srv));
|
|
|
|
test_io_channel_socket_path_exists(listen_addr, false);
|
2015-02-27 17:19:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void test_io_channel_ipv4(bool async)
|
|
|
|
{
|
2017-04-26 09:36:41 +02:00
|
|
|
SocketAddress *listen_addr = g_new0(SocketAddress, 1);
|
|
|
|
SocketAddress *connect_addr = g_new0(SocketAddress, 1);
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2017-04-26 09:36:41 +02:00
|
|
|
listen_addr->type = SOCKET_ADDRESS_TYPE_INET;
|
|
|
|
listen_addr->u.inet = (InetSocketAddress) {
|
2016-03-03 17:16:48 +01:00
|
|
|
.host = g_strdup("127.0.0.1"),
|
|
|
|
.port = NULL, /* Auto-select */
|
|
|
|
};
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2017-04-26 09:36:41 +02:00
|
|
|
connect_addr->type = SOCKET_ADDRESS_TYPE_INET;
|
|
|
|
connect_addr->u.inet = (InetSocketAddress) {
|
2016-03-03 17:16:48 +01:00
|
|
|
.host = g_strdup("127.0.0.1"),
|
|
|
|
.port = NULL, /* Filled in later */
|
|
|
|
};
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2015-12-21 13:04:21 +01:00
|
|
|
test_io_channel(async, listen_addr, connect_addr, false);
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2017-04-26 09:36:41 +02:00
|
|
|
qapi_free_SocketAddress(listen_addr);
|
|
|
|
qapi_free_SocketAddress(connect_addr);
|
2015-02-27 17:19:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void test_io_channel_ipv4_sync(void)
|
|
|
|
{
|
|
|
|
return test_io_channel_ipv4(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void test_io_channel_ipv4_async(void)
|
|
|
|
{
|
|
|
|
return test_io_channel_ipv4(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void test_io_channel_ipv6(bool async)
|
|
|
|
{
|
2017-04-26 09:36:41 +02:00
|
|
|
SocketAddress *listen_addr = g_new0(SocketAddress, 1);
|
|
|
|
SocketAddress *connect_addr = g_new0(SocketAddress, 1);
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2017-04-26 09:36:41 +02:00
|
|
|
listen_addr->type = SOCKET_ADDRESS_TYPE_INET;
|
|
|
|
listen_addr->u.inet = (InetSocketAddress) {
|
2016-03-03 17:16:48 +01:00
|
|
|
.host = g_strdup("::1"),
|
|
|
|
.port = NULL, /* Auto-select */
|
|
|
|
};
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2017-04-26 09:36:41 +02:00
|
|
|
connect_addr->type = SOCKET_ADDRESS_TYPE_INET;
|
|
|
|
connect_addr->u.inet = (InetSocketAddress) {
|
2016-03-03 17:16:48 +01:00
|
|
|
.host = g_strdup("::1"),
|
|
|
|
.port = NULL, /* Filled in later */
|
|
|
|
};
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2015-12-21 13:04:21 +01:00
|
|
|
test_io_channel(async, listen_addr, connect_addr, false);
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2017-04-26 09:36:41 +02:00
|
|
|
qapi_free_SocketAddress(listen_addr);
|
|
|
|
qapi_free_SocketAddress(connect_addr);
|
2015-02-27 17:19:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void test_io_channel_ipv6_sync(void)
|
|
|
|
{
|
|
|
|
return test_io_channel_ipv6(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void test_io_channel_ipv6_async(void)
|
|
|
|
{
|
|
|
|
return test_io_channel_ipv6(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void test_io_channel_unix(bool async)
|
|
|
|
{
|
2017-04-26 09:36:41 +02:00
|
|
|
SocketAddress *listen_addr = g_new0(SocketAddress, 1);
|
|
|
|
SocketAddress *connect_addr = g_new0(SocketAddress, 1);
|
2015-02-27 17:19:33 +01:00
|
|
|
|
|
|
|
#define TEST_SOCKET "test-io-channel-socket.sock"
|
2017-04-26 09:36:41 +02:00
|
|
|
listen_addr->type = SOCKET_ADDRESS_TYPE_UNIX;
|
|
|
|
listen_addr->u.q_unix.path = g_strdup(TEST_SOCKET);
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2017-04-26 09:36:41 +02:00
|
|
|
connect_addr->type = SOCKET_ADDRESS_TYPE_UNIX;
|
|
|
|
connect_addr->u.q_unix.path = g_strdup(TEST_SOCKET);
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2015-12-21 13:04:21 +01:00
|
|
|
test_io_channel(async, listen_addr, connect_addr, true);
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2017-04-26 09:36:41 +02:00
|
|
|
qapi_free_SocketAddress(listen_addr);
|
|
|
|
qapi_free_SocketAddress(connect_addr);
|
2015-02-27 17:19:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void test_io_channel_unix_sync(void)
|
|
|
|
{
|
|
|
|
return test_io_channel_unix(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void test_io_channel_unix_async(void)
|
|
|
|
{
|
|
|
|
return test_io_channel_unix(true);
|
|
|
|
}
|
2015-12-21 12:58:51 +01:00
|
|
|
|
2022-08-02 09:52:00 +02:00
|
|
|
#ifndef _WIN32
|
2015-12-21 12:58:51 +01:00
|
|
|
static void test_io_channel_unix_fd_pass(void)
|
|
|
|
{
|
2017-04-26 09:36:41 +02:00
|
|
|
SocketAddress *listen_addr = g_new0(SocketAddress, 1);
|
|
|
|
SocketAddress *connect_addr = g_new0(SocketAddress, 1);
|
2019-01-14 12:33:18 +01:00
|
|
|
QIOChannel *src, *dst, *srv;
|
2015-12-21 12:58:51 +01:00
|
|
|
int testfd;
|
|
|
|
int fdsend[3];
|
|
|
|
int *fdrecv = NULL;
|
|
|
|
size_t nfdrecv = 0;
|
|
|
|
size_t i;
|
|
|
|
char bufsend[12], bufrecv[12];
|
|
|
|
struct iovec iosend[1], iorecv[1];
|
|
|
|
|
|
|
|
#define TEST_SOCKET "test-io-channel-socket.sock"
|
|
|
|
#define TEST_FILE "test-io-channel-socket.txt"
|
|
|
|
|
|
|
|
testfd = open(TEST_FILE, O_RDWR|O_TRUNC|O_CREAT, 0700);
|
|
|
|
g_assert(testfd != -1);
|
|
|
|
fdsend[0] = testfd;
|
|
|
|
fdsend[1] = testfd;
|
|
|
|
fdsend[2] = testfd;
|
|
|
|
|
2017-04-26 09:36:41 +02:00
|
|
|
listen_addr->type = SOCKET_ADDRESS_TYPE_UNIX;
|
|
|
|
listen_addr->u.q_unix.path = g_strdup(TEST_SOCKET);
|
2015-12-21 12:58:51 +01:00
|
|
|
|
2017-04-26 09:36:41 +02:00
|
|
|
connect_addr->type = SOCKET_ADDRESS_TYPE_UNIX;
|
|
|
|
connect_addr->u.q_unix.path = g_strdup(TEST_SOCKET);
|
2015-12-21 12:58:51 +01:00
|
|
|
|
2019-01-14 12:33:18 +01:00
|
|
|
test_io_channel_setup_sync(listen_addr, connect_addr, &srv, &src, &dst);
|
2015-12-21 12:58:51 +01:00
|
|
|
|
|
|
|
memcpy(bufsend, "Hello World", G_N_ELEMENTS(bufsend));
|
|
|
|
|
|
|
|
iosend[0].iov_base = bufsend;
|
|
|
|
iosend[0].iov_len = G_N_ELEMENTS(bufsend);
|
|
|
|
|
|
|
|
iorecv[0].iov_base = bufrecv;
|
|
|
|
iorecv[0].iov_len = G_N_ELEMENTS(bufrecv);
|
|
|
|
|
|
|
|
g_assert(qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
|
|
|
|
g_assert(qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
|
|
|
|
|
|
|
|
qio_channel_writev_full(src,
|
|
|
|
iosend,
|
|
|
|
G_N_ELEMENTS(iosend),
|
|
|
|
fdsend,
|
|
|
|
G_N_ELEMENTS(fdsend),
|
2022-05-13 08:28:31 +02:00
|
|
|
0,
|
2015-12-21 12:58:51 +01:00
|
|
|
&error_abort);
|
|
|
|
|
|
|
|
qio_channel_readv_full(dst,
|
|
|
|
iorecv,
|
|
|
|
G_N_ELEMENTS(iorecv),
|
|
|
|
&fdrecv,
|
|
|
|
&nfdrecv,
|
2022-12-20 19:44:17 +01:00
|
|
|
0,
|
2015-12-21 12:58:51 +01:00
|
|
|
&error_abort);
|
|
|
|
|
|
|
|
g_assert(nfdrecv == G_N_ELEMENTS(fdsend));
|
|
|
|
/* Each recvd FD should be different from sent FD */
|
|
|
|
for (i = 0; i < nfdrecv; i++) {
|
|
|
|
g_assert_cmpint(fdrecv[i], !=, testfd);
|
|
|
|
}
|
|
|
|
/* Each recvd FD should be different from each other */
|
|
|
|
g_assert_cmpint(fdrecv[0], !=, fdrecv[1]);
|
|
|
|
g_assert_cmpint(fdrecv[0], !=, fdrecv[2]);
|
|
|
|
g_assert_cmpint(fdrecv[1], !=, fdrecv[2]);
|
|
|
|
|
|
|
|
/* Check the I/O buf we sent at the same time matches */
|
|
|
|
g_assert(memcmp(bufsend, bufrecv, G_N_ELEMENTS(bufsend)) == 0);
|
|
|
|
|
|
|
|
/* Write some data into the FD we received */
|
|
|
|
g_assert(write(fdrecv[0], bufsend, G_N_ELEMENTS(bufsend)) ==
|
|
|
|
G_N_ELEMENTS(bufsend));
|
|
|
|
|
|
|
|
/* Read data from the original FD and make sure it matches */
|
|
|
|
memset(bufrecv, 0, G_N_ELEMENTS(bufrecv));
|
|
|
|
g_assert(lseek(testfd, 0, SEEK_SET) == 0);
|
|
|
|
g_assert(read(testfd, bufrecv, G_N_ELEMENTS(bufrecv)) ==
|
|
|
|
G_N_ELEMENTS(bufrecv));
|
|
|
|
g_assert(memcmp(bufsend, bufrecv, G_N_ELEMENTS(bufsend)) == 0);
|
|
|
|
|
|
|
|
object_unref(OBJECT(src));
|
|
|
|
object_unref(OBJECT(dst));
|
2019-01-14 12:33:18 +01:00
|
|
|
object_unref(OBJECT(srv));
|
2017-04-26 09:36:41 +02:00
|
|
|
qapi_free_SocketAddress(listen_addr);
|
|
|
|
qapi_free_SocketAddress(connect_addr);
|
2015-12-21 12:58:51 +01:00
|
|
|
unlink(TEST_SOCKET);
|
|
|
|
unlink(TEST_FILE);
|
|
|
|
close(testfd);
|
|
|
|
for (i = 0; i < nfdrecv; i++) {
|
|
|
|
close(fdrecv[i]);
|
|
|
|
}
|
|
|
|
g_free(fdrecv);
|
|
|
|
}
|
2022-08-02 09:52:00 +02:00
|
|
|
#endif /* _WIN32 */
|
2016-09-29 17:52:38 +02:00
|
|
|
|
|
|
|
static void test_io_channel_unix_listen_cleanup(void)
|
|
|
|
{
|
|
|
|
QIOChannelSocket *ioc;
|
|
|
|
struct sockaddr_un un;
|
|
|
|
int sock;
|
|
|
|
|
|
|
|
#define TEST_SOCKET "test-io-channel-socket.sock"
|
|
|
|
|
|
|
|
ioc = qio_channel_socket_new();
|
|
|
|
|
|
|
|
/* Manually bind ioc without calling the qio api to avoid setting
|
|
|
|
* the LISTEN feature */
|
|
|
|
sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
|
|
|
|
memset(&un, 0, sizeof(un));
|
|
|
|
un.sun_family = AF_UNIX;
|
|
|
|
snprintf(un.sun_path, sizeof(un.sun_path), "%s", TEST_SOCKET);
|
|
|
|
unlink(TEST_SOCKET);
|
|
|
|
bind(sock, (struct sockaddr *)&un, sizeof(un));
|
|
|
|
ioc->fd = sock;
|
|
|
|
ioc->localAddrLen = sizeof(ioc->localAddr);
|
|
|
|
getsockname(sock, (struct sockaddr *)&ioc->localAddr,
|
|
|
|
&ioc->localAddrLen);
|
|
|
|
|
|
|
|
g_assert(g_file_test(TEST_SOCKET, G_FILE_TEST_EXISTS));
|
|
|
|
object_unref(OBJECT(ioc));
|
|
|
|
g_assert(g_file_test(TEST_SOCKET, G_FILE_TEST_EXISTS));
|
|
|
|
|
|
|
|
unlink(TEST_SOCKET);
|
|
|
|
}
|
|
|
|
|
2016-02-03 15:00:28 +01:00
|
|
|
static void test_io_channel_ipv4_fd(void)
|
|
|
|
{
|
|
|
|
QIOChannel *ioc;
|
|
|
|
int fd = -1;
|
2016-03-08 16:27:30 +01:00
|
|
|
struct sockaddr_in sa = {
|
|
|
|
.sin_family = AF_INET,
|
|
|
|
.sin_addr = {
|
|
|
|
.s_addr = htonl(INADDR_LOOPBACK),
|
|
|
|
}
|
|
|
|
/* Leave port unset for auto-assign */
|
|
|
|
};
|
|
|
|
socklen_t salen = sizeof(sa);
|
2016-02-03 15:00:28 +01:00
|
|
|
|
|
|
|
fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
g_assert_cmpint(fd, >, -1);
|
|
|
|
|
2016-03-08 16:27:30 +01:00
|
|
|
g_assert_cmpint(bind(fd, (struct sockaddr *)&sa, salen), ==, 0);
|
|
|
|
|
2016-02-03 15:00:28 +01:00
|
|
|
ioc = qio_channel_new_fd(fd, &error_abort);
|
|
|
|
|
|
|
|
g_assert_cmpstr(object_get_typename(OBJECT(ioc)),
|
|
|
|
==,
|
|
|
|
TYPE_QIO_CHANNEL_SOCKET);
|
|
|
|
|
|
|
|
object_unref(OBJECT(ioc));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-27 17:19:33 +01:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2022-08-02 09:52:00 +02:00
|
|
|
bool has_ipv4, has_ipv6, has_afunix;
|
2015-02-27 17:19:33 +01:00
|
|
|
|
|
|
|
module_call_init(MODULE_INIT_QOM);
|
2020-09-15 14:13:09 +02:00
|
|
|
qemu_init_main_loop(&error_abort);
|
2016-03-08 12:44:26 +01:00
|
|
|
socket_init();
|
2015-02-27 17:19:33 +01:00
|
|
|
|
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
|
|
|
|
|
|
/* We're creating actual IPv4/6 sockets, so we should
|
|
|
|
* check if the host running tests actually supports
|
|
|
|
* each protocol to avoid breaking tests on machines
|
|
|
|
* with either IPv4 or IPv6 disabled.
|
|
|
|
*/
|
2017-12-22 12:45:24 +01:00
|
|
|
if (socket_check_protocol_support(&has_ipv4, &has_ipv6) < 0) {
|
2019-07-09 21:24:46 +02:00
|
|
|
g_printerr("socket_check_protocol_support() failed\n");
|
|
|
|
goto end;
|
2015-02-27 17:19:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (has_ipv4) {
|
|
|
|
g_test_add_func("/io/channel/socket/ipv4-sync",
|
|
|
|
test_io_channel_ipv4_sync);
|
|
|
|
g_test_add_func("/io/channel/socket/ipv4-async",
|
|
|
|
test_io_channel_ipv4_async);
|
2016-02-03 15:00:28 +01:00
|
|
|
g_test_add_func("/io/channel/socket/ipv4-fd",
|
|
|
|
test_io_channel_ipv4_fd);
|
2015-02-27 17:19:33 +01:00
|
|
|
}
|
|
|
|
if (has_ipv6) {
|
|
|
|
g_test_add_func("/io/channel/socket/ipv6-sync",
|
|
|
|
test_io_channel_ipv6_sync);
|
|
|
|
g_test_add_func("/io/channel/socket/ipv6-async",
|
|
|
|
test_io_channel_ipv6_async);
|
|
|
|
}
|
|
|
|
|
2022-08-02 09:52:00 +02:00
|
|
|
socket_check_afunix_support(&has_afunix);
|
|
|
|
if (has_afunix) {
|
|
|
|
g_test_add_func("/io/channel/socket/unix-sync",
|
|
|
|
test_io_channel_unix_sync);
|
|
|
|
g_test_add_func("/io/channel/socket/unix-async",
|
|
|
|
test_io_channel_unix_async);
|
2015-02-27 17:19:33 +01:00
|
|
|
#ifndef _WIN32
|
2022-08-02 09:52:00 +02:00
|
|
|
g_test_add_func("/io/channel/socket/unix-fd-pass",
|
|
|
|
test_io_channel_unix_fd_pass);
|
|
|
|
#endif
|
|
|
|
g_test_add_func("/io/channel/socket/unix-listen-cleanup",
|
|
|
|
test_io_channel_unix_listen_cleanup);
|
|
|
|
}
|
2015-02-27 17:19:33 +01:00
|
|
|
|
2019-07-09 21:24:46 +02:00
|
|
|
end:
|
2015-02-27 17:19:33 +01:00
|
|
|
return g_test_run();
|
|
|
|
}
|