Skip to content

Commit

Permalink
Test IP address in config file.
Browse files Browse the repository at this point in the history
Write constant IP addresses into pfresolved config.
Wait until pfresolved creates table regress-pfresolved.
Read IP addresses from pf table with pfctl.
Check that output IP is the same as input IP.
  • Loading branch information
bluhm committed Nov 3, 2023
1 parent 24d9ba7 commit 9c44b82
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 8 deletions.
61 changes: 61 additions & 0 deletions regress/Pfctl.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# $OpenBSD$

# Copyright (c) 2023 Alexander Bluhm <bluhm@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use strict;
use warnings;

package Pfctl;
use parent 'Proc';
use Carp;

sub new {
my $class = shift;
my %args = @_;
$args{func} ||= \&func;
$args{logfile} ||= "pfctl.log";
$args{up} ||= "Table";

my $self = Proc::new($class, %args);
return $self;
}

sub child {
my $self = shift;
my $timeout = $self->{timeout} || 5;
my $updates = $self->{updates};
my $pfresolved = $self->{pfresolved};

my $table = "updating addresses for pf table";
my $tomsg = $timeout ? " after $timeout seconds" : "";
my $upmsg = $updates ? " for $updates times" : "";
$pfresolved->loggrep($table, $timeout, $updates)
or die ref($self), " no '$table' in $pfresolved->{logfile}",
$tomsg, $upmsg;

open(STDOUT, '>&', \*STDERR)
or die ref($self), " dup STDOUT failed: $!";
}

sub func {
my $self = shift;
my @sudo = $ENV{SUDO} ? $ENV{SUDO} : ();

my @cmd = (@sudo, qw(pfctl -t regress-pfresolved -T show));
system(@cmd)
and die die ref($self), " command '@cmd' failed: $?";
}

1;
2 changes: 2 additions & 0 deletions regress/Pfresolved.pm
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ sub new {
" config file '$self->{conffile}' create failed: $!";
print $fh "# test $test\n";
print $fh "regress-pfresolved {\n";
print $fh "\t", join(",", @{$self->{address_list}}), "\n"
if $self->{address_list};
print $fh "\n}\n";

return $self;
Expand Down
1 change: 0 additions & 1 deletion regress/Proc.pm
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ sub run {
do {
$self->child();
print STDERR $self->{up}, "\n";
$self->{begin} = time();
$self->{func}->($self);
} while ($self->{redo});
print STDERR "Shutdown", "\n";
Expand Down
21 changes: 21 additions & 0 deletions regress/args-ip.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Write constant IP addresses into pfresolved config.
# Wait until pfresolved creates table regress-pfresolved.
# Read IP addresses from pf table with pfctl.
# Check that output IP is the same as input IP.

use strict;
use warnings;

our %args = (
pfresolved => {
address_list => [qw(192.0.2.1 2001:DB8::1)], # documentation IPs
},
pfctl => {
loggrep => {
qr/^ 192.0.2.1$/ => 1,
qr/^ 2001:db8::1$/ => 1,
},
},
);

1;
10 changes: 5 additions & 5 deletions regress/funcs.pl
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
use warnings;

sub check_logs {
my ($n, $d, $p, %args) = @_;
my ($n, $d, $s, %args) = @_;

return if $args{nocheck};

check_loggrep($n, $d, $p, %args);
check_loggrep($n, $d, $s, %args);
}

sub check_loggrep {
my ($n, $d, $p, %args) = @_;
my ($n, $d, $s, %args) = @_;

my %name2proc = (nsd => $n, pfresolved => $d, pf => $p);
foreach my $name (qw(pfresolved)) {
my %name2proc = (nsd => $n, pfresolved => $d, pfctl => $s);
foreach my $name (qw(pfresolved pfctl)) {
my $p = $name2proc{$name} or next;
my $pattern = $args{$name}{loggrep} or next;
$pattern = [ $pattern ] unless ref($pattern) eq 'ARRAY';
Expand Down
12 changes: 10 additions & 2 deletions regress/pfresolved.pl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use warnings;

use Pfresolved;
use Pfctl;
require 'funcs.pl';

sub usage {
Expand All @@ -36,13 +37,20 @@ sub usage {

my $d = Pfresolved->new(
%{$args{pfresolved}},
testfile => $testfile,
testfile => $testfile,
);
my $s = Pfctl->new(
%{$args{pfctl}},
pfresolved => $d,
);

$d->run;
$d->up;

$s->run;
$s->up;

$d->kill_child;
$d->down;

check_logs(undef, $d, undef, %args);
check_logs(undef, $d, $s, %args);

0 comments on commit 9c44b82

Please sign in to comment.