diff --git a/crates/voelin-core/src/link/connect.rs b/crates/voelin-core/src/link/connect.rs index 532529d..c8c862a 100644 --- a/crates/voelin-core/src/link/connect.rs +++ b/crates/voelin-core/src/link/connect.rs @@ -75,12 +75,19 @@ pub(crate) fn connect_params( /// TeamSpeak identity goes as the JSON the key store keeps, and none at all as /// an empty one, for which the TeamSpeak connector makes an identity up (the /// protocol's convention for "generate one", follow-up P11); a Voelin key -/// signs. +/// signs. A TeamSpeak identity that holds no JSON is refused: sent as it is, +/// it would be that same empty blob and be taken for the request to make one +/// up, which only the absence of an identity may be. fn credential(identity: Option<&IdentityBlob>) -> Result { let Some(blob) = identity else { return Ok(Credential::Foreign { kind: KIND_TEAMSPEAK, blob: Bytes::new() }); }; if blob.is_teamspeak() { + if blob.json.trim().is_empty() { + return Err(SessionError::Protocol( + "the TeamSpeak identity cannot be read: it is empty".into(), + )); + } let json = Bytes::copy_from_slice(blob.json.as_bytes()); Ok(Credential::Foreign { kind: KIND_TEAMSPEAK, blob: json }) } else if blob.is_voelin() { @@ -206,6 +213,27 @@ mod tests { assert_eq!(text, "an identity of kind \"account\" cannot be used to connect"); } + /// A TeamSpeak identity with no JSON is no identity. The empty blob is + /// what asks the TeamSpeak connector to make one up (`identity: None`), so + /// it is refused here instead of being sent as that request. + #[test] + fn a_teamspeak_identity_without_json_is_refused() { + let mut options = VoiceOptions::new("ts.example.com", "nick"); + for json in ["", " \n\t"] { + options.identity = Some(IdentityBlob::teamspeak("uid=", 8, json)); + let Err(SessionError::Protocol(text)) = params(&options) else { + panic!("a refusal for {json:?}") + }; + assert_eq!(text, "the TeamSpeak identity cannot be read: it is empty"); + } + // No identity at all is the request to make one up, as before. + options.identity = None; + assert!(matches!( + params(&options).unwrap().credential, + Credential::Foreign { kind: "teamspeak", blob } if blob.is_empty() + )); + } + /// The client version spec rides the link as it was typed, whatever it /// holds, and only for an address that is a TeamSpeak bookmark or link. #[test] diff --git a/crates/voelin-core/tests/session_fake.rs b/crates/voelin-core/tests/session_fake.rs index dd56422..e7fe65c 100644 --- a/crates/voelin-core/tests/session_fake.rs +++ b/crates/voelin-core/tests/session_fake.rs @@ -23,7 +23,8 @@ use voelin_core::history::MessageSource; use voelin_core::settings::Settings; use voelin_core::{ Backends, Command, DownloadTo, Engine, Event, History, HistoryMessage, HistorySource, - JoinFailure, SessionId, SessionState, Source, TransferState, VoiceOptions, VoiceState, + IdentityBlob, JoinFailure, SessionId, SessionState, Source, TransferState, VoiceOptions, + VoiceState, }; use voelin_model::{ChatMessage, ChatTarget, Presence}; use voelin_proto::link::{CloseReason, Connector, FlowId, MediaPacket, SessionError, voice_sub}; @@ -1480,3 +1481,24 @@ async fn a_connect_that_fails_says_why_and_the_next_one_works() { assert_eq!(first.address, "fake://host"); assert_eq!(first.hello.nickname, "tester"); } + +/// A TeamSpeak identity that holds no JSON is refused with its reason before a +/// connector is asked for anything: an empty credential blob would ask a +/// TeamSpeak connector to make an identity up, which only `identity: None` may. +#[tokio::test(flavor = "multi_thread")] +async fn a_teamspeak_identity_without_json_is_refused_and_no_session_is_made() { + let mut h = Harness::start(); + let mut options = VoiceOptions::new("fake://host", "tester"); + options.identity = Some(IdentityBlob::teamspeak("uid=", 8, "")); + h.engine.send(Command::ConnectVoice { session: SESSION, options: Box::new(options) }); + let state = h.state(|s| s.voice == VoiceState::Disconnected).await; + assert_eq!((state.own_channel, state.own_client), (None, None)); + assert_eq!(h.error().await, "voice: the TeamSpeak identity cannot be read: it is empty"); + assert!(h.connector.connects().is_empty(), "the connector was asked to connect"); + assert!(h.connector.last_session().is_none(), "a session was made"); + + // Without an identity the connector is asked as usual. + let handle = h.connect().await; + assert!(handle.close_reason().is_none()); + assert_eq!(h.connector.connects().len(), 1); +}