Skip to content

Commit

Permalink
Improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
manforowicz committed Jul 10, 2024
1 parent 3ac29a6 commit 6b6b118
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 34 deletions.
2 changes: 1 addition & 1 deletion gday/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.5.8", features = ["derive"] }
clap = { version = "4.5.9", features = ["derive"] }
env_logger = "0.11.3"
gday_encryption = { version = "^0.2.0", path = "../gday_encryption" }
gday_file_transfer = { version = "^0.2.0", path = "../gday_file_transfer" }
Expand Down
1 change: 0 additions & 1 deletion gday/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#![warn(clippy::all)]

mod dialog;
mod tests;
mod transfer;

use crate::dialog::ask_receive;
Expand Down
1 change: 0 additions & 1 deletion gday/src/tests.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,75 @@ use gday_encryption::EncryptedStream;
use rand::{RngCore, SeedableRng};
use std::{
collections::VecDeque,
io::{Read, Write},
net::Ipv6Addr,
net::SocketAddr,
io::{BufRead, Read, Write},
};

/// Try to spot edge-cases that occur when sending
/// several large messages.
/// Transfer `bytes` over [`EncryptedStream`],
/// flushing every `chunk_size` bytes.
#[test]
fn test_large_messages() {
fn test_transfers() {
// A pseudorandom encryption key
let mut rng = rand::rngs::StdRng::seed_from_u64(5);
let mut shared_key = [0u8; 32];
rng.fill_bytes(&mut shared_key);

// A pseudorandom test vector
let mut rng = rand::rngs::StdRng::seed_from_u64(10);
let mut bytes = vec![0_u8; 1_000_000];
rng.fill_bytes(&mut bytes);

test_transfers(bytes, 200_000);
// How many bytes will be sent at a time
let chunk_size = 200_000;

// Listens on the loopback address
let listener = std::net::TcpListener::bind("[::]:0").unwrap();
let pipe_addr = listener.local_addr().unwrap();

// A thread that will send data to the loopback address
let bytes_clone = bytes.clone();
std::thread::spawn(move || {
let mut peer_a = std::net::TcpStream::connect(pipe_addr).unwrap();

let mut stream_a = EncryptedStream::encrypt_connection(&mut peer_a, &shared_key).unwrap();

for chunk in bytes_clone.chunks(chunk_size) {
stream_a.write_all(chunk).unwrap();
stream_a.flush().unwrap();
}
});

// Stream that will receive the test data sent to the loopback address.
let mut peer_b = listener.accept().unwrap().0;
let mut stream_b = EncryptedStream::encrypt_connection(&mut peer_b, &shared_key).unwrap();

// Receive and verify the encrypted test data.
for chunk in bytes.chunks(chunk_size) {
let mut received = vec![0; chunk.len()];
stream_b.read_exact(&mut received).unwrap();
assert_eq!(*chunk, received);
}
}

/// Transfer `bytes` over [`EncryptedStream`],
/// flushing every `chunk_size` bytes.
fn test_transfers(bytes: Vec<u8>, chunk_size: usize) {
/// Test bufread
#[test]
fn test_bufread() {
// A pseudorandom encryption key
let mut rng = rand::rngs::StdRng::seed_from_u64(10);
let mut rng = rand::rngs::StdRng::seed_from_u64(20);
let mut shared_key = [0u8; 32];
rng.fill_bytes(&mut shared_key);

// The loopback address that peer_a will connect to.
let pipe_addr = SocketAddr::from((Ipv6Addr::LOCALHOST, 2000));
// A pseudorandom test vector
let mut rng = rand::rngs::StdRng::seed_from_u64(25);
let mut bytes = vec![0_u8; 1_000_000];
rng.fill_bytes(&mut bytes);
bytes.push(0);

// How many bytes will be sent at a time
let chunk_size = 200_000;

// Listens on the loopback address
let listener = std::net::TcpListener::bind(pipe_addr).unwrap();
let listener = std::net::TcpListener::bind("[::]:0").unwrap();
let pipe_addr = listener.local_addr().unwrap();

// A thread that will send data to the loopback address
let bytes_clone = bytes.clone();
Expand All @@ -53,11 +92,13 @@ fn test_transfers(bytes: Vec<u8>, chunk_size: usize) {
let mut stream_b = EncryptedStream::encrypt_connection(&mut peer_b, &shared_key).unwrap();

// Receive and verify the encrypted test data.
for chunk in bytes.chunks(chunk_size) {
let mut received = vec![0; chunk.len()];
stream_b.read_exact(&mut received).unwrap();
assert_eq!(*chunk, received);
let mut received = Vec::new();
let zeros = bytes.iter().filter(|num| **num == 0).count();
for _ in 0..zeros {
let bytes_read = stream_b.read_until(0, &mut received).unwrap();
assert_ne!(bytes_read, 0);
}
assert_eq!(received, bytes);
}

/// Confirm there are no infinite loops on EOF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use gday_file_transfer::{
};
use std::fs::{self, create_dir_all};
use std::io::Write;
use std::net::SocketAddr;
use std::{fs::File, path::PathBuf};

/// Returns a temporary directory
Expand Down Expand Up @@ -207,11 +206,9 @@ fn test_get_file_metas_2() {
/// Test the file transfer.
#[test]
fn file_transfer() {
// The loopback address that peer_a will connect to.
let pipe_addr: SocketAddr = "[::1]:2000".parse().unwrap();

// Listens on the loopback address
let listener = std::net::TcpListener::bind(pipe_addr).unwrap();
let listener = std::net::TcpListener::bind("[::1]:0").unwrap();
let pipe_addr = listener.local_addr().unwrap();

// dir_a contains test files, some of which
// will be sent
Expand Down Expand Up @@ -239,14 +236,14 @@ fn file_transfer() {
send_files(&file_metas, &response, &mut stream_a, |_| {}).unwrap();
});

// directory to receive the files in
// dir_b will receive the files in
let dir_b = tempfile::tempdir().unwrap();

// create pre-existing file1 and file1 (1)
let mut f = File::create_new(dir_b.path().join("file1")).unwrap();
write!(f, "This is a pre-existing file1").unwrap();
let mut f = File::create_new(dir_b.path().join("file1 (1)")).unwrap();
write!(f, "This is a pre-existing file1 (1)").unwrap();
write!(f, "This is file1").unwrap();

// create pre-existing file2.txt
let mut f = File::create_new(dir_b.path().join("file2.txt")).unwrap();
Expand All @@ -258,7 +255,7 @@ fn file_transfer() {
let mut f = File::create_new(dir_b.path().join("subdir1/file2.txt.part29")).unwrap();
write!(f, "This is dir/subdi").unwrap();

// Stream that will receive the files to the loopback address.s
// Stream that will receive the files from the loopback address.
let mut stream_b = listener.accept().unwrap().0;

// read the file offer message
Expand All @@ -267,9 +264,9 @@ fn file_transfer() {
let response_msg =
FileResponseMsg::accept_only_new_and_interrupted(&file_offer, dir_b.path()).unwrap();

assert_eq!(response_msg.get_num_not_rejected(), 4);
assert_eq!(response_msg.get_num_not_rejected(), 3);
assert_eq!(response_msg.get_num_partially_accepted(), 1);
assert_eq!(response_msg.get_num_fully_accepted(), 3);
assert_eq!(response_msg.get_num_fully_accepted(), 2);

write_to(&response_msg, &mut stream_b).unwrap();

Expand Down Expand Up @@ -300,7 +297,7 @@ fn file_transfer() {
);
assert_eq!(
fs::read(dir_b.path().join("file1 (1)")).unwrap(),
b"This is a pre-existing file1 (1)"
b"This is file1"
);
assert_eq!(
fs::read(dir_b.path().join("file2.txt")).unwrap(),
Expand All @@ -310,6 +307,7 @@ fn file_transfer() {
// confirm that files rejected or not offered
// weren't downloaded
assert!(fs::read(dir_b.path().join("dir/file1")).is_err());
assert!(fs::read(dir_b.path().join("dir/file1 (2)")).is_err());
assert!(fs::read(dir_b.path().join("dir/file2.txt")).is_err());
assert!(fs::read(dir_b.path().join("dir/subdir2/file1")).is_err());
assert!(fs::read(dir_b.path().join("dir/subdir2/file2.txt")).is_err());
Expand Down
2 changes: 1 addition & 1 deletion gday_hole_punch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ blake3 = "1.5.1"
gday_contact_exchange_protocol = { version = "^0.2.0", path = "../gday_contact_exchange_protocol" }
log = "0.4.22"
rand = "0.8.5"
rustls = { version = "0.23.10", features = ["ring", "log", "logging", "std", "tls12"], default-features = false }
rustls = { version = "0.23.11", features = ["ring", "log", "logging", "std", "tls12"], default-features = false }
serde = "1.0.204"
socket2 = { version = "0.5.7", features = ["all"] }
spake2 = { version = "0.4.0", features = ["std"] }
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion gday_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.5.8", features = ["derive"] }
clap = { version = "4.5.9", features = ["derive"] }
socket2 = { version = "0.5.7", features = ["all"] }
tokio = { version = "1.38.0", features = [
"rt-multi-thread",
Expand Down
File renamed without changes.

0 comments on commit 6b6b118

Please sign in to comment.