aerogramme/tests/common/mod.rs

91 lines
2.9 KiB
Rust
Raw Normal View History

2024-01-04 10:23:26 +00:00
#![allow(dead_code)]
pub mod constants;
pub mod fragments;
2024-01-03 08:47:52 +00:00
use anyhow::{bail, Context, Result};
2024-01-03 09:28:10 +00:00
use std::io::Read;
2024-01-03 08:47:52 +00:00
use std::net::{Shutdown, TcpStream};
use std::process::Command;
2024-01-04 10:23:26 +00:00
use std::thread;
2024-01-03 08:47:52 +00:00
2024-01-04 10:23:26 +00:00
use constants::SMALL_DELAY;
2024-01-03 08:47:52 +00:00
2024-01-03 09:28:10 +00:00
pub fn aerogramme_provider_daemon_dev(
mut fx: impl FnMut(&mut TcpStream, &mut TcpStream) -> Result<()>,
) -> Result<()> {
2024-01-04 10:23:26 +00:00
// Check port is not used (= free) before starting the test
let mut max_retry = 20;
loop {
max_retry -= 1;
match (TcpStream::connect("[::1]:1143"), max_retry) {
(Ok(_), 0) => bail!("something is listening on [::1]:1143 and prevent the test from starting"),
(Ok(_), _) => println!("something is listening on [::1]:1143, maybe a previous daemon quitting, retrying soon..."),
(Err(_), _) => {
println!("test ready to start, [::1]:1143 is free!");
break
}
}
thread::sleep(SMALL_DELAY);
}
// Start daemon
2024-01-03 08:47:52 +00:00
let mut daemon = Command::new(env!("CARGO_BIN_EXE_aerogramme"))
.arg("--dev")
.arg("provider")
.arg("daemon")
.spawn()?;
2024-01-04 10:23:26 +00:00
// Check that our daemon is correctly listening on the free port
2024-01-03 08:47:52 +00:00
let mut max_retry = 20;
let mut imap_socket = loop {
max_retry -= 1;
match (TcpStream::connect("[::1]:1143"), max_retry) {
(Err(e), 0) => bail!("no more retry, last error is: {}", e),
(Err(e), _) => {
2024-01-04 10:23:26 +00:00
println!("unable to connect: {} ; will retry soon...", e);
2024-01-03 08:47:52 +00:00
}
(Ok(v), _) => break v,
}
thread::sleep(SMALL_DELAY);
};
2024-01-04 10:23:26 +00:00
// Assuming now it's safe to open a LMTP socket
2024-01-03 09:28:10 +00:00
let mut lmtp_socket =
TcpStream::connect("[::1]:1025").context("lmtp socket must be connected")?;
2024-01-03 08:47:52 +00:00
println!("-- ready to test imap features --");
let result = fx(&mut imap_socket, &mut lmtp_socket);
println!("-- test teardown --");
imap_socket
.shutdown(Shutdown::Both)
.context("closing imap socket at the end of the test")?;
lmtp_socket
.shutdown(Shutdown::Both)
.context("closing lmtp socket at the end of the test")?;
daemon.kill().context("daemon should be killed")?;
result.context("all tests passed")
}
2024-01-03 09:28:10 +00:00
pub fn read_lines<'a, F: Read>(
reader: &mut F,
buffer: &'a mut [u8],
stop_marker: Option<&[u8]>,
) -> Result<&'a [u8]> {
let mut nbytes = 0;
loop {
nbytes += reader.read(&mut buffer[nbytes..])?;
//println!("partial read: {}", std::str::from_utf8(&buffer[..nbytes])?);
let pre_condition = match stop_marker {
None => true,
Some(mark) => buffer[..nbytes].windows(mark.len()).any(|w| w == mark),
};
if pre_condition && &buffer[nbytes - 2..nbytes] == &b"\r\n"[..] {
break;
}
}
println!("read: {}", std::str::from_utf8(&buffer[..nbytes])?);
Ok(&buffer[..nbytes])
}