Debian Codegraph @

t/plugin_tests/badmailfrom

raw
1 #!perl -w
2
3 use strict;
4 use Data::Dumper;
5
6 use Qpsmtpd::Address;
7 use Qpsmtpd::Constants;
8
9 sub register_tests {
10 my $self = shift;
11
12 $self->register_test("test_badmailfrom_is_immune_sender", 5);
13 $self->register_test("test_badmailfrom_match", 7);
14 $self->register_test("test_badmailfrom_hook_mail", 4);
15 }
16
17 sub test_badmailfrom_is_immune_sender {
18 my $self = shift;
19
20 my $transaction = $self->qp->transaction;
21 my $test_email = '[email protected]';
22 my $address = Qpsmtpd::Address->new( "<$test_email>" );
23 $transaction->sender($address);
24 ok( $self->is_immune_sender( $transaction->sender, [] ), "empty list");
25
26 $address = Qpsmtpd::Address->new( '<>' );
27 $transaction->sender($address);
28 ok( $self->is_immune_sender( $transaction->sender, ['[email protected]'] ), "null sender");
29
30 $address = Qpsmtpd::Address->new( '<matt@>' );
31 $transaction->sender($address);
32 ok( $self->is_immune_sender( $transaction->sender, ['[email protected]'] ), "missing host");
33
34 $address = Qpsmtpd::Address->new( '<@example.com>' );
35 $transaction->sender($address);
36 ok( $self->is_immune_sender( $transaction->sender, ['[email protected]'] ), "missing user");
37
38 $address = Qpsmtpd::Address->new( '<[email protected]>' );
39 $transaction->sender($address);
40 ok( ! $self->is_immune_sender( $transaction->sender, ['[email protected]'] ), "false");
41 };
42
43 sub test_badmailfrom_hook_mail {
44 my $self = shift;
45
46 $self->_reset_connection_flags();
47
48 my $transaction = $self->qp->transaction;
49
50 my $test_email = '[email protected]';
51 my $address = Qpsmtpd::Address->new( "<$test_email>" );
52 $transaction->sender($address);
53
54 $self->{_badmailfrom_config} = ['[email protected]','[email protected]'];
55 $self->{_args}{reject} = 1;
56 $transaction->notes('naughty', '');
57 my ($r, $err) = $self->hook_mail( $transaction, $address );
58 cmp_ok( $r, '==', DENY, "hook_mail rc");
59 cmp_ok( $err, 'eq', 'Your envelope sender is in my badmailfrom list', "default reason");
60
61 $self->{_badmailfrom_config} = ['[email protected]','[email protected] Yer a spammin bastert'];
62 $transaction->notes('naughty', '');
63 ($r, $err) = $self->hook_mail( $transaction, $address );
64 cmp_ok( $r, '==', DENY, "hook_mail rc");
65 cmp_ok( $err, 'eq', 'Yer a spammin bastert', "custom reason");
66 };
67
68 sub test_badmailfrom_match {
69 my $self = shift;
70
71 # is_match receives ( $from, $bad, $host )
72
73 my $r = $self->is_match( '[email protected]', '[email protected]', 'test.net' );
74 ok($r, "match");
75
76 ok( ! $self->is_match( '[email protected]', '[email protected]', 'tnpi.net' ),
77 "non-match");
78
79 ok( $self->is_match( '[email protected]', '@test.net', 'test.net' ),
80 "match host");
81
82 ok( ! $self->is_match( '[email protected]', '@test.not', 'test.net' ),
83 "non-match host");
84
85 ok( ! $self->is_match( '[email protected]', '@test.net', 'test.not' ),
86 "non-match host");
87
88 ok( $self->is_match( '[email protected]', 'test.net$', 'tnpi.net' ),
89 "pattern match");
90
91 ok( ! $self->is_match( '[email protected]', 'test.not$', 'tnpi.net' ),
92 "pattern non-match");
93 };
94
95 sub _reset_connection_flags {
96 my $self = shift;
97 $self->qp->connection->relay_client(0);
98 $self->qp->connection->notes('whitelisthost', 0);
99 $self->connection->notes('naughty',0);
100 $self->connection->notes('rejected', 0);
101 };
102