forked from lavina/lavina
1
0
Fork 0

xmpp: in integration tests extract server startup code

This commit is contained in:
Nikita Vilunov 2024-04-13 02:32:41 +02:00
parent fd694cd75c
commit 0944c449ca
1 changed files with 53 additions and 61 deletions

View File

@ -1,5 +1,4 @@
use std::io::ErrorKind; use std::io::ErrorKind;
use std::net::SocketAddr;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
@ -20,7 +19,7 @@ use tokio_rustls::TlsConnector;
use lavina_core::player::PlayerRegistry; use lavina_core::player::PlayerRegistry;
use lavina_core::repo::{Storage, StorageConfig}; use lavina_core::repo::{Storage, StorageConfig};
use lavina_core::room::RoomRegistry; use lavina_core::room::RoomRegistry;
use projection_xmpp::{launch, ServerConfig}; use projection_xmpp::{launch, RunningServer, ServerConfig};
use proto_xmpp::xml::{Continuation, FromXml, Parser}; use proto_xmpp::xml::{Continuation, FromXml, Parser};
pub async fn read_irc_message(reader: &mut BufReader<ReadHalf<'_>>, buf: &mut Vec<u8>) -> Result<usize> { pub async fn read_irc_message(reader: &mut BufReader<ReadHalf<'_>>, buf: &mut Vec<u8>) -> Result<usize> {
@ -122,9 +121,16 @@ impl ServerCertVerifier for IgnoreCertVerification {
} }
} }
#[tokio::test] struct TestServer {
async fn scenario_basic() -> Result<()> { metrics: MetricsRegistry,
tracing_subscriber::fmt::try_init(); storage: Storage,
rooms: RoomRegistry,
players: PlayerRegistry,
server: RunningServer,
}
impl TestServer {
async fn start() -> Result<TestServer> {
let _ = tracing_subscriber::fmt::try_init();
let config = ServerConfig { let config = ServerConfig {
listen_on: "127.0.0.1:0".parse().unwrap(), listen_on: "127.0.0.1:0".parse().unwrap(),
cert: "tests/certs/xmpp.pem".parse().unwrap(), cert: "tests/certs/xmpp.pem".parse().unwrap(),
@ -137,14 +143,27 @@ async fn scenario_basic() -> Result<()> {
.await?; .await?;
let rooms = RoomRegistry::new(&mut metrics, storage.clone()).unwrap(); let rooms = RoomRegistry::new(&mut metrics, storage.clone()).unwrap();
let players = PlayerRegistry::empty(rooms.clone(), &mut metrics).unwrap(); let players = PlayerRegistry::empty(rooms.clone(), &mut metrics).unwrap();
let server = launch(config, players, rooms, metrics, storage.clone()).await.unwrap(); let server = launch(config, players.clone(), rooms.clone(), metrics.clone(), storage.clone()).await.unwrap();
Ok(TestServer {
metrics,
storage,
rooms,
players,
server,
})
}
}
#[tokio::test]
async fn scenario_basic() -> Result<()> {
let mut server = TestServer::start().await?;
// test scenario // test scenario
storage.create_user("tester").await?; server.storage.create_user("tester").await?;
storage.set_password("tester", "password").await?; server.storage.set_password("tester", "password").await?;
let mut stream = TcpStream::connect(server.addr).await?; let mut stream = TcpStream::connect(server.server.addr).await?;
let mut s = TestScope::new(&mut stream); let mut s = TestScope::new(&mut stream);
tracing::info!("TCP connection established"); tracing::info!("TCP connection established");
@ -169,7 +188,7 @@ async fn scenario_basic() -> Result<()> {
.with_no_client_auth(), .with_no_client_auth(),
)); ));
tracing::info!("Initiating TLS connection..."); tracing::info!("Initiating TLS connection...");
let mut stream = connector.connect(ServerName::IpAddress(server.addr.ip()), stream).await?; let mut stream = connector.connect(ServerName::IpAddress(server.server.addr.ip()), stream).await?;
tracing::info!("TLS connection established"); tracing::info!("TLS connection established");
let mut s = TestScopeTls::new(&mut stream, buffer); let mut s = TestScopeTls::new(&mut stream, buffer);
@ -183,33 +202,20 @@ async fn scenario_basic() -> Result<()> {
// wrap up // wrap up
server.terminate().await?; server.server.terminate().await?;
Ok(()) Ok(())
} }
#[tokio::test] #[tokio::test]
async fn scenario_basic_without_headers() -> Result<()> { async fn scenario_basic_without_headers() -> Result<()> {
tracing_subscriber::fmt::try_init(); let mut server = TestServer::start().await?;
let config = ServerConfig {
listen_on: "127.0.0.1:0".parse().unwrap(),
cert: "tests/certs/xmpp.pem".parse().unwrap(),
key: "tests/certs/xmpp.key".parse().unwrap(),
};
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
storage.create_user("tester").await?; server.storage.create_user("tester").await?;
storage.set_password("tester", "password").await?; server.storage.set_password("tester", "password").await?;
let mut stream = TcpStream::connect(server.addr).await?; let mut stream = TcpStream::connect(server.server.addr).await?;
let mut s = TestScope::new(&mut stream); let mut s = TestScope::new(&mut stream);
tracing::info!("TCP connection established"); tracing::info!("TCP connection established");
@ -233,7 +239,7 @@ async fn scenario_basic_without_headers() -> Result<()> {
.with_no_client_auth(), .with_no_client_auth(),
)); ));
tracing::info!("Initiating TLS connection..."); tracing::info!("Initiating TLS connection...");
let mut stream = connector.connect(ServerName::IpAddress(server.addr.ip()), stream).await?; let mut stream = connector.connect(ServerName::IpAddress(server.server.addr.ip()), stream).await?;
tracing::info!("TLS connection established"); tracing::info!("TLS connection established");
let mut s = TestScopeTls::new(&mut stream, buffer); let mut s = TestScopeTls::new(&mut stream, buffer);
@ -246,33 +252,20 @@ async fn scenario_basic_without_headers() -> Result<()> {
// wrap up // wrap up
server.terminate().await?; server.server.terminate().await?;
Ok(()) Ok(())
} }
#[tokio::test] #[tokio::test]
async fn terminate_socket() -> Result<()> { async fn terminate_socket() -> Result<()> {
tracing_subscriber::fmt::try_init(); let mut server = TestServer::start().await?;
let config = ServerConfig {
listen_on: "127.0.0.1:0".parse().unwrap(),
cert: "tests/certs/xmpp.pem".parse().unwrap(),
key: "tests/certs/xmpp.key".parse().unwrap(),
};
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();
let address: SocketAddr = ("127.0.0.1:0".parse().unwrap());
// test scenario // test scenario
storage.create_user("tester").await?; server.storage.create_user("tester").await?;
storage.set_password("tester", "password").await?; server.storage.set_password("tester", "password").await?;
let mut stream = TcpStream::connect(server.addr).await?; let mut stream = TcpStream::connect(server.server.addr).await?;
let mut s = TestScope::new(&mut stream); let mut s = TestScope::new(&mut stream);
tracing::info!("TCP connection established"); tracing::info!("TCP connection established");
@ -288,7 +281,6 @@ async fn terminate_socket() -> Result<()> {
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"features")); assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"features"));
s.send(r#"<starttls/>"#).await?; s.send(r#"<starttls/>"#).await?;
assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"proceed")); assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"proceed"));
let buffer = s.buffer;
let connector = TlsConnector::from(Arc::new( let connector = TlsConnector::from(Arc::new(
ClientConfig::builder() ClientConfig::builder()
@ -298,10 +290,10 @@ async fn terminate_socket() -> Result<()> {
)); ));
tracing::info!("Initiating TLS connection..."); tracing::info!("Initiating TLS connection...");
let mut stream = connector.connect(ServerName::IpAddress(server.addr.ip()), stream).await?; let mut stream = connector.connect(ServerName::IpAddress(server.server.addr.ip()), stream).await?;
tracing::info!("TLS connection established"); tracing::info!("TLS connection established");
server.terminate().await?; server.server.terminate().await?;
assert_eq!(stream.read_u8().await.unwrap_err().kind(), ErrorKind::UnexpectedEof); assert_eq!(stream.read_u8().await.unwrap_err().kind(), ErrorKind::UnexpectedEof);