1"""Tests for SSL handling in httplib2.""" 2 3import httplib2 4import os 5import ssl 6import sys 7import unittest 8 9 10class TestSslProtocol(unittest.TestCase): 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 ) 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__)), 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") 33 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)) 44 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)) 60 61 62if __name__ == "__main__": 63 unittest.main() 64