Mail-Send-DKIM
view release on metacpan or search on metacpan
t/function.t view on Meta::CPAN
local $SIG{'__WARN__'} = sub { push(@warnings, @_) };
my @found = Mail::Send::DKIM::_parse_addresses('some value');
is_deeply(\@given, [['Mail::Address', 'some value']], 'the value goes to Mail::Address->parse');
is(scalar(@found), 1, 'and what it found comes back as a list');
is_deeply(\@warnings, [], 'FAIL: its warning was not passed on');
is(ref($SIG{'__WARN__'}), 'CODE', 'the caller\'s warning handler is back afterwards');
};
# ---------------------------------------------------------------------------
# from_domain
# ---------------------------------------------------------------------------
scenario 'from_domain: the lower-case host of the first author address' => sub {
my ($from, $addresses) = ('x', []);
mock 'Mail::Send::DKIM::_parse_headers' => sub { return defined($from) ? { 'from' => $from } : {} };
mock 'Mail::Address::parse' => sub { return @{$addresses} };
$addresses = [Local::FakeAddress->new('host' => 'Example.ORG'), Local::FakeAddress->new('host' => 'second.org')];
is(Mail::Send::DKIM::from_domain('m'), 'example.org', 'lower case, and the first address');
is(Mail::Send::DKIM->from_domain('m'), 'example.org', 'as a class method');
is(Mail::Send::DKIM::from_domain(new_dkim(), 'm'), 'example.org', 'as an object method');
returns_ok(Mail::Send::DKIM::from_domain('m'), { 'type' => 'string', 'matches' => qr/\A[a-z0-9.-]+\z/ }, 'a host name');
verbose('from_domain was given the header: ' . $from);
};
scenario 'from_domain: FAIL: no usable domain' => sub {
my ($from, $addresses) = ('x', []);
mock 'Mail::Send::DKIM::_parse_headers' => sub { return defined($from) ? { 'from' => $from } : {} };
mock 'Mail::Address::parse' => sub { return @{$addresses} };
$from = undef;
is(Mail::Send::DKIM::from_domain('m'), undef, 'no From: header: undef');
$from = 'x';
$addresses = [];
is(Mail::Send::DKIM::from_domain('m'), undef, 'the parser finds no address: undef');
foreach my $host (undef, '', 'exa_mple.org', 'a b.org', "example.org\n", "example.org\0", 'exa/mple', 'b' . "\x{fc}" . 'cher.example', '-', 'a@b') {
$addresses = [Local::FakeAddress->new('host' => $host)];
my $shown = defined($host) ? $host : 'undef';
$shown =~ s/([^ -~])/sprintf('\\x%02x', ord($1))/ge;
my $got = Mail::Send::DKIM::from_domain('m');
ok(!defined($got) || $got =~ /\A[a-z0-9.-]+\z/, "'$shown' is not passed on as it is");
}
$addresses = [Local::FakeAddress->new('host' => "example.org\n")];
is(Mail::Send::DKIM::from_domain('m'), undef, 'a trailing newline does not slip past the check');
};
scenario 'from_domain: FAIL: arguments that are not a message' => sub {
throws_ok { Mail::Send::DKIM::from_domain([]) } qr/message.*must be a string/, 'a reference is refused, naming the parameter';
is(Mail::Send::DKIM::from_domain(undef), undef, 'undef has no From:');
is(Mail::Send::DKIM::from_domain(), undef, 'neither has no argument at all');
is(Mail::Send::DKIM::from_domain("From: evil\@example.org\0\@good.org\n\n"), undef, 'a NUL in the address: no domain is trusted from it');
is(Mail::Send::DKIM::from_domain("From: a\@example.org\x1b\n\n"), undef, 'nor an escape character');
is(Mail::Send::DKIM::from_domain("From:\ta\@example.org\n\n"), 'example.org', 'but a tab is ordinary white space');
};
# ---------------------------------------------------------------------------
# selector and _private_keys
# ---------------------------------------------------------------------------
scenario 'selector: the order in which a selector is chosen' => sub {
my ($keys, $said) = ([], capture_say());
mock 'Mail::Send::DKIM::_private_keys' => sub { return $keys };
is(new_dkim('selector' => 'given', 'keyfile' => '/k/other.private')->selector(), 'given', 'a selector that was given');
is(new_dkim('keyfile' => '/etc/dkim/2026.private')->selector(), '2026', 'else the name of the key file');
is(new_dkim('keyfile' => '/k/name.key')->selector(), 'name', '.key is not part of the name');
is(new_dkim('keyfile' => '/k/name.pem')->selector(), 'name', 'nor .pem');
is(new_dkim('keyfile' => '/k/plain')->selector(), 'plain', 'a name with no suffix is the name');
$keys = ['only.private'];
is(new_dkim()->selector(), 'only', 'else the only key in the directory');
returns_ok(new_dkim()->selector(), { 'type' => 'string', 'optional' => 1 }, 'a string');
is(scalar(@{$said}), 0, 'nothing was said');
};
scenario 'selector: FAIL: nothing to choose, or too much' => sub {
my ($keys, $said) = (undef, capture_say());
mock 'Mail::Send::DKIM::_private_keys' => sub { return $keys };
my $dkim = new_dkim('keydir' => '/the/dir');
is($dkim->selector(), undef, 'an unreadable directory: undef');
is(scalar(@{$said}), 0, 'with nothing more said (_private_keys said why)');
$keys = [];
is($dkim->selector(), undef, 'no keys: undef');
is_deeply($said->[-1], ['keys_ambiguous', { 'keydir' => '/the/dir', 'count' => 0 }], 'with the count of 0');
$keys = ['a.private', 'b.private'];
is($dkim->selector(), undef, 'two keys: undef');
is($said->[-1][1]{'count'}, 2, 'with the count of 2');
is(new_dkim('selector' => '')->selector(), undef, 'an empty selector is no selector');
foreach my $keyfile ('/k/.private', '/k/.key', '/', '') {
my $got = new_dkim('keyfile' => $keyfile)->selector();
ok(!defined($got) || length($got), "a key file '$keyfile' never gives an empty selector");
}
is(new_dkim('keyfile' => '/k/dir/')->selector(), 'dir', 'a trailing slash is not part of the name');
};
scenario '_private_keys: the key files of a directory, sorted' => sub {
my $dir = File::Spec->catdir($TMP, 'pk-ok');
mkdir($dir);
write_file(File::Spec->catfile($dir, $_), 'k') foreach qw(b.private a.private notes.txt);
my $said = capture_say();
my $keys = new_dkim('keydir' => $dir)->_private_keys();
is_deeply($keys, ['a.private', 'b.private'], 'only the .private files, in order');
returns_ok($keys, { 'type' => 'arrayref' }, 'an array reference');
is(scalar(@{$said}), 0, 'nothing was said');
memory_cycle_ok($keys, 'no cycle');
};
scenario '_private_keys: FAIL: directories that are missing, wrong, or full of tricks' => sub {
my $said = capture_say();
my $missing = File::Spec->catdir($TMP, 'pk-missing');
is(new_dkim('keydir' => $missing)->_private_keys(), undef, 'a missing directory: undef');
is($said->[-1][0], 'keydir_unreadable', 'with the keydir_unreadable message');
is($said->[-1][1]{'keydir'}, $missing, 'naming the directory');
{
local $! = ENOENT;
is($said->[-1][1]{'os_error'}, "$!", 'and the operating system\'s words');
}
my $file = write_file(File::Spec->catfile($TMP, 'pk-a-file'), 'x');
is(new_dkim('keydir' => $file)->_private_keys(), undef, 'a file where a directory should be: undef');
{
local $! = ENOTDIR;
is($said->[-1][1]{'os_error'}, "$!", 'with the matching words');
}
SKIP: {
skip 'root can read anything', 1 if($> == 0);
my $locked = File::Spec->catdir($TMP, 'pk-locked');
mkdir($locked, $CONFIG{'MODE_NONE'});
is(new_dkim('keydir' => $locked)->_private_keys(), undef, 'a directory that may not be read: undef');
chmod($CONFIG{'MODE_RUNNABLE'}, $locked);
}
my $tricks = File::Spec->catdir($TMP, 'pk-tricks');
mkdir($tricks);
mkdir(File::Spec->catdir($tricks, 'dir.private'));
write_file(File::Spec->catfile($tricks, '.private'), 'k');
write_file(File::Spec->catfile($tricks, 'real.private.bak'), 'k');
write_file(File::Spec->catfile($tricks, 'real.PRIVATE'), 'k');
write_file(File::Spec->catfile($tricks, "new\nline.private"), 'k');
is_deeply(new_dkim('keydir' => $tricks)->_private_keys(), ["new\nline.private"], 'a directory, a bare .private, and near misses are not keys');
my $crowd = File::Spec->catdir($TMP, 'pk-crowd');
mkdir($crowd);
write_file(File::Spec->catfile($crowd, sprintf('k%05d.private', $_)), '') foreach (1 .. $CONFIG{'MANY'});
my $start = time();
my $keys = new_dkim('keydir' => $crowd)->_private_keys();
is(scalar(@{$keys}), $CONFIG{'MANY'}, 'thousands of keys are listed');
is($keys->[0], 'k00001.private', 'in order');
cmp_ok(time() - $start, '<', $CONFIG{'SLOW_SECONDS'}, 'quickly');
SKIP: {
skip 'no /proc/self/fd to count descriptors with', 1 unless(-d '/proc/self/fd');
my $before = () = glob('/proc/self/fd/*');
new_dkim('keydir' => $crowd)->_private_keys() for (1 .. 20);
new_dkim('keydir' => $missing)->_private_keys() for (1 .. 20);
my $after = () = glob('/proc/self/fd/*');
is($after, $before, 'no directory handle is left open');
}
};
# ---------------------------------------------------------------------------
# i18n, _choose_format, _interpolate
# ---------------------------------------------------------------------------
scenario 'i18n: picks the format, then fills it in' => sub {
my @calls;
mock 'Mail::Send::DKIM::_choose_format' => sub { push(@calls, ['choose', @_]); return 'FORMAT' };
mock 'Mail::Send::DKIM::_interpolate' => sub { my ($self, $key, $format, $args) = @_; push(@calls, ['interpolate', $key, $format, $args]); return 'TEXT' };
my $dkim = new_dkim('messages' => { 'mine' => 'my entry' });
my $args = { 'a' => 1 };
is($dkim->i18n('mine', $args), 'TEXT', 'the filled-in text');
is($calls[0][1], 'my entry', 'the entry of the object\'s own catalogue was chosen from');
is($calls[0][2], $args, 'with the arguments');
is_deeply($calls[1], ['interpolate', 'mine', 'FORMAT', $args], 'and what was chosen is what is filled in');
$dkim->i18n('syslog_failed', { 'error' => 'x' });
like($calls[2][1], qr/cannot write to syslog/, 'a message the object does not override comes from the default catalogue');
returns_ok($dkim->i18n('mine'), { 'type' => 'string' }, 'a string');
};
scenario 'i18n: FAIL: a message that is not there, or arguments that are not right' => sub {
my $dkim = new_dkim();
throws_ok { $dkim->i18n('no_such_key') } qr/\AMail::Send::DKIM: no message is defined for the key "no_such_key" at /, 'FAIL: an unknown key, with exactly this text';
throws_ok { $dkim->i18n(undef) } qr/no message is defined for the key ""/, 'FAIL: an undefined key is an unknown key, not a Perl warning';
throws_ok { $dkim->i18n("a\nb") } qr/no message is defined for the key "a\nb"/, 'FAIL: a key with a newline is quoted, not interpreted';
throws_ok { $dkim->i18n('__proto__') } qr/no message is defined/, 'FAIL: a key that looks like a prototype';
throws_ok { $dkim->i18n('syslog_failed', []) } qr/\AMail::Send::DKIM: the arguments of a message must be a hash reference, not ARRAY at /, 'FAIL: arguments that are an array';
throws_ok { $dkim->i18n('syslog_failed', 'text') } qr/not a plain value at /, 'FAIL: arguments that are a string';
throws_ok { $dkim->i18n('syslog_failed', {}) } qr/\AMail::Send::DKIM: the message "syslog_failed" needs the argument "error" at /, 'FAIL: a missing argument, named';
my $blanked = new_dkim('messages' => { 'unknown_message' => undef, 'bad_arguments' => undef });
throws_ok { $blanked->i18n('no_such_key') } qr/no message is defined/, 'FAIL: a catalogue cannot blank the message about missing messages';
throws_ok { $blanked->i18n('syslog_failed', []) } qr/must be a hash reference/, 'FAIL: nor the one about bad arguments';
};
scenario '_choose_format: a string stays, a hash is chosen from' => sub {
is(Mail::Send::DKIM::_choose_format('plain', {}), 'plain', 'a string is the format');
my $entry = { 'zero' => 'Z', 'one' => 'O', 'other' => 'M', 'f' => 'F', 'f.one' => 'F1', 'f.other' => 'FM' };
my @table = (
[{ 'count' => 0 }, 'Z'], [{ 'count' => 1 }, 'O'], [{ 'count' => 2 }, 'M'], [{ 'count' => -1 }, 'M'], [{ 'count' => 1.5 }, 'M'],
[{ 'count' => '1' }, 'O'], [{ 'count' => '0.0' }, 'Z'], [{}, 'M'],
[{ 'variant' => 'f' }, 'F'], [{ 'variant' => 'f', 'count' => 1 }, 'F1'], [{ 'variant' => 'f', 'count' => 5 }, 'FM'],
[{ 'variant' => 'f', 'count' => 0 }, 'F'], [{ 'variant' => 'm', 'count' => 1 }, 'O'],
);
foreach my $row (@table) {
is(Mail::Send::DKIM::_choose_format($entry, $row->[0]), $row->[1], join(',', map { "$_=$row->[0]{$_}" } sort keys %{ $row->[0] }) || 'no arguments');
}
returns_ok(Mail::Send::DKIM::_choose_format($entry, {}), { 'type' => 'string' }, 'a string');
};
scenario '_choose_format: FAIL: counts and entries that make no sense' => sub {
my @warnings;
( run in 1.403 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )