Common module in test created

This commit is contained in:
Quentin 2024-01-03 09:47:52 +01:00
parent 7ebc708aca
commit 9ce8e18fb8
Signed by: quentin
GPG key ID: E9602264D639FF68
3 changed files with 72 additions and 65 deletions

View file

@ -68,6 +68,6 @@ imap-types = { git = "https://github.com/duesee/imap-codec", branch = "v2" }
imap-codec = { git = "https://github.com/duesee/imap-codec", branch = "v2" } imap-codec = { git = "https://github.com/duesee/imap-codec", branch = "v2" }
[[test]] [[test]]
name = "imap_features" name = "rfc3501_imap4rev1_base"
path = "tests/imap_features.rs" path = "tests/rfc3501_imap4rev1_base.rs"
harness = false harness = false

43
tests/common.rs Normal file
View file

@ -0,0 +1,43 @@
use anyhow::{bail, Context, Result};
use std::net::{Shutdown, TcpStream};
use std::process::Command;
use std::{thread, time};
static SMALL_DELAY: time::Duration = time::Duration::from_millis(200);
pub fn aerogramme_provider_daemon_dev(mut fx: impl FnMut(&mut TcpStream, &mut TcpStream) -> Result<()>) -> Result<()> {
let mut daemon = Command::new(env!("CARGO_BIN_EXE_aerogramme"))
.arg("--dev")
.arg("provider")
.arg("daemon")
.spawn()?;
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), _) => {
println!("unable to connect: {} ; will retry in 1 sec", e);
}
(Ok(v), _) => break v,
}
thread::sleep(SMALL_DELAY);
};
let mut lmtp_socket = TcpStream::connect("[::1]:1025").context("lmtp socket must be connected")?;
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")
}

View file

@ -1,9 +1,10 @@
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::net::{Shutdown, TcpStream}; use std::net::TcpStream;
use std::process::Command;
use std::{thread, time}; use std::{thread, time};
mod common;
static SMALL_DELAY: time::Duration = time::Duration::from_millis(200); static SMALL_DELAY: time::Duration = time::Duration::from_millis(200);
static EMAIL1: &[u8] = b"Date: Sat, 8 Jul 2023 07:14:29 +0200\r static EMAIL1: &[u8] = b"Date: Sat, 8 Jul 2023 07:14:29 +0200\r
From: Bob Robert <bob@example.tld>\r From: Bob Robert <bob@example.tld>\r
@ -57,44 +58,7 @@ Hello world!\r
"; ";
fn main() { fn main() {
let mut daemon = Command::new(env!("CARGO_BIN_EXE_aerogramme")) common::aerogramme_provider_daemon_dev(|imap_socket, lmtp_socket| {
.arg("--dev")
.arg("provider")
.arg("daemon")
.spawn()
.expect("daemon should be started");
let mut max_retry = 20;
let mut imap_socket = loop {
max_retry -= 1;
match (TcpStream::connect("[::1]:1143"), max_retry) {
(Err(e), 0) => panic!("no more retry, last error is: {}", e),
(Err(e), _) => {
println!("unable to connect: {} ; will retry in 1 sec", e);
}
(Ok(v), _) => break v,
}
thread::sleep(SMALL_DELAY);
};
let mut lmtp_socket = TcpStream::connect("[::1]:1025").expect("lmtp socket must be connected");
println!("-- ready to test imap features --");
let result = generic_test(&mut imap_socket, &mut lmtp_socket);
println!("-- test teardown --");
imap_socket
.shutdown(Shutdown::Both)
.expect("closing imap socket at the end of the test");
lmtp_socket
.shutdown(Shutdown::Both)
.expect("closing lmtp socket at the end of the test");
daemon.kill().expect("daemon should be killed");
result.expect("all tests passed");
}
fn generic_test(imap_socket: &mut TcpStream, lmtp_socket: &mut TcpStream) -> Result<()> {
connect(imap_socket).context("server says hello")?; connect(imap_socket).context("server says hello")?;
capability(imap_socket).context("check server capabilities")?; capability(imap_socket).context("check server capabilities")?;
login(imap_socket).context("login test")?; login(imap_socket).context("login test")?;
@ -117,6 +81,7 @@ fn generic_test(imap_socket: &mut TcpStream, lmtp_socket: &mut TcpStream) -> Res
rename_mailbox(imap_socket).context("archive mailbox is renamed my-archives")?; rename_mailbox(imap_socket).context("archive mailbox is renamed my-archives")?;
delete_mailbox(imap_socket).context("my-archives mailbox is deleted")?; delete_mailbox(imap_socket).context("my-archives mailbox is deleted")?;
Ok(()) Ok(())
}).expect("test fully run");
} }
fn connect(imap: &mut TcpStream) -> Result<()> { fn connect(imap: &mut TcpStream) -> Result<()> {
@ -135,7 +100,6 @@ fn capability(imap: &mut TcpStream) -> Result<()> {
let read = read_lines(imap, &mut buffer, Some(&b"5 OK"[..]))?; let read = read_lines(imap, &mut buffer, Some(&b"5 OK"[..]))?;
let srv_msg = std::str::from_utf8(read)?; let srv_msg = std::str::from_utf8(read)?;
assert!(srv_msg.contains("IMAP4REV1")); assert!(srv_msg.contains("IMAP4REV1"));
assert!(srv_msg.contains("IDLE"));
Ok(()) Ok(())
} }