File: testSRS.py

package info (click to toggle)
pysrs 1.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 280 kB
  • sloc: python: 1,288; sh: 72; makefile: 23
file content (261 lines) | stat: -rw-r--r-- 8,267 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# $Log$
# Revision 1.2  2006/02/16 05:16:58  customdesigned
# Support SRS signing mode.
#
# Revision 1.1.1.2  2005/06/03 04:13:55  customdesigned
# Support sendmail socketmap
#
# Revision 1.4  2004/08/26 03:31:38  stuart
# Introduce sendmail socket map
#
# Revision 1.3  2004/03/25 00:02:21  stuart
# FIXME where case smash test depends on day
#
# Revision 1.2  2004/03/22 18:20:00  stuart
# Read config for sendmail maps from /etc/mail/pysrs.cfg
#
# Revision 1.1.1.1  2004/03/19 05:23:13  stuart
# Import to CVS
#
#
# AUTHOR
# Shevek
# CPAN ID: SHEVEK
# cpan@anarres.org
# http://www.anarres.org/projects/
#
# Translated to Python by stuart@bmsi.com
# http://bmsi.com/python/milter.html
#
# Copyright (c) 2017 Stuart Gathman  All rights reserved.
# Portions Copyright (c) 2004 Shevek. All rights reserved.
# Portions Copyright (c) 2004,2006 Business Management Systems. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Python itself.

import unittest
import Milter
from Milter.test import TestBase
from SRS.Guarded import Guarded
from SRS.DB import DB
from SRS.Reversible import Reversible
from SRS.Daemon import Daemon
import srsmilter
import SRS
import threading
import socket
try:
  from StringIO import StringIO
except:
  from io import StringIO

class TestMilter(TestBase,srsmilter.srsMilter):
  def __init__(self):
    TestBase.__init__(self)
    srsmilter.config = srsmilter.Config(['pysrs.cfg'])
    srsmilter.srsMilter.__init__(self)
    self.setsymval('j','test.milter.org')

class SRSMilterTestCase(unittest.TestCase):

  msg = '''From: good@example.com
Subject: test

test
'''

  ## Test rejecting bounce spam
  def testReject(self):
    milter = TestMilter()
    milter.conf.srs_domain = set(['example.com'])
    milter.conf.srs_reject_spoofed = False
    fp = StringIO(self.msg)
    rc = milter.connect('testReject',ip='192.0.3.1')
    self.assertEqual(rc,Milter.CONTINUE)
    rc = milter.feedFile(fp,sender='',rcpt='good@example.org')
    self.assertEqual(rc,Milter.CONTINUE)
    milter.conf.srs_reject_spoofed = True
    fp.seek(0)
    rc = milter.feedFile(fp,sender='',rcpt='bad@example.com')
    self.assertEqual(rc,Milter.REJECT)
    milter.close()

  ## Test SRS coding of MAIL FROM
  def testSign(self):
    milter = TestMilter()
    milter.conf.signdomain = set(['example.com'])
    milter.conf.miltersrs = True
    fp = StringIO(self.msg)
    rc = milter.connect('testSign',ip='192.0.3.1')
    self.assertEqual(rc,Milter.CONTINUE)
    fp.seek(0)
    rc = milter.feedFile(fp,sender='good@example.com',rcpt='good@example.org')
    self.assertEqual(rc,Milter.CONTINUE)
    s = milter.conf.srs.reverse(milter._sender[1:-1])
    self.assertEqual(s,'good@example.com')
    # check that it doesn't happen when disabled
    milter.conf.miltersrs = False
    fp.seek(0)
    rc = milter.feedFile(fp,sender='good@example.com',rcpt='good@example.org')
    self.assertEqual(rc,Milter.CONTINUE)
    self.assertEqual(milter._sender,'<good@example.com>')
    milter.close()

class SRSTestCase(unittest.TestCase):
  
  def setUp(self):
    # make sure user modified tag works
    SRS.SRS0TAG = 'ALT0'
    SRS.SRS1TAG = 'ALT1'

  def warn(self,*msg):
    self.case_smashed = True

  # There and back again
  def testGuarded(self):
    srs = Guarded()
    self.assertRaises(AssertionError,srs.forward,
            'mouse@disney.com','mydomain.com')
    srs.set_secret('shhhh!')
    srs.separator = '+'
    sender = '"Blah blah"@orig.com'
    srsaddr = srs.forward(sender,sender)
    self.assertEqual(srsaddr,sender)
    srsaddr = srs.forward(sender,'second.com')
    self.assertTrue(srsaddr.startswith('"'+SRS.SRS0TAG),srsaddr)
    srsaddr1 = srs.forward(srsaddr,'third.com')
    #print srsaddr1
    self.assertTrue(srsaddr1.startswith('"'+SRS.SRS1TAG))
    srsaddr2 = srs.forward(srsaddr1,'fourth.com')
    #print srsaddr2
    self.assertTrue(srsaddr2.startswith('"'+SRS.SRS1TAG))
    addr = srs.reverse(srsaddr2)
    self.assertEqual(srsaddr,addr)
    addr = srs.reverse(srsaddr1)
    self.assertEqual(srsaddr,addr)
    addr = srs.reverse(srsaddr)
    self.assertEqual(sender,addr)

  def testSign(self):
    srs = Guarded()
    srs.set_secret('shhhh!')
    srs.separator = '+'
    sender = 'mouse@orig.com'
    sig = srs.sign(sender)
    addr = srs.reverse(sig)
    self.assertEqual(sender,addr)
    sender = 'mouse@ORIG.com'
    sig = srs.sign(sender)
    addr = srs.reverse(sig)
    self.assertEqual(sender,addr)
    addr = srs.reverse(sig.lower())
    self.assertEqual(sender.lower(),addr)

  def testCaseSmash(self):
    srs = SRS.new(secret='shhhhh!',separator='+')
    # FIXME: whether case smashing occurs depends on what day it is.
    sender = 'mouse@fickle1.com'
    srsaddr = srs.forward(sender,'second.com')
    self.assertTrue(srsaddr.startswith(SRS.SRS0TAG))
    self.case_smashed = False
    srs.warn = self.warn
    addr = srs.reverse(srsaddr.lower())
    self.assertTrue(self.case_smashed)	# check that warn was called
    self.assertEqual(sender,addr)

  def testReversible(self):
    srs = Reversible()
    self.assertRaises(AssertionError,srs.forward,
            'mouse@disney.com','mydomain.com')
    srs.set_secret('shhhh!')
    srs.separator = '+'
    sender = 'mouse@orig.com'
    srsaddr = srs.forward(sender,sender)
    self.assertEqual(srsaddr,sender)
    srsaddr = srs.forward(sender,'second.com')
    #print srsaddr
    self.assertTrue(srsaddr.startswith(SRS.SRS0TAG))
    srsaddr1 = srs.forward(srsaddr,'third.com')
    #print srsaddr1
    self.assertTrue(srsaddr1.startswith(SRS.SRS0TAG))
    srsaddr2 = srs.forward(srsaddr1,'fourth.com')
    #print srsaddr2
    self.assertTrue(srsaddr2.startswith(SRS.SRS0TAG))
    addr = srs.reverse(srsaddr2)
    self.assertEqual(srsaddr1,addr)
    addr = srs.reverse(srsaddr1)
    self.assertEqual(srsaddr,addr)
    addr = srs.reverse(srsaddr)
    self.assertEqual(sender,addr)

  def testDB(self,database='/tmp/srstest'):
    srs = DB(database=database)
    self.assertRaises(AssertionError,srs.forward,
            'mouse@disney.com','mydomain.com')
    srs.set_secret('shhhh!')
    sender = 'mouse@orig.com'
    srsaddr = srs.forward(sender,sender)
    self.assertEqual(srsaddr,sender)
    srsaddr = srs.forward(sender,'second.com')
    #print(srsaddr)
    self.assertTrue(srsaddr.startswith(SRS.SRS0TAG))
    srsaddr1 = srs.forward(srsaddr,'third.com')
    #print(srsaddr1)
    self.assertTrue(srsaddr1.startswith(SRS.SRS0TAG))
    srsaddr2 = srs.forward(srsaddr1,'fourth.com')
    #print(srsaddr2)
    self.assertTrue(srsaddr2.startswith(SRS.SRS0TAG))
    addr = srs.reverse(srsaddr2)
    self.assertEqual(srsaddr1,addr)
    addr = srs.reverse(srsaddr1)
    self.assertEqual(srsaddr,addr)
    addr = srs.reverse(srsaddr)
    self.assertEqual(sender,addr)

  def run2(self): # handle two requests
    self.daemon.server.handle_request()
    self.daemon.server.handle_request()

  def sendcmd(self,*args):
    sock = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)
    sock.connect(self.sockname)
    sock.send(b' '.join(args)+b'\n')
    res = sock.recv(128).strip()
    sock.close()
    return res

  def testExim(self,sockname='/tmp/srsd',secret="shhhh!"):
    self.sockname = sockname
    self.daemon = Daemon(socket=sockname,secret=secret)
    server = threading.Thread(target=self.run2,name='srsd')
    server.start()
    sender = b'mouse@orig.com'
    srsaddr = self.sendcmd(b'FORWARD',sender,b'second.com')
    addr = self.sendcmd(b'REVERSE',srsaddr)
    server.join()
    self.assertEqual(sender,addr)

  def testProgMap(self):
    import envfrom2srs
    import srs2envtol
    orig = 'mickey<@orig.com.>'
    newaddr = envfrom2srs.forward(orig)
    self.assertTrue(newaddr.endswith('.>'))
    addr2 = srs2envtol.reverse(newaddr)
    self.assertEqual(addr2,orig)
    # check case smashing by braindead mailers
    self.case_smashed = False
    srs2envtol.srs.warn = self.warn
    addr2 = srs2envtol.reverse(newaddr.lower())
    self.assertEqual(addr2,orig)
    self.assertTrue(self.case_smashed)

def suite(): 
  s = unittest.makeSuite(SRSTestCase,'test')
  s.addTest(makeSuite(SRSMilterTestCase,'test'))
  #s.addTest(doctest.DocTestSuite(bms))
  return s

if __name__ == '__main__':
    unittest.main()