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

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

Issue 28332: silent truncations in socket.htons and socket.ntohs
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:
View unified diff | Download patch
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 good_values = [ 0, 1, 2, 3 ]
storchaka 2016/10/01 21:37:42 Add 0xffff.
893 bad_values = [ -1, -2, -3 ]
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 ]
893 for k in good_values: 895 for k in good_values:
894 socket.ntohl(k) 896 socket.ntohl(k)
895 socket.ntohs(k) 897 socket.ntohs(k)
896 socket.htonl(k) 898 socket.htonl(k)
897 socket.htons(k) 899 socket.htons(k)
898 for k in bad_values: 900 for k in bad_values:
899 self.assertRaises(OverflowError, socket.ntohl, k) 901 self.assertRaises(OverflowError, socket.ntohl, k)
900 self.assertRaises(OverflowError, socket.ntohs, k) 902 self.assertRaises(OverflowError, socket.ntohs, k)
901 self.assertRaises(OverflowError, socket.htonl, k) 903 self.assertRaises(OverflowError, socket.htonl, k)
902 self.assertRaises(OverflowError, socket.htons, k) 904 self.assertRaises(OverflowError, socket.htons, k)
905 for k in deprecated_values:
906 self.assertWarns(DeprecationWarning, socket.ntohs, k)
907 self.assertWarns(DeprecationWarning, socket.htons, k)
903 908
904 def testGetServBy(self): 909 def testGetServBy(self):
905 eq = self.assertEqual 910 eq = self.assertEqual
906 # Find one service that exists, then check all the related interfaces. 911 # 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 912 # I've ordered this by protocols that have both a tcp and udp
908 # protocol, at least for modern Linuxes. 913 # protocol, at least for modern Linuxes.
909 if (sys.platform.startswith(('freebsd', 'netbsd', 'gnukfreebsd')) 914 if (sys.platform.startswith(('freebsd', 'netbsd', 'gnukfreebsd'))
910 or sys.platform in ('linux', 'darwin')): 915 or sys.platform in ('linux', 'darwin')):
911 # avoid the 'echo' service on this platform, as there is an 916 # avoid the 'echo' service on this platform, as there is an
912 # assumption breaking non-standard port/protocol entry 917 # assumption breaking non-standard port/protocol entry
(...skipping 4676 matching lines...) Expand 10 before | Expand all | Expand 10 after
5589 SendfileUsingSendTest, 5594 SendfileUsingSendTest,
5590 SendfileUsingSendfileTest, 5595 SendfileUsingSendfileTest,
5591 ]) 5596 ])
5592 5597
5593 thread_info = support.threading_setup() 5598 thread_info = support.threading_setup()
5594 support.run_unittest(*tests) 5599 support.run_unittest(*tests)
5595 support.threading_cleanup(*thread_info) 5600 support.threading_cleanup(*thread_info)
5596 5601
5597 if __name__ == "__main__": 5602 if __name__ == "__main__":
5598 test_main() 5603 test_main()
OLDNEW

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