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

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

Issue 9682: socket.create_connection error message for domain subpart with invalid length is very confusing
Patch Set: Created 6 years, 11 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
« Lib/encodings/idna.py ('K') | « Lib/encodings/idna.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 \
r.david.murray 2013/04/27 03:04:44 I would prefer to do black box testing: don't test
16 validate_max_length, \
17 validate_min_length, \
18 ToASCII
14 import warnings 19 import warnings
15 from test import support, string_tests 20 from test import support, string_tests
21 from unittest import mock
16 22
17 # Error handling (bad decoder return) 23 # Error handling (bad decoder return)
18 def search_function(encoding): 24 def search_function(encoding):
19 def decode1(input, errors="strict"): 25 def decode1(input, errors="strict"):
20 return 42 # not a tuple 26 return 42 # not a tuple
21 def encode1(input, errors="strict"): 27 def encode1(input, errors="strict"):
22 return 42 # not a tuple 28 return 42 # not a tuple
23 def encode2(input, errors="strict"): 29 def encode2(input, errors="strict"):
24 return (42, 42) # no unicode 30 return (42, 42) # no unicode
25 def decode2(input, errors="strict"): 31 def decode2(input, errors="strict"):
(...skipping 1639 matching lines...) Expand 10 before | Expand all | Expand 10 after
1665 ('F4 8F BF 00', FFFD+'\x00'), ('F4 8F BF 7F', FFFD+'\x7f'), 1671 ('F4 8F BF 00', FFFD+'\x00'), ('F4 8F BF 7F', FFFD+'\x7f'),
1666 ('F4 8F BF C0', FFFDx2), ('F4 8F BF FF', FFFDx2) 1672 ('F4 8F BF C0', FFFDx2), ('F4 8F BF FF', FFFDx2)
1667 ] 1673 ]
1668 for seq, res in sequences: 1674 for seq, res in sequences:
1669 self.assertCorrectUTF8Decoding(self.to_bytestring(seq), res, 1675 self.assertCorrectUTF8Decoding(self.to_bytestring(seq), res,
1670 'invalid continuation byte') 1676 'invalid continuation byte')
1671 1677
1672 def test_codecs_idna(self): 1678 def test_codecs_idna(self):
1673 # Test whether trailing dot is preserved 1679 # Test whether trailing dot is preserved
1674 self.assertEqual("www.python.org.".encode("idna"), b"www.python.org.") 1680 self.assertEqual("www.python.org.".encode("idna"), b"www.python.org.")
1681
1682 def test_max_length_validation(self):
1683 expected = "Z" * 64
1684 label = expected.encode('ascii')
1685 with self.assertRaisesRegexp(UnicodeError, expected):
1686 validate_max_length(label)
1687
1688 def test_min_length_validation(self):
1689 label = "".encode('ascii')
1690 labels = "abcdefg".encode('ascii')
1691 with self.assertRaisesRegexp(UnicodeError, " in 'abcdefg'"):
1692 validate_min_length(label, labels)
1693
1694 with self.assertRaisesRegexp(UnicodeError, "empty label"):
1695 validate_min_length(label)
1696
1697 def test_min_codecs_error_messages(self):
1698 # Test Empty
1699 self.assertRaisesRegexp(
1700 UnicodeError,
1701 '1..com',
1702 '1..com'.encode,
1703 'idna')
1704
1705 def test_max_codecs_errors_messages(self):
1706 # Test label greater then 64
1707 label = "A" * 64
1708 value = "first." + label + ".last"
1709 self.assertRaisesRegexp(
1710 UnicodeError,
1711 label,
1712 value.encode,
1713 'idna')
1714 # Test last label length 64
1715 value = "first.second." + label
1716 self.assertRaisesRegexp(
1717 UnicodeError,
1718 label,
1719 value.encode,
1720 'idna')
1721 # Test last label length greater then 64
1722 label += "A"
1723 value += "A"
1724 self.assertRaisesRegexp(
1725 UnicodeError,
1726 label,
1727 value.encode,
1728 'idna')
1729
1730 def test_min_label_encode_ascii(self):
1731 with self.assertRaisesRegexp(UnicodeError, "empty label"):
1732 ToASCII('')
1733
1734 def test_max_label_encode_ascii(self):
1735 label = "Z" * 64
1736 with self.assertRaisesRegexp(UnicodeError, label):
1737 ToASCII(label)
1738
1739 @mock.patch('encodings.idna.nameprep')
r.david.murray 2013/04/27 03:04:44 I don't understand the purpose of these tests. Ca
mmilkin 2013/04/27 16:04:56 Sure, the ToASCII function has many calls to valid
1740 def test_escaped_min_label(self, nameprep):
1741 label = mock.Mock()
1742 label.encode.side_effect = UnicodeError
1743 nameprep.return_value = ''
1744 with self.assertRaisesRegexp(UnicodeError, "empty label"):
1745 ToASCII(label)
1746
1747 @mock.patch('encodings.idna.nameprep')
1748 def test_escaped_max_label(self, nameprep):
1749 mock_label = mock.Mock()
1750 mock_label.encode.side_effect = UnicodeError
1751 label = "Z" * 64
1752 nameprep.return_value = label
1753 with self.assertRaisesRegexp(UnicodeError, label):
1754 ToASCII(mock_label)
1755
1756 @mock.patch('encodings.idna.nameprep')
1757 def test_escape_max_punycode_ascii(self, nameprep):
1758 mock_label = mock.Mock()
1759 mock_label.encode.side_effect = UnicodeError
1760 name_space_label = mock.Mock()
1761 expected_label = "Z" * 64
1762 expected_label = expected_label.encode('ascii')
1763 def encode(value):
1764 if value == "ascii":
1765 raise UnicodeError
1766 return expected_label
1767
1768 name_space_label.encode = encode
1769 name_space_label.startswith.return_value = False
1770 nameprep.return_value = name_space_label
1771 with self.assertRaisesRegexp(UnicodeError, "Z" * 64):
1772 ToASCII(mock_label)
1675 1773
1676 def test_codecs_errors(self): 1774 def test_codecs_errors(self):
1677 # Error handling (encoding) 1775 # Error handling (encoding)
1678 self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii') 1776 self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii')
1679 self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii','strict') 1777 self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii','strict')
1680 self.assertEqual('Andr\202 x'.encode('ascii','ignore'), b"Andr x") 1778 self.assertEqual('Andr\202 x'.encode('ascii','ignore'), b"Andr x")
1681 self.assertEqual('Andr\202 x'.encode('ascii','replace'), b"Andr? x") 1779 self.assertEqual('Andr\202 x'.encode('ascii','replace'), b"Andr? x")
1682 self.assertEqual('Andr\202 x'.encode('ascii', 'replace'), 1780 self.assertEqual('Andr\202 x'.encode('ascii', 'replace'),
1683 'Andr\202 x'.encode('ascii', errors='replace')) 1781 'Andr\202 x'.encode('ascii', errors='replace'))
1684 self.assertEqual('Andr\202 x'.encode('ascii', 'ignore'), 1782 self.assertEqual('Andr\202 x'.encode('ascii', 'ignore'),
(...skipping 655 matching lines...) Expand 10 before | Expand all | Expand 10 after
2340 "obj", 2438 "obj",
2341 [(True, 'arg'), 2439 [(True, 'arg'),
2342 (False, 'key1'), 2440 (False, 'key1'),
2343 (False, 'key2'), 2441 (False, 'key2'),
2344 ]]) 2442 ]])
2345 self.assertRaises(TypeError, _string.formatter_field_name_split, 1) 2443 self.assertRaises(TypeError, _string.formatter_field_name_split, 1)
2346 2444
2347 2445
2348 if __name__ == "__main__": 2446 if __name__ == "__main__":
2349 unittest.main() 2447 unittest.main()
OLDNEW
« Lib/encodings/idna.py ('K') | « Lib/encodings/idna.py ('k') | no next file » | no next file with comments »

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