forked from microsoft/debugpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_socket.py
More file actions
45 lines (39 loc) · 1.76 KB
/
Copy pathtest_socket.py
File metadata and controls
45 lines (39 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE in the project root
# for license information.
import pytest
import sys
from debugpy.common import sockets
class TestSocketServerReuse(object):
HOST1 = "127.0.0.1"
# NOTE: Windows allows loopback range 127/8. Some flavors of Linux support
# 127/8 range. Mac by default supports only 127/0. Configuring /etc/network/interface
# for this one test is overkill so use '0.0.0.0' on Mac instead.
HOST2 = "127.0.0.2" if sys.platform != "darwin" else "0.0.0.0"
def test_reuse_same_address_port(self):
# NOTE: This test should ensure that same address port can be used by two
# sockets. This to prevent accidental changes to socket options. In Windows
# SO_REUSEADDR flag allows two sockets to bind to same address:port combination.
# Windows should always use SO_EXCLUSIVEADDRUSE
sock1 = sockets.create_server(self.HOST1, 0)
try:
_, PORT1 = sock1.getsockname()
with pytest.raises(Exception):
sockets.create_server(self.HOST1, PORT1)
finally:
sockets.close_socket(sock1)
def test_reuse_same_port(self):
try:
sock1, sock2 = None, None
sock1 = sockets.create_server(self.HOST1, 0)
_, PORT1 = sock1.getsockname()
sock2 = sockets.create_server(self.HOST2, PORT1)
assert sock1.getsockname() == (self.HOST1, PORT1)
assert sock2.getsockname() == (self.HOST2, PORT1)
except Exception:
pytest.fail()
finally:
if sock1 is not None:
sockets.close_socket(sock1)
if sock2 is not None:
sockets.close_socket(sock2)