[3.13] gh-132099: Harmonize Bluetooth address handling (GH-132486) (GH-132497)

Now all protocols always accept the Bluetooth address as string and
getsockname() always returns the Bluetooth address as string.

* BTPROTO_SCO now accepts not only bytes, but str.
* BTPROTO_SCO now checks address for embedded null.
* On *BSD, BTPROTO_HCI now accepts str instead of bytes.
* On FreeBSD, getsockname() for BTPROTO_HCI now returns str instead of bytes.
* On NetBSD and DragonFly BSD, BTPROTO_HCI now checks address for embedded null.
(cherry picked from commit 1fc1df8dcc)
This commit is contained in:
Serhiy Storchaka 2025-04-14 19:36:04 +03:00 committed by GitHub
parent a50aa3325b
commit d321b6ec82
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 58 additions and 38 deletions

View file

@ -2656,6 +2656,8 @@ class BasicBluetoothTest(unittest.TestCase):
f.bind(socket.BDADDR_ANY)
with self.assertRaises(OSError):
f.bind((socket.BDADDR_ANY.encode(), 0x1001))
with self.assertRaises(OSError):
f.bind(('\ud812', 0x1001))
def testBindRfcommSocket(self):
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM) as s:
@ -2687,6 +2689,8 @@ class BasicBluetoothTest(unittest.TestCase):
s.bind((socket.BDADDR_ANY, channel, 0))
with self.assertRaises(OSError):
s.bind((socket.BDADDR_ANY + '\0', channel))
with self.assertRaises(OSError):
s.bind('\ud812')
with self.assertRaises(OSError):
s.bind(('invalid', channel))
@ -2694,7 +2698,7 @@ class BasicBluetoothTest(unittest.TestCase):
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())
s.bind(socket.BDADDR_ANY)
addr = s.getsockname()
self.assertEqual(addr, socket.BDADDR_ANY)
else:
@ -2713,14 +2717,17 @@ class BasicBluetoothTest(unittest.TestCase):
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)
s.bind(socket.BDADDR_ANY.encode())
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)
s.bind((socket.BDADDR_ANY,))
with self.assertRaises(OSError):
s.bind(socket.BDADDR_ANY + '\0')
with self.assertRaises((ValueError, OSError)):
s.bind(socket.BDADDR_ANY + ' '*100)
with self.assertRaises(OSError):
s.bind('\ud812')
with self.assertRaises(OSError):
s.bind('invalid')
with self.assertRaises(OSError):
s.bind(b'invalid')
else:
@ -2731,11 +2738,18 @@ class BasicBluetoothTest(unittest.TestCase):
s.bind((dev, 0))
with self.assertRaises(OSError):
s.bind(dev)
with self.assertRaises(OSError):
s.bind(socket.BDADDR_ANY)
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)
addr = s.getsockname()
self.assertEqual(addr, socket.BDADDR_ANY)
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_SCO) as s:
s.bind(socket.BDADDR_ANY.encode())
addr = s.getsockname()
@ -2745,9 +2759,17 @@ class BasicBluetoothTest(unittest.TestCase):
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)
s.bind((socket.BDADDR_ANY,))
with self.assertRaises(OSError):
s.bind((socket.BDADDR_ANY.encode(),))
with self.assertRaises(ValueError):
s.bind(socket.BDADDR_ANY + '\0')
with self.assertRaises(ValueError):
s.bind(socket.BDADDR_ANY.encode() + b'\0')
with self.assertRaises(UnicodeEncodeError):
s.bind('\ud812')
with self.assertRaises(OSError):
s.bind('invalid')
with self.assertRaises(OSError):
s.bind(b'invalid')