summaryrefslogtreecommitdiffstats
path: root/debian/tests/testmails
blob: 3329809b57770da3b4092ea7990b34df8428c44c (plain)
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
#!/usr/bin/python3

import grp
import imaplib
import os
import os.path
import poplib
import pwd
import random
import string
import subprocess
import sys
import unittest

from passlib.hash import des_crypt


def random_string(length):
    '''Return a random string, consisting of ASCII letters, with given
    length.'''

    s = ''
    maxind = len(string.ascii_letters)-1
    for _ in range(length):
        s += string.ascii_letters[random.randint(0, maxind)]
    return s.lower()


def login_exists(login):
    '''Checks whether the given login exists on the system.'''

    try:
        pwd.getpwnam(login)
        return True
    except KeyError:
        return False


def get_distribution():
    '''Return the name of the Linux Distribution we are running.'''
    cmd = ['lsb_release', '-si']
    output = subprocess.check_output(cmd)
    return output.strip()


class TestUser:
    '''Create a temporary test user and remove it again on close.'''

    def __init__(self):
        '''Create a new user account with a random password.'''

        self.login = None

        while True:
            login = random_string(8)
            if not login_exists(login):
                break

        self.salt = random_string(2)
        self.password = random_string(8)
        self.crypted = des_crypt.using(salt=self.salt).hash(self.password)

        subprocess.check_call(['useradd', '-p', self.crypted, '-m', login])

        self.login = login
        p = pwd.getpwnam(self.login)
        self.uid = p[2]
        self.gid = p[3]

    def __del__(self):
        '''Remove the created user account.'''

        if self.login:
            self.close()

    def close(self):
        '''Remove the created user account.'''

        subprocess.check_call(['userdel', '-f', '-r', self.login])
        self.login = None


class DovecotBasics(unittest.TestCase):
    '''Base operational tests for Dovecot server.'''

    def setUp(self):
        '''Create test scenario.

        We want to test the default setup, but pre-setup an mbox on a tmp user
        '''

        self.distribution = get_distribution()
        self.user = TestUser()

        # create fresh test mailbox with one new and one old mail
        self.mailbox = '/var/mail/' + self.user.login
        self.orig_mbox = '''From test1@test1.com Fri Nov 17 02:21:08 2006
Date: Thu, 16 Nov 2006 17:12:23 -0800
From: Test User 1 <test1@test1.com>
To: Dovecot tester <dovecot@test.com>
Subject: Test 1
Status: N

Some really important news.

From test2@test1.com Tue Nov 28 11:29:34 2006
Date: Tue, 28 Nov 2006 11:29:34 +0100
From: Test User 2 <test2@test2.com>
To: Dovecot tester <dovecot@test.com>
Subject: Test 2
Status: R

More news.

Get cracking!
'''
        with open(self.mailbox, 'w') as f:
            f.write(self.orig_mbox)
        os.chown(self.mailbox, self.user.uid, grp.getgrnam('mail')[2])
        os.chmod(self.mailbox, 0o660)

    def tearDown(self):
        self.user.close()

    def _test_pop3_proto(self, pop):
        '''Internal factorization of POP3 protocol checks with an established
        connection.'''

        # check empty password
        self.assertEqual(pop.user(self.user.login), b'+OK')
        self.assertRaises(poplib.error_proto, pop.pass_, '')

        # check wrong password
        self.assertEqual(pop.user(self.user.login), b'+OK')
        self.assertRaises(poplib.error_proto, pop.pass_, '123')

        # check correct password
        self.assertEqual(pop.user(self.user.login), b'+OK')
        self.assertEqual(pop.pass_(self.user.password), b'+OK Logged in.')

        # check messages
        self.assertEqual(pop.stat()[0], 2, b'2 available messages')
        self.assertEqual(pop.list()[1], [b'1 163', b'2 161'])
        self.assertEqual('\n'.join(l.decode() for l in pop.retr(1)[1]), '''Date: Thu, 16 Nov 2006 17:12:23 -0800
From: Test User 1 <test1@test1.com>
To: Dovecot tester <dovecot@test.com>
Subject: Test 1

Some really important news.''')
        self.assertEqual('\n'.join(l.decode() for l in pop.retr(2)[1]), '''Date: Tue, 28 Nov 2006 11:29:34 +0100
From: Test User 2 <test2@test2.com>
To: Dovecot tester <dovecot@test.com>
Subject: Test 2

More news.

Get cracking!''')

        self.assertEqual(pop.quit(), b'+OK Logging out.')

    def test_pop3(self):
        '''Test POP3 protocol.'''

        pop = poplib.POP3('localhost')
        self.assertEqual(pop.getwelcome(),
                         b'+OK Dovecot (%s) ready.' % self.distribution)

        self._test_pop3_proto(pop)

    def test_pop3s(self):
        '''Test POP3S protocol.'''

        pop = poplib.POP3_SSL('localhost')
        self.assertEqual(pop.getwelcome(),
                         b'+OK Dovecot (%s) ready.' % self.distribution)

        self._test_pop3_proto(pop)

    def _test_imap_proto(self, imap):
        '''Internal factorization of IMAP4 protocol checks with an established
        connection.'''

        # invalid passwords
        self.assertRaises(imaplib.IMAP4.error, imap.login,
                          self.user.login, '')
        self.assertRaises(imaplib.IMAP4.error, imap.login,
                          self.user.login, '123')

        # correct password
        imap.login(self.user.login, self.user.password)

        # list mailboxes
        status, imlist = imap.list()
        self.assertEqual(status, 'OK')
        self.assertTrue(imlist[0].decode().endswith('INBOX'))

        # check mails
        imap.select()
        self.assertEqual(imap.search(None, 'ALL'), ('OK', [b'1 2']))
        self.assertEqual(imap.fetch('1', '(FLAGS)'),
                         ('OK', [b'1 (FLAGS (\\Recent))']))
        self.assertEqual(imap.fetch('2', '(FLAGS)'),
                         ('OK', [b'2 (FLAGS (\\Seen \\Recent))']))
        self.assertEqual(imap.fetch('1', '(BODY[TEXT])')[1][0][1],
                         b'Some really important news.\r\n')
        self.assertEqual(imap.fetch('2', '(BODY[TEXT])')[1][0][1],
                         b'More news.\r\n\r\nGet cracking!')

        self.assertEqual(imap.fetch('1', '(RFC822)')[1],
                         [(b'1 (RFC822 {163}',
                           b'''Date: Thu, 16 Nov 2006 17:12:23 -0800\r
From: Test User 1 <test1@test1.com>\r
To: Dovecot tester <dovecot@test.com>\r
Subject: Test 1\r
\r
Some really important news.\r
'''), b')'])

        # delete mail 1
        self.assertEqual(imap.store('1', '+FLAGS', '\\Deleted')[0], 'OK')
        self.assertEqual(imap.expunge()[0], 'OK')
        self.assertEqual(imap.search(None, 'ALL'), ('OK', [b'1']))

        # old mail 2 is mail 1 now
        self.assertEqual(imap.fetch('1', '(RFC822)')[1],
                         [(b'1 (RFC822 {161}',
                           b'''Date: Tue, 28 Nov 2006 11:29:34 +0100\r
From: Test User 2 <test2@test2.com>\r
To: Dovecot tester <dovecot@test.com>\r
Subject: Test 2\r
\r
More news.\r
\r
Get cracking!'''), b')'])
        imap.close()
        imap.logout()

    def test_imap(self):
        '''Test IMAP4 protocol.'''

        imap = imaplib.IMAP4('localhost')
        self._test_imap_proto(imap)

    def test_imaps(self):
        '''Test IMAP4S protocol.'''

        imap = imaplib.IMAP4_SSL('localhost')
        self._test_imap_proto(imap)


if __name__ == '__main__':
    os.dup2(1, 2)
    suite = unittest.TestSuite()
    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(DovecotBasics))
    result = unittest.TextTestRunner(verbosity=2).run(suite)
    sys.exit(not result.wasSuccessful())