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

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

Issue 28332: silent truncations in socket.htons and socket.ntohs
Patch Set: Created 2 years, 10 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 | « Doc/library/socket.rst ('k') | Modules/socketmodule.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 870 matching lines...) Expand 10 before | Expand all | Expand 10 after
881 for func, size in sizes.items(): 881 for func, size in sizes.items():
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 good_values = [ 1, 2, 3, 1, 2, 3 ] 891 import _testcapi
892 bad_values = [ -1, -2, -3, -1, -2, -3 ] 892 s_good_values = [ 0, 1, 2, 0xffff ]
893 for k in good_values: 893 l_good_values = [ 0, 1, 2, 0xffffffff ]
894 s_bad_values = [ -1, -2, -3, _testcapi.INT_MIN - 1,
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:
894 socket.ntohl(k) 902 socket.ntohl(k)
895 socket.ntohs(k)
896 socket.htonl(k) 903 socket.htonl(k)
897 socket.htons(k) 904 for k in s_bad_values:
898 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:
899 self.assertRaises(OverflowError, socket.ntohl, k) 908 self.assertRaises(OverflowError, socket.ntohl, k)
900 self.assertRaises(OverflowError, socket.ntohs, k)
901 self.assertRaises(OverflowError, socket.htonl, k) 909 self.assertRaises(OverflowError, socket.htonl, k)
902 self.assertRaises(OverflowError, socket.htons, k) 910 for k in s_deprecated_values:
911 self.assertWarns(DeprecationWarning, socket.ntohs, k)
912 self.assertWarns(DeprecationWarning, socket.htons, k)
903 913
904 def testGetServBy(self): 914 def testGetServBy(self):
905 eq = self.assertEqual 915 eq = self.assertEqual
906 # Find one service that exists, then check all the related interfaces. 916 # Find one service that exists, then check all the related interfaces.
907 # 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
908 # protocol, at least for modern Linuxes. 918 # protocol, at least for modern Linuxes.
909 if (sys.platform.startswith(('freebsd', 'netbsd', 'gnukfreebsd')) 919 if (sys.platform.startswith(('freebsd', 'netbsd', 'gnukfreebsd'))
910 or sys.platform in ('linux', 'darwin')): 920 or sys.platform in ('linux', 'darwin')):
911 # avoid the 'echo' service on this platform, as there is an 921 # avoid the 'echo' service on this platform, as there is an
912 # assumption breaking non-standard port/protocol entry 922 # assumption breaking non-standard port/protocol entry
(...skipping 4676 matching lines...) Expand 10 before | Expand all | Expand 10 after
5589 SendfileUsingSendTest, 5599 SendfileUsingSendTest,
5590 SendfileUsingSendfileTest, 5600 SendfileUsingSendfileTest,
5591 ]) 5601 ])
5592 5602
5593 thread_info = support.threading_setup() 5603 thread_info = support.threading_setup()
5594 support.run_unittest(*tests) 5604 support.run_unittest(*tests)
5595 support.threading_cleanup(*thread_info) 5605 support.threading_cleanup(*thread_info)
5596 5606
5597 if __name__ == "__main__": 5607 if __name__ == "__main__":
5598 test_main() 5608 test_main()
OLDNEW
« no previous file with comments | « Doc/library/socket.rst ('k') | Modules/socketmodule.c » ('j') | no next file with comments »

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