Skip to content

Commit

Permalink
Make sure the dynamic link uses a different port from the static link
Browse files Browse the repository at this point in the history
Otherwise it exposes a bug in knet
  • Loading branch information
chrissie-c committed Jun 21, 2021
1 parent 5e3bf4e commit 779385d
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions libknet/bindings/rust/tests/src/bin/knet-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ fn setup_node(our_hostid: &knet::HostId, other_hostid: &knet::HostId, name: &str
}

// Called by the ACL tests to get a free port for a dynamic link
fn setup_dynamic_link(handle: knet::Handle, hostid: &knet::HostId, link: u8) -> Result<()>
fn setup_dynamic_link(handle: knet::Handle, hostid: &knet::HostId, link: u8,
lowest_port: u16) -> Result<()>
{
let mut src_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0);
for p in 1025..=65535 {
for p in lowest_port..=65535 {
src_addr.set_port(p);

if let Err(e) = knet::link_set_config(handle, hostid, link,
Expand All @@ -147,6 +148,7 @@ fn setup_dynamic_link(handle: knet::Handle, hostid: &knet::HostId, link: u8) ->
// In use - try the next port number
}
} else {
println!("Dynamic link - Using port {}", p);
return Ok(())
}
}
Expand All @@ -156,7 +158,7 @@ fn setup_dynamic_link(handle: knet::Handle, hostid: &knet::HostId, link: u8) ->
// This is the bit that configures two links on two handles that talk to each other
// while also making sure they don't clash with anything else on the system
fn setup_links(handle1: knet::Handle, hostid1: &knet::HostId,
handle2: knet::Handle, hostid2: &knet::HostId) -> Result<()>
handle2: knet::Handle, hostid2: &knet::HostId) -> Result<u16>
{
let mut src_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0);
let mut dst_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0);
Expand Down Expand Up @@ -198,7 +200,7 @@ fn setup_links(handle1: knet::Handle, hostid1: &knet::HostId,
}
}
println!("Bound to ports {} & {}",p, p+1);
return Ok(())
return Ok(p+2)
}
}
Err(Error::new(ErrorKind::Other, "No ports available"))
Expand Down Expand Up @@ -732,15 +734,15 @@ fn test_metadata_calls(handle: knet::Handle, host: &knet::HostId) -> Result<()>
}


fn test_acl(handle: knet::Handle, host: &knet::HostId) -> Result<()>
fn test_acl(handle: knet::Handle, host: &knet::HostId, low_port: u16) -> Result<()>
{
if let Err(e) = knet::handle_enable_access_lists(handle, true) {
println!("Error from handle_enable_access_lists: {:?}", e);
return Err(e);
}

// Dynamic link for testing ACL APIs (it never gets used)
if let Err(e) = setup_dynamic_link(handle, host, 1) {
if let Err(e) = setup_dynamic_link(handle, host, 1, low_port) {
println!("Error from link_set_config (dynamic): {}", e);
return Err(e);
}
Expand Down Expand Up @@ -779,6 +781,10 @@ fn test_acl(handle: knet::Handle, host: &knet::HostId) -> Result<()>
return Err(e);
}

if let Err(e) = knet::handle_enable_access_lists(handle, false) {
println!("Error from handle_enable_access_lists: {:?}", e);
return Err(e);
}
Ok(())
}

Expand Down Expand Up @@ -832,7 +838,7 @@ fn main() -> Result<()>
// Now test traffic
let handle1 = setup_node(&host1, &host2, "host2")?;
let handle2 = setup_node(&host2, &host1, "host1")?;
setup_links(handle1, &host1, handle2, &host2)?;
let low_port = setup_links(handle1, &host1, handle2, &host2)?;
configure_link(handle1, &host1, &host2)?;
configure_link(handle2, &host2, &host1)?;

Expand Down Expand Up @@ -868,7 +874,7 @@ fn main() -> Result<()>
send_messages(handle1, true)?;
send_messages(handle2, true)?;

test_acl(handle1, &host2)?;
test_acl(handle1, &host2, low_port)?;

// Wait for recv threads to finish
for handle in thread_handles {
Expand Down

0 comments on commit 779385d

Please sign in to comment.