forked from sovereign/sovereign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
398 lines (316 loc) · 13 KB
/
tests.py
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import unittest
from time import sleep
import uuid
import socket
import requests
import os
TEST_SERVER = 'sovereign.local'
TEST_ADDRESS = 'sovereign@sovereign.local'
TEST_PASSWORD = 'foo'
CA_BUNDLE = 'roles/common/files/wildcard_ca.pem'
socket.setdefaulttimeout(5)
os.environ['REQUESTS_CA_BUNDLE'] = CA_BUNDLE
class SSHTests(unittest.TestCase):
def test_ssh_banner(self):
"""SSH is responding with its banner"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TEST_SERVER, 22))
data = s.recv(1024)
s.close()
self.assertRegexpMatches(data, '^SSH-2.0-OpenSSH')
class WebTests(unittest.TestCase):
def test_blog_http(self):
"""Blog is redirecting to https"""
# FIXME: requests won't verify sovereign.local with *.sovereign.local cert
r = requests.get('http://' + TEST_SERVER, verify=False)
# We should be redirected to https
self.assertEquals(r.history[0].status_code, 301)
self.assertEquals(r.url, 'https://' + TEST_SERVER + '/')
# 403 - Since there is no documents in the blog directory
self.assertEquals(r.status_code, 403)
def test_mail_autoconfig_http_and_https(self):
"""Email autoconfiguration XML file is accessible over both HTTP and HTTPS"""
# Test getting the file over HTTP and HTTPS
for proto in ['http', 'https']:
r = requests.get(proto + '://autoconfig.' + TEST_SERVER + '/mail/config-v1.1.xml')
# 200 - We should see the XML file
self.assertEquals(r.status_code, 200)
self.assertIn('application/xml', r.headers['Content-Type'])
self.assertIn('clientConfig version="1.1"', r.content)
def test_webmail_http(self):
"""Webmail is redirecting to https and displaying login page"""
r = requests.get('http://mail.' + TEST_SERVER)
# We should be redirected to https
self.assertEquals(r.history[0].status_code, 301)
self.assertEquals(r.url, 'https://mail.' + TEST_SERVER + '/')
# 200 - We should be at the login page
self.assertEquals(r.status_code, 200)
self.assertIn(
'Welcome to Roundcube Webmail',
r.content
)
def test_zpush_http_unauthorized(self):
r = requests.get('http://mail.' + TEST_SERVER + '/Microsoft-Server-ActiveSync')
# We should be redirected to https
self.assertEquals(r.history[0].status_code, 301)
self.assertEquals(r.url, 'https://mail.' + TEST_SERVER + '/Microsoft-Server-ActiveSync')
# Unauthorized
self.assertEquals(r.status_code, 401)
def test_zpush_https(self):
r = requests.post('https://mail.' + TEST_SERVER + '/Microsoft-Server-ActiveSync',
auth=('sovereign@sovereign.local', 'foo'),
params={
'DeviceId': '1234',
'DeviceType': 'testbot',
'Cmd': 'Ping',
})
self.assertEquals(r.headers['content-type'],
'application/vnd.ms-sync.wbxml')
self.assertEquals(r.headers['ms-server-activesync'],
'14.0')
def test_owncloud_http(self):
"""ownCloud is redirecting to https and displaying login page"""
r = requests.get('http://cloud.' + TEST_SERVER)
# We should be redirected to https
self.assertEquals(r.history[0].status_code, 301)
self.assertEquals(r.url, 'https://cloud.' + TEST_SERVER + '/')
# 200 - We should be at the login page
self.assertEquals(r.status_code, 200)
self.assertIn(
'ownCloud',
r.content
)
def test_selfoss_http(self):
"""selfoss is redirecting to https and displaying login page"""
r = requests.get('http://news.' + TEST_SERVER)
# We should be redirected to https
self.assertEquals(r.history[0].status_code, 301)
self.assertEquals(r.url, 'https://news.' + TEST_SERVER + '/')
# 200 - We should be at the login page
self.assertEquals(r.status_code, 200)
self.assertIn(
'selfoss',
r.content
)
self.assertIn(
'login',
r.content
)
def test_cgit_http(self):
"""CGit web interface is displaying home page"""
r = requests.get('http://git.' + TEST_SERVER, verify=False)
# We should be redirected to https
self.assertEquals(r.history[0].status_code, 301)
self.assertEquals(r.url, 'https://git.' + TEST_SERVER + '/')
# 200 - We should be at the repository page
self.assertEquals(r.status_code, 200)
self.assertIn(
'git repository',
r.content
)
class IRCTests(unittest.TestCase):
def test_irc_auth(self):
"""ZNC is accepting encrypted logins"""
import ssl
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_sock = ssl.wrap_socket(s, ca_certs=CA_BUNDLE, cert_reqs=ssl.CERT_REQUIRED)
ssl_sock.connect((TEST_SERVER, 6697))
# Check the encryption parameters
cipher, version, bits = ssl_sock.cipher()
self.assertEquals(cipher, 'AES256-GCM-SHA384')
self.assertEquals(version, 'TLSv1/SSLv3')
self.assertEquals(bits, 256)
# Login
ssl_sock.send('CAP REQ sasl multi-prefix\r\n')
ssl_sock.send('PASS foo\r\n')
ssl_sock.send('NICK sovereign\r\n')
ssl_sock.send('USER sovereign 0 * Sov\r\n')
# Read until we see the ZNC banner (or timeout)
while 1:
r = ssl_sock.recv(1024)
if 'Connected to ZNC' in r:
break
def new_message(from_email, to_email):
"""Creates an email (headers & body) with a random subject"""
from email.mime.text import MIMEText
msg = MIMEText('Testing')
msg['Subject'] = uuid.uuid4().hex[:8]
msg['From'] = from_email
msg['To'] = to_email
return msg.as_string(), msg['subject']
class MailTests(unittest.TestCase):
def assertIMAPReceived(self, subject):
"""Connects with IMAP and asserts the existence of an email, then deletes it"""
import imaplib
sleep(1)
# Login to IMAP
m = imaplib.IMAP4_SSL(TEST_SERVER, 993)
m.login(TEST_ADDRESS, TEST_PASSWORD)
m.select()
# Assert the message exists
typ, data = m.search(None, '(SUBJECT \"{}\")'.format(subject))
self.assertTrue(len(data[0].split()), 1)
# Delete it & logout
m.store(data[0].strip(), '+FLAGS', '\\Deleted')
m.expunge()
m.close()
m.logout()
def assertPOP3Received(self, subject):
"""Connects with POP3S and asserts the existence of an email, then deletes it"""
import poplib
sleep(1)
# Login to POP3
mail = poplib.POP3_SSL(TEST_SERVER, 995)
mail.user(TEST_ADDRESS)
mail.pass_(TEST_PASSWORD)
# Assert the message exists
num = len(mail.list()[1])
resp, text, octets = mail.retr(num)
self.assertTrue("Subject: " + subject in text)
# Delete it and log out
mail.dele(num)
mail.quit()
def test_imap_requires_ssl(self):
"""IMAP without SSL is NOT available"""
import imaplib
with self.assertRaisesRegexp(socket.timeout, 'timed out'):
imaplib.IMAP4(TEST_SERVER, 143)
def test_pop3_requires_ssl(self):
"""POP3 without SSL is NOT available"""
import poplib
with self.assertRaisesRegexp(socket.timeout, 'timed out'):
poplib.POP3(TEST_SERVER, 110)
def test_smtps(self):
"""Email sent from an MUA via SMTPS is delivered"""
import smtplib
msg, subject = new_message(TEST_ADDRESS, 'root@sovereign.local')
s = smtplib.SMTP_SSL(TEST_SERVER, 465)
s.login(TEST_ADDRESS, TEST_PASSWORD)
s.sendmail(TEST_ADDRESS, ['root@sovereign.local'], msg)
s.quit()
self.assertIMAPReceived(subject)
def test_smtps_delimiter_to(self):
"""Email sent to address with delimiter is delivered"""
import smtplib
msg, subject = new_message(TEST_ADDRESS, 'root+foo@sovereign.local')
s = smtplib.SMTP_SSL(TEST_SERVER, 465)
s.login(TEST_ADDRESS, TEST_PASSWORD)
s.sendmail(TEST_ADDRESS, ['root+foo@sovereign.local'], msg)
s.quit()
self.assertIMAPReceived(subject)
def test_smtps_requires_auth(self):
"""SMTPS with no authentication is rejected"""
import smtplib
s = smtplib.SMTP_SSL(TEST_SERVER, 465)
with self.assertRaisesRegexp(smtplib.SMTPRecipientsRefused, 'Access denied'):
s.sendmail(TEST_ADDRESS, ['root@sovereign.local'], 'Test')
s.quit()
def test_smtp(self):
"""Email sent from an MTA is delivered"""
import smtplib
msg, subject = new_message('someone@example.com', TEST_ADDRESS)
s = smtplib.SMTP(TEST_SERVER, 25)
s.sendmail('someone@example.com', [TEST_ADDRESS], msg)
s.quit()
self.assertIMAPReceived(subject)
def test_smtp_tls(self):
"""Email sent from an MTA via SMTP+TLS is delivered"""
import smtplib
msg, subject = new_message('someone@example.com', TEST_ADDRESS)
s = smtplib.SMTP(TEST_SERVER, 25)
s.starttls()
s.sendmail('someone@example.com', [TEST_ADDRESS], msg)
s.quit()
self.assertIMAPReceived(subject)
def test_smtps_headers(self):
"""Email sent from an MUA has DKIM and TLS headers"""
import smtplib
import imaplib
# Send a message to root
msg, subject = new_message(TEST_ADDRESS, 'root@sovereign.local')
s = smtplib.SMTP_SSL(TEST_SERVER, 465)
s.login(TEST_ADDRESS, TEST_PASSWORD)
s.sendmail(TEST_ADDRESS, ['root@sovereign.local'], msg)
s.quit()
sleep(1)
# Get the message
m = imaplib.IMAP4_SSL(TEST_SERVER, 993)
m.login(TEST_ADDRESS, TEST_PASSWORD)
m.select()
_, res = m.search(None, '(SUBJECT \"{}\")'.format(subject))
_, data = m.fetch(res[0], '(RFC822)')
self.assertIn(
'DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sovereign.local;',
data[0][1]
)
self.assertIn(
'ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)',
data[0][1]
)
# Clean up
m.store(res[0].strip(), '+FLAGS', '\\Deleted')
m.expunge()
m.close()
m.logout()
def test_smtp_headers(self):
"""Email sent from an MTA via SMTP+TLS has TLS headers"""
import smtplib
import imaplib
# Send a message to root
msg, subject = new_message('someone@example.com', TEST_ADDRESS)
s = smtplib.SMTP(TEST_SERVER, 25)
s.starttls()
s.sendmail('someone@example.com', [TEST_ADDRESS], msg)
s.quit()
sleep(1)
# Get the message
m = imaplib.IMAP4_SSL(TEST_SERVER, 993)
m.login(TEST_ADDRESS, TEST_PASSWORD)
m.select()
_, res = m.search(None, '(SUBJECT \"{}\")'.format(subject))
_, data = m.fetch(res[0], '(RFC822)')
self.assertIn(
'ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)',
data[0][1]
)
# Clean up
m.store(res[0].strip(), '+FLAGS', '\\Deleted')
m.expunge()
m.close()
m.logout()
def test_pop3s(self):
"""Connects with POP3S and asserts the existance of an email, then deletes it"""
import smtplib
msg, subject = new_message(TEST_ADDRESS, 'root@sovereign.local')
s = smtplib.SMTP_SSL(TEST_SERVER, 465)
s.login(TEST_ADDRESS, TEST_PASSWORD)
s.sendmail(TEST_ADDRESS, ['root@sovereign.local'], msg)
s.quit()
self.assertPOP3Received(subject)
class XMPPTests(unittest.TestCase):
def test_xmpp_c2s(self):
"""Prosody is listening on 5222 for clients and requiring TLS"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TEST_SERVER, 5222))
# Based off http://wiki.xmpp.org/web/Programming_Jabber_Clients
s.send("<stream:stream xmlns:stream='http://etherx.jabber.org/streams' "
"xmlns='jabber:client' to='sovereign.local' version='1.0'>")
data = s.recv(1024)
s.close()
self.assertRegexpMatches(
data,
"<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required/></starttls>"
)
def test_xmpp_s2s(self):
"""Prosody is listening on 5269 for servers"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TEST_SERVER, 5269))
# Base off http://xmpp.org/extensions/xep-0114.html
s.send("<stream:stream xmlns:stream='http://etherx.jabber.org/streams' "
"xmlns='jabber:component:accept' to='sovereign.local'>")
data = s.recv(1024)
s.close()
self.assertRegexpMatches(
data,
"from='sovereign.local'"
)