From 3e363d8dbe3228b3b2546f28b67746c9d16a19a9 Mon Sep 17 00:00:00 2001
From: Erik Auerswald <auerswal@unix-ag.uni-kl.de>
Date: Sun, 27 Mar 2022 14:33:29 +0200
Subject: [PATCH 1/8] add more generator tests for edge cases

Test and thus document current edge case behavior of "fping -g":

* wrong number or kind of arguments fails and prints usage;
* an empty range silently pings nothing and fping returns 1;
* a too large range fails with an error message;
* a zero CIDR prefix length fails with an error message.

While a zero CIDR prefix length is syntactically valid and used
to describe the default route (0.0.0.0/0), this range includes
both IP multicast (224.0.0.0/4) and the reserved 240.0.0.0/4
(formerly class E) addresses.  Thus it seems dubious to add zero
CIDR prefix length handling for IPv4 address generation to fping.

Additionally, the current CIDR generator code would overflow with
a zero prefix length on systems where "unsigned long" is a 32-bit
type (e.g., Windows).
---
 ci/test-06-options-f-h.pl | 59 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 57 insertions(+), 2 deletions(-)

diff --git a/ci/test-06-options-f-h.pl b/ci/test-06-options-f-h.pl
index a0e2998..4f26b9e 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 => 24;
+use Test::Command tests => 45;
 use File::Temp;
 
 #  -f file    read list of targets from a file ( - means stdin) (only if no -g specified)
@@ -37,6 +37,29 @@ $cmd->stdout_is_eq("127.0.0.1 is alive\n127.0.0.2 is alive\n");
 $cmd->stderr_is_eq("");
 }
 
+# fping -g (error: no argument)
+{
+my $cmd = Test::Command->new(cmd => "fping -g");
+$cmd->exit_is_num(1);
+$cmd->stdout_is_eq("");
+$cmd->stderr_like(qr{^Usage: fping \[options\] \[targets\.\.\.\]});
+}
+
+# fping -g (error: single argument, but not in cidr format)
+{
+my $cmd = Test::Command->new(cmd => "fping -g 127.0.0.1");
+$cmd->exit_is_num(1);
+$cmd->stdout_is_eq("");
+$cmd->stderr_like(qr{^Usage: fping \[options\] \[targets\.\.\.\]});
+}
+# fping -g (error: too many arguments)
+{
+my $cmd = Test::Command->new(cmd => "fping -g 127.0.0.1 127.0.0.2 127.0.0.3");
+$cmd->exit_is_num(1);
+$cmd->stdout_is_eq("");
+$cmd->stderr_like(qr{^Usage: fping \[options\] \[targets\.\.\.\]});
+}
+
 # fping -g (range)
 {
 my $cmd = Test::Command->new(cmd => "fping -g 127.0.0.1 127.0.0.5");
@@ -45,6 +68,22 @@ $cmd->stdout_is_eq("127.0.0.1 is alive\n127.0.0.2 is alive\n127.0.0.3 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");
+$cmd->exit_is_num(1);
+$cmd->stdout_is_eq("");
+$cmd->stderr_is_eq("");
+}
+
+# fping -g (too large range)
+{
+my $cmd = Test::Command->new(cmd => "fping -g 127.0.0.1 127.255.255.254");
+$cmd->exit_is_num(1);
+$cmd->stdout_is_eq("");
+$cmd->stderr_is_eq("fping: -g parameter generates too many addresses\n");
+}
+
 # fping -g (cidr)
 {
 my $cmd = Test::Command->new(cmd => "fping -g 127.0.0.1/30");
@@ -53,7 +92,7 @@ $cmd->stdout_is_eq("127.0.0.1 is alive\n127.0.0.2 is alive\n");
 $cmd->stderr_is_eq("");
 }
 
-# fping -g (cidr - long prefixes)
+# fping -g (cidr - long prefixes: point-to-point)
 {
 my $cmd = Test::Command->new(cmd => "fping -g 127.0.0.2/31");
 $cmd->exit_is_num(0);
@@ -61,6 +100,14 @@ $cmd->stdout_is_eq("127.0.0.2 is alive\n127.0.0.3 is alive\n");
 $cmd->stderr_is_eq("");
 }
 
+# fping -g (cidr - long prefixes: host)
+{
+my $cmd = Test::Command->new(cmd => "fping -g 127.0.0.2/32");
+$cmd->exit_is_num(0);
+$cmd->stdout_is_eq("127.0.0.2 is alive\n");
+$cmd->stderr_is_eq("");
+}
+
 # fping -g (cidr - too long prefixes)
 {
 my $cmd = Test::Command->new(cmd => "fping -g 127.0.0.2/33");
@@ -69,6 +116,14 @@ $cmd->stdout_is_eq("");
 $cmd->stderr_is_eq("fping: netmask must be between 1 and 32 (is: 33)\n");
 }
 
+# fping -g (cidr - too short prefixes)
+{
+my $cmd = Test::Command->new(cmd => "fping -g 127.0.0.2/0");
+$cmd->exit_is_num(1);
+$cmd->stdout_is_eq("");
+$cmd->stderr_is_eq("fping: netmask must be between 1 and 32 (is: 0)\n");
+}
+
 # fping -H
 {
 my $cmd = Test::Command->new(cmd => "fping -H 1 127.0.0.1");
-- 
2.25.1

