|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | + |
| 6 | +use FindBin qw($Bin); |
| 7 | +use File::Spec; |
| 8 | +use File::Temp qw(tempdir); |
| 9 | +use Test::More; |
| 10 | +use lib "$Bin/../lib"; |
| 11 | + |
| 12 | +use TestBootstrap (); |
| 13 | + |
| 14 | +{ |
| 15 | + package Local::AbuseIPConfig; |
| 16 | + |
| 17 | + sub new { |
| 18 | + my ($class, $config) = @_; |
| 19 | + return bless { config => $config }, $class; |
| 20 | + } |
| 21 | + |
| 22 | + sub config { |
| 23 | + my ($self) = @_; |
| 24 | + return %{ $self->{config} }; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +sub reload_abuseip_module { |
| 29 | + my ($config) = @_; |
| 30 | + |
| 31 | + require ConfigServer::Config; |
| 32 | + |
| 33 | + no warnings qw(redefine once); |
| 34 | + local *ConfigServer::Config::loadconfig = sub { |
| 35 | + return Local::AbuseIPConfig->new($config); |
| 36 | + }; |
| 37 | + |
| 38 | + delete $INC{'ConfigServer/AbuseIP.pm'}; |
| 39 | + require ConfigServer::AbuseIP; |
| 40 | + return 1; |
| 41 | +} |
| 42 | + |
| 43 | +sub build_fake_host_binary { |
| 44 | + my ($dir, %opts) = @_; |
| 45 | + |
| 46 | + my $script = File::Spec->catfile($dir, 'fake-host.pl'); |
| 47 | + my $args_log = File::Spec->catfile($dir, 'fake-host-args.txt'); |
| 48 | + my $output = $opts{output} // ""; |
| 49 | + |
| 50 | + open(my $fh, '>', $script) or die "Unable to create $script: $!"; |
| 51 | + print {$fh} <<EOF; |
| 52 | +#!/usr/bin/env perl |
| 53 | +use strict; |
| 54 | +use warnings; |
| 55 | +
|
| 56 | +open(my \$log, '>', q($args_log)) or die "Unable to write $args_log: \$!"; |
| 57 | +print {\$log} join("\n", \@ARGV), "\n"; |
| 58 | +close(\$log); |
| 59 | +
|
| 60 | +print <<'OUT'; |
| 61 | +$output |
| 62 | +OUT |
| 63 | +EOF |
| 64 | + close($fh); |
| 65 | + |
| 66 | + chmod 0755, $script or die "Unable to chmod $script: $!"; |
| 67 | + return ($script, $args_log); |
| 68 | +} |
| 69 | + |
| 70 | +sub slurp_file { |
| 71 | + my ($path) = @_; |
| 72 | + |
| 73 | + open(my $fh, '<', $path) or die "Unable to open $path: $!"; |
| 74 | + my @lines = <$fh>; |
| 75 | + close($fh); |
| 76 | + chomp @lines; |
| 77 | + return @lines; |
| 78 | +} |
| 79 | + |
| 80 | +subtest 'abuseip resolves an IPv4 abuse contact and builds the human message' => sub { |
| 81 | + my $dir = tempdir(CLEANUP => 1); |
| 82 | + my $lookup = '10.2.0.192.abuse-contacts.abusix.org'; |
| 83 | + my ($host_bin, $args_log) = build_fake_host_binary( |
| 84 | + $dir, |
| 85 | + output => qq{$lookup has TXT record "abuse\@example.test"\n}, |
| 86 | + ); |
| 87 | + |
| 88 | + reload_abuseip_module({ HOST => $host_bin }); |
| 89 | + my ($contact, $message) = ConfigServer::AbuseIP::abuseip('192.0.2.10'); |
| 90 | + |
| 91 | + is($contact, 'abuse@example.test', 'IPv4 lookup returns the TXT contact value'); |
| 92 | + like($message, qr/Abuse Contact for 192\.0\.2\.10: \[abuse\@example\.test\]/, 'message includes the IP and abuse contact'); |
| 93 | + like($message, qr/abusix\.com/, 'message includes the explanatory abuse-contact text'); |
| 94 | + is_deeply( |
| 95 | + [ slurp_file($args_log) ], |
| 96 | + ['-W', '5', '-t', 'TXT', $lookup], |
| 97 | + 'host binary receives the expected IPv4 reverse lookup target', |
| 98 | + ); |
| 99 | +}; |
| 100 | + |
| 101 | +subtest 'abuseip resolves an IPv6 abuse contact' => sub { |
| 102 | + my $dir = tempdir(CLEANUP => 1); |
| 103 | + my $lookup = '0.2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.abuse-contacts.abusix.org'; |
| 104 | + my ($host_bin, $args_log) = build_fake_host_binary( |
| 105 | + $dir, |
| 106 | + output => qq{$lookup has TXT record "ipv6-contact\@example.test"\n}, |
| 107 | + ); |
| 108 | + |
| 109 | + reload_abuseip_module({ HOST => $host_bin }); |
| 110 | + my ($contact, $message) = ConfigServer::AbuseIP::abuseip('2001:db8::20'); |
| 111 | + |
| 112 | + is($contact, 'ipv6-contact@example.test', 'IPv6 lookup returns the TXT contact value'); |
| 113 | + like($message, qr/Abuse Contact for 2001:db8::20: \[ipv6-contact\@example\.test\]/, 'message includes the IPv6 address and contact'); |
| 114 | + is_deeply( |
| 115 | + [ slurp_file($args_log) ], |
| 116 | + ['-W', '5', '-t', 'TXT', $lookup], |
| 117 | + 'host binary receives the expected IPv6 reverse lookup target', |
| 118 | + ); |
| 119 | +}; |
| 120 | + |
| 121 | +subtest 'abuseip returns nothing for invalid input and skips the host lookup entirely' => sub { |
| 122 | + my $dir = tempdir(CLEANUP => 1); |
| 123 | + my ($host_bin, $args_log) = build_fake_host_binary( |
| 124 | + $dir, |
| 125 | + output => qq{should not be used\n}, |
| 126 | + ); |
| 127 | + |
| 128 | + reload_abuseip_module({ HOST => $host_bin }); |
| 129 | + my $result = ConfigServer::AbuseIP::abuseip('not-an-ip'); |
| 130 | + |
| 131 | + ok(!$result, 'invalid input returns a false value'); |
| 132 | + ok(!-e $args_log, 'host binary is not invoked for invalid input'); |
| 133 | +}; |
| 134 | + |
| 135 | +subtest 'abuseip returns nothing when the lookup output contains no quoted contact' => sub { |
| 136 | + my $dir = tempdir(CLEANUP => 1); |
| 137 | + my $lookup = '10.2.0.192.abuse-contacts.abusix.org'; |
| 138 | + my ($host_bin, $args_log) = build_fake_host_binary( |
| 139 | + $dir, |
| 140 | + output => qq{$lookup lookup failed\n}, |
| 141 | + ); |
| 142 | + |
| 143 | + reload_abuseip_module({ HOST => $host_bin }); |
| 144 | + my $result = ConfigServer::AbuseIP::abuseip('192.0.2.10'); |
| 145 | + |
| 146 | + ok(!$result, 'missing TXT contact produces a false value'); |
| 147 | + is_deeply( |
| 148 | + [ slurp_file($args_log) ], |
| 149 | + ['-W', '5', '-t', 'TXT', $lookup], |
| 150 | + 'host lookup still runs even when no contact is found', |
| 151 | + ); |
| 152 | +}; |
| 153 | + |
| 154 | +done_testing; |
0 commit comments