Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -2670,6 +2670,96 @@ def testBindBrEdrL2capSocket(self):
addr = f.getsockname()
self.assertEqual(addr, (socket.BDADDR_ANY, psm))

@unittest.skipUnless(HAVE_SOCKET_BLUETOOTH_L2CAP, 'Bluetooth L2CAP sockets required for this test')
def testBadL2capAddr(self):
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_L2CAP) as f:
with self.assertRaises(OSError):
f.bind((socket.BDADDR_ANY, 0, 0, socket.BDADDR_BREDR, 0))
with self.assertRaises(OSError):
f.bind((socket.BDADDR_ANY,))
with self.assertRaises(OSError):
f.bind(socket.BDADDR_ANY)
with self.assertRaises(OSError):
f.bind((socket.BDADDR_ANY.encode(), 0x1001))

@unittest.skipIf(sys.platform == "win32", "test does not work on windows")
def testBindRfcommSocket(self):
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM) as s:
channel = 0
s.bind((socket.BDADDR_ANY, channel))
addr = s.getsockname()
self.assertEqual(addr, (socket.BDADDR_ANY, channel))

def testBadRfcommAddr(self):
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM) as s:
channel = 0
with self.assertRaises(OSError):
s.bind((socket.BDADDR_ANY.encode(), channel))
with self.assertRaises(OSError):
s.bind((socket.BDADDR_ANY,))
with self.assertRaises(OSError):
s.bind((socket.BDADDR_ANY, channel, 0))
with self.assertRaises(OSError):
s.bind((socket.BDADDR_ANY + '\0', channel))
with self.assertRaises(OSError):
s.bind(('invalid', channel))

@unittest.skipUnless(hasattr(socket, 'BTPROTO_HCI'), 'Bluetooth HCI sockets required for this test')
def testBindHciSocket(self):
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI) as s:
if sys.platform.startswith(('netbsd', 'dragonfly', 'freebsd')):
s.bind(socket.BDADDR_ANY.encode())
addr = s.getsockname()
self.assertEqual(addr, socket.BDADDR_ANY)
else:
dev = 0
s.bind((dev,))
addr = s.getsockname()
self.assertEqual(addr, dev)

@unittest.skipUnless(hasattr(socket, 'BTPROTO_HCI'), 'Bluetooth HCI sockets required for this test')
def testBadHciAddr(self):
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI) as s:
if sys.platform.startswith(('netbsd', 'dragonfly', 'freebsd')):
with self.assertRaises(OSError):
s.bind(socket.BDADDR_ANY)
with self.assertRaises(OSError):
s.bind((socket.BDADDR_ANY.encode(),))
if sys.platform.startswith('freebsd'):
with self.assertRaises(ValueError):
s.bind(socket.BDADDR_ANY.encode() + b'\0')
with self.assertRaises(ValueError):
s.bind(socket.BDADDR_ANY.encode() + b' '*100)
with self.assertRaises(OSError):
s.bind(b'invalid')
else:
dev = 0
with self.assertRaises(OSError):
s.bind(())
with self.assertRaises(OSError):
s.bind((dev, 0))
with self.assertRaises(OSError):
s.bind(dev)
with self.assertRaises(OSError):
s.bind(socket.BDADDR_ANY.encode())

@unittest.skipUnless(hasattr(socket, 'BTPROTO_SCO'), 'Bluetooth SCO sockets required for this test')
def testBindScoSocket(self):
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_SCO) as s:
s.bind(socket.BDADDR_ANY.encode())
addr = s.getsockname()
self.assertEqual(addr, socket.BDADDR_ANY)

@unittest.skipUnless(hasattr(socket, 'BTPROTO_SCO'), 'Bluetooth SCO sockets required for this test')
def testBadScoAddr(self):
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_SCO) as s:
with self.assertRaises(OSError):
s.bind(socket.BDADDR_ANY)
with self.assertRaises(OSError):
s.bind((socket.BDADDR_ANY.encode(),))
with self.assertRaises(OSError):
s.bind(b'invalid')


@unittest.skipUnless(HAVE_SOCKET_HYPERV,
'Hyper-V sockets required for this test.')
Expand Down
Loading