Some checks failed
Windows project source guards / test (push) Failing after 3m35s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
67 lines
3.2 KiB
Python
67 lines
3.2 KiB
Python
"""Which family carries the connection to the server: read from the OS table of real TCP connections (Windows).
|
|
|
|
Real sockets, real ``GetExtendedTcpTable``: a listener and a client on ``::1`` and on ``127.0.0.1`` of this machine, and one real
|
|
connection to a public server. Elsewhere the tests skip: the table reader exists only on Windows.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import socket
|
|
import unittest
|
|
|
|
from xray_fluent.network import connection_family
|
|
|
|
|
|
class FamilyOfConnectionsTests(unittest.TestCase):
|
|
def test_the_family_is_read_from_the_connections_to_the_servers_addresses(self) -> None:
|
|
table = [("a.exe", "2001:db8::5"), ("b.exe", "192.0.2.9"), ("c.exe", "192.0.2.10"), ("d.exe", "203.0.113.1")]
|
|
self.assertEqual(6, connection_family.family_of_connections(table, ["2001:db8::5"]))
|
|
self.assertEqual(4, connection_family.family_of_connections(table, ["192.0.2.9", "2001:db8::6"]))
|
|
self.assertEqual(4, connection_family.family_of_connections(table, ["192.0.2.9", "192.0.2.10", "2001:db8::5"]))
|
|
self.assertIsNone(connection_family.family_of_connections(table, ["198.51.100.7"]))
|
|
self.assertIsNone(connection_family.family_of_connections([], ["192.0.2.9"]))
|
|
|
|
def test_a_pinned_family_is_taken_as_it_is_and_nothing_is_read(self) -> None:
|
|
self.assertEqual(6, connection_family.observe("never-resolved.invalid", pinned=6))
|
|
self.assertEqual(4, connection_family.observe("never-resolved.invalid", pinned=4))
|
|
|
|
def test_labels(self) -> None:
|
|
self.assertEqual(("IPv6", "IPv4", ""), (connection_family.label(6), connection_family.label(4), connection_family.label(None)))
|
|
|
|
|
|
@unittest.skipUnless(os.name == "nt", "the TCP table reader is Windows only")
|
|
class RealTcpTableTests(unittest.TestCase):
|
|
def _connected_pair(self, family: int, address: str):
|
|
listener = socket.socket(family, socket.SOCK_STREAM)
|
|
listener.bind((address, 0))
|
|
listener.listen(1)
|
|
client = socket.create_connection(listener.getsockname()[:2], timeout=5)
|
|
accepted, _ = listener.accept()
|
|
return listener, client, accepted
|
|
|
|
def test_loopback_connections_of_both_families_are_found_by_address(self) -> None:
|
|
for family, address, expected in ((socket.AF_INET6, "::1", 6), (socket.AF_INET, "127.0.0.1", 4)):
|
|
with self.subTest(address=address):
|
|
try:
|
|
listener, client, accepted = self._connected_pair(family, address)
|
|
except OSError as exc:
|
|
self.skipTest(f"{address} is not available here: {exc}")
|
|
try:
|
|
self.assertEqual(expected, connection_family.observe(address))
|
|
finally:
|
|
for item in (client, accepted, listener):
|
|
item.close()
|
|
|
|
def test_a_real_connection_to_a_public_server_is_seen_as_ipv4_on_a_network_without_ipv6(self) -> None:
|
|
try:
|
|
client = socket.create_connection(("1.1.1.1", 443), timeout=8)
|
|
except OSError as exc:
|
|
self.skipTest(f"no route to the public test server: {exc}")
|
|
try:
|
|
self.assertEqual(4, connection_family.observe("1.1.1.1"))
|
|
finally:
|
|
client.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|