neotalk/tests/use_cases/test_handshake.py
Andros Fenollosa bfd8b510b6 Initial neotalk: P2P encrypted chat over UDP discovery + TCP streaming
Clean architecture (core use cases + infra gateways), X25519/AES-GCM E2E
encryption, tkinter GUI, and a pytest suite. 45 tests passing.
2026-08-21 09:16:07 +02:00

46 lines
1.5 KiB
Python

"""Tests for building and reading the TCP handshake."""
from neotalk.core.entities.constants import FrameType
from neotalk.core.entities.responses import ResponseTypes
from neotalk.core.use_cases.conversation.handshake import (
build_handshake_use_case,
read_handshake_use_case,
)
def test_handshake_round_trips(identity):
# Given a handshake built from our identity
built = build_handshake_use_case(identity)
frame = built["data"]["frame"]
# When the other side reads it
read = read_handshake_use_case(frame)
# Then the identity fields survive the trip
assert read["type"] == ResponseTypes.SUCCESS
assert read["data"]["peer_id"] == identity.peer_id
assert read["data"]["alias"] == identity.alias
assert read["data"]["public_key"] == identity.public_key
def test_read_handshake_rejects_wrong_frame_type():
# Given a non-handshake frame
frame = {"type": FrameType.SECURE, "body": "..."}
# When reading it as a handshake
response = read_handshake_use_case(frame)
# Then it is rejected
assert response["type"] == ResponseTypes.PARAMETERS_ERROR
def test_read_handshake_rejects_missing_alias():
# Given a handshake missing its alias
frame = {"type": FrameType.HANDSHAKE, "peer_id": "id", "pubkey": "AA=="}
# When reading it
response = read_handshake_use_case(frame)
# Then it is a parameters error
assert response["type"] == ResponseTypes.PARAMETERS_ERROR
assert response["errors"][0]["field"] == "alias"