Files
qbz/crates/qbz-subsonic/tests/live.rs
T
vicrodh 0f41c44dac feat(subsonic): the protocol crate, with the HTTP-200 trap enforced in code
Stage 5's second protocol crate. One client for Navidrome, Gonic,
Airsonic, Astiga and Ampache — which is why the owner scoped this as
"Subsonic, not just Navidrome". Only Navidrome 0.63.2 was on the bench,
so the places servers are likely to differ are named rather than
smoothed over.

The dangerous property of this protocol is that it reports FAILURE with
HTTP 200. `error_for_status()` passes on a refusal. Three defences, all
in code rather than in a reviewer's memory:

- `parse_envelope` is the only way a body becomes a payload, and it
  checks `subsonic-response.status` first. It also accepts XML on the
  failure path, because the measured no-credential getCoverArt reply came
  back as XML DESPITE `f=json` — a JSON-only parser would report "not
  understood" for a perfectly clear "missing parameter: 'u'".
- `looks_like_audio` exists because a refused stream is a ~200-byte JSON
  blob served as the audio body. That is the one way this feature could
  reach the protected decoder with garbage.
- code 40 and 70 become their own variants, so a caller can act on
  "re-authenticate" without matching on prose.

Two mapping decisions worth stating: `duration` is SECONDS on the wire
and milliseconds in QBZ, and `bitDepth` is **0** for a lossy track where
Jellyfin reports null — both sentinels fold to None so downstream sees
one vocabulary rather than two.

The salt is FIXED per install, not rolled per request. Rolling looks
more careful and would make every cover URL unique per request, which
re-downloads every cover on every pass since the artwork cache keys on
the URL. It buys nothing: the password is not transmitted either way.
The consequence is recorded where it matters — a cover URL carries
credentials, so callers must key their cache on the opaque coverArt id.

`detect_sweep_mode` ASKS rather than checks the server's name: search3
with an empty query is 14 requests for this library and getAlbumList2 +
getAlbum is 675, but the fast path is the behaviour most likely to
differ on a server that was never tested, and probing one page costs
~100 ms.

14 unit tests + 6 live. The live suite runs in PARALLEL by design —
Subsonic auth is stateless, so unlike Jellyfin nothing here can revoke
anything else's session. Against Navidrome 0.63.2, three consecutive
runs, 6/6 each:

  ping ......... navidrome 0.63.2, openSubsonic=true
  wrong pass ... Unauthorized, and the raw request really answered 200
  sweep ........ Search3 detected; page 0 = 500 rows, 344 bit depths
  per-album .... fallback returns 500 album ids, 17 tracks in the first
  stream ....... 206 audio/flac, Content-Range total == the API's own
                 reported file size, so nothing was re-encoded
  cover ........ authed image/webp 69 KB; unauthed HTTP 200
                 application/xml 251 B, rejected by the guard

cc
2026-08-20 07:43:46 -06:00

260 lines
9.4 KiB
Rust

//! LIVE tests against a real Subsonic-compatible server. Skipped unless the
//! environment names one.
//!
//! QBZ_SUBSONIC_URL=http://192.168.0.69:4533 \
//! QBZ_SUBSONIC_USER=admin QBZ_SUBSONIC_PASS=... \
//! cargo test -p qbz-subsonic --test live -- --nocapture
//!
//! Unlike Jellyfin's, these run happily in PARALLEL: Subsonic auth is
//! stateless — credentials ride on every request and there is no server-side
//! session to invalidate — so nothing here can revoke anything else's token.
//! That difference is itself worth knowing, and it is why this file has no
//! shared-session machinery.
use std::env;
use std::sync::Once;
use qbz_subsonic::{Credentials, SubsonicClient, SubsonicError, SweepMode};
fn install_crypto_provider() {
static ONCE: Once = Once::new();
ONCE.call_once(|| {
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
});
}
fn server() -> Option<(String, String, String)> {
let v = (
env::var("QBZ_SUBSONIC_URL").ok()?,
env::var("QBZ_SUBSONIC_USER").ok()?,
env::var("QBZ_SUBSONIC_PASS").ok()?,
);
install_crypto_provider();
Some(v)
}
macro_rules! skip_without_server {
() => {
match server() {
Some(v) => v,
None => {
eprintln!("SKIP: set QBZ_SUBSONIC_URL / _USER / _PASS to run the live tests");
return;
}
}
};
}
fn connect(url: &str, user: &str, pass: &str) -> SubsonicClient {
// A FIXED salt, as the app uses one — see `Credentials`' docs for why a
// rolling salt would re-download every cover on every pass.
SubsonicClient::new(url, Credentials::new(user, pass, "qbzlive")).unwrap()
}
#[tokio::test]
async fn ping_reports_an_opensubsonic_server() {
let (url, user, pass) = skip_without_server!();
let info = connect(&url, &user, &pass).ping().await.expect("ping");
eprintln!(
"server: {} {} (openSubsonic={})",
info.kind,
info.server_version.as_deref().unwrap_or(&info.version),
info.open_subsonic
);
// Without the OpenSubsonic extensions there is no bitDepth / samplingRate
// and every quality badge in the app degrades to unknown. Worth asserting
// on the bench so a server downgrade is loud.
assert!(info.open_subsonic, "this server is not OpenSubsonic");
}
/// THE TRAP, end to end. A wrong password is answered with **HTTP 200** and a
/// failure envelope; only the envelope parser can tell. If this ever starts
/// passing as success, every other assertion in the suite is worthless.
#[tokio::test]
async fn wrong_credentials_fail_despite_an_http_200() {
let (url, user, _) = skip_without_server!();
let c = SubsonicClient::new(
&url,
Credentials::new(&user, "definitely-not-the-password", "qbzlive"),
)
.unwrap();
match c.ping().await {
Err(SubsonicError::Unauthorized) => {}
other => panic!("expected Unauthorized, got {other:?}"),
}
// And the raw truth underneath it: the transport really did say 200.
let bad = Credentials::new(&user, "definitely-not-the-password", "qbzlive");
let raw = format!(
"{}/ping.view?{}",
qbz_subsonic::normalize_base_url(&url),
bad.query()
);
let status = reqwest::get(&raw).await.expect("raw ping").status();
assert_eq!(
status.as_u16(),
200,
"the protocol stopped answering 200 on failure — the envelope guard may be redundant now"
);
}
#[tokio::test]
async fn the_fast_sweep_is_detected_and_paginates() {
let (url, user, pass) = skip_without_server!();
let c = connect(&url, &user, &pass);
let mode = c.detect_sweep_mode().await;
eprintln!("sweep mode: {mode:?}");
assert_eq!(mode, SweepMode::Search3, "Navidrome should honour search3");
let first = c.search_page(0).await.expect("page 0");
assert!(!first.is_empty());
let second = c.search_page(500).await.expect("page 500");
assert!(!second.is_empty(), "the second page came back empty");
// Real pagination, not the same page twice.
assert_ne!(first[0].id, second[0].id, "songOffset did not move the window");
let hires = first
.iter()
.filter(|t| t.bit_depth.unwrap_or(0) > 16 || t.sample_rate_hz.unwrap_or(0) > 48_000)
.count();
let with_depth = first.iter().filter(|t| t.bit_depth.is_some()).count();
assert!(
with_depth > 0,
"no row carried a bit depth — this server is not returning OpenSubsonic fields"
);
for t in first.iter().filter(|t| t.suffix == "mp3") {
assert_eq!(t.bit_depth, None, "an mp3 reported a bit depth: {}", t.title);
}
eprintln!(
"page 0: {} rows, {} with a bit depth, {} hi-res",
first.len(),
with_depth,
hires
);
}
/// The PORTABLE path has to work too — it is the fallback for every server
/// that was never on the bench.
#[tokio::test]
async fn the_per_album_sweep_also_returns_tracks() {
let (url, user, pass) = skip_without_server!();
let c = connect(&url, &user, &pass);
let ids = c.album_ids(0).await.expect("album ids");
assert!(!ids.is_empty(), "no albums");
let tracks = c.album_tracks(&ids[0]).await.expect("album tracks");
assert!(!tracks.is_empty(), "album {} has no tracks", ids[0]);
for t in &tracks {
assert!(!t.id.is_empty());
assert!(!t.album_id.is_empty(), "track {} has no album id", t.title);
}
// getAlbumList2 caps at 500 whatever `size` asks for.
assert!(ids.len() <= 500, "the server returned {} albums", ids.len());
eprintln!("per-album: {} album ids, first album has {} tracks", ids.len(), tracks.len());
}
/// THE audio contract. `format=raw` must hand back the ORIGINAL bytes, and the
/// server's own reported `size` is an independent check that nothing was
/// re-encoded on the way out.
#[tokio::test]
async fn the_stream_is_raw_seekable_and_the_reported_size() {
let (url, user, pass) = skip_without_server!();
let c = connect(&url, &user, &pass);
let page = c.search_page(0).await.expect("page");
let track = page
.iter()
.find(|t| t.suffix == "flac" && t.size.is_some())
.expect("no flac with a reported size in the first page");
let resp = reqwest::Client::new()
.get(c.stream_url(&track.id))
.header("Range", "bytes=0-65535")
.send()
.await
.expect("range request");
assert_eq!(resp.status().as_u16(), 206, "the server refused a Range request");
let range = resp
.headers()
.get(reqwest::header::CONTENT_RANGE)
.and_then(|v| v.to_str().ok())
.unwrap_or_default()
.to_string();
let ctype = resp
.headers()
.get(reqwest::header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok())
.unwrap_or("")
.to_string();
let bytes = resp.bytes().await.expect("body");
assert!(
qbz_subsonic::looks_like_audio(Some(&ctype), bytes.len()),
"the body looks like an error envelope, not audio: {ctype}"
);
assert_eq!(&bytes[..4], b"fLaC", "not a FLAC bitstream");
// `Content-Range: bytes 0-65535/<total>` — the total must be the size the
// API reported for the file. A transcode could not match it.
let total: u64 = range
.rsplit('/')
.next()
.and_then(|t| t.parse().ok())
.expect("no total in Content-Range");
assert_eq!(
total,
track.size.unwrap(),
"streamed length differs from the reported file size — the server re-encoded"
);
eprintln!("stream: {} -> 206 {ctype}, total {total} == reported size", track.title);
}
/// Cover art works WITH credentials, and — the trap again — an unauthenticated
/// request answers 200 with something that is not an image.
#[tokio::test]
async fn cover_art_needs_credentials_and_says_so_with_a_200() {
let (url, user, pass) = skip_without_server!();
let c = connect(&url, &user, &pass);
let page = c.search_page(0).await.expect("page");
let track = page
.iter()
.find(|t| t.cover_art.is_some())
.expect("no row in the first page has cover art");
let art = track.cover_art.as_deref().unwrap();
let resp = reqwest::get(c.cover_url(art, qbz_subsonic::IMAGE_PX))
.await
.expect("cover");
let ctype = resp
.headers()
.get(reqwest::header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok())
.unwrap_or("")
.to_string();
assert!(ctype.starts_with("image/"), "authenticated cover returned {ctype}");
let len = resp.bytes().await.expect("cover body").len();
assert!(len > 1024);
// Same endpoint, no credentials: HTTP 200, and NOT an image.
let naked = format!(
"{}/getCoverArt.view?v={}&c=QBZ&id={}",
qbz_subsonic::normalize_base_url(&url),
qbz_subsonic::API_VERSION,
art
);
let bad = reqwest::get(&naked).await.expect("naked cover");
assert_eq!(bad.status().as_u16(), 200, "an unauthenticated cover was NOT a 200");
let bad_ct = bad
.headers()
.get(reqwest::header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok())
.unwrap_or("")
.to_string();
let bad_body = bad.bytes().await.expect("naked body");
assert!(
!qbz_subsonic::looks_like_audio(Some(&bad_ct), bad_body.len()),
"the guard failed to reject an error envelope"
);
eprintln!(
"cover: authed {ctype} {len} B · unauthed HTTP 200 {bad_ct} {} B",
bad_body.len()
);
}