Merge pull request #232 from jaredj/mock-not-fake

Change fake_{config,hook} to mock_{config,hook}
This commit is contained in:
Matt Simerson 2015-02-12 14:28:56 -08:00
commit f7558f346d
8 changed files with 51 additions and 51 deletions

View File

@ -126,65 +126,65 @@ sub run_plugin_tests {
$Test->done_testing(); $Test->done_testing();
} }
sub fake_hook { sub mock_hook {
########################################################################### ###########################################################################
# Inserts a given subroutine into the beginning of the set of hooks already # Inserts a given subroutine into the beginning of the set of hooks already
# in place. Used to test code against different potential plugins it will # in place. Used to test code against different potential plugins it will
# interact with. For example, to test behavior against various results of # interact with. For example, to test behavior against various results of
# the data_post hook: # the data_post hook:
# #
# $self->fake_hook('data_post',sub { return DECLINED }; # $self->mock_hook('data_post',sub { return DECLINED };
# ok(...); # ok(...);
# $self->fake_hook('data_post',sub { return DENYSOFT }; # $self->mock_hook('data_post',sub { return DENYSOFT };
# ok(...); # ok(...);
# $self->fake_hook('data_post',sub { return DENY }; # $self->mock_hook('data_post',sub { return DENY };
# ok(...); # ok(...);
# $self->fake_hook('data_post',sub { return DENY_DISCONNECT }; # $self->mock_hook('data_post',sub { return DENY_DISCONNECT };
# ok(...); # ok(...);
# $self->unfake_hook('data_post'); # $self->unmock_hook('data_post');
########################################################################### ###########################################################################
my ( $self, $hook, $sub ) = @_; my ( $self, $hook, $sub ) = @_;
unshift @{ $self->hooks->{$hook} ||= [] }, unshift @{ $self->hooks->{$hook} ||= [] },
{ {
name => '___FakeHook___', name => '___MockHook___',
code => $sub, code => $sub,
}; };
} }
sub unfake_hook { sub unmock_hook {
my ( $self, $hook ) = @_; my ( $self, $hook ) = @_;
$self->hooks->{$hook} = [ $self->hooks->{$hook} = [
grep { $_->{name} ne '___FakeHook___' } grep { $_->{name} ne '___MockHook___' }
@{ $self->hooks->{$hook} || [] } @{ $self->hooks->{$hook} || [] }
]; ];
} }
sub fake_config { sub mock_config {
#################################################################### ####################################################################
# Used to test code against various possible configurations # Used to test code against various possible configurations
# For example, to test against various possible config('me') values: # For example, to test against various possible config('me') values:
# #
# $self->fake_config( me => '***invalid***' ); # $self->mock_config( me => '***invalid***' );
# ok(...); # ok(...);
# $self->fake_config( me => 'valid-nonfqdn' ); # $self->mock_config( me => 'valid-nonfqdn' );
# ok(...); # ok(...);
# $self->fake_config( me => 'valid-fqdn.com'); # $self->mock_config( me => 'valid-fqdn.com');
# ok(...); # ok(...);
# $self->unfake_config(); # $self->unmock_config();
#################################################################### ####################################################################
my $self = shift; my $self = shift;
my $fake_config = {@_}; my $mock_config = {@_};
$self->fake_hook( 'config', $self->mock_hook( 'config',
sub { sub {
my ( $self, $txn, $conf ) = @_; my ( $self, $txn, $conf ) = @_;
return DECLINED if ! exists $fake_config->{$conf}; return DECLINED if ! exists $mock_config->{$conf};
return OK, $fake_config->{$conf}; return OK, $mock_config->{$conf};
} ); } );
} }
sub unfake_config { sub unmock_config {
my ( $self ) = @_; my ( $self ) = @_;
$self->unfake_hook('config'); $self->unmock_hook('config');
} }
1; 1;

View File

@ -84,9 +84,9 @@ sub validate_password {
return $deny, "$file - wrong password"; return $deny, "$file - wrong password";
} }
sub fake_hook { shift->qp->fake_hook(@_) } sub mock_hook { shift->qp->mock_hook(@_) }
sub unfake_hook { shift->qp->unfake_hook(@_) } sub unmock_hook { shift->qp->unmock_hook(@_) }
sub fake_config { shift->qp->fake_config(@_) } sub mock_config { shift->qp->mock_config(@_) }
sub unfake_config { shift->qp->unfake_config(@_) } sub unmock_config { shift->qp->unmock_config(@_) }
1; 1;

View File

@ -16,11 +16,11 @@ sub test_content_log_file {
is( $self->content_log_file, is( $self->content_log_file,
strftime('mail/%Y%m%d', localtime(time)), strftime('mail/%Y%m%d', localtime(time)),
'content_log_file() returns the right default value' ); 'content_log_file() returns the right default value' );
$self->fake_config( content_log_file => '/path/to/%Y%m%d%H%M' ); $self->mock_config( content_log_file => '/path/to/%Y%m%d%H%M' );
is( $self->content_log_file, is( $self->content_log_file,
strftime('/path/to/%Y%m%d%H%M', localtime(time)), strftime('/path/to/%Y%m%d%H%M', localtime(time)),
'content_log_file config changes content_log_file() output' ); 'content_log_file config changes content_log_file() output' );
$self->unfake_config; $self->unmock_config;
} }
sub test_content_log_enabled { sub test_content_log_enabled {
@ -69,10 +69,10 @@ sub test_content_log_enabled {
. ( defined $_->{config } ? "'$_->{config }'" : 'undef' ) . ( defined $_->{config } ? "'$_->{config }'" : 'undef' )
. ( $_->{expected} ? ' enables' : ' disables' ) . ( $_->{expected} ? ' enables' : ' disables' )
. ' content logging'; . ' content logging';
$self->fake_config( content_log_enabled => $_->{config } ); $self->mock_config( content_log_enabled => $_->{config } );
is( $self->content_log_enabled, $_->{expected}, $descr ); is( $self->content_log_enabled, $_->{expected}, $descr );
} }
$self->unfake_config; $self->unmock_config;
} }
sub test_exclude { sub test_exclude {

View File

@ -66,7 +66,7 @@ sub test_register_genre_blocking {
'rcpt_handler not registered when no blocked genres are configured' ); 'rcpt_handler not registered when no blocked genres are configured' );
$self->register_hook(); $self->register_hook();
$self->fake_config( p0f_blocked_operating_systems => 'Crapple Macintawsh' ); $self->mock_config( p0f_blocked_operating_systems => 'Crapple Macintawsh' );
$self->register_genre_blocking(); $self->register_genre_blocking();
is( $self->{_lastreg}, 'rcpt,rcpt_handler', is( $self->{_lastreg}, 'rcpt,rcpt_handler',
'rcpt_handler registered when blocked genre phrases are configured' ); 'rcpt_handler registered when blocked genre phrases are configured' );
@ -76,7 +76,7 @@ sub test_register_genre_blocking {
'no blocked OS regexes' ); 'no blocked OS regexes' );
$self->register_hook(); $self->register_hook();
$self->fake_config( p0f_blocked_operating_systems => '/windoze/' ); $self->mock_config( p0f_blocked_operating_systems => '/windoze/' );
$self->register_genre_blocking(); $self->register_genre_blocking();
is( $self->{_lastreg}, 'rcpt,rcpt_handler', is( $self->{_lastreg}, 'rcpt,rcpt_handler',
'rcpt_handler registered when blocked genre regexes are configured' ); 'rcpt_handler registered when blocked genre regexes are configured' );
@ -85,7 +85,7 @@ sub test_register_genre_blocking {
is( join('|', @{ delete $self->{os_block_re} || [] }), qr/windoze/i, is( join('|', @{ delete $self->{os_block_re} || [] }), qr/windoze/i,
'blocked OS regexes processed correctly' ); 'blocked OS regexes processed correctly' );
$self->unfake_config; $self->unmock_config;
} }
sub test_rcpt_handler { sub test_rcpt_handler {

View File

@ -176,11 +176,11 @@ sub __config {
}, },
); );
for (@test_data) { for (@test_data) {
$qp->fake_hook( 'user_config', sub { return @{$_->{result}} } ) $qp->mock_hook( 'user_config', sub { return @{$_->{result}} } )
if $_->{result}; if $_->{result};
is($sender->config($_->{pref}), $_->{expected}, $_->{descr}); is($sender->config($_->{pref}), $_->{expected}, $_->{descr});
} }
$qp->unfake_hook('user_config'); $qp->unmock_hook('user_config');
} }
sub __canonify { sub __canonify {

View File

@ -22,7 +22,7 @@ sub __db {
is( $db->{name}, 'testfoo', 'accepts name argument' ); is( $db->{name}, 'testfoo', 'accepts name argument' );
delete $plugin->{db}; delete $plugin->{db};
$db = $plugin->db( class => 'FakeDB' ); $db = $plugin->db( class => 'FakeDB' );
is( $db->{name}, '___FakeHook___', 'db name defaults to plugin name' ); is( $db->{name}, '___MockHook___', 'db name defaults to plugin name' );
} }
sub __register_hook { sub __register_hook {
@ -30,25 +30,25 @@ sub __register_hook {
my $plugin = FakePlugin->new; my $plugin = FakePlugin->new;
$plugin->register_hook('bogus_hook'); $plugin->register_hook('bogus_hook');
}; };
ok( $@ =~ /^___FakeHook___: Invalid hook: bogus_hook/, ok( $@ =~ /^___MockHook___: Invalid hook: bogus_hook/,
'register_hook() validates hook name' ); 'register_hook() validates hook name' );
my $qp = Test::Qpsmtpd->new; my $qp = Test::Qpsmtpd->new;
my $plugin = FakePlugin->new; my $plugin = FakePlugin->new;
$plugin->{_qp} = $qp; $plugin->{_qp} = $qp;
$plugin->register_hook('logging', sub { shift; return "arguments:@_" }); $plugin->register_hook('logging', sub { shift; return "arguments:@_" });
ok( my $registered = $qp->hooks->{logging}->[-1] ); ok( my $registered = $qp->hooks->{logging}->[-1] );
is( $registered->{name}, '___FakeHook___', is( $registered->{name}, '___MockHook___',
'register_hook() sets plugin name' ); 'register_hook() sets plugin name' );
my $code = $registered->{code}; my $code = $registered->{code};
is( ref $code, 'CODE', 'register_hook() sets a coderef' ); is( ref $code, 'CODE', 'register_hook() sets a coderef' );
is( join('',$code->(undef,qw[arg1 arg2])), 'arguments:arg1 arg2', is( join('',$code->(undef,qw[arg1 arg2])), 'arguments:arg1 arg2',
'register_hook(): coderef set correctly' ); 'register_hook(): coderef set correctly' );
$qp->unfake_hook('logging'); $qp->unmock_hook('logging');
} }
package FakePlugin; package FakePlugin;
use parent 'Qpsmtpd::Plugin'; use parent 'Qpsmtpd::Plugin';
sub plugin_name { '___FakeHook___' } sub plugin_name { '___MockHook___' }
package FakeDB; package FakeDB;
sub new { sub new {

View File

@ -158,8 +158,8 @@ sub __data_respond {
# data_respond also runs the data_post hooks, so this will require a bit # data_respond also runs the data_post hooks, so this will require a bit
# more work to get under test. we also don't yet have a way to mock # more work to get under test. we also don't yet have a way to mock
# message data; that will probably require overriding getline() # message data; that will probably require overriding getline()
#$smtpd->fake_data( _test_message() ); #$smtpd->mock_data( _test_message() );
#$smtpd->fake_hook( data_post => sub { return DECLINED } ); #$smtpd->mock_hook( data_post => sub { return DECLINED } );
#is( $smtpd->data_respond(DECLINED), 1, 'data_respond, DECLINED' ); #is( $smtpd->data_respond(DECLINED), 1, 'data_respond, DECLINED' );
} }

View File

@ -111,13 +111,13 @@ sub __run_continuation {
my @local_hooks = @{ $smtpd->hooks->{connect} }; my @local_hooks = @{ $smtpd->hooks->{connect} };
$smtpd->{_continuation} = ['connect', [DECLINED, "test mess"], @local_hooks]; $smtpd->{_continuation} = ['connect', [DECLINED, "test mess"], @local_hooks];
$smtpd->fake_config( 'smtpgreeting', 'Hello there ESMTP' ); $smtpd->mock_config( 'smtpgreeting', 'Hello there ESMTP' );
eval { $r = $smtpd->run_continuation }; eval { $r = $smtpd->run_continuation };
ok(!$@, "run_continuation with a continuation doesn't throw exception"); ok(!$@, "run_continuation with a continuation doesn't throw exception");
is($r->[0], 220, "hook_responder, code"); is($r->[0], 220, "hook_responder, code");
is($r->[1], 'Hello there ESMTP', "hook_responder, message"); is($r->[1], 'Hello there ESMTP', "hook_responder, message");
$smtpd->unfake_config; $smtpd->unmock_config;
my @test_data = ( my @test_data = (
{ {
@ -154,21 +154,21 @@ sub __run_continuation {
hooks => [ [123456,undef], [DENY, 'goaway'] ], hooks => [ [123456,undef], [DENY, 'goaway'] ],
expected_response => '550/goaway', expected_response => '550/goaway',
disconnected => 0, disconnected => 0,
logged => 'LOGERROR:Plugin ___FakeHook___, hook helo returned 123456', logged => 'LOGERROR:Plugin ___MockHook___, hook helo returned 123456',
descr => 'INVALID -> DENY', descr => 'INVALID -> DENY',
}, },
{ {
hooks => [ sub { die "dead\n" }, [DENY, 'begone'] ], hooks => [ sub { die "dead\n" }, [DENY, 'begone'] ],
expected_response => '550/begone', expected_response => '550/begone',
disconnected => 0, disconnected => 0,
logged => 'LOGCRIT:FATAL PLUGIN ERROR [___FakeHook___]: dead', logged => 'LOGCRIT:FATAL PLUGIN ERROR [___MockHook___]: dead',
descr => 'fatal error -> DENY', descr => 'fatal error -> DENY',
}, },
{ {
hooks => [ [undef], [DENY, 'nm'] ], hooks => [ [undef], [DENY, 'nm'] ],
expected_response => '550/nm', expected_response => '550/nm',
disconnected => 0, disconnected => 0,
logged => 'LOGERROR:Plugin ___FakeHook___, hook helo returned undef!', logged => 'LOGERROR:Plugin ___MockHook___, hook helo returned undef!',
descr => 'undef -> DENY', descr => 'undef -> DENY',
}, },
{ {
@ -180,10 +180,10 @@ sub __run_continuation {
}, },
); );
for my $t (@test_data) { for my $t (@test_data) {
$smtpd->fake_config( me => 'fqdn' ); $smtpd->mock_config( me => 'fqdn' );
for my $h ( reverse @{ $t->{hooks} } ) { for my $h ( reverse @{ $t->{hooks} } ) {
my $sub = ( ref $h eq 'ARRAY' ? sub { return @$h } : $h ); my $sub = ( ref $h eq 'ARRAY' ? sub { return @$h } : $h );
$smtpd->fake_hook( 'helo', $sub ); $smtpd->mock_hook( 'helo', $sub );
} }
$smtpd->{_continuation} = [ 'helo', ['somearg'], @{ $smtpd->hooks->{helo} } ]; $smtpd->{_continuation} = [ 'helo', ['somearg'], @{ $smtpd->hooks->{helo} } ];
delete $smtpd->{_response}; delete $smtpd->{_response};
@ -205,8 +205,8 @@ sub __run_continuation {
is( join("\n", @{ $smtpd->{_logged} || [] }), $t->{logged}, is( join("\n", @{ $smtpd->{_logged} || [] }), $t->{logged},
"run_continuation() logging on $t->{descr}" ); "run_continuation() logging on $t->{descr}" );
} }
$smtpd->unfake_hook('helo'); $smtpd->unmock_hook('helo');
$smtpd->unfake_config(); $smtpd->unmock_config();
} }
} }
@ -431,7 +431,7 @@ sub __config {
); );
for my $t (@test_data) { for my $t (@test_data) {
for my $hook ( grep { $t->{hooks}{$_} } qw( config user_config ) ) { for my $hook ( grep { $t->{hooks}{$_} } qw( config user_config ) ) {
$qp->fake_hook( $hook, sub { return @{ $t->{hooks}{$hook} } } ); $qp->mock_hook( $hook, sub { return @{ $t->{hooks}{$hook} } } );
} }
is( is(
$qp->config($t->{pref}, $a), $qp->config($t->{pref}, $a),
@ -442,7 +442,7 @@ sub __config {
$t->{expected}{global}, $t->{expected}{global},
"Global config: $t->{descr}"); "Global config: $t->{descr}");
} }
$qp->unfake_hook($_) for qw( config user_config ); $qp->unmock_hook($_) for qw( config user_config );
} }
1; 1;