tests for irc

This commit is contained in:
Nikita Vilunov 2023-02-13 21:04:08 +01:00
parent 7f5fa955ec
commit b1b8ec800e
2 changed files with 83 additions and 0 deletions

View File

@ -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,

View File

@ -0,0 +1,80 @@
use tokio::{net::{TcpStream, tcp::{ReadHalf, WriteHalf}}, io::{BufReader, AsyncBufReadExt, AsyncWriteExt}};
use crate::prelude::*;
struct TestScope<'a> {
reader: BufReader<ReadHalf<'a>>,
writer: WriteHalf<'a>,
buffer: Vec<u8>,
}
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<TestScope> {
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(())
}