import sys import unittest import socket class BugsOrPatchesTester(unittest.TestCase): def test_AF_NETLINK(self): if 'win32' == sys.platform: return with socket.socket(socket.AF_NETLINK, socket.SOCK_RAW) as sock: with self.assertRaisesRegex(TypeError, r"^bind\(\): AF_NETLINK address must " r"be tuple, not list$"): sock.bind([]) with self.assertRaisesRegex(TypeError, r"^AF_NETLINK address must be a pair " r"\(pid, groups\)$"): sock.bind(()) def test_AF_INET(self): with socket.socket() as sock: with self.assertRaisesRegex(TypeError, r"^bind\(\): AF_INET address must " r"be tuple, not list$"): sock.bind([]) with self.assertRaisesRegex(TypeError, r"^connect\(\): AF_INET address must " r"be tuple, not list$"): sock.connect([]) with self.assertRaisesRegex(TypeError, r"^connect_ex\(\): AF_INET address " r"must be tuple, not list$"): sock.connect_ex([]) with self.assertRaisesRegex(TypeError, r"^sendto\(\): AF_INET address " r"must be tuple, not list$"): sock.sendto(b'a', []) if 'linux' == sys.platform: with self.assertRaisesRegex(TypeError, r"^sendmsg\(\): AF_INET address " r"must be tuple, not list$"): sock.sendmsg(b'a', 0, 0, []) with self.assertRaisesRegex(TypeError, r"^AF_INET address must be a pair " r"\(host, port\)$"): sock.bind(()) with self.assertRaisesRegex(TypeError, r"^AF_INET address must be a pair " r"\(host, port\)$"): sock.connect(()) with self.assertRaisesRegex(TypeError, r"^AF_INET address must be a pair " r"\(host, port\)$"): sock.connect_ex(()) with self.assertRaisesRegex(TypeError, r"^AF_INET address must be a pair " r"\(host, port\)$"): sock.sendto(b'a', ()) if 'linux' == sys.platform: with self.assertRaisesRegex(TypeError, r"^AF_INET address must be a pair " r"\(host, port\)$"): sock.sendmsg(b'a', 0, 0, ()) for i in [-1 << 1000, -1, 65536, 1 << 1000]: with self.assertRaisesRegex(OverflowError, r"^connect\(\): port must be " r"0-65535.$"): sock.connect(('localhost', i)) def test_AF_INET6(self): with socket.socket(socket.AF_INET6) as sock: with self.assertRaisesRegex(TypeError, r"^bind\(\): AF_INET6 address must " r"be tuple, not list$"): sock.bind([]) with self.assertRaisesRegex(TypeError, r"^AF_INET6 address must be a tuple " r"\(host, port\[, flowinfo\[, " r"scopeid\]\]\)$"): sock.bind(()) for i in [-1 << 1000, -1, 65536, 1 << 1000]: with self.assertRaisesRegex(OverflowError, r"^connect\(\): port must be " r"0-65535.$"): sock.connect(('localhost', i)) for i in [-1, 1048576]: with self.assertRaisesRegex(OverflowError, r"^connect\(\): flowinfo must be " r"0-1048575.$"): sock.connect(('localhost', 0, i)) def test_AF_BLUETOOTH(self): if 'win32' == sys.platform: return with socket.socket(socket.AF_BLUETOOTH, proto=socket.BTPROTO_L2CAP) as sock: with self.assertRaisesRegex(OSError, r"^bind\(\): wrong format$"): sock.bind([]) with socket.socket(socket.AF_BLUETOOTH, proto=socket.BTPROTO_RFCOMM) as sock: with self.assertRaisesRegex(OSError, r"^bind\(\): wrong format$"): sock.bind([]) with socket.socket(socket.AF_BLUETOOTH, 3, proto=socket.BTPROTO_HCI) as sock: with self.assertRaisesRegex(OSError, r"^bind\(\): wrong format$"): sock.bind([]) with socket.socket(socket.AF_BLUETOOTH, 5, proto=socket.BTPROTO_SCO) as sock: with self.assertRaisesRegex(OSError, r"^bind\(\): wrong format$"): sock.bind([]) with socket.socket(socket.AF_BLUETOOTH, 3, proto=4) as sock: with self.assertRaisesRegex(OSError, r"^bind\(\): unknown Bluetooth " r"protocol$"): sock.bind([]) def test_AF_PACKET(self): if 'win32' == sys.platform: return # running this script using 'sudo ./python testPatches.py' is # necessary (at least on my Ubuntu 16.04 VM). with socket.socket(socket.AF_PACKET, socket.SOCK_RAW) as sock: with self.assertRaisesRegex(TypeError, r"^bind\(\): AF_PACKET address must " r"be tuple, not list$"): sock.bind([]) with self.assertRaisesRegex(TypeError, r"^AF_PACKET address must be a tuple " r"of two to five elements$"): sock.bind((1,)) for i in [-1 << 1000, 1 << 1000]: with self.assertRaisesRegex(OverflowError, r"^bind\(\): address argument out " r"of range$"): sock.bind(('lo', i)) for i in [-1, 65536]: with self.assertRaisesRegex(OverflowError, r"^bind\(\): protoNumber must be " r"0-65535.$"): sock.bind(('lo', i)) def test_AF_TIPC(self): if 'win32' == sys.platform: return with socket.socket(socket.AF_TIPC, socket.SOCK_RDM) as sock: with self.assertRaisesRegex(TypeError, r"^bind\(\): AF_TIPC address must " r"be tuple, not list$"): sock.bind([]) with self.assertRaisesRegex(TypeError, r"^AF_TIPC address must be a tuple " r"\(addr_type, v1, v2, v3" r"\[, scope\]\)$"): sock.bind((1,)) def test_AF_CAN(self): if 'win32' == sys.platform: return with socket.socket(socket.AF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as sock: with self.assertRaisesRegex(TypeError, r"^bind\(\): AF_CAN address must " r"be tuple, not list$"): sock.bind([]) with self.assertRaisesRegex(TypeError, r"^AF_CAN address must be a tuple " r"\(interface, \)$"): sock.bind(()) # here should come a test of the 'unsupported CAN protocol' message, # but I couldn't find a way to trigger it from Python code. def test_PF_SYSTEM(self): # socket.PF_SYSTEM doesn't exist on my Windows 10, nor on my Ubuntu # 16.04 VM. pass def test_AF_ALG(self): if 'win32' == sys.platform: return with socket.socket(socket.AF_ALG, socket.SOCK_SEQPACKET, 0) as sock: with self.assertRaisesRegex(TypeError, r"^bind\(\): AF_ALG address must " r"be tuple, not list$"): sock.bind([]) with self.assertRaisesRegex(TypeError, r"^AF_ALG address must be a tuple " r"\(type, name\[, feat\[, " r"mask\]\]\)$"): sock.bind(()) def test_bad_family(self): with socket.socket(socket.AF_IRDA) as sock: with self.assertRaisesRegex(OSError, r"^bind\(\): bad family$"): sock.bind([]) if __name__ == '__main__': unittest.main()