mod assertion;
mod media;
mod replication;
mod subtitles;
pub use assertion::*;
pub use media::*;
pub use replication::*;
pub use subtitles::*;
use std::fmt;
use serde::{Deserialize, Serialize};
pub const PROTOCOL_VERSION: u32 = 1;
pub const SHARES_CHANGED_EVERY: std::time::Duration = std::time::Duration::from_secs(30);
pub const MAX_REQUEST: u32 = 16 << 10;
pub const MAX_HINTS: usize = crate::invite::MAX_HINTS;
pub mod reset {
pub const NOT_ALLOWED: u32 = 1;
pub const BAD_REQUEST: u32 = 2;
pub const BUSY: u32 = 3;
pub const STALLED: u32 = 4;
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PeerHello {
pub protocol_version: u32,
pub server_id: String,
pub name: String,
}
impl PeerHello {
pub fn within_bounds(mut self) -> Self {
self.name = server_name(self.name);
self
}
}
pub fn names_federated<T: Serialize + ?Sized>(request: &T) -> bool {
fn names(value: &ciborium::Value) -> bool {
match value {
ciborium::Value::Text(text) => federated_id(text),
ciborium::Value::Array(values) => values.iter().any(names),
ciborium::Value::Map(entries) => entries.iter().any(|(k, v)| names(k) || names(v)),
ciborium::Value::Tag(_, value) => names(value),
_ => false,
}
}
ciborium::Value::serialized(request).is_ok_and(|value| names(&value))
}
pub fn federated_id(text: &str) -> bool {
use spoolframe_types::{ArtworkId, LibraryId, MediaId};
text.starts_with("fed:")
&& (text
.parse::<MediaId>()
.is_ok_and(|id| id.remote().is_some())
|| text
.parse::<LibraryId>()
.is_ok_and(|id| id.remote().is_some())
|| text
.parse::<ArtworkId>()
.is_ok_and(|id| id.remote().is_some()))
}
pub fn server_name(mut name: String) -> String {
name.retain(|c| !c.is_control());
crate::messages::cut(&mut name);
name
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PeerInfo {
pub protocol_version: u32,
pub server_id: String,
pub name: String,
pub standing: Standing,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Standing {
Stranger,
Paired,
KeyHeld,
#[serde(other)]
Unknown,
}
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RedeemPairing {
pub code: String,
#[serde(default)]
pub addrs: Vec<String>,
#[serde(default)]
pub relays: Vec<String>,
}
impl fmt::Debug for RedeemPairing {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RedeemPairing")
.field("code", &"<redacted>")
.field("addrs", &self.addrs)
.field("relays", &self.relays)
.finish()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Paired {
pub server_id: String,
pub name: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum PairingRefused {
InviteInvalid,
InviteExpired,
InviteExhausted,
WrongInviteKind,
RateLimited {
retry_after_secs: u64,
},
AlreadyPaired,
ServerIdChanged,
SameServerId,
KeyRefused,
KeyHeld,
#[serde(other)]
Unknown,
}
impl PairingRefused {
pub fn name(self) -> &'static str {
match self {
PairingRefused::InviteInvalid => "invite_invalid",
PairingRefused::InviteExpired => "invite_expired",
PairingRefused::InviteExhausted => "invite_exhausted",
PairingRefused::WrongInviteKind => "wrong_invite_kind",
PairingRefused::RateLimited { .. } => "rate_limited",
PairingRefused::AlreadyPaired => "already_paired",
PairingRefused::ServerIdChanged => "server_id_changed",
PairingRefused::SameServerId => "same_server_id",
PairingRefused::KeyRefused => "key_refused",
PairingRefused::KeyHeld => "key_held",
PairingRefused::Unknown => "unknown",
}
}
}
impl fmt::Display for PairingRefused {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PairingRefused::InviteInvalid => f.write_str("that invite code is not valid"),
PairingRefused::InviteExpired => f.write_str("the invite has expired"),
PairingRefused::InviteExhausted => f.write_str("the invite has been used up"),
PairingRefused::WrongInviteKind => {
f.write_str("that invite is not one to pair a server with")
}
PairingRefused::RateLimited { retry_after_secs } => {
write!(f, "too many attempts; try again in {retry_after_secs} s")
}
PairingRefused::AlreadyPaired => f.write_str("the servers are paired already"),
PairingRefused::ServerIdChanged => {
f.write_str("this server is paired under another server id")
}
PairingRefused::SameServerId => f.write_str("that server id is this server's own"),
PairingRefused::KeyRefused => {
f.write_str("that new key for a paired server is not held for its admin")
}
PairingRefused::KeyHeld => f.write_str(
"the other server holds this server's new key until its admin confirms it",
),
PairingRefused::Unknown => f.write_str("refused for a reason this build does not know"),
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Unpaired {}
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PairServer {
pub link: String,
}
impl fmt::Debug for PairServer {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("PairServer { link: <redacted> }")
}
}
pub const MAX_SHARED: usize = 64;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ShareLibraries {
pub server_id: String,
pub libraries: Vec<spoolframe_types::LibraryId>,
}
impl ShareLibraries {
pub fn within_bounds(mut self) -> Self {
self.libraries.truncate(MAX_SHARED);
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PeerServer {
pub name: String,
pub server_id: String,
pub endpoint_id: String,
pub fingerprint: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum PeerRequest {
Hello(PeerHello),
RedeemPairing(RedeemPairing),
Unpaired(Unpaired),
Libraries,
SharesChanged,
Sync(SyncLibrary),
Artwork(ArtworkFetch),
AssertUser(AssertUser),
SearchSubtitles(SubtitleSearchAsk),
DownloadSubtitle(SubtitleDownloadAsk),
RemoveSubtitleDownload(SubtitleRemovalAsk),
#[serde(other)]
Unknown,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum PeerReply {
Info(PeerInfo),
Paired(Paired),
Refused {
reason: PairingRefused,
},
Done,
Libraries {
libraries: Vec<SharedLibrary>,
},
Page(SyncPage),
Artwork {
content_type: String,
#[serde(with = "serde_bytes")]
bytes: Vec<u8>,
},
NotShared,
NotFound,
Unavailable,
Token(TokenGranted),
AssertionRefused {
reason: AssertionRefused,
},
SubtitleResults {
search: spoolframe_types::SubtitleSearch,
},
SubtitleDownloaded {
downloaded: spoolframe_types::SubtitleDownloaded,
},
Failed {
error: crate::Error,
},
#[serde(other)]
Unknown,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::codec;
fn round_trip<T>(value: T)
where
T: Serialize + serde::de::DeserializeOwned + PartialEq + fmt::Debug,
{
let bytes = codec::encode(&value).unwrap();
assert_eq!(codec::decode::<T>(&bytes).unwrap(), value);
}
#[test]
fn a_fed_id_is_found_in_any_field_of_a_request() {
let alien: spoolframe_types::MediaId = "fed:movie:mzxw6ytbojtg633c:plex:1".parse().unwrap();
assert!(names_federated(&crate::Request::Item { id: alien.clone() }));
assert!(names_federated(&crate::Request::UpdateWatchProgress {
id: alien,
position_ms: 0,
}));
assert!(!names_federated(&crate::Request::Item {
id: "plex:movie:1".parse().unwrap(),
}));
assert!(!names_federated(&crate::Request::SetServerRow {
server_id: "fed:nothing".into(),
shown: true,
}));
}
fn hello() -> PeerHello {
PeerHello {
protocol_version: PROTOCOL_VERSION,
server_id: "mzxw6ytbojtg633c".into(),
name: "Bob's server".into(),
}
}
#[test]
fn every_request_goes_both_ways() {
round_trip(PeerRequest::Hello(hello()));
round_trip(PeerRequest::RedeemPairing(RedeemPairing {
code: "QW7K-2MFP-XR4T".into(),
addrs: vec!["203.0.113.7:7420".into()],
relays: vec!["https://relay.example.org".into()],
}));
round_trip(PeerRequest::Unpaired(Unpaired {}));
}
fn item() -> RemoteItem {
RemoteItem {
id: "plex:track:33".into(),
kind: RemoteKind::Track,
title: "Digital Love".into(),
parent: Some("plex:album:31".into()),
grandparent: Some("plex:artist:30".into()),
parent_title: Some("Discovery".into()),
grandparent_title: Some("Daft Punk".into()),
index: Some(3),
parent_index: Some(1),
year: Some(2001),
date: Some("2001-03-12".into()),
summary: None,
content_rating: None,
duration_ms: Some(301_000),
count: Some(14),
genres: vec!["Electronic".into()],
artwork: vec![RemoteArtwork {
id: "plex:/library/metadata/31/thumb/9".into(),
kind: RemoteArtworkKind::Poster,
}],
external_ids: vec!["mbid://5e4b7b2c".into()],
sort_title: None,
released: None,
added_at: None,
rating_tenths: None,
audience_rating_tenths: None,
size: None,
resolution: None,
dynamic_range: None,
hash: String::new(),
}
.hashed()
}
#[test]
fn replication_goes_both_ways() {
round_trip(PeerRequest::Libraries);
round_trip(PeerRequest::SharesChanged);
round_trip(PeerRequest::Sync(SyncLibrary {
library: "plex:2".into(),
since: None,
}));
round_trip(PeerRequest::Sync(SyncLibrary {
library: "plex:2".into(),
since: Some("0192f1c4".into()),
}));
round_trip(PeerRequest::Artwork(ArtworkFetch {
id: "plex:/library/metadata/31/thumb/9".into(),
}));
round_trip(PeerReply::Libraries {
libraries: vec![SharedLibrary {
id: "plex:2".into(),
title: "Music".into(),
kind: RemoteLibraryKind::Music,
}],
});
for mode in [SyncMode::Changes, SyncMode::Snapshot] {
round_trip(PeerReply::Page(SyncPage {
mode,
cursor: "0192f1c4".into(),
items: vec![item()],
removed: vec!["plex:track:32".into()],
more: true,
}));
}
round_trip(PeerReply::Artwork {
content_type: "image/png".into(),
bytes: vec![0x89, b'P', b'N', b'G'],
});
round_trip(PeerReply::NotShared);
round_trip(PeerReply::NotFound);
round_trip(PeerReply::Unavailable);
}
#[test]
fn an_item_s_hash_follows_its_content() {
let a = item();
assert_eq!(a.digest(), a.hash);
assert_eq!(a.clone().hashed(), a);
let renamed = RemoteItem {
title: "Something About Us".into(),
..a.clone()
};
assert_ne!(renamed.digest(), a.hash);
}
#[test]
fn the_browse_facts_are_sent_only_when_known() {
let bytes = crate::codec::encode(&item()).unwrap();
let value: ciborium::Value = ciborium::from_reader(bytes.as_slice()).unwrap();
let keys: Vec<String> = value
.as_map()
.unwrap()
.iter()
.filter_map(|(k, _)| k.as_text().map(str::to_owned))
.collect();
assert_eq!(
keys,
[
"id",
"kind",
"title",
"parent",
"grandparent",
"parent_title",
"grandparent_title",
"index",
"parent_index",
"year",
"date",
"summary",
"content_rating",
"duration_ms",
"count",
"genres",
"artwork",
"external_ids",
"hash",
]
);
let known = RemoteItem {
sort_title: Some("Something".into()),
released: Some("2001-03-12".into()),
added_at: Some(1_700_000_000),
rating_tenths: Some(87),
audience_rating_tenths: Some(91),
size: Some(30 << 30),
resolution: Some(spoolframe_types::Resolution::FullHd),
dynamic_range: Some(spoolframe_types::DynamicRange::DolbyVision),
..item()
}
.hashed();
assert_ne!(known.hash, item().hash);
round_trip(PeerReply::Page(SyncPage {
mode: SyncMode::Snapshot,
cursor: "c".into(),
items: vec![known],
removed: Vec::new(),
more: false,
}));
}
#[test]
fn a_page_is_cut_to_its_bounds() {
let long = RemoteItem {
title: "x".repeat(MAX_ITEM_TEXT + 10),
summary: Some("y".repeat(MAX_ITEM_SUMMARY + 10)),
genres: vec!["g".into(); MAX_ITEM_GENRES + 1],
artwork: vec![item().artwork[0].clone(); MAX_ITEM_ARTWORK + 1],
external_ids: vec!["tmdb://1".into(); MAX_ITEM_EXTERNAL_IDS + 1],
..item()
};
let page = SyncPage {
mode: SyncMode::Snapshot,
cursor: "c".into(),
items: vec![long; MAX_PAGE_ITEMS + 1],
removed: vec!["plex:movie:1".into(); MAX_PAGE_REMOVED + 1],
more: false,
}
.within_bounds();
assert_eq!(page.items.len(), MAX_PAGE_ITEMS);
assert_eq!(page.removed.len(), MAX_PAGE_REMOVED);
let item = &page.items[0];
assert_eq!(item.title.chars().count(), MAX_ITEM_TEXT);
assert_eq!(
item.summary.as_ref().unwrap().chars().count(),
MAX_ITEM_SUMMARY
);
assert_eq!(item.genres.len(), MAX_ITEM_GENRES);
assert_eq!(item.artwork.len(), MAX_ITEM_ARTWORK);
assert_eq!(item.external_ids.len(), MAX_ITEM_EXTERNAL_IDS);
let listed = libraries_within_bounds(vec![
SharedLibrary {
id: "plex:1".into(),
title: "Movies".into(),
kind: RemoteLibraryKind::Movies,
};
MAX_SHARED_LIBRARIES + 1
]);
assert_eq!(listed.len(), MAX_SHARED_LIBRARIES);
}
#[test]
fn what_a_newer_server_replicates_reads_as_unknown() {
let decode = |value: serde_json::Value| codec::encode(&value).unwrap();
let page = serde_json::json!({
"type": "page",
"mode": "patch",
"cursor": "c",
"items": [{
"id": "plex:clip:1",
"kind": "clip",
"title": "Trailer",
"artwork": [{ "id": "a", "kind": "banner" }],
"hash": "h",
"rating": 7,
}],
"more": false,
});
let PeerReply::Page(page) = codec::decode::<PeerReply>(&decode(page)).unwrap() else {
panic!("not a page");
};
assert_eq!(page.mode, SyncMode::Unknown);
assert_eq!(page.items[0].kind, RemoteKind::Unknown);
assert_eq!(page.items[0].artwork[0].kind, RemoteArtworkKind::Unknown);
let libraries = serde_json::json!({
"type": "libraries",
"libraries": [{ "id": "plex:4", "title": "Photos", "kind": "photos" }],
});
let PeerReply::Libraries { libraries } =
codec::decode::<PeerReply>(&decode(libraries)).unwrap()
else {
panic!("not libraries");
};
assert_eq!(libraries[0].kind, RemoteLibraryKind::Unknown);
}
#[test]
fn every_reply_goes_both_ways() {
for standing in [Standing::Stranger, Standing::Paired, Standing::KeyHeld] {
round_trip(PeerReply::Info(PeerInfo {
protocol_version: PROTOCOL_VERSION,
server_id: "mzxw6ytbojtg633c".into(),
name: "Alice's server".into(),
standing,
}));
}
round_trip(PeerReply::Paired(Paired {
server_id: "mzxw6ytbojtg633c".into(),
name: "Alice's server".into(),
}));
for reason in [
PairingRefused::InviteInvalid,
PairingRefused::InviteExpired,
PairingRefused::InviteExhausted,
PairingRefused::WrongInviteKind,
PairingRefused::RateLimited {
retry_after_secs: 30,
},
PairingRefused::AlreadyPaired,
PairingRefused::ServerIdChanged,
PairingRefused::SameServerId,
PairingRefused::KeyRefused,
PairingRefused::KeyHeld,
] {
round_trip(PeerReply::Refused { reason });
}
round_trip(PeerReply::Done);
}
#[test]
fn a_refusal_s_name_is_its_tag() {
for reason in [
PairingRefused::InviteInvalid,
PairingRefused::InviteExpired,
PairingRefused::InviteExhausted,
PairingRefused::WrongInviteKind,
PairingRefused::RateLimited {
retry_after_secs: 30,
},
PairingRefused::AlreadyPaired,
PairingRefused::ServerIdChanged,
PairingRefused::SameServerId,
PairingRefused::KeyRefused,
PairingRefused::KeyHeld,
PairingRefused::Unknown,
] {
let json = serde_json::to_value(reason).unwrap();
assert_eq!(json["type"], reason.name(), "{reason:?}");
}
}
#[test]
fn what_a_newer_server_sends_reads_as_unknown() {
let decode = |value: serde_json::Value| codec::encode(&value).unwrap();
assert_eq!(
codec::decode::<PeerRequest>(&decode(serde_json::json!({ "type": "share_library" })))
.unwrap(),
PeerRequest::Unknown
);
assert_eq!(
codec::decode::<PeerReply>(&decode(serde_json::json!({ "type": "catalog" }))).unwrap(),
PeerReply::Unknown
);
let info = serde_json::json!({
"type": "info",
"protocol_version": 2,
"server_id": "mzxw6ytbojtg633c",
"name": "Alice's server",
"standing": "neighbour",
"libraries": 3,
});
let PeerReply::Info(info) = codec::decode::<PeerReply>(&decode(info)).unwrap() else {
panic!("not info");
};
assert_eq!(info.standing, Standing::Unknown);
let refused = serde_json::json!({ "type": "refused", "reason": { "type": "busy" } });
assert_eq!(
codec::decode::<PeerReply>(&decode(refused)).unwrap(),
PeerReply::Refused {
reason: PairingRefused::Unknown
}
);
}
#[test]
fn a_notice_of_shares_changed_is_its_tag_alone() {
let wire = codec::encode(&serde_json::json!({ "type": "shares_changed" })).unwrap();
assert_eq!(
codec::decode::<PeerRequest>(&wire).unwrap(),
PeerRequest::SharesChanged
);
assert_eq!(codec::encode(&PeerRequest::SharesChanged).unwrap(), wire);
}
#[test]
fn a_redemption_without_hints_has_none() {
let sent = serde_json::json!({ "type": "redeem_pairing", "code": "QW7K2MFPXR4T" });
let PeerRequest::RedeemPairing(redeem) =
codec::decode(&codec::encode(&sent).unwrap()).unwrap()
else {
panic!("not a redemption");
};
assert!(redeem.addrs.is_empty() && redeem.relays.is_empty());
}
#[test]
fn pairing_from_an_app_goes_both_ways_and_keeps_the_link_out_of_debug() {
use crate::{Error, Reply, Request, Response};
let request = Request::PairServer(PairServer {
link: "spoolframe://invite?server=x&kind=federation&cap=QW7K-2MFP-XR4T".into(),
});
round_trip(request.clone());
assert!(!format!("{request:?}").contains("QW7K"));
round_trip(Response::Ok(Reply::ServerPaired {
server: PeerServer {
name: "Alice's server".into(),
server_id: "mzxw6ytbojtg633c".into(),
endpoint_id: "ab".repeat(32),
fingerprint: "PGJA-BHZ1-F68M-DFSE".into(),
},
}));
round_trip(Response::Err(Error::PairingRefused {
reason: PairingRefused::AlreadyPaired,
}));
}
#[test]
fn sharing_from_an_app_goes_both_ways_and_is_bounded() {
use crate::{Reply, Request, Response};
let library: spoolframe_types::LibraryId = "plex:1".parse().unwrap();
let share = ShareLibraries {
server_id: "mzxw6ytbojtg633c".into(),
libraries: vec![library.clone(); MAX_SHARED + 3],
};
round_trip(Request::ShareLibraries(share.clone()));
round_trip(Response::Ok(Reply::LibrariesShared {
libraries: vec![library],
}));
assert_eq!(share.within_bounds().libraries.len(), MAX_SHARED);
}
#[test]
fn client_exposure_goes_both_ways_and_is_left_out_when_empty() {
use crate::{Event, HomeScreen, Reply, Request, Response, ServerRow};
use spoolframe_types::{Library, LibraryKind, LibraryServer};
let server = LibraryServer {
server_id: "mzxw6ytbojtg633c".into(),
name: "Alice's server".into(),
online: false,
};
let shared = Library {
id: "fed:mzxw6ytbojtg633c:plex:1".parse().unwrap(),
title: "Movies".into(),
kind: LibraryKind::Movies,
server: Some(server.clone()),
};
round_trip(Response::Ok(Reply::Libraries {
libraries: vec![shared],
}));
round_trip(Response::Ok(Reply::Home(HomeScreen {
unavailable: vec!["fed:movie:mzxw6ytbojtg633c:plex:1".parse().unwrap()],
servers: vec![ServerRow {
server,
shown: false,
items: Vec::new(),
}],
..HomeScreen::default()
})));
round_trip(Request::SetServerRow {
server_id: "mzxw6ytbojtg633c".into(),
shown: true,
});
round_trip(Event::LibrariesChanged);
let alien: spoolframe_types::MediaId = "fed:movie:mzxw6ytbojtg633c:plex:1".parse().unwrap();
round_trip(Request::UpdateWatchProgress {
id: alien.clone(),
position_ms: 2_400_000,
});
round_trip(Response::Ok(Reply::Items {
items: Vec::new(),
total: 1,
unavailable: vec![alien],
}));
let items = serde_json::to_value(Reply::Items {
items: Vec::new(),
total: 0,
unavailable: Vec::new(),
})
.unwrap();
assert!(items.get("unavailable").is_none(), "{items}");
let own = Library {
id: "plex:1".parse().unwrap(),
title: "Movies".into(),
kind: LibraryKind::Movies,
server: None,
};
let json = serde_json::to_value(&own).unwrap();
assert!(json.get("server").is_none(), "{json}");
let home = serde_json::to_value(HomeScreen::default()).unwrap();
assert!(home.get("servers").is_none() && home.get("unavailable").is_none());
assert_eq!(serde_json::from_value::<Library>(json).unwrap(), own);
}
#[test]
fn the_code_stays_out_of_debug_output() {
let redeem = RedeemPairing {
code: "QW7K-2MFP-XR4T".into(),
addrs: Vec::new(),
relays: Vec::new(),
};
assert!(!format!("{redeem:?}").contains("QW7K"));
}
#[test]
fn an_assertion_goes_both_ways() {
let assertion = AssertUser {
user: "0192f1c4-5b6e-7a3d-9c41-2e8f7b6a5d4c".into(),
display_name: "Bob".into(),
client_endpoint: "ab".repeat(32),
item: "plex:movie:1".into(),
scope: TokenScope::Play,
preferences: None,
download: None,
};
round_trip(PeerRequest::AssertUser(assertion.clone()));
let mut preferences = spoolframe_types::TrackPreferences::default();
preferences.audio.languages = vec!["fra".into()];
round_trip(PeerRequest::AssertUser(AssertUser {
preferences: Some(preferences),
..assertion.clone()
}));
let granted = TokenGranted {
token: "f00d".repeat(16),
scope: TokenScope::Play,
expires_at: 1_791_000_300,
};
assert!(!format!("{granted:?}").contains("f00d"));
round_trip(PeerReply::Token(granted));
for reason in [
AssertionRefused::NotShared,
AssertionRefused::InvalidUser,
AssertionRefused::InvalidDevice,
AssertionRefused::InvalidScope,
AssertionRefused::Unavailable,
] {
round_trip(PeerReply::AssertionRefused { reason });
}
let decode = |value: serde_json::Value| codec::encode(&value).unwrap();
let sent = serde_json::json!({
"type": "assert_user",
"user": assertion.user,
"display_name": "Bob",
"client_endpoint": assertion.client_endpoint,
"item": "plex:movie:1",
});
assert_eq!(
codec::decode::<PeerRequest>(&decode(sent)).unwrap(),
PeerRequest::AssertUser(assertion.clone())
);
let later = serde_json::json!({
"type": "assert_user",
"user": assertion.user,
"display_name": "Bob",
"client_endpoint": assertion.client_endpoint,
"item": "plex:movie:1",
"scope": "subtitles",
});
let PeerRequest::AssertUser(later) = codec::decode(&decode(later)).unwrap() else {
panic!("not an assertion");
};
assert_eq!(later.scope, TokenScope::Unknown);
let long = AssertUser {
display_name: "x".repeat(500),
..assertion
};
assert_eq!(
long.within_bounds().display_name.chars().count(),
crate::MAX_HELLO_TEXT
);
}
#[test]
fn a_download_token_and_its_device_go_both_ways() {
use crate::{Reply, Request, Response};
round_trip(PeerRequest::AssertUser(AssertUser {
user: "0192f1c4-5b6e-7a3d-9c41-2e8f7b6a5d4c".into(),
display_name: "Bob".into(),
client_endpoint: "ab".repeat(32),
item: "plex:movie:1".into(),
scope: TokenScope::Download,
preferences: None,
download: Some("ab".repeat(16)),
}));
let alien: spoolframe_types::MediaId = "fed:movie:mzxw6ytbojtg633c:plex:1".parse().unwrap();
round_trip(Request::RemoteGrant {
id: alien.clone(),
download: Some("cd".repeat(16)),
});
round_trip(Response::Ok(Reply::RemoteGranted(RemoteGranted {
token: "f00d".repeat(16),
item: "plex:movie:1".parse().unwrap(),
endpoint_id: "ab".repeat(32),
expires_at: 1_791_000_300,
download: Some("ab".repeat(16)),
})));
round_trip(Request::PrepareDownload {
id: alien.clone(),
source: crate::downloads::DownloadSource::Original,
tracks: crate::downloads::DownloadTracks::All,
device: Some(crate::downloads::DownloadDevice {
platform: spoolframe_types::Platform::Ios,
capabilities: None,
}),
});
round_trip(crate::downloads::DownloadState::Failed {
reason: "gone".into(),
cause: crate::downloads::DownloadFailure::NoLongerAvailable,
});
let older = serde_json::json!({ "type": "remote_grant", "id": alien.to_string() });
assert_eq!(
codec::decode::<Request>(&codec::encode(&older).unwrap()).unwrap(),
Request::RemoteGrant {
id: alien,
download: None
}
);
}
#[test]
fn a_name_keeps_no_control_character() {
let forged = PeerHello {
name: "Bob\n2026-10-23T00:00:00Z ERROR forged\u{1b}[0m".into(),
..hello()
};
assert_eq!(
forged.within_bounds().name,
"Bob2026-10-23T00:00:00Z ERROR forged[0m"
);
}
#[test]
fn a_long_name_is_cut_as_a_device_s_is() {
let long = PeerHello {
name: "x".repeat(500),
..hello()
};
assert_eq!(
long.within_bounds().name.chars().count(),
crate::MAX_HELLO_TEXT
);
}
#[test]
fn subtitle_asks_go_both_ways() {
let subtitles = spoolframe_types::SubtitlePreferences {
sdh: spoolframe_types::SdhPreference::Prefer,
..Default::default()
};
round_trip(PeerRequest::SearchSubtitles(SubtitleSearchAsk {
user: "0192f1c4-0000-7000-8000-000000000001".into(),
subtitles,
item: "plex:1".into(),
language: Some("pt-BR".into()),
}));
round_trip(PeerRequest::DownloadSubtitle(SubtitleDownloadAsk {
user: "0192f1c4-0000-7000-8000-000000000001".into(),
item: "plex:1".into(),
result: "opensubtitles:101".into(),
}));
round_trip(PeerRequest::RemoveSubtitleDownload(SubtitleRemovalAsk {
user: "0192f1c4-0000-7000-8000-000000000001".into(),
item: "plex:1".into(),
download: "d1".into(),
}));
round_trip(PeerReply::SubtitleResults {
search: spoolframe_types::SubtitleSearch {
languages: vec!["en".into()],
results: Vec::new(),
sources: Vec::new(),
},
});
round_trip(PeerReply::Failed {
error: crate::Error::QuotaExhausted {
whose: spoolframe_types::QuotaOf::User,
resets_at: Some(1_893_456_000),
site: None,
},
});
#[derive(Serialize)]
struct Bare<'a> {
user: &'a str,
item: &'a str,
}
let bytes = codec::encode(&Bare {
user: "u",
item: "plex:1",
})
.unwrap();
let read: SubtitleSearchAsk = codec::decode(&bytes).unwrap();
assert_eq!(read.subtitles, Default::default());
assert_eq!(read.language, None);
}
}