Source
tests/purrc/app_netcat_test.cpp
1
// Copyright (c) 2026 BigBrain LLC. MIT-licensed (see LICENSE).2
// Original work; see ACKNOWLEDGMENTS.md for the open-source ideas we build upon.3
// System-level "application" test: a small TCP round-trip ("netcat"-style) app4
// that only passes if socket + string + io all cooperate end to end.5
//6
// This is deliberately a multi-module integration program, not a single-function7
// probe: it stands up a real loopback TCP connection, builds a message with the8
// `string` and `io` modules, ships it over the socket, reads it back on the peer9
// fd, and verifies byte-exactness -- all inside a SINGLE cheatah process.10
//11
// Why it doesn't deadlock in one process: for 127.0.0.1 the kernel completes the12
// TCP handshake on connect() and queues the connection on the listener's accept13
// backlog, so connect() returns before accept() is called and the subsequent14
// accept() returns immediately. The payload is small enough to fit in the socket15
// send buffer, so sendall() does not block waiting for a reader. Output is fully16
// deterministic (only booleans + fixed labels), so it can be asserted17
// byte-for-byte.18
//19
// Modules exercised: socket (tcp_listen/local_port/tcp_connect/accept/sendall/20
// recv/close), string (upper, concat), io (str, print), plus the builtin len().22
#include "e2e_harness.hpp"25
TEST(SystemApps, NetworkRoundtrip) {26
e2e::expect_e2e("app_netcat", R"PURR(import io27
import string28
import socket30
let listener = socket.tcp_listen("127.0.0.1", 0, 8)31
let port = socket.local_port(listener)32
let client = socket.tcp_connect("127.0.0.1", port)33
let server = socket.accept(listener)35
let payload = string.upper("ping") + " " + io.str(port > 0)36
let sent = socket.sendall(client, payload)37
let got = socket.recv(server, 1024)39
io.print("listen_ok", listener >= 0)40
io.print("port_ok", port > 0)41
io.print("connect_ok", client >= 0)42
io.print("accept_ok", server >= 0)43
io.print("send_ok", sent == 0)44
io.print("len_match", len(got) == len(payload))45
io.print("bytes_match", got == payload)47
socket.close(server)48
socket.close(client)49
socket.close(listener)50
)PURR",51
"listen_ok True\n"52
"port_ok True\n"53
"connect_ok True\n"54
"accept_ok True\n"55
"send_ok True\n"56
"len_match True\n"57
"bytes_match True\n");58
}