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

Delta Between Two Patch Sets: Lib/test/test_unicode.py

Issue 9682: socket.create_connection error message for domain subpart with invalid length is very confusing
Left Patch Set: Created 6 years, 10 months ago
Right Patch Set: Created 6 years, 9 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/encodings/idna.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 """ Test script for the Unicode implementation. 1 """ Test script for the Unicode implementation.
2 2
3 Written by Marc-Andre Lemburg (mal@lemburg.com). 3 Written by Marc-Andre Lemburg (mal@lemburg.com).
4 4
5 (c) Copyright CNRI, All Rights Reserved. NO WARRANTY. 5 (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
6 6
7 """#" 7 """#"
8 import _string 8 import _string
9 import codecs 9 import codecs
10 import itertools 10 import itertools
11 import struct 11 import struct
12 import sys 12 import sys
13
13 import unittest 14 import unittest
15 from encodings.idna import ToASCII
14 import warnings 16 import warnings
15 from test import support, string_tests 17 from test import support, string_tests
18 from unittest import mock
16 19
17 # Error handling (bad decoder return) 20 # Error handling (bad decoder return)
18 def search_function(encoding): 21 def search_function(encoding):
19 def decode1(input, errors="strict"): 22 def decode1(input, errors="strict"):
20 return 42 # not a tuple 23 return 42 # not a tuple
21 def encode1(input, errors="strict"): 24 def encode1(input, errors="strict"):
22 return 42 # not a tuple 25 return 42 # not a tuple
23 def encode2(input, errors="strict"): 26 def encode2(input, errors="strict"):
24 return (42, 42) # no unicode 27 return (42, 42) # no unicode
25 def decode2(input, errors="strict"): 28 def decode2(input, errors="strict"):
(...skipping 1640 matching lines...) Expand 10 before | Expand all | Expand 10 after
1666 ('F4 8F BF C0', FFFDx2), ('F4 8F BF FF', FFFDx2) 1669 ('F4 8F BF C0', FFFDx2), ('F4 8F BF FF', FFFDx2)
1667 ] 1670 ]
1668 for seq, res in sequences: 1671 for seq, res in sequences:
1669 self.assertCorrectUTF8Decoding(self.to_bytestring(seq), res, 1672 self.assertCorrectUTF8Decoding(self.to_bytestring(seq), res,
1670 'invalid continuation byte') 1673 'invalid continuation byte')
1671 1674
1672 def test_codecs_idna(self): 1675 def test_codecs_idna(self):
1673 # Test whether trailing dot is preserved 1676 # Test whether trailing dot is preserved
1674 self.assertEqual("www.python.org.".encode("idna"), b"www.python.org.") 1677 self.assertEqual("www.python.org.".encode("idna"), b"www.python.org.")
1675 1678
1676 def test_codecs_errors_messages(self): 1679 def test_min_codecs_error_messages(self):
1677 # Test Empty 1680 # Test Empty
1678 self.assertRaisesRegexp( 1681 self.assertRaisesRegexp(
1679 UnicodeError, 1682 UnicodeError,
1680 '1..com', 1683 '1..com',
1681 '1..com'.encode, 1684 '1..com'.encode,
1682 'idna') 1685 'idna')
1686
1687 def test_max_codecs_errors_messages(self):
1683 # Test label greater then 64 1688 # Test label greater then 64
1684 label = "A" * 64 1689 label = "A" * 64
1685 value = "first." + label + ".last" 1690 value = "first." + label + ".last"
1686 self.assertRaisesRegexp( 1691 self.assertRaisesRegexp(
1687 UnicodeError, 1692 UnicodeError,
1688 label, 1693 label,
1689 value.encode, 1694 value.encode,
1690 'idna') 1695 'idna')
1691 # Test last label length 64 1696 # Test last label length 64
1692 value = "first.second." + label 1697 value = "first.second." + label
1693 self.assertRaisesRegexp( 1698 self.assertRaisesRegexp(
1694 UnicodeError, 1699 UnicodeError,
1695 label, 1700 label,
1696 value.encode, 1701 value.encode,
1697 'idna') 1702 'idna')
1698 # Test last label length greater then 64 1703 # Test last label length greater then 64
1699 label += "A" 1704 label += "A"
1700 value += "A" 1705 value += "A"
1701 self.assertRaisesRegexp( 1706 self.assertRaisesRegexp(
1702 UnicodeError, 1707 UnicodeError,
1703 label, 1708 label,
1704 value.encode, 1709 value.encode,
1705 'idna') 1710 'idna')
1706 1711
1712 def test_min_label_encode_ascii(self):
1713 with self.assertRaisesRegexp(UnicodeError, "empty label"):
1714 ToASCII('')
1715
1716 def test_max_label_encode_ascii(self):
1717 label = "Z" * 64
1718 with self.assertRaisesRegexp(UnicodeError, label):
1719 ToASCII(label)
1720
1721 @mock.patch('encodings.idna.nameprep')
1722 def test_escaped_min_label(self, nameprep):
1723 label = mock.Mock()
1724 label.encode.side_effect = UnicodeError
1725 nameprep.return_value = ''
1726 with self.assertRaisesRegexp(UnicodeError, "empty label"):
1727 ToASCII(label)
1728
1729 @mock.patch('encodings.idna.nameprep')
1730 def test_escaped_max_label(self, nameprep):
1731 mock_label = mock.Mock()
1732 mock_label.encode.side_effect = UnicodeError
1733 label = "Z" * 64
1734 nameprep.return_value = label
1735 with self.assertRaisesRegexp(UnicodeError, label):
1736 ToASCII(mock_label)
1737
1738 @mock.patch('encodings.idna.nameprep')
1739 def test_escape_max_punycode_ascii(self, nameprep):
1740 mock_label = mock.Mock()
1741 mock_label.encode.side_effect = UnicodeError
1742 name_space_label = mock.Mock()
1743 expected_label = "Z" * 64
1744 expected_label = expected_label.encode('ascii')
1745 def encode(value):
1746 if value == "ascii":
1747 raise UnicodeError
1748 return expected_label
1749
1750 name_space_label.encode = encode
1751 name_space_label.startswith.return_value = False
1752 nameprep.return_value = name_space_label
1753 with self.assertRaisesRegexp(UnicodeError, "Z" * 64):
1754 ToASCII(mock_label)
1755
1707 def test_codecs_errors(self): 1756 def test_codecs_errors(self):
1708 # Error handling (encoding) 1757 # Error handling (encoding)
1709 self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii') 1758 self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii')
1710 self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii','strict') 1759 self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii','strict')
1711 self.assertEqual('Andr\202 x'.encode('ascii','ignore'), b"Andr x") 1760 self.assertEqual('Andr\202 x'.encode('ascii','ignore'), b"Andr x")
1712 self.assertEqual('Andr\202 x'.encode('ascii','replace'), b"Andr? x") 1761 self.assertEqual('Andr\202 x'.encode('ascii','replace'), b"Andr? x")
1713 self.assertEqual('Andr\202 x'.encode('ascii', 'replace'), 1762 self.assertEqual('Andr\202 x'.encode('ascii', 'replace'),
1714 'Andr\202 x'.encode('ascii', errors='replace')) 1763 'Andr\202 x'.encode('ascii', errors='replace'))
1715 self.assertEqual('Andr\202 x'.encode('ascii', 'ignore'), 1764 self.assertEqual('Andr\202 x'.encode('ascii', 'ignore'),
1716 'Andr\202 x'.encode(encoding='ascii', errors='ignore')) 1765 'Andr\202 x'.encode(encoding='ascii', errors='ignore'))
(...skipping 654 matching lines...) Expand 10 before | Expand all | Expand 10 after
2371 "obj", 2420 "obj",
2372 [(True, 'arg'), 2421 [(True, 'arg'),
2373 (False, 'key1'), 2422 (False, 'key1'),
2374 (False, 'key2'), 2423 (False, 'key2'),
2375 ]]) 2424 ]])
2376 self.assertRaises(TypeError, _string.formatter_field_name_split, 1) 2425 self.assertRaises(TypeError, _string.formatter_field_name_split, 1)
2377 2426
2378 2427
2379 if __name__ == "__main__": 2428 if __name__ == "__main__":
2380 unittest.main() 2429 unittest.main()
LEFTRIGHT

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