Mail-Send-DKIM

 view release on metacpan or  search on metacpan

t/headers.t  view on Meta::CPAN

{
	my ($message, @args) = @_;

	my @lines;
	no warnings 'redefine';
	local *Sys::Syslog::openlog = sub { };
	local *Sys::Syslog::syslog = sub { push(@lines, $_[-1]) };
	local *Sys::Syslog::closelog = sub { };

	$dkim->log_send($message, @args);

	return $lines[0];
}

subtest 'log_send understands the options sendmail takes' => sub {
	my $message = "From: Me <me\@example.org>\nTo: \"Doe, Jane\" <jane\@example.net>, bob\@example.net\nCc: cc\@example.net\nBcc: bcc\@example.net\nSubject: Hi\n\nb\n";

	like(logged_line($message, '-t'), qr/recipient=jane\@example\.net,bob\@example\.net,cc\@example\.net,bcc\@example\.net /,
		'-t reads To, Cc and Bcc, and a quoted comma does not split an address');
	like(logged_line($message, '-ti'), qr/recipient=jane\@/, 't in a cluster of flags still means -t');
	like(logged_line($message, '-it'), qr/recipient=jane\@/, 'in either order');
	like(logged_line($message, '-f', 'bounce@example.org', 'a@example.net'), qr/\Asender=bounce\@example\.org recipient=a\@example\.net /,
		'-f with its value as the next argument');
	like(logged_line($message, '-fbounce@example.org', 'a@example.net'), qr/\Asender=bounce\@example\.org /, '-f with its value attached');
	like(logged_line($message, '-f', '', 'a@example.net'), qr/\Asender=- /, 'an empty -f is a null sender, not the From: address');
	like(logged_line($message, '-oi', '-odq', 'a@example.net'), qr/recipient=a\@example\.net /,
		'-oi and -odq are options, not recipients');
	like(logged_line($message, '-o', 'i', 'a@example.net'), qr/recipient=a\@example\.net /, 'the value of -o is not a recipient');
	like(logged_line($message, '--', 'a@example.net'), qr/recipient=a\@example\.net /, '-- is not a recipient');
	like(logged_line($message, '-FTest Nigel', 'a@example.net'), qr/recipient=a\@example\.net /,
		'the value of -F is not a recipient');
	unlike(logged_line($message, '-FNigelTest', 'a@example.net'), qr/jane/,
		'and a t inside the value of -F does not turn on -t');
	unlike(logged_line($message, '-bt', 'a@example.net'), qr/jane/, '-bt is sendmail\'s address test, not -t');
	like(logged_line("From: a\@example.org\nSubject: one\x01two\n\nb\n", 'r@example.net'), qr/subject=one two\z/,
		'a control character in the subject is a space');
	like(logged_line("From: a\@example.org\n\nb\n", '-t'), qr/\Asender=a\@example\.org recipient=- subject=-\z/,
		'what is missing is -');
};

subtest 'the helpers are private, and only the message writer may be extended' => sub {
	# Under a test harness the two modules let every call through, so that
	# white-box tests can reach the helpers; this subtest is about the
	# enforcement itself, so it asks for it back.
	no warnings 'once';
	local $Sub::Private::config{'harness_bypass'} = 0;
	local $Sub::Protected::config{'harness_bypass'} = 0;

	foreach my $private (qw(_local _parse_headers _addresses _domain_set _parse_sendmail_args _trim_error _choose_format)) {
		throws_ok { no strict 'refs'; &{"Mail::Send::DKIM::$private"}('x') } qr/private subroutine of Mail::Send::DKIM/,
			"$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 {
	my $dir = tempdir('CLEANUP' => 1);
	my $refuser = File::Spec->catfile($dir, 'refuse.sh');
	open(my $fh, '>', $refuser) or BAIL_OUT("Cannot write $refuser: $!");
	print $fh "#!/bin/sh\nexit 75\n";
	close($fh);
	chmod(0755, $refuser) or BAIL_OUT("Cannot chmod $refuser: $!");

	# Larger than any pipe buffer, so the write meets the closed pipe.
	my $big = ('x' x 79) . "\n";
	$big x= 100_000;

	is(Mail::Send::DKIM->new('sendmail' => $refuser)->send($big), 75, 'sendmail\'s status is returned');

	my $accepts = File::Spec->catfile($dir, 'accept.sh');
	open($fh, '>', $accepts) or BAIL_OUT("Cannot write $accepts: $!");
	print $fh "#!/bin/sh\ncat >/dev/null\nexit 0\n";
	close($fh);
	chmod(0755, $accepts) or BAIL_OUT("Cannot chmod $accepts: $!");
	is(Mail::Send::DKIM->new('sendmail' => $accepts)->send($big), 0, 'and 0 when it reads and accepts it');
};

done_testing();



( run in 1.354 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )