Skip to content

Commit 9543e99

Browse files
committed
Add test-suite
1 parent b1af75f commit 9543e99

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

tests/core/security/test_pnet.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import pytest
2+
import trio
3+
4+
from libp2p.io.abc import ReadWriteCloser
5+
from libp2p.network.connection.raw_connection import RawConnection
6+
from libp2p.security.pnet.protector import new_protected_conn
7+
8+
9+
# --- MemoryPipe: implements ReadWriteCloser interface ---
10+
class MemoryPipe(ReadWriteCloser):
11+
"""Wrap a pair of Trio memory channels into a ReadWriteCloser-like object."""
12+
13+
def __init__(
14+
self, send: trio.MemorySendChannel, receive: trio.MemoryReceiveChannel
15+
):
16+
self._send = send
17+
self._receive = receive
18+
19+
async def read(self, n: int | None = None) -> bytes:
20+
"""Read next chunk from receive channel."""
21+
return await self._receive.receive()
22+
23+
async def write(self, data: bytes) -> None:
24+
"""Write a chunk to send channel."""
25+
await self._send.send(data)
26+
27+
async def close(self) -> None:
28+
"""Close channels (noop for memory channels)."""
29+
pass
30+
31+
def get_remote_address(self) -> tuple[str, int] | None:
32+
# Memory pipe doesn’t have a real address, so return None
33+
return None
34+
35+
36+
# --- Helper function to create a connected pair of PskConns ---
37+
async def make_psk_pair(psk_hex: str):
38+
send1, recv1 = trio.open_memory_channel(0)
39+
send2, recv2 = trio.open_memory_channel(0)
40+
41+
pipe1 = MemoryPipe(send1, recv2)
42+
pipe2 = MemoryPipe(send2, recv1)
43+
44+
raw1 = RawConnection(pipe1, False)
45+
raw2 = RawConnection(pipe2, False)
46+
47+
# NOTE: The new_protected_conn function needs to perform the handshake.
48+
# We'll assume it does for this example. If not, a handshake() call
49+
# might be needed here within a nursery.
50+
psk_conn1 = new_protected_conn(raw1, psk_hex)
51+
psk_conn2 = new_protected_conn(raw2, psk_hex)
52+
53+
return psk_conn1, psk_conn2
54+
55+
56+
@pytest.mark.trio
57+
async def test_psk_simple_message():
58+
# Use a fixed PSK for testing
59+
psk_hex = "dffb7e3135399a8b1612b2aaca1c36a3a8ac2cd0cca51ceeb2ced87d308cac6d"
60+
conn1, conn2 = await make_psk_pair(psk_hex)
61+
62+
msg = b"hello world"
63+
64+
async with trio.open_nursery() as nursery:
65+
nursery.start_soon(conn1.write, msg)
66+
received = await conn2.read(len(msg))
67+
68+
assert received == msg, "Decrypted message does not match original"
69+
70+
71+
@pytest.mark.trio
72+
async def test_psk_empty_message():
73+
# PSK for testing
74+
psk_hex = "dffb7e3135399a8b1612b2aaca1c36a3a8ac2cd0cca51ceeb2ced87d308cac6d"
75+
conn1, conn2 = await make_psk_pair(psk_hex)
76+
77+
# Empty message should round-trip correctly
78+
async with trio.open_nursery() as nursery:
79+
nursery.start_soon(conn1.write, b"")
80+
received = await conn2.read(0)
81+
82+
assert received == b"", "Empty message failed"

0 commit comments

Comments
 (0)