diff options
author | Markus Armbruster <armbru@redhat.com> | 2020-11-02 10:44:14 +0100 |
---|---|---|
committer | Daniel P. Berrangé <berrange@redhat.com> | 2020-11-03 13:09:28 +0000 |
commit | 718a9be02df880ca4b4e34ce253daf2bfc5d059c (patch) | |
tree | 658e57bf5e9851b9709c28eae921d1ee5f0d644e | |
parent | d1a393211b5333f9374b439394424f594f69d282 (diff) |
test-util-sockets: Clean up SocketAddress construction
The thread functions build the SocketAddress from global variable
@abstract_sock_name and the tight flag passed as pointer
argument (either NULL or (gpointer)1). There is no need for such
hackery; simply pass the SocketAddress instead.
While there, dumb down g_rand_int_range() to g_random_int().
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
-rw-r--r-- | tests/test-util-sockets.c | 64 |
1 files changed, 26 insertions, 38 deletions
diff --git a/tests/test-util-sockets.c b/tests/test-util-sockets.c index 9d317e73a6..a4792253ba 100644 --- a/tests/test-util-sockets.c +++ b/tests/test-util-sockets.c @@ -230,25 +230,15 @@ static void test_socket_fd_pass_num_nocli(void) #endif #ifdef __linux__ -static gchar *abstract_sock_name; - static gpointer unix_server_thread_func(gpointer user_data) { - SocketAddress addr; - Error *err = NULL; - int fd = -1; - int connfd = -1; + SocketAddress *addr = user_data; + int fd; + int connfd; 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.has_tight = true; - addr.u.q_unix.tight = user_data != NULL; - addr.u.q_unix.has_abstract = true; - addr.u.q_unix.abstract = true; - - fd = socket_listen(&addr, 1, &err); + fd = socket_listen(addr, 1, &error_abort); g_assert_cmpint(fd, >=, 0); g_assert(fd_is_socket(fd)); @@ -257,69 +247,67 @@ static gpointer unix_server_thread_func(gpointer user_data) close(connfd); close(fd); - return NULL; } static gpointer unix_client_thread_func(gpointer user_data) { - SocketAddress addr; - Error *err = NULL; - int fd = -1; - - addr.type = SOCKET_ADDRESS_TYPE_UNIX; - addr.u.q_unix.path = abstract_sock_name; - addr.u.q_unix.has_tight = true; - addr.u.q_unix.tight = user_data != NULL; - addr.u.q_unix.has_abstract = true; - addr.u.q_unix.abstract = true; - - fd = socket_connect(&addr, &err); + SocketAddress *addr = user_data; + int fd; + fd = socket_connect(addr, &error_abort); g_assert_cmpint(fd, >=, 0); - close(fd); - return NULL; } static void test_socket_unix_abstract_good(void) { - GRand *r = g_rand_new(); + SocketAddress addr; - abstract_sock_name = g_strdup_printf("unix-%d-%d", getpid(), - g_rand_int_range(r, 100, 1000)); + addr.type = SOCKET_ADDRESS_TYPE_UNIX; + 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; /* non tight socklen serv and cli */ + + addr.u.q_unix.has_tight = false; + addr.u.q_unix.tight = false; + GThread *serv = g_thread_new("abstract_unix_server", unix_server_thread_func, - NULL); + &addr); sleep(1); GThread *cli = g_thread_new("abstract_unix_client", unix_client_thread_func, - NULL); + &addr); g_thread_join(cli); g_thread_join(serv); /* tight socklen serv and cli */ + + addr.u.q_unix.has_tight = true; + addr.u.q_unix.tight = true; + serv = g_thread_new("abstract_unix_server", unix_server_thread_func, - (gpointer)1); + &addr); sleep(1); cli = g_thread_new("abstract_unix_client", unix_client_thread_func, - (gpointer)1); + &addr); g_thread_join(cli); g_thread_join(serv); - g_free(abstract_sock_name); - g_rand_free(r); + g_free(addr.u.q_unix.path); } #endif |