[client] Calls: four MLS kinds (call, callAnswer, callIce, callEnd) with the frame key in the offer; coturn in the NixOS module; Caddy's global logger mkDefault; v0.3.0
This commit is contained in:
+473
-2
@@ -24,7 +24,7 @@ pub use files::{
|
||||
chunks_for, FileRef, FileUpload, FILE_CHUNK_PLAIN, MAX_FILE_BYTES, MAX_FILE_NAME_CHARS,
|
||||
MAX_MIME_BYTES, MAX_THUMB_BYTES,
|
||||
};
|
||||
pub use tabellarius_protocol::{FileId, FILE_CHUNK_BYTES, MAX_FILE_CHUNKS};
|
||||
pub use tabellarius_protocol::{CallId, CallKey, FileId, FILE_CHUNK_BYTES, MAX_FILE_CHUNKS};
|
||||
|
||||
mod snapshot;
|
||||
pub use snapshot::SNAPSHOT_VERSION;
|
||||
@@ -59,8 +59,18 @@ pub const MAX_POLL_OPTIONS: usize = 10;
|
||||
pub const MAX_OPTION_CHARS: usize = 100;
|
||||
/// The longest poll question, in characters.
|
||||
pub const MAX_QUESTION_CHARS: usize = 300;
|
||||
/// The longest SDP in a call's offer or answer, in bytes (`.agents/features/call-kinds.md`).
|
||||
pub const MAX_SDP_BYTES: usize = 16 * 1024;
|
||||
/// The most ICE candidates in one `callIce`.
|
||||
pub const MAX_CANDIDATES: usize = 32;
|
||||
/// The longest ICE candidate, in bytes.
|
||||
pub const MAX_CANDIDATE_BYTES: usize = 512;
|
||||
/// The longest call a `callEnd` reports, in seconds (a day).
|
||||
pub const MAX_CALL_SECONDS: u32 = 86_400;
|
||||
/// How many calls in progress the client remembers, in memory only; the oldest is forgotten first.
|
||||
pub const MAX_LIVE_CALLS: usize = 8;
|
||||
/// The payload kinds this client knows; any other kind that carries a `seq` is `Unsupported`.
|
||||
const KNOWN_KINDS: [&str; 9] = [
|
||||
const KNOWN_KINDS: [&str; 13] = [
|
||||
"hello",
|
||||
"text",
|
||||
"receipt",
|
||||
@@ -70,6 +80,10 @@ const KNOWN_KINDS: [&str; 9] = [
|
||||
"vote",
|
||||
"pollClose",
|
||||
"file",
|
||||
"call",
|
||||
"callAnswer",
|
||||
"callIce",
|
||||
"callEnd",
|
||||
];
|
||||
|
||||
const WELCOME_ID_LABEL: &[u8] = b"tabellarius v0 welcome mailbox";
|
||||
@@ -105,6 +119,10 @@ pub enum ClientError {
|
||||
FileDamaged,
|
||||
/// A snapshot that does not parse, has another version, or does not match OpenMLS's storage.
|
||||
State,
|
||||
/// A call that cannot be sent: an answer to anything but their live, unanswered offer, candidates
|
||||
/// for a call that is not live, an SDP or a candidate outside its limits, too many candidates, or
|
||||
/// a `callEnd` over [`MAX_CALL_SECONDS`].
|
||||
BadCall,
|
||||
}
|
||||
|
||||
impl From<TransportError> for ClientError {
|
||||
@@ -201,6 +219,58 @@ pub enum Event {
|
||||
/// The other side's app has saved our texts up to `upto` (a `seq` from
|
||||
/// [`Client::sent_seq`]). Only while receipts are on ([`Client::set_receipts`]).
|
||||
Delivered { contact: ContactId, upto: u64 },
|
||||
/// The contact rings: an offer with its SDP and the call's frame key. `at` is the caller's clock
|
||||
/// (Unix seconds): ring only when it is recent, else show the call as missed.
|
||||
CallOffered {
|
||||
contact: ContactId,
|
||||
id: CallId,
|
||||
video: bool,
|
||||
sdp: String,
|
||||
key: CallKey,
|
||||
at: i64,
|
||||
},
|
||||
/// The contact answered our live offer `id`.
|
||||
CallAnswered {
|
||||
contact: ContactId,
|
||||
id: CallId,
|
||||
sdp: String,
|
||||
},
|
||||
/// ICE candidates for the live call `id`; an empty list says there are no more.
|
||||
CallCandidates {
|
||||
contact: ContactId,
|
||||
id: CallId,
|
||||
candidates: Vec<String>,
|
||||
},
|
||||
/// A call is over: the one line it leaves in the chat (a message: `seq` names it, like a text's).
|
||||
/// Arrives even for an offer this client never saw.
|
||||
CallEnded {
|
||||
contact: ContactId,
|
||||
id: CallId,
|
||||
seq: u64,
|
||||
reason: CallEndReason,
|
||||
seconds: u32,
|
||||
},
|
||||
}
|
||||
|
||||
/// Why a call ended, as its `callEnd` says.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum CallEndReason {
|
||||
HangUp,
|
||||
Declined,
|
||||
Busy,
|
||||
Missed,
|
||||
Failed,
|
||||
}
|
||||
|
||||
/// A call in progress, remembered in memory only (`.agents/features/call-kinds.md` §2).
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
struct LiveCall {
|
||||
contact: ContactId,
|
||||
id: CallId,
|
||||
/// We made the offer.
|
||||
ours: bool,
|
||||
answered: bool,
|
||||
}
|
||||
|
||||
/// One operation for the relay, to deliver once the app has saved the snapshot: a blob to post,
|
||||
@@ -380,6 +450,36 @@ enum Payload {
|
||||
/// A new post token for the mailbox the receiver already knows (unblock).
|
||||
#[serde(rename_all = "camelCase")]
|
||||
Inbox { post_token: Token },
|
||||
/// A call's offer (`.agents/features/call-kinds.md`). No `seq`.
|
||||
Call {
|
||||
id: CallId,
|
||||
video: bool,
|
||||
sdp: String,
|
||||
key: CallKey,
|
||||
at: i64,
|
||||
},
|
||||
/// The answer to the receiver's offer `id`. No `seq`.
|
||||
CallAnswer { id: CallId, sdp: String },
|
||||
/// ICE candidates for `id`; `[]` says no more. No `seq`.
|
||||
CallIce { id: CallId, candidates: Vec<String> },
|
||||
/// The call is over: a message, sharing the sender's `seq` with texts.
|
||||
CallEnd {
|
||||
id: CallId,
|
||||
seq: u64,
|
||||
reason: CallEndReason,
|
||||
seconds: u32,
|
||||
},
|
||||
}
|
||||
|
||||
fn sdp_ok(sdp: &str) -> bool {
|
||||
!sdp.is_empty() && sdp.len() <= MAX_SDP_BYTES
|
||||
}
|
||||
|
||||
fn candidates_ok(candidates: &[String]) -> bool {
|
||||
candidates.len() <= MAX_CANDIDATES
|
||||
&& candidates.iter().all(|c| {
|
||||
!c.is_empty() && c.len() <= MAX_CANDIDATE_BYTES && !c.chars().any(char::is_control)
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
@@ -427,6 +527,8 @@ pub struct Client<T: Transport> {
|
||||
next_contact: u32,
|
||||
/// Send receipts and report the other side's (reciprocal). On by default.
|
||||
receipts: bool,
|
||||
/// Calls in progress: in memory only, never in a snapshot.
|
||||
calls: Vec<LiveCall>,
|
||||
}
|
||||
|
||||
impl<T: Transport> Client<T> {
|
||||
@@ -457,6 +559,7 @@ impl<T: Transport> Client<T> {
|
||||
welcome_boxes: Vec::new(),
|
||||
next_contact: 0,
|
||||
receipts: true,
|
||||
calls: Vec::new(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -680,6 +783,137 @@ impl<T: Transport> Client<T> {
|
||||
Ok(Outgoing::post(to, post, blob))
|
||||
}
|
||||
|
||||
/// Seals `payload` for a contact whose `hello` has arrived.
|
||||
fn seal_for(&mut self, index: usize, payload: &Payload) -> Result<Outgoing, ClientError> {
|
||||
let c = &mut self.contacts[index];
|
||||
let (to, post) = c.outbox.ok_or(ClientError::UnknownContact)?;
|
||||
let blob = Self::seal(&self.provider, &self.signer, &mut c.group, payload)?;
|
||||
Ok(Outgoing::post(to, post, blob))
|
||||
}
|
||||
|
||||
fn live_call(&self, contact: ContactId, id: CallId) -> Option<usize> {
|
||||
self.calls
|
||||
.iter()
|
||||
.position(|c| c.contact == contact && c.id == id)
|
||||
}
|
||||
|
||||
fn remember_call(&mut self, call: LiveCall) {
|
||||
if self.calls.len() >= MAX_LIVE_CALLS {
|
||||
self.calls.remove(0);
|
||||
}
|
||||
self.calls.push(call);
|
||||
}
|
||||
|
||||
/// Rings a contact: a new call's id and frame key, and the offer to deliver. `sdp` is the app's
|
||||
/// WebRTC offer; `now` its clock in Unix seconds. No network (`.agents/features/call-kinds.md`).
|
||||
pub fn offer_call(
|
||||
&mut self,
|
||||
contact: ContactId,
|
||||
video: bool,
|
||||
sdp: &str,
|
||||
now: i64,
|
||||
) -> Result<(CallId, CallKey, Outgoing), ClientError> {
|
||||
if !sdp_ok(sdp) {
|
||||
return Err(ClientError::BadCall);
|
||||
}
|
||||
let index = self.index_of(contact)?;
|
||||
let id = CallId::from_bytes(self.random()?);
|
||||
let key = CallKey::from_bytes(self.random()?);
|
||||
let out = self.seal_for(
|
||||
index,
|
||||
&Payload::Call {
|
||||
id,
|
||||
video,
|
||||
sdp: sdp.to_string(),
|
||||
key,
|
||||
at: now,
|
||||
},
|
||||
)?;
|
||||
self.remember_call(LiveCall {
|
||||
contact,
|
||||
id,
|
||||
ours: true,
|
||||
answered: false,
|
||||
});
|
||||
Ok((id, key, out))
|
||||
}
|
||||
|
||||
/// Answers the contact's live offer `id`, once ([`ClientError::BadCall`] otherwise).
|
||||
pub fn answer_call(
|
||||
&mut self,
|
||||
contact: ContactId,
|
||||
id: CallId,
|
||||
sdp: &str,
|
||||
) -> Result<Outgoing, ClientError> {
|
||||
let index = self.index_of(contact)?;
|
||||
let live = self
|
||||
.live_call(contact, id)
|
||||
.filter(|&i| !self.calls[i].ours && !self.calls[i].answered)
|
||||
.ok_or(ClientError::BadCall)?;
|
||||
if !sdp_ok(sdp) {
|
||||
return Err(ClientError::BadCall);
|
||||
}
|
||||
let out = self.seal_for(
|
||||
index,
|
||||
&Payload::CallAnswer {
|
||||
id,
|
||||
sdp: sdp.to_string(),
|
||||
},
|
||||
)?;
|
||||
self.calls[live].answered = true;
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// ICE candidates for the live call `id`, at most [`MAX_CANDIDATES`]; `[]` says no more.
|
||||
pub fn call_candidates(
|
||||
&mut self,
|
||||
contact: ContactId,
|
||||
id: CallId,
|
||||
candidates: &[String],
|
||||
) -> Result<Outgoing, ClientError> {
|
||||
let index = self.index_of(contact)?;
|
||||
if self.live_call(contact, id).is_none() || !candidates_ok(candidates) {
|
||||
return Err(ClientError::BadCall);
|
||||
}
|
||||
self.seal_for(
|
||||
index,
|
||||
&Payload::CallIce {
|
||||
id,
|
||||
candidates: candidates.to_vec(),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// Ends a call, live or not (an app restarted mid-call can still say so): the chat's line for
|
||||
/// it, with its `seq`, and the message to deliver. A live call is forgotten.
|
||||
pub fn end_call(
|
||||
&mut self,
|
||||
contact: ContactId,
|
||||
id: CallId,
|
||||
reason: CallEndReason,
|
||||
seconds: u32,
|
||||
) -> Result<(u64, Outgoing), ClientError> {
|
||||
if seconds > MAX_CALL_SECONDS {
|
||||
return Err(ClientError::BadCall);
|
||||
}
|
||||
let index = self.index_of(contact)?;
|
||||
let seq = self.contacts[index].sent_seq + 1;
|
||||
let out = self.seal_for(
|
||||
index,
|
||||
&Payload::CallEnd {
|
||||
id,
|
||||
seq,
|
||||
reason,
|
||||
seconds,
|
||||
},
|
||||
)?;
|
||||
self.contacts[index].sent_seq = seq;
|
||||
if let Some(i) = self.live_call(contact, id) {
|
||||
self.calls.remove(i);
|
||||
}
|
||||
Ok((seq, out))
|
||||
}
|
||||
|
||||
/// Our reaction to a message in this conversation: `Some(emoji)` sets it (replacing our
|
||||
/// earlier one), `None` removes it. No network: save the snapshot, then deliver.
|
||||
pub fn react(
|
||||
@@ -1467,6 +1701,95 @@ impl<T: Transport> Client<T> {
|
||||
c.outbox = Some((mailbox, post_token));
|
||||
Ok(None)
|
||||
}
|
||||
Payload::Call {
|
||||
id,
|
||||
video,
|
||||
sdp,
|
||||
key,
|
||||
at,
|
||||
} => {
|
||||
let c = &self.contacts[index];
|
||||
if c.name.is_none() {
|
||||
return Err(ClientError::Mls);
|
||||
}
|
||||
let contact = c.id;
|
||||
// A second offer under a live id (on either side) is not a new call.
|
||||
if c.blocked || !sdp_ok(&sdp) || self.calls.iter().any(|l| l.id == id) {
|
||||
return Ok(None);
|
||||
}
|
||||
self.remember_call(LiveCall {
|
||||
contact,
|
||||
id,
|
||||
ours: false,
|
||||
answered: false,
|
||||
});
|
||||
Ok(Some(Event::CallOffered {
|
||||
contact,
|
||||
id,
|
||||
video,
|
||||
sdp,
|
||||
key,
|
||||
at,
|
||||
}))
|
||||
}
|
||||
Payload::CallAnswer { id, sdp } => {
|
||||
let c = &self.contacts[index];
|
||||
if c.name.is_none() {
|
||||
return Err(ClientError::Mls);
|
||||
}
|
||||
let contact = c.id;
|
||||
// Only our own offer to this contact, answered once.
|
||||
let live = self
|
||||
.live_call(contact, id)
|
||||
.filter(|&i| self.calls[i].ours && !self.calls[i].answered);
|
||||
let (false, Some(live), true) = (c.blocked, live, sdp_ok(&sdp)) else {
|
||||
return Ok(None);
|
||||
};
|
||||
self.calls[live].answered = true;
|
||||
Ok(Some(Event::CallAnswered { contact, id, sdp }))
|
||||
}
|
||||
Payload::CallIce { id, candidates } => {
|
||||
let c = &self.contacts[index];
|
||||
if c.name.is_none() {
|
||||
return Err(ClientError::Mls);
|
||||
}
|
||||
let contact = c.id;
|
||||
if c.blocked || !candidates_ok(&candidates) || self.live_call(contact, id).is_none()
|
||||
{
|
||||
return Ok(None);
|
||||
}
|
||||
Ok(Some(Event::CallCandidates {
|
||||
contact,
|
||||
id,
|
||||
candidates,
|
||||
}))
|
||||
}
|
||||
Payload::CallEnd {
|
||||
id,
|
||||
seq,
|
||||
reason,
|
||||
seconds,
|
||||
} => {
|
||||
let c = &mut self.contacts[index];
|
||||
if c.name.is_none() {
|
||||
return Err(ClientError::Mls);
|
||||
}
|
||||
if c.blocked || seq == 0 || seconds > MAX_CALL_SECONDS {
|
||||
return Ok(None);
|
||||
}
|
||||
c.received_seq = c.received_seq.max(seq);
|
||||
let contact = c.id;
|
||||
if let Some(i) = self.live_call(contact, id) {
|
||||
self.calls.remove(i);
|
||||
}
|
||||
Ok(Some(Event::CallEnded {
|
||||
contact,
|
||||
id,
|
||||
seq,
|
||||
reason,
|
||||
seconds,
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2282,6 +2605,133 @@ mod tests {
|
||||
|
||||
/// Every kind this client sends is in `KNOWN_KINDS`: a variant left out would arrive as
|
||||
/// *Unsupported* at a client of the same version.
|
||||
// Calls (`.agents/features/call-kinds.md` §2): what a misbehaving or older client could send.
|
||||
const ID: &str = "AgICAgICAgICAgICAgICAg"; // [2; 16]
|
||||
const KEY: &str = "AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM"; // [3; 32]
|
||||
|
||||
fn call_json(kind: &str, rest: &str) -> String {
|
||||
format!(r#"{{"kind":"{kind}","id":"{ID}"{rest}}}"#)
|
||||
}
|
||||
|
||||
fn offer() -> String {
|
||||
call_json(
|
||||
"call",
|
||||
&format!(r#","video":false,"sdp":"v=0","key":"{KEY}","at":1"#),
|
||||
)
|
||||
}
|
||||
|
||||
fn receive_all(c: &mut Client<MemoryTransport>) -> Vec<Event> {
|
||||
c.receive().expect("receive").events
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_second_offer_under_a_live_id_and_answers_that_are_not_to_our_offer_are_dropped() {
|
||||
let (mut eva, mut jan) = paired();
|
||||
// Jan rings twice under one id: the second is not a new call.
|
||||
for _ in 0..2 {
|
||||
let out = seal_raw(&mut jan, &offer());
|
||||
jan.deliver(&out).expect("deliver");
|
||||
}
|
||||
let got = receive_all(&mut eva);
|
||||
assert_eq!(got.len(), 1, "{got:?}");
|
||||
// Jan "answers" his own offer: Eva did not make it, so it is dropped.
|
||||
let answer = call_json("callAnswer", r#","sdp":"v=0""#);
|
||||
let out = seal_raw(&mut jan, &answer);
|
||||
jan.deliver(&out).expect("deliver");
|
||||
assert!(receive_all(&mut eva).is_empty());
|
||||
// Eva's own offer, answered twice: the second is dropped.
|
||||
let (id, _, out) = eva
|
||||
.offer_call(eva.contacts[0].id, false, "v=0", 1)
|
||||
.expect("offer");
|
||||
eva.deliver(&out).expect("deliver");
|
||||
jan.receive().expect("receive");
|
||||
let answer = call_json("callAnswer", r#","sdp":"v=0""#).replace(ID, &id.to_wire());
|
||||
for _ in 0..2 {
|
||||
let out = seal_raw(&mut jan, &answer);
|
||||
jan.deliver(&out).expect("deliver");
|
||||
}
|
||||
assert_eq!(receive_all(&mut eva).len(), 1);
|
||||
// An answer from another contact: the live call belongs to someone else.
|
||||
let (id, _, _) = eva
|
||||
.offer_call(eva.contacts[0].id, false, "v=0", 1)
|
||||
.expect("offer");
|
||||
eva.calls.last_mut().expect("live").contact = ContactId(99);
|
||||
let answer = call_json("callAnswer", r#","sdp":"v=0""#).replace(ID, &id.to_wire());
|
||||
let out = seal_raw(&mut jan, &answer);
|
||||
jan.deliver(&out).expect("deliver");
|
||||
assert!(receive_all(&mut eva).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_call_kinds_are_dropped_and_an_unknown_field_drops_a_known_kind() {
|
||||
let (mut eva, mut jan) = paired();
|
||||
let many = format!(r#","candidates":[{}]"#, vec![r#""c""#; 33].join(","));
|
||||
for json in [
|
||||
call_json(
|
||||
"call",
|
||||
&format!(r#","video":false,"sdp":"","key":"{KEY}","at":1"#),
|
||||
),
|
||||
call_json(
|
||||
"call",
|
||||
&format!(r#","video":false,"sdp":"v=0","key":"{KEY}","at":1,"x":1"#),
|
||||
),
|
||||
call_json("callIce", r#","candidates":["c"]"#), // no live call
|
||||
call_json("callEnd", r#","seq":0,"reason":"hangUp","seconds":1"#),
|
||||
call_json("callEnd", r#","seq":1,"reason":"hangUp","seconds":86401"#),
|
||||
call_json("callEnd", r#","seq":1,"reason":"shrug","seconds":1"#),
|
||||
] {
|
||||
let out = seal_raw(&mut jan, &json);
|
||||
jan.deliver(&out).expect("deliver");
|
||||
assert!(receive_all(&mut eva).is_empty(), "{json}");
|
||||
}
|
||||
let out = seal_raw(&mut jan, &offer());
|
||||
jan.deliver(&out).expect("deliver");
|
||||
assert_eq!(receive_all(&mut eva).len(), 1);
|
||||
for json in [
|
||||
call_json("callIce", &many),
|
||||
call_json(
|
||||
"callIce",
|
||||
r#","candidates":["a
|
||||
b"]"#,
|
||||
),
|
||||
call_json("callIce", r#","candidates":[""]"#),
|
||||
] {
|
||||
let out = seal_raw(&mut jan, &json);
|
||||
jan.deliver(&out).expect("deliver");
|
||||
assert!(receive_all(&mut eva).is_empty(), "{json}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_call_end_carries_a_seq_so_an_older_client_shows_it_and_drops_the_rest() {
|
||||
for (json, seq) in [
|
||||
(offer(), None),
|
||||
(call_json("callAnswer", r#","sdp":"v=0""#), None),
|
||||
(call_json("callIce", r#","candidates":[]"#), None),
|
||||
(
|
||||
call_json("callEnd", r#","seq":5,"reason":"missed","seconds":0"#),
|
||||
Some(5),
|
||||
),
|
||||
] {
|
||||
let value: serde_json::Value = serde_json::from_str(&json).expect("json");
|
||||
assert_eq!(
|
||||
value.get("seq").and_then(serde_json::Value::as_u64),
|
||||
seq,
|
||||
"{json}"
|
||||
);
|
||||
// What a client without these kinds does with it: the rule for unknown kinds.
|
||||
let older = json.replace(r#""kind":"call"#, r#""kind":"futureCall"#);
|
||||
let parsed = parse_payload(older.as_bytes());
|
||||
match seq {
|
||||
Some(s) => assert!(
|
||||
matches!(parsed, Ok(Parsed::Unknown { seq }) if seq == s),
|
||||
"{older}"
|
||||
),
|
||||
None => assert!(parsed.is_err(), "{older}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_payload_kind_is_known() {
|
||||
let to = MessageRef {
|
||||
@@ -2322,6 +2772,27 @@ mod tests {
|
||||
}],
|
||||
},
|
||||
their_file(3, "a.jpg", "image/jpeg", 10, Some(JPEG.to_vec())),
|
||||
Payload::Call {
|
||||
id: CallId::from_bytes([2; 16]),
|
||||
video: true,
|
||||
sdp: "v=0".into(),
|
||||
key: CallKey::from_bytes([3; 32]),
|
||||
at: 1,
|
||||
},
|
||||
Payload::CallAnswer {
|
||||
id: CallId::from_bytes([2; 16]),
|
||||
sdp: "v=0".into(),
|
||||
},
|
||||
Payload::CallIce {
|
||||
id: CallId::from_bytes([2; 16]),
|
||||
candidates: vec![],
|
||||
},
|
||||
Payload::CallEnd {
|
||||
id: CallId::from_bytes([2; 16]),
|
||||
seq: 4,
|
||||
reason: CallEndReason::Declined,
|
||||
seconds: 0,
|
||||
},
|
||||
];
|
||||
assert_eq!(all.len(), KNOWN_KINDS.len());
|
||||
for payload in all {
|
||||
|
||||
@@ -188,6 +188,7 @@ impl<T: Transport> Client<T> {
|
||||
welcome_boxes: state.welcome_boxes,
|
||||
next_contact: state.next_contact,
|
||||
receipts: state.receipts,
|
||||
calls: Vec::new(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,308 @@
|
||||
//! Calls: the four MLS kinds and the call's key (`.agents/features/call-kinds.md` §4). No I/O: runs on
|
||||
//! the Android emulator too.
|
||||
|
||||
use tabellarius_client::*;
|
||||
|
||||
type C = Client<MemoryTransport>;
|
||||
|
||||
const OFFER: &str = "v=0\r\no=- 1 2 IN IP4 0.0.0.0\r\na=fingerprint:sha-256 AA:BB\r\n";
|
||||
const ANSWER: &str = "v=0\r\no=- 3 4 IN IP4 0.0.0.0\r\na=fingerprint:sha-256 CC:DD\r\n";
|
||||
const NOW: i64 = 1_767_000_000;
|
||||
|
||||
fn pair(relay: &MemoryTransport) -> (C, ContactId, C, ContactId) {
|
||||
let mut eva = Client::new("Eva", relay.clone()).expect("eva");
|
||||
let mut jan = Client::new("Jan", relay.clone()).expect("jan");
|
||||
let invite = eva.create_invite().expect("invite");
|
||||
let jan_side = jan.accept_invite(&invite).expect("accept");
|
||||
let Some(Event::Joined {
|
||||
contact: eva_side, ..
|
||||
}) = eva.poll().expect("poll").first().cloned()
|
||||
else {
|
||||
panic!("no join")
|
||||
};
|
||||
jan.poll().expect("poll");
|
||||
(eva, eva_side, jan, jan_side)
|
||||
}
|
||||
|
||||
fn candidates(n: usize) -> Vec<String> {
|
||||
(0..n)
|
||||
.map(|i| format!("candidate:{i} 1 udp 2 192.0.2.{i} 3478 typ relay raddr 0.0.0.0 rport 0"))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Eva rings Jan: the offer is delivered and fetched.
|
||||
fn ring(eva: &mut C, eva_side: ContactId, jan: &mut C) -> (CallId, CallKey) {
|
||||
let (id, key, out) = eva.offer_call(eva_side, true, OFFER, NOW).expect("offer");
|
||||
eva.deliver(&out).expect("deliver");
|
||||
let events = jan.poll().expect("poll");
|
||||
assert!(
|
||||
events
|
||||
.iter()
|
||||
.any(|e| matches!(e, Event::CallOffered { id: got, .. } if *got == id)),
|
||||
"{events:?}"
|
||||
);
|
||||
(id, key)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_call_is_offered_answered_and_ended_and_both_hold_the_same_key() {
|
||||
let relay = MemoryTransport::default();
|
||||
let (mut eva, eva_side, mut jan, jan_side) = pair(&relay);
|
||||
|
||||
let (id, key, out) = eva.offer_call(eva_side, true, OFFER, NOW).expect("offer");
|
||||
eva.deliver(&out).expect("deliver");
|
||||
assert_eq!(
|
||||
jan.poll().expect("poll"),
|
||||
vec![Event::CallOffered {
|
||||
contact: jan_side,
|
||||
id,
|
||||
video: true,
|
||||
sdp: OFFER.to_string(),
|
||||
key,
|
||||
at: NOW,
|
||||
}]
|
||||
);
|
||||
|
||||
let out = jan.answer_call(jan_side, id, ANSWER).expect("answer");
|
||||
jan.deliver(&out).expect("deliver");
|
||||
let out = jan
|
||||
.call_candidates(jan_side, id, &candidates(3))
|
||||
.expect("ice");
|
||||
jan.deliver(&out).expect("deliver");
|
||||
assert_eq!(
|
||||
eva.poll().expect("poll"),
|
||||
vec![
|
||||
Event::CallAnswered {
|
||||
contact: eva_side,
|
||||
id,
|
||||
sdp: ANSWER.to_string()
|
||||
},
|
||||
Event::CallCandidates {
|
||||
contact: eva_side,
|
||||
id,
|
||||
candidates: candidates(3)
|
||||
},
|
||||
]
|
||||
);
|
||||
// Eva's candidates, then an empty list: no more.
|
||||
for list in [candidates(2), Vec::new()] {
|
||||
let out = eva.call_candidates(eva_side, id, &list).expect("ice");
|
||||
eva.deliver(&out).expect("deliver");
|
||||
}
|
||||
let got = jan.poll().expect("poll");
|
||||
assert_eq!(got.len(), 2, "{got:?}");
|
||||
|
||||
let (seq, out) = eva
|
||||
.end_call(eva_side, id, CallEndReason::HangUp, 83)
|
||||
.expect("end");
|
||||
assert_eq!(seq, 1, "the call's line is Eva's first message");
|
||||
eva.deliver(&out).expect("deliver");
|
||||
assert_eq!(
|
||||
jan.poll().expect("poll"),
|
||||
vec![Event::CallEnded {
|
||||
contact: jan_side,
|
||||
id,
|
||||
seq: 1,
|
||||
reason: CallEndReason::HangUp,
|
||||
seconds: 83
|
||||
}]
|
||||
);
|
||||
// Over on both sides: nothing more can be said about it.
|
||||
assert_eq!(
|
||||
jan.call_candidates(jan_side, id, &candidates(1)).err(),
|
||||
Some(ClientError::BadCall)
|
||||
);
|
||||
assert_eq!(
|
||||
eva.call_candidates(eva_side, id, &candidates(1)).err(),
|
||||
Some(ClientError::BadCall)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_call_has_its_own_id_and_key() {
|
||||
let relay = MemoryTransport::default();
|
||||
let (mut eva, eva_side, _jan, _) = pair(&relay);
|
||||
let (a, ka, _) = eva.offer_call(eva_side, false, OFFER, NOW).expect("offer");
|
||||
let (b, kb, _) = eva.offer_call(eva_side, false, OFFER, NOW).expect("offer");
|
||||
assert_ne!(a, b);
|
||||
assert_ne!(ka.as_bytes(), kb.as_bytes());
|
||||
assert_eq!(format!("{ka:?}"), "CallKey(..)", "a key is never printed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn what_is_not_a_live_call_or_breaks_the_limits_is_refused_before_anything_is_sealed() {
|
||||
let relay = MemoryTransport::default();
|
||||
let (mut eva, eva_side, mut jan, jan_side) = pair(&relay);
|
||||
let (id, _) = ring(&mut eva, eva_side, &mut jan);
|
||||
let other = CallId::from_bytes([7; 16]);
|
||||
|
||||
// An answer: only to their live offer, once.
|
||||
assert_eq!(
|
||||
eva.answer_call(eva_side, id, ANSWER).err(),
|
||||
Some(ClientError::BadCall),
|
||||
"her own offer"
|
||||
);
|
||||
assert_eq!(
|
||||
jan.answer_call(jan_side, other, ANSWER).err(),
|
||||
Some(ClientError::BadCall),
|
||||
"no such call"
|
||||
);
|
||||
assert_eq!(
|
||||
jan.answer_call(jan_side, id, "").err(),
|
||||
Some(ClientError::BadCall),
|
||||
"an empty SDP"
|
||||
);
|
||||
jan.answer_call(jan_side, id, ANSWER).expect("answer");
|
||||
assert_eq!(
|
||||
jan.answer_call(jan_side, id, ANSWER).err(),
|
||||
Some(ClientError::BadCall),
|
||||
"a second answer"
|
||||
);
|
||||
|
||||
// Offers and candidates within their limits.
|
||||
let long = "a".repeat(16 * 1024 + 1);
|
||||
assert_eq!(
|
||||
eva.offer_call(eva_side, true, &long, NOW).err(),
|
||||
Some(ClientError::BadCall)
|
||||
);
|
||||
assert_eq!(
|
||||
eva.offer_call(eva_side, true, "", NOW).err(),
|
||||
Some(ClientError::BadCall)
|
||||
);
|
||||
assert_eq!(
|
||||
jan.call_candidates(jan_side, other, &candidates(1)).err(),
|
||||
Some(ClientError::BadCall)
|
||||
);
|
||||
assert_eq!(
|
||||
jan.call_candidates(jan_side, id, &candidates(33)).err(),
|
||||
Some(ClientError::BadCall)
|
||||
);
|
||||
for bad in ["", "candidate:1\r\na=evil", &"c".repeat(513)] {
|
||||
assert_eq!(
|
||||
jan.call_candidates(jan_side, id, &[bad.to_string()]).err(),
|
||||
Some(ClientError::BadCall),
|
||||
"{bad:?}"
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
eva.end_call(eva_side, id, CallEndReason::HangUp, 86_401)
|
||||
.err(),
|
||||
Some(ClientError::BadCall)
|
||||
);
|
||||
assert_eq!(
|
||||
relay.waiting_blobs(),
|
||||
0,
|
||||
"nothing refused was sealed or sent"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_missed_call_leaves_its_line_even_when_the_offer_never_arrived_and_receipts_cover_it() {
|
||||
let relay = MemoryTransport::default();
|
||||
let (mut eva, eva_side, mut jan, jan_side) = pair(&relay);
|
||||
// The offer is made but never delivered (the relay was away); Eva gives up.
|
||||
let (id, _, _never) = eva.offer_call(eva_side, false, OFFER, NOW).expect("offer");
|
||||
let (seq, out) = eva
|
||||
.end_call(eva_side, id, CallEndReason::Missed, 0)
|
||||
.expect("end");
|
||||
eva.deliver(&out).expect("deliver");
|
||||
assert_eq!(
|
||||
jan.poll().expect("poll"),
|
||||
vec![Event::CallEnded {
|
||||
contact: jan_side,
|
||||
id,
|
||||
seq,
|
||||
reason: CallEndReason::Missed,
|
||||
seconds: 0
|
||||
}]
|
||||
);
|
||||
assert_eq!(
|
||||
eva.poll().expect("poll"),
|
||||
vec![Event::Delivered {
|
||||
contact: eva_side,
|
||||
upto: seq
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_blocked_contacts_call_does_not_ring() {
|
||||
let relay = MemoryTransport::default();
|
||||
let (mut eva, eva_side, mut jan, jan_side) = pair(&relay);
|
||||
let (_, _, out) = jan.offer_call(jan_side, true, OFFER, NOW).expect("offer");
|
||||
jan.deliver(&out).expect("deliver");
|
||||
let rotate = eva.block(eva_side).expect("block");
|
||||
eva.deliver(&rotate).expect("rotate");
|
||||
let events = eva.poll().expect("poll");
|
||||
assert!(events.is_empty(), "{events:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_restart_forgets_live_calls_and_a_call_can_still_be_ended() {
|
||||
let relay = MemoryTransport::default();
|
||||
let (mut eva, eva_side, mut jan, jan_side) = pair(&relay);
|
||||
let (id, _) = ring(&mut eva, eva_side, &mut jan);
|
||||
let saved = eva.snapshot().expect("snapshot");
|
||||
drop(eva);
|
||||
let mut eva = Client::restore(&saved, relay.clone()).expect("restore");
|
||||
|
||||
let out = jan.answer_call(jan_side, id, ANSWER).expect("answer");
|
||||
jan.deliver(&out).expect("deliver");
|
||||
let events = eva.poll().expect("poll");
|
||||
assert!(
|
||||
events.is_empty(),
|
||||
"an answer to a call she no longer knows: {events:?}"
|
||||
);
|
||||
let (_, out) = eva
|
||||
.end_call(eva_side, id, CallEndReason::Failed, 0)
|
||||
.expect("end after a restart");
|
||||
eva.deliver(&out).expect("deliver");
|
||||
assert!(matches!(
|
||||
jan.poll().expect("poll").as_slice(),
|
||||
[Event::CallEnded {
|
||||
reason: CallEndReason::Failed,
|
||||
..
|
||||
}]
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn at_most_eight_calls_are_live_and_the_oldest_is_forgotten_first() {
|
||||
let relay = MemoryTransport::default();
|
||||
let (mut eva, eva_side, mut jan, jan_side) = pair(&relay);
|
||||
let ids: Vec<CallId> = (0..9)
|
||||
.map(|_| ring(&mut eva, eva_side, &mut jan).0)
|
||||
.collect();
|
||||
assert_eq!(
|
||||
jan.answer_call(jan_side, ids[0], ANSWER).err(),
|
||||
Some(ClientError::BadCall)
|
||||
);
|
||||
jan.answer_call(jan_side, ids[1], ANSWER)
|
||||
.expect("the second is still live");
|
||||
jan.answer_call(jan_side, ids[8], ANSWER)
|
||||
.expect("the last is live");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_relay_holds_none_of_a_calls_sdp_candidates_or_key() {
|
||||
let relay = MemoryTransport::default();
|
||||
let (mut eva, eva_side, _jan, _) = pair(&relay);
|
||||
let (id, key, out) = eva.offer_call(eva_side, true, OFFER, NOW).expect("offer");
|
||||
eva.deliver(&out).expect("deliver");
|
||||
let out = eva
|
||||
.call_candidates(eva_side, id, &candidates(4))
|
||||
.expect("ice");
|
||||
eva.deliver(&out).expect("deliver");
|
||||
// Before anyone fetches: both are waiting on the relay, sealed.
|
||||
let held = relay.everything_stored();
|
||||
let contains = |needle: &[u8]| held.windows(needle.len()).any(|w| w == needle);
|
||||
for needle in [
|
||||
&b"fingerprint"[..],
|
||||
b"192.0.2.",
|
||||
b"candidate:",
|
||||
key.as_bytes(),
|
||||
id.as_bytes(),
|
||||
] {
|
||||
assert!(!contains(needle), "the relay holds {needle:?}");
|
||||
}
|
||||
}
|
||||
@@ -247,6 +247,19 @@ fn pair_and_talk_through_a_real_relay_which_never_sees_a_name_or_a_word() {
|
||||
.compose_poll(jan_side, question, &options, false)
|
||||
.expect("poll");
|
||||
jan.deliver(&out).expect("deliver");
|
||||
// And a call (`.agents/features/call-kinds.md`): its SDP, candidates, id and key neither.
|
||||
let sdp = "v=0
|
||||
a=fingerprint:sha-256 SDP-CANARY
|
||||
";
|
||||
let (call, key, out) = jan
|
||||
.offer_call(jan_side, true, sdp, 1_767_000_000)
|
||||
.expect("offer");
|
||||
jan.deliver(&out).expect("deliver");
|
||||
let candidates = vec!["candidate:1 1 udp 2 203.0.113.77 3478 typ relay".to_string()];
|
||||
let out = jan
|
||||
.call_candidates(jan_side, call, &candidates)
|
||||
.expect("ice");
|
||||
jan.deliver(&out).expect("deliver");
|
||||
|
||||
// Snapshot the database while both messages wait, before anyone fetches.
|
||||
let waiting = std::fs::read(&db).expect("db");
|
||||
@@ -280,6 +293,19 @@ fn pair_and_talk_through_a_real_relay_which_never_sees_a_name_or_a_word() {
|
||||
options: options.clone(),
|
||||
multiple: false,
|
||||
},
|
||||
Event::CallOffered {
|
||||
contact: eva_side,
|
||||
id: call,
|
||||
video: true,
|
||||
sdp: sdp.into(),
|
||||
key,
|
||||
at: 1_767_000_000,
|
||||
},
|
||||
Event::CallCandidates {
|
||||
contact: eva_side,
|
||||
id: call,
|
||||
candidates: candidates.clone(),
|
||||
},
|
||||
]
|
||||
);
|
||||
let after = std::fs::read(&db).expect("db");
|
||||
@@ -294,6 +320,10 @@ fn pair_and_talk_through_a_real_relay_which_never_sees_a_name_or_a_word() {
|
||||
("inviter's name", "Eva Nováková".as_bytes()),
|
||||
("invitee's name", "Jan Dvořák".as_bytes()),
|
||||
("invite secret", invite.secret.as_bytes().as_slice()),
|
||||
("call SDP", b"SDP-CANARY".as_slice()),
|
||||
("call candidate", b"203.0.113.77".as_slice()),
|
||||
("call id", call.as_bytes().as_slice()),
|
||||
("call key", key.as_bytes().as_slice()),
|
||||
] {
|
||||
assert!(
|
||||
!contains(snapshot, needle),
|
||||
|
||||
Reference in New Issue
Block a user