forked from lavina/lavina
1
0
Fork 0

add irc integration tests

This commit is contained in:
Nikita Vilunov 2023-02-13 21:58:44 +01:00
parent b1b8ec800e
commit 315b7e638b
3 changed files with 77 additions and 38 deletions

View File

@ -232,7 +232,10 @@ impl Player {
connection_id,
body,
} => {
tracing::info!("Handling incoming message");
tracing::info!(
"Handling incoming message, player_id={:?}",
player_id.clone()
);
for socket in &self.sockets {
log::info!("Send message to socket");
socket

View File

@ -142,9 +142,8 @@ async fn handle_registered_socket<'a>(
let mut buffer = vec![];
log::info!("Handling registered user: {user:?}");
let mut connection = players
.connect_to_player(PlayerId(user.nickname.clone()))
.await;
let player_id = PlayerId(user.nickname.clone());
let mut connection = players.connect_to_player(player_id.clone()).await;
ServerMessage {
tags: vec![],
@ -217,7 +216,7 @@ async fn handle_registered_socket<'a>(
match update.unwrap() {
Updates::RoomJoined { room_id } => {},
Updates::NewMessage { author_id, connection_id, room_id, body } => {
if connection.connection_id != connection_id {
if player_id != author_id || connection.connection_id != connection_id {
ServerMessage {
tags: vec![],
sender: Some(author_id.0.clone()),

View File

@ -1,4 +1,6 @@
use tokio::{net::{TcpStream, tcp::{ReadHalf, WriteHalf}}, io::{BufReader, AsyncBufReadExt, AsyncWriteExt}};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::net::tcp::{ReadHalf, WriteHalf};
use tokio::net::TcpStream;
use crate::prelude::*;
@ -8,15 +10,15 @@ struct TestScope<'a> {
buffer: Vec<u8>,
}
impl<'a> TestScope<'a> {
async fn send(&mut self, str: &str) -> Result<()> {
self.writer.write_all(str.as_bytes()).await?;
async fn send(&mut self, str: &(impl AsRef<str> + ?Sized)) -> Result<()> {
self.writer.write_all(str.as_ref().as_bytes()).await?;
self.writer.flush().await?;
Ok(())
}
async fn expect(&mut self, str: &str) -> Result<()> {
async fn expect(&mut self, str: &(impl AsRef<str> + ?Sized)) -> Result<()> {
let len = self.reader.read_until(b'\n', &mut self.buffer).await?;
assert_eq!(std::str::from_utf8(&self.buffer[0..len])?, str);
assert_eq!(std::str::from_utf8(&self.buffer[0..len])?, str.as_ref());
self.buffer.clear();
Ok(())
}
@ -26,27 +28,43 @@ 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 })
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?;
macro_rules! send {
($scope: expr, $($arg:tt),*) => {{
$scope.send(&format!($($arg,)*)).await?;
}};
}
macro_rules! expect {
($scope: expr, $($arg:tt),*) => {{
$scope.expect(&format!($($arg,)*)).await?;
}};
}
async fn registration(scope: &mut TestScope<'_>, nickname: &str) -> Result<()> {
expect!(scope, ":irc.localhost NOTICE * :Welcome to my server!\n");
send!(scope, "NICK {nickname}\n");
send!(scope, "USER UserName 0 * :Real Name\n");
expect!(scope, ":irc.localhost 001 {nickname} :Welcome to Kek Server\n");
expect!(scope, ":irc.localhost 002 {nickname} :Welcome to Kek Server\n");
expect!(scope, ":irc.localhost 003 {nickname} :Welcome to Kek Server\n");
expect!(scope, ":irc.localhost 004 {nickname} irc.localhost kek-0.1.alpha.3 DGMQRSZagiloswz CFILPQbcefgijklmnopqrstvz bkloveqjfI\n");
expect!(scope, ":irc.localhost 005 {nickname} CHANTYPES=# :are supported by this server\n");
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?;
async fn join(scope: &mut TestScope<'_>, nickname: &str) -> Result<()> {
send!(scope, "JOIN #channol\n");
expect!(scope, ":{nickname} JOIN #channol\n");
expect!(scope, ":irc.localhost 332 {nickname} #channol :chan topic lol\n");
expect!(scope, ":irc.localhost 353 {nickname} = #channol :{nickname}\n");
expect!(scope, ":irc.localhost 366 {nickname} #channol :End of /NAMES list\n");
Ok(())
}
@ -55,26 +73,45 @@ 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?;
registration(&mut scope, "NickName").await?;
join(&mut scope, "NickName").await?;
Ok(())
}
#[tokio::test]
async fn test_two_persons() -> Result<()> {
async fn test_two_connections_one_player() -> 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?;
registration(&mut scope1, "NickName").await?;
registration(&mut scope2, "NickName").await?;
join(&mut scope1, "NickName").await?;
join(&mut scope2, "NickName").await?;
send!(scope1, "PRIVMSG #channol :Chmoki vsem v etam chati!\n");
expect!(scope2, ":NickName PRIVMSG #channol :Chmoki vsem v etam chati!\n");
send!(scope2, "PRIVMSG #channol :I tebe privetiki\n");
expect!(scope1, ":NickName PRIVMSG #channol :I tebe privetiki\n");
Ok(())
}
#[tokio::test]
async fn test_two_players() -> 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, "NickName1").await?;
registration(&mut scope2, "NickName2").await?;
join(&mut scope1, "NickName1").await?;
join(&mut scope2, "NickName2").await?;
send!(scope1, "PRIVMSG #channol :Chmoki vsem v etam chati!\n");
expect!(scope2, ":NickName1 PRIVMSG #channol :Chmoki vsem v etam chati!\n");
send!(scope2, "PRIVMSG #channol :I tebe privetiki\n");
expect!(scope1, ":NickName2 PRIVMSG #channol :I tebe privetiki\n");
Ok(())
}