test_https.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. """unit tests module for ndg.httpsclient.https.HTTPSconnection class
  2. PyOpenSSL utility to make a httplib-like interface suitable for use with
  3. urllib2
  4. """
  5. __author__ = "P J Kershaw (STFC)"
  6. __date__ = "06/01/12"
  7. __copyright__ = "(C) 2012 Science and Technology Facilities Council"
  8. __license__ = "BSD - see LICENSE file in top-level directory"
  9. __contact__ = "Philip.Kershaw@stfc.ac.uk"
  10. __revision__ = '$Id$'
  11. import logging
  12. logging.basicConfig(level=logging.DEBUG)
  13. log = logging.getLogger(__name__)
  14. import unittest
  15. import socket
  16. from OpenSSL import SSL
  17. from ndg.httpsclient.test import Constants
  18. from ndg.httpsclient.https import HTTPSConnection
  19. from ndg.httpsclient.ssl_peer_verification import ServerSSLCertVerification
  20. class TestHTTPSConnection(unittest.TestCase):
  21. '''Test ndg HTTPS client HTTPSConnection class'''
  22. def test01_open(self):
  23. conn = HTTPSConnection(Constants.HOSTNAME, port=Constants.PORT)
  24. conn.connect()
  25. conn.request('GET', '/')
  26. resp = conn.getresponse()
  27. print('Response = %s' % resp.read())
  28. conn.close()
  29. def test02_open_fails(self):
  30. conn = HTTPSConnection(Constants.HOSTNAME, port=Constants.PORT2)
  31. self.failUnlessRaises(socket.error, conn.connect)
  32. def test03_ssl_verification_of_peer_fails(self):
  33. ctx = SSL.Context(SSL.SSLv3_METHOD)
  34. def verify_callback(conn, x509, errnum, errdepth, preverify_ok):
  35. log.debug('SSL peer certificate verification failed for %r',
  36. x509.get_subject())
  37. return preverify_ok
  38. ctx.set_verify(SSL.VERIFY_PEER, verify_callback)
  39. ctx.set_verify_depth(9)
  40. # Set bad location - unit test dir has no CA certs to verify with
  41. ctx.load_verify_locations(None, Constants.UNITTEST_DIR)
  42. conn = HTTPSConnection(Constants.HOSTNAME, port=Constants.PORT,
  43. ssl_context=ctx)
  44. conn.connect()
  45. self.failUnlessRaises(SSL.Error, conn.request, 'GET', '/')
  46. def test03_ssl_verification_of_peer_succeeds(self):
  47. ctx = SSL.Context(SSL.SSLv3_METHOD)
  48. verify_callback = lambda conn, x509, errnum, errdepth, preverify_ok: \
  49. preverify_ok
  50. ctx.set_verify(SSL.VERIFY_PEER, verify_callback)
  51. ctx.set_verify_depth(9)
  52. # Set correct location for CA certs to verify with
  53. ctx.load_verify_locations(None, Constants.CACERT_DIR)
  54. conn = HTTPSConnection(Constants.HOSTNAME, port=Constants.PORT,
  55. ssl_context=ctx)
  56. conn.connect()
  57. conn.request('GET', '/')
  58. resp = conn.getresponse()
  59. print('Response = %s' % resp.read())
  60. def test04_ssl_verification_with_subj_alt_name(self):
  61. ctx = SSL.Context(SSL.SSLv3_METHOD)
  62. verify_callback = ServerSSLCertVerification(hostname='localhost')
  63. ctx.set_verify(SSL.VERIFY_PEER, verify_callback)
  64. ctx.set_verify_depth(9)
  65. # Set correct location for CA certs to verify with
  66. ctx.load_verify_locations(None, Constants.CACERT_DIR)
  67. conn = HTTPSConnection(Constants.HOSTNAME, port=Constants.PORT,
  68. ssl_context=ctx)
  69. conn.connect()
  70. conn.request('GET', '/')
  71. resp = conn.getresponse()
  72. print('Response = %s' % resp.read())
  73. def test04_ssl_verification_with_subj_common_name(self):
  74. ctx = SSL.Context(SSL.SSLv3_METHOD)
  75. # Explicitly set verification of peer hostname using peer certificate
  76. # subject common name
  77. verify_callback = ServerSSLCertVerification(hostname='localhost',
  78. subj_alt_name_match=False)
  79. ctx.set_verify(SSL.VERIFY_PEER, verify_callback)
  80. ctx.set_verify_depth(9)
  81. # Set correct location for CA certs to verify with
  82. ctx.load_verify_locations(None, Constants.CACERT_DIR)
  83. conn = HTTPSConnection(Constants.HOSTNAME, port=Constants.PORT,
  84. ssl_context=ctx)
  85. conn.connect()
  86. conn.request('GET', '/')
  87. resp = conn.getresponse()
  88. print('Response = %s' % resp.read())
  89. if __name__ == "__main__":
  90. unittest.main()