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

2 tests demonstrating the overflow bug #60

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
98 changes: 98 additions & 0 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,101 @@ impl<P> Drop for UtpStream<P> {
let _ = self.shutdown();
}
}

#[cfg(test)]
mod test {
use crate::conn::ConnectionConfig;
use crate::socket::UtpSocket;
use std::net::SocketAddr;

#[tokio::test]
async fn test_transfer_65535_packets_in_theory_should_pass() {
// set-up test
_ = tracing_subscriber::fmt::try_init();
let sender_addr = SocketAddr::from(([127, 0, 0, 1], 3700));
let receiver_addr = SocketAddr::from(([127, 0, 0, 1], 3701));

let sender = UtpSocket::bind(sender_addr).await.unwrap();
let receiver = UtpSocket::bind(receiver_addr).await.unwrap();

let config = ConnectionConfig::default();

// accept connection
let rx = async move {
let mut rx_stream = receiver.accept(config).await.unwrap();
// read data from the remote peer until the peer indicates there is no data left to
// write.
let mut data = vec![];
rx_stream
.read_to_eof(&mut data)
.await
.expect("Should read 1 megabyte")
};

let tx = async move {
// write 100k bytes data to the remote peer over the stream.
let data = vec![0xef; 1];
let mut tx_stream = sender.connect(receiver_addr, config).await.unwrap();

let mut letter = 0;
for _ in 1..65535 {
letter += tx_stream
.write(data.as_slice())
.await
.expect("Should send 1 megabyte");
}

letter
};

let (tx_res, rx_res) = tokio::join!(tx, rx);

assert_eq!(tx_res, rx_res);
}

#[tokio::test]
async fn test_transfer_65536_packets_in_theory_should_fail() {
// set-up test
_ = tracing_subscriber::fmt::try_init();
let sender_addr = SocketAddr::from(([127, 0, 0, 1], 3800));
let receiver_addr = SocketAddr::from(([127, 0, 0, 1], 3801));

let sender = UtpSocket::bind(sender_addr).await.unwrap();
let receiver = UtpSocket::bind(receiver_addr).await.unwrap();

let config = ConnectionConfig::default();

// accept connection
let rx = async move {
let mut rx_stream = receiver.accept(config).await.unwrap();
// read data from the remote peer until the peer indicates there is no data left to
// write.
let mut data = vec![];
rx_stream
.read_to_eof(&mut data)
.await
.expect("Should pass to send 65535 packets")
};

let tx = async move {
// write 100k bytes data to the remote peer over the stream.
let data = vec![0xef; 1];
let mut tx_stream = sender.connect(receiver_addr, config).await.unwrap();

let mut letter = 0;
for _ in 1..65536 {
letter += tx_stream
.write(data.as_slice())
.await
.expect("Should fail to send 65536 packets");
}

letter
};

let (tx_res, rx_res) = tokio::join!(tx, rx);

assert_eq!(tx_res, rx_res);
}
}