pf-tools/pf-tools: Syntax.pm: can use regexp or own sub to valid...
parmelan-guest at users.alioth.debian.org
parmelan-guest at users.alioth.debian.org
Mon Dec 15 12:19:01 UTC 2014
details: http://hg.debian.org/hg/pf-tools/pf-tools/rev/a434840de06f
changeset: 1383:a434840de06f
user: melkor <melkor at sitadelle.com>
date: Mon Dec 15 13:16:45 2014 +0100
description:
Syntax.pm: can use regexp or own sub to validate a value defined in a pf-tools CONFIG file
diffstat:
lib/PFTools/Conf/Syntax.pm | 39 ++++++++++++++++++++++++++-------------
t/14.syntax.t | 34 ++++++++++++++++++++++++++++++++++
2 files changed, 60 insertions(+), 13 deletions(-)
diffs (111 lines):
diff -r f28563e8d1d4 -r a434840de06f lib/PFTools/Conf/Syntax.pm
--- a/lib/PFTools/Conf/Syntax.pm Mon Dec 15 10:13:02 2014 +0100
+++ b/lib/PFTools/Conf/Syntax.pm Mon Dec 15 13:16:45 2014 +0100
@@ -402,9 +402,17 @@
? $section_hash->{$key}
: [ $section_hash->{$key} ];
- __check_key_value_from_definition( $key, $value,
- $definition->{$def_key},
- $section_name, $file );
+ eval {
+ if (!__check_key_value_from_definition($value, $definition->{$def_key}, $section_hash)) {
+ croak
+ qq{ERROR: file $file, section $section_name, key $key, value '$value' is not valid};
+ }
+ };
+
+ if ($EVAL_ERROR) {
+ croak
+ qq{ERROR: file $file, section $section_name, key $key, value '$value': $EVAL_ERROR};
+ }
}
if ($check_mandatory_keys) {
@@ -419,7 +427,7 @@
return 1;
}
-=head2 __check_key_value_from_definition($key_name,$values_ref,$regexp,$section_name,$file_name)
+=head2 __check_key_value_from_definition($values_ref,$regexp)
Checks values for a specified key. Returns true value on success or croaks
on errors. The parameters are:
@@ -432,25 +440,30 @@
=item I<regexp> the regexp agains which each value will be checked
-=item I<section_name> the section name (for error messages)
-
-=item I<file_name> the file name (for error messages)
-
=back
=cut
sub __check_key_value_from_definition {
- my ( $key_name, $values_ref, $regexp, $section_name, $file_name ) = @_;
+ my ( $values_ref, $check, $section ) = @_;
foreach my $value ( @{$values_ref} ) {
# Remove surrounding whitespace
$value =~ s{ \A \s* (\S*) \s* \z }{$1}xms;
-
- if ( $value !~ m{ \A $regexp \z }xms ) {
- croak
- qq{ERROR: file $file_name, section $section_name, key $key_name: value '$value' doesn't match $regexp};
+ my $check_type = ref $check;
+ if ($check_type eq q{CODE}) {
+ if (!$check->($value, $section)) {
+ return 0;
+ }
+ }
+ elsif ($check_type eq q{Regexp}) {
+ if ($value !~ $check) {
+ return 0;
+ }
+ }
+ elsif ( $value !~ m{ \A $check \z }xms ) {
+ return 0;
}
}
diff -r f28563e8d1d4 -r a434840de06f t/14.syntax.t
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/t/14.syntax.t Mon Dec 15 13:16:45 2014 +0100
@@ -0,0 +1,34 @@
+#!perl
+
+use strict;
+use warnings;
+
+use English qw( -no_match_vars ); # Avoids regex performance penalty
+use Test::Exception;
+use Test::More qw( no_plan );
+
+use PFTools::Conf::Syntax;
+
+########################################################################
+my $test_module = q{PFTools::Conf::Syntax};
+
+my $test_sub = q{__check_key_value_from_definition};
+note(qq{Testing ${test_module}::$test_sub});
+
+can_ok( $test_module, ( $test_sub ) );
+
+my $ipv4 = '1.2.3.4';
+
+ok( PFTools::Conf::Syntax::__check_key_value_from_definition([ $ipv4 ], $PFTools::Conf::Syntax::DEF_SECTIONS->{network}{server}{ipv4}), q{ipv4 definition in DEF_SECTIONS});
+
+ok( PFTools::Conf::Syntax::__check_key_value_from_definition([ $ipv4 ], qr{(\d{1,3})((\.\d{1,3}){1,3})?}), q{ipv4 definition with regexp});
+
+ok( PFTools::Conf::Syntax::__check_key_value_from_definition([ $ipv4 ], sub { return shift =~ m{(\d{1,3})((\.\d{1,3}){1,3})?}} ), q{ipv4 definition with sub});
+
+ok( PFTools::Conf::Syntax::__check_key_value_from_definition([ $ipv4 ], sub { my ($value, $hash) = @_; return ($hash->{ipv4} or $hash->{ipv6})}, { ipv4 => undef, ipv6 => q{1:::1} }), q{ipv6 definition in sub with hash} );
+
+$test_sub = q{check_section_structure};
+note(qq{Testing ${test_module}::$test_sub});
+
+can_ok( $test_module, ( $test_sub ) );
+__END__
More information about the pf-tools-commits
mailing list