added Utils::is_localhost and t/qpsmtpd-utils.t

resolves Issue #89
This commit is contained in:
Matt Simerson 2014-09-10 13:43:46 -07:00
parent 39c454c95e
commit 60877da4ed
3 changed files with 47 additions and 3 deletions

View File

@ -591,7 +591,7 @@ sub spool_dir {
unless ($Spool_dir) { # first time through unless ($Spool_dir) { # first time through
$self->log(LOGDEBUG, "Initializing spool_dir"); $self->log(LOGDEBUG, "Initializing spool_dir");
$Spool_dir = $self->config('spool_dir') $Spool_dir = $self->config('spool_dir')
|| Qpsmtpd::Utils::tildeexp('~/tmp/'); || Qpsmtpd::Utils->tildeexp('~/tmp/');
$Spool_dir .= "/" unless ($Spool_dir =~ m!/$!); $Spool_dir .= "/" unless ($Spool_dir =~ m!/$!);

View File

@ -2,7 +2,7 @@ package Qpsmtpd::Utils;
use strict; use strict;
sub tildeexp { sub tildeexp {
my $path = shift; my ($self, $path) = @_;
$path =~ s{^~([^/]*)} { $path =~ s{^~([^/]*)} {
$1 $1
? (getpwnam($1))[7] ? (getpwnam($1))[7]
@ -11,4 +11,13 @@ sub tildeexp {
return $path; return $path;
} }
sub is_localhost {
my ($self, $ip) = @_;
return if ! $ip;
return 1 if $ip =~ /^127\./; # IPv4
return 1 if $ip =~ /:127\./; # IPv4 mapped IPv6
return 1 if $ip eq '::1'; # IPv6
return;
}
1; 1;

35
t/qpsmtpd-utils.t Normal file
View File

@ -0,0 +1,35 @@
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use lib 'lib'; # test lib/Qpsmtpd/Utils (vs site_perl)
BEGIN { use_ok('Qpsmtpd::Utils'); }
my $utils = bless {}, 'Qpsmtpd::Utils';
__tildeexp();
__is_localhost();
done_testing();
sub __is_localhost {
for my $local_ip (qw/ 127.0.0.1 ::1 2607:f060:b008:feed::127.0.0.1 127.0.0.2 /) {
ok( $utils->is_localhost($local_ip), "is_localhost, $local_ip");
}
for my $rem_ip (qw/ 128.0.0.1 ::2 2607:f060:b008:feed::128.0.0.1 /) {
ok( !$utils->is_localhost($rem_ip), "!is_localhost, $rem_ip");
}
};
sub __tildeexp {
my $path = $utils->tildeexp('~root/foo.txt');
ok( $path, "tildeexp, $path");
$path = $utils->tildeexp('no/tilde/in/path');
cmp_ok( $path, 'eq', 'no/tilde/in/path', 'tildeexp, no expansion');
};