diff --git a/Cargo.lock b/Cargo.lock index 09a8b25..f838968 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1279,6 +1279,7 @@ name = "projection-xmpp" version = "0.0.2-dev" dependencies = [ "anyhow", + "assert_matches", "derive_more", "futures-util", "lavina-core", @@ -1290,6 +1291,7 @@ dependencies = [ "tokio", "tokio-rustls", "tracing", + "tracing-subscriber", "uuid", ] diff --git a/Cargo.toml b/Cargo.toml index 0953ce7..07a6784 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ tracing = "0.1.37" # logging & tracing api prometheus = { version = "0.13.3", default-features = false } base64 = "0.21.3" lavina-core = { path = "crates/lavina-core" } +tracing-subscriber = "0.3.16" [package] name = "lavina" @@ -44,7 +45,7 @@ serde.workspace = true serde_json = "1.0.93" tokio.workspace = true tracing.workspace = true -tracing-subscriber = "0.3.16" +tracing-subscriber.workspace = true futures-util.workspace = true prometheus.workspace = true nonempty.workspace = true diff --git a/crates/projection-irc/tests/lib.rs b/crates/projection-irc/tests/lib.rs index 11131f2..56e9420 100644 --- a/crates/projection-irc/tests/lib.rs +++ b/crates/projection-irc/tests/lib.rs @@ -1,14 +1,14 @@ use std::time::Duration; -use anyhow::Result; +use anyhow::{anyhow, Result}; use prometheus::Registry as MetricsRegistry; -use tokio::io::{AsyncWriteExt, BufReader}; +use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader}; use tokio::net::tcp::{ReadHalf, WriteHalf}; use tokio::net::TcpStream; use lavina_core::repo::{Storage, StorageConfig}; use lavina_core::{player::PlayerRegistry, room::RoomRegistry}; -use projection_irc::{launch, read_irc_message, ServerConfig}; +use projection_irc::{launch, read_irc_message, RunningServer, ServerConfig}; struct TestScope<'a> { reader: BufReader>, @@ -44,29 +44,66 @@ impl<'a> TestScope<'a> { self.buffer.clear(); 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 { + 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] async fn scenario_basic() -> Result<()> { - 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(); + let mut server = TestServer::start().await?; // test scenario - storage.create_user("tester").await?; - storage.set_password("tester", "password").await?; + server.storage.create_user("tester").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); s.send("PASS password").await?; @@ -78,11 +115,15 @@ async fn scenario_basic() -> Result<()> { 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.terminate().await.unwrap(); + server.server.terminate().await?; Ok(()) } diff --git a/crates/projection-xmpp/Cargo.toml b/crates/projection-xmpp/Cargo.toml index c69cb12..6395b29 100644 --- a/crates/projection-xmpp/Cargo.toml +++ b/crates/projection-xmpp/Cargo.toml @@ -15,6 +15,10 @@ futures-util.workspace = true quick-xml.workspace = true proto-xmpp = { path = "../proto-xmpp" } uuid = { version = "1.3.0", features = ["v4"] } -tokio-rustls = "0.24.1" +tokio-rustls = { version = "0.24.1", features = ["dangerous_configuration"] } rustls-pemfile = "1.0.2" derive_more.workspace = true + +[dev-dependencies] +tracing-subscriber.workspace = true +assert_matches = "1.5.0" diff --git a/crates/projection-xmpp/rustfmt.toml b/crates/projection-xmpp/rustfmt.toml new file mode 100644 index 0000000..5a3cea5 --- /dev/null +++ b/crates/projection-xmpp/rustfmt.toml @@ -0,0 +1,2 @@ +max_width = 120 +chain_width = 120 diff --git a/crates/projection-xmpp/src/lib.rs b/crates/projection-xmpp/src/lib.rs index 9865d24..29b3dee 100644 --- a/crates/projection-xmpp/src/lib.rs +++ b/crates/projection-xmpp/src/lib.rs @@ -58,13 +58,24 @@ struct Authenticated { xmpp_muc_name: Resource, } +pub struct RunningServer { + pub addr: SocketAddr, + terminator: Terminator, +} + +impl RunningServer { + pub async fn terminate(self) -> Result<()> { + self.terminator.terminate().await + } +} + pub async fn launch( config: ServerConfig, players: PlayerRegistry, rooms: RoomRegistry, metrics: MetricsRegistry, storage: Storage, -) -> Result { +) -> Result { log::info!("Starting XMPP projection"); let certs = certs(&mut SyncBufReader::new(File::open(config.cert)?))?; @@ -81,6 +92,8 @@ pub async fn launch( }); let listener = TcpListener::bind(config.listen_on).await?; + let addr = listener.local_addr()?; + let terminator = Terminator::spawn(|mut termination| async move { let (stopped_tx, mut stopped_rx) = channel(32); let mut actors = HashMap::new(); @@ -139,7 +152,7 @@ pub async fn launch( Ok(()) }); log::info!("Started XMPP projection"); - Ok(terminator) + Ok(RunningServer { addr, terminator }) } async fn handle_socket( @@ -165,6 +178,7 @@ async fn handle_socket( .with_single_cert(vec![config.cert.clone()], config.key.clone())?; config.key_log = Arc::new(tokio_rustls::rustls::KeyLogFile::new()); + log::debug!("Accepting TLS connection..."); let acceptor = TlsAcceptor::from(Arc::new(config)); let new_stream = acceptor.accept(stream).await?; log::debug!("TLS connection established"); diff --git a/crates/projection-xmpp/tests/certs/xmpp.key b/crates/projection-xmpp/tests/certs/xmpp.key new file mode 100644 index 0000000..869f211 --- /dev/null +++ b/crates/projection-xmpp/tests/certs/xmpp.key @@ -0,0 +1,52 @@ +-----BEGIN PRIVATE KEY----- +MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQCuViBTGN8tMaQ8 +G8zixL7duElTFCuP6wQhmDsX8ut4V3eEshUpDIIFkCSX17hzfI7duBp1pe7Ket+F +z5XjbV+ruvxpawvsCgsfGrwXE1vaDVJduy0JyRzLvRSXELWgAbcdllbvBKvGLtY1 +ogm5YJWLbtgQJjutMoLisxn7Xd04fzMQy4aqhy2ZrsxyQSMINuR1Qz/VBzDZi4EH +Q08rb7GManQfbabbTs1I/GHuAM7PDeb/ou9AZHPASg2fzam5SJhvDYutHmX8wOS3 +b+I+amI6g3N8fJssjx0ryAEL+c+Mbv6mXQhGqh7T++kXtB8h5GoLCOg3yGtaW7o0 +PacAP1UsadDsGN13cWAAsytg1BxqgWk6IqA3Yff5uc2A+TYmX5K5DV46sonovybo +FI9fdKmL4oCbMIz+Tq+L5vHsdUh5/5S6F2RIKQDJIDYJfE7XVCPyToabQirQsQ/B +n27L0bCO1hD9cGR+z5td9TPGxrm7GUVGZ/fC58Q4WD/TrVhC3pUq8n7hMzGakg+w +Ri7FSJTPIZQXiTL/HtPle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ToUSsh2HDGCZfrkn+kbZPp4y0ZCBj0TeYGaDRiTaSBYX9pEgkC1s +52f31rrRhbRlErlTitGS6Ra4Phm4GDV9EDOs07teqQlEM3bmRcybF/7LlyMz8jHD +TQKCAQEAvjdo48rvwjlj4IKkhkEWP2Qn19bxeQT6lWb9Wtgqar1CIIk9ei74V6xF +5cyhw9vCXqxvlDM37n0JRHc0EM70aCE7IFL7WxCmmwGXBwT/PeyIJMaaZippd+Ff +QjBx4z6OSsCnzWnM8YPkRwSSCGVk9EDvxQtwS7BmpqSgilHL1/n1yDKAiiGsvAPI +uR5WhXzeN/OUHji7Gp5tCxc6Lo4dKqMhQrMFTomUbupw/o4X8TZ0O3hZdzLv9d4Q +9LM5nI4JOwB4qEUOY9kFQNjvxwPvrTPj8QuIyPIjPQYWZ4Jw8lNCOK3COPV8H+Rb +kO/SCOEdhp17yWQspky/Uo1RC4lKVA== +-----END PRIVATE KEY----- diff --git a/crates/projection-xmpp/tests/certs/xmpp.pem b/crates/projection-xmpp/tests/certs/xmpp.pem new file mode 100644 index 0000000..f9c2229 --- /dev/null +++ b/crates/projection-xmpp/tests/certs/xmpp.pem @@ -0,0 +1,30 @@ +-----BEGIN CERTIFICATE----- +MIIFDTCCAvWgAwIBAgIUGd9jmau898T/kGdIeiRCcdXueaowDQYJKoZIhvcNAQEL +BQAwFjEUMBIGA1UEAwwLZXhhbXBsZS5jb20wHhcNMjMxMDA5MTIyMTU2WhcNMjMx +MTA4MTIyMTU2WjAWMRQwEgYDVQQDDAtleGFtcGxlLmNvbTCCAiIwDQYJKoZIhvcN +AQEBBQADggIPADCCAgoCggIBAK5WIFMY3y0xpDwbzOLEvt24SVMUK4/rBCGYOxfy +63hXd4SyFSkMggWQJJfXuHN8jt24GnWl7sp634XPleNtX6u6/GlrC+wKCx8avBcT +W9oNUl27LQnJHMu9FJcQtaABtx2WVu8Eq8Yu1jWiCblglYtu2BAmO60yguKzGftd +3Th/MxDLhqqHLZmuzHJBIwg25HVDP9UHMN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XkxGQZnhfmQdYwc7Y3IgsJJoN0ZfqnyBY+u/ZOBegcZFuhMiDGILJBgFi +QmgQFTozXKkeEzFdWoRn57lgcBABcMHSucnjekk80uLIWIlL36IyYkPbeI6/I0/l +ajKftsVEY96hYUXtDBykBpg6gsJXj2gP2FXHW7ngtuI4/mOqI3ltcWh1C83MvM5N +zMYfTNtxVM1vyvN1M7b7iMeosvmAIvQE3bFk/pJCHAZPfsu0zmeizPHBgCySygLn +JQ== +-----END CERTIFICATE----- diff --git a/crates/projection-xmpp/tests/lib.rs b/crates/projection-xmpp/tests/lib.rs new file mode 100644 index 0000000..39675eb --- /dev/null +++ b/crates/projection-xmpp/tests/lib.rs @@ -0,0 +1,186 @@ +use std::sync::Arc; +use std::time::Duration; + +use anyhow::Result; +use assert_matches::*; +use prometheus::Registry as MetricsRegistry; +use quick_xml::events::Event; +use quick_xml::NsReader; +use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; +use tokio::io::{ReadHalf as GenericReadHalf, WriteHalf as GenericWriteHalf}; +use tokio::net::tcp::{ReadHalf, WriteHalf}; +use tokio::net::TcpStream; +use tokio_rustls::client::TlsStream; +use tokio_rustls::rustls::client::ServerCertVerifier; +use tokio_rustls::rustls::{ClientConfig, ServerName}; +use tokio_rustls::TlsConnector; + +use lavina_core::player::PlayerRegistry; +use lavina_core::repo::{Storage, StorageConfig}; +use lavina_core::room::RoomRegistry; +use projection_xmpp::{launch, ServerConfig}; +use proto_xmpp::xml::{Continuation, FromXml, Parser}; + +pub async fn read_irc_message(reader: &mut BufReader>, buf: &mut Vec) -> Result { + let mut size = 0; + let res = reader.read_until(b'\n', buf).await?; + size += res; + return Ok(size); +} + +struct TestScope<'a> { + reader: NsReader>>, + writer: WriteHalf<'a>, + buffer: Vec, +} + +impl<'a> TestScope<'a> { + fn new(stream: &mut TcpStream) -> TestScope<'_> { + let (reader, writer) = stream.split(); + let reader = NsReader::from_reader(BufReader::new(reader)); + let buffer = vec![]; + TestScope { reader, writer, buffer } + } + + async fn send(&mut self, str: &str) -> Result<()> { + self.writer.write_all(str.as_bytes()).await?; + self.writer.flush().await?; + Ok(()) + } + + async fn next_xml_event(&mut self) -> Result> { + self.buffer.clear(); + let event = self.reader.read_event_into_async(&mut self.buffer).await?; + Ok(event) + } + + async fn read(&mut self) -> Result { + self.buffer.clear(); + let (ns, event) = self.reader.read_resolved_event_into_async(&mut self.buffer).await?; + let mut parser: Continuation<_, std::result::Result> = T::parse().consume(ns, &event); + loop { + match parser { + Continuation::Final(res) => return Ok(res?), + Continuation::Continue(next) => { + let (ns, event) = self.reader.read_resolved_event_into_async(&mut self.buffer).await?; + parser = next.consume(ns, &event); + } + } + } + } +} + +struct TestScopeTls<'a> { + reader: NsReader>>>, + writer: GenericWriteHalf<&'a mut TlsStream>, + buffer: Vec, + pub timeout: Duration, +} + +impl<'a> TestScopeTls<'a> { + fn new(stream: &'a mut TlsStream, buffer: Vec) -> TestScopeTls<'a> { + let (reader, writer) = tokio::io::split(stream); + let reader = NsReader::from_reader(BufReader::new(reader)); + let timeout = Duration::from_millis(100); + + TestScopeTls { + reader, + writer, + buffer, + timeout, + } + } + + async fn send(&mut self, str: &str) -> Result<()> { + self.writer.write_all(str.as_bytes()).await?; + self.writer.flush().await?; + Ok(()) + } + + async fn next_xml_event(&mut self) -> Result> { + self.buffer.clear(); + let event = self.reader.read_event_into_async(&mut self.buffer); + let event = tokio::time::timeout(self.timeout, event).await??; + Ok(event) + } +} + +struct IgnoreCertVerification; +impl ServerCertVerifier for IgnoreCertVerification { + fn verify_server_cert( + &self, + _end_entity: &tokio_rustls::rustls::Certificate, + _intermediates: &[tokio_rustls::rustls::Certificate], + _server_name: &ServerName, + _scts: &mut dyn Iterator, + _ocsp_response: &[u8], + _now: std::time::SystemTime, + ) -> std::result::Result { + Ok(tokio_rustls::rustls::client::ServerCertVerified::assertion()) + } +} + +#[tokio::test] +async fn scenario_basic() -> Result<()> { + tracing_subscriber::fmt::init(); + 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 + + storage.create_user("tester").await?; + storage.set_password("tester", "password").await?; + + let mut stream = TcpStream::connect(server.addr).await?; + let mut s = TestScope::new(&mut stream); + tracing::info!("TCP connection established"); + + s.send(r#""#).await?; + s.send(r#""#).await?; + assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream")); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"features")); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"starttls")); + assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"required")); + assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"starttls")); + assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"features")); + s.send(r#""#).await?; + assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"proceed")); + let buffer = s.buffer; + tracing::info!("TLS feature negotiation complete"); + + let connector = TlsConnector::from(Arc::new( + ClientConfig::builder() + .with_safe_defaults() + .with_custom_certificate_verifier(Arc::new(IgnoreCertVerification)) + .with_no_client_auth(), + )); + tracing::info!("Initiating TLS connection..."); + let mut stream = connector.connect(ServerName::IpAddress(server.addr.ip()), stream).await?; + tracing::info!("TLS connection established"); + + let mut s = TestScopeTls::new(&mut stream, buffer); + + s.send(r#""#).await?; + s.send(r#""#).await?; + assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream")); + + stream.shutdown().await?; + + // wrap up + + server.terminate().await?; + Ok(()) +}