Message ID | 20220802075200.907360-5-bmeng.cn@gmail.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | Enable unix socket support on Windows | expand |
Hi On Tue, Aug 2, 2022 at 1:21 PM Bin Meng <bmeng.cn@gmail.com> wrote: > From: Bin Meng <bin.meng@windriver.com> > > Change to dynamically include the test cases by checking AF_UNIX > availability using a new helper socket_check_afunix_support(). > With such changes testing on a Windows host can be covered as well. > > Signed-off-by: Bin Meng <bin.meng@windriver.com> > --- > > Changes in v4: > - introduce a new helper socket_check_afunix_support() to runtime-check > the availability of AF_UNIX socket, and skip those appropriately > > Changes in v2: > - new patch: tests/unit: Update test-io-channel-socket.c for Windows > > tests/unit/socket-helpers.h | 9 +++++++ > tests/unit/socket-helpers.c | 16 +++++++++++++ > tests/unit/test-io-channel-socket.c | 37 ++++++++++++++++++----------- > 3 files changed, 48 insertions(+), 14 deletions(-) > > diff --git a/tests/unit/socket-helpers.h b/tests/unit/socket-helpers.h > index 512a004811..ed8477ceb3 100644 > --- a/tests/unit/socket-helpers.h > +++ b/tests/unit/socket-helpers.h > @@ -32,4 +32,13 @@ > */ > int socket_check_protocol_support(bool *has_ipv4, bool *has_ipv6); > > +/* > + * @has_afunix: set to true on return if unix socket support is available > + * > + * Check whether unix domain socket support is available for use. > + * On success, @has_afunix will be set to indicate whether AF_UNIX > protocol > + * is available. > + */ > +void socket_check_afunix_support(bool *has_afunix); > + > #endif > diff --git a/tests/unit/socket-helpers.c b/tests/unit/socket-helpers.c > index 5af4de513b..eecadf3a3c 100644 > --- a/tests/unit/socket-helpers.c > +++ b/tests/unit/socket-helpers.c > @@ -154,3 +154,19 @@ int socket_check_protocol_support(bool *has_ipv4, > bool *has_ipv6) > > return 0; > } > + > +void socket_check_afunix_support(bool *has_afunix) > Why not return TRUE for success? Ah, for consistency with socket_check_protocol_support (). Maybe you should extend that function instead then? > +{ > + int fd; > + > + fd = socket(PF_UNIX, SOCK_STREAM, 0); > + closesocket(fd); > + > +#ifdef _WIN32 > + *has_afunix = (fd != (int)INVALID_SOCKET); > +#else > + *has_afunix = (fd >= 0); > +#endif > + > + return; > +} > diff --git a/tests/unit/test-io-channel-socket.c > b/tests/unit/test-io-channel-socket.c > index 6713886d02..b36a5d972a 100644 > --- a/tests/unit/test-io-channel-socket.c > +++ b/tests/unit/test-io-channel-socket.c > @@ -179,10 +179,12 @@ static void test_io_channel(bool async, > test_io_channel_setup_async(listen_addr, connect_addr, > &srv, &src, &dst); > > +#ifndef _WIN32 > 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)); > +#endif > g_assert(qio_channel_has_feature(src, > QIO_CHANNEL_FEATURE_SHUTDOWN)); > g_assert(qio_channel_has_feature(dst, > QIO_CHANNEL_FEATURE_SHUTDOWN)); > > @@ -206,10 +208,12 @@ static void test_io_channel(bool async, > test_io_channel_setup_async(listen_addr, connect_addr, > &srv, &src, &dst); > > +#ifndef _WIN32 > 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)); > +#endif > g_assert(qio_channel_has_feature(src, > QIO_CHANNEL_FEATURE_SHUTDOWN)); > g_assert(qio_channel_has_feature(dst, > QIO_CHANNEL_FEATURE_SHUTDOWN)); > > @@ -236,10 +240,12 @@ static void test_io_channel(bool async, > test_io_channel_setup_sync(listen_addr, connect_addr, > &srv, &src, &dst); > > +#ifndef _WIN32 > 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)); > +#endif > g_assert(qio_channel_has_feature(src, > QIO_CHANNEL_FEATURE_SHUTDOWN)); > g_assert(qio_channel_has_feature(dst, > QIO_CHANNEL_FEATURE_SHUTDOWN)); > > @@ -263,10 +269,12 @@ static void test_io_channel(bool async, > test_io_channel_setup_sync(listen_addr, connect_addr, > &srv, &src, &dst); > > +#ifndef _WIN32 > 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)); > +#endif > g_assert(qio_channel_has_feature(src, > QIO_CHANNEL_FEATURE_SHUTDOWN)); > g_assert(qio_channel_has_feature(dst, > QIO_CHANNEL_FEATURE_SHUTDOWN)); > > @@ -367,7 +375,6 @@ static void test_io_channel_ipv6_async(void) > } > > > -#ifndef _WIN32 > static void test_io_channel_unix(bool async) > { > SocketAddress *listen_addr = g_new0(SocketAddress, 1); > @@ -398,6 +405,7 @@ static void test_io_channel_unix_async(void) > return test_io_channel_unix(true); > } > > +#ifndef _WIN32 > static void test_io_channel_unix_fd_pass(void) > { > SocketAddress *listen_addr = g_new0(SocketAddress, 1); > @@ -491,6 +499,7 @@ static void test_io_channel_unix_fd_pass(void) > } > g_free(fdrecv); > } > +#endif /* _WIN32 */ > > static void test_io_channel_unix_listen_cleanup(void) > { > @@ -522,9 +531,6 @@ static void test_io_channel_unix_listen_cleanup(void) > unlink(TEST_SOCKET); > } > > -#endif /* _WIN32 */ > - > - > static void test_io_channel_ipv4_fd(void) > { > QIOChannel *ioc; > @@ -555,7 +561,7 @@ static void test_io_channel_ipv4_fd(void) > > int main(int argc, char **argv) > { > - bool has_ipv4, has_ipv6; > + bool has_ipv4, has_ipv6, has_afunix; > > module_call_init(MODULE_INIT_QOM); > qemu_init_main_loop(&error_abort); > @@ -588,16 +594,19 @@ int main(int argc, char **argv) > test_io_channel_ipv6_async); > } > > + 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); > (I would also have g_test_skip() inside the tests, but that's pre-existing too) #ifndef _WIN32 > - 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); > - g_test_add_func("/io/channel/socket/unix-fd-pass", > - test_io_channel_unix_fd_pass); > - g_test_add_func("/io/channel/socket/unix-listen-cleanup", > - test_io_channel_unix_listen_cleanup); > -#endif /* _WIN32 */ > + 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); > + } > > end: > return g_test_run(); > -- > 2.34.1 > > > So, lgtm Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
diff --git a/tests/unit/socket-helpers.h b/tests/unit/socket-helpers.h index 512a004811..ed8477ceb3 100644 --- a/tests/unit/socket-helpers.h +++ b/tests/unit/socket-helpers.h @@ -32,4 +32,13 @@ */ int socket_check_protocol_support(bool *has_ipv4, bool *has_ipv6); +/* + * @has_afunix: set to true on return if unix socket support is available + * + * Check whether unix domain socket support is available for use. + * On success, @has_afunix will be set to indicate whether AF_UNIX protocol + * is available. + */ +void socket_check_afunix_support(bool *has_afunix); + #endif diff --git a/tests/unit/socket-helpers.c b/tests/unit/socket-helpers.c index 5af4de513b..eecadf3a3c 100644 --- a/tests/unit/socket-helpers.c +++ b/tests/unit/socket-helpers.c @@ -154,3 +154,19 @@ int socket_check_protocol_support(bool *has_ipv4, bool *has_ipv6) return 0; } + +void socket_check_afunix_support(bool *has_afunix) +{ + int fd; + + fd = socket(PF_UNIX, SOCK_STREAM, 0); + closesocket(fd); + +#ifdef _WIN32 + *has_afunix = (fd != (int)INVALID_SOCKET); +#else + *has_afunix = (fd >= 0); +#endif + + return; +} diff --git a/tests/unit/test-io-channel-socket.c b/tests/unit/test-io-channel-socket.c index 6713886d02..b36a5d972a 100644 --- a/tests/unit/test-io-channel-socket.c +++ b/tests/unit/test-io-channel-socket.c @@ -179,10 +179,12 @@ static void test_io_channel(bool async, test_io_channel_setup_async(listen_addr, connect_addr, &srv, &src, &dst); +#ifndef _WIN32 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)); +#endif g_assert(qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_SHUTDOWN)); g_assert(qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_SHUTDOWN)); @@ -206,10 +208,12 @@ static void test_io_channel(bool async, test_io_channel_setup_async(listen_addr, connect_addr, &srv, &src, &dst); +#ifndef _WIN32 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)); +#endif g_assert(qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_SHUTDOWN)); g_assert(qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_SHUTDOWN)); @@ -236,10 +240,12 @@ static void test_io_channel(bool async, test_io_channel_setup_sync(listen_addr, connect_addr, &srv, &src, &dst); +#ifndef _WIN32 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)); +#endif g_assert(qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_SHUTDOWN)); g_assert(qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_SHUTDOWN)); @@ -263,10 +269,12 @@ static void test_io_channel(bool async, test_io_channel_setup_sync(listen_addr, connect_addr, &srv, &src, &dst); +#ifndef _WIN32 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)); +#endif g_assert(qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_SHUTDOWN)); g_assert(qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_SHUTDOWN)); @@ -367,7 +375,6 @@ static void test_io_channel_ipv6_async(void) } -#ifndef _WIN32 static void test_io_channel_unix(bool async) { SocketAddress *listen_addr = g_new0(SocketAddress, 1); @@ -398,6 +405,7 @@ static void test_io_channel_unix_async(void) return test_io_channel_unix(true); } +#ifndef _WIN32 static void test_io_channel_unix_fd_pass(void) { SocketAddress *listen_addr = g_new0(SocketAddress, 1); @@ -491,6 +499,7 @@ static void test_io_channel_unix_fd_pass(void) } g_free(fdrecv); } +#endif /* _WIN32 */ static void test_io_channel_unix_listen_cleanup(void) { @@ -522,9 +531,6 @@ static void test_io_channel_unix_listen_cleanup(void) unlink(TEST_SOCKET); } -#endif /* _WIN32 */ - - static void test_io_channel_ipv4_fd(void) { QIOChannel *ioc; @@ -555,7 +561,7 @@ static void test_io_channel_ipv4_fd(void) int main(int argc, char **argv) { - bool has_ipv4, has_ipv6; + bool has_ipv4, has_ipv6, has_afunix; module_call_init(MODULE_INIT_QOM); qemu_init_main_loop(&error_abort); @@ -588,16 +594,19 @@ int main(int argc, char **argv) test_io_channel_ipv6_async); } + 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); #ifndef _WIN32 - 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); - g_test_add_func("/io/channel/socket/unix-fd-pass", - test_io_channel_unix_fd_pass); - g_test_add_func("/io/channel/socket/unix-listen-cleanup", - test_io_channel_unix_listen_cleanup); -#endif /* _WIN32 */ + 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); + } end: return g_test_run();