diff options
author | Greg Bowser <topnotcher@gmail.com> | 2019-08-02 20:29:52 (GMT) |
---|---|---|
committer | Steve Dower <steve.dower@python.org> | 2019-08-02 20:29:52 (GMT) |
commit | 8fbece135d7615e836a845ca39223097046c8b8b (patch) | |
tree | 1fa62c02a29b7aa27de4a2d495c4358dc9c4693a /Lib | |
parent | cb65b3a4f484ce71dcb76a918af98c7015513025 (diff) | |
download | cpython-8fbece135d7615e836a845ca39223097046c8b8b.zip cpython-8fbece135d7615e836a845ca39223097046c8b8b.tar.gz cpython-8fbece135d7615e836a845ca39223097046c8b8b.tar.bz2 |
bpo-36590: Add Bluetooth RFCOMM and support for Windows. (GH-12767)
Support for RFCOMM, L2CAP, HCI, SCO is based on the BTPROTO_* macros
being defined. Winsock only supports RFCOMM, even though it has a
BTHPROTO_L2CAP macro. L2CAP support would build on windows, but not
necessarily work.
This also adds some basic unittests for constants (all of which existed
prior to this commit, just not on windows) and creating sockets.
pair: Nate Duarte <slacknate@gmail.com>
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_socket.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 11b2a38..ce816cd 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -114,6 +114,19 @@ def _have_socket_vsock(): return ret +def _have_socket_bluetooth(): + """Check whether AF_BLUETOOTH sockets are supported on this host.""" + try: + # RFCOMM is supported by all platforms with bluetooth support. Windows + # does not support omitting the protocol. + s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM) + except (AttributeError, OSError): + return False + else: + s.close() + return True + + @contextlib.contextmanager def socket_setdefaulttimeout(timeout): old_timeout = socket.getdefaulttimeout() @@ -138,6 +151,8 @@ HAVE_SOCKET_VSOCK = _have_socket_vsock() HAVE_SOCKET_UDPLITE = hasattr(socket, "IPPROTO_UDPLITE") +HAVE_SOCKET_BLUETOOTH = _have_socket_bluetooth() + # Size in bytes of the int type SIZEOF_INT = array.array("i").itemsize @@ -2257,6 +2272,45 @@ class BasicVSOCKTest(unittest.TestCase): socket.SO_VM_SOCKETS_BUFFER_MIN_SIZE)) +@unittest.skipUnless(HAVE_SOCKET_BLUETOOTH, + 'Bluetooth sockets required for this test.') +class BasicBluetoothTest(unittest.TestCase): + + def testBluetoothConstants(self): + socket.BDADDR_ANY + socket.BDADDR_LOCAL + socket.AF_BLUETOOTH + socket.BTPROTO_RFCOMM + + if sys.platform != "win32": + socket.BTPROTO_HCI + socket.SOL_HCI + socket.BTPROTO_L2CAP + + if not sys.platform.startswith("freebsd"): + socket.BTPROTO_SCO + + def testCreateRfcommSocket(self): + with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM) as s: + pass + + @unittest.skipIf(sys.platform == "win32", "windows does not support L2CAP sockets") + def testCreateL2capSocket(self): + with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_L2CAP) as s: + pass + + @unittest.skipIf(sys.platform == "win32", "windows does not support HCI sockets") + def testCreateHciSocket(self): + with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI) as s: + pass + + @unittest.skipIf(sys.platform == "win32" or sys.platform.startswith("freebsd"), + "windows and freebsd do not support SCO sockets") + def testCreateScoSocket(self): + with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_SCO) as s: + pass + + class BasicTCPTest(SocketConnectedTest): def __init__(self, methodName='runTest'): @@ -6416,6 +6470,7 @@ def test_main(): BasicVSOCKTest, ThreadedVSOCKSocketStreamTest, ]) + tests.append(BasicBluetoothTest) tests.extend([ CmsgMacroTests, SendmsgUDPTest, |