From 3b0831cf0e499aaf567319506bbcaa604415bd0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juozapas=20Bo=C4=8Dkus?= Date: Thu, 21 Nov 2024 16:16:06 +0200 Subject: [PATCH 1/7] Add natlab test for start telio with tun --- .unreleased/LLT-3424 | 0 nat-lab/tests/telio.py | 12 ++++++++ nat-lab/tests/test_start_with_tun.py | 40 +++++++++++++++++++++++++ nat-lab/tests/uniffi/libtelio_proxy.py | 10 +++++++ nat-lab/tests/uniffi/libtelio_remote.py | 23 ++++++++++++++ 5 files changed, 85 insertions(+) create mode 100644 .unreleased/LLT-3424 create mode 100644 nat-lab/tests/test_start_with_tun.py diff --git a/.unreleased/LLT-3424 b/.unreleased/LLT-3424 new file mode 100644 index 000000000..e69de29bb diff --git a/nat-lab/tests/telio.py b/nat-lab/tests/telio.py index d6dc85b72..f954ea2e7 100644 --- a/nat-lab/tests/telio.py +++ b/nat-lab/tests/telio.py @@ -514,6 +514,18 @@ async def simple_start(self): if isinstance(self.get_router(), LinuxRouter): await self.get_proxy().set_fwmark(int(LINUX_FWMARK_VALUE)) + async def create_tun(self) -> int: + return await self.get_proxy().create_tun() + + async def start_with_tun(self, tun: int): + await self.get_proxy().start_with_tun( + private_key=self._node.private_key, + adapter=self._adapter_type, + tun=tun, + ) + if isinstance(self.get_router(), LinuxRouter): + await self.get_proxy().set_fwmark(int(LINUX_FWMARK_VALUE)) + async def wait_for_state_peer( self, public_key, diff --git a/nat-lab/tests/test_start_with_tun.py b/nat-lab/tests/test_start_with_tun.py new file mode 100644 index 000000000..1a761680b --- /dev/null +++ b/nat-lab/tests/test_start_with_tun.py @@ -0,0 +1,40 @@ +from typing import List +from contextlib import AsyncExitStack +from helpers import SetupParameters, ping_between_all_nodes, setup_mesh_nodes +from utils.bindings import ( + default_features, + TelioAdapterType, +) +from utils.connection_util import ConnectionTag + + +def _generate_setup_parameters( + conn_tags: List[ConnectionTag], +) -> List[SetupParameters]: + return [ + SetupParameters( + connection_tag=conn_tag, + adapter_type_override=TelioAdapterType.BORING_TUN, + features=default_features(), + fingerprint=f"{conn_tag}", + ) + for conn_tag in conn_tags + ] + + +async def test_start_with_tun() -> None: + setup_params = _generate_setup_parameters( + [ + ConnectionTag.DOCKER_CONE_CLIENT_1, + ConnectionTag.DOCKER_CONE_CLIENT_2, + ] + ) + + async with AsyncExitStack() as exit_stack: + env = await setup_mesh_nodes(exit_stack, setup_params) + alpha_client, _ = env.clients + + await alpha_client.stop_device() + tun = await alpha_client.create_tun() + await alpha_client.start_with_tun(tun) + await ping_between_all_nodes(env) diff --git a/nat-lab/tests/uniffi/libtelio_proxy.py b/nat-lab/tests/uniffi/libtelio_proxy.py index dfccad918..6d0064905 100644 --- a/nat-lab/tests/uniffi/libtelio_proxy.py +++ b/nat-lab/tests/uniffi/libtelio_proxy.py @@ -99,6 +99,16 @@ def start_named(self, private_key, adapter, name: str): lambda r: r.start_named(private_key, adapter.value, name) ) + @move_to_async_thread + def create_tun(self) -> int: + return self._handle_remote_error(lambda r: r.crete_tun()) + + @move_to_async_thread + def start_with_tun(self, private_key, adapter, tun: int): + self._handle_remote_error( + lambda r: r.start_with_tun(private_key, adapter.value, tun) + ) + @move_to_async_thread def set_fwmark(self, fwmark: int): self._handle_remote_error(lambda r: r.set_fwmark(fwmark)) diff --git a/nat-lab/tests/uniffi/libtelio_remote.py b/nat-lab/tests/uniffi/libtelio_remote.py index be4404627..2e2281d19 100644 --- a/nat-lab/tests/uniffi/libtelio_remote.py +++ b/nat-lab/tests/uniffi/libtelio_remote.py @@ -1,5 +1,7 @@ import datetime +import fcntl import os +import struct import Pyro5.api # type: ignore import Pyro5.server # type: ignore import shutil @@ -109,6 +111,27 @@ def start_named(self, private_key, adapter, name: str): private_key, libtelio.TelioAdapterType(adapter), name ) + # TODO pass name + @serialize_error + def create_tun(self) -> int: + # Constants for TUN/TAP interface creation (from Linux's if_tun.h) + TUNSETIFF = 0x400454CA + IFF_TUN = 0x0001 + IFF_NO_PI = 0x1000 + + tun_fd = os.open("/dev/net/tun", os.O_RDWR) + # '16sH' means we need to pass 16-byte string (interface name) and 2-byte short (flags) + ifr = struct.pack("16sH", b"tun0", IFF_TUN | IFF_NO_PI) + fcntl.ioctl(tun_fd, TUNSETIFF, ifr) + + return tun_fd + + @serialize_error + def start_with_tun(self, private_key, adapter, tun: int): + self._libtelio.start_with_tun( + private_key, libtelio.TelioAdapterType(adapter), tun + ) + @serialize_error def set_fwmark(self, fwmark: int): self._libtelio.set_fwmark(fwmark) From 4fac5aab85ae4f4c1305ecbd5f13ff580bbe85f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juozapas=20Bo=C4=8Dkus?= Date: Mon, 25 Nov 2024 11:25:37 +0200 Subject: [PATCH 2/7] Remove comment --- nat-lab/tests/uniffi/libtelio_remote.py | 1 - 1 file changed, 1 deletion(-) diff --git a/nat-lab/tests/uniffi/libtelio_remote.py b/nat-lab/tests/uniffi/libtelio_remote.py index 2e2281d19..5d942991e 100644 --- a/nat-lab/tests/uniffi/libtelio_remote.py +++ b/nat-lab/tests/uniffi/libtelio_remote.py @@ -111,7 +111,6 @@ def start_named(self, private_key, adapter, name: str): private_key, libtelio.TelioAdapterType(adapter), name ) - # TODO pass name @serialize_error def create_tun(self) -> int: # Constants for TUN/TAP interface creation (from Linux's if_tun.h) From 8768ae8f044feb039057581144bd944026714b6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juozapas=20Bo=C4=8Dkus?= Date: Mon, 25 Nov 2024 14:08:09 +0200 Subject: [PATCH 3/7] Fix typo --- nat-lab/tests/uniffi/libtelio_proxy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nat-lab/tests/uniffi/libtelio_proxy.py b/nat-lab/tests/uniffi/libtelio_proxy.py index 6d0064905..2289ae9ad 100644 --- a/nat-lab/tests/uniffi/libtelio_proxy.py +++ b/nat-lab/tests/uniffi/libtelio_proxy.py @@ -101,7 +101,7 @@ def start_named(self, private_key, adapter, name: str): @move_to_async_thread def create_tun(self) -> int: - return self._handle_remote_error(lambda r: r.crete_tun()) + return self._handle_remote_error(lambda r: r.create_tun()) @move_to_async_thread def start_with_tun(self, private_key, adapter, tun: int): From cffb45d6f0bbe55db284c0bdef052f63a19b8731 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juozapas=20Bo=C4=8Dkus?= Date: Mon, 25 Nov 2024 17:33:58 +0200 Subject: [PATCH 4/7] Fix --- nat-lab/tests/uniffi/libtelio_remote.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nat-lab/tests/uniffi/libtelio_remote.py b/nat-lab/tests/uniffi/libtelio_remote.py index 5d942991e..b293bb8f9 100644 --- a/nat-lab/tests/uniffi/libtelio_remote.py +++ b/nat-lab/tests/uniffi/libtelio_remote.py @@ -120,7 +120,7 @@ def create_tun(self) -> int: tun_fd = os.open("/dev/net/tun", os.O_RDWR) # '16sH' means we need to pass 16-byte string (interface name) and 2-byte short (flags) - ifr = struct.pack("16sH", b"tun0", IFF_TUN | IFF_NO_PI) + ifr = struct.pack("16sH", b"tun11", IFF_TUN | IFF_NO_PI) fcntl.ioctl(tun_fd, TUNSETIFF, ifr) return tun_fd From 06326c9e268f8ce3d9b737c676afaac9e4f2e21f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juozapas=20Bo=C4=8Dkus?= Date: Mon, 25 Nov 2024 17:44:00 +0200 Subject: [PATCH 5/7] Make tun name configurable --- nat-lab/tests/telio.py | 4 ++-- nat-lab/tests/test_start_with_tun.py | 2 +- nat-lab/tests/uniffi/libtelio_proxy.py | 4 ++-- nat-lab/tests/uniffi/libtelio_remote.py | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/nat-lab/tests/telio.py b/nat-lab/tests/telio.py index f954ea2e7..b8e867986 100644 --- a/nat-lab/tests/telio.py +++ b/nat-lab/tests/telio.py @@ -514,8 +514,8 @@ async def simple_start(self): if isinstance(self.get_router(), LinuxRouter): await self.get_proxy().set_fwmark(int(LINUX_FWMARK_VALUE)) - async def create_tun(self) -> int: - return await self.get_proxy().create_tun() + async def create_tun(self, tun_name: bytes) -> int: + return await self.get_proxy().create_tun(tun_name) async def start_with_tun(self, tun: int): await self.get_proxy().start_with_tun( diff --git a/nat-lab/tests/test_start_with_tun.py b/nat-lab/tests/test_start_with_tun.py index 1a761680b..e36b01a21 100644 --- a/nat-lab/tests/test_start_with_tun.py +++ b/nat-lab/tests/test_start_with_tun.py @@ -35,6 +35,6 @@ async def test_start_with_tun() -> None: alpha_client, _ = env.clients await alpha_client.stop_device() - tun = await alpha_client.create_tun() + tun = await alpha_client.create_tun(b"tun11") await alpha_client.start_with_tun(tun) await ping_between_all_nodes(env) diff --git a/nat-lab/tests/uniffi/libtelio_proxy.py b/nat-lab/tests/uniffi/libtelio_proxy.py index 2289ae9ad..b27079523 100644 --- a/nat-lab/tests/uniffi/libtelio_proxy.py +++ b/nat-lab/tests/uniffi/libtelio_proxy.py @@ -100,8 +100,8 @@ def start_named(self, private_key, adapter, name: str): ) @move_to_async_thread - def create_tun(self) -> int: - return self._handle_remote_error(lambda r: r.create_tun()) + def create_tun(self, tun_name: bytes) -> int: + return self._handle_remote_error(lambda r: r.create_tun(tun_name)) @move_to_async_thread def start_with_tun(self, private_key, adapter, tun: int): diff --git a/nat-lab/tests/uniffi/libtelio_remote.py b/nat-lab/tests/uniffi/libtelio_remote.py index b293bb8f9..d0f10e6a8 100644 --- a/nat-lab/tests/uniffi/libtelio_remote.py +++ b/nat-lab/tests/uniffi/libtelio_remote.py @@ -112,7 +112,7 @@ def start_named(self, private_key, adapter, name: str): ) @serialize_error - def create_tun(self) -> int: + def create_tun(self, tun_name: bytes) -> int: # Constants for TUN/TAP interface creation (from Linux's if_tun.h) TUNSETIFF = 0x400454CA IFF_TUN = 0x0001 @@ -120,7 +120,7 @@ def create_tun(self) -> int: tun_fd = os.open("/dev/net/tun", os.O_RDWR) # '16sH' means we need to pass 16-byte string (interface name) and 2-byte short (flags) - ifr = struct.pack("16sH", b"tun11", IFF_TUN | IFF_NO_PI) + ifr = struct.pack("16sH", tun_name, IFF_TUN | IFF_NO_PI) fcntl.ioctl(tun_fd, TUNSETIFF, ifr) return tun_fd From e43220389730aefcbd88ff55b6d283483830ff04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juozapas=20Bo=C4=8Dkus?= Date: Tue, 26 Nov 2024 10:32:52 +0200 Subject: [PATCH 6/7] Remove forced type --- nat-lab/tests/telio.py | 2 +- nat-lab/tests/uniffi/libtelio_proxy.py | 2 +- nat-lab/tests/uniffi/libtelio_remote.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/nat-lab/tests/telio.py b/nat-lab/tests/telio.py index b8e867986..fd380c424 100644 --- a/nat-lab/tests/telio.py +++ b/nat-lab/tests/telio.py @@ -514,7 +514,7 @@ async def simple_start(self): if isinstance(self.get_router(), LinuxRouter): await self.get_proxy().set_fwmark(int(LINUX_FWMARK_VALUE)) - async def create_tun(self, tun_name: bytes) -> int: + async def create_tun(self, tun_name) -> int: return await self.get_proxy().create_tun(tun_name) async def start_with_tun(self, tun: int): diff --git a/nat-lab/tests/uniffi/libtelio_proxy.py b/nat-lab/tests/uniffi/libtelio_proxy.py index b27079523..ce634b7a3 100644 --- a/nat-lab/tests/uniffi/libtelio_proxy.py +++ b/nat-lab/tests/uniffi/libtelio_proxy.py @@ -100,7 +100,7 @@ def start_named(self, private_key, adapter, name: str): ) @move_to_async_thread - def create_tun(self, tun_name: bytes) -> int: + def create_tun(self, tun_name) -> int: return self._handle_remote_error(lambda r: r.create_tun(tun_name)) @move_to_async_thread diff --git a/nat-lab/tests/uniffi/libtelio_remote.py b/nat-lab/tests/uniffi/libtelio_remote.py index d0f10e6a8..ead1a98ab 100644 --- a/nat-lab/tests/uniffi/libtelio_remote.py +++ b/nat-lab/tests/uniffi/libtelio_remote.py @@ -112,7 +112,7 @@ def start_named(self, private_key, adapter, name: str): ) @serialize_error - def create_tun(self, tun_name: bytes) -> int: + def create_tun(self, tun_name) -> int: # Constants for TUN/TAP interface creation (from Linux's if_tun.h) TUNSETIFF = 0x400454CA IFF_TUN = 0x0001 From 98b22dd73b77d18f1896c44fd71e03e0efc56845 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juozapas=20Bo=C4=8Dkus?= Date: Tue, 26 Nov 2024 13:45:11 +0200 Subject: [PATCH 7/7] Fix --- nat-lab/tests/telio.py | 2 +- nat-lab/tests/uniffi/libtelio_proxy.py | 2 +- nat-lab/tests/uniffi/libtelio_remote.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/nat-lab/tests/telio.py b/nat-lab/tests/telio.py index fd380c424..b8e867986 100644 --- a/nat-lab/tests/telio.py +++ b/nat-lab/tests/telio.py @@ -514,7 +514,7 @@ async def simple_start(self): if isinstance(self.get_router(), LinuxRouter): await self.get_proxy().set_fwmark(int(LINUX_FWMARK_VALUE)) - async def create_tun(self, tun_name) -> int: + async def create_tun(self, tun_name: bytes) -> int: return await self.get_proxy().create_tun(tun_name) async def start_with_tun(self, tun: int): diff --git a/nat-lab/tests/uniffi/libtelio_proxy.py b/nat-lab/tests/uniffi/libtelio_proxy.py index ce634b7a3..b27079523 100644 --- a/nat-lab/tests/uniffi/libtelio_proxy.py +++ b/nat-lab/tests/uniffi/libtelio_proxy.py @@ -100,7 +100,7 @@ def start_named(self, private_key, adapter, name: str): ) @move_to_async_thread - def create_tun(self, tun_name) -> int: + def create_tun(self, tun_name: bytes) -> int: return self._handle_remote_error(lambda r: r.create_tun(tun_name)) @move_to_async_thread diff --git a/nat-lab/tests/uniffi/libtelio_remote.py b/nat-lab/tests/uniffi/libtelio_remote.py index ead1a98ab..d0f10e6a8 100644 --- a/nat-lab/tests/uniffi/libtelio_remote.py +++ b/nat-lab/tests/uniffi/libtelio_remote.py @@ -112,7 +112,7 @@ def start_named(self, private_key, adapter, name: str): ) @serialize_error - def create_tun(self, tun_name) -> int: + def create_tun(self, tun_name: bytes) -> int: # Constants for TUN/TAP interface creation (from Linux's if_tun.h) TUNSETIFF = 0x400454CA IFF_TUN = 0x0001