Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(4)

Side by Side Diff: Lib/test/test_ssl.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_socketserver.py ('k') | Lib/test/test_subprocess.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Test the support for SSL and sockets 1 # Test the support for SSL and sockets
2 2
3 import sys 3 import sys
4 import unittest 4 import unittest
5 from test import support 5 from test import support
6 import socket 6 import socket
7 import select 7 import select
8 import time 8 import time
9 import gc 9 import gc
10 import os 10 import os
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 BADKEY = data_file("badkey.pem") 53 BADKEY = data_file("badkey.pem")
54 54
55 55
56 def handle_error(prefix): 56 def handle_error(prefix):
57 exc_format = ' '.join(traceback.format_exception(*sys.exc_info())) 57 exc_format = ' '.join(traceback.format_exception(*sys.exc_info()))
58 if support.verbose: 58 if support.verbose:
59 sys.stdout.write(prefix + exc_format) 59 sys.stdout.write(prefix + exc_format)
60 60
61 def can_clear_options(): 61 def can_clear_options():
62 # 0.9.8m or higher 62 # 0.9.8m or higher
63 return ssl._OPENSSL_API_VERSION >= (0, 9, 8, 13, 15) 63 return ssl.OPENSSL_VERSION_INFO >= (0, 9, 8, 13, 15)
64 64
65 def no_sslv2_implies_sslv3_hello(): 65 def no_sslv2_implies_sslv3_hello():
66 # 0.9.7h or higher 66 # 0.9.7h or higher
67 return ssl.OPENSSL_VERSION_INFO >= (0, 9, 7, 8, 15) 67 return ssl.OPENSSL_VERSION_INFO >= (0, 9, 7, 8, 15)
68 68
69 69
70 # Issue #9415: Ubuntu hijacks their OpenSSL and forcefully disables SSLv2 70 # Issue #9415: Ubuntu hijacks their OpenSSL and forcefully disables SSLv2
71 def skip_if_broken_ubuntu_ssl(func): 71 def skip_if_broken_ubuntu_ssl(func):
72 if hasattr(ssl, 'PROTOCOL_SSLv2'): 72 if hasattr(ssl, 'PROTOCOL_SSLv2'):
73 @functools.wraps(func) 73 @functools.wraps(func)
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 # Empty cert / no cert 313 # Empty cert / no cert
314 self.assertRaises(ValueError, ssl.match_hostname, None, 'example.com') 314 self.assertRaises(ValueError, ssl.match_hostname, None, 'example.com')
315 self.assertRaises(ValueError, ssl.match_hostname, {}, 'example.com') 315 self.assertRaises(ValueError, ssl.match_hostname, {}, 'example.com')
316 316
317 def test_server_side(self): 317 def test_server_side(self):
318 # server_hostname doesn't work for server sockets 318 # server_hostname doesn't work for server sockets
319 ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) 319 ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
320 with socket.socket() as sock: 320 with socket.socket() as sock:
321 self.assertRaises(ValueError, ctx.wrap_socket, sock, True, 321 self.assertRaises(ValueError, ctx.wrap_socket, sock, True,
322 server_hostname="some.hostname") 322 server_hostname="some.hostname")
323
324 def test_unknown_channel_binding(self):
325 # should raise ValueError for unknown type
326 s = socket.socket(socket.AF_INET)
327 ss = ssl.wrap_socket(s)
328 with self.assertRaises(ValueError):
329 ss.get_channel_binding("unknown-type")
330
331 @unittest.skipUnless("tls-unique" in ssl.CHANNEL_BINDING_TYPES,
332 "'tls-unique' channel binding not available")
333 def test_tls_unique_channel_binding(self):
334 # unconnected should return None for known type
335 s = socket.socket(socket.AF_INET)
336 ss = ssl.wrap_socket(s)
337 self.assertIsNone(ss.get_channel_binding("tls-unique"))
338 # the same for server-side
339 s = socket.socket(socket.AF_INET)
340 ss = ssl.wrap_socket(s, server_side=True, certfile=CERTFILE)
341 self.assertIsNone(ss.get_channel_binding("tls-unique"))
342 323
343 class ContextTests(unittest.TestCase): 324 class ContextTests(unittest.TestCase):
344 325
345 @skip_if_broken_ubuntu_ssl 326 @skip_if_broken_ubuntu_ssl
346 def test_constructor(self): 327 def test_constructor(self):
347 if hasattr(ssl, 'PROTOCOL_SSLv2'): 328 if hasattr(ssl, 'PROTOCOL_SSLv2'):
348 ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv2) 329 ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv2)
349 ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) 330 ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
350 ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv3) 331 ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv3)
351 ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1) 332 ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after
838 return 819 return
839 elif (self.server.starttls_server and self.sslconn 820 elif (self.server.starttls_server and self.sslconn
840 and stripped == b'ENDTLS'): 821 and stripped == b'ENDTLS'):
841 if support.verbose and self.server.connectionchatty: 822 if support.verbose and self.server.connectionchatty:
842 sys.stdout.write(" server: read ENDTLS from clie nt, sending OK...\n") 823 sys.stdout.write(" server: read ENDTLS from clie nt, sending OK...\n")
843 self.write(b"OK\n") 824 self.write(b"OK\n")
844 self.sock = self.sslconn.unwrap() 825 self.sock = self.sslconn.unwrap()
845 self.sslconn = None 826 self.sslconn = None
846 if support.verbose and self.server.connectionchatty: 827 if support.verbose and self.server.connectionchatty:
847 sys.stdout.write(" server: connection is now une ncrypted...\n") 828 sys.stdout.write(" server: connection is now une ncrypted...\n")
848 elif stripped == b'CB tls-unique':
849 if support.verbose and self.server.connectionchatty:
850 sys.stdout.write(" server: read CB tls-unique fr om client, sending our CB data...\n")
851 data = self.sslconn.get_channel_binding("tls-unique" )
852 self.write(repr(data).encode("us-ascii") + b"\n")
853 else: 829 else:
854 if (support.verbose and 830 if (support.verbose and
855 self.server.connectionchatty): 831 self.server.connectionchatty):
856 ctype = (self.sslconn and "encrypted") or "unenc rypted" 832 ctype = (self.sslconn and "encrypted") or "unenc rypted"
857 sys.stdout.write(" server: read %r (%s), sending back %r (%s)...\n" 833 sys.stdout.write(" server: read %r (%s), sending back %r (%s)...\n"
858 % (msg, ctype, msg.lower(), cty pe)) 834 % (msg, ctype, msg.lower(), cty pe))
859 self.write(msg.lower()) 835 self.write(msg.lower())
860 except socket.error: 836 except socket.error:
861 if self.server.chatty: 837 if self.server.chatty:
862 handle_error("Test server failure:\n") 838 handle_error("Test server failure:\n")
(...skipping 779 matching lines...) Expand 10 before | Expand all | Expand 10 after
1642 # Will attempt handshake and time out 1618 # Will attempt handshake and time out
1643 self.assertRaisesRegex(socket.timeout, "timed out", 1619 self.assertRaisesRegex(socket.timeout, "timed out",
1644 c.connect, (host, port)) 1620 c.connect, (host, port))
1645 finally: 1621 finally:
1646 c.close() 1622 c.close()
1647 finally: 1623 finally:
1648 finish = True 1624 finish = True
1649 t.join() 1625 t.join()
1650 server.close() 1626 server.close()
1651 1627
1652 @unittest.skipUnless("tls-unique" in ssl.CHANNEL_BINDING_TYPES,
1653 "'tls-unique' channel binding not available")
1654 def test_tls_unique_channel_binding(self):
1655 """Test tls-unique channel binding."""
1656 if support.verbose:
1657 sys.stdout.write("\n")
1658
1659 server = ThreadedEchoServer(CERTFILE,
1660 certreqs=ssl.CERT_NONE,
1661 ssl_version=ssl.PROTOCOL_TLSv1,
1662 cacerts=CERTFILE,
1663 chatty=True,
1664 connectionchatty=False)
1665 flag = threading.Event()
1666 server.start(flag)
1667 # wait for it to start
1668 flag.wait()
1669 # try to connect
1670 s = ssl.wrap_socket(socket.socket(),
1671 server_side=False,
1672 certfile=CERTFILE,
1673 ca_certs=CERTFILE,
1674 cert_reqs=ssl.CERT_NONE,
1675 ssl_version=ssl.PROTOCOL_TLSv1)
1676 s.connect((HOST, server.port))
1677 try:
1678 # get the data
1679 cb_data = s.get_channel_binding("tls-unique")
1680 if support.verbose:
1681 sys.stdout.write(" got channel binding data: {0!r}\n"
1682 .format(cb_data))
1683
1684 # check if it is sane
1685 self.assertIsNotNone(cb_data)
1686 self.assertEqual(len(cb_data), 12) # True for TLSv1
1687
1688 # and compare with the peers version
1689 s.write(b"CB tls-unique\n")
1690 peer_data_repr = s.read().strip()
1691 self.assertEqual(peer_data_repr,
1692 repr(cb_data).encode("us-ascii"))
1693 s.close()
1694
1695 # now, again
1696 s = ssl.wrap_socket(socket.socket(),
1697 server_side=False,
1698 certfile=CERTFILE,
1699 ca_certs=CERTFILE,
1700 cert_reqs=ssl.CERT_NONE,
1701 ssl_version=ssl.PROTOCOL_TLSv1)
1702 s.connect((HOST, server.port))
1703 new_cb_data = s.get_channel_binding("tls-unique")
1704 if support.verbose:
1705 sys.stdout.write(" got another channel binding data: {0!r}\n "
1706 .format(new_cb_data))
1707 # is it really unique
1708 self.assertNotEqual(cb_data, new_cb_data)
1709 self.assertIsNotNone(cb_data)
1710 self.assertEqual(len(cb_data), 12) # True for TLSv1
1711 s.write(b"CB tls-unique\n")
1712 peer_data_repr = s.read().strip()
1713 self.assertEqual(peer_data_repr,
1714 repr(new_cb_data).encode("us-ascii"))
1715 s.close()
1716 finally:
1717 server.stop()
1718 server.join()
1719 1628
1720 def test_main(verbose=False): 1629 def test_main(verbose=False):
1721 if support.verbose: 1630 if support.verbose:
1722 plats = { 1631 plats = {
1723 'Linux': platform.linux_distribution, 1632 'Linux': platform.linux_distribution,
1724 'Mac': platform.mac_ver, 1633 'Mac': platform.mac_ver,
1725 'Windows': platform.win32_ver, 1634 'Windows': platform.win32_ver,
1726 } 1635 }
1727 for name, func in plats.items(): 1636 for name, func in plats.items():
1728 plat = func() 1637 plat = func()
(...skipping 25 matching lines...) Expand all
1754 tests.append(ThreadedTests) 1663 tests.append(ThreadedTests)
1755 1664
1756 try: 1665 try:
1757 support.run_unittest(*tests) 1666 support.run_unittest(*tests)
1758 finally: 1667 finally:
1759 if _have_threads: 1668 if _have_threads:
1760 support.threading_cleanup(*thread_info) 1669 support.threading_cleanup(*thread_info)
1761 1670
1762 if __name__ == "__main__": 1671 if __name__ == "__main__":
1763 test_main() 1672 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_socketserver.py ('k') | Lib/test/test_subprocess.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+