Skip to content

Commit b3b54a4

Browse files
committedSep 4, 2019
Add tests for penaltybox and userthrottle
1 parent 0a99fba commit b3b54a4

File tree

3 files changed

+187
-2
lines changed

3 files changed

+187
-2
lines changed
 

‎penaltybox.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ int
2525
main(int ac, char *av[]) {
2626
char * redis_host = "127.0.0.1";
2727
int redis_port = 6379;
28+
int window = 300;
2829
char * ip;
2930
char * cksum;
3031
char * from;
@@ -42,7 +43,7 @@ main(int ac, char *av[]) {
4243
urclHandle *urc;
4344
redisReply *res;
4445

45-
while ((c = getopt(ac, av, "h:H:p:P:")) != -1) {
46+
while ((c = getopt(ac, av, "h:H:p:P:w:")) != -1) {
4647
switch (c) {
4748
case 'h':
4849
redis_host = optarg;
@@ -64,6 +65,13 @@ main(int ac, char *av[]) {
6465
case 'P':
6566
prefix = optarg;
6667
break;
68+
case 'w':
69+
errno = 0;
70+
window = strtoll(optarg, NULL, 10);
71+
if (errno != 0) {
72+
err++;
73+
}
74+
break;
6775
case '?':
6876
default:
6977
err++;
@@ -130,7 +138,7 @@ main(int ac, char *av[]) {
130138
setenv("TZ", "", 1);
131139
tzset();
132140
then = mktime(&tm);
133-
if ((timediff = difftime(now, then)) > 300) {
141+
if ((timediff = difftime(now, then)) > window) {
134142
urcl_command(urc, key, "DEL %s", key);
135143
if (ht_threshold > 0) {
136144
if (((res = urcl_command(urc, ht_key, "INCR %s", ht_key)) !=

‎test/test_penaltybox.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import subprocess
2+
import time
3+
4+
import pytest
5+
6+
7+
@pytest.fixture
8+
def run_penaltybox(tool_path, redis):
9+
def _run_penaltybox(cksum='foo', ip='10.11.112.113'):
10+
env = {
11+
'SIMTA_CHECKSUM': cksum,
12+
'SIMTA_REMOTE_IP': ip,
13+
'SIMTA_SMTP_MAIL_FROM': 'foo@example.com',
14+
}
15+
16+
return subprocess.call(
17+
[
18+
tool_path('penaltybox'),
19+
'-p', str(redis),
20+
'-w', '1',
21+
'-H', '3',
22+
'no reason',
23+
],
24+
env=env,
25+
)
26+
27+
return _run_penaltybox
28+
29+
30+
def test_single(run_penaltybox):
31+
assert run_penaltybox() == 1
32+
33+
34+
def test_window(run_penaltybox):
35+
assert run_penaltybox() == 1
36+
assert run_penaltybox() == 1
37+
38+
39+
@pytest.mark.parametrize('ip', [
40+
'10.11.112.113',
41+
'fdee:f057:3bf9:7dce:dead:60ff:d00d:60ff',
42+
])
43+
def test_accept(run_penaltybox, ip):
44+
assert run_penaltybox(ip=ip) == 1
45+
time.sleep(2)
46+
assert run_penaltybox(ip=ip) == 0
47+
48+
49+
@pytest.mark.parametrize('ips', [
50+
('10.11.112.113', '10.11.112.1'),
51+
('fdee:f057:3bf9:7dce:dead:60ff:d00d:60ff', 'fdee:f057:3bf9:7dce:0001:0001:0001:0001'),
52+
])
53+
def test_accept_subnet(run_penaltybox, ips):
54+
assert run_penaltybox(ip=ips[0]) == 1
55+
time.sleep(2)
56+
assert run_penaltybox(ip=ips[1]) == 0
57+
58+
59+
@pytest.mark.parametrize('ips', [
60+
('10.11.112.113', '10.11.111.1'),
61+
('fdee:f057:3bf9:7dce:dead:60ff:d00d:60ff', 'fdee:f057:3bf9:7dcd:0001:0001:0001:0001'),
62+
])
63+
def test_reject_subnet(run_penaltybox, ips):
64+
assert run_penaltybox(ip=ips[0]) == 1
65+
time.sleep(2)
66+
assert run_penaltybox(ip=ips[1]) == 1
67+
68+
69+
def test_reject_checksum(run_penaltybox):
70+
assert run_penaltybox() == 1
71+
time.sleep(2)
72+
assert run_penaltybox(cksum='bar') == 1
73+
74+
75+
def test_hattrick(run_penaltybox):
76+
msgs = ['foo', 'bar', 'baz']
77+
for msg in msgs:
78+
assert run_penaltybox(cksum=msg) == 1
79+
time.sleep(2)
80+
for msg in msgs:
81+
assert run_penaltybox(cksum=msg) == 0
82+
83+
# This IP should now be exempt from penalties
84+
assert run_penaltybox(cksum='quux') == 0

‎test/test_userthrottle.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import subprocess
2+
3+
import pytest
4+
5+
6+
@pytest.fixture
7+
def run_userthrottle(tool_path, redis):
8+
def _run_userthrottle(auth='foo', ip='10.11.112.113', mfrom='foo@example.com', hfrom=None, nrcpts=1):
9+
env = {
10+
'SIMTA_AUTH_ID': auth,
11+
'SIMTA_REMOTE_IP': ip,
12+
'SIMTA_SMTP_MAIL_FROM': mfrom,
13+
'SIMTA_NRCPTS': str(nrcpts),
14+
}
15+
16+
if hfrom:
17+
env['SIMTA_HEADER_FROM'] = hfrom
18+
19+
res = subprocess.check_output(
20+
[
21+
tool_path('userthrottle'),
22+
'-p', str(redis),
23+
'-d', 'example.com',
24+
],
25+
env=env,
26+
)
27+
28+
return res.strip()
29+
return _run_userthrottle
30+
31+
32+
def test_single(run_userthrottle):
33+
assert run_userthrottle() == '1'
34+
35+
36+
def test_multiple(run_userthrottle):
37+
assert run_userthrottle() == '1'
38+
assert run_userthrottle() == '2'
39+
40+
41+
def test_nrcpt(run_userthrottle):
42+
assert run_userthrottle() == '1'
43+
assert run_userthrottle(nrcpts=5) == '6'
44+
45+
46+
def test_multiple_ips(run_userthrottle):
47+
assert run_userthrottle() == '1'
48+
assert run_userthrottle(ip='10.12.112.114') == '2'
49+
50+
51+
def test_mfrom_mismatch(run_userthrottle):
52+
assert run_userthrottle(mfrom='bar@example.com') == '2'
53+
54+
55+
def test_multiple_addresses(run_userthrottle):
56+
assert run_userthrottle() == '1'
57+
assert run_userthrottle(mfrom='bar@example.com') == '5'
58+
assert run_userthrottle(mfrom='baz@example.com') == '17'
59+
assert run_userthrottle() == '23'
60+
61+
62+
def test_multiple_auth(run_userthrottle):
63+
assert run_userthrottle() == '1'
64+
assert run_userthrottle(auth='bar', mfrom='bar@example.com') == '1'
65+
66+
67+
def test_multiple_mfrom(run_userthrottle):
68+
assert run_userthrottle() == '1'
69+
assert run_userthrottle(mfrom='bar@example.com') == '5'
70+
71+
72+
def test_hfrom_match(run_userthrottle):
73+
assert run_userthrottle(hfrom='foo@example.com') == '1'
74+
assert run_userthrottle(hfrom='foo@example.com') == '2'
75+
76+
77+
def test_hfrom_mismatch(run_userthrottle):
78+
assert run_userthrottle(hfrom='bar@example.com') == '2'
79+
assert run_userthrottle(hfrom='bar@example.com') == '4'
80+
81+
82+
def test_domain_mismatch(run_userthrottle):
83+
assert run_userthrottle(mfrom='foo@example.edu') == '2'
84+
assert run_userthrottle(hfrom='foo@example.com') == '4'
85+
86+
87+
def test_worst_case(run_userthrottle):
88+
assert run_userthrottle() == '1'
89+
assert run_userthrottle(mfrom='bar@example.edu') == '9'
90+
assert run_userthrottle(mfrom='baz@example.edu', hfrom='baz@example.com') == '57'
91+
assert run_userthrottle(mfrom='baz@example.edu', hfrom='baz@example.com', nrcpts=10) == '537'
92+
assert run_userthrottle(auth='bar', mfrom='baz@example.edu', hfrom='baz@example.com') == '8'
93+
assert run_userthrottle(auth='bar', mfrom='foo@example.edu', hfrom='baz@example.com') == '24'

0 commit comments

Comments
 (0)
Please sign in to comment.