2023-04-01 19:02:13 +02:00
|
|
|
use std::{
|
|
|
|
fs::{self, File},
|
|
|
|
io::{BufWriter, Write},
|
|
|
|
path::PathBuf,
|
2023-05-01 00:39:58 +02:00
|
|
|
str::FromStr,
|
2023-04-01 19:02:13 +02:00
|
|
|
};
|
|
|
|
|
2023-05-27 15:01:39 +02:00
|
|
|
use contego::{
|
|
|
|
client::Client,
|
|
|
|
server::Server,
|
|
|
|
util::{metadata, Ip},
|
|
|
|
};
|
|
|
|
use env_logger::Env;
|
|
|
|
use log::debug;
|
|
|
|
use ntest::timeout;
|
|
|
|
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
|
|
|
use tokio::{fs::read_to_string, sync::mpsc};
|
|
|
|
|
|
|
|
#[tokio::test]
|
2023-04-01 19:02:13 +02:00
|
|
|
#[timeout(2000)]
|
2023-05-27 15:01:39 +02:00
|
|
|
/// Ensures backend communications integrity & the ability to handle individual requests.
|
|
|
|
async fn sockets_integration() {
|
|
|
|
env_logger::Builder::from_env(Env::default().default_filter_or("debug"))
|
|
|
|
.is_test(true)
|
|
|
|
.try_init()
|
|
|
|
.unwrap();
|
|
|
|
//env_logger::builder().is_test(true).try_init().unwrap();
|
2023-04-01 19:02:13 +02:00
|
|
|
|
2023-05-27 15:01:39 +02:00
|
|
|
debug!("Initializing and starting the test");
|
2023-04-01 19:02:13 +02:00
|
|
|
|
2023-05-27 15:01:39 +02:00
|
|
|
let (testdata, paths) = testdata();
|
|
|
|
let (metadata, index) = metadata(&paths).await.unwrap();
|
2023-04-01 19:02:13 +02:00
|
|
|
|
2023-05-27 15:01:39 +02:00
|
|
|
let addr = Ip::Local.fetch(8080).unwrap();
|
|
|
|
let outdir = PathBuf::from("./tests/output/");
|
|
|
|
let key = String::from("testkey");
|
|
|
|
let c_key = key.clone();
|
2023-04-01 19:02:13 +02:00
|
|
|
|
2023-05-27 15:01:39 +02:00
|
|
|
let (tx, rx) = mpsc::channel::<()>(1);
|
|
|
|
|
|
|
|
let server_handle = tokio::spawn(async move {
|
|
|
|
debug!("Initializing the asynchronous server task");
|
|
|
|
let server = Server::new(addr, key, 8192, metadata, index);
|
|
|
|
debug!("Starting to listen to incoming connections");
|
|
|
|
server.start(rx).await.unwrap();
|
2023-04-01 19:02:13 +02:00
|
|
|
});
|
|
|
|
|
2023-05-27 15:01:39 +02:00
|
|
|
let client_handle = tokio::spawn(async move {
|
|
|
|
debug!("Initializing the asynchronous client task");
|
|
|
|
let client = Client::new(addr, c_key, outdir);
|
|
|
|
debug!("Connecting to the server");
|
|
|
|
client.connection().await.unwrap();
|
2023-04-01 19:02:13 +02:00
|
|
|
});
|
|
|
|
|
2023-05-27 15:01:39 +02:00
|
|
|
client_handle.await.unwrap();
|
|
|
|
tx.send(()).await.unwrap();
|
|
|
|
server_handle.await.unwrap();
|
2023-05-01 00:39:58 +02:00
|
|
|
|
2023-05-27 15:01:39 +02:00
|
|
|
debug!("Checking for file integrity");
|
2023-05-01 00:39:58 +02:00
|
|
|
|
2023-05-27 15:01:39 +02:00
|
|
|
for file in testdata {
|
|
|
|
let path = String::from("./tests/output/") + file.0;
|
|
|
|
let recv_content = read_to_string(path).await.unwrap();
|
2023-04-01 19:02:13 +02:00
|
|
|
|
2023-05-27 15:01:39 +02:00
|
|
|
assert_eq!(
|
|
|
|
recv_content, file.1,
|
|
|
|
"Output '{}' doesn't match the input '{}'",
|
|
|
|
recv_content, file.1
|
|
|
|
);
|
2023-04-01 19:02:13 +02:00
|
|
|
|
|
|
|
fs::remove_file(String::from("./tests/output/") + file.0).unwrap();
|
|
|
|
fs::remove_file(String::from("./tests/data/") + file.0).unwrap();
|
2023-05-27 15:01:39 +02:00
|
|
|
|
|
|
|
debug!("File '{}' checked and removed successfully", file.0);
|
2023-04-01 19:02:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-27 15:01:39 +02:00
|
|
|
fn testdata() -> (Vec<(&'static str, String)>, Vec<PathBuf>) {
|
2023-05-01 00:39:58 +02:00
|
|
|
let mut paths = Vec::new();
|
|
|
|
let testdata = vec![
|
|
|
|
("1.txt", generate_data()),
|
|
|
|
("2.txt", generate_data()),
|
|
|
|
("3.txt", generate_data()),
|
|
|
|
];
|
|
|
|
|
|
|
|
for file in &testdata {
|
|
|
|
let filepath = PathBuf::from_str("./tests/data/").unwrap().join(file.0);
|
|
|
|
let mut writer = BufWriter::new(File::create(filepath.clone()).unwrap());
|
|
|
|
paths.push(filepath);
|
|
|
|
writer.write_all(file.1.as_bytes()).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
(testdata, paths)
|
|
|
|
}
|
|
|
|
|
2023-04-01 19:02:13 +02:00
|
|
|
fn generate_data() -> String {
|
|
|
|
thread_rng()
|
|
|
|
.sample_iter(&Alphanumeric)
|
|
|
|
.take(30)
|
|
|
|
.map(char::from)
|
|
|
|
.collect::<String>()
|
|
|
|
}
|