cheatah
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) app
4// 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-function
7// probe: it stands up a real loopback TCP connection, builds a message with the
8// `string` and `io` modules, ships it over the socket, reads it back on the peer
9// 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 the
12// TCP handshake on connect() and queues the connection on the listener's accept
13// backlog, so connect() returns before accept() is called and the subsequent
14// accept() returns immediately. The payload is small enough to fit in the socket
15// send buffer, so sendall() does not block waiting for a reader. Output is fully
16// deterministic (only booleans + fixed labels), so it can be asserted
17// 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"
25TEST(SystemApps, NetworkRoundtrip) {
26 e2e::expect_e2e("app_netcat", R"PURR(import io
27import string
28import socket
30let listener = socket.tcp_listen("127.0.0.1", 0, 8)
31let port = socket.local_port(listener)
32let client = socket.tcp_connect("127.0.0.1", port)
33let server = socket.accept(listener)
35let payload = string.upper("ping") + " " + io.str(port > 0)
36let sent = socket.sendall(client, payload)
37let got = socket.recv(server, 1024)
39io.print("listen_ok", listener >= 0)
40io.print("port_ok", port > 0)
41io.print("connect_ok", client >= 0)
42io.print("accept_ok", server >= 0)
43io.print("send_ok", sent == 0)
44io.print("len_match", len(got) == len(payload))
45io.print("bytes_match", got == payload)
47socket.close(server)
48socket.close(client)
49socket.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");