Skip to content

Commit

Permalink
Add IPv6 check
Browse files Browse the repository at this point in the history
  • Loading branch information
silent-observer committed Oct 31, 2024
1 parent 69f5f03 commit c95a137
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions src/backend/cdb/motion/test/cdbsenddummypacket_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,21 +296,46 @@ test_send_dummy_packet_ipv6_to_ipv6_wildcard(void **state)
wait_for_receiver(false);
}


/*
* Test if IPv6 is supported.
*
* Sometimes even if IPv6 socket is successfully created it is not possible
* to bind an address to it (if IPv6 is disabled).
*
* Note: this is not a general solution and should only be used for this
* specific test. It is theoretically possible that loopback IPv6 address
* is available but real IPv6 addresses aren't. However, checking for that
* would be a lot more complicated
*/
static bool is_ipv6_supported(void) {
int sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
if (sockfd < 0 && errno == EAFNOSUPPORT)
return false;

struct sockaddr_in6 socket_struct;
socket_struct.sin6_family = AF_INET6;
socket_struct.sin6_addr = in6addr_loopback;
socket_struct.sin6_port = 0;
socket_struct.sin6_scope_id = 0;
int res = bind(sockfd, (struct sockaddr*) &socket_struct, sizeof(socket_struct));
if (res < 0 && errno == EADDRNOTAVAIL)
return false;

closesocket(sockfd);
return true;
}

int
main(int argc, char* argv[])
{
cmockery_parse_arguments(argc, argv);

int is_ipv6_supported = true;
int sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
if (sockfd < 0 && errno == EAFNOSUPPORT)
is_ipv6_supported = false;

log_min_messages = DEBUG1;

start_receiver();

if (is_ipv6_supported)
if (is_ipv6_supported())
{
const UnitTest tests[] = {
unit_test(test_send_dummy_packet_ipv4_to_ipv4),
Expand Down

0 comments on commit c95a137

Please sign in to comment.