Skip to content

Commit 4d197db

Browse files
committed
test: make csf.pl easier to unit test
1 parent 0b4bc7f commit 4d197db

5 files changed

Lines changed: 340 additions & 144 deletions

File tree

.github/tests/lib/TestCSFScript.pm

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package TestCSFScript;
2+
3+
use strict;
4+
use warnings;
5+
6+
use Exporter qw(import);
7+
use File::Spec;
8+
use TestBootstrap qw(repo_root);
9+
10+
our @EXPORT_OK = qw(load_csf_pl);
11+
12+
sub _install_stubs {
13+
{
14+
no warnings 'once';
15+
package ConfigServer::Config;
16+
sub import { return }
17+
sub loadconfig { bless {}, __PACKAGE__ }
18+
sub get_config { return '' }
19+
sub config { return () }
20+
sub ipv4reg { return qr/(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/ }
21+
sub ipv6reg { return qr/[0-9A-Fa-f:]+/ }
22+
}
23+
$INC{'ConfigServer/Config.pm'} = __FILE__;
24+
25+
{
26+
no warnings 'once';
27+
package ConfigServer::URLGet;
28+
sub import { return }
29+
sub urlget { return }
30+
}
31+
$INC{'ConfigServer/URLGet.pm'} = __FILE__;
32+
}
33+
34+
sub load_csf_pl {
35+
_install_stubs();
36+
my $script = File::Spec->catfile( repo_root(), 'csf.pl' );
37+
my $loaded = do $script;
38+
die $@ || $! unless $loaded;
39+
return $script;
40+
}
41+
42+
1;

.github/tests/unit/csf_dogrep.t

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env perl
2+
3+
BEGIN {
4+
*CORE::GLOBAL::exit = sub { die "unexpected exit(@_)" };
5+
}
6+
7+
use strict;
8+
use warnings;
9+
10+
use FindBin qw($Bin);
11+
use File::Spec;
12+
use lib File::Spec->catdir( $Bin, '..', 'lib' );
13+
14+
use Test::More;
15+
use TestCSFScript qw(load_csf_pl);
16+
17+
load_csf_pl();
18+
19+
my @warnings;
20+
local $SIG{__WARN__} = sub { push @warnings, @_ };
21+
22+
{
23+
no warnings 'once';
24+
no warnings 'redefine';
25+
26+
local %sbin::csf::config = (
27+
IPTABLES => '/sbin/iptables',
28+
IPTABLESWAIT => '',
29+
NAT => 0,
30+
MANGLE => 0,
31+
RAW => 0,
32+
IPV6 => 0,
33+
LF_IPSET => 0,
34+
);
35+
local $sbin::csf::verbose = 0;
36+
local $sbin::csf::cleanreg = qr/[\r\n]+/;
37+
38+
local *sbin::csf::run_open3 = sub {
39+
my $output = "Chain INPUT (policy ACCEPT)\n";
40+
open my $fh, '<', \$output or die $!;
41+
$_[0] = undef;
42+
$_[1] = $fh;
43+
$_[2] = $fh;
44+
return -1;
45+
};
46+
47+
local *sbin::csf::slurpee = sub {
48+
my ($path) = @_;
49+
return ( '', '0|1.2.3.4|80|in|600|note' ) if $path =~ /csf\.tempallow$/;
50+
return ( '', '0|1.2.3.4|80|in|600|note' ) if $path =~ /csf\.tempban$/;
51+
return ('') if $path =~ /csf\.(allow|deny)$/;
52+
return;
53+
};
54+
55+
local *sbin::csf::slurp = sub {
56+
my ($path) = @_;
57+
return ('') if $path =~ /csf\.(allow|deny)$/;
58+
return;
59+
};
60+
61+
local *sbin::csf::checkip = sub { return };
62+
63+
my $ok = eval { sbin::csf::dogrep('1.2.3.4'); 1 };
64+
ok( $ok, 'dogrep handles blank temp allow/deny lines without dying' ) or diag $@;
65+
}
66+
67+
is_deeply( \@warnings, [], 'dogrep emits no warnings for blank temp allow/deny lines' );
68+
69+
done_testing;

.github/tests/unit/csf_modulino.t

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env perl
2+
3+
BEGIN {
4+
*CORE::GLOBAL::exit = sub { die "unexpected exit(@_)" };
5+
}
6+
7+
use strict;
8+
use warnings;
9+
10+
use FindBin qw($Bin);
11+
use File::Spec;
12+
use lib File::Spec->catdir( $Bin, '..', 'lib' );
13+
14+
use Test::More;
15+
use TestBootstrap qw(repo_root);
16+
use TestCSFScript qw(load_csf_pl);
17+
18+
my $script = File::Spec->catfile( repo_root(), 'csf.pl' );
19+
local @ARGV = ('__should_not_run__');
20+
21+
my $loaded = eval { load_csf_pl(); 1 };
22+
ok( $loaded, 'csf.pl loads successfully as a modulino' ) or diag $@ || $!;
23+
can_ok( 'sbin::csf', qw(run run_open3 process_input) );
24+
{
25+
no warnings 'once';
26+
ok( !defined $sbin::csf::input{command}, 'loading csf.pl does not execute top-level dispatch' );
27+
}
28+
29+
done_testing;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env perl
2+
3+
BEGIN {
4+
*CORE::GLOBAL::exit = sub { die "unexpected exit(@_)" };
5+
}
6+
7+
use strict;
8+
use warnings;
9+
10+
use FindBin qw($Bin);
11+
use File::Spec;
12+
use lib File::Spec->catdir( $Bin, '..', 'lib' );
13+
14+
use Test::More;
15+
use TestCSFScript qw(load_csf_pl);
16+
17+
load_csf_pl();
18+
19+
{
20+
no warnings 'once';
21+
local %sbin::csf::input;
22+
local @ARGV = ( '--ADD', '1.2.3.4', 'with', 'comment' );
23+
24+
sbin::csf::process_input();
25+
26+
is( $sbin::csf::input{command}, '--add', 'process_input lowercases the command' );
27+
is( $sbin::csf::input{argument}, '1.2.3.4 with comment', 'process_input joins remaining args into argument' );
28+
}
29+
30+
{
31+
no warnings 'once';
32+
local %sbin::csf::input;
33+
local @ARGV = ('--help');
34+
35+
sbin::csf::process_input();
36+
37+
is( $sbin::csf::input{command}, '--help', 'single-argument command is preserved' );
38+
ok( !defined $sbin::csf::input{argument}, 'single-argument command leaves argument undefined' );
39+
}
40+
41+
done_testing;

0 commit comments

Comments
 (0)