/* * SPDX-FileCopyrightText: 2020 Stalwart Labs LLC * * SPDX-License-Identifier: Apache-2.0 OR MIT */ use std::io::{self, BufRead, BufReader, Write}; use std::net::{TcpListener, TcpStream}; use std::sync::{Arc, Mutex}; use std::thread; use std::time::Duration; const IO_TIMEOUT: Duration = Duration::from_secs(5); pub type Script = Box io::Result<()> + Send + 'static>; pub struct MockConn { pub reader: BufReader, pub writer: TcpStream, } impl MockConn { pub fn write_line(&mut self, s: &str) -> io::Result<()> { self.writer.write_all(s.as_bytes())?; self.writer.write_all(b"\r\n")?; self.writer.flush() } pub fn write_raw(&mut self, bytes: &[u8]) -> io::Result<()> { self.writer.write_all(bytes)?; self.writer.flush() } pub fn read_line(&mut self) -> io::Result { let mut line = String::new(); if self.reader.read_line(&mut line)? == 0 { return Err(io::Error::new( io::ErrorKind::UnexpectedEof, "client closed", )); } Ok(line.trim_end_matches(['\r', '\n']).to_owned()) } } pub struct LineServer { pub addr: String, pub port: u16, _thread: thread::JoinHandle<()>, } impl LineServer { pub fn start(scripts: Vec