|
@@ -1,383 +0,0 @@
|
1
|
|
-import unittest
|
2
|
|
-from time import sleep
|
3
|
|
-import uuid
|
4
|
|
-import socket
|
5
|
|
-import requests
|
6
|
|
-import os
|
7
|
|
-
|
8
|
|
-TEST_SERVER = 'sovereign.local'
|
9
|
|
-TEST_ADDRESS = 'sovereign@sovereign.local'
|
10
|
|
-TEST_PASSWORD = 'foo'
|
11
|
|
-CA_BUNDLE = 'roles/common/files/wildcard_ca.pem'
|
12
|
|
-
|
13
|
|
-socket.setdefaulttimeout(5)
|
14
|
|
-os.environ['REQUESTS_CA_BUNDLE'] = CA_BUNDLE
|
15
|
|
-
|
16
|
|
-class SSHTests(unittest.TestCase):
|
17
|
|
- def test_ssh_banner(self):
|
18
|
|
- """SSH is responding with its banner"""
|
19
|
|
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
20
|
|
- s.connect((TEST_SERVER, 22))
|
21
|
|
- data = s.recv(1024)
|
22
|
|
- s.close()
|
23
|
|
-
|
24
|
|
- self.assertRegexpMatches(data, '^SSH-2.0-OpenSSH')
|
25
|
|
-
|
26
|
|
-
|
27
|
|
-class WebTests(unittest.TestCase):
|
28
|
|
- def test_blog_http(self):
|
29
|
|
- """Blog is redirecting to https"""
|
30
|
|
- # FIXME: requests won't verify sovereign.local with *.sovereign.local cert
|
31
|
|
- r = requests.get('http://' + TEST_SERVER, verify=False)
|
32
|
|
-
|
33
|
|
- # We should be redirected to https
|
34
|
|
- self.assertEquals(r.history[0].status_code, 301)
|
35
|
|
- self.assertEquals(r.url, 'https://' + TEST_SERVER + '/')
|
36
|
|
-
|
37
|
|
- # 403 - Since there is no documents in the blog directory
|
38
|
|
- self.assertEquals(r.status_code, 403)
|
39
|
|
-
|
40
|
|
- def test_mail_autoconfig_http_and_https(self):
|
41
|
|
- """Email autoconfiguration XML file is accessible over both HTTP and HTTPS"""
|
42
|
|
-
|
43
|
|
- # Test getting the file over HTTP and HTTPS
|
44
|
|
- for proto in ['http', 'https']:
|
45
|
|
- r = requests.get(proto + '://autoconfig.' + TEST_SERVER + '/mail/config-v1.1.xml')
|
46
|
|
-
|
47
|
|
- # 200 - We should see the XML file
|
48
|
|
- self.assertEquals(r.status_code, 200)
|
49
|
|
- self.assertIn('application/xml', r.headers['Content-Type'])
|
50
|
|
- self.assertIn('clientConfig version="1.1"', r.content)
|
51
|
|
-
|
52
|
|
- def test_webmail_http(self):
|
53
|
|
- """Webmail is redirecting to https and displaying login page"""
|
54
|
|
- r = requests.get('http://mail.' + TEST_SERVER)
|
55
|
|
-
|
56
|
|
- # We should be redirected to https
|
57
|
|
- self.assertEquals(r.history[0].status_code, 301)
|
58
|
|
- self.assertEquals(r.url, 'https://mail.' + TEST_SERVER + '/')
|
59
|
|
-
|
60
|
|
- # 200 - We should be at the login page
|
61
|
|
- self.assertEquals(r.status_code, 200)
|
62
|
|
- self.assertIn(
|
63
|
|
- 'Welcome to Roundcube Webmail',
|
64
|
|
- r.content
|
65
|
|
- )
|
66
|
|
-
|
67
|
|
- def test_zpush_http_unauthorized(self):
|
68
|
|
- r = requests.get('http://mail.' + TEST_SERVER + '/Microsoft-Server-ActiveSync')
|
69
|
|
-
|
70
|
|
- # We should be redirected to https
|
71
|
|
- self.assertEquals(r.history[0].status_code, 301)
|
72
|
|
- self.assertEquals(r.url, 'https://mail.' + TEST_SERVER + '/Microsoft-Server-ActiveSync')
|
73
|
|
-
|
74
|
|
- # Unauthorized
|
75
|
|
- self.assertEquals(r.status_code, 401)
|
76
|
|
-
|
77
|
|
- def test_zpush_https(self):
|
78
|
|
- r = requests.post('https://mail.' + TEST_SERVER + '/Microsoft-Server-ActiveSync',
|
79
|
|
- auth=('sovereign@sovereign.local', 'foo'),
|
80
|
|
- params={
|
81
|
|
- 'DeviceId': '1234',
|
82
|
|
- 'DeviceType': 'testbot',
|
83
|
|
- 'Cmd': 'Ping',
|
84
|
|
- })
|
85
|
|
-
|
86
|
|
- self.assertEquals(r.headers['content-type'],
|
87
|
|
- 'application/vnd.ms-sync.wbxml')
|
88
|
|
- self.assertEquals(r.headers['ms-server-activesync'],
|
89
|
|
- '14.0')
|
90
|
|
-
|
91
|
|
- def test_owncloud_http(self):
|
92
|
|
- """ownCloud is redirecting to https and displaying login page"""
|
93
|
|
- r = requests.get('http://cloud.' + TEST_SERVER)
|
94
|
|
-
|
95
|
|
- # We should be redirected to https
|
96
|
|
- self.assertEquals(r.history[0].status_code, 301)
|
97
|
|
- self.assertEquals(r.url, 'https://cloud.' + TEST_SERVER + '/')
|
98
|
|
-
|
99
|
|
- # 200 - We should be at the login page
|
100
|
|
- self.assertEquals(r.status_code, 200)
|
101
|
|
- self.assertIn(
|
102
|
|
- 'ownCloud',
|
103
|
|
- r.content
|
104
|
|
- )
|
105
|
|
-
|
106
|
|
- def test_selfoss_http(self):
|
107
|
|
- """selfoss is redirecting to https and displaying login page"""
|
108
|
|
- r = requests.get('http://news.' + TEST_SERVER)
|
109
|
|
-
|
110
|
|
- # We should be redirected to https
|
111
|
|
- self.assertEquals(r.history[0].status_code, 301)
|
112
|
|
- self.assertEquals(r.url, 'https://news.' + TEST_SERVER + '/')
|
113
|
|
-
|
114
|
|
- # 200 - We should be at the login page
|
115
|
|
- self.assertEquals(r.status_code, 200)
|
116
|
|
- self.assertIn(
|
117
|
|
- 'selfoss',
|
118
|
|
- r.content
|
119
|
|
- )
|
120
|
|
- self.assertIn(
|
121
|
|
- 'login',
|
122
|
|
- r.content
|
123
|
|
- )
|
124
|
|
-
|
125
|
|
-
|
126
|
|
-class IRCTests(unittest.TestCase):
|
127
|
|
- def test_irc_auth(self):
|
128
|
|
- """ZNC is accepting encrypted logins"""
|
129
|
|
- import ssl
|
130
|
|
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
131
|
|
- ssl_sock = ssl.wrap_socket(s, ca_certs=CA_BUNDLE, cert_reqs=ssl.CERT_REQUIRED)
|
132
|
|
- ssl_sock.connect((TEST_SERVER, 6697))
|
133
|
|
-
|
134
|
|
- # Check the encryption parameters
|
135
|
|
- cipher, version, bits = ssl_sock.cipher()
|
136
|
|
- self.assertEquals(cipher, 'AES256-GCM-SHA384')
|
137
|
|
- self.assertEquals(version, 'TLSv1/SSLv3')
|
138
|
|
- self.assertEquals(bits, 256)
|
139
|
|
-
|
140
|
|
- # Login
|
141
|
|
- ssl_sock.send('CAP REQ sasl multi-prefix\r\n')
|
142
|
|
- ssl_sock.send('PASS foo\r\n')
|
143
|
|
- ssl_sock.send('NICK sovereign\r\n')
|
144
|
|
- ssl_sock.send('USER sovereign 0 * Sov\r\n')
|
145
|
|
-
|
146
|
|
- # Read until we see the ZNC banner (or timeout)
|
147
|
|
- while 1:
|
148
|
|
- r = ssl_sock.recv(1024)
|
149
|
|
- if 'Connected to ZNC' in r:
|
150
|
|
- break
|
151
|
|
-
|
152
|
|
-
|
153
|
|
-def new_message(from_email, to_email):
|
154
|
|
- """Creates an email (headers & body) with a random subject"""
|
155
|
|
- from email.mime.text import MIMEText
|
156
|
|
- msg = MIMEText('Testing')
|
157
|
|
- msg['Subject'] = uuid.uuid4().hex[:8]
|
158
|
|
- msg['From'] = from_email
|
159
|
|
- msg['To'] = to_email
|
160
|
|
- return msg.as_string(), msg['subject']
|
161
|
|
-
|
162
|
|
-
|
163
|
|
-class MailTests(unittest.TestCase):
|
164
|
|
- def assertIMAPReceived(self, subject):
|
165
|
|
- """Connects with IMAP and asserts the existence of an email, then deletes it"""
|
166
|
|
- import imaplib
|
167
|
|
-
|
168
|
|
- sleep(1)
|
169
|
|
-
|
170
|
|
- # Login to IMAP
|
171
|
|
- m = imaplib.IMAP4_SSL(TEST_SERVER, 993)
|
172
|
|
- m.login(TEST_ADDRESS, TEST_PASSWORD)
|
173
|
|
- m.select()
|
174
|
|
-
|
175
|
|
- # Assert the message exists
|
176
|
|
- typ, data = m.search(None, '(SUBJECT \"{}\")'.format(subject))
|
177
|
|
- self.assertTrue(len(data[0].split()), 1)
|
178
|
|
-
|
179
|
|
- # Delete it & logout
|
180
|
|
- m.store(data[0].strip(), '+FLAGS', '\\Deleted')
|
181
|
|
- m.expunge()
|
182
|
|
- m.close()
|
183
|
|
- m.logout()
|
184
|
|
-
|
185
|
|
- def assertPOP3Received(self, subject):
|
186
|
|
- """Connects with POP3S and asserts the existence of an email, then deletes it"""
|
187
|
|
- import poplib
|
188
|
|
-
|
189
|
|
- sleep(1)
|
190
|
|
-
|
191
|
|
- # Login to POP3
|
192
|
|
- mail = poplib.POP3_SSL(TEST_SERVER, 995)
|
193
|
|
- mail.user(TEST_ADDRESS)
|
194
|
|
- mail.pass_(TEST_PASSWORD)
|
195
|
|
-
|
196
|
|
- # Assert the message exists
|
197
|
|
- num = len(mail.list()[1])
|
198
|
|
- resp, text, octets = mail.retr(num)
|
199
|
|
- self.assertTrue("Subject: " + subject in text)
|
200
|
|
-
|
201
|
|
- # Delete it and log out
|
202
|
|
- mail.dele(num)
|
203
|
|
- mail.quit()
|
204
|
|
-
|
205
|
|
- def test_imap_requires_ssl(self):
|
206
|
|
- """IMAP without SSL is NOT available"""
|
207
|
|
- import imaplib
|
208
|
|
-
|
209
|
|
- with self.assertRaisesRegexp(socket.timeout, 'timed out'):
|
210
|
|
- imaplib.IMAP4(TEST_SERVER, 143)
|
211
|
|
-
|
212
|
|
- def test_pop3_requires_ssl(self):
|
213
|
|
- """POP3 without SSL is NOT available"""
|
214
|
|
- import poplib
|
215
|
|
-
|
216
|
|
- with self.assertRaisesRegexp(socket.timeout, 'timed out'):
|
217
|
|
- poplib.POP3(TEST_SERVER, 110)
|
218
|
|
-
|
219
|
|
- def test_smtps(self):
|
220
|
|
- """Email sent from an MUA via SMTPS is delivered"""
|
221
|
|
- import smtplib
|
222
|
|
- msg, subject = new_message(TEST_ADDRESS, 'root@sovereign.local')
|
223
|
|
- s = smtplib.SMTP_SSL(TEST_SERVER, 465)
|
224
|
|
- s.login(TEST_ADDRESS, TEST_PASSWORD)
|
225
|
|
- s.sendmail(TEST_ADDRESS, ['root@sovereign.local'], msg)
|
226
|
|
- s.quit()
|
227
|
|
- self.assertIMAPReceived(subject)
|
228
|
|
-
|
229
|
|
- def test_smtps_delimiter_to(self):
|
230
|
|
- """Email sent to address with delimiter is delivered"""
|
231
|
|
- import smtplib
|
232
|
|
- msg, subject = new_message(TEST_ADDRESS, 'root+foo@sovereign.local')
|
233
|
|
- s = smtplib.SMTP_SSL(TEST_SERVER, 465)
|
234
|
|
- s.login(TEST_ADDRESS, TEST_PASSWORD)
|
235
|
|
- s.sendmail(TEST_ADDRESS, ['root+foo@sovereign.local'], msg)
|
236
|
|
- s.quit()
|
237
|
|
- self.assertIMAPReceived(subject)
|
238
|
|
-
|
239
|
|
- def test_smtps_requires_auth(self):
|
240
|
|
- """SMTPS with no authentication is rejected"""
|
241
|
|
- import smtplib
|
242
|
|
- s = smtplib.SMTP_SSL(TEST_SERVER, 465)
|
243
|
|
-
|
244
|
|
- with self.assertRaisesRegexp(smtplib.SMTPRecipientsRefused, 'Access denied'):
|
245
|
|
- s.sendmail(TEST_ADDRESS, ['root@sovereign.local'], 'Test')
|
246
|
|
-
|
247
|
|
- s.quit()
|
248
|
|
-
|
249
|
|
- def test_smtp(self):
|
250
|
|
- """Email sent from an MTA is delivered"""
|
251
|
|
- import smtplib
|
252
|
|
- msg, subject = new_message('someone@example.com', TEST_ADDRESS)
|
253
|
|
- s = smtplib.SMTP(TEST_SERVER, 25)
|
254
|
|
- s.sendmail('someone@example.com', [TEST_ADDRESS], msg)
|
255
|
|
- s.quit()
|
256
|
|
- self.assertIMAPReceived(subject)
|
257
|
|
-
|
258
|
|
- def test_smtp_tls(self):
|
259
|
|
- """Email sent from an MTA via SMTP+TLS is delivered"""
|
260
|
|
- import smtplib
|
261
|
|
- msg, subject = new_message('someone@example.com', TEST_ADDRESS)
|
262
|
|
- s = smtplib.SMTP(TEST_SERVER, 25)
|
263
|
|
- s.starttls()
|
264
|
|
- s.sendmail('someone@example.com', [TEST_ADDRESS], msg)
|
265
|
|
- s.quit()
|
266
|
|
- self.assertIMAPReceived(subject)
|
267
|
|
-
|
268
|
|
- def test_smtps_headers(self):
|
269
|
|
- """Email sent from an MUA has DKIM and TLS headers"""
|
270
|
|
- import smtplib
|
271
|
|
- import imaplib
|
272
|
|
-
|
273
|
|
- # Send a message to root
|
274
|
|
- msg, subject = new_message(TEST_ADDRESS, 'root@sovereign.local')
|
275
|
|
- s = smtplib.SMTP_SSL(TEST_SERVER, 465)
|
276
|
|
- s.login(TEST_ADDRESS, TEST_PASSWORD)
|
277
|
|
- s.sendmail(TEST_ADDRESS, ['root@sovereign.local'], msg)
|
278
|
|
- s.quit()
|
279
|
|
-
|
280
|
|
- sleep(1)
|
281
|
|
-
|
282
|
|
- # Get the message
|
283
|
|
- m = imaplib.IMAP4_SSL(TEST_SERVER, 993)
|
284
|
|
- m.login(TEST_ADDRESS, TEST_PASSWORD)
|
285
|
|
- m.select()
|
286
|
|
- _, res = m.search(None, '(SUBJECT \"{}\")'.format(subject))
|
287
|
|
- _, data = m.fetch(res[0], '(RFC822)')
|
288
|
|
-
|
289
|
|
- self.assertIn(
|
290
|
|
- 'DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sovereign.local;',
|
291
|
|
- data[0][1]
|
292
|
|
- )
|
293
|
|
-
|
294
|
|
- self.assertIn(
|
295
|
|
- 'ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)',
|
296
|
|
- data[0][1]
|
297
|
|
- )
|
298
|
|
-
|
299
|
|
- # Clean up
|
300
|
|
- m.store(res[0].strip(), '+FLAGS', '\\Deleted')
|
301
|
|
- m.expunge()
|
302
|
|
- m.close()
|
303
|
|
- m.logout()
|
304
|
|
-
|
305
|
|
- def test_smtp_headers(self):
|
306
|
|
- """Email sent from an MTA via SMTP+TLS has TLS headers"""
|
307
|
|
- import smtplib
|
308
|
|
- import imaplib
|
309
|
|
-
|
310
|
|
- # Send a message to root
|
311
|
|
- msg, subject = new_message('someone@example.com', TEST_ADDRESS)
|
312
|
|
- s = smtplib.SMTP(TEST_SERVER, 25)
|
313
|
|
- s.starttls()
|
314
|
|
- s.sendmail('someone@example.com', [TEST_ADDRESS], msg)
|
315
|
|
- s.quit()
|
316
|
|
-
|
317
|
|
- sleep(1)
|
318
|
|
-
|
319
|
|
- # Get the message
|
320
|
|
- m = imaplib.IMAP4_SSL(TEST_SERVER, 993)
|
321
|
|
- m.login(TEST_ADDRESS, TEST_PASSWORD)
|
322
|
|
- m.select()
|
323
|
|
- _, res = m.search(None, '(SUBJECT \"{}\")'.format(subject))
|
324
|
|
- _, data = m.fetch(res[0], '(RFC822)')
|
325
|
|
-
|
326
|
|
- self.assertIn(
|
327
|
|
- 'ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)',
|
328
|
|
- data[0][1]
|
329
|
|
- )
|
330
|
|
-
|
331
|
|
- # Clean up
|
332
|
|
- m.store(res[0].strip(), '+FLAGS', '\\Deleted')
|
333
|
|
- m.expunge()
|
334
|
|
- m.close()
|
335
|
|
- m.logout()
|
336
|
|
-
|
337
|
|
- def test_pop3s(self):
|
338
|
|
- """Connects with POP3S and asserts the existance of an email, then deletes it"""
|
339
|
|
- import smtplib
|
340
|
|
- msg, subject = new_message(TEST_ADDRESS, 'root@sovereign.local')
|
341
|
|
- s = smtplib.SMTP_SSL(TEST_SERVER, 465)
|
342
|
|
- s.login(TEST_ADDRESS, TEST_PASSWORD)
|
343
|
|
- s.sendmail(TEST_ADDRESS, ['root@sovereign.local'], msg)
|
344
|
|
- s.quit()
|
345
|
|
- self.assertPOP3Received(subject)
|
346
|
|
-
|
347
|
|
-
|
348
|
|
-class XMPPTests(unittest.TestCase):
|
349
|
|
- def test_xmpp_c2s(self):
|
350
|
|
- """Prosody is listening on 5222 for clients and requiring TLS"""
|
351
|
|
-
|
352
|
|
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
353
|
|
- s.connect((TEST_SERVER, 5222))
|
354
|
|
-
|
355
|
|
- # Based off http://wiki.xmpp.org/web/Programming_Jabber_Clients
|
356
|
|
- s.send("<stream:stream xmlns:stream='http://etherx.jabber.org/streams' "
|
357
|
|
- "xmlns='jabber:client' to='sovereign.local' version='1.0'>")
|
358
|
|
-
|
359
|
|
- data = s.recv(1024)
|
360
|
|
- s.close()
|
361
|
|
-
|
362
|
|
- self.assertRegexpMatches(
|
363
|
|
- data,
|
364
|
|
- "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required/></starttls>"
|
365
|
|
- )
|
366
|
|
-
|
367
|
|
- def test_xmpp_s2s(self):
|
368
|
|
- """Prosody is listening on 5269 for servers"""
|
369
|
|
-
|
370
|
|
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
371
|
|
- s.connect((TEST_SERVER, 5269))
|
372
|
|
-
|
373
|
|
- # Base off http://xmpp.org/extensions/xep-0114.html
|
374
|
|
- s.send("<stream:stream xmlns:stream='http://etherx.jabber.org/streams' "
|
375
|
|
- "xmlns='jabber:component:accept' to='sovereign.local'>")
|
376
|
|
-
|
377
|
|
- data = s.recv(1024)
|
378
|
|
- s.close()
|
379
|
|
-
|
380
|
|
- self.assertRegexpMatches(
|
381
|
|
- data,
|
382
|
|
- "from='sovereign.local'"
|
383
|
|
- )
|