Debian Codegraph @

t/plugin_tests/relay

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_relay_only', 2);
12 $self->register_test('test_is_octet_match', 3);
13 $self->register_test('test_is_in_cidr_block', 4);
14 $self->register_test('test_is_in_norelayclients', 5);
15 }
16
17 sub test_relay_only {
18 my $self = shift;
19
20 $self->qp->connection->relay_client(0);
21 my $r = $self->relay_only();
22 cmp_ok( $r, '==', DENY, "relay_only -");
23
24 $self->qp->connection->relay_client(1);
25 $r = $self->relay_only();
26 cmp_ok( $r, '==', OK, "relay_only +");
27
28 $self->qp->connection->relay_client(0);
29 };
30
31 sub test_is_octet_match {
32 my $self = shift;
33
34 $self->populate_relayclients();
35
36 $self->qp->connection->remote_ip('192.0.1.1');
37 ok( $self->is_octet_match(), "match, +");
38
39 $self->qp->connection->remote_ip('192.51.1.1');
40 ok( ! $self->is_octet_match(), "nope, -");
41
42 $self->qp->connection->remote_ip('203.0.113.0');
43 ok( ! $self->is_octet_match(), "nope, -");
44 };
45
46 sub test_is_in_cidr_block {
47 my $self = shift;
48
49 $self->qp->connection->remote_ip('192.0.1.1');
50 $self->{_cidr_blocks} = [ '192.0.1.0/24' ];
51 ok( $self->is_in_cidr_block(), "match, +" );
52
53 $self->{_cidr_blocks} = [ '192.0.0.0/24' ];
54 ok( ! $self->is_in_cidr_block(), "nope, -" );
55
56
57 $self->qp->connection->remote_ip('fdda:b13d:e431:ae06:00a1::');
58 $self->{_cidr_blocks} = [ 'fdda:b13d:e431:ae06::/64' ];
59 ok( $self->is_in_cidr_block(), "match, +" );
60
61 $self->{_cidr_blocks} = [ 'fdda:b13d:e431:be17::' ];
62 ok( ! $self->is_in_cidr_block(), "nope, -" );
63 };
64
65 sub test_is_in_norelayclients {
66 my $self = shift;
67
68 my @matches = qw/ 192.0.99.5 192.0.98.1 192.0.98.255 /;
69 my @false = qw/ 192.0.99.7 192.0.109.7 /;
70
71 foreach ( @matches ) {
72 $self->qp->connection->remote_ip($_);
73 ok( $self->is_in_norelayclients(), "match, + ($_)");
74 };
75
76 foreach ( @false ) {
77 $self->qp->connection->remote_ip($_);
78 ok( ! $self->is_in_norelayclients(), "match, - ($_)");
79 };
80 };
81