forked from lavina/lavina
Compare commits
No commits in common. "dccb0b0f307d83e18e1082dc63db80cda31148f1" and "013a20cbeac467cfb08e7ad8b1ba8ac6b7249eaa" have entirely different histories.
dccb0b0f30
...
013a20cbea
|
@ -1,14 +1,14 @@
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::Result;
|
||||||
use prometheus::Registry as MetricsRegistry;
|
use prometheus::Registry as MetricsRegistry;
|
||||||
use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader};
|
use tokio::io::{AsyncWriteExt, BufReader};
|
||||||
use tokio::net::tcp::{ReadHalf, WriteHalf};
|
use tokio::net::tcp::{ReadHalf, WriteHalf};
|
||||||
use tokio::net::TcpStream;
|
use tokio::net::TcpStream;
|
||||||
|
|
||||||
use lavina_core::repo::{Storage, StorageConfig};
|
use lavina_core::repo::{Storage, StorageConfig};
|
||||||
use lavina_core::{player::PlayerRegistry, room::RoomRegistry};
|
use lavina_core::{player::PlayerRegistry, room::RoomRegistry};
|
||||||
use projection_irc::{launch, read_irc_message, RunningServer, ServerConfig};
|
use projection_irc::{launch, read_irc_message, ServerConfig};
|
||||||
|
|
||||||
struct TestScope<'a> {
|
struct TestScope<'a> {
|
||||||
reader: BufReader<ReadHalf<'a>>,
|
reader: BufReader<ReadHalf<'a>>,
|
||||||
|
@ -44,66 +44,29 @@ impl<'a> TestScope<'a> {
|
||||||
self.buffer.clear();
|
self.buffer.clear();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn expect_eof(&mut self) -> Result<()> {
|
|
||||||
let mut buf = [0; 1];
|
|
||||||
let len = tokio::time::timeout(self.timeout, self.reader.read(&mut buf)).await??;
|
|
||||||
if len != 0 {
|
|
||||||
return Err(anyhow!("not a eof"));
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn expect_nothing(&mut self) -> Result<()> {
|
|
||||||
let mut buf = [0; 1];
|
|
||||||
match tokio::time::timeout(self.timeout, self.reader.read(&mut buf)).await {
|
|
||||||
Ok(res) => Err(anyhow!("received something: {:?}", res)),
|
|
||||||
Err(_) => Ok(()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct TestServer {
|
|
||||||
metrics: MetricsRegistry,
|
|
||||||
storage: Storage,
|
|
||||||
rooms: RoomRegistry,
|
|
||||||
players: PlayerRegistry,
|
|
||||||
server: RunningServer,
|
|
||||||
}
|
|
||||||
impl TestServer {
|
|
||||||
async fn start() -> Result<TestServer> {
|
|
||||||
let config = ServerConfig {
|
|
||||||
listen_on: "127.0.0.1:0".parse().unwrap(),
|
|
||||||
server_name: "testserver".into(),
|
|
||||||
};
|
|
||||||
let mut metrics = MetricsRegistry::new();
|
|
||||||
let mut storage = Storage::open(StorageConfig {
|
|
||||||
db_path: ":memory:".into(),
|
|
||||||
})
|
|
||||||
.await?;
|
|
||||||
let rooms = RoomRegistry::new(&mut metrics, storage.clone()).unwrap();
|
|
||||||
let players = PlayerRegistry::empty(rooms.clone(), &mut metrics).unwrap();
|
|
||||||
let server = launch(config, players.clone(), rooms.clone(), metrics.clone(), storage.clone()).await.unwrap();
|
|
||||||
Ok(TestServer {
|
|
||||||
metrics,
|
|
||||||
storage,
|
|
||||||
rooms,
|
|
||||||
players,
|
|
||||||
server,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn scenario_basic() -> Result<()> {
|
async fn scenario_basic() -> Result<()> {
|
||||||
let mut server = TestServer::start().await?;
|
let config = ServerConfig {
|
||||||
|
listen_on: "127.0.0.1:0".parse().unwrap(),
|
||||||
|
server_name: "testserver".into(),
|
||||||
|
};
|
||||||
|
let mut metrics = MetricsRegistry::new();
|
||||||
|
let mut storage = Storage::open(StorageConfig {
|
||||||
|
db_path: ":memory:".into(),
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
let rooms = RoomRegistry::new(&mut metrics, storage.clone()).unwrap();
|
||||||
|
let players = PlayerRegistry::empty(rooms.clone(), &mut metrics).unwrap();
|
||||||
|
let server = launch(config, players, rooms, metrics, storage.clone()).await.unwrap();
|
||||||
|
|
||||||
// test scenario
|
// test scenario
|
||||||
|
|
||||||
server.storage.create_user("tester").await?;
|
storage.create_user("tester").await?;
|
||||||
server.storage.set_password("tester", "password").await?;
|
storage.set_password("tester", "password").await?;
|
||||||
|
|
||||||
let mut stream = TcpStream::connect(server.server.addr).await?;
|
let mut stream = TcpStream::connect(server.addr).await?;
|
||||||
let mut s = TestScope::new(&mut stream);
|
let mut s = TestScope::new(&mut stream);
|
||||||
|
|
||||||
s.send("PASS password").await?;
|
s.send("PASS password").await?;
|
||||||
|
@ -115,60 +78,11 @@ async fn scenario_basic() -> Result<()> {
|
||||||
s.expect(":testserver 003 tester :Welcome to Kek Server").await?;
|
s.expect(":testserver 003 tester :Welcome to Kek Server").await?;
|
||||||
s.expect(":testserver 004 tester testserver kek-0.1.alpha.3 r CFILPQbcefgijklmnopqrstvz").await?;
|
s.expect(":testserver 004 tester testserver kek-0.1.alpha.3 r CFILPQbcefgijklmnopqrstvz").await?;
|
||||||
s.expect(":testserver 005 tester CHANTYPES=# :are supported by this server").await?;
|
s.expect(":testserver 005 tester CHANTYPES=# :are supported by this server").await?;
|
||||||
s.expect_nothing().await?;
|
|
||||||
s.send("QUIT :Leaving").await?;
|
|
||||||
s.expect(":testserver ERROR :Leaving the server").await?;
|
|
||||||
s.expect_eof().await?;
|
|
||||||
|
|
||||||
stream.shutdown().await?;
|
stream.shutdown().await?;
|
||||||
|
|
||||||
// wrap up
|
// wrap up
|
||||||
|
|
||||||
server.server.terminate().await?;
|
server.terminate().await.unwrap();
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn scenario_cap_negotiation() -> Result<()> {
|
|
||||||
let mut server = TestServer::start().await?;
|
|
||||||
|
|
||||||
// test scenario
|
|
||||||
|
|
||||||
server.storage.create_user("tester").await?;
|
|
||||||
server.storage.set_password("tester", "password").await?;
|
|
||||||
|
|
||||||
let mut stream = TcpStream::connect(server.server.addr).await?;
|
|
||||||
let mut s = TestScope::new(&mut stream);
|
|
||||||
|
|
||||||
s.send("CAP LS 302").await?;
|
|
||||||
s.send("NICK tester").await?;
|
|
||||||
s.send("USER UserName 0 * :Real Name").await?;
|
|
||||||
s.expect(":testserver CAP * LS :sasl=PLAIN").await?;
|
|
||||||
s.send("CAP REQ :sasl").await?;
|
|
||||||
s.expect(":testserver CAP tester ACK :sasl=PLAIN").await?;
|
|
||||||
s.send("AUTHENTICATE PLAIN").await?;
|
|
||||||
s.expect("AUTHENTICATE +").await?;
|
|
||||||
s.send("AUTHENTICATE dGVzdGVyAHRlc3RlcgBwYXNzd29yZA==").await?; // base64-encoded 'tester\x00tester\x00password'
|
|
||||||
s.expect(":testserver 900 tester ??? ??? :You are now logged in as tester").await?;
|
|
||||||
s.expect(":testserver 903 tester :SASL authentication successful").await?;
|
|
||||||
|
|
||||||
s.send("CAP END").await?;
|
|
||||||
|
|
||||||
s.expect(":testserver NOTICE * :Welcome to my server!").await?;
|
|
||||||
s.expect(":testserver 001 tester :Welcome to Kek Server").await?;
|
|
||||||
s.expect(":testserver 002 tester :Welcome to Kek Server").await?;
|
|
||||||
s.expect(":testserver 003 tester :Welcome to Kek Server").await?;
|
|
||||||
s.expect(":testserver 004 tester testserver kek-0.1.alpha.3 r CFILPQbcefgijklmnopqrstvz").await?;
|
|
||||||
s.expect(":testserver 005 tester CHANTYPES=# :are supported by this server").await?;
|
|
||||||
s.expect_nothing().await?;
|
|
||||||
s.send("QUIT :Leaving").await?;
|
|
||||||
s.expect(":testserver ERROR :Leaving the server").await?;
|
|
||||||
s.expect_eof().await?;
|
|
||||||
|
|
||||||
stream.shutdown().await?;
|
|
||||||
|
|
||||||
// wrap up
|
|
||||||
|
|
||||||
server.server.terminate().await?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue