#!/usr/bin/env python3 """ End-to-end TCP integration test for the compiled native moon-i2p SAMv3 client. """ import os import re import subprocess import sys import time sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from mock_sam import MockSamBridge REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) CLIENT_BIN = os.path.join( REPO_ROOT, "_build", "native", "debug", "build", "cmd", "client", "client.exe" ) def run_client(sam_addr: str, args: list[str], stdin_data: str | None = None) -> subprocess.CompletedProcess: cmd = [CLIENT_BIN, "--sam", sam_addr] + args return subprocess.run( cmd, input=stdin_data, text=True, capture_output=True, timeout=15, check=True, ) def main() -> None: if not os.path.exists(CLIENT_BIN): raise SystemExit(f"Client binary not found: {CLIENT_BIN}\nRun: moon build --target native cmd/client") bridge = MockSamBridge("127.0.0.1", 0) eepsite_b32 = bridge.register_mock_eepsite("hello.i2p") port = bridge.start() sam_addr = f"127.0.0.1:{port}" print(f"[e2e] Started loopback SAMv3 bridge on {sam_addr} (hello.i2p -> {eepsite_b32})") try: # 1. keygen res = run_client(sam_addr, ["keygen"]) assert "SAM Version: 3.3" in res.stdout, res.stdout m = re.search(r"B32 Address:\s+([a-z2-7]{52}\.b32\.i2p)", res.stdout) assert m is not None, f"missing valid b32 in keygen output:\n{res.stdout}" assert "Public:" in res.stdout and "Private:" in res.stdout, res.stdout print(f"[e2e] PASS keygen -> {m.group(1)}") # 2. lookup res = run_client(sam_addr, ["lookup", "hello.i2p"]) assert f"B32 Address: {eepsite_b32}" in res.stdout, res.stdout print(f"[e2e] PASS lookup hello.i2p -> {eepsite_b32}") # 3. http-get against mock eepsite over SAMv3 STREAM res = run_client(sam_addr, ["http-get", "hello.i2p", "/status"]) assert "HTTP/1.1 200 OK" in res.stdout, res.stdout assert f"Welcome to hello.i2p ({eepsite_b32})" in res.stdout, res.stdout assert "Path: /status" in res.stdout, res.stdout assert ".b32.i2p" in res.stdout, res.stdout print("[e2e] PASS http-get hello.i2p /status -> HTTP/1.1 200 OK") # 4. listen + dial: two real MoonBit native client processes talking over SAMv3! listener = subprocess.Popen( [CLIENT_BIN, "--sam", sam_addr, "listen", "pong from moon-i2p listener"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, ) listener_b32 = None assert listener.stdout is not None for _ in range(10): line = listener.stdout.readline() if not line: break m = re.search(r"B32 Address:\s+([a-z2-7]{52}\.b32\.i2p)", line) if m: listener_b32 = m.group(1) if "Waiting for incoming I2P stream..." in line: break assert listener_b32 is not None, "listener did not print its .b32.i2p address" # Give listener a tiny moment to issue STREAM ACCEPT time.sleep(0.15) dial_res = run_client(sam_addr, ["dial", listener_b32, "ping from moon-i2p dialer"]) assert "pong from moon-i2p listener" in dial_res.stdout, dial_res.stdout listen_rest, listen_err = listener.communicate(timeout=10) assert listener.returncode == 0, f"listener failed: {listen_err}" assert "Accepted from:" in listen_rest, listen_rest assert "Received: ping from moon-i2p dialer" in listen_rest, listen_rest print(f"[e2e] PASS listen + dial ({listener_b32}) bidirectional exchange") # 5. dial with stdin payload listener2 = subprocess.Popen( [CLIENT_BIN, "--sam", sam_addr, "listen", "stdin-ack"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, ) listener2_b32 = None assert listener2.stdout is not None for _ in range(10): line = listener2.stdout.readline() if not line: break m = re.search(r"B32 Address:\s+([a-z2-7]{52}\.b32\.i2p)", line) if m: listener2_b32 = m.group(1) if "Waiting for incoming I2P stream..." in line: break assert listener2_b32 is not None time.sleep(0.15) dial_stdin = run_client(sam_addr, ["dial", listener2_b32], stdin_data="hello via stdin") assert "stdin-ack" in dial_stdin.stdout, dial_stdin.stdout listen2_rest, _ = listener2.communicate(timeout=10) assert "Received: hello via stdin" in listen2_rest, listen2_rest print("[e2e] PASS dial (stdin) + listen") print("[e2e] ALL END-TO-END TESTS PASSED!") finally: bridge.stop() if __name__ == "__main__": main()