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

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

Issue 28332: silent truncations in socket.htons and socket.ntohs
Left Patch Set: Created 2 years, 9 months ago
Right Patch Set: Created 2 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 | « Doc/library/socket.rst ('k') | Modules/socketmodule.c » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 import unittest 1 import unittest
2 from test import support 2 from test import support
3 3
4 import errno 4 import errno
5 import io 5 import io
6 import itertools 6 import itertools
7 import socket 7 import socket
8 import select 8 import select
9 import tempfile 9 import tempfile
10 import time 10 import time
(...skipping 871 matching lines...) Expand 10 before | Expand all | Expand 10 after
882 mask = (1<<size) - 1 882 mask = (1<<size) - 1
883 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): 883 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210):
884 self.assertEqual(i & mask, func(func(i&mask)) & mask) 884 self.assertEqual(i & mask, func(func(i&mask)) & mask)
885 885
886 swapped = func(mask) 886 swapped = func(mask)
887 self.assertEqual(swapped & mask, mask) 887 self.assertEqual(swapped & mask, mask)
888 self.assertRaises(OverflowError, func, 1<<34) 888 self.assertRaises(OverflowError, func, 1<<34)
889 889
890 def testNtoHErrors(self): 890 def testNtoHErrors(self):
891 import _testcapi 891 import _testcapi
892 good_values = [ 0, 1, 2, 3 ] 892 s_good_values = [ 0, 1, 2, 0xffff ]
storchaka 2016/10/01 21:37:42 Add 0xffff.
893 bad_values = [ -1, -2, -3 ] 893 l_good_values = [ 0, 1, 2, 0xffffffff ]
storchaka 2016/10/01 21:37:42 Add large integer value that doesn't fit in C int
894 deprecated_values = [ _testcapi.USHRT_MAX + 1, _testcapi.INT_MAX ] 894 s_bad_values = [ -1, -2, -3, _testcapi.INT_MIN - 1,
895 for k in good_values: 895 _testcapi.INT_MAX + 1, 2 ** 1000]
896 l_bad_values = [ -1, -2, 2 ** 32, _testcapi.ULONG_MAX + 1, 2 ** 1000 ]
897 s_deprecated_values = [ 2 ** 16, _testcapi.INT_MAX ]
898 for k in s_good_values:
899 socket.ntohs(k)
900 socket.htons(k)
901 for k in l_good_values:
896 socket.ntohl(k) 902 socket.ntohl(k)
897 socket.ntohs(k)
898 socket.htonl(k) 903 socket.htonl(k)
899 socket.htons(k) 904 for k in s_bad_values:
900 for k in bad_values: 905 self.assertRaises(OverflowError, socket.ntohs, k)
906 self.assertRaises(OverflowError, socket.htons, k)
907 for k in l_bad_values:
901 self.assertRaises(OverflowError, socket.ntohl, k) 908 self.assertRaises(OverflowError, socket.ntohl, k)
902 self.assertRaises(OverflowError, socket.ntohs, k)
903 self.assertRaises(OverflowError, socket.htonl, k) 909 self.assertRaises(OverflowError, socket.htonl, k)
904 self.assertRaises(OverflowError, socket.htons, k) 910 for k in s_deprecated_values:
905 for k in deprecated_values:
906 self.assertWarns(DeprecationWarning, socket.ntohs, k) 911 self.assertWarns(DeprecationWarning, socket.ntohs, k)
907 self.assertWarns(DeprecationWarning, socket.htons, k) 912 self.assertWarns(DeprecationWarning, socket.htons, k)
908 913
909 def testGetServBy(self): 914 def testGetServBy(self):
910 eq = self.assertEqual 915 eq = self.assertEqual
911 # Find one service that exists, then check all the related interfaces. 916 # Find one service that exists, then check all the related interfaces.
912 # I've ordered this by protocols that have both a tcp and udp 917 # I've ordered this by protocols that have both a tcp and udp
913 # protocol, at least for modern Linuxes. 918 # protocol, at least for modern Linuxes.
914 if (sys.platform.startswith(('freebsd', 'netbsd', 'gnukfreebsd')) 919 if (sys.platform.startswith(('freebsd', 'netbsd', 'gnukfreebsd'))
915 or sys.platform in ('linux', 'darwin')): 920 or sys.platform in ('linux', 'darwin')):
(...skipping 4678 matching lines...) Expand 10 before | Expand all | Expand 10 after
5594 SendfileUsingSendTest, 5599 SendfileUsingSendTest,
5595 SendfileUsingSendfileTest, 5600 SendfileUsingSendfileTest,
5596 ]) 5601 ])
5597 5602
5598 thread_info = support.threading_setup() 5603 thread_info = support.threading_setup()
5599 support.run_unittest(*tests) 5604 support.run_unittest(*tests)
5600 support.threading_cleanup(*thread_info) 5605 support.threading_cleanup(*thread_info)
5601 5606
5602 if __name__ == "__main__": 5607 if __name__ == "__main__":
5603 test_main() 5608 test_main()
LEFTRIGHT

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