aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorPeter Maydell <peter.maydell@linaro.org>2020-11-03 14:40:40 +0000
committerPeter Maydell <peter.maydell@linaro.org>2020-11-03 14:40:40 +0000
commitad262888993f795db68fd7c2bdfa72f467fe0096 (patch)
tree50f03093d7fcdcc52549f1a3baf2a520e8b21f97 /tests
parent83851c7c60c90e9fb6a23ff48076387a77bc33cd (diff)
parent8acefc79deaab1c7ee2ab07b540b0e3edf0f9f47 (diff)
downloadqemu-ad262888993f795db68fd7c2bdfa72f467fe0096.zip
qemu-ad262888993f795db68fd7c2bdfa72f467fe0096.tar.gz
qemu-ad262888993f795db68fd7c2bdfa72f467fe0096.tar.bz2
Merge remote-tracking branch 'remotes/berrange-gitlab/tags/sock-next-pull-request' into staging
- Fix inverted logic in abstract socket QAPI support - Only report abstract socket support in QAPI on Linux hosts - Expand test coverage - Misc other code cleanups # gpg: Signature made Tue 03 Nov 2020 14:00:53 GMT # gpg: using RSA key DAF3A6FDB26B62912D0E8E3FBE86EBB415104FDF # gpg: Good signature from "Daniel P. Berrange <dan@berrange.com>" [full] # gpg: aka "Daniel P. Berrange <berrange@redhat.com>" [full] # Primary key fingerprint: DAF3 A6FD B26B 6291 2D0E 8E3F BE86 EBB4 1510 4FDF * remotes/berrange-gitlab/tags/sock-next-pull-request: sockets: Make abstract UnixSocketAddress depend on CONFIG_LINUX sockets: Bypass "replace empty @path" for abstract unix sockets char-socket: Fix qemu_chr_socket_address() for abstract sockets sockets: Fix socket_sockaddr_to_address_unix() for abstract sockets sockets: Fix default of UnixSocketAddress member @tight test-util-sockets: Test the complete abstract socket matrix test-util-sockets: Synchronize properly, don't sleep(1) test-util-sockets: Factor out test_socket_unix_abstract_one() test-util-sockets: Clean up SocketAddress construction test-util-sockets: Correct to set has_abstract, has_tight test-util-sockets: Plug file descriptor leak Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/test-util-sockets.c145
1 files changed, 78 insertions, 67 deletions
diff --git a/tests/test-util-sockets.c b/tests/test-util-sockets.c
index f6336e0..6748605 100644
--- a/tests/test-util-sockets.c
+++ b/tests/test-util-sockets.c
@@ -229,94 +229,105 @@ static void test_socket_fd_pass_num_nocli(void)
}
#endif
-#ifdef __linux__
-static gchar *abstract_sock_name;
+#ifdef CONFIG_LINUX
-static gpointer unix_server_thread_func(gpointer user_data)
+#define ABSTRACT_SOCKET_VARIANTS 3
+
+typedef struct {
+ SocketAddress *server, *client[ABSTRACT_SOCKET_VARIANTS];
+ bool expect_connect[ABSTRACT_SOCKET_VARIANTS];
+} abstract_socket_matrix_row;
+
+static gpointer unix_client_thread_func(gpointer user_data)
{
- SocketAddress addr;
+ abstract_socket_matrix_row *row = user_data;
Error *err = NULL;
- int fd = -1;
- int connfd = -1;
+ int i, fd;
+
+ for (i = 0; i < ABSTRACT_SOCKET_VARIANTS; i++) {
+ if (row->expect_connect[i]) {
+ fd = socket_connect(row->client[i], &error_abort);
+ g_assert_cmpint(fd, >=, 0);
+ } else {
+ fd = socket_connect(row->client[i], &err);
+ g_assert_cmpint(fd, ==, -1);
+ error_free_or_abort(&err);
+ }
+ close(fd);
+ }
+ return NULL;
+}
+
+static void test_socket_unix_abstract_row(abstract_socket_matrix_row *test)
+{
+ int fd, connfd, i;
+ GThread *cli;
struct sockaddr_un un;
socklen_t len = sizeof(un);
- addr.type = SOCKET_ADDRESS_TYPE_UNIX;
- addr.u.q_unix.path = abstract_sock_name;
- addr.u.q_unix.tight = user_data != NULL;
- addr.u.q_unix.abstract = true;
+ /* Last one must connect, or else accept() below hangs */
+ assert(test->expect_connect[ABSTRACT_SOCKET_VARIANTS - 1]);
- fd = socket_listen(&addr, 1, &err);
+ fd = socket_listen(test->server, 1, &error_abort);
g_assert_cmpint(fd, >=, 0);
g_assert(fd_is_socket(fd));
- connfd = accept(fd, (struct sockaddr *)&un, &len);
- g_assert_cmpint(connfd, !=, -1);
+ cli = g_thread_new("abstract_unix_client",
+ unix_client_thread_func,
+ test);
+
+ for (i = 0; i < ABSTRACT_SOCKET_VARIANTS; i++) {
+ if (test->expect_connect[i]) {
+ connfd = accept(fd, (struct sockaddr *)&un, &len);
+ g_assert_cmpint(connfd, !=, -1);
+ close(connfd);
+ }
+ }
close(fd);
-
- return NULL;
+ g_thread_join(cli);
}
-static gpointer unix_client_thread_func(gpointer user_data)
+static void test_socket_unix_abstract(void)
{
- SocketAddress addr;
- Error *err = NULL;
- int fd = -1;
+ SocketAddress addr, addr_tight, addr_padded;
+ abstract_socket_matrix_row matrix[ABSTRACT_SOCKET_VARIANTS] = {
+ { &addr,
+ { &addr_tight, &addr_padded, &addr },
+ { true, false, true } },
+ { &addr_tight,
+ { &addr_padded, &addr, &addr_tight },
+ { false, true, true } },
+ { &addr_padded,
+ { &addr, &addr_tight, &addr_padded },
+ { false, false, true } }
+ };
+ int i;
addr.type = SOCKET_ADDRESS_TYPE_UNIX;
- addr.u.q_unix.path = abstract_sock_name;
- addr.u.q_unix.tight = user_data != NULL;
+ addr.u.q_unix.path = g_strdup_printf("unix-%d-%u",
+ getpid(), g_random_int());
+ addr.u.q_unix.has_abstract = true;
addr.u.q_unix.abstract = true;
+ addr.u.q_unix.has_tight = false;
+ addr.u.q_unix.tight = false;
- fd = socket_connect(&addr, &err);
+ addr_tight = addr;
+ addr_tight.u.q_unix.has_tight = true;
+ addr_tight.u.q_unix.tight = true;
- g_assert_cmpint(fd, >=, 0);
+ addr_padded = addr;
+ addr_padded.u.q_unix.has_tight = true;
+ addr_padded.u.q_unix.tight = false;
- close(fd);
+ for (i = 0; i < ABSTRACT_SOCKET_VARIANTS; i++) {
+ test_socket_unix_abstract_row(&matrix[i]);
+ }
- return NULL;
+ g_free(addr.u.q_unix.path);
}
-static void test_socket_unix_abstract_good(void)
-{
- GRand *r = g_rand_new();
-
- abstract_sock_name = g_strdup_printf("unix-%d-%d", getpid(),
- g_rand_int_range(r, 100, 1000));
-
- /* non tight socklen serv and cli */
- GThread *serv = g_thread_new("abstract_unix_server",
- unix_server_thread_func,
- NULL);
-
- sleep(1);
-
- GThread *cli = g_thread_new("abstract_unix_client",
- unix_client_thread_func,
- NULL);
-
- g_thread_join(cli);
- g_thread_join(serv);
-
- /* tight socklen serv and cli */
- serv = g_thread_new("abstract_unix_server",
- unix_server_thread_func,
- (gpointer)1);
-
- sleep(1);
-
- cli = g_thread_new("abstract_unix_client",
- unix_client_thread_func,
- (gpointer)1);
-
- g_thread_join(cli);
- g_thread_join(serv);
-
- g_free(abstract_sock_name);
- g_rand_free(r);
-}
-#endif
+#endif /* CONFIG_LINUX */
int main(int argc, char **argv)
{
@@ -358,9 +369,9 @@ int main(int argc, char **argv)
#endif
}
-#ifdef __linux__
- g_test_add_func("/util/socket/unix-abstract/good",
- test_socket_unix_abstract_good);
+#ifdef CONFIG_LINUX
+ g_test_add_func("/util/socket/unix-abstract",
+ test_socket_unix_abstract);
#endif
end: