Mail-Send-DKIM

 view release on metacpan or  search on metacpan

lib/Mail/Send/DKIM.pm  view on Meta::CPAN

	my $selector = $self->{'selector'};
	return $selector if(defined($selector) && length($selector));

	# A named key file carries its selector in its name, which is the layout
	# the keys are kept in: <keydir>/<selector>.private.
	if(defined($self->{'keyfile'})) {
		($selector = basename($self->{'keyfile'})) =~ s/$KEYFILE_NAME_SUFFIX//;
		return $selector if(length($selector));
	}

	my $keys = $self->_private_keys();
	# undef, as the POD says: a bare return would be an empty list when the
	# result is used in a list, as in a hash of options.
	return undef unless(defined($keys)); ## no critic (Subroutines::ProhibitExplicitReturnUndef)

	unless(@{$keys} == 1) {
		$self->_say('keys_ambiguous', { 'keydir' => $self->{'keydir'}, 'count' => scalar(@{$keys}) });
		return undef; ## no critic (Subroutines::ProhibitExplicitReturnUndef)
	}

	($selector = $keys->[0]) =~ s/\Q$KEY_SUFFIX\E\z//;

	return $selector;
}

# Purpose:        List the keys in the key directory.
# Entry criteria: $self->{keydir} is set.
# Exit status:    An array reference of the names of the files ending .private, sorted;
#                 undef, after a warning, when the directory cannot be read.
# Side effects:   Reads a directory; may warn.
sub _private_keys :Private
{
	my $self = shift;

	# An unreadable directory is a warning and an unsigned message, so autodie
	# stands aside.
	no autodie qw(opendir closedir);

	my $keys;
	if(opendir(my $dh, $self->{'keydir'})) {
		# A key is a file with a name before the suffix: a directory called

t/function.t  view on Meta::CPAN

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 {

t/headers.t  view on Meta::CPAN

			"$private cannot be called from outside";
	}
	throws_ok { $dkim->_say('syslog_failed', { 'error' => 'x' }) } qr/protected method/, '_say cannot be called from outside';
	throws_ok { $dkim->_sign_for('example.org', 'x') } qr/private/, 'nor can a private method';

	# A subclass may use the protected method; it still cannot use the private ones.
	{
		package Local::Subclass;
		our @ISA = ('Mail::Send::DKIM');
		sub shout { my $self = shift; return $self->_say('syslog_failed', { 'error' => 'from a subclass' }) }
		sub peek { my $self = shift; return $self->_private_keys() }
	}
	my @warnings;
	local $SIG{'__WARN__'} = sub { push(@warnings, @_) };
	my $sub = Local::Subclass->new();
	is($sub->shout(), $sub, 'a subclass may say things, and the call chains');
	like($warnings[0], qr/from a subclass/, 'and the warning is the catalogue\'s');
	throws_ok { $sub->peek() } qr/private/, 'but not use a private method';
};

subtest 'a sendmail that refuses the message without reading it is a status, not a death' => sub {



( run in 3.288 seconds using v1.01-cache-2.11-cpan-8379b9b8ec2 )