Debian Codegraph @

t/plugin_tests/rcpt_ok

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_get_rcpt_host', 7);
12 $self->register_test('test_is_in_rcpthosts', 3);
13 $self->register_test('test_is_in_morercpthosts', 2);
14 $self->register_test('test_hook_rcpt', 3);
15 }
16
17
18 sub test_hook_rcpt {
19 my $self = shift;
20
21 my $transaction = $self->qp->transaction;
22
23 my $address = Qpsmtpd::Address->parse('<user@localhost>');
24 my ($r, $mess) = $self->hook_rcpt( $transaction, $address );
25 cmp_ok( $r, '==', OK, "localhost");
26
27 $address = Qpsmtpd::Address->parse('<[email protected]>');
28 ($r, $mess) = $self->hook_rcpt( $transaction, $address );
29 cmp_ok( $r, '==', DENY, "example.com");
30
31 $self->qp->connection->relay_client(1);
32 ($r, $mess) = $self->hook_rcpt( $transaction, $address );
33 cmp_ok( $r, '==', OK, "example.com");
34 $self->qp->connection->relay_client(0);
35 };
36
37 sub test_is_in_rcpthosts {
38 my $self = shift;
39
40 my @hosts = $self->qp->config('rcpthosts');
41 my $host = $hosts[0];
42
43 if ( $host ) {
44 ok( $self->is_in_rcpthosts( $host ), "is_in_rcpthosts, $host");
45 }
46 else {
47 ok(1, "is_in_rcpthosts (skip, no entries)" );
48 };
49
50 ok( $self->is_in_rcpthosts( 'localhost' ), "is_in_rcpthosts +");
51 ok( ! $self->is_in_rcpthosts( 'example.com' ), "is_in_rcpthosts -");
52 };
53
54 sub test_is_in_morercpthosts {
55 my $self = shift;
56
57 my $ref = $self->qp->config('morercpthosts', 'map');
58 my ($domain) = keys %$ref;
59 if ( $domain ) {
60 ok( $self->is_in_morercpthosts( $domain ), "$domain");
61 }
62 else {
63 ok(1, "is_in_morercpthosts (skip, no entries)" );
64 };
65
66 ok( ! $self->is_in_morercpthosts( 'example.com' ), "missing -");
67 };
68
69 sub test_get_rcpt_host {
70 my $self = shift;
71
72 my $address = Qpsmtpd::Address->parse('<[email protected]>');
73 cmp_ok( $self->get_rcpt_host( $address ), 'eq', 'example.com', "+" );
74
75 $address = Qpsmtpd::Address->parse('<[email protected]>');
76 cmp_ok( $self->get_rcpt_host( $address ), 'eq', 'example.com', "+" );
77
78 $address = Qpsmtpd::Address->parse('<[email protected]>');
79 cmp_ok( $self->get_rcpt_host( $address ), 'eq', 'example.com', "+" );
80
81 $address = Qpsmtpd::Address->parse('<postmaster>');
82 my $local_hostname = $self->get_rcpt_host( $address );
83 if ( $local_hostname eq 'some.host.example.org' ) {
84 cmp_ok( $self->get_rcpt_host( $address ), 'eq', 'some.host.example.org',
85 "special postmaster +" );
86 }
87 else {
88 ok( 1, "special postmaster + ($local_hostname)" );
89 }
90
91 # I think this is a bug. Qpsmtpd::Address fails to parse <abuse>
92 $address = Qpsmtpd::Address->parse('<abuse>');
93 ok( ! $self->get_rcpt_host( $address ), "missing host" );
94
95 $address = Qpsmtpd::Address->parse('<>');
96 ok( ! $self->get_rcpt_host( $address ), "null recipient" );
97
98 $address = Qpsmtpd::Address->parse('<@example.com>');
99 ok( ! $self->get_rcpt_host( $address ), "missing user" );
100 };
101