Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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