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): |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame] | 11 | def testSslCertValidationWithInvalidCaCert(self): |
| 12 | if sys.version_info >= (2, 6): |
| 13 | http = httplib2.Http(ca_certs="/nosuchfile") |
| 14 | if sys.version_info >= (2, 7): |
| 15 | with self.assertRaises(IOError): |
| 16 | http.request("https://www.google.com/", "GET") |
| 17 | else: |
| 18 | self.assertRaises( |
| 19 | ssl.SSLError, http.request, "https://www.google.com/", "GET" |
| 20 | ) |
Alex Yu | a780bf5 | 2016-06-15 01:20:25 -0400 | [diff] [blame] | 21 | |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame] | 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__)), |
| 26 | "test", |
| 27 | "other_cacerts.txt", |
| 28 | ) |
| 29 | http = httplib2.Http(ca_certs=other_ca_certs) |
| 30 | if sys.platform != "darwin": |
| 31 | with self.assertRaises(httplib2.SSLHandshakeError): |
| 32 | http.request("https://www.google.com/", "GET") |
Alex Yu | a780bf5 | 2016-06-15 01:20:25 -0400 | [diff] [blame] | 33 | |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame] | 34 | def testSslProtocolTlsV1AndShouldPass(self): |
| 35 | http = httplib2.Http(ssl_version=ssl.PROTOCOL_TLSv1) |
| 36 | urls = [ |
| 37 | "https://www.amazon.com", |
| 38 | "https://www.apple.com", |
| 39 | "https://www.twitter.com", |
| 40 | ] |
| 41 | for url in urls: |
| 42 | if sys.version_info >= (2, 7): |
| 43 | self.assertIsNotNone(http.request(uri=url)) |
Alex Yu | a780bf5 | 2016-06-15 01:20:25 -0400 | [diff] [blame] | 44 | |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame] | 45 | def testSslProtocolV3AndShouldFailDueToPoodle(self): |
| 46 | http = httplib2.Http(ssl_version=ssl.PROTOCOL_SSLv3) |
| 47 | urls = [ |
| 48 | "https://www.amazon.com", |
| 49 | "https://www.apple.com", |
| 50 | "https://www.twitter.com", |
| 51 | ] |
| 52 | for url in urls: |
| 53 | if sys.version_info >= (2, 7): |
| 54 | with self.assertRaises(httplib2.SSLHandshakeError): |
| 55 | http.request(url) |
| 56 | try: |
| 57 | http.request(url) |
| 58 | except httplib2.SSLHandshakeError as e: |
| 59 | self.assertTrue("sslv3 alert handshake failure" in str(e)) |
Alex Yu | a780bf5 | 2016-06-15 01:20:25 -0400 | [diff] [blame] | 60 | |
| 61 | |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame] | 62 | if __name__ == "__main__": |
| 63 | unittest.main() |