diff --git a/ci/test-06-options-f-h.pl b/ci/test-06-options-f-h.pl index c9b520a..a354ae5 100755 --- a/ci/test-06-options-f-h.pl +++ b/ci/test-06-options-f-h.pl @@ -1,6 +1,6 @@ #!/usr/bin/perl -w -use Test::Command tests => 75; +use Test::Command tests => 102; use Test::More; use File::Temp; @@ -114,6 +114,14 @@ $cmd->stderr_is_eq(""); } +# fping -4 -g (range) +{ +my $cmd = Test::Command->new(cmd => "fping -4 -g 127.0.0.1 127.0.0.5"); +$cmd->exit_is_num(0); +$cmd->stdout_is_eq("127.0.0.1 is alive\n127.0.0.2 is alive\n127.0.0.3 is alive\n127.0.0.4 is alive\n127.0.0.5 is alive\n"); +$cmd->stderr_is_eq(""); +} + # fping -g (empty range) { my $cmd = Test::Command->new(cmd => "fping -g 127.0.0.2 127.0.0.1"); @@ -178,7 +186,51 @@ $cmd->stderr_is_eq("fping: -g parameter generates too many addresses\n"); } +# fping -4 -g (range, wrong address family) +SKIP: { + if($ENV{SKIP_IPV6}) { + skip 'Skip IPv6 tests', 3; + } + my $cmd = Test::Command->new(cmd => "fping -4 -g ::1 ::1"); + $cmd->exit_is_num(1); + $cmd->stdout_is_eq(""); + $cmd->stderr_like(qr{can't parse address ::1:.*(not supported|not known)}); +} + +# fping -6 -g (range, wrong address family) +SKIP: { + if($ENV{SKIP_IPV6}) { + skip 'Skip IPv6 tests', 3; + } + my $cmd = Test::Command->new(cmd => "fping -6 -g 127.0.0.1 127.0.0.1"); + $cmd->exit_is_num(1); + $cmd->stdout_is_eq(""); + $cmd->stderr_like(qr{can't parse address 127\.0\.0\.1:.*(not supported|not known)}); +} + # fping -g (range - no IPv6 generator) +SKIP: { + if($ENV{SKIP_IPV6}) { + skip 'Skip IPv6 tests', 3; + } + my $cmd = Test::Command->new(cmd => "fping -g ::1 ::1"); + $cmd->exit_is_num(1); + $cmd->stdout_is_eq(""); + $cmd->stderr_is_eq("fping: -g works only with IPv4 addresses\n"); +} + +# fping -g (empty range - no IPv6 generator) +SKIP: { + if($ENV{SKIP_IPV6}) { + skip 'Skip IPv6 tests', 3; + } + my $cmd = Test::Command->new(cmd => "fping -g ::1 ::"); + $cmd->exit_is_num(1); + $cmd->stdout_is_eq(""); + $cmd->stderr_is_eq("fping: -g works only with IPv4 addresses\n"); +} + +# fping -6 -g (range - no IPv6 generator) SKIP: { if($ENV{SKIP_IPV6}) { skip 'Skip IPv6 tests', 3; @@ -189,7 +241,7 @@ $cmd->stderr_is_eq("fping: -g works only with IPv4 addresses\n"); } -# fping -g (range - no IPv6 generator - start address IPv6) +# fping -6 -g (range - no IPv6 generator - start address IPv6) SKIP: { if($ENV{SKIP_IPV6}) { skip 'Skip IPv6 tests', 3; @@ -201,6 +253,17 @@ } # fping -g (range - no IPv6 generator - end address IPv6) +SKIP: { + if($ENV{SKIP_IPV6}) { + skip 'Skip IPv6 tests', 3; + } + my $cmd = Test::Command->new(cmd => "fping -g 127.0.0.1 ::1"); + $cmd->exit_is_num(1); + $cmd->stdout_is_eq(""); + $cmd->stderr_like(qr{fping: can't parse address ::1: .*\n}); +} + +# fping -6 -g (range - no IPv6 generator - end address IPv6) SKIP: { if($ENV{SKIP_IPV6}) { skip 'Skip IPv6 tests', 3; @@ -208,10 +271,43 @@ my $cmd = Test::Command->new(cmd => "fping -6 -g 127.0.0.1 ::1"); $cmd->exit_is_num(1); $cmd->stdout_is_eq(""); - $cmd->stderr_is_eq("fping: -g works only with IPv4 addresses\n"); + $cmd->stderr_like(qr{fping: can't parse address 127\.0\.0\.1: .*\n}); +} + +# fping -4 -g (cidr, wrong address family) +SKIP: { + if($ENV{SKIP_IPV6}) { + skip 'Skip IPv6 tests', 3; + } + my $cmd = Test::Command->new(cmd => "fping -4 -g ::1/128"); + $cmd->exit_is_num(1); + $cmd->stdout_is_eq(""); + $cmd->stderr_like(qr{can't parse address ::1:.*(not supported|not known)}); +} + +# fping -6 -g (cidr, wrong address family) +SKIP: { + if($ENV{SKIP_IPV6}) { + skip 'Skip IPv6 tests', 3; + } + my $cmd = Test::Command->new(cmd => "fping -6 -g 127.0.0.1/32"); + $cmd->exit_is_num(1); + $cmd->stdout_is_eq(""); + $cmd->stderr_like(qr{can't parse address 127\.0\.0\.1:.*(not supported|not known)}); } # fping -g (CIDR - no IPv6 generator) +SKIP: { + if($ENV{SKIP_IPV6}) { + skip 'Skip IPv6 tests', 3; + } + my $cmd = Test::Command->new(cmd => "fping -g ::1/128"); + $cmd->exit_is_num(1); + $cmd->stdout_is_eq(""); + $cmd->stderr_is_eq("fping: -g works only with IPv4 addresses\n"); +} + +# fping -6 -g (CIDR - no IPv6 generator) SKIP: { if($ENV{SKIP_IPV6}) { skip 'Skip IPv6 tests', 3; diff --git a/src/fping.c b/src/fping.c index 5e059d0..bd2edd6 100644 --- a/src/fping.c +++ b/src/fping.c @@ -1307,7 +1307,7 @@ void add_cidr(char *addr) /* parse address (IPv4 only) */ memset(&addr_hints, 0, sizeof(struct addrinfo)); - addr_hints.ai_family = AF_UNSPEC; + addr_hints.ai_family = hints_ai_family; addr_hints.ai_flags = AI_NUMERICHOST; ret = getaddrinfo(addr, NULL, &addr_hints, &addr_res); if (ret) { @@ -1354,7 +1354,7 @@ void add_range(char *start, char *end) /* parse start address (IPv4 only) */ memset(&addr_hints, 0, sizeof(struct addrinfo)); - addr_hints.ai_family = AF_UNSPEC; + addr_hints.ai_family = hints_ai_family; addr_hints.ai_flags = AI_NUMERICHOST; ret = getaddrinfo(start, NULL, &addr_hints, &addr_res); if (ret) { @@ -1366,11 +1366,12 @@ void add_range(char *start, char *end) fprintf(stderr, "%s: -g works only with IPv4 addresses\n", prog); exit(1); } + hints_ai_family = addr_res->ai_family; start_long = ntohl(((struct sockaddr_in *)addr_res->ai_addr)->sin_addr.s_addr); /* parse end address (IPv4 only) */ memset(&addr_hints, 0, sizeof(struct addrinfo)); - addr_hints.ai_family = AF_UNSPEC; + addr_hints.ai_family = hints_ai_family; addr_hints.ai_flags = AI_NUMERICHOST; ret = getaddrinfo(end, NULL, &addr_hints, &addr_res); if (ret) {