aboutsummaryrefslogtreecommitdiff
path: root/tests/test-util-sockets.c
diff options
context:
space:
mode:
authorDaniel P. Berrange <berrange@redhat.com>2017-12-22 11:08:49 +0000
committerDaniel P. Berrangé <berrange@redhat.com>2018-03-13 18:06:06 +0000
commit30bdb3c56ddd911ab2b1629faa4ce6e883b80e2a (patch)
tree1d16d017837d0f101626771ca2c1c47180c78cce /tests/test-util-sockets.c
parent58dc31f1a7dc6cd0f21bd51a34011ba366d36e53 (diff)
sockets: check that the named file descriptor is a socket
The SocketAddress struct has an "fd" type, which references the name of a file descriptor passed over the monitor using the "getfd" command. We currently blindly assume the FD is a socket, which can lead to hard to diagnose errors later. This adds an explicit check that the FD is actually a socket to improve the error diagnosis. Reviewed-by: Eric Blake <eblake@redhat.com> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Diffstat (limited to 'tests/test-util-sockets.c')
-rw-r--r--tests/test-util-sockets.c89
1 files changed, 89 insertions, 0 deletions
diff --git a/tests/test-util-sockets.c b/tests/test-util-sockets.c
index 65190e0530..06eb0e4a28 100644
--- a/tests/test-util-sockets.c
+++ b/tests/test-util-sockets.c
@@ -23,6 +23,7 @@
#include "qemu/sockets.h"
#include "qapi/error.h"
#include "socket-helpers.h"
+#include "monitor/monitor.h"
static void test_fd_is_socket_bad(void)
{
@@ -49,6 +50,90 @@ static void test_fd_is_socket_good(void)
close(fd);
}
+static int mon_fd = -1;
+static const char *mon_fdname;
+
+int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
+{
+ g_assert(cur_mon);
+ g_assert(mon == cur_mon);
+ if (mon_fd == -1 || !g_str_equal(mon_fdname, fdname)) {
+ error_setg(errp, "No fd named %s", fdname);
+ return -1;
+ }
+ return dup(mon_fd);
+}
+
+/* Syms in libqemustub.a are discarded at .o file granularity.
+ * To replace monitor_get_fd() we must ensure everything in
+ * stubs/monitor.c is defined, to make sure monitor.o is discarded
+ * otherwise we get duplicate syms at link time.
+ */
+Monitor *cur_mon;
+void monitor_init(Chardev *chr, int flags) {}
+
+
+static void test_socket_fd_pass_good(void)
+{
+ SocketAddress addr;
+ int fd;
+
+ cur_mon = g_malloc(1); /* Fake a monitor */
+ mon_fdname = "myfd";
+ mon_fd = qemu_socket(AF_INET, SOCK_STREAM, 0);
+ g_assert_cmpint(mon_fd, >, STDERR_FILENO);
+
+ addr.type = SOCKET_ADDRESS_TYPE_FD;
+ addr.u.fd.str = g_strdup(mon_fdname);
+
+ fd = socket_connect(&addr, &error_abort);
+ g_assert_cmpint(fd, !=, -1);
+ g_assert_cmpint(fd, !=, mon_fd);
+ close(fd);
+
+ fd = socket_listen(&addr, &error_abort);
+ g_assert_cmpint(fd, !=, -1);
+ g_assert_cmpint(fd, !=, mon_fd);
+ close(fd);
+
+ g_free(addr.u.fd.str);
+ mon_fdname = NULL;
+ close(mon_fd);
+ mon_fd = -1;
+ g_free(cur_mon);
+ cur_mon = NULL;
+}
+
+static void test_socket_fd_pass_bad(void)
+{
+ SocketAddress addr;
+ Error *err = NULL;
+ int fd;
+
+ cur_mon = g_malloc(1); /* Fake a monitor */
+ mon_fdname = "myfd";
+ mon_fd = dup(STDOUT_FILENO);
+ g_assert_cmpint(mon_fd, >, STDERR_FILENO);
+
+ addr.type = SOCKET_ADDRESS_TYPE_FD;
+ addr.u.fd.str = g_strdup(mon_fdname);
+
+ fd = socket_connect(&addr, &err);
+ g_assert_cmpint(fd, ==, -1);
+ error_free_or_abort(&err);
+
+ fd = socket_listen(&addr, &err);
+ g_assert_cmpint(fd, ==, -1);
+ error_free_or_abort(&err);
+
+ g_free(addr.u.fd.str);
+ mon_fdname = NULL;
+ close(mon_fd);
+ mon_fd = -1;
+ g_free(cur_mon);
+ cur_mon = NULL;
+}
+
int main(int argc, char **argv)
{
bool has_ipv4, has_ipv6;
@@ -71,6 +156,10 @@ int main(int argc, char **argv)
test_fd_is_socket_bad);
g_test_add_func("/util/socket/is-socket/good",
test_fd_is_socket_good);
+ g_test_add_func("/socket/fd-pass/good",
+ test_socket_fd_pass_good);
+ g_test_add_func("/socket/fd-pass/bad",
+ test_socket_fd_pass_bad);
}
return g_test_run();