From b1b8ec800e08f1a4566ba402cf7c8bb2f9908f47 Mon Sep 17 00:00:00 2001 From: Nikita Vilunov Date: Mon, 13 Feb 2023 21:04:08 +0100 Subject: [PATCH] tests for irc --- src/projections/{irc.rs => irc/mod.rs} | 3 + src/projections/irc/test.rs | 80 ++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) rename src/projections/{irc.rs => irc/mod.rs} (99%) create mode 100644 src/projections/irc/test.rs diff --git a/src/projections/irc.rs b/src/projections/irc/mod.rs similarity index 99% rename from src/projections/irc.rs rename to src/projections/irc/mod.rs index 78688b2..0baa453 100644 --- a/src/projections/irc.rs +++ b/src/projections/irc/mod.rs @@ -17,6 +17,9 @@ use crate::protos::irc::server::{ServerMessage, ServerMessageBody}; use crate::protos::irc::{Chan, Recipient}; use crate::util::Terminator; +#[cfg(test)] +mod test; + #[derive(Deserialize, Debug, Clone)] pub struct ServerConfig { pub listen_on: SocketAddr, diff --git a/src/projections/irc/test.rs b/src/projections/irc/test.rs new file mode 100644 index 0000000..f41c46e --- /dev/null +++ b/src/projections/irc/test.rs @@ -0,0 +1,80 @@ +use tokio::{net::{TcpStream, tcp::{ReadHalf, WriteHalf}}, io::{BufReader, AsyncBufReadExt, AsyncWriteExt}}; + +use crate::prelude::*; + +struct TestScope<'a> { + reader: BufReader>, + writer: WriteHalf<'a>, + buffer: Vec, +} +impl<'a> TestScope<'a> { + async fn send(&mut self, str: &str) -> Result<()> { + self.writer.write_all(str.as_bytes()).await?; + self.writer.flush().await?; + Ok(()) + } + + async fn expect(&mut self, str: &str) -> Result<()> { + let len = self.reader.read_until(b'\n', &mut self.buffer).await?; + assert_eq!(std::str::from_utf8(&self.buffer[0..len])?, str); + self.buffer.clear(); + Ok(()) + } +} + +async fn init_client(stream: &mut TcpStream) -> Result { + let (reader, writer) = stream.split(); + let reader = BufReader::new(reader); + let buffer = vec![]; + Ok(TestScope { reader, writer, buffer }) +} + +async fn registration(scope: &mut TestScope<'_>) -> Result<()> { + scope.expect(":irc.localhost NOTICE * :Welcome to my server!\n").await?; + scope.send("NICK NickName\n").await?; + scope.send("USER UserName 0 * :Real Name\n").await?; + scope.expect(":irc.localhost 001 NickName :Welcome to Kek Server\n").await?; + scope.expect(":irc.localhost 002 NickName :Welcome to Kek Server\n").await?; + scope.expect(":irc.localhost 003 NickName :Welcome to Kek Server\n").await?; + scope.expect(":irc.localhost 004 NickName irc.localhost kek-0.1.alpha.3 DGMQRSZagiloswz CFILPQbcefgijklmnopqrstvz bkloveqjfI\n").await?; + scope.expect(":irc.localhost 005 NickName CHANTYPES=# :are supported by this server\n").await?; + Ok(()) +} + +async fn join(scope: &mut TestScope<'_>) -> Result<()> { + scope.send("JOIN #channol\n").await?; + scope.expect(":NickName JOIN #channol\n").await?; + scope.expect(":irc.localhost 332 NickName #channol :chan topic lol\n").await?; + scope.expect(":irc.localhost 353 NickName = #channol :NickName\n").await?; + scope.expect(":irc.localhost 366 NickName #channol :End of /NAMES list\n").await?; + Ok(()) +} + +#[tokio::test] +async fn test_registration() -> Result<()> { + let mut stream = TcpStream::connect("127.0.0.1:6667").await?; + let mut scope = init_client(&mut stream).await?; + + registration(&mut scope).await?; + join(&mut scope).await?; + Ok(()) +} + +#[tokio::test] +async fn test_two_persons() -> Result<()> { + let mut stream1 = TcpStream::connect("127.0.0.1:6667").await?; + let mut scope1 = init_client(&mut stream1).await?; + let mut stream2 = TcpStream::connect("127.0.0.1:6667").await?; + let mut scope2 = init_client(&mut stream2).await?; + + registration(&mut scope1).await?; + registration(&mut scope2).await?; + join(&mut scope1).await?; + join(&mut scope2).await?; + scope1.send("PRIVMSG #channol :Chmoki vsem v etam chati!\n").await?; + scope2.expect(":NickName PRIVMSG #channol :Chmoki vsem v etam chati!\n").await?; + scope2.send("PRIVMSG #channol :I tebe privetiki\n").await?; + scope1.expect(":NickName PRIVMSG #channol :I tebe privetiki\n").await?; + + Ok(()) +} \ No newline at end of file