8000 Add tests for invalid addresses. · python/cpython@94c9287 · GitHub
[go: up one dir, main page]

Skip to content

Commit 94c9287

Browse files
Add tests for invalid addresses.
1 parent 909871a commit 94c9287

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

Lib/test/test_socket.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2670,13 +2670,40 @@ def testBindBrEdrL2capSocket(self):
26702670
addr = f.getsockname()
26712671
self.assertEqual(addr, (socket.BDADDR_ANY, psm))
26722672

2673+
@unittest.skipUnless(HAVE_SOCKET_BLUETOOTH_L2CAP, 'Bluetooth L2CAP sockets required for this test')
2674+
def testBadL2capAddr(self):
2675+
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_L2CAP) as f:
2676+
with self.assertRaises(OSError):
2677+
f.bind((socket.BDADDR_ANY, 0, 0, socket.BDADDR_BREDR, 0))
2678+
with self.assertRaises(OSError):
2679+
f.bind((socket.BDADDR_ANY,))
2680+
with self.assertRaises(OSError):
2681+
f.bind(socket.BDADDR_ANY)
2682+
with self.assertRaises(OSError):
2683+
f.bind((socket.BDADDR_ANY.encode(), 0x1001))
2684+
2685+
@unittest.skipIf(sys.platform == "win32", "test does not work on windows")
26732686
def testBindRfcommSocket(self):
26742687
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM) as s:
26752688
channel = 0
26762689
s.bind((socket.BDADDR_ANY, channel))
26772690
addr = s.getsockname()
26782691
self.assertEqual(addr, (socket.BDADDR_ANY, channel))
26792692

2693+
def testBadRfcommAddr(self):
2694+
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM) as s:
2695+
channel = 0
2696+
with self.assertRaises(OSError):
2697+
s.bind((socket.BDADDR_ANY.encode(), channel))
2698+
with self.assertRaises(OSError):
2699+
s.bind((socket.BDADDR_ANY,))
2700+
with self.assertRaises(OSError):
2701+
s.bind((socket.BDADDR_ANY, channel, 0))
2702+
with self.assertRaises(OSError):
2703+
s.bind((socket.BDADDR_ANY + '\0', channel))
2704+
with self.assertRaises(OSError):
2705+
s.bind(('invalid', channel))
2706+
26802707
@unittest.skipUnless(hasattr(socket, 'BTPROTO_HCI'), 'Bluetooth HCI sockets required for this test')
26812708
def testBindHciSocket(self):
26822709
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI) as s:
@@ -2690,13 +2717,49 @@ def testBindHciSocket(self):
26902717
addr = s.getsockname()
26912718
self.assertEqual(addr, dev)
26922719

2720+
@unittest.skipUnless(hasattr(socket, 'BTPROTO_HCI'), 'Bluetooth HCI sockets required for this test')
2721+
def testBadHciAddr(self):
2722+
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI) as s:
2723+
if sys.platform.startswith(('netbsd', 'dragonfly', 'freebsd')):
2724+
with self.assertRaises(OSError):
2725+
s.bind(socket.BDADDR_ANY)
2726+
with self.assertRaises(OSError):
2727+
s.bind((socket.BDADDR_ANY.encode(),))
2728+
if sys.platform.startswith('freebsd'):
2729+
with self.assertRaises(ValueError):
2730+
s.bind(socket.BDADDR_ANY.encode() + b'\0')
2731+
with self.assertRaises(ValueError):
2732+
s.bind(socket.BDADDR_ANY.encode() + b' '*100)
2733+
with self.assertRaises(OSError):
2734+
s.bind(b'invalid')
2735+
else:
2736+
dev = 0
2737+
with self.assertRaises(OSError):
2738+
s.bind(())
2739+
with self.assertRaises(OSError):
2740+
s.bind((dev, 0))
2741+
with self.assertRaises(OSError):
2742+
s.bind(dev)
2743+
with self.assertRaises(OSError):
2744+
s.bind(socket.BDADDR_ANY.encode())
2745+
26932746
@unittest.skipUnless(hasattr(socket, 'BTPROTO_SCO'), 'Bluetooth SCO sockets required for this test')
26942747
def testBindScoSocket(self):
26952748
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_SCO) as s:
26962749
s.bind(socket.BDADDR_ANY.encode())
26972750
addr = s.getsockname()
26982751
self.assertEqual(addr, socket.BDADDR_ANY)
26992752

2753+
@unittest.skipUnless(hasattr(socket, 'BTPROTO_SCO'), 'Bluetooth SCO sockets required for this test')
2754+
def testBadScoAddr(self):
2755+
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_SCO) as s:
2756+
with self.assertRaises(OSError):
2757+
s.bind(socket.BDADDR_ANY)
2758+
with self.assertRaises(OSError):
2759+
s.bind((socket.BDADDR_ANY.encode(),))
2760+
with self.assertRaises(OSError):
2761+
s.bind(b'invalid')
2762+
27002763

27012764
@unittest.skipUnless(HAVE_SOCKET_HYPERV,
27022765
'Hyper-V sockets required for this test.')

0 commit comments

Comments
 (0)
0