This repository has been archived by the owner on Sep 15, 2021. It is now read-only.
forked from sovereign/sovereign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
298 lines (235 loc) · 9.3 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
import unittest
from time import sleep
import uuid
import socket
import requests
import os
TEST_SERVER = 'sovereign.local'
TEST_ADDRESS = '[email protected]'
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_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_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_znc_http(self):
"""ZNC web interface is displaying login page"""
# FIXME: requests won't verify sovereign.local with *.sovereign.local cert
r = requests.get('https://' + TEST_SERVER + ':6697', verify=False)
self.assertEquals(r.status_code, 200)
self.assertIn(
"Welcome to ZNC's web interface!",
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-SHA')
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 assertEmailReceived(self, subject):
"""Connects with IMAP and asserts the existance 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 exist
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 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_smtps(self):
"""Email sent from an MUA via SMTPS is delivered"""
import smtplib
msg, subject = new_message(TEST_ADDRESS, '[email protected]')
s = smtplib.SMTP_SSL(TEST_SERVER, 465)
s.login(TEST_ADDRESS, TEST_PASSWORD)
s.sendmail(TEST_ADDRESS, ['[email protected]'], msg)
s.quit()
self.assertEmailReceived(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, ['[email protected]'], 'Test')
s.quit()
def test_smtp(self):
"""Email sent from an MTA is delivered"""
import smtplib
msg, subject = new_message('[email protected]', TEST_ADDRESS)
s = smtplib.SMTP(TEST_SERVER, 25)
s.sendmail('[email protected]', [TEST_ADDRESS], msg)
s.quit()
self.assertEmailReceived(subject)
def test_smtp_tls(self):
"""Email sent from an MTA via SMTP+TLS is delivered"""
import smtplib
msg, subject = new_message('[email protected]', TEST_ADDRESS)
s = smtplib.SMTP(TEST_SERVER, 25)
s.starttls()
s.sendmail('[email protected]', [TEST_ADDRESS], msg)
s.quit()
self.assertEmailReceived(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, '[email protected]')
s = smtplib.SMTP_SSL(TEST_SERVER, 465)
s.login(TEST_ADDRESS, TEST_PASSWORD)
s.sendmail(TEST_ADDRESS, ['[email protected]'], 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(
'(using TLSv1 with cipher DHE-RSA-AES256-SHA (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 X-DSPAM and TLS headers"""
import smtplib
import imaplib
# Send a message to root
msg, subject = new_message('[email protected]', TEST_ADDRESS)
s = smtplib.SMTP(TEST_SERVER, 25)
s.starttls()
s.sendmail('[email protected]', [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(
'X-DSPAM-Result: Innocent',
data[0][1]
)
self.assertIn(
'(using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))',
data[0][1]
)
# Clean up
m.store(res[0].strip(), '+FLAGS', '\\Deleted')
m.expunge()
m.close()
m.logout()
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'"
)