Alex Yu | a780bf5 | 2016-06-15 01:20:25 -0400 | [diff] [blame^] | 1 | """Tests for SSL handling in httplib2.""" |
| 2 | |
| 3 | import httplib2 |
| 4 | import os |
| 5 | import ssl |
| 6 | import sys |
| 7 | import unittest |
| 8 | |
| 9 | |
| 10 | class TestSslProtocol(unittest.TestCase): |
| 11 | |
| 12 | def testSslCertValidationWithInvalidCaCert(self): |
| 13 | if sys.version_info >= (2, 6): |
| 14 | http = httplib2.Http(ca_certs='/nosuchfile') |
| 15 | if sys.version_info >= (2, 7): |
| 16 | with self.assertRaises(IOError): |
| 17 | http.request('https://www.google.com/', 'GET') |
| 18 | else: |
| 19 | self.assertRaises( |
| 20 | ssl.SSLError, http.request, 'https://www.google.com/', 'GET') |
| 21 | |
| 22 | def testSslCertValidationWithSelfSignedCaCert(self): |
| 23 | if sys.version_info >= (2, 7): |
| 24 | other_ca_certs = os.path.join( |
| 25 | os.path.dirname(os.path.abspath(httplib2.__file__ )), 'test', |
| 26 | 'other_cacerts.txt') |
| 27 | http = httplib2.Http(ca_certs=other_ca_certs) |
| 28 | if sys.platform != 'darwin': |
| 29 | with self.assertRaises(httplib2.SSLHandshakeError): |
| 30 | http.request('https://www.google.com/', 'GET') |
| 31 | |
| 32 | def testSslProtocolTlsV1AndShouldPass(self): |
| 33 | http = httplib2.Http(ssl_version=ssl.PROTOCOL_TLSv1) |
| 34 | urls = ['https://www.amazon.com', |
| 35 | 'https://www.apple.com', |
| 36 | 'https://www.twitter.com'] |
| 37 | for url in urls: |
| 38 | if sys.version_info >= (2, 7): |
| 39 | self.assertIsNotNone(http.request(uri=url)) |
| 40 | |
| 41 | def testSslProtocolV3AndShouldFailDueToPoodle(self): |
| 42 | http = httplib2.Http(ssl_version=ssl.PROTOCOL_SSLv3) |
| 43 | urls = ['https://www.amazon.com', |
| 44 | 'https://www.apple.com', |
| 45 | 'https://www.twitter.com'] |
| 46 | for url in urls: |
| 47 | if sys.version_info >= (2, 7): |
| 48 | with self.assertRaises(httplib2.SSLHandshakeError): |
| 49 | http.request(url) |
| 50 | try: |
| 51 | http.request(url) |
| 52 | except httplib2.SSLHandshakeError as e: |
| 53 | self.assertTrue('sslv3 alert handshake failure' in str(e)) |
| 54 | |
| 55 | |
| 56 | if __name__ == '__main__': |
| 57 | unittest.main() |