From b49f7e801bef78736e1a33361b883228dd55f134 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Thu, 28 Dec 2023 18:18:21 +0100 Subject: wip testing --- tests/imap_features.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/imap_features.rs b/tests/imap_features.rs index 3333b03..59892b2 100644 --- a/tests/imap_features.rs +++ b/tests/imap_features.rs @@ -1,5 +1,8 @@ use std::process::Command; +use std::net::{Shutdown, TcpStream}; use std::{thread, time}; +use anyhow::Result; +use std::io::{Write, Read}; static ONE_SEC: time::Duration = time::Duration::from_secs(1); @@ -11,7 +14,79 @@ fn main() { .spawn() .expect("daemon should be started"); - thread::sleep(ONE_SEC); + let mut max_retry = 10; + 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(ONE_SEC); + }; + let mut lmtp_socket = TcpStream::connect("[::1]:1025").expect("lmtp socket must be connected"); + + println!("-- ready to test imap features --"); + login(&mut imap_socket).expect("login test"); + select_inbox(&mut imap_socket).expect("select inbox"); + inject_email(&mut lmtp_socket).expect("inject email"); + + 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"); } + +fn login(imap: &mut TcpStream) -> Result<()> { + let mut buffer: [u8; 1500] = [0; 1500]; + let mut nbytes = 0; + loop { + nbytes += imap.read(&mut buffer)?; + if buffer[..nbytes].windows(2).any(|w| w == &b"\r\n"[..]) { + break + } + } + println!("read: {}", std::str::from_utf8(&buffer[..nbytes])?); + assert_eq!(&buffer[..4], &b"* OK"[..]); + assert_eq!(&buffer[nbytes-2..nbytes], &b"\r\n"[..]); + + imap.write(&b"10 login alice hunter2\r\n"[..])?; + + let mut nbytes = 0; + loop { + nbytes += imap.read(&mut buffer)?; + if &buffer[nbytes-2..nbytes] == &b"\r\n"[..] { + break + } + } + println!("read: {}", std::str::from_utf8(&buffer[..nbytes])?); + assert_eq!(&buffer[..5], &b"10 OK"[..]); + + Ok(()) +} + +fn select_inbox(imap: &mut TcpStream) -> Result<()> { + let mut buffer: [u8; 6000] = [0; 6000]; + + imap.write(&b"20 select inbox\r\n"[..])?; + + let mut nbytes = 0; + loop { + nbytes += imap.read(&mut buffer)?; + if buffer[..nbytes].windows(5).any(|w| w == &b"20 OK"[..]) && &buffer[nbytes-2..nbytes] == &b"\r\n"[..] { + break + } + } + println!("read: {}", std::str::from_utf8(&buffer[..nbytes])?); + + Ok(()) +} + +fn inject_email(lmtp: &mut TcpStream) -> Result<()> { + + Ok(()) +} -- cgit v1.2.3