From 37105260f649a43f64ab610020afdf82e65a219b Mon Sep 17 00:00:00 2001 From: Ian McIntyre Date: Sat, 9 Dec 2023 14:10:46 -0500 Subject: [PATCH] Add simple tool for TCP loopback testing Monitor your board's serial log for the IP address it's assigned. Then, pass it into this command-line tool to make sure you can connect, send a message, and receive the response. --- Cargo.toml | 1 + tools/Cargo.toml | 11 +++++++++++ tools/src/bin/tcp_loopback.rs | 24 ++++++++++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 tools/Cargo.toml create mode 100644 tools/src/bin/tcp_loopback.rs diff --git a/Cargo.toml b/Cargo.toml index d64d5152..7f02b7f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -87,6 +87,7 @@ eh02-unproven = [] members = [ "board", "logging", + "tools", ] [workspace.dependencies] diff --git a/tools/Cargo.toml b/tools/Cargo.toml new file mode 100644 index 00000000..7c7de721 --- /dev/null +++ b/tools/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "tools" +version = "0.1.0" +repository.workspace = true +keywords.workspace = true +categories.workspace = true +license.workspace = true +edition.workspace = true +publish = false + +[dependencies] diff --git a/tools/src/bin/tcp_loopback.rs b/tools/src/bin/tcp_loopback.rs new file mode 100644 index 00000000..6450c6ee --- /dev/null +++ b/tools/src/bin/tcp_loopback.rs @@ -0,0 +1,24 @@ +use std::io::prelude::*; +use std::net::TcpStream; + +fn main() -> Result<(), Box> { + let ip = std::env::args() + .skip(1) + .next() + .ok_or("Provide an IPv4 address")?; + + let ip: std::net::Ipv4Addr = ip.parse()?; + + let mut client = TcpStream::connect(std::net::SocketAddrV4::new(ip, 5000))?; + + const MSG: &[u8] = b"Hello, world!"; + client.write(MSG)?; + let mut resp = [0; MSG.len()]; + client.read(&mut resp)?; + + if resp == MSG { + Ok(()) + } else { + Err(format!("Expected '{MSG:?}' but received '{resp:?}'").into()) + } +}