Debian Codegraph @

t/plugin_tests/badrcptto

raw
1 #!perl -w
2
3 use strict;
4 use warnings;
5
6 use Qpsmtpd::Constants;
7
8 sub register_tests {
9 my $self = shift;
10
11 $self->register_test("test_is_match", 10);
12 $self->register_test("test_hook_rcpt", 3);
13 $self->register_test("test_get_host_and_to", 8);
14 }
15
16 sub _reset_connection_flags {
17 my $self = shift;
18 $self->qp->connection->relay_client(0);
19 $self->qp->connection->notes('whitelisthost', 0);
20 $self->connection->notes('naughty',0);
21 $self->connection->notes('rejected', 0);
22 };
23
24 sub test_is_match {
25 my $self = shift;
26
27 # is_match receives ( $to, $bad, $host )
28
29 my $r = $self->is_match( '[email protected]', '[email protected]', 'example.com' );
30 ok($r, "match");
31
32 ok( $self->is_match( '[email protected]', '[email protected]', 'tnpi.com' ),
33 "case insensitive match");
34
35 ok( $self->is_match( '[email protected]', '[email protected]', 'tnpi.com' ),
36 "case insensitive match +");
37
38 ok( ! $self->is_match( '[email protected]', '[email protected]', 'tnpi.com' ),
39 "non-match");
40
41 ok( ! $self->is_match( '[email protected]', '[email protected]', 'tnpi.com' ),
42 "case insensitive non-match");
43
44 ok( $self->is_match( '[email protected]', '@example.com', 'example.com' ),
45 "match host");
46
47 ok( ! $self->is_match( '[email protected]', '@example.not', 'example.com' ),
48 "non-match host");
49
50 ok( ! $self->is_match( '[email protected]', '@example.com', 'example.not' ),
51 "non-match host");
52
53 ok( $self->is_match( '[email protected]', 'example.com$', 'tnpi.com' ),
54 "pattern match");
55
56 ok( ! $self->is_match( '[email protected]', 'example.not$', 'tnpi.com' ),
57 "pattern non-match");
58 };
59
60 sub test_hook_rcpt {
61 my $self = shift;
62
63 $self->_reset_connection_flags();
64
65 my $transaction = $self->qp->transaction;
66 my $recipient = Qpsmtpd::Address->new( '<[email protected]>' );
67
68 my ($r, $mess) = $self->hook_rcpt( $transaction, $recipient );
69 cmp_ok( $r, '==', DECLINED, "valid +");
70
71 $recipient = Qpsmtpd::Address->new( '<[email protected]>' );
72 ($r, $mess) = $self->hook_rcpt( $transaction, $recipient );
73 cmp_ok( $r, '==', DENY, "bad match, +, $mess");
74
75 $recipient = Qpsmtpd::Address->new( '<[email protected]>' );
76 ($r, $mess) = $self->hook_rcpt( $transaction, $recipient );
77 cmp_ok( $r, '==', DENY, "bad host match, +, $mess");
78 };
79
80 sub test_get_host_and_to {
81 my $self = shift;
82
83 my $recipient = Qpsmtpd::Address->new( '<>' );
84 my ($host, $to) = $self->get_host_and_to( $recipient );
85 ok( ! $host, "null recipient -");
86
87 $recipient = Qpsmtpd::Address->new( '<user>' );
88 ($host, $to) = $self->get_host_and_to( $recipient );
89 ok( ! $host, "missing host -");
90 ok( ! $to, "unparseable to -");
91
92 $recipient = Qpsmtpd::Address->new( '<[email protected]>' );
93 ($host, $to) = $self->get_host_and_to( $recipient );
94 ok( $host, "valid host +");
95 ok( $to, "valid to +");
96 cmp_ok( $to, 'eq', '[email protected]', "valid to +");
97
98 $recipient = Qpsmtpd::Address->new( '<[email protected]>' );
99 ($host, $to) = $self->get_host_and_to( $recipient );
100 cmp_ok( $host, 'eq', 'example.com', "case normalized +");
101 cmp_ok( $to, 'eq', '[email protected]', "case normalized +");
102 };