Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra test for rust-libp2p ping protocol #3242

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,229 changes: 2,216 additions & 13 deletions src/Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ add_subdirectory(file)
add_subdirectory(futex)
add_subdirectory(golang)
add_subdirectory(ifaddrs)
add_subdirectory(libp2p)
add_subdirectory(memory)
add_subdirectory(netlink)
add_subdirectory(phold)
Expand Down
7 changes: 7 additions & 0 deletions src/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,16 @@ path = "time/nanosleep/test_nanosleep.rs"
name = "test_prctl"
path = "prctl/test_prctl.rs"

[[bin]]
name = "test_libp2p_ping"
path = "libp2p/ping/test_ping.rs"

[dependencies]
anyhow = { version = "1.0.78", features = ["backtrace"] }
formatting-nostd = { path = "../lib/formatting-nostd" }
futures = "0.3.29"
libc = "0.2"
libp2p = { version = "0.53.1", features = ["noise", "ping", "tcp", "tokio", "yamux"] }
linux-api = { path = "../lib/linux-api", features = ["std"] }
neli = "0.6.4"
nix = { version = "0.26.4", features = ["event", "feature", "fs", "poll", "process", "sched", "signal", "socket", "uio"] }
Expand All @@ -229,3 +235,4 @@ signal-hook = "0.3.17"
once_cell = "1.19.0"
vasi-sync = { path = "../lib/vasi-sync" }
static_assertions = "1.1.0"
tokio = { version = "1.34.0", features = ["full"] }
Copy link
Contributor

@stevenengler stevenengler Mar 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the "full" feature needed, or is there a smaller set of features that could be used instead?

1 change: 1 addition & 0 deletions src/test/libp2p/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(ping)
4 changes: 4 additions & 0 deletions src/test/libp2p/ping/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_shadow_tests(BASENAME libp2p-ping
SHADOW_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/ping.yaml"
POST_CMD "${CMAKE_CURRENT_SOURCE_DIR}/verify.sh"
CONFIGURATIONS extra)
23 changes: 23 additions & 0 deletions src/test/libp2p/ping/ping.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
general:
stop_time: 5 min
model_unblocked_syscall_latency: true
network:
graph:
type: 1_gbit_switch
hosts:
peer1:
network_node_id: 0
ip_addr: 1.1.1.1
processes:
- path: ../../../target/debug/test_libp2p_ping
args: "9001"
start_time: 1
expected_final_state: running
peer2:
network_node_id: 0
ip_addr: 1.1.1.2
processes:
- path: ../../../target/debug/test_libp2p_ping
args: ['9001', '/ip4/1.1.1.1/tcp/9001']
start_time: 5
expected_final_state: running
47 changes: 47 additions & 0 deletions src/test/libp2p/ping/test_ping.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use futures::prelude::*;
use libp2p::{multiaddr::Protocol, noise, ping, swarm::SwarmEvent, tcp, yamux, Multiaddr};
use std::{error::Error, time::Duration};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Usage: ./test_libp2p_ping [listen-port] [peer-multiaddr]

let mut swarm = libp2p::SwarmBuilder::with_new_identity()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For those of us who aren't familiar with libp2p, can you add a few more comments about what the program is doing, and what things like noise::Config::new and yamux::Config::default are used for?

.with_tokio()
.with_tcp(
tcp::Config::default(),
noise::Config::new,
yamux::Config::default,
)?
.with_behaviour(|_| ping::Behaviour::default())?
.with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX)))
.build();

// Set the listening port to the one given as the first command-line
// argument or zero if it is not given.
let mut addr: Multiaddr = "/ip4/0.0.0.0".parse()?;
let port = match std::env::args().nth(1) {
Some(port) => port.parse()?,
None => 0,
};
addr.push(Protocol::Tcp(port));

// Listen for the incoming peers.
swarm.listen_on(addr)?;

// Dial the peer identified by the multi-address given as the second
// command-line argument, if any.
if let Some(addr) = std::env::args().nth(2) {
let remote: Multiaddr = addr.parse()?;
swarm.dial(remote)?;
println!("Dialed {addr}")
}

loop {
match swarm.select_next_some().await {
SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {address:?}"),
SwarmEvent::Behaviour(event) => println!("{event:?}"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the verify.sh script searches for this println and depends on the formatting of it, can you add a match case for SwarmEvent::Behaviour(libp2p::ping::Event(event)) that formats the output in some explicit way. This way if the debug formatting changes for libp2p::ping::Event, it won't cause the regexes to fail.

_ => {}
}
}
}
31 changes: 31 additions & 0 deletions src/test/libp2p/ping/verify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

set -euo pipefail

echo "Test libp2p ping"

text="Dialed /ip4/1.1.1.1/tcp/9001"
cat ./hosts/peer2/*.stdout | grep "$text"
text="Listening on \"/ip4/127.0.0.1/tcp/9001\""
cat ./hosts/peer1/*.stdout | grep "$text"
cat ./hosts/peer2/*.stdout | grep "$text"
text="Listening on \"/ip4/1.1.1.1/tcp/9001\""
cat ./hosts/peer1/*.stdout | grep "$text"
text="Listening on \"/ip4/1.1.1.2/tcp/9001\""
cat ./hosts/peer2/*.stdout | grep "$text"

expected_ping_count=20
Copy link
Contributor

@stevenengler stevenengler Mar 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the expected count 20? Can you add a comment?

regex="Event { peer: PeerId(.*), connection: ConnectionId(.*), result: Ok(.*) }"

actual_ping_count=$(cat ./hosts/peer1/*.stdout | grep "$regex" | wc -l)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the shell lint check:

 actual_ping_count=$(cat ./hosts/peer1/*.stdout | grep "$regex" | wc -l)
                                                 ^-----------^ SC2126 (style): Consider using grep -c instead of grep|wc -l.

(And for the other grep | wc below.)

if [[ "${actual_ping_count}" != "${expected_ping_count}" ]]; then
printf "Peer1 doesn't have the expected number of pings"
exit 1
fi
actual_ping_count=$(cat ./hosts/peer2/*.stdout | grep "$regex" | wc -l)
if [[ "${actual_ping_count}" != "${expected_ping_count}" ]]; then
printf "Peer2 doesn't have the expected number of pings"
exit 1
fi

echo "Verification succeeded"
Loading