Ingen beskrivning
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

tests.py 13KB

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