diff options
author | Alexander Sulfrian <alex@spline.inf.fu-berlin.de> | 2016-06-21 02:42:25 +0200 |
---|---|---|
committer | Alexander Sulfrian <alex@spline.inf.fu-berlin.de> | 2016-06-21 02:42:25 +0200 |
commit | 339312c92a71a0943ae34aab27b1026bb3479d0d (patch) | |
tree | cea49efa50ec436b517a25dc1d1ff90f575e2f18 /t/srs.t | |
parent | d9ba41806745b830fd229b4cbcd8b5386203af27 (diff) | |
download | srs-339312c92a71a0943ae34aab27b1026bb3479d0d.tar.gz srs-339312c92a71a0943ae34aab27b1026bb3479d0d.tar.bz2 srs-339312c92a71a0943ae34aab27b1026bb3479d0d.zip |
Make subclasses of Net::Server
Spline::Socketmap is now a subclasses of Net::Server and
Spline::Socketmap::Srs is a subclass of Spline::Socketmap. This way the
Srs stuff is completly independent of the postfix-socketmap protocol
handling.
The config is located in the instances and configurable with Net::Server
command line options or config file.
Diffstat (limited to 't/srs.t')
-rw-r--r-- | t/srs.t | 184 |
1 files changed, 105 insertions, 79 deletions
@@ -1,30 +1,16 @@ use strict; use warnings; -use Test::More tests => 39; +use Test::More tests => 35; use Test::Exception; use Test::MockModule; BEGIN { use_ok 'Spline::Srs', qw( - config_set - config_get check_exclude - srs_forward - srs_reverse ) or BAIL_OUT; } -# Test config -my $old_value = config_get('hash_length'); -isnt($old_value, undef, 'Config value exists'); -config_set('hash_length', $old_value+1); -isnt(config_get('hash_length'), $old_value, 'Config changed'); - -is(config_get('invalid'), undef, 'Invalid config value is undef'); -config_set('invalid', 'test'); -is(config_get('invalid'), undef, 'Cannot set invalid config values'); - # Testing ignores is(check_exclude('test@example.com',['example.com']), 1, 'Ignore matching domain'); is(check_exclude('test@example.de', ['example.com']), 0, 'Do not ignore non-matching domain'); @@ -32,80 +18,120 @@ is(check_exclude('test@test.example.com', ['example.com']), 0, 'Do not ignore su is(check_exclude('test@test.example.com', ['.example.com']), 1, 'Ignore sub-domain if requested'); # Try without config -throws_ok { srs_forward('something@example.com') } qr/configure.*secret/, 'Secret required'; -config_set('secret', 'abc'); -throws_ok { srs_forward('something@example.com') } qr/configure.*secret/, 'Long secret required'; -config_set('secret', '12345678901234567890'); -throws_ok { srs_forward('something@example.com') } qr/configure.*alias/, 'Alias required'; -config_set('alias', 'something'); -lives_ok { srs_forward('something@example.com') } 'Config complete'; +my %config = (); +throws_ok { new Spline::Srs(\%config) } qr/configure.*secret/, 'Secret required'; +$config{secret} = 'abc'; +throws_ok { new Spline::Srs(\%config) } qr/configure.*secret/, 'Long secret required'; +$config{secret} = '12345678901234567890'; +throws_ok { new Spline::Srs(\%config) } qr/configure.*alias/, 'Alias required'; +$config{alias} = 'something'; +lives_ok { new Spline::Srs(\%config) } 'Config complete'; # Testing ignores with config -config_set('alias', 'domain.invalid'); -is(srs_forward('test@domain.invalid'), 'test@domain.invalid', 'Exclude alias domain by default'); -isnt(srs_forward('test@example.com'), 'test@example.com', 'Not excluding something other'); +$config{alias} = 'domain.invalid'; +{ + my $srs = new Spline::Srs(\%config); + is($srs->forward('test@domain.invalid'), 'test@domain.invalid', + 'Exclude alias domain by default'); + isnt($srs->forward('test@example.com'), 'test@example.com', + 'Not excluding something other'); +} -config_set('excludes', ['example.com']); -is(srs_forward('test@domain.invalid'), 'test@domain.invalid', 'Exclude alias domain by default'); -is(srs_forward('test@example.com'), 'test@example.com', 'Exclude is working'); -isnt(srs_forward('test@test.example.com'), 'test@test.example.com', 'Excluded domain should not match subdomain'); +$config{excludes} = ['example.com']; +{ + my $srs = new Spline::Srs(\%config); + is($srs->forward('test@domain.invalid'), 'test@domain.invalid', + 'Exclude alias domain by default'); + is($srs->forward('test@example.com'), 'test@example.com', + 'Exclude is working'); + isnt($srs->forward('test@test.example.com'), 'test@test.example.com', + 'Excluded domain should not match subdomain'); +} -config_set('excludes', ['.example.com']); -is(srs_forward('test@domain.invalid'), 'test@domain.invalid', 'Exclude alias domain by default'); -is(srs_forward('test@test.example.com'), 'test@test.example.com', 'Exclude subdomains'); -isnt(srs_forward('test@example.com'), 'test@example.com', 'Excluded subdomain should not match domain'); +$config{excludes} = ['.example.com']; +{ + my $srs = new Spline::Srs(\%config); + is($srs->forward('test@domain.invalid'), 'test@domain.invalid', + 'Exclude alias domain by default'); + is($srs->forward('test@test.example.com'), 'test@test.example.com', + 'Exclude subdomains'); + isnt($srs->forward('test@example.com'), 'test@example.com', + 'Excluded subdomain should not match domain'); +} -config_set('excludes', ['example.com', '.example.com']); -is(srs_forward('test@domain.invalid'), 'test@domain.invalid', 'Exclude alias domain by default'); -is(srs_forward('test@test.example.com'), 'test@test.example.com', 'Multiple excludes'); -is(srs_forward('test@example.com'), 'test@example.com', 'Multiple excludes'); +$config{excludes} = ['example.com', '.example.com']; +{ + my $srs = new Spline::Srs(\%config); + is($srs->forward('test@domain.invalid'), 'test@domain.invalid', + 'Exclude alias domain by default'); + is($srs->forward('test@test.example.com'), 'test@test.example.com', + 'Multiple excludes'); + is($srs->forward('test@example.com'), 'test@example.com', + 'Multiple excludes'); +} # SRS Forward -config_set('secret', '12345678901234567890'); -config_set('alias', 'domain.invalid'); -config_set('excludes', undef); -my $result = srs_forward('alex@example.com'); -isnt($result, undef, 'Not undef'); -isnt($result, 'alex@example.com', 'Not rewritten'); -like($result, qr/^SRS0[+=-]/, 'SRS0 Prefix'); -like($result, qr/\@domain\.invalid$/, 'Rewrite to alias-Domain'); - -# Check SRS forward an backwards -my $address = 'alex@example.com'; -my $srs_address = srs_forward($address); -isnt($srs_address, undef, 'Forward works'); -isnt($srs_address, $address, 'Forward works'); -is(srs_reverse($srs_address), $address, 'Reverse maps back to original'); - -# Some checks requires a fixed time for deterministic output +$config{secret} = '12345678901234567890'; +$config{alias} = 'domain.invalid'; +$config{excludes} = undef; +$config{hash_length} = 6; my ($old_srs_address, $broken); { - no warnings 'redefine'; - my $timestamp_create = \&Mail::SRS::timestamp_create; - local *Mail::SRS::timestamp_create = sub { - my $self = shift; - $timestamp_create->($self, 1); - }; - - $old_srs_address = srs_forward($address); - isnt($old_srs_address, $srs_address, 'Time should have changed'); - is($old_srs_address, 'SRS0=R0OuN1=AA=example.com=alex@domain.invalid', 'exakt match with fixed time'); - - # Hash with broken chars - $broken = srs_forward('nk@example.com'); - is($broken, 'SRS0=s/6g+P=AA=example.com=nk@domain.invalid', 'srs address is as expected'); + my $srs = new Spline::Srs(\%config); + my $result = $srs->forward('alex@example.com'); + isnt($result, undef, 'Not undef'); + isnt($result, 'alex@example.com', 'Not rewritten'); + like($result, qr/^SRS0[+=-]/, 'SRS0 Prefix'); + like($result, qr/\@domain\.invalid$/, 'Rewrite to alias-Domain'); + + # Check SRS forward an backwards + my $address = 'alex@example.com'; + my $srs_address = $srs->forward($address); + isnt($srs_address, undef, 'Forward works'); + isnt($srs_address, $address, 'Forward works'); + is($srs->reverse($srs_address), $address, 'Reverse maps back to original'); + + # Some checks requires a fixed time for deterministic output + { + no warnings 'redefine'; + my $timestamp_create = \&Mail::SRS::timestamp_create; + local *Mail::SRS::timestamp_create = sub { + my $self = shift; + $timestamp_create->($self, 1); + }; + + $old_srs_address = $srs->forward($address); + isnt($old_srs_address, $srs_address, 'Time should have changed'); + is($old_srs_address, 'SRS0=R0OuN1=AA=example.com=alex@domain.invalid', + 'exakt match with fixed time'); + + # Hash with broken chars + $broken = $srs->forward('nk@example.com'); + is($broken, 'SRS0=s/6g+P=AA=example.com=nk@domain.invalid', + 'srs address is as expected'); + } + + # Timestamp should timeout (but old timestamps can be ignored) + throws_ok { $srs->reverse($old_srs_address) } qr/Invalid timestamp/, + 'Timestamp should be to old'; } -# Timestamp should timeout (but old timestamps can be ignored) -throws_ok { srs_reverse($old_srs_address) } qr/Invalid timestamp/, 'Timestamp should be to old'; -config_set('ignore_time', 1); -lives_ok { srs_reverse($old_srs_address) } 'Ignore old timestamp'; - -# libsrs_alt uses different base64 chars and should also work -lives_ok { srs_reverse($broken) } 'Reverse with special chars'; -$broken = 'SRS0=s.6g_P=AA=example.com=nk@domain.invalid'; -throws_ok { srs_reverse($broken) } qr/Invalid hash/, 'Reverse with legacy chars'; -config_set('srsalt_fallback', 1); -is(srs_reverse($broken), 'nk@example.com', 'Reverse with legacy chars'); +$config{'ignore_time'} = 1; +{ + my $srs = new Spline::Srs(\%config); + lives_ok { $srs->reverse($old_srs_address) } 'Ignore old timestamp'; + + # libsrs_alt uses different base64 chars and should also work + lives_ok { $srs->reverse($broken) } 'Reverse with special chars'; + $broken = 'SRS0=s.6g_P=AA=example.com=nk@domain.invalid'; + throws_ok { $srs->reverse($broken) } qr/Invalid hash/, + 'Reverse with legacy chars'; +} + +$config{'srsalt_fallback'} = 1; +{ + my $srs = new Spline::Srs(\%config); + is($srs->reverse($broken), 'nk@example.com', 'Reverse with legacy chars'); +} # vim: set et ts=4: |