Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

test(media-proxy): remove server, storage, http and policy tests

Hampuse94830be84ff
diff --git a/fluxer_media_proxy/src/asset_hash.rs b/fluxer_media_proxy/src/asset_hash.rs
index 826511724..8ccb0581a 100644
--- a/fluxer_media_proxy/src/asset_hash.rs
+++ b/fluxer_media_proxy/src/asset_hash.rs
@@ -30,27 +30,3 @@ pub fn strip_animation_prefix(hash: &str) -> &str {
pub fn has_animation_prefix(hash: &str) -> bool {
hash.starts_with("a_")
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn parsed_hashes_split_the_animation_prefix_from_the_digest() {
- let plain = AssetHash::parse("abc123");
- assert_eq!("abc123", plain.digest());
- assert!(!plain.is_animated());
-
- let animated = AssetHash::parse("a_abc123");
- assert_eq!("abc123", animated.digest());
- assert!(animated.is_animated());
-
- let empty = AssetHash::parse("");
- assert_eq!("", empty.digest());
- assert!(!empty.is_animated());
-
- let doubled = AssetHash::parse("a_a_abc123");
- assert_eq!("a_abc123", doubled.digest());
- assert!(doubled.is_animated());
- }
-}
diff --git a/fluxer_media_proxy/src/byte_budget.rs b/fluxer_media_proxy/src/byte_budget.rs
index e88d1994c..96330e7e7 100644
--- a/fluxer_media_proxy/src/byte_budget.rs
+++ b/fluxer_media_proxy/src/byte_budget.rs
@@ -208,70 +208,3 @@ impl Drop for ByteReservationInner {
self.budget.inner.release(amount);
}
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn try_reserve_exactly_at_limit_succeeds() {
- let budget = ByteBudget::new(100);
- let reservation = budget.try_reserve(100).expect("reserve at limit");
- assert_eq!(reservation.amount(), 100);
- }
-
- #[test]
- fn try_reserve_over_limit_fails() {
- let budget = ByteBudget::new(100);
- let _reservation = budget.try_reserve(100).expect("reserve at limit");
- assert!(budget.try_reserve(1).is_none());
- }
-
- #[test]
- fn try_grow_within_remaining_budget_succeeds() {
- let budget = ByteBudget::new(100);
- let mut reservation = budget.try_reserve(60).expect("reserve under limit");
- assert!(reservation.try_grow(30));
- assert_eq!(reservation.amount(), 90);
- }
-
- #[test]
- fn try_grow_past_remaining_budget_fails_and_leaves_reservation_unchanged() {
- let budget = ByteBudget::new(100);
- let mut reservation = budget.try_reserve(60).expect("reserve under limit");
- assert!(!reservation.try_grow(41));
- assert_eq!(reservation.amount(), 60);
- let remaining = budget.try_reserve(40).expect("untouched remaining budget");
- assert_eq!(remaining.amount(), 40);
- }
-
- #[test]
- fn shrink_to_releases_delta_back_to_budget() {
- let budget = ByteBudget::new(100);
- let mut reservation = budget.try_reserve(100).expect("reserve at limit");
- reservation.shrink_to(70);
- assert_eq!(reservation.amount(), 70);
- let released = budget.try_reserve(30).expect("released delta available");
- assert_eq!(released.amount(), 30);
- }
-
- #[test]
- fn drop_releases_full_reservation() {
- let budget = ByteBudget::new(100);
- let reservation = budget.try_reserve(100).expect("reserve at limit");
- drop(reservation);
- let reservation = budget
- .try_reserve(100)
- .expect("full budget available again");
- assert_eq!(reservation.amount(), 100);
- }
-
- #[test]
- fn resident_bytes_uses_reservation_amount_when_budgeted() {
- let budget = ByteBudget::new(100);
- let reservation = budget.try_reserve(50).expect("reserve under limit");
- let budgeted = BudgetedBytes::budgeted(Bytes::from_static(b"hello"), reservation);
- assert_eq!(budgeted.len(), 5);
- assert_eq!(budgeted.resident_bytes(), 50);
- }
-}
diff --git a/fluxer_media_proxy/src/byte_cache.rs b/fluxer_media_proxy/src/byte_cache.rs
index 6520f3100..ba0f87e84 100644
--- a/fluxer_media_proxy/src/byte_cache.rs
+++ b/fluxer_media_proxy/src/byte_cache.rs
@@ -86,22 +86,6 @@ impl ByteCacheValue for BudgetedBytes {
}
}
-#[derive(Clone, Copy, Debug, Eq, PartialEq)]
-#[must_use]
-pub enum ByteCacheInsertOutcome {
- Inserted,
- Rejected(ByteCacheInsertRejection),
-}
-
-#[derive(Clone, Copy, Debug, Eq, PartialEq)]
-pub enum ByteCacheInsertRejection {
- CacheDisabled,
- EmptyValue,
- MaxEntryBytesExceeded,
- WeightExceedsCapacity,
- WeightExceedsU32,
-}
-
#[derive(Debug)]
pub struct ByteCache<T: ByteCacheValue = BudgetedBytes> {
storage: Option<ByteCacheStorage<T>>,
@@ -177,21 +161,6 @@ where
storage.inner.insert(key, value);
ByteCacheInsertOutcome::Inserted
}
-
- #[cfg(test)]
- pub(crate) fn settle(&self) {
- if let Some(storage) = self.storage.as_ref() {
- storage.inner.run_pending_tasks();
- }
- }
-
- #[cfg(test)]
- fn weighted_size(&self) -> u64 {
- self.storage
- .as_ref()
- .map(|storage| storage.inner.weighted_size())
- .unwrap_or(0)
- }
}
fn entry_weight<T: ByteCacheValue>(key_bytes: usize, value: &T) -> Option<u32> {
@@ -202,179 +171,3 @@ fn entry_weight<T: ByteCacheValue>(key_bytes: usize, value: &T) -> Option<u32> {
.and_then(|weight| weight.checked_add(value.bytes().resident_bytes()))
.and_then(|weight| u32::try_from(weight).ok())
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use bytes::Bytes;
- use std::sync::{
- Arc,
- atomic::{AtomicU64, Ordering},
- };
-
- fn byte_cache(capacity_bytes: usize, max_entry_bytes: usize, ttl_ms: u64) -> ByteCache {
- ByteCache::new(
- ByteCacheSettings::clamped(capacity_bytes, max_entry_bytes, ttl_ms),
- || {},
- )
- }
-
- #[test]
- fn byte_cache_roundtrips_returning_shared_handles() {
- let cache = byte_cache(4096, 4096, 60_000);
- assert_eq!(
- ByteCacheInsertOutcome::Inserted,
- cache.put("a", Bytes::from_static(b"abc"))
- );
- let a1 = cache.get("a").unwrap();
- assert_eq!(b"abc", a1.as_ref());
- let a2 = cache.get("a").unwrap();
- assert_eq!(a1.as_ptr(), a2.as_ptr());
- }
-
- #[test]
- fn byte_cache_bounds_total_weighted_size() {
- let cache = byte_cache(4096, 4096, 60_000);
- for i in 0..50 {
- let _ = cache.put(format!("k{i}"), Bytes::from(vec![0u8; 4]));
- }
- cache.settle();
- assert!(cache.weighted_size() <= 4096);
- }
-
- #[test]
- fn byte_cache_skips_entries_over_max_entry_size() {
- let cache = byte_cache(4096, 4, 60_000);
- assert_eq!(
- ByteCacheInsertOutcome::Rejected(ByteCacheInsertRejection::MaxEntryBytesExceeded),
- cache.put("large", Bytes::from_static(b"12345"))
- );
- assert_eq!(
- None,
- cache.get("large").map(|value| value.as_bytes().clone())
- );
- }
-
- #[test]
- fn byte_cache_clamps_the_entry_ceiling_to_the_capacity() {
- let cache = byte_cache(4096, 8192, 60_000);
- assert_eq!(
- ByteCacheInsertOutcome::Rejected(ByteCacheInsertRejection::MaxEntryBytesExceeded),
- cache.put("over", Bytes::from(vec![7u8; 5000]))
- );
- assert_eq!(
- None,
- cache.get("over").map(|value| value.as_bytes().clone())
- );
- assert_eq!(
- ByteCacheInsertOutcome::Inserted,
- cache.put("fits", Bytes::from(vec![7u8; 3000]))
- );
- assert_eq!(
- Some(Bytes::from(vec![7u8; 3000])),
- cache.get("fits").map(|value| value.as_bytes().clone())
- );
- }
-
- #[test]
- fn byte_cache_disabled_when_capacity_or_ttl_is_zero() {
- let no_capacity = byte_cache(0, 4, 60_000);
- assert_eq!(
- ByteCacheInsertOutcome::Rejected(ByteCacheInsertRejection::CacheDisabled),
- no_capacity.put("a", Bytes::from_static(b"abc"))
- );
- assert_eq!(
- None,
- no_capacity.get("a").map(|value| value.as_bytes().clone())
- );
-
- let no_ttl = byte_cache(32, 4, 0);
- assert_eq!(
- ByteCacheInsertOutcome::Rejected(ByteCacheInsertRejection::CacheDisabled),
- no_ttl.put("a", Bytes::from_static(b"abc"))
- );
- assert_eq!(None, no_ttl.get("a").map(|value| value.as_bytes().clone()));
-
- let no_max_entry = byte_cache(4096, 0, 60_000);
- assert_eq!(
- ByteCacheInsertOutcome::Rejected(ByteCacheInsertRejection::CacheDisabled),
- no_max_entry.put("a", Bytes::from_static(b"abc"))
- );
- }
-
- #[test]
- fn byte_cache_settings_reject_every_invalid_shape() {
- assert_eq!(
- Err(ByteCacheSettingsError::CapacityIsZero),
- ByteCacheSettings::try_new(0, 1, 1)
- );
- assert_eq!(
- Err(ByteCacheSettingsError::MaxEntryBytesIsZero),
- ByteCacheSettings::try_new(16, 0, 1)
- );
- assert_eq!(
- Err(ByteCacheSettingsError::MaxEntryBytesExceedsCapacity),
- ByteCacheSettings::try_new(16, 17, 1)
- );
- assert_eq!(
- Err(ByteCacheSettingsError::TtlIsZero),
- ByteCacheSettings::try_new(16, 16, 0)
- );
- assert_eq!(
- ByteCacheSettings::disabled(),
- ByteCacheSettings::clamped(16, 17, 0)
- );
- assert_eq!(
- ByteCacheSettings::try_new(16, 16, 1),
- Ok(ByteCacheSettings::clamped(16, 17, 1))
- );
- }
-
- #[test]
- fn byte_cache_rejects_an_empty_value_and_a_weight_over_the_capacity() {
- let cache = byte_cache(2_048, 1_024, 60_000);
- assert_eq!(
- ByteCacheInsertOutcome::Rejected(ByteCacheInsertRejection::EmptyValue),
- cache.put("empty", Bytes::new())
- );
- assert_eq!(
- ByteCacheInsertOutcome::Rejected(ByteCacheInsertRejection::MaxEntryBytesExceeded),
- cache.put("large", Bytes::from(vec![0; 1_025]))
- );
- assert_eq!(
- ByteCacheInsertOutcome::Inserted,
- cache.put("0123456789abcdef", Bytes::from_static(b"0123456789abcdef"))
- );
- assert_eq!(
- b"0123456789abcdef",
- cache
- .get("0123456789abcdef")
- .expect("cached bytes")
- .as_ref()
- );
-
- let tight = byte_cache(1_024, 1_024, 60_000);
- assert_eq!(
- ByteCacheInsertOutcome::Rejected(ByteCacheInsertRejection::WeightExceedsCapacity),
- tight.put("key", Bytes::from(vec![0; 1_024]))
- );
- }
-
- #[test]
- fn byte_cache_reports_evictions_through_the_hook() {
- let evictions = Arc::new(AtomicU64::new(0));
- let recorded = Arc::clone(&evictions);
- let cache: ByteCache = ByteCache::new(
- ByteCacheSettings::clamped(8_192, 8_192, 60_000),
- move || {
- recorded.fetch_add(1, Ordering::Relaxed);
- },
- );
- for index in 0..64 {
- let _ = cache.put(format!("entry-{index}"), Bytes::from(vec![0u8; 512]));
- }
- cache.settle();
- assert!(cache.weighted_size() <= 8_192);
- assert!(evictions.load(Ordering::Relaxed) > 0);
- }
-}
diff --git a/fluxer_media_proxy/src/coalescer/mod.rs b/fluxer_media_proxy/src/coalescer/mod.rs
index b2894a967..0dbed61a2 100644
--- a/fluxer_media_proxy/src/coalescer/mod.rs
+++ b/fluxer_media_proxy/src/coalescer/mod.rs
@@ -15,9 +15,6 @@ use std::{
use thiserror::Error;
use tokio::sync::{Notify, OwnedSemaphorePermit, Semaphore, TryAcquireError};
-#[cfg(test)]
-mod tests;
-
const TIMED_OUT_RETRY_AFTER: Duration = Duration::from_secs(60);
#[derive(Clone, Copy, Debug, Error, Eq, PartialEq)]
diff --git a/fluxer_media_proxy/src/component_tests.rs b/fluxer_media_proxy/src/component_tests.rs
index f485a4cb0..e380f0669 100644
--- a/fluxer_media_proxy/src/component_tests.rs
+++ b/fluxer_media_proxy/src/component_tests.rs
@@ -1,59 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
-use crate::{
- media_process::native_runtime::ensure_vips_init,
- native::{
- self, NativeStatus,
- nsfw_frame_output::{NSFWFrameCopyError, NSFWFrameOutput},
- },
- test_fixtures::{adversarial_media_bytes, ffmpeg_gen_media, minimal_gif},
-};
-use libc::{c_double, c_int, c_void, size_t};
-use std::{ffi::CString, ptr};
-
-fn thumbnail_status(
- input: &[u8],
- deadline_monotonic_ms: i64,
-) -> (NativeStatus, *mut native::VipsImage) {
- ensure_vips_init().expect("libvips initialises");
- let mut out = ptr::null_mut();
- let status = NativeStatus::from_code(unsafe {
- native::fluxer_vips_thumbnail_buffer_ex(
- input.as_ptr().cast(),
- input.len(),
- deadline_monotonic_ms,
- &mut out,
- 16,
- 16,
- 1,
- native::THUMB_CROP_NONE,
- 1,
- 1_048_576,
- )
- });
- (status, out)
-}
-
-#[test]
-fn negative_thumbnail_deadline_is_rejected_at_the_native_boundary() {
- let (status, out) = thumbnail_status(&minimal_gif(), -1);
- assert_eq!(status, NativeStatus::CodecFailure);
- assert!(out.is_null());
-}
-
-#[test]
-fn expired_thumbnail_deadline_is_reported_as_deadline_exceeded() {
- let (status, out) = thumbnail_status(&minimal_gif(), 1);
- assert_eq!(status, NativeStatus::DeadlineExceeded);
- assert!(out.is_null());
-}
-
-#[test]
-fn empty_thumbnail_input_is_rejected_before_libvips_runs() {
- let (status, out) = thumbnail_status(&[], 0);
- assert_eq!(status, NativeStatus::CodecFailure);
- assert!(out.is_null());
-}
+use crate::{media_process::native_runtime::ensure_vips_init, native};
+use std::ffi::CString;
#[test]
fn blocked_libvips_loaders_cannot_decode_untrusted_input() {
@@ -66,412 +14,3 @@ fn blocked_libvips_loaders_cannot_decode_untrusted_input() {
assert!(raw.is_null());
unsafe { native::fluxer_vips_error_clear() };
}
-
-fn gif_resize_status(
- input: &[u8],
- deadline_monotonic_ms: i64,
-) -> (NativeStatus, *mut c_void, size_t, size_t) {
- let mut output = ptr::null_mut::<c_void>();
- let mut output_size = usize::MAX;
- let mut output_capacity = usize::MAX;
- let status = NativeStatus::from_code(unsafe {
- native::fluxer_ffmpeg_resize_gif(
- input.as_ptr().cast(),
- input.len(),
- 1,
- 1,
- 1,
- deadline_monotonic_ms,
- 16,
- 16,
- 1_000,
- 1_048_576,
- 1_048_576,
- &mut output,
- &mut output_size,
- &mut output_capacity,
- )
- });
- (status, output, output_size, output_capacity)
-}
-
-#[test]
-fn negative_gif_deadline_is_rejected_at_the_native_boundary() {
- let (status, output, output_size, output_capacity) = gif_resize_status(&minimal_gif(), -1);
- assert_eq!(NativeStatus::CodecFailure, status);
- assert!(output.is_null());
- assert_eq!(0, output_size);
- assert_eq!(0, output_capacity);
-}
-
-fn webp_direct_transform_status(input: &[u8]) -> (NativeStatus, *mut c_void, size_t) {
- let limits = native::WebpAnimLimits {
- max_frames: 16,
- max_duration_ms: 1_000,
- deadline_monotonic_ms: 0,
- };
- let mut output = ptr::null_mut::<c_void>();
- let mut output_size = usize::MAX;
- let status = NativeStatus::from_code(unsafe {
- native::fluxer_webp_transform_animated(
- input.as_ptr().cast(),
- input.len(),
- 16,
- 16,
- 85,
- 0,
- 4,
- 90,
- 1,
- 0,
- 16,
- 1_048_576,
- &limits,
- 1_048_576,
- &mut output,
- &mut output_size,
- )
- });
- (status, output, output_size)
-}
-
-fn apng_probe_status(input: &[u8]) -> (NativeStatus, c_int, c_int, c_int) {
- let mut width = c_int::MAX;
- let mut height = c_int::MAX;
- let mut frames = c_int::MAX;
- let status = NativeStatus::from_code(unsafe {
- native::fluxer_apng_probe(
- input.as_ptr().cast(),
- input.len(),
- 16,
- 1_048_576,
- &mut width,
- &mut height,
- &mut frames,
- )
- });
- (status, width, height, frames)
-}
-
-fn heif_validate_status(input: &[u8]) -> NativeStatus {
- NativeStatus::from_code(unsafe {
- native::fluxer_heif_validate(input.as_ptr().cast(), input.len(), 0)
- })
-}
-
-fn gif_animation_status(input: &[u8]) -> NativeStatus {
- NativeStatus::from_code(unsafe {
- native::fluxer_validate_gif_animation(
- input.as_ptr().cast(),
- input.len(),
- 16,
- 1_000,
- 1_048_576,
- )
- })
-}
-
-fn probe_animated_status(input: &[u8]) -> (NativeStatus, c_int, c_int, c_int, c_int) {
- ensure_vips_init().expect("libvips initialises");
- let mut width = c_int::MAX;
- let mut height = c_int::MAX;
- let mut pages = c_int::MAX;
- let mut bands = c_int::MAX;
- let mut format = c_int::MAX;
- let status = NativeStatus::from_code(unsafe {
- native::fluxer_vips_probe_animated(
- input.as_ptr().cast(),
- input.len(),
- &mut width,
- &mut height,
- &mut pages,
- &mut bands,
- &mut format,
- )
- });
- unsafe { native::fluxer_vips_error_clear() };
- (status, width, height, pages, bands)
-}
-
-fn apng_decode_status(input: &[u8]) -> (NativeStatus, *mut native::VipsImage, u32) {
- let mut out = ptr::null_mut();
- let mut num_plays = u32::MAX;
- let status = NativeStatus::from_code(unsafe {
- native::fluxer_ffmpeg_decode_apng(
- input.as_ptr().cast(),
- input.len(),
- 1,
- 0,
- &mut out,
- 16,
- 1_048_576,
- 1,
- &mut num_plays,
- )
- });
- (status, out, num_plays)
-}
-
-fn bmp_decode_status(input: &[u8]) -> (NativeStatus, *mut native::VipsImage) {
- let mut out = ptr::null_mut();
- let status = NativeStatus::from_code(unsafe {
- native::fluxer_ffmpeg_decode_bmp(
- input.as_ptr().cast(),
- input.len(),
- 1,
- 0,
- &mut out,
- 1_048_576,
- )
- });
- (status, out)
-}
-
-fn heif_still_decode_status(
- input: &[u8],
-) -> (
- NativeStatus,
- *mut native::VipsImage,
- native::FluxerHEIFPrimaryStillDecodeFacts,
-) {
- let mut out = ptr::null_mut();
- let mut facts = native::FluxerHEIFPrimaryStillDecodeFacts {
- hdr_tone_mapped: c_int::MAX,
- hdr_gain_map_detected: c_int::MAX,
- };
- let status = NativeStatus::from_code(unsafe {
- native::fluxer_heif_decode_primary_still(
- input.as_ptr().cast(),
- input.len(),
- 0,
- &mut out,
- 1_048_576,
- 4_096,
- &mut facts,
- )
- });
- (status, out, facts)
-}
-
-fn webp_nsfw_extract_status(input: &[u8]) -> NativeStatus {
- let indices: [c_int; 1] = [0];
- let mut frames = NSFWFrameOutput::new(1);
- let status = NativeStatus::from_code(unsafe {
- native::fluxer_webp_extract_frames_for_nsfw(
- input.as_ptr().cast(),
- input.len(),
- 1,
- 0,
- indices.as_ptr(),
- indices.len(),
- 16,
- 1_048_576,
- 1_048_576,
- frames.as_mut_ptr(),
- )
- });
- assert_eq!(
- Err(NSFWFrameCopyError::InvalidOutput),
- frames.copy_frames(),
- "input {input:?}"
- );
- status
-}
-
-#[test]
-fn every_remaining_native_validator_zeroes_its_out_params_on_failure() {
- let valid_gif = minimal_gif();
- for bytes in adversarial_media_bytes() {
- let decodable = bytes == valid_gif;
-
- assert_eq!(
- decodable,
- gif_animation_status(&bytes) == NativeStatus::Ok,
- "input {bytes:?}"
- );
-
- let (status, width, height, pages, bands) = probe_animated_status(&bytes);
- assert_eq!(decodable, status == NativeStatus::Ok, "input {bytes:?}");
- if decodable {
- assert_eq!(1, width, "input {bytes:?}");
- assert_eq!(1, height, "input {bytes:?}");
- assert_eq!(1, pages, "input {bytes:?}");
- assert!(bands > 0, "input {bytes:?}");
- } else {
- assert_eq!(0, width, "input {bytes:?}");
- assert_eq!(0, height, "input {bytes:?}");
- assert_eq!(0, pages, "input {bytes:?}");
- assert_eq!(0, bands, "input {bytes:?}");
- }
-
- let (status, out, num_plays) = apng_decode_status(&bytes);
- assert_ne!(NativeStatus::Ok, status, "input {bytes:?}");
- assert!(out.is_null(), "input {bytes:?}");
- assert_eq!(0, num_plays, "input {bytes:?}");
-
- let (status, out) = bmp_decode_status(&bytes);
- assert_ne!(NativeStatus::Ok, status, "input {bytes:?}");
- assert!(out.is_null(), "input {bytes:?}");
-
- let (status, out, facts) = heif_still_decode_status(&bytes);
- assert_ne!(NativeStatus::Ok, status, "input {bytes:?}");
- assert!(out.is_null(), "input {bytes:?}");
- assert_eq!(0, facts.hdr_tone_mapped, "input {bytes:?}");
- assert_eq!(0, facts.hdr_gain_map_detected, "input {bytes:?}");
-
- assert_ne!(
- NativeStatus::Ok,
- webp_nsfw_extract_status(&bytes),
- "input {bytes:?}"
- );
- }
-}
-
-fn nsfw_extract_statuses(input: &[u8], deadline_monotonic_ms: i64) -> [NativeStatus; 3] {
- let indices: [c_int; 1] = [0];
- let timestamps: [c_double; 1] = [0.0];
- let mut apng_frames = NSFWFrameOutput::new(1);
- let apng = NativeStatus::from_code(unsafe {
- native::fluxer_ffmpeg_extract_apng_frames_for_nsfw(
- input.as_ptr().cast(),
- input.len(),
- 1,
- deadline_monotonic_ms,
- indices.as_ptr(),
- indices.len(),
- 16,
- 1_048_576,
- 1_048_576,
- apng_frames.as_mut_ptr(),
- )
- });
- assert_eq!(
- Err(NSFWFrameCopyError::InvalidOutput),
- apng_frames.copy_frames()
- );
- let mut gif_frames = NSFWFrameOutput::new(1);
- let gif = NativeStatus::from_code(unsafe {
- native::fluxer_ffmpeg_extract_gif_frames_for_nsfw(
- input.as_ptr().cast(),
- input.len(),
- 1,
- deadline_monotonic_ms,
- indices.as_ptr(),
- indices.len(),
- 16,
- 1_048_576,
- 1_048_576,
- gif_frames.as_mut_ptr(),
- )
- });
- assert_eq!(
- Err(NSFWFrameCopyError::InvalidOutput),
- gif_frames.copy_frames()
- );
- let mut av_frames = NSFWFrameOutput::new(1);
- let av = NativeStatus::from_code(unsafe {
- native::fluxer_av_extract_frames_for_nsfw(
- input.as_ptr().cast(),
- input.len(),
- 1,
- deadline_monotonic_ms,
- timestamps.as_ptr(),
- timestamps.len(),
- 1_048_576,
- av_frames.as_mut_ptr(),
- )
- });
- assert_eq!(
- Err(NSFWFrameCopyError::InvalidOutput),
- av_frames.copy_frames()
- );
- [apng, gif, av]
-}
-
-#[test]
-fn native_validation_rejects_adversarial_containers_without_allocating() {
- for bytes in adversarial_media_bytes() {
- let (status, output, output_size, output_capacity) = gif_resize_status(&bytes, -1);
- assert_eq!(NativeStatus::CodecFailure, status, "input {bytes:?}");
- assert!(output.is_null(), "input {bytes:?}");
- assert_eq!(0, output_size, "input {bytes:?}");
- assert_eq!(0, output_capacity, "input {bytes:?}");
-
- let (status, width, height, frames) = apng_probe_status(&bytes);
- assert_ne!(NativeStatus::Ok, status, "input {bytes:?}");
- assert_eq!(0, width, "input {bytes:?}");
- assert_eq!(0, height, "input {bytes:?}");
- assert_eq!(0, frames, "input {bytes:?}");
-
- let (status, output, output_size) = webp_direct_transform_status(&bytes);
- assert_ne!(NativeStatus::Ok, status, "input {bytes:?}");
- assert!(output.is_null(), "input {bytes:?}");
- assert_eq!(0, output_size, "input {bytes:?}");
-
- assert_eq!(
- NativeStatus::CodecFailure,
- heif_validate_status(&bytes),
- "input {bytes:?}"
- );
-
- for status in nsfw_extract_statuses(&bytes, -1) {
- assert_eq!(NativeStatus::CodecFailure, status, "input {bytes:?}");
- }
- }
-}
-
-fn matroska_video(color_args: &[&str]) -> Option<Vec<u8>> {
- let mut args = vec![
- "-f",
- "lavfi",
- "-i",
- "testsrc=size=64x64:rate=10:duration=1",
- "-pix_fmt",
- "yuv420p",
- "-c:v",
- "mpeg4",
- ];
- args.extend_from_slice(color_args);
- args.extend_from_slice(&["-f", "matroska"]);
- ffmpeg_gen_media("fixture.mkv", &args)
-}
-
-fn av_nsfw_extract(
- input: &[u8],
- timestamps: &[c_double],
-) -> (NativeStatus, Result<Vec<Vec<u8>>, NSFWFrameCopyError>) {
- ensure_vips_init().expect("libvips initialises");
- let mut frames = NSFWFrameOutput::new(timestamps.len());
- let status = NativeStatus::from_code(unsafe {
- native::fluxer_av_extract_frames_for_nsfw(
- input.as_ptr().cast(),
- input.len(),
- 1,
- 0,
- timestamps.as_ptr(),
- timestamps.len(),
- 1_048_576,
- frames.as_mut_ptr(),
- )
- });
- let copied = frames.copy_frames();
- (status, copied)
-}
-
-#[test]
-fn nsfw_frame_emit_failure_is_reported_instead_of_decoder_eof() {
- let Some(sdr) = matroska_video(&[]) else {
- eprintln!("skipping: ffmpeg is unavailable");
- return;
- };
- let timestamps: [c_double; 3] = [0.0, 0.4, 0.8];
- let (sdr_status, sdr_frames) = av_nsfw_extract(&sdr, &timestamps);
- assert_eq!(NativeStatus::Ok, sdr_status);
- assert_eq!(3, sdr_frames.expect("sdr frames").len());
-
- let rejected =
- matroska_video(&["-vf", "setparams=color_trc=smpte428"]).expect("ffmpeg tags the transfer");
- let (status, frames) = av_nsfw_extract(&rejected, &timestamps);
- assert_eq!(NativeStatus::Unsupported, status);
- assert_eq!(Err(NSFWFrameCopyError::InvalidOutput), frames);
-}
diff --git a/fluxer_media_proxy/src/disposition.rs b/fluxer_media_proxy/src/disposition.rs
index 68767db9a..fe72879a8 100644
--- a/fluxer_media_proxy/src/disposition.rs
+++ b/fluxer_media_proxy/src/disposition.rs
@@ -164,137 +164,3 @@ pub fn header(
.expect("sanitised content disposition must be a valid header value"),
))
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- fn header_string(decision: Decision, filename: Option<&str>) -> String {
- header(decision, filename)
- .expect("disposition header")
- .into_header_value()
- .to_str()
- .expect("disposition header is ascii")
- .to_owned()
- }
-
- #[test]
- fn inline_vs_attachment_based_on_mime() {
- assert_eq!(Decision::Inline, decide("image/png", false));
- assert_eq!(
- Decision::Inline,
- decide("image/jpeg; charset=binary", false)
- );
- assert_eq!(Decision::Inline, decide("video/mp4", false));
- assert_eq!(Decision::Attachment, decide("application/pdf", false));
- assert_eq!(Decision::Attachment, decide("image/svg+xml", false));
- assert_eq!(
- Decision::Attachment,
- decide("application/octet-stream", false)
- );
- assert_eq!(Decision::Attachment, decide("text/html", false));
- assert_eq!(
- Decision::Attachment,
- decide("application/x-msdownload", false)
- );
- }
-
- #[test]
- fn explicit_download_forces_attachment() {
- assert_eq!(Decision::Attachment, decide("image/png", true));
- assert_eq!(Decision::Attachment, decide("video/mp4", true));
- }
-
- #[test]
- fn case_insensitive_mime_matching() {
- assert_eq!(Decision::Inline, decide("IMAGE/PNG", false));
- assert_eq!(Decision::Attachment, decide("Image/Svg+Xml", false));
- }
-
- #[test]
- fn a_multibyte_boundary_inside_the_mime_prefix_is_not_a_panic() {
- assert_eq!(Decision::Attachment, decide("XX#-X\u{605}", false));
- assert!(!is_inline_viewable("XX#-X\u{605}"));
- assert!(!is_inline_viewable("\u{1f600}\u{1f600}"));
- assert!(!is_inline_viewable("imag\u{e9}/png"));
- for input in crate::test_fixtures::ADVERSARIAL_TEXT_INPUTS {
- let _ = decide(input, false);
- let _ = decide(input, true);
- }
- }
-
- #[test]
- fn format_header_ascii_filename_quoted() {
- assert_eq!(
- "attachment; filename=\"report.pdf\"",
- header_string(Decision::Attachment, Some("report.pdf"))
- );
- }
-
- #[test]
- fn format_header_inline_no_filename() {
- assert_eq!("inline", header_string(Decision::Inline, None));
- assert_eq!("inline", header_string(Decision::Inline, Some("")));
- }
-
- #[test]
- fn format_header_non_ascii_filename_uses_rfc5987_ext_form() {
- let out = header_string(Decision::Attachment, Some("naïve résumé.pdf"));
- assert!(out.contains("filename*=UTF-8''"));
- assert!(out.contains("%C3%A9"));
- assert!(out.contains("filename=\""));
- }
-
- #[test]
- fn format_header_strips_embedded_quote_and_backslash() {
- let out = header_string(Decision::Attachment, Some("evil\"name\\.txt"));
- assert!(!out.contains("evil\""));
- assert!(!out.contains('\\'));
- assert!(out.contains("filename*=UTF-8''"));
- assert!(out.contains("%22"));
- assert!(out.contains("%5C"));
- }
-
- #[test]
- fn sanitized_fallback_filename_replaces_path_separators() {
- assert_eq!(
- "attachment; filename=\".._.._x.png\"; filename*=UTF-8''..%2F..%2Fx.png",
- header_string(Decision::Attachment, Some("../../x.png"))
- );
- }
-
- #[test]
- fn filename_bound_leaves_room_for_the_worst_case_expansion() {
- assert_eq!(2035, CONTENT_DISPOSITION_FILENAME_BYTES_MAX);
- let widest = "\u{7f}".repeat(CONTENT_DISPOSITION_FILENAME_BYTES_MAX);
- assert!(header(Decision::Attachment, Some(&widest)).is_ok());
- }
-
- #[test]
- fn oversized_filenames_are_truncated_instead_of_dropped() {
- let oversized = "\u{7f}".repeat(PUBLIC_MEDIA_MAX_HEADER_VALUE_BYTES);
- let value = header_string(Decision::Attachment, Some(&oversized));
- assert!(value.starts_with("attachment; filename=\""));
- assert!(value.len() <= PUBLIC_MEDIA_MAX_HEADER_VALUE_BYTES);
- }
-
- #[test]
- fn long_multi_byte_filenames_keep_a_bounded_header_for_both_dispositions() {
- let filename = "\u{e9}".repeat(1100);
- for (decision, expected_prefix) in [
- (Decision::Inline, "inline; filename=\""),
- (Decision::Attachment, "attachment; filename=\""),
- ] {
- let value = header_string(decision, Some(&filename));
- assert!(value.starts_with(expected_prefix), "{value}");
- assert!(value.len() <= PUBLIC_MEDIA_MAX_HEADER_VALUE_BYTES);
- let encoded = value
- .split_once("filename*=UTF-8''")
- .expect("percent encoded filename")
- .1;
- assert_eq!(0, encoded.len() % 3);
- assert!(encoded.ends_with("%A9"));
- assert!(encoded.len() < filename.len() * 3);
- }
- }
-}
diff --git a/fluxer_media_proxy/src/http_client/retry.rs b/fluxer_media_proxy/src/http_client/retry.rs
index 4a9a9147d..edaa23ef0 100644
--- a/fluxer_media_proxy/src/http_client/retry.rs
+++ b/fluxer_media_proxy/src/http_client/retry.rs
@@ -146,239 +146,3 @@ pub(crate) fn is_dropped_connection(error: &(dyn std::error::Error + 'static)) -
}
false
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use crate::metrics::Metrics;
- use std::time::Duration;
- use tokio::io::AsyncReadExt as _;
-
- fn response_with_status(status: StatusCode) -> Result<reqwest::Response, MiddlewareError> {
- let response = http::Response::builder()
- .status(status)
- .body("")
- .expect("building a synthetic response cannot fail");
- Ok(reqwest::Response::from(response))
- }
-
- fn counter_line(rendered: &str, name: &str) -> String {
- rendered
- .lines()
- .find(|line| line.starts_with(name) && !line.starts_with('#'))
- .unwrap_or_else(|| panic!("{name} is missing from the rendered metrics"))
- .to_owned()
- }
-
- #[test]
- fn retry_strategy_retries_only_explicit_transient_statuses() {
- assert!(matches!(
- retryable_status(StatusCode::REQUEST_TIMEOUT),
- Some(Retryable::Transient)
- ));
- assert!(matches!(
- retryable_status(StatusCode::TOO_MANY_REQUESTS),
- Some(Retryable::Transient)
- ));
- assert!(matches!(
- retryable_status(StatusCode::INTERNAL_SERVER_ERROR),
- Some(Retryable::Transient)
- ));
- assert!(matches!(
- retryable_status(StatusCode::BAD_GATEWAY),
- Some(Retryable::Transient)
- ));
- assert!(matches!(
- retryable_status(StatusCode::SERVICE_UNAVAILABLE),
- Some(Retryable::Transient)
- ));
- assert!(matches!(
- retryable_status(StatusCode::GATEWAY_TIMEOUT),
- Some(Retryable::Transient)
- ));
- assert!(matches!(
- retryable_status(StatusCode::NOT_FOUND),
- Some(Retryable::Fatal)
- ));
- assert!(matches!(
- retryable_status(StatusCode::NOT_IMPLEMENTED),
- Some(Retryable::Fatal)
- ));
- assert!(retryable_status(StatusCode::OK).is_none());
- assert!(retryable_status(StatusCode::FOUND).is_none());
- }
-
- #[test]
- fn retryable_status_ignores_success_and_redirect_statuses() {
- assert!(retryable_status(StatusCode::OK).is_none());
- assert!(retryable_status(StatusCode::FOUND).is_none());
- }
-
- #[test]
- fn server_errors_outside_the_whitelist_stay_fatal() {
- for status in [
- StatusCode::NOT_IMPLEMENTED,
- StatusCode::HTTP_VERSION_NOT_SUPPORTED,
- StatusCode::INSUFFICIENT_STORAGE,
- StatusCode::LOOP_DETECTED,
- StatusCode::NETWORK_AUTHENTICATION_REQUIRED,
- ] {
- assert!(
- matches!(retryable_status(status), Some(Retryable::Fatal)),
- "{status} must not be retried"
- );
- }
- }
-
- #[test]
- fn middleware_failures_are_fatal_and_are_not_counted_as_transient() {
- let metrics = Metrics::new();
- let strategy = MediaProxyRetryStrategy::new(metrics.http_client());
- let error = Err(MiddlewareError::Middleware(anyhow::anyhow!(
- "middleware refused the request"
- )));
- assert!(matches!(strategy.handle(&error), Some(Retryable::Fatal)));
- let rendered = metrics.render();
- assert_eq!(
- counter_line(
- &rendered,
- "fluxer_media_proxy_http_retryable_classifications_total{reason=\"error\"}"
- ),
- "fluxer_media_proxy_http_retryable_classifications_total{reason=\"error\"} 0"
- );
- }
-
- #[test]
- fn transient_status_classifications_are_counted_once_and_fatal_ones_are_not() {
- let metrics = Metrics::new();
- let strategy = MediaProxyRetryStrategy::new(metrics.http_client());
- assert!(matches!(
- strategy.handle(&response_with_status(StatusCode::SERVICE_UNAVAILABLE)),
- Some(Retryable::Transient)
- ));
- assert!(matches!(
- strategy.handle(&response_with_status(StatusCode::NOT_FOUND)),
- Some(Retryable::Fatal)
- ));
- assert!(
- strategy
- .handle(&response_with_status(StatusCode::OK))
- .is_none()
- );
- let rendered = metrics.render();
- assert_eq!(
- counter_line(
- &rendered,
- "fluxer_media_proxy_http_retryable_classifications_total{reason=\"status\"}"
- ),
- "fluxer_media_proxy_http_retryable_classifications_total{reason=\"status\"} 1"
- );
- }
-
- #[test]
- fn every_retry_decision_reaches_the_injected_metrics_handle() {
- let metrics = Metrics::new();
- let backoff = ExponentialBackoff::builder()
- .retry_bounds(Duration::from_millis(1), Duration::from_millis(2))
- .build_with_max_retries(1);
- let policy = ObservableRetryPolicy::new(backoff, None, metrics.http_client());
- let started = SystemTime::now();
- assert!(matches!(
- policy.should_retry(started, 0),
- RetryDecision::Retry { .. }
- ));
- assert!(matches!(
- policy.should_retry(started, 1),
- RetryDecision::DoNotRetry
- ));
- let rendered = metrics.render();
- assert_eq!(
- counter_line(&rendered, "fluxer_media_proxy_http_retries_total"),
- "fluxer_media_proxy_http_retries_total 1"
- );
- assert_eq!(
- counter_line(&rendered, "fluxer_media_proxy_http_retries_exhausted_total"),
- "fluxer_media_proxy_http_retries_exhausted_total 1"
- );
- }
-
- #[derive(Debug, thiserror::Error)]
- #[error("request failed")]
- struct WrappedTransportError(#[source] std::io::Error);
-
- #[test]
- fn a_dropped_connection_is_recognised_anywhere_in_the_error_chain() {
- for kind in [
- ErrorKind::ConnectionReset,
- ErrorKind::ConnectionAborted,
- ErrorKind::BrokenPipe,
- ErrorKind::UnexpectedEof,
- ] {
- assert!(
- is_dropped_connection(&WrappedTransportError(std::io::Error::from(kind))),
- "{kind:?} must count as a dropped connection"
- );
- }
- assert!(!is_dropped_connection(&WrappedTransportError(
- std::io::Error::from(ErrorKind::PermissionDenied)
- )));
- }
-
- #[tokio::test]
- async fn a_connection_closed_before_the_response_arrives_is_transient() {
- let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
- .await
- .expect("a loopback listener binds");
- let address = listener
- .local_addr()
- .expect("the listener has a local address");
- tokio::spawn(async move {
- let (mut socket, _) = listener.accept().await.expect("the client connects");
- let mut request = [0u8; 4096];
- let _ = socket.read(&mut request).await;
- });
- let error = crate::http_client::build_raw_default()
- .get(format!("http://{address}/"))
- .send()
- .await
- .expect_err("the server closes the connection without answering");
- assert!(matches!(
- retryable_error(&MiddlewareError::Reqwest(error)),
- Some(Retryable::Transient)
- ));
- }
-
- fn policy(window: Option<Duration>) -> ObservableRetryPolicy {
- ObservableRetryPolicy::new(
- ExponentialBackoff::builder().build_with_max_retries(2),
- window,
- Arc::new(HTTPClientMetrics::default()),
- )
- }
-
- #[test]
- fn a_request_past_its_retry_window_is_not_retried() {
- let started = SystemTime::now() - Duration::from_secs(20);
- assert!(matches!(
- policy(Some(Duration::from_secs(15))).should_retry(started, 0),
- RetryDecision::DoNotRetry
- ));
- }
-
- #[test]
- fn a_request_inside_its_retry_window_is_retried() {
- assert!(matches!(
- policy(Some(Duration::from_secs(15))).should_retry(SystemTime::now(), 0),
- RetryDecision::Retry { .. }
- ));
- }
-
- #[test]
- fn a_policy_without_a_window_keeps_retrying_slow_requests() {
- let started = SystemTime::now() - Duration::from_secs(60);
- assert!(matches!(
- policy(None).should_retry(started, 0),
- RetryDecision::Retry { .. }
- ));
- }
-}
diff --git a/fluxer_media_proxy/src/policy_tests/adversarial.rs b/fluxer_media_proxy/src/policy_tests/adversarial.rs
index 70070b671..8251daf50 100644
--- a/fluxer_media_proxy/src/policy_tests/adversarial.rs
+++ b/fluxer_media_proxy/src/policy_tests/adversarial.rs
@@ -1,46 +1,10 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
use crate::{
- asset_hash::{self, AssetHash},
disposition::{self, Decision, PUBLIC_MEDIA_MAX_HEADER_VALUE_BYTES},
test_fixtures::ADVERSARIAL_TEXT_INPUTS,
- upload_relay::token::token_from_query,
};
-const RELAY_TOKEN_BYTES_MAX: usize = 16 * 1024;
-
-#[test]
-fn adversarial_text_never_escapes_the_asset_hash_digest_contract() {
- for text in ADVERSARIAL_TEXT_INPUTS {
- let parsed = AssetHash::parse(text);
- assert!(
- text.ends_with(parsed.digest()),
- "the digest is not a suffix of {text}"
- );
- assert_eq!(
- parsed.is_animated(),
- text.starts_with("a_"),
- "the animation flag disagrees with {text}"
- );
- assert_eq!(
- text.len() - parsed.digest().len(),
- if parsed.is_animated() { 2 } else { 0 },
- "the digest dropped bytes from {text}"
- );
- assert_eq!(parsed.digest(), asset_hash::strip_animation_prefix(text));
- }
-}
-
-#[test]
-fn adversarial_query_text_yields_at_most_one_relay_token() {
- for text in ADVERSARIAL_TEXT_INPUTS {
- if let Ok(token) = token_from_query(Some(text)) {
- assert!(!token.is_empty(), "an empty token was accepted from {text}");
- assert!(token.len() <= RELAY_TOKEN_BYTES_MAX);
- }
- }
-}
-
#[test]
fn adversarial_filenames_never_break_the_content_disposition_header() {
for text in ADVERSARIAL_TEXT_INPUTS {
diff --git a/fluxer_media_proxy/src/policy_tests/headers.rs b/fluxer_media_proxy/src/policy_tests/headers.rs
deleted file mode 100644
index e96c6b5be..000000000
--- a/fluxer_media_proxy/src/policy_tests/headers.rs
+++ /dev/null
@@ -1,34 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-or-later
-
-use crate::{http_headers, range::ByteRange};
-use http::{HeaderMap, header};
-
-#[test]
-fn public_media_headers_never_negotiate_a_conditional_validator() {
- let mut responses = Vec::new();
- for content_type in ["image/png", "video/mp4", "audio/mpeg", "image/svg+xml"] {
- for byte_range in [None, Some(ByteRange { start: 10, end: 19 })] {
- let mut headers = HeaderMap::new();
- http_headers::add_media_headers(&mut headers, 100, content_type, byte_range);
- responses.push(headers);
- }
- }
- let mut unsatisfiable = HeaderMap::new();
- http_headers::add_unsatisfiable_headers(&mut unsatisfiable, 4096);
- responses.push(unsatisfiable);
- let mut security_only = HeaderMap::new();
- http_headers::add_security_headers(&mut security_only);
- responses.push(security_only);
-
- for headers in &responses {
- for negotiated in ["etag", "if-none-match", "if-modified-since", "age"] {
- assert!(
- headers.get(negotiated).is_none(),
- "{negotiated} was negotiated on a public media response"
- );
- }
- if let Some(vary) = headers.get(header::VARY) {
- assert_eq!(vary, "Accept-Encoding");
- }
- }
-}
diff --git a/fluxer_media_proxy/src/policy_tests/mod.rs b/fluxer_media_proxy/src/policy_tests/mod.rs
index 30b5379b4..699250438 100644
--- a/fluxer_media_proxy/src/policy_tests/mod.rs
+++ b/fluxer_media_proxy/src/policy_tests/mod.rs
@@ -1,5 +1,3 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
mod adversarial;
-mod headers;
-mod parsers;
diff --git a/fluxer_media_proxy/src/policy_tests/parsers.rs b/fluxer_media_proxy/src/policy_tests/parsers.rs
deleted file mode 100644
index e3f7967d6..000000000
--- a/fluxer_media_proxy/src/policy_tests/parsers.rs
+++ /dev/null
@@ -1,45 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-or-later
-
-use crate::{
- constants::{DEFAULT_IMAGE_SIZE, parse_image_size},
- image_quality::ImageQuality,
- image_transform::EncodeEffort,
-};
-
-#[test]
-fn malformed_query_values_fall_back_instead_of_failing_the_request() {
- for raw in ["", "-1", "1e3", "99999999999", "128px", " 128"] {
- assert_eq!(
- parse_image_size(Some(raw)),
- DEFAULT_IMAGE_SIZE,
- "size {raw} did not fall back"
- );
- }
- // A value that parses is snapped up the ladder, never dropped to the default: the old era
- // pinned this so an off-ladder request can never be served fewer pixels than it asked for.
- assert_eq!(parse_image_size(Some("0")), 16);
- assert_eq!(parse_image_size(Some("777")), 1024);
- assert_eq!(parse_image_size(Some("256")), 256);
- assert_eq!(parse_image_size(None), DEFAULT_IMAGE_SIZE);
-
- for raw in ["", " ", "LOW", "lossy", "%FF", "high "] {
- assert_eq!(
- ImageQuality::parse_lenient(raw),
- ImageQuality::High,
- "quality {raw} did not fall back"
- );
- }
- assert_eq!(ImageQuality::parse_lenient("low"), ImageQuality::Low);
-
- for raw in ["", "-1", "nine", "10.5", "256"] {
- assert_eq!(
- EncodeEffort::parse_lenient(raw),
- None,
- "effort {raw} was accepted"
- );
- }
- assert_eq!(
- EncodeEffort::parse_lenient("250").map(EncodeEffort::get),
- Some(9)
- );
-}
diff --git a/fluxer_media_proxy/src/public_net_policy/tests.rs b/fluxer_media_proxy/src/public_net_policy/tests.rs
index 0925448c8..4241cf79d 100644
--- a/fluxer_media_proxy/src/public_net_policy/tests.rs
+++ b/fluxer_media_proxy/src/public_net_policy/tests.rs
@@ -24,28 +24,6 @@ fn blocks_private_and_special_ip_literals() {
assert!(public("2606:4700:4700::1111"));
}
-#[test]
-fn validates_public_host_syntax() {
- assert!(is_valid_public_hostname("example.com"));
- assert!(is_valid_public_hostname("xn--bcher-kva.example"));
- assert!(!is_valid_public_hostname("localhost"));
- assert!(!is_valid_public_hostname("example"));
- assert!(!is_valid_public_hostname("bad_name.example"));
- assert!(!is_valid_public_hostname("example.123"));
-}
-
-#[test]
-fn resolves_relative_redirects() {
- assert_eq!(
- "https://example.com/a/d?y=2",
- resolve_redirect("https://example.com/a/b/c?x=1", "../d?y=2#ignored").unwrap()
- );
- assert_eq!(
- "https://example.com/z",
- resolve_redirect("https://example.com/a/b/c", "/z").unwrap()
- );
-}
-
#[test]
fn blocks_every_documented_ipv4_ssrf_range() {
for ip in [
@@ -105,34 +83,12 @@ fn rejects_non_http_s_schemes() {
);
}
-#[test]
-fn redirect_that_returns_to_same_url_is_allowed() {
- assert_eq!(
- "https://example.com/path",
- resolve_redirect("https://example.com/path", "/path").unwrap()
- );
-}
-
#[test]
fn redirect_with_dot_dot_cannot_escape_host() {
let r = resolve_redirect("https://example.com/a", "../../../etc").unwrap();
assert!(r.starts_with("https://example.com/"));
}
-#[test]
-fn logged_urls_keep_only_scheme_host_and_port() {
- assert_eq!(
- "https://cdn.example.com/[redacted]",
- external_url_for_log("https://agent:[email protected]/rooms/private?token=abc#f")
- );
- assert_eq!(
- "http://cdn.example.com:8080/[redacted]",
- external_url_for_log("http://cdn.example.com:8080/a/b/c")
- );
- assert_eq!("[invalid-url]", external_url_for_log("not a url"));
- assert_eq!("[invalid-url]", external_url_for_log(""));
-}
-
#[test]
fn ipv6_outside_global_unicast_is_blocked_by_default() {
for literal in [
@@ -162,16 +118,6 @@ fn ipv6_outside_global_unicast_is_blocked_by_default() {
}
}
-#[test]
-fn globally_reachable_special_use_addresses_stay_reachable() {
- for literal in ["192.0.0.9", "192.0.0.10", "2001:1::1", "2001:20::1"] {
- assert!(public(literal), "{literal}");
- }
- for literal in ["192.0.0.8", "192.0.0.11", "2001:1::4", "2001:40::1"] {
- assert!(!public(literal), "{literal}");
- }
-}
-
#[test]
fn ipv4_written_in_ipv6_notation_is_revalidated_against_the_v4_table() {
for literal in [
@@ -208,35 +154,6 @@ fn public_urls_are_restricted_to_the_standard_web_ports() {
}
}
-#[test]
-fn urls_with_a_fragment_are_rejected() {
- assert_eq!(
- Err(Error::BlockedUrl),
- validate_url("https://example.com/a#section")
- );
- assert_eq!(
- Err(Error::BlockedUrl),
- validate_url("https://example.com#section")
- );
- assert_eq!(Ok(()), validate_url("https://example.com/a"));
- let next = resolve_redirect("https://example.com/a/b", "/next#anchor").unwrap();
- assert_eq!("https://example.com/next", next);
- assert_eq!(Ok(()), validate_url(&next));
-}
-
-#[test]
-fn url_validation_accepts_public_syntax_without_resolving_dns() {
- assert_eq!(
- Ok(()),
- validate_url("https://this-host-does-not-exist.invalid/a")
- );
- assert_eq!(
- Err(Error::BlockedUrl),
- validate_url("https://169.254.169.254/latest/meta-data/")
- );
- assert_eq!(Err(Error::BlockedUrl), validate_url("http://localhost/a"));
-}
-
#[test]
fn resolved_address_sets_are_bounded_and_fail_closed() {
assert_eq!(
@@ -261,17 +178,6 @@ fn resolved_address_sets_are_bounded_and_fail_closed() {
);
}
-#[test]
-fn a_resolver_rejection_is_recognisable_however_deeply_the_transport_wraps_it() {
- assert!(is_pinned_dns_failure(&ResolveError::BlockedAddress));
- assert!(is_pinned_dns_failure(&ResolveError::LookupFailed));
- let wrapped = anyhow::Error::new(ResolveError::HostResolvedToNoAddress).context("dns error");
- assert!(is_pinned_dns_failure(wrapped.as_ref()));
- assert!(!is_pinned_dns_failure(&std::io::Error::other(
- "tcp connect error"
- )));
-}
-
#[test]
fn url_validation_uses_the_canonical_ipv4_host() {
for url in [
diff --git a/fluxer_media_proxy/src/range.rs b/fluxer_media_proxy/src/range.rs
index b9b91c91a..f5bbe3b74 100644
--- a/fluxer_media_proxy/src/range.rs
+++ b/fluxer_media_proxy/src/range.rs
@@ -100,265 +100,3 @@ pub fn parse_content_range(header: Option<&str>) -> Option<ContentRange> {
size: Some(size),
})
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use crate::test_fixtures::ADVERSARIAL_RANGE_HEADERS;
-
- #[test]
- fn range_parser() {
- assert_eq!(
- RangeSelection::Partial(ByteRange { start: 0, end: 9 }),
- parse_range(Some("bytes=0-9"), 100)
- );
- assert_eq!(
- RangeSelection::Partial(ByteRange { start: 95, end: 99 }),
- parse_range(Some("bytes=-5"), 100)
- );
- assert_eq!(
- RangeSelection::Unsatisfiable,
- parse_range(Some("bytes=100-200"), 100)
- );
- }
-
- #[test]
- fn open_ended_range() {
- assert_eq!(
- RangeSelection::Partial(ByteRange { start: 50, end: 99 }),
- parse_range(Some("bytes=50-"), 100)
- );
- }
-
- #[test]
- fn suffix_larger_than_file_clamps_to_whole_file() {
- assert_eq!(
- RangeSelection::Partial(ByteRange { start: 0, end: 99 }),
- parse_range(Some("bytes=-9999"), 100)
- );
- }
-
- #[test]
- fn a_malformed_spec_is_ignored_even_against_a_zero_length_object() {
- // RFC 9110 s14.2: an unparsable range-spec is ignored and the full resource is served.
- // The zero-length shortcut must not pre-empt that check. The old era reached the same
- // answer on GET by classifying the spec before it ever consulted the length; only its
- // HEAD branch went through this parser and reported 416, which is the bug being closed.
- for raw in [
- "bytes=abc-def",
- "bytes=--",
- "bytes=1-2-3",
- "bytes=x-y",
- "bytes=-abc",
- ] {
- assert_eq!(
- RangeSelection::Full,
- parse_range(Some(raw), 0),
- "{raw} against an empty object must be ignored, not 416"
- );
- assert_eq!(
- RangeSelection::Full,
- parse_range(Some(raw), 100),
- "{raw} against a sized object must be ignored"
- );
- }
- // A well-formed spec against an empty object stays unsatisfiable.
- for raw in ["bytes=0-9", "bytes=5-", "bytes=-5", "bytes=10-5"] {
- assert_eq!(
- RangeSelection::Unsatisfiable,
- parse_range(Some(raw), 0),
- "{raw} against an empty object must stay unsatisfiable"
- );
- }
- }
-
- #[test]
- fn missing_or_malformed_range_falls_through_to_no_range() {
- assert_eq!(RangeSelection::Full, parse_range(None, 100));
- assert_eq!(RangeSelection::Full, parse_range(Some("rows=0-9"), 100));
- assert_eq!(RangeSelection::Full, parse_range(Some("bytes="), 100));
- assert_eq!(
- RangeSelection::Full,
- parse_range(Some("bytes=abc-def"), 100)
- );
- assert_eq!(RangeSelection::Full, parse_range(Some("bytes=1"), 100));
- assert_eq!(RangeSelection::Full, parse_range(Some("bytes= x-y"), 100));
- }
-
- #[test]
- fn end_past_eof_clamps() {
- assert_eq!(
- RangeSelection::Partial(ByteRange { start: 0, end: 99 }),
- parse_range(Some("bytes=0-9999"), 100)
- );
- }
-
- #[test]
- fn a_request_range_unit_is_matched_exactly_but_a_response_unit_is_not() {
- // Every request-side parser has to agree with classify_request_range, which forwards
- // only a lowercase "bytes=" spec. A HEAD that read an uppercase unit as a range would
- // report 206 and a partial length for an object its own GET serves whole.
- assert_eq!(RangeSelection::Full, parse_range(Some("BYTES=0-1"), 100));
- assert_eq!(RangeSelection::Full, parse_range(Some("Bytes=0-1"), 100));
- assert_eq!(RangeSelection::Full, parse_range(Some("items=0-1"), 100));
- assert_eq!(
- RequestRange::Absent,
- classify_request_range(Some("BYTES=0-1"))
- );
- // The response side reads the object store's own reply rather than client input, so a
- // lenient unit there only rescues a Content-Range the strict match would drop.
- assert_eq!(
- Some(ContentRange {
- start: 0,
- end: 1,
- size: Some(2)
- }),
- parse_content_range(Some("Bytes 0-1/2"))
- );
- assert_eq!(None, parse_content_range(Some("items 0-1/2")));
- }
-
- #[test]
- fn only_ascii_digits_are_accepted_as_range_offsets() {
- // A deliberate divergence from origin/main, which parsed offsets with usize::from_str
- // and so accepted the leading sign that RFC 9110's first-pos = 1*DIGIT forbids. The old
- // era already served "bytes=+5-" whole on GET, because the spec reached the object store
- // verbatim and was ignored there, and only its HEAD branch answered 206. The strict
- // parse is what makes the two agree.
- assert_eq!(None, parse_decimal_usize(""));
- assert_eq!(None, parse_decimal_usize("+5"));
- assert_eq!(None, parse_decimal_usize(" 5"));
- assert_eq!(None, parse_decimal_usize("5_0"));
- assert_eq!(Some(50), parse_decimal_usize("50"));
- assert_eq!(RangeSelection::Full, parse_range(Some("bytes=+5-"), 100));
- assert_eq!(RangeSelection::Full, parse_range(Some("bytes=-+5"), 100));
- assert_eq!(
- RequestRange::Forwardable("bytes=+5-"),
- classify_request_range(Some("bytes=+5-")),
- "forwarding is unchanged: the object store still settles the spec it always saw"
- );
- }
-
- #[test]
- fn classified_request_ranges_agree_with_the_size_aware_parser() {
- for raw in ["bytes=0-9", "bytes=-5", "bytes=50-", "bytes=0-9999"] {
- assert_eq!(
- RequestRange::Forwardable(raw),
- classify_request_range(Some(raw)),
- "range={raw} must reach the upstream"
- );
- assert!(matches!(
- parse_range(Some(raw), 100),
- RangeSelection::Partial(_)
- ));
- }
- assert_eq!(
- RequestRange::Forwardable("bytes=0-9"),
- classify_request_range(Some(" bytes=0-9 "))
- );
- assert_eq!(
- RequestRange::Forwardable("bytes=100-200"),
- classify_request_range(Some("bytes=100-200")),
- "only the object size can settle a range that starts past the end"
- );
- for raw in ["bytes=10-5", "bytes=-0"] {
- assert_eq!(
- RequestRange::Unsatisfiable,
- classify_request_range(Some(raw)),
- "range={raw} is unsatisfiable at every size"
- );
- assert_eq!(RangeSelection::Unsatisfiable, parse_range(Some(raw), 100));
- assert_eq!(RangeSelection::Unsatisfiable, parse_range(Some(raw), 1));
- }
- for raw in [
- "rows=0-9",
- "bytes=",
- "bytes=abc-def",
- "bytes=0-abc",
- "bytes=0-1, 2-3",
- "bytes=0",
- ] {
- assert_eq!(
- RequestRange::Absent,
- classify_request_range(Some(raw)),
- "range={raw} must not reach the upstream"
- );
- assert_eq!(RangeSelection::Full, parse_range(Some(raw), 100));
- }
- assert_eq!(RequestRange::Absent, classify_request_range(None));
- }
-
- #[test]
- fn unsatisfiable_content_range_parser_reads_the_total() {
- assert_eq!(
- Some(100),
- parse_unsatisfiable_content_range(Some("bytes */100"))
- );
- assert_eq!(
- Some(0),
- parse_unsatisfiable_content_range(Some("bytes */0"))
- );
- assert_eq!(None, parse_unsatisfiable_content_range(Some("bytes */*")));
- assert_eq!(
- None,
- parse_unsatisfiable_content_range(Some("bytes 0-9/100"))
- );
- assert_eq!(None, parse_unsatisfiable_content_range(Some("*/100")));
- assert_eq!(None, parse_unsatisfiable_content_range(None));
- }
-
- #[test]
- fn content_range_parser_accepts_known_and_unknown_totals() {
- assert_eq!(
- Some(ContentRange {
- start: 0,
- end: 9,
- size: Some(100)
- }),
- parse_content_range(Some("bytes 0-9/100"))
- );
- assert_eq!(
- Some(ContentRange {
- start: 0,
- end: 9,
- size: None
- }),
- parse_content_range(Some("bytes 0-9/*"))
- );
- assert_eq!(None, parse_content_range(Some("bytes */100")));
- assert_eq!(None, parse_content_range(Some("bytes 10-9/100")));
- assert_eq!(None, parse_content_range(Some("bytes 0-100/100")));
- assert_eq!(None, parse_content_range(Some("bytes 0-0/0")));
- assert_eq!(None, parse_content_range(Some("bytes 0-0/")));
- assert_eq!(
- None,
- parse_content_range(Some("bytes 0-0/184467440737095516160"))
- );
- }
-
- #[test]
- fn range_parsing_stays_within_the_declared_file_size_on_adversarial_headers() {
- for header in ADVERSARIAL_RANGE_HEADERS {
- for file_size in [0_usize, 1, 100, usize::from(u16::MAX)] {
- if let RangeSelection::Partial(selected) = parse_range(Some(header), file_size) {
- assert!(file_size > 0, "partial range for empty file from {header}");
- assert!(
- selected.start <= selected.end,
- "inverted range from {header}"
- );
- assert!(selected.end < file_size, "range beyond file from {header}");
- }
- }
- if let Some(parsed) = parse_content_range(Some(header)) {
- assert!(
- parsed.start <= parsed.end,
- "inverted content range {header}"
- );
- if let Some(size) = parsed.size {
- assert!(size > 0, "zero-sized content range {header}");
- assert!(parsed.end < size, "content range beyond size {header}");
- }
- }
- }
- }
-}
diff --git a/fluxer_media_proxy/src/request_log/failure.rs b/fluxer_media_proxy/src/request_log/failure.rs
index 5a0630c7c..9533de59a 100644
--- a/fluxer_media_proxy/src/request_log/failure.rs
+++ b/fluxer_media_proxy/src/request_log/failure.rs
@@ -19,11 +19,4 @@ impl ErrorReason {
source: Some(format!("{source:?}")),
}
}
-
- pub fn with_message(code: &'static str, message: impl Into<String>) -> Self {
- Self {
- code,
- source: Some(message.into()),
- }
- }
}
diff --git a/fluxer_media_proxy/src/server/attachment_signature.rs b/fluxer_media_proxy/src/server/attachment_signature.rs
index c38932749..cf2087ece 100644
--- a/fluxer_media_proxy/src/server/attachment_signature.rs
+++ b/fluxer_media_proxy/src/server/attachment_signature.rs
@@ -165,9 +165,6 @@ fn now_secs() -> u64 {
.as_secs()
}
-#[cfg(test)]
-use crate::test_fixtures::CapturedLog;
-
#[cfg(test)]
mod tests {
use super::*;
@@ -310,92 +307,4 @@ mod tests {
assert_eq!(bodies[0], *body);
}
}
-
- #[test]
- fn the_would_deny_line_includes_the_verdict_and_a_clipped_user_agent() {
- let mut headers = HeaderMap::new();
- headers.insert(
- header::USER_AGENT,
- "u".repeat(4096).parse().expect("ascii user agent"),
- );
- let captured = CapturedLog::default();
- let subscriber = tracing_subscriber::fmt()
- .with_writer(captured.clone())
- .with_ansi(false)
- .with_max_level(tracing::Level::TRACE)
- .finish();
- {
- let _guard = tracing::subscriber::set_default(subscriber);
- log_would_deny(
- &WouldDenyLog::new(),
- &Method::GET,
- Verdict::Expired,
- &headers,
- Some(&RequestId("ABCDEFGHJKMN".to_owned())),
- );
- }
- let line = captured.text();
- assert!(
- line.contains("reason=\"attachment_signature_would_deny\""),
- "{line}"
- );
- assert!(line.contains("kind=\"attachment\""), "{line}");
- assert!(line.contains("method=GET"), "{line}");
- assert!(line.contains("verdict=\"expired\""), "{line}");
- assert!(line.contains("req=\"ABCDEFGHJKMN\""), "{line}");
- assert!(line.contains(&"u".repeat(HEADER_LOG_BYTES_MAX)), "{line}");
- assert!(
- !line.contains(&"u".repeat(HEADER_LOG_BYTES_MAX + 1)),
- "{line}"
- );
- }
-
- #[test]
- fn a_request_without_an_id_or_user_agent_still_logs_one_line() {
- let captured = CapturedLog::default();
- let subscriber = tracing_subscriber::fmt()
- .with_writer(captured.clone())
- .with_ansi(false)
- .with_max_level(tracing::Level::TRACE)
- .finish();
- {
- let _guard = tracing::subscriber::set_default(subscriber);
- log_would_deny(
- &WouldDenyLog::new(),
- &Method::HEAD,
- Verdict::Missing,
- &HeaderMap::new(),
- None,
- );
- }
- let line = captured.text();
- assert_eq!(1, line.lines().count(), "{line}");
- assert!(line.contains("verdict=\"missing\""), "{line}");
- assert!(line.contains("req=\"\""), "{line}");
- assert!(line.contains("suppressed=0"), "{line}");
- }
-
- #[test]
- fn the_would_deny_budget_bounds_a_window_and_reports_what_it_dropped() {
- let budget = WouldDenyLog::new();
- let window = 10_000;
- for line in 0..WOULD_DENY_LOG_LINES_PER_WINDOW {
- assert_eq!(Some(0), budget.admit(window), "line {line}");
- }
- for line in 0..5 {
- assert_eq!(None, budget.admit(window + 500), "line {line}");
- }
- let next = window + WOULD_DENY_LOG_WINDOW_MS;
- assert_eq!(Some(5), budget.admit(next));
- assert_eq!(Some(0), budget.admit(next));
- }
-
- #[test]
- fn the_would_deny_budget_never_wedges_on_a_clock_that_goes_backwards() {
- let budget = WouldDenyLog::new();
- assert_eq!(Some(0), budget.admit(10_000));
- assert_eq!(Some(0), budget.admit(9_000));
- assert_eq!(Some(0), budget.admit(i64::MIN));
- assert_eq!(Some(0), budget.admit(i64::MAX));
- }
}
diff --git a/fluxer_media_proxy/src/server/cors.rs b/fluxer_media_proxy/src/server/cors.rs
index 9ab975289..0419f1a6e 100644
--- a/fluxer_media_proxy/src/server/cors.rs
+++ b/fluxer_media_proxy/src/server/cors.rs
@@ -122,19 +122,10 @@ fn origin_for_log(request: &HeaderMap) -> &str {
&origin[..origin.len().min(ORIGIN_LOG_BYTES_MAX)]
}
-#[cfg(test)]
-use crate::test_fixtures::CapturedLog;
-
#[cfg(test)]
mod tests {
use super::*;
- use crate::{
- http_headers::{self, add_media_headers, add_unsatisfiable_headers},
- request_log::ErrorReason,
- server::response::error::text,
- test_fixtures::ADVERSARIAL_TEXT_INPUTS,
- };
- use axum::body::to_bytes;
+ use crate::test_fixtures::ADVERSARIAL_TEXT_INPUTS;
const WEB: &str = "https://web.fluxer.app";
const CANARY: &str = "https://web.canary.fluxer.app";
@@ -158,20 +149,6 @@ mod tests {
check_origin(&allowlist(), &origin_headers(origins))
}
- fn header_text(headers: &HeaderMap, name: header::HeaderName) -> Option<&str> {
- headers
- .get(name)
- .map(|value| value.to_str().expect("header is ascii"))
- }
-
- fn vary_values(headers: &HeaderMap) -> Vec<&str> {
- headers
- .get_all(header::VARY)
- .iter()
- .map(|value| value.to_str().expect("vary is ascii"))
- .collect()
- }
-
#[test]
fn origin_check_classifies_every_origin_shape() {
assert_eq!(OriginCheck::Absent, check(&[]));
@@ -230,122 +207,4 @@ mod tests {
"at least one adversarial input is a header value"
);
}
-
- #[test]
- fn narrowing_rewrites_only_an_allow_origin_a_builder_set() {
- let allowed = OriginCheck::Allowed(HeaderValue::from_static(WEB));
-
- let mut media_allowed = HeaderMap::new();
- add_media_headers(&mut media_allowed, 16, "image/png", None);
- narrow_response(&allowed, &mut media_allowed);
- assert_eq!(
- Some(WEB),
- header_text(&media_allowed, header::ACCESS_CONTROL_ALLOW_ORIGIN)
- );
-
- let mut media_absent = HeaderMap::new();
- add_media_headers(&mut media_absent, 16, "image/png", None);
- narrow_response(&OriginCheck::Absent, &mut media_absent);
- assert!(!media_absent.contains_key(header::ACCESS_CONTROL_ALLOW_ORIGIN));
-
- let mut media_refused = HeaderMap::new();
- add_media_headers(&mut media_refused, 16, "image/png", None);
- narrow_response(&OriginCheck::Refused, &mut media_refused);
- assert!(!media_refused.contains_key(header::ACCESS_CONTROL_ALLOW_ORIGIN));
-
- let mut unsatisfiable = HeaderMap::new();
- add_unsatisfiable_headers(&mut unsatisfiable, 16);
- narrow_response(&allowed, &mut unsatisfiable);
- assert_eq!(
- Some(WEB),
- header_text(&unsatisfiable, header::ACCESS_CONTROL_ALLOW_ORIGIN)
- );
-
- let mut not_found = text(StatusCode::NOT_FOUND, "Not Found").headers().clone();
- narrow_response(&allowed, &mut not_found);
- assert!(!not_found.contains_key(header::ACCESS_CONTROL_ALLOW_ORIGIN));
-
- for headers in [
- &media_allowed,
- &media_absent,
- &media_refused,
- &unsatisfiable,
- &not_found,
- ] {
- assert_eq!(vec!["Accept-Encoding, Origin"], vary_values(headers));
- }
- }
-
- #[tokio::test]
- async fn refused_response_is_a_no_store_forbidden_without_cors() {
- let response = refused_response(&origin_headers(&[HeaderValue::from_static(
- "https://evil.example",
- )]));
- assert_eq!(StatusCode::FORBIDDEN, response.status());
- let headers = response.headers();
- assert_eq!(
- Some("text/plain; charset=utf-8"),
- header_text(headers, header::CONTENT_TYPE)
- );
- assert_eq!(
- Some("no-store"),
- header_text(headers, header::CACHE_CONTROL)
- );
- assert_eq!(vec!["Accept-Encoding, Origin"], vary_values(headers));
- assert!(!headers.contains_key(header::ACCESS_CONTROL_ALLOW_ORIGIN));
- assert_eq!(
- Some(http_headers::MEDIA_CSP),
- header_text(headers, header::CONTENT_SECURITY_POLICY)
- );
- assert_eq!(
- Some(http_headers::STRICT_TRANSPORT_SECURITY),
- header_text(headers, header::STRICT_TRANSPORT_SECURITY)
- );
- let reason = response
- .extensions()
- .get::<ErrorReason>()
- .expect("a refusal has an error reason")
- .clone();
- assert_eq!("cors_origin_denied", reason.code);
- assert_eq!(
- Some(format!("{:?}", "origin=https://evil.example")),
- reason.source
- );
- assert_eq!(
- b"Forbidden".as_slice(),
- to_bytes(response.into_body(), usize::MAX)
- .await
- .expect("refusal body")
- );
- }
-
- #[test]
- fn a_long_origin_is_clipped_for_logging() {
- let long = "a".repeat(4096);
- let headers = origin_headers(&[HeaderValue::from_str(&long).expect("ascii origin")]);
- let clipped = "a".repeat(ORIGIN_LOG_BYTES_MAX);
- assert_eq!(clipped, origin_for_log(&headers));
-
- let captured = CapturedLog::default();
- let subscriber = tracing_subscriber::fmt()
- .with_writer(captured.clone())
- .with_ansi(false)
- .finish();
- {
- let _guard = tracing::subscriber::set_default(subscriber);
- log_would_refuse(&Method::GET, "/avatars/1/hash.png", &headers);
- }
- let line = captured.text();
- assert!(line.contains("reason=\"cors_origin_would_deny\""), "{line}");
- assert!(line.contains("kind=\"asset_image\""), "{line}");
- assert!(line.contains("method=GET"), "{line}");
- assert!(
- line.trim_end().ends_with(&format!("origin={clipped}")),
- "{line}"
- );
- assert!(
- !line.contains(&"a".repeat(ORIGIN_LOG_BYTES_MAX + 1)),
- "{line}"
- );
- }
}
diff --git a/fluxer_media_proxy/src/server/download_stream.rs b/fluxer_media_proxy/src/server/download_stream.rs
index 347bfe1a0..a547836e5 100644
--- a/fluxer_media_proxy/src/server/download_stream.rs
+++ b/fluxer_media_proxy/src/server/download_stream.rs
@@ -156,166 +156,3 @@ impl ActiveDownloadStream {
fn fail(error: io::Error) -> GuardedFrame {
Some((Err(error), None))
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use crate::metrics::Metrics;
- use http_body_util::BodyExt as _;
-
- fn body_from_chunks(chunks: Vec<&'static [u8]>) -> Body {
- Body::from_stream(futures_util::stream::iter(
- chunks
- .into_iter()
- .map(|chunk| Ok::<_, io::Error>(Bytes::from_static(chunk))),
- ))
- }
-
- async fn collect_error(body: Body) -> io::Error {
- let mut error = body
- .collect()
- .await
- .map(|collected| collected.to_bytes())
- .expect_err("the guarded stream fails")
- .into_inner();
- loop {
- error = match error.downcast::<io::Error>() {
- Ok(error) => return *error,
- Err(error) => error
- .downcast::<axum::Error>()
- .expect("the guarded stream fails with an io error")
- .into_inner(),
- };
- }
- }
-
- #[tokio::test]
- async fn a_stream_matching_its_content_length_passes_through_unchanged() {
- let guarded = DownloadStreamPolicy::for_passthrough(30_000)
- .guard(body_from_chunks(vec![b"he", b"llo"]), 5);
- let collected = guarded
- .collect()
- .await
- .expect("the guarded stream ends")
- .to_bytes();
- assert_eq!(&b"hello"[..], &collected[..]);
- }
-
- #[tokio::test]
- async fn a_stream_that_ends_before_its_content_length_fails_with_unexpected_eof() {
- let guarded =
- DownloadStreamPolicy::for_passthrough(30_000).guard(body_from_chunks(vec![b"hel"]), 5);
- assert_eq!(
- io::ErrorKind::UnexpectedEof,
- collect_error(guarded).await.kind()
- );
- }
-
- #[tokio::test]
- async fn a_stream_that_overruns_its_content_length_fails_with_invalid_data() {
- let guarded = DownloadStreamPolicy::for_passthrough(30_000)
- .guard(body_from_chunks(vec![b"hello", b"world"]), 5);
- assert_eq!(
- io::ErrorKind::InvalidData,
- collect_error(guarded).await.kind()
- );
- }
-
- #[tokio::test]
- async fn a_chunk_larger_than_the_remaining_content_length_fails_with_invalid_data() {
- let guarded = DownloadStreamPolicy::for_passthrough(30_000)
- .guard(body_from_chunks(vec![b"hello world"]), 5);
- assert_eq!(
- io::ErrorKind::InvalidData,
- collect_error(guarded).await.kind()
- );
- }
-
- #[tokio::test]
- async fn a_stalled_stream_fails_at_its_idle_timeout() {
- let stalled =
- Body::from_stream(futures_util::stream::pending::<Result<Bytes, io::Error>>());
- let guarded = DownloadStreamPolicy::for_passthrough(1).guard(stalled, 5);
- assert_eq!(io::ErrorKind::TimedOut, collect_error(guarded).await.kind());
- }
-
- #[tokio::test]
- async fn an_external_stream_that_sends_past_its_content_length_is_counted() {
- let metrics = Metrics::new();
- let guarded = DownloadStreamPolicy::for_external(30_000, metrics.external())
- .guard(body_from_chunks(vec![b"hello", b"world"]), 5);
- assert_eq!(
- io::ErrorKind::InvalidData,
- collect_error(guarded).await.kind()
- );
- assert!(
- metrics
- .render()
- .contains("fluxer_media_proxy_external_stream_overruns_total 1\n")
- );
- }
-
- #[tokio::test]
- async fn an_external_chunk_past_the_remaining_content_length_is_counted() {
- let metrics = Metrics::new();
- let guarded = DownloadStreamPolicy::for_external(30_000, metrics.external())
- .guard(body_from_chunks(vec![b"hello world"]), 5);
- assert_eq!(
- io::ErrorKind::InvalidData,
- collect_error(guarded).await.kind()
- );
- assert!(
- metrics
- .render()
- .contains("fluxer_media_proxy_external_stream_overruns_total 1\n")
- );
- }
-
- #[tokio::test]
- async fn a_well_behaved_external_stream_leaves_the_overrun_counter_alone() {
- let metrics = Metrics::new();
- let guarded = DownloadStreamPolicy::for_external(30_000, metrics.external())
- .guard(body_from_chunks(vec![b"he", b"llo"]), 5);
- let collected = guarded
- .collect()
- .await
- .expect("the guarded stream ends")
- .to_bytes();
- assert_eq!(&b"hello"[..], &collected[..]);
- assert!(
- metrics
- .render()
- .contains("fluxer_media_proxy_external_stream_overruns_total 0\n")
- );
- }
-
- #[tokio::test]
- async fn a_capped_stream_that_ends_before_its_cap_passes_through_unchanged() {
- let guarded = DownloadStreamPolicy::for_passthrough(30_000)
- .guard_capped(body_from_chunks(vec![b"he", b"llo"]), 1024);
- let collected = guarded
- .collect()
- .await
- .expect("the guarded stream ends")
- .to_bytes();
- assert_eq!(&b"hello"[..], &collected[..]);
- }
-
- #[tokio::test]
- async fn a_capped_stream_that_exceeds_its_cap_fails_with_invalid_data() {
- let guarded = DownloadStreamPolicy::for_passthrough(30_000)
- .guard_capped(body_from_chunks(vec![b"hello", b"world"]), 5);
- assert_eq!(
- io::ErrorKind::InvalidData,
- collect_error(guarded).await.kind()
- );
- }
-
- #[tokio::test]
- async fn a_stalled_capped_stream_still_fails_at_its_idle_timeout() {
- let stalled =
- Body::from_stream(futures_util::stream::pending::<Result<Bytes, io::Error>>());
- let guarded = DownloadStreamPolicy::for_passthrough(1).guard_capped(stalled, 1024);
- assert_eq!(io::ErrorKind::TimedOut, collect_error(guarded).await.kind());
- }
-}
diff --git a/fluxer_media_proxy/src/server/external/tests/mod.rs b/fluxer_media_proxy/src/server/external/tests/mod.rs
index 1e1daaf48..292deca64 100644
--- a/fluxer_media_proxy/src/server/external/tests/mod.rs
+++ b/fluxer_media_proxy/src/server/external/tests/mod.rs
@@ -1,643 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
use super::{
- ExternalBody, ExternalFetchError, ExternalSuccessStatus, FetchedExternal,
- PreparedExternalRequest,
- byte_range::{
- ExternalPartial, ExternalRangeSelection, parse_external_requested_range,
- validate_external_partial,
- },
- external_fetch_error_response, external_head_response,
- fetch::{
- ExternalBufferRequest, ExternalHead, buffer_external_response,
- external_streamed_source_is_svg,
- },
- is_redirect_status, map_internal_metadata_upstream_status, map_upstream_status,
- response::{
- ExternalPartialResponse, ExternalStreamDecision, ExternalStreamingResponse,
- external_partial_response, external_stream_decision, external_streaming_response,
- },
- serve_fetched_external,
+ fetch::external_streamed_source_is_svg,
+ response::{ExternalStreamDecision, external_stream_decision},
};
-use crate::{
- byte_budget::{BudgetedBytes, ByteBudget},
- config::Config,
- constants,
- metrics::external::ExternalMetrics,
- server::{download_stream::DownloadStreamPolicy, state::AppState},
-};
-use axum::{
- body::to_bytes,
- http::{HeaderMap, HeaderValue, Method, StatusCode, header},
-};
-use bytes::Bytes;
-use std::{collections::HashMap, sync::Arc};
-
-fn test_stream_policy() -> DownloadStreamPolicy {
- DownloadStreamPolicy::for_passthrough(30_000)
-}
-
-fn validated_partial(requested: &str, content_range: &str, content_length: u64) -> ExternalPartial {
- validate_external_partial(
- parse_external_requested_range(requested),
- Some(content_range),
- Some(content_length),
- constants::MAX_MEDIA_PROXY_BYTES,
- )
- .expect("the upstream partial response is valid")
-}
-
-#[test]
-fn external_stream_decision_streams_an_unknown_length_media_body() {
- assert_eq!(
- ExternalStreamDecision::Stream(Some(1024)),
- external_stream_decision(true, Some(1024), "video/mp4")
- );
- assert_eq!(
- ExternalStreamDecision::Stream(None),
- external_stream_decision(true, None, "video/mp4")
- );
- assert_eq!(
- ExternalStreamDecision::Buffer,
- external_stream_decision(false, Some(1024), "video/mp4")
- );
- assert_eq!(
- ExternalStreamDecision::Buffer,
- external_stream_decision(true, Some(1024), "application/octet-stream")
- );
- assert_eq!(
- ExternalStreamDecision::Buffer,
- external_stream_decision(true, None, "")
- );
- assert_eq!(
- ExternalStreamDecision::Buffer,
- external_stream_decision(true, Some(1024), "image/svg+xml")
- );
- assert_eq!(
- ExternalStreamDecision::Buffer,
- external_stream_decision(true, Some(1024), "image/svg+xml; charset=utf-8")
- );
- assert_eq!(
- ExternalStreamDecision::Buffer,
- external_stream_decision(
- true,
- Some(constants::MAX_MEDIA_PROXY_BYTES as u64 + 1),
- "video/mp4"
- )
- );
- assert_eq!(
- ExternalStreamDecision::Stream(Some(constants::MAX_MEDIA_PROXY_BYTES as u64)),
- external_stream_decision(
- true,
- Some(constants::MAX_MEDIA_PROXY_BYTES as u64),
- "video/mp4"
- )
- );
-}
-
-#[tokio::test]
-async fn external_streaming_response_passes_body_through() {
- let upstream = reqwest::Response::from(
- http::Response::builder()
- .status(StatusCode::OK)
- .body("streamed bytes")
- .unwrap(),
- );
- let response = external_streaming_response(ExternalStreamingResponse {
- method: Method::GET,
- response: upstream,
- prefix: Bytes::new(),
- content_length: Some(14),
- content_type: "video/mp4",
- disposition: Some(HeaderValue::from_static("inline; filename=\"clip.mp4\"")),
- stream_policy: test_stream_policy(),
- });
-
- assert_eq!(StatusCode::OK, response.status());
- assert_eq!(
- "14",
- response
- .headers()
- .get(header::CONTENT_LENGTH)
- .unwrap()
- .to_str()
- .unwrap()
- );
- assert_eq!(
- "video/mp4",
- response
- .headers()
- .get(header::CONTENT_TYPE)
- .unwrap()
- .to_str()
- .unwrap()
- );
- let body = to_bytes(response.into_body(), 64).await.unwrap();
- assert_eq!(b"streamed bytes", body.as_ref());
-}
-
-#[tokio::test]
-async fn external_partial_response_streams_partial_body() {
- let upstream = reqwest::Response::from(
- http::Response::builder()
- .status(StatusCode::PARTIAL_CONTENT)
- .body("abcd")
- .unwrap(),
- );
- let fetched = FetchedExternal {
- url: "https://media.example.test/clip.webm".to_owned(),
- status: ExternalSuccessStatus::Partial(validated_partial("bytes=0-3", "bytes 0-3/10", 4)),
- body: ExternalBody::Streaming {
- response: upstream,
- prefix: Bytes::new(),
- content_length: Some(4),
- },
- content_type: "video/webm".to_owned(),
- };
- let response = external_partial_response(ExternalPartialResponse {
- method: Method::GET,
- partial: fetched
- .status
- .partial()
- .expect("the fetched response is partial"),
- fetched,
- disposition: None,
- stream_policy: test_stream_policy(),
- });
-
- assert_eq!(StatusCode::PARTIAL_CONTENT, response.status());
- assert_eq!(
- "bytes 0-3/10",
- response
- .headers()
- .get(header::CONTENT_RANGE)
- .unwrap()
- .to_str()
- .unwrap()
- );
- assert_eq!(
- "4",
- response
- .headers()
- .get(header::CONTENT_LENGTH)
- .unwrap()
- .to_str()
- .unwrap()
- );
- let body = to_bytes(response.into_body(), 64).await.unwrap();
- assert_eq!(b"abcd", body.as_ref());
-}
-
-#[test]
-fn external_partial_response_uses_media_headers() {
- let fetched = FetchedExternal {
- url: "https://media.example.test/clip.webm".to_owned(),
- status: ExternalSuccessStatus::Partial(validated_partial("bytes=0-3", "bytes 0-3/10", 4)),
- body: ExternalBody::Buffered(BudgetedBytes::unbudgeted(Bytes::from_static(b"abcd"))),
- content_type: "video/webm".to_owned(),
- };
- let response = external_partial_response(ExternalPartialResponse {
- method: Method::GET,
- partial: fetched
- .status
- .partial()
- .expect("the fetched response is partial"),
- fetched,
- disposition: Some(HeaderValue::from_static("inline; filename=\"clip.webm\"")),
- stream_policy: test_stream_policy(),
- });
-
- assert_eq!(StatusCode::PARTIAL_CONTENT, response.status());
- assert_eq!(
- "*",
- response
- .headers()
- .get(header::ACCESS_CONTROL_ALLOW_ORIGIN)
- .unwrap()
- .to_str()
- .unwrap()
- );
- assert_eq!(
- "video/webm",
- response
- .headers()
- .get(header::CONTENT_TYPE)
- .unwrap()
- .to_str()
- .unwrap()
- );
- assert_eq!(
- "bytes 0-3/10",
- response
- .headers()
- .get(header::CONTENT_RANGE)
- .unwrap()
- .to_str()
- .unwrap()
- );
- assert_eq!(
- "inline; filename=\"clip.webm\"",
- response
- .headers()
- .get(header::CONTENT_DISPOSITION)
- .unwrap()
- .to_str()
- .unwrap()
- );
- assert!(
- response
- .headers()
- .contains_key(header::CONTENT_SECURITY_POLICY)
- );
- assert!(response.headers().contains_key("strict-transport-security"));
- assert!(response.headers().contains_key("permissions-policy"));
- assert!(
- response
- .headers()
- .contains_key(header::X_CONTENT_TYPE_OPTIONS)
- );
- assert!(response.headers().contains_key("CDN-Cache-Control"));
-}
-
-#[test]
-fn external_range_parser_accepts_one_canonical_range() {
- assert!(matches!(
- parse_external_requested_range("bytes=10-20"),
- Some(ExternalRangeSelection::Bounded { start: 10, end: 20 })
- ));
- assert!(matches!(
- parse_external_requested_range(" bytes=10- \t"),
- Some(ExternalRangeSelection::From { start: 10 })
- ));
- assert!(matches!(
- parse_external_requested_range("BYTES=-20"),
- Some(ExternalRangeSelection::Suffix { length: 20 })
- ));
- for invalid in ["items=1-2", "bytes=", "bytes=1-2, 3-4"] {
- assert!(
- parse_external_requested_range(invalid).is_none(),
- "accepted {invalid}"
- );
- }
-}
-
-#[test]
-fn external_partial_validation_matches_requested_range_and_lengths() {
- let bounded = parse_external_requested_range("bytes=10-99");
- let partial = validate_external_partial(bounded, Some("bytes 10-49/50"), Some(40), 100)
- .expect("bounded partial");
- assert_eq!(Some(40), partial.content_length());
- assert_eq!(
- Some(HeaderValue::from_static("bytes 10-49/50")),
- partial.header_value()
- );
-
- let suffix = parse_external_requested_range("bytes=-10");
- assert!(validate_external_partial(suffix, Some("bytes 40-49/50"), Some(10), 10).is_some());
- let oversized_suffix = parse_external_requested_range("bytes=-100");
- assert!(
- validate_external_partial(oversized_suffix, Some("bytes 0-49/50"), Some(50), 50).is_some()
- );
-
- let from = parse_external_requested_range("bytes=10-");
- assert!(validate_external_partial(from, Some("bytes 10-49/50"), Some(40), 40).is_some());
- assert!(validate_external_partial(from, Some("bytes 11-49/50"), Some(39), 40).is_none());
- assert!(validate_external_partial(from, Some("bytes 10-49/*"), Some(40), 40).is_some());
- assert!(validate_external_partial(from, Some("bytes 10-49/50"), Some(39), 40).is_none());
- assert!(validate_external_partial(from, Some("bytes 10-49/50"), Some(40), 39).is_none());
-}
-
-#[test]
-fn upstream_status_mapping_and_redirect_policy_are_closed() {
- assert_eq!(
- map_upstream_status(StatusCode::NOT_FOUND),
- StatusCode::NOT_FOUND
- );
- assert_eq!(
- map_upstream_status(StatusCode::INTERNAL_SERVER_ERROR),
- StatusCode::BAD_GATEWAY
- );
- assert!(is_redirect_status(StatusCode::MOVED_PERMANENTLY));
- assert!(is_redirect_status(StatusCode::PERMANENT_REDIRECT));
- assert!(!is_redirect_status(StatusCode::NOT_MODIFIED));
- assert!(!is_redirect_status(StatusCode::OK));
-}
-
-#[test]
-fn upstream_status_is_clamped_to_known_whitelist() {
- assert_eq!(
- StatusCode::NOT_FOUND,
- map_upstream_status(StatusCode::NOT_FOUND)
- );
- assert_eq!(
- StatusCode::TOO_MANY_REQUESTS,
- map_upstream_status(StatusCode::TOO_MANY_REQUESTS)
- );
- assert_eq!(
- StatusCode::from_u16(428).unwrap(),
- map_upstream_status(StatusCode::from_u16(428).unwrap())
- );
- assert_eq!(
- StatusCode::BAD_GATEWAY,
- map_upstream_status(StatusCode::from_u16(451).unwrap())
- );
- assert_eq!(
- StatusCode::BAD_GATEWAY,
- map_upstream_status(StatusCode::INTERNAL_SERVER_ERROR)
- );
- assert_eq!(
- StatusCode::BAD_GATEWAY,
- map_upstream_status(StatusCode::SERVICE_UNAVAILABLE)
- );
-}
-
-#[test]
-fn internal_metadata_does_not_surface_origin_429() {
- assert_eq!(
- StatusCode::SERVICE_UNAVAILABLE,
- map_internal_metadata_upstream_status(StatusCode::TOO_MANY_REQUESTS)
- );
- assert_eq!(
- StatusCode::NOT_FOUND,
- map_internal_metadata_upstream_status(StatusCode::NOT_FOUND)
- );
-}
-
-#[test]
-fn external_partial_accepts_an_unknown_complete_length() {
- let bounded = parse_external_requested_range("bytes=0-9");
- let partial = validate_external_partial(bounded, Some("bytes 0-9/*"), Some(10), 100)
- .expect("an unknown complete length is a legal Content-Range");
- assert_eq!(Some(10), partial.content_length());
- assert_eq!(
- Some(HeaderValue::from_static("bytes 0-9/*")),
- partial.header_value()
- );
- assert!(validate_external_partial(bounded, Some("bytes 1-9/*"), Some(9), 100).is_none());
- assert!(validate_external_partial(bounded, Some("bytes 0-8/*"), Some(9), 100).is_some());
-
- let from = parse_external_requested_range("bytes=10-");
- assert!(validate_external_partial(from, Some("bytes 10-49/*"), Some(40), 100).is_some());
- assert!(validate_external_partial(from, Some("bytes 11-49/*"), Some(39), 100).is_none());
-
- let suffix = parse_external_requested_range("bytes=-10");
- assert!(validate_external_partial(suffix, Some("bytes 40-49/*"), Some(10), 100).is_some());
- assert!(validate_external_partial(suffix, Some("bytes 0-4/*"), Some(5), 100).is_some());
- assert!(validate_external_partial(suffix, Some("bytes 40-48/*"), Some(9), 100).is_none());
-}
-
-#[test]
-fn external_partial_response_serves_an_unknown_complete_length() {
- let fetched = FetchedExternal {
- url: "https://media.example.test/clip.webm".to_owned(),
- status: ExternalSuccessStatus::Partial(validated_partial("bytes=0-3", "bytes 0-3/*", 4)),
- body: ExternalBody::Buffered(BudgetedBytes::unbudgeted(Bytes::from_static(b"abcd"))),
- content_type: "video/webm".to_owned(),
- };
- let response = external_partial_response(ExternalPartialResponse {
- method: Method::GET,
- partial: fetched
- .status
- .partial()
- .expect("the fetched response is partial"),
- fetched,
- disposition: None,
- stream_policy: test_stream_policy(),
- });
-
- assert_eq!(StatusCode::PARTIAL_CONTENT, response.status());
- assert_eq!(
- "bytes 0-3/*",
- response
- .headers()
- .get(header::CONTENT_RANGE)
- .unwrap()
- .to_str()
- .unwrap()
- );
- assert_eq!(
- "4",
- response
- .headers()
- .get(header::CONTENT_LENGTH)
- .unwrap()
- .to_str()
- .unwrap()
- );
-}
-
-#[test]
-fn external_multi_range_reaches_the_origin_verbatim() {
- for spec in [
- "bytes=0-9,20-29",
- "bytes=1-2,3-4",
- "bytes=-0",
- "bytes=2-1",
- "bytes=1--2",
- "bytes=+5-",
- "bytes=cheese",
- ] {
- let forwarded =
- parse_external_requested_range(spec).expect("an unparsed spec reaches the origin");
- assert!(matches!(forwarded, ExternalRangeSelection::Verbatim { .. }));
- assert_eq!(forwarded.header_value(), spec);
- }
-}
-
-#[test]
-fn external_verbatim_range_forwards_the_upstream_partial_unchanged() {
- let multi = parse_external_requested_range("bytes=0-9,20-29");
- let partial = validate_external_partial(multi, Some("bytes 0-9/50"), Some(10), 100)
- .expect("a verbatim range trusts the upstream partial");
- assert_eq!(None, partial.content_length());
- assert_eq!(
- Some(HeaderValue::from_static("bytes 0-9/50")),
- partial.header_value()
- );
-
- let multipart = validate_external_partial(multi, None, Some(4096), 100)
- .expect("a multipart partial has no Content-Range");
- assert_eq!(None, multipart.content_length());
- assert_eq!(None, multipart.header_value());
-}
-
-#[tokio::test]
-async fn external_multipart_partial_keeps_the_upstream_body_length() {
- let fetched = FetchedExternal {
- url: "https://media.example.test/clip.webm".to_owned(),
- status: ExternalSuccessStatus::Partial(
- validate_external_partial(
- parse_external_requested_range("bytes=0-1,3-4"),
- None,
- None,
- constants::MAX_MEDIA_PROXY_BYTES,
- )
- .expect("a verbatim range trusts the upstream partial"),
- ),
- body: ExternalBody::Buffered(BudgetedBytes::unbudgeted(Bytes::from_static(b"abcde"))),
- content_type: "multipart/byteranges; boundary=xyz".to_owned(),
- };
- let response = external_partial_response(ExternalPartialResponse {
- method: Method::GET,
- partial: fetched
- .status
- .partial()
- .expect("the fetched response is partial"),
- fetched,
- disposition: None,
- stream_policy: test_stream_policy(),
- });
-
- assert_eq!(StatusCode::PARTIAL_CONTENT, response.status());
- assert!(!response.headers().contains_key(header::CONTENT_RANGE));
- assert_eq!(
- "5",
- response
- .headers()
- .get(header::CONTENT_LENGTH)
- .unwrap()
- .to_str()
- .unwrap()
- );
- let body = to_bytes(response.into_body(), 64).await.unwrap();
- assert_eq!(b"abcde", body.as_ref());
-}
-
-#[test]
-fn external_partial_validation_rejects_a_partial_the_client_did_not_request() {
- let bounded = parse_external_requested_range("bytes=10-19");
- assert!(validate_external_partial(bounded, Some("bytes 10-19/50"), Some(10), 100).is_some());
- assert!(validate_external_partial(bounded, Some("bytes 0-19/50"), Some(20), 100).is_none());
- assert!(validate_external_partial(bounded, Some("bytes 10-29/50"), Some(20), 100).is_none());
- assert!(validate_external_partial(bounded, Some("bytes 11-19/50"), Some(9), 100).is_none());
- assert!(validate_external_partial(bounded, None, Some(10), 100).is_none());
- assert!(
- validate_external_partial(None, Some("bytes 10-19/50"), Some(10), 100).is_none(),
- "an unrequested partial is never validated"
- );
-}
-
-#[tokio::test]
-async fn external_buffering_rejects_a_body_shorter_than_its_content_length() {
- let budget = ByteBudget::new(constants::MAX_MEDIA_PROXY_BYTES * 4);
- let metrics = ExternalMetrics::default();
- let truncated = buffer_external_response(ExternalBufferRequest {
- response: reqwest::Response::from(
- http::Response::builder()
- .status(StatusCode::OK)
- .header(header::CONTENT_LENGTH, "10")
- .body("abcd")
- .unwrap(),
- ),
- prefix: Bytes::new(),
- url: "https://media.example.test/clip.webm",
- budget: &budget,
- metrics: &metrics,
- content_length: Some(10),
- limit: constants::MAX_MEDIA_PROXY_BYTES,
- })
- .await;
- assert!(matches!(truncated, Err(ExternalFetchError::FetchFailed)));
-
- let complete = buffer_external_response(ExternalBufferRequest {
- response: reqwest::Response::from(
- http::Response::builder()
- .status(StatusCode::OK)
- .header(header::CONTENT_LENGTH, "4")
- .body("abcd")
- .unwrap(),
- ),
- prefix: Bytes::new(),
- url: "https://media.example.test/clip.webm",
- budget: &budget,
- metrics: &metrics,
- content_length: Some(4),
- limit: constants::MAX_MEDIA_PROXY_BYTES,
- })
- .await
- .expect("a complete body is buffered");
- assert_eq!(b"abcd", complete.as_bytes().as_ref());
-}
-
-#[test]
-fn a_narrowed_upstream_partial_is_forwarded_instead_of_failing_the_fetch() {
- assert_eq!(
- StatusCode::BAD_GATEWAY,
- external_fetch_error_response(
- "https://media.example.test/clip.webm",
- ExternalFetchError::FetchFailed
- )
- .status()
- );
-
- let bounded = parse_external_requested_range("bytes=0-999");
- let narrowed = validate_external_partial(bounded, Some("bytes 0-255/4096"), Some(256), 4096)
- .expect("an origin may answer a range request with a narrower range");
- assert_eq!(Some(256), narrowed.content_length());
- assert_eq!(
- Some(HeaderValue::from_static("bytes 0-255/4096")),
- narrowed.header_value()
- );
- assert!(validate_external_partial(bounded, Some("bytes 0-255/*"), Some(256), 4096).is_some());
-
- let from = parse_external_requested_range("bytes=100-");
- assert!(validate_external_partial(from, Some("bytes 100-355/4096"), Some(256), 4096).is_some());
- assert!(validate_external_partial(from, Some("bytes 100-355/*"), Some(256), 4096).is_some());
-
- assert!(
- validate_external_partial(bounded, Some("bytes 1-255/4096"), Some(255), 4096).is_none()
- );
- assert!(
- validate_external_partial(bounded, Some("bytes 0-1000/4096"), Some(1001), 4096).is_none()
- );
- assert!(validate_external_partial(from, Some("bytes 99-355/4096"), Some(257), 4096).is_none());
- assert!(validate_external_partial(None, Some("bytes 0-255/4096"), Some(256), 4096).is_none());
-}
-
-#[tokio::test]
-async fn an_unknown_length_upstream_streams_without_a_content_length() {
- let upstream = reqwest::Response::from(
- http::Response::builder()
- .status(StatusCode::OK)
- .body("streamed bytes")
- .unwrap(),
- );
- let response = external_streaming_response(ExternalStreamingResponse {
- method: Method::GET,
- response: upstream,
- prefix: Bytes::new(),
- content_length: None,
- content_type: "video/mp4",
- disposition: None,
- stream_policy: test_stream_policy(),
- });
-
- assert_eq!(StatusCode::OK, response.status());
- assert!(!response.headers().contains_key(header::CONTENT_LENGTH));
- assert_eq!(
- "video/mp4",
- response
- .headers()
- .get(header::CONTENT_TYPE)
- .unwrap()
- .to_str()
- .unwrap()
- );
- let body = to_bytes(response.into_body(), 64).await.unwrap();
- assert_eq!(b"streamed bytes", body.as_ref());
-}
-
-#[test]
-fn a_blocked_url_answers_bad_request_rather_than_bad_gateway() {
- assert_eq!(
- StatusCode::BAD_REQUEST,
- external_fetch_error_response(
- "https://metadata.internal.example/latest/",
- ExternalFetchError::BlockedUrl
- )
- .status()
- );
-}
#[test]
fn a_mislabeled_svg_body_never_takes_the_external_streaming_path() {
@@ -661,314 +27,3 @@ fn a_mislabeled_svg_body_never_takes_the_external_streaming_path() {
));
assert!(!external_streamed_source_is_svg(b"", "clip.mp4"));
}
-
-#[tokio::test]
-async fn external_streaming_response_emits_the_sniff_prefix_then_the_rest() {
- let upstream = reqwest::Response::from(
- http::Response::builder()
- .status(StatusCode::OK)
- .body("rest of the body")
- .unwrap(),
- );
- let response = external_streaming_response(ExternalStreamingResponse {
- method: Method::GET,
- response: upstream,
- prefix: Bytes::from_static(b"prefix "),
- content_length: Some(23),
- content_type: "video/mp4",
- disposition: None,
- stream_policy: test_stream_policy(),
- });
-
- assert_eq!(StatusCode::OK, response.status());
- assert_eq!(
- "23",
- response
- .headers()
- .get(header::CONTENT_LENGTH)
- .unwrap()
- .to_str()
- .unwrap()
- );
- let body = to_bytes(response.into_body(), 64).await.unwrap();
- assert_eq!(b"prefix rest of the body", body.as_ref());
-}
-
-#[tokio::test]
-async fn external_buffering_prepends_the_sniff_prefix() {
- let budget = ByteBudget::new(constants::MAX_MEDIA_PROXY_BYTES * 4);
- let metrics = ExternalMetrics::default();
- let buffered = buffer_external_response(ExternalBufferRequest {
- response: reqwest::Response::from(
- http::Response::builder()
- .status(StatusCode::OK)
- .header(header::CONTENT_LENGTH, "9")
- .body("ffix rest")
- .unwrap(),
- ),
- prefix: Bytes::from_static(b"<svg "),
- url: "https://media.example.test/logo.png",
- budget: &budget,
- metrics: &metrics,
- content_length: Some(14),
- limit: constants::MAX_MEDIA_PROXY_BYTES,
- })
- .await
- .expect("the sniffed prefix and the rest are one body");
- assert_eq!(b"<svg ffix rest", buffered.as_bytes().as_ref());
-}
-
-#[tokio::test]
-async fn a_cached_external_transform_is_served_without_refetching_the_origin() {
- use super::{external_hint, serve_external};
- use crate::{
- config::Config,
- server::{
- params::url_filename,
- state::AppState,
- transform::{ServeBytesRequest, parameters::TransformRoute, serve_bytes_or_transform},
- },
- test_fixtures::fixture_jpeg,
- };
- use axum::http::HeaderMap;
- use std::{collections::HashMap, sync::Arc};
-
- let cfg = Config::load_from_iter([(
- "FLUXER_MEDIA_PROXY_SECRET_KEY".to_owned(),
- "secret".to_owned(),
- )])
- .expect("config loads");
- let app = Arc::new(AppState::for_tests(cfg));
- // A loopback origin is refused before any socket work, so a 200 can only be cached bytes.
- let url = "http://127.0.0.1/photo.jpg";
- let proxy_path = build_external_media_proxy_path(url).expect("the external path builds");
- let rest = format!(
- "{}/{proxy_path}",
- fluxer_common::external_media_path::create_signature(
- &proxy_path,
- app.cfg.secret_key.as_bytes()
- )
- );
- let filename = url_filename(url);
- let data = Bytes::from(fixture_jpeg());
- let headers = HeaderMap::new();
-
- for pairs in [
- vec![("width", "32")],
- vec![("width", "32"), ("height", "24")],
- vec![("format", "png"), ("width", "32")],
- vec![("width", "32"), ("quality", "low")],
- ] {
- let params: HashMap<String, String> = pairs
- .iter()
- .map(|(key, value)| ((*key).to_owned(), (*value).to_owned()))
- .collect();
- assert_eq!(
- StatusCode::BAD_REQUEST,
- serve_external(&app, Method::GET, &rest, &params, &headers)
- .await
- .status(),
- "{pairs:?} must reach the fetch path while nothing is cached"
- );
-
- let served = serve_bytes_or_transform(
- app.media.transforms(),
- ServeBytesRequest {
- method: Method::GET,
- data: data.clone(),
- content_type: "image/jpeg".to_owned(),
- cache_identity: url,
- filename: &filename,
- route: TransformRoute::External,
- params: &params,
- headers: &headers,
- },
- )
- .await;
- assert_eq!(StatusCode::OK, served.status(), "{pairs:?}");
- let served_content_type = served
- .headers()
- .get(header::CONTENT_TYPE)
- .expect("a transform response declares its content type")
- .clone();
- let transformed = to_bytes(served.into_body(), usize::MAX).await.unwrap();
- app.media.external_hints().insert(
- url.to_owned(),
- external_hint(url, &filename, "image/jpeg", &data),
- );
-
- let cached = serve_external(&app, Method::GET, &rest, &params, &headers).await;
- assert_eq!(
- StatusCode::OK,
- cached.status(),
- "{pairs:?} must be answered from the transform cache"
- );
- assert_eq!(
- Some(&served_content_type),
- cached.headers().get(header::CONTENT_TYPE),
- "{pairs:?}"
- );
- assert_eq!(
- transformed,
- to_bytes(cached.into_body(), usize::MAX).await.unwrap(),
- "{pairs:?}"
- );
- }
-}
-
-fn probed_head(url: &str, content_type: &str, content_length: Option<u64>) -> ExternalHead {
- ExternalHead {
- url: url.to_owned(),
- status: StatusCode::OK,
- content_type: content_type.to_owned(),
- content_length,
- }
-}
-
-#[test]
-fn a_plain_head_is_answered_from_the_upstream_head_metadata() {
- let response = external_head_response(
- probed_head("https://cdn.example.test/clip.mp4", "video/mp4", Some(4096)),
- false,
- )
- .expect("an upstream HEAD with a usable length answers the probe");
- assert_eq!(StatusCode::OK, response.status());
- assert_eq!(
- Some(&HeaderValue::from_static("4096")),
- response.headers().get(header::CONTENT_LENGTH)
- );
- assert_eq!(
- Some(&HeaderValue::from_static("video/mp4")),
- response.headers().get(header::CONTENT_TYPE)
- );
-
- assert_eq!(
- Some(&HeaderValue::from_static("video/mp4")),
- external_head_response(
- probed_head(
- "https://cdn.example.test/clip.mp4",
- "application/octet-stream",
- Some(4096)
- ),
- false,
- )
- .expect("an untrusted upstream type falls back to the filename")
- .headers()
- .get(header::CONTENT_TYPE)
- );
-
- let mut partial = probed_head("https://cdn.example.test/clip.mp4", "video/mp4", Some(4096));
- partial.status = StatusCode::PARTIAL_CONTENT;
- for unusable in [
- probed_head(
- "https://cdn.example.test/logo.svg",
- "image/svg+xml",
- Some(16),
- ),
- probed_head("https://cdn.example.test/clip.mp4", "video/mp4", None),
- probed_head(
- "https://cdn.example.test/clip.mp4",
- "video/mp4",
- Some(constants::MAX_MEDIA_PROXY_BYTES as u64 + 1),
- ),
- partial,
- ] {
- assert!(
- external_head_response(unusable, false).is_none(),
- "unusable upstream metadata must fall back to the body fetch"
- );
- }
-}
-
-#[tokio::test]
-async fn an_upstream_that_ignores_the_client_range_still_streams_a_complete_body() {
- let cfg =
- Config::load_from_iter([("FLUXER_MEDIA_PROXY_SECRET_KEY", "secret")]).expect("test config");
- let app = Arc::new(AppState::for_tests(cfg));
- let url = "https://media.example.test/clip.mp4";
- let proxy_path = build_external_media_proxy_path(url).expect("the external path builds");
- let signature = fluxer_common::external_media_path::create_signature(
- &proxy_path,
- app.cfg.secret_key.as_bytes(),
- );
- let rest = format!("{signature}/{proxy_path}");
- let params = HashMap::new();
- let mut headers = HeaderMap::new();
- headers.insert(header::RANGE, HeaderValue::from_static("bytes=0-3"));
- let prepared = PreparedExternalRequest::new(&app, Method::GET, &rest, &params, &headers)
- .expect("the signed external request is accepted");
- let fetched = FetchedExternal {
- url: url.to_owned(),
- status: ExternalSuccessStatus::Complete,
- body: ExternalBody::Streaming {
- response: reqwest::Response::from(
- http::Response::builder()
- .status(StatusCode::OK)
- .body("streamed bytes")
- .unwrap(),
- ),
- prefix: bytes::Bytes::new(),
- content_length: Some(14),
- },
- content_type: "video/mp4".to_owned(),
- };
-
- let response = serve_fetched_external(prepared, fetched).await;
-
- assert_eq!(StatusCode::OK, response.status());
- assert!(!response.headers().contains_key(header::CONTENT_RANGE));
- assert_eq!(
- "14",
- response
- .headers()
- .get(header::CONTENT_LENGTH)
- .unwrap()
- .to_str()
- .unwrap()
- );
- let body = to_bytes(response.into_body(), 64).await.unwrap();
- assert_eq!(b"streamed bytes", body.as_ref());
-}
-
-#[tokio::test]
-async fn external_buffering_accepts_transport_chunks_of_any_size_within_the_limit() {
- const LARGE_CHUNK_BYTES: usize =
- crate::response_body_limit::RESPONSE_BODY_TRANSPORT_CHUNK_BYTES_MAX + 155_648;
- const SMALL_CHUNK_BYTES: usize = 1448;
- const SMALL_CHUNKS: usize = 512;
- let budget = ByteBudget::new(constants::MAX_MEDIA_PROXY_BYTES * 4);
- let metrics = ExternalMetrics::default();
- let buffer = |response: reqwest::Response, length: usize| {
- buffer_external_response(ExternalBufferRequest {
- response,
- prefix: Bytes::new(),
- url: "https://media.example.test/clip.webm",
- budget: &budget,
- metrics: &metrics,
- content_length: Some(length as u64),
- limit: constants::MAX_MEDIA_PROXY_BYTES,
- })
- };
-
- let large = buffer(
- reqwest::Response::from(http::Response::new(vec![7u8; LARGE_CHUNK_BYTES])),
- LARGE_CHUNK_BYTES,
- )
- .await
- .expect("a transport chunk larger than the reserved allowance");
- assert_eq!(LARGE_CHUNK_BYTES, large.as_bytes().len());
-
- let small_body = reqwest::Body::wrap_stream(futures_util::stream::iter(
- (0..SMALL_CHUNKS)
- .map(|_| Ok::<Bytes, std::io::Error>(Bytes::from(vec![9u8; SMALL_CHUNK_BYTES]))),
- ));
- let small = buffer(
- reqwest::Response::from(http::Response::new(small_body)),
- SMALL_CHUNK_BYTES * SMALL_CHUNKS,
- )
- .await
- .expect("packet-sized transport chunks");
- assert_eq!(SMALL_CHUNK_BYTES * SMALL_CHUNKS, small.as_bytes().len());
-}
-
-use fluxer_common::external_media_path::build_external_media_proxy_path;
diff --git a/fluxer_media_proxy/src/server/format_policy.rs b/fluxer_media_proxy/src/server/format_policy.rs
index 6395a610e..893c99c2b 100644
--- a/fluxer_media_proxy/src/server/format_policy.rs
+++ b/fluxer_media_proxy/src/server/format_policy.rs
@@ -247,390 +247,3 @@ pub(in crate::server) fn transform_response_content_type(
fallback_content_type
}
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use crate::server::params::{
- animated_param, explicit_output_format, filename_from_storage_key, url_filename,
- };
- use std::collections::HashMap;
-
- #[test]
- fn animated_gif_requests_downgrade_webp_output_to_gif() {
- assert_eq!(
- OutputFormat::APNG,
- effective_animated_image_output_format(
- Some(AssetExtension::Apng),
- OutputFormat::PNG,
- true
- )
- );
- assert_eq!(
- OutputFormat::PNG,
- effective_animated_image_output_format(
- Some(AssetExtension::Apng),
- OutputFormat::PNG,
- false
- )
- );
- assert_eq!(
- OutputFormat::GIF,
- effective_animated_image_output_format(
- Some(AssetExtension::Gif),
- OutputFormat::WebP,
- true
- )
- );
- assert_eq!(
- OutputFormat::WebP,
- effective_animated_image_output_format(
- Some(AssetExtension::Gif),
- OutputFormat::WebP,
- false
- )
- );
- assert_eq!(
- OutputFormat::WebP,
- effective_animated_image_output_format(
- Some(AssetExtension::Webp),
- OutputFormat::WebP,
- true
- )
- );
- assert_eq!(
- OutputFormat::PNG,
- effective_animated_image_output_format(
- Some(AssetExtension::Png),
- OutputFormat::PNG,
- true
- )
- );
- assert_eq!(
- OutputFormat::PNG,
- effective_animated_image_output_format(
- Some(AssetExtension::Png),
- OutputFormat::PNG,
- false
- )
- );
- assert!(animated_param(
- &HashMap::from([("animated".to_owned(), "true".to_owned())]),
- false
- ));
- assert!(!animated_param(
- &HashMap::from([("animatd".to_owned(), "true".to_owned())]),
- false
- ));
- }
-
- #[test]
- fn attachment_and_external_query_helpers_match_v1_edges() {
- for name in [
- "clip.mp4",
- "clip.m4v",
- "clip.webm",
- "clip.mov",
- "clip.ogv",
- "clip.mkv",
- "clip.3gp",
- "clip.avi",
- "clip.flv",
- "clip.ts",
- "clip.mpg",
- "clip.mpeg",
- "clip.wmv",
- ] {
- assert_eq!(None, image_extension_from_filename(name));
- }
- assert_eq!(
- AssetExtension::Gif,
- external_default_output_extension("welcome.png", "image/gif")
- );
- assert_eq!(
- AssetExtension::Png,
- external_default_output_extension("welcome.png", "application/octet-stream")
- );
- assert_eq!(
- "image/gif",
- transform_response_content_type(
- None,
- AssetExtension::Gif,
- OutputFormat::GIF,
- "image/gif"
- )
- );
- assert_eq!(
- "image/webp",
- transform_response_content_type(
- None,
- AssetExtension::Heic,
- OutputFormat::WebP,
- "image/heic"
- )
- );
- assert_eq!(
- "image/webp",
- transform_response_content_type(
- None,
- AssetExtension::Webp,
- OutputFormat::WebP,
- "image/svg+xml; charset=utf-8"
- )
- );
- assert_eq!(
- "image/gif",
- transform_response_content_type(
- Some(AssetExtension::Webp),
- AssetExtension::Webp,
- OutputFormat::GIF,
- "image/gif"
- )
- );
- assert_eq!(
- "file.png",
- url_filename("https://example.test/a/file.png?x=1#frag")
- );
- assert_eq!(
- "photo.png",
- filename_from_storage_key("attachments/123/456/photo.png")
- );
- assert_eq!(
- "file.bin",
- filename_from_storage_key("attachments/123/456/")
- );
- assert!(
- explicit_output_format(&HashMap::from([("format".to_owned(), "auto".to_owned())]))
- .is_err()
- );
- }
-
- #[test]
- fn unmeasurable_bytes_never_reuse_the_original() {
- assert!(
- !same_format_loaded_image_request_can_use_original_with_sniff(
- mime::sniff(b"GIF89a"),
- OriginalImageRequest {
- source_ext: Some(AssetExtension::Gif),
- explicit_out_ext: None,
- out_ext: OutputFormat::GIF,
- width: Some(300),
- height: None,
- has_quality: false,
- effort: None,
- animated: false,
- }
- )
- );
- assert!(
- !same_format_loaded_image_request_can_use_original_with_sniff(
- mime::sniff(b"GIF89a"),
- OriginalImageRequest {
- source_ext: Some(AssetExtension::Png),
- explicit_out_ext: None,
- out_ext: OutputFormat::PNG,
- width: Some(300),
- height: None,
- has_quality: false,
- effort: None,
- animated: true,
- }
- )
- );
- }
-
- #[test]
- fn same_format_gif_noop_requests_use_original_bytes() {
- let gif_header = b"GIF89a\x2c\x01\xe1\x00";
- assert!(
- same_format_loaded_image_request_can_use_original_with_sniff(
- mime::sniff(gif_header),
- OriginalImageRequest {
- source_ext: Some(AssetExtension::Gif),
- explicit_out_ext: None,
- out_ext: OutputFormat::GIF,
- width: Some(300),
- height: Some(225),
- has_quality: false,
- effort: None,
- animated: true,
- }
- )
- );
- assert!(
- same_format_loaded_image_request_can_use_original_with_sniff(
- mime::sniff(gif_header),
- OriginalImageRequest {
- source_ext: Some(AssetExtension::Gif),
- explicit_out_ext: None,
- out_ext: OutputFormat::GIF,
- width: Some(301),
- height: None,
- has_quality: false,
- effort: None,
- animated: true,
- }
- )
- );
- assert!(
- !same_format_loaded_image_request_can_use_original_with_sniff(
- mime::sniff(gif_header),
- OriginalImageRequest {
- source_ext: Some(AssetExtension::Gif),
- explicit_out_ext: None,
- out_ext: OutputFormat::GIF,
- width: Some(299),
- height: None,
- has_quality: false,
- effort: None,
- animated: true,
- }
- )
- );
- assert!(
- !same_format_loaded_image_request_can_use_original_with_sniff(
- mime::sniff(gif_header),
- OriginalImageRequest {
- source_ext: Some(AssetExtension::Gif),
- explicit_out_ext: Some(AssetExtension::Webp),
- out_ext: OutputFormat::WebP,
- width: None,
- height: None,
- has_quality: false,
- effort: None,
- animated: true,
- }
- )
- );
- assert!(
- !same_format_loaded_image_request_can_use_original_with_sniff(
- mime::sniff(gif_header),
- OriginalImageRequest {
- source_ext: Some(AssetExtension::Gif),
- explicit_out_ext: Some(AssetExtension::Webp),
- out_ext: OutputFormat::WebP,
- width: Some(300),
- height: None,
- has_quality: false,
- effort: None,
- animated: false,
- }
- )
- );
- assert!(
- !same_format_loaded_image_request_can_use_original_with_sniff(
- mime::sniff(gif_header),
- OriginalImageRequest {
- source_ext: Some(AssetExtension::Gif),
- explicit_out_ext: None,
- out_ext: OutputFormat::GIF,
- width: Some(300),
- height: None,
- has_quality: false,
- effort: EncodeEffort::parse_lenient("1"),
- animated: false,
- }
- )
- );
- }
-
- fn animated_vp8x_webp(width: u32, height: u32) -> Vec<u8> {
- let mut webp = vec![0_u8; 30];
- webp[0..4].copy_from_slice(b"RIFF");
- webp[8..12].copy_from_slice(b"WEBP");
- webp[12..16].copy_from_slice(b"VP8X");
- webp[16..20].copy_from_slice(&10_u32.to_le_bytes());
- webp[20] = 0x02;
- webp[24..27].copy_from_slice(&(width - 1).to_le_bytes()[..3]);
- webp[27..30].copy_from_slice(&(height - 1).to_le_bytes()[..3]);
- webp
- }
-
- #[test]
- fn still_requests_reuse_original_bytes_only_when_the_source_is_still() {
- let gif_header = b"GIF89a\x2c\x01\xe1\x00";
- assert!(!mime::sniff(gif_header).animated);
- assert!(
- same_format_loaded_image_request_can_use_original_with_sniff(
- mime::sniff(gif_header),
- OriginalImageRequest {
- source_ext: Some(AssetExtension::Gif),
- explicit_out_ext: None,
- out_ext: OutputFormat::GIF,
- width: Some(300),
- height: None,
- has_quality: false,
- effort: None,
- animated: false,
- }
- )
- );
- let animated_webp = animated_vp8x_webp(300, 225);
- assert!(mime::sniff(&animated_webp).animated);
- assert!(
- !same_format_loaded_image_request_can_use_original_with_sniff(
- mime::sniff(&animated_webp),
- OriginalImageRequest {
- source_ext: Some(AssetExtension::Webp),
- explicit_out_ext: None,
- out_ext: OutputFormat::WebP,
- width: Some(300),
- height: Some(225),
- has_quality: false,
- effort: None,
- animated: false,
- }
- )
- );
- assert!(
- !same_format_loaded_image_request_can_use_original_with_sniff(
- mime::sniff(&animated_webp),
- OriginalImageRequest {
- source_ext: Some(AssetExtension::Webp),
- explicit_out_ext: Some(AssetExtension::Webp),
- out_ext: OutputFormat::WebP,
- width: None,
- height: None,
- has_quality: false,
- effort: None,
- animated: false,
- }
- )
- );
- assert!(
- same_format_loaded_image_request_can_use_original_with_sniff(
- mime::sniff(&animated_webp),
- OriginalImageRequest {
- source_ext: Some(AssetExtension::Webp),
- explicit_out_ext: Some(AssetExtension::Webp),
- out_ext: OutputFormat::WebP,
- width: None,
- height: None,
- has_quality: false,
- effort: None,
- animated: true,
- }
- )
- );
- }
-
- #[test]
- fn a_sniffed_mime_disagreeing_with_the_source_extension_still_uses_original_bytes() {
- let gif_header = b"GIF89a\x2c\x01\xe1\x00";
- assert!(
- same_format_loaded_image_request_can_use_original_with_sniff(
- mime::sniff(gif_header),
- OriginalImageRequest {
- source_ext: Some(AssetExtension::Png),
- explicit_out_ext: None,
- out_ext: OutputFormat::PNG,
- width: Some(300),
- height: None,
- has_quality: false,
- effort: None,
- animated: true,
- }
- )
- );
- }
-}
diff --git a/fluxer_media_proxy/src/server/media_operations/input.rs b/fluxer_media_proxy/src/server/media_operations/input.rs
index 73e2b43c1..05dfaa280 100644
--- a/fluxer_media_proxy/src/server/media_operations/input.rs
+++ b/fluxer_media_proxy/src/server/media_operations/input.rs
@@ -276,220 +276,3 @@ async fn read_own_object(
pub(super) fn retained_input_bytes(data: BudgetedBytes) -> Bytes {
Bytes::from_owner(data)
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use crate::{
- config::Config,
- storage::tests::{FakeObject, FakeS3, fake_s3},
- };
- use axum::http::{Method, StatusCode, header};
-
- const BUFFER_LIMIT: MediaInputLimit = MediaInputLimit {
- encoded_bytes: 1024,
- decoded_bytes: 1024,
- };
-
- fn local_storage_app(root: &std::path::Path) -> AppState {
- let spool_dir = root.join("spool");
- std::fs::create_dir(&spool_dir).unwrap();
- let cfg = Config::load_from_iter([
- ("FLUXER_MEDIA_PROXY_SECRET_KEY", "secret".to_owned()),
- ("FLUXER_MEDIA_PROXY_STORAGE_BACKEND", "local".to_owned()),
- (
- "FLUXER_MEDIA_PROXY_STORAGE_ROOT",
- root.display().to_string(),
- ),
- (
- "FLUXER_MEDIA_PROXY_UPLOAD_RELAY_SPOOL_DIR",
- spool_dir.display().to_string(),
- ),
- ])
- .expect("test config");
- AppState::for_tests(cfg)
- }
-
- #[tokio::test]
- async fn a_stored_input_larger_than_the_buffer_limit_is_read_from_a_spooled_copy() {
- let tmp = tempfile::tempdir().unwrap();
- let root = tmp.path().canonicalize().unwrap();
- let app = local_storage_app(&root);
- let uploads = app.cfg.storage.bucket_uploads.clone();
- let cdn = app.cfg.storage.bucket_cdn.clone();
- let mp4 = crate::test_fixtures::fixture_h264_mp4();
- assert!(mp4.len() > BUFFER_LIMIT.decoded_bytes);
- for bucket in [&uploads, &cdn] {
- app.store
- .write_object(bucket, "big.mp4", &mp4, "video/mp4")
- .await
- .unwrap();
- }
-
- let upload = load_media_input(
- &app,
- MediaInput::Upload {
- upload_filename: "big.mp4".to_owned(),
- filename: None,
- },
- BUFFER_LIMIT,
- )
- .await
- .unwrap_or_else(|failure| panic!("upload input failed: {}", failure.code()));
- let stored = load_media_input(
- &app,
- MediaInput::Storage {
- bucket: cdn,
- key: "big.mp4".to_owned(),
- filename: Some("named.mp4".to_owned()),
- },
- BUFFER_LIMIT,
- )
- .await
- .unwrap_or_else(|failure| panic!("stored input failed: {}", failure.code()));
-
- assert_eq!(&mp4[..], &upload.data[..]);
- assert_eq!("big.mp4", upload.filename);
- assert_eq!(&mp4[..], &stored.data[..]);
- assert_eq!("named.mp4", stored.filename);
- let metadata =
- crate::media_process::probe_av_metadata(&upload.data, 0, &app.media.limits(), None)
- .unwrap();
- assert!(metadata.probe.has_video);
- assert!(
- std::fs::read_dir(root.join("spool"))
- .unwrap()
- .next()
- .is_none(),
- "a spooled copy is never visible in the spool directory"
- );
- }
-
- fn upload_input(key: &str) -> MediaInput {
- MediaInput::Upload {
- upload_filename: key.to_owned(),
- filename: None,
- }
- }
-
- fn body_reads(fake: &FakeS3) -> Vec<Option<String>> {
- fake.requests()
- .into_iter()
- .filter(|(method, _, _, _)| *method == Method::GET)
- .map(|(_, _, headers, _)| {
- headers
- .get(header::RANGE)
- .and_then(|value| value.to_str().ok())
- .map(str::to_owned)
- })
- .collect()
- }
-
- #[tokio::test]
- async fn object_storage_spools_a_video_larger_than_the_buffer_limit() {
- let fake = fake_s3().await;
- let mp4 = crate::test_fixtures::fixture_h264_mp4();
- fake.put_object(
- "uploads/big.mp4",
- FakeObject {
- body: mp4.clone(),
- content_type: Some("application/octet-stream".to_owned()),
- ..FakeObject::default()
- },
- );
- let tmp = tempfile::tempdir().unwrap();
- let app = AppState::for_tests(fake.config(tmp.path()));
-
- let loaded = load_media_input(&app, upload_input("big.mp4"), BUFFER_LIMIT)
- .await
- .unwrap_or_else(|failure| panic!("upload input failed: {}", failure.code()));
-
- assert_eq!(&mp4[..], &loaded.data[..]);
- assert_eq!(
- vec![None, Some("bytes=0-8191".to_owned()), None],
- body_reads(&fake)
- );
- }
-
- #[tokio::test]
- async fn an_oversized_input_that_is_not_audio_or_video_is_refused_from_its_prefix() {
- let fake = fake_s3().await;
- let mut png = crate::test_fixtures::synthetic_png(8, 8);
- png.resize(BUFFER_LIMIT.decoded_bytes + 1, 0);
- for (key, body) in [
- ("uploads/huge.png", png),
- (
- "uploads/notes.mp4",
- vec![b'a'; BUFFER_LIMIT.decoded_bytes + 1],
- ),
- ] {
- fake.put_object(
- key,
- FakeObject {
- body,
- ..FakeObject::default()
- },
- );
- }
- let tmp = tempfile::tempdir().unwrap();
- let app = AppState::for_tests(fake.config(tmp.path()));
-
- for key in ["huge.png", "notes.mp4"] {
- let failure = load_media_input(&app, upload_input(key), BUFFER_LIMIT)
- .await
- .err()
- .expect("only audio and video are read past the buffer limit");
- assert_eq!("metadata_upload_read", failure.code(), "{key}");
- }
-
- assert_eq!(
- vec![
- None,
- Some("bytes=0-8191".to_owned()),
- None,
- Some("bytes=0-8191".to_owned())
- ],
- body_reads(&fake)
- );
- }
-
- #[tokio::test]
- async fn a_stream_without_a_declared_length_is_not_spooled() {
- let tmp = tempfile::tempdir().unwrap();
- let app = local_storage_app(&tmp.path().canonicalize().unwrap());
- let object = StreamObject {
- body: Body::from_stream(futures_util::stream::iter([Ok::<_, std::io::Error>(
- Bytes::from_static(b"payload"),
- )])),
- status: StatusCode::OK,
- content_length: None,
- content_type: "video/mp4".to_owned(),
- byte_range: None,
- total_length: None,
- };
-
- let refused = spool_stream(&app, object).await;
-
- assert!(matches!(refused, Err(StorageError::StreamTooLong)));
- }
-
- #[tokio::test]
- async fn a_missing_stored_input_is_reported_as_a_read_failure() {
- let tmp = tempfile::tempdir().unwrap();
- let app = local_storage_app(&tmp.path().canonicalize().unwrap());
-
- let failure = load_media_input(
- &app,
- MediaInput::Upload {
- upload_filename: "missing.mp4".to_owned(),
- filename: None,
- },
- BUFFER_LIMIT,
- )
- .await
- .err()
- .expect("a missing upload cannot load");
-
- assert_eq!("metadata_upload_read", failure.code());
- }
-}
diff --git a/fluxer_media_proxy/src/server/media_operations/mod.rs b/fluxer_media_proxy/src/server/media_operations/mod.rs
index b6cb746cd..97ae5ab24 100644
--- a/fluxer_media_proxy/src/server/media_operations/mod.rs
+++ b/fluxer_media_proxy/src/server/media_operations/mod.rs
@@ -131,93 +131,3 @@ fn replace_image_extension(filename: &str, ext: AssetExtension) -> String {
None => format!("{}.{}", filename, ext.name()),
}
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use crate::byte_budget::{BudgetedBytes, ByteBudget};
- use crate::config::Config;
- use std::sync::Arc;
-
- fn test_app_state() -> Arc<AppState> {
- let cfg = Config::load_from_iter([("FLUXER_MEDIA_PROXY_SECRET_KEY", "secret")])
- .expect("test config");
- Arc::new(AppState::for_tests(cfg))
- }
-
- #[test]
- fn metadata_base64_svg_detection_uses_bytes_or_filename() {
- let svg_bytes = LoadedMediaInput {
- data: Bytes::from_static(br#"<svg xmlns="http://www.w3.org/2000/svg"></svg>"#),
- filename: "upload.bin".to_owned(),
- };
- assert!(metadata_input_is_svg(&svg_bytes));
-
- let svg_filename = LoadedMediaInput {
- data: Bytes::from_static(b"not svg"),
- filename: "icons/logo.svg".to_owned(),
- };
- assert!(metadata_input_is_svg(&svg_filename));
-
- let png_filename = LoadedMediaInput {
- data: Bytes::from_static(b"not svg"),
- filename: "icons/logo.png".to_owned(),
- };
- assert!(!metadata_input_is_svg(&png_filename));
- }
-
- #[test]
- fn replace_image_extension_only_changes_last_path_segment() {
- assert_eq!(
- "avatars/user.icon.webp",
- replace_image_extension("avatars/user.icon.svg", AssetExtension::Webp)
- );
- assert_eq!(
- "avatars.v1/user.webp",
- replace_image_extension("avatars.v1/user", AssetExtension::Webp)
- );
- }
-
- #[tokio::test]
- async fn metadata_base64_svg_rasterizes_to_webp_bytes() {
- let app = test_app_state();
- let input = LoadedMediaInput {
- data: Bytes::from_static(
- br#"<svg xmlns="http://www.w3.org/2000/svg" width="8" height="8"><rect width="8" height="8" fill="red"/></svg>"#,
- ),
- filename: "icons/logo.svg".to_owned(),
- };
- let raster = match rasterize_metadata_svg(&app, input).await {
- Ok(raster) => raster,
- Err(failure) => panic!("unexpected failure {}", failure.code()),
- };
-
- assert_eq!("icons/logo.webp", raster.filename);
- assert_eq!("image/webp", mime::sniff(&raster.data).mime);
- }
-
- #[test]
- fn an_external_media_input_keeps_owing_the_shared_buffer_budget() {
- let budget = ByteBudget::new(16);
- let reservation = budget.try_reserve(16).expect("reserve at limit");
- let data = input::retained_input_bytes(BudgetedBytes::budgeted(
- Bytes::from_static(b"payload"),
- reservation,
- ));
-
- assert_eq!(b"payload", data.as_ref());
- assert!(
- budget.try_reserve(1).is_none(),
- "a resident external input still owes the external buffer budget"
- );
-
- drop(data);
- assert_eq!(
- 16,
- budget
- .try_reserve(16)
- .expect("dropping the input releases its reservation")
- .amount()
- );
- }
-}
diff --git a/fluxer_media_proxy/src/server/middleware.rs b/fluxer_media_proxy/src/server/middleware.rs
index 729938dd5..5a7d627ca 100644
--- a/fluxer_media_proxy/src/server/middleware.rs
+++ b/fluxer_media_proxy/src/server/middleware.rs
@@ -226,58 +226,6 @@ mod tests {
headers.get(name).map(|value| value.to_str().unwrap())
}
- #[tokio::test]
- async fn mp_mode_serves_javascript_as_plain_text() {
- for content_type in [
- "text/javascript",
- "application/javascript; charset=utf-8",
- "image/png, text/javascript",
- ] {
- let headers = headers_for(DeploymentMode::Mp, Some(content_type)).await;
- assert_eq!(
- Some(INERT),
- header_text(&headers, header::CONTENT_TYPE),
- "{content_type}"
- );
- assert_eq!(
- Some("nosniff"),
- header_text(&headers, header::X_CONTENT_TYPE_OPTIONS),
- "{content_type}"
- );
- }
- }
-
- #[tokio::test]
- async fn mp_mode_serves_javascript_that_browsers_parse_past_junk_as_plain_text() {
- for content_type in BROWSER_PARSED_JAVASCRIPT {
- for status in [StatusCode::OK, StatusCode::PARTIAL_CONTENT] {
- let headers =
- headers_with_status(DeploymentMode::Mp, status, Some(content_type)).await;
- assert_eq!(
- Some(INERT),
- header_text(&headers, header::CONTENT_TYPE),
- "{status} {content_type:?}"
- );
- assert_eq!(1, headers.get_all(header::CONTENT_TYPE).iter().count());
- }
- }
- }
-
- #[tokio::test]
- async fn static_mode_serves_javascript_unchanged() {
- for content_type in ["text/javascript", "application/javascript; charset=utf-8"]
- .into_iter()
- .chain(BROWSER_PARSED_JAVASCRIPT)
- {
- let headers = headers_for(DeploymentMode::Static, Some(content_type)).await;
- assert_eq!(
- Some(content_type),
- header_text(&headers, header::CONTENT_TYPE),
- "{content_type:?}"
- );
- }
- }
-
#[tokio::test]
async fn every_mode_that_serves_stored_bytes_neutralizes_javascript() {
for mode in [
@@ -299,89 +247,6 @@ mod tests {
}
}
- #[tokio::test]
- async fn non_javascript_content_types_pass_through_every_mode() {
- for mode in [
- DeploymentMode::Mp,
- DeploymentMode::Static,
- DeploymentMode::Upload,
- DeploymentMode::Relay,
- ] {
- for content_type in [
- "text/css; charset=utf-8",
- "text/css",
- "video/mp2t",
- "video/mp4",
- "image/png",
- "image/svg+xml",
- "application/json",
- "text/plain",
- "text/javascriptx",
- "text/javascript1.6",
- ] {
- let headers = headers_for(mode, Some(content_type)).await;
- assert_eq!(
- Some(content_type),
- header_text(&headers, header::CONTENT_TYPE),
- "{mode:?} {content_type}"
- );
- }
- }
- }
-
- #[tokio::test]
- async fn mp_mode_types_an_untyped_full_or_partial_response_as_plain_text() {
- for status in [StatusCode::OK, StatusCode::PARTIAL_CONTENT] {
- for content_type in [None, Some(""), Some(" ")] {
- let headers = headers_with_status(DeploymentMode::Mp, status, content_type).await;
- assert_eq!(
- Some(INERT),
- header_text(&headers, header::CONTENT_TYPE),
- "{status} {content_type:?}"
- );
- assert_eq!(
- Some("nosniff"),
- header_text(&headers, header::X_CONTENT_TYPE_OPTIONS),
- "{status} {content_type:?}"
- );
- }
- }
- }
-
- #[tokio::test]
- async fn untyped_responses_of_other_statuses_or_modes_stay_untyped() {
- for status in [
- StatusCode::NO_CONTENT,
- StatusCode::NOT_MODIFIED,
- StatusCode::NOT_FOUND,
- StatusCode::METHOD_NOT_ALLOWED,
- StatusCode::RANGE_NOT_SATISFIABLE,
- ] {
- let headers = headers_with_status(DeploymentMode::Mp, status, None).await;
- assert_eq!(None, headers.get(header::CONTENT_TYPE), "{status}");
- }
- for status in [
- StatusCode::OK,
- StatusCode::PARTIAL_CONTENT,
- StatusCode::RANGE_NOT_SATISFIABLE,
- ] {
- let headers = headers_with_status(DeploymentMode::Static, status, None).await;
- assert_eq!(None, headers.get(header::CONTENT_TYPE), "{status}");
- }
- for mode in [DeploymentMode::Upload, DeploymentMode::Relay] {
- let headers = headers_with_status(mode, StatusCode::RANGE_NOT_SATISFIABLE, None).await;
- assert_eq!(None, headers.get(header::CONTENT_TYPE), "{mode:?}");
- for status in [StatusCode::OK, StatusCode::PARTIAL_CONTENT] {
- let headers = headers_with_status(mode, status, None).await;
- assert_eq!(
- Some(INERT),
- header_text(&headers, header::CONTENT_TYPE),
- "{mode:?} {status}"
- );
- }
- }
- }
-
fn transport_stream_object(content_type: &str) -> FakeObject {
let mut body = vec![0_u8; 376];
body[0] = 0x47;
@@ -541,71 +406,4 @@ mod tests {
header_text(theme.headers(), header::CONTENT_TYPE)
);
}
-
- async fn robots_header_for(mode: DeploymentMode) -> Option<String> {
- let router = Router::new()
- .route(
- "/probe",
- get(|| async {
- let mut response = Response::new(Body::empty());
- http_headers::add_media_headers(response.headers_mut(), 0, "text/plain", None);
- response
- }),
- )
- .layer(axum::middleware::from_fn_with_state(
- mode,
- add_security_header_middleware,
- ));
- let response = tower::ServiceExt::oneshot(
- router,
- Request::builder()
- .uri("/probe")
- .body(Body::empty())
- .unwrap(),
- )
- .await
- .unwrap();
- response
- .headers()
- .get("X-Robots-Tag")
- .map(|v| v.to_str().unwrap().to_owned())
- }
-
- #[tokio::test]
- async fn static_mode_does_not_set_robots_tag() {
- assert_eq!(robots_header_for(DeploymentMode::Static).await, None);
- }
-
- #[tokio::test]
- async fn media_and_upload_modes_still_set_robots_tag() {
- assert_eq!(
- robots_header_for(DeploymentMode::Mp).await.as_deref(),
- Some(http_headers::ROBOTS)
- );
- assert_eq!(
- robots_header_for(DeploymentMode::Upload).await.as_deref(),
- Some(http_headers::ROBOTS)
- );
- }
-
- #[tokio::test]
- async fn request_drain_waits_until_every_response_body_is_dropped() {
- let drain = HttpRequestDrain::new();
- let router = Router::new().route("/probe", get(|| async { "OK" })).layer(
- axum::middleware::from_fn_with_state(drain.clone(), track_active_request),
- );
- let response = tower::ServiceExt::oneshot(
- router,
- Request::builder()
- .uri("/probe")
- .body(Body::empty())
- .unwrap(),
- )
- .await
- .unwrap();
- assert_eq!(drain.active_requests(), 1);
- drop(response);
- drain.wait_for_requests_drained().await;
- assert_eq!(drain.active_requests(), 0);
- }
}
diff --git a/fluxer_media_proxy/src/server/params.rs b/fluxer_media_proxy/src/server/params.rs
index c2964203a..1224dfa0c 100644
--- a/fluxer_media_proxy/src/server/params.rs
+++ b/fluxer_media_proxy/src/server/params.rs
@@ -113,62 +113,3 @@ pub(in crate::server) fn url_filename(url: &str) -> String {
pub(in crate::server) fn extension_of(filename: &str) -> Option<&str> {
filename.rsplit_once('.').map(|(_, ext)| ext)
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn asset_manual_format_override_is_v1_compatible() {
- assert_eq!(
- Some(AssetExtension::Webp),
- asset_manual_format_override(&HashMap::new(), AssetExtension::Webp)
- );
- assert_eq!(
- None,
- asset_manual_format_override(
- &HashMap::from([("format".to_owned(), "auto".to_owned())]),
- AssetExtension::Webp
- )
- );
- assert_eq!(
- Some(AssetExtension::Png),
- asset_manual_format_override(
- &HashMap::from([("fmt".to_owned(), "png".to_owned())]),
- AssetExtension::Webp
- )
- );
- assert_eq!(
- Some(AssetExtension::Webp),
- asset_manual_format_override(
- &HashMap::from([("format".to_owned(), "svg".to_owned())]),
- AssetExtension::Webp
- )
- );
- }
-
- #[test]
- fn an_empty_last_url_segment_keeps_the_old_era_file_bin_fallback() {
- assert_eq!("file.bin", url_filename("https://example.test/"));
- assert_eq!("file.bin", url_filename("https://example.test/a/b/"));
- assert_eq!("file.bin", url_filename("https://example.test/a/?x=1"));
- assert_eq!("file.bin", url_filename("https://example.test/a/#frag"));
- }
-
- #[test]
- fn an_external_url_filename_is_bounded_to_the_disposition_budget() {
- assert_eq!(
- "file.png",
- url_filename("https://example.test/a/file.png?x=1#frag")
- );
- assert_eq!("file.bin", url_filename("https://example.test/"));
- let long = format!(
- "https://example.test/{}",
- "\u{e9}".repeat(CONTENT_DISPOSITION_FILENAME_BYTES_MAX)
- );
- let bounded = url_filename(&long);
- assert!(bounded.len() <= CONTENT_DISPOSITION_FILENAME_BYTES_MAX);
- assert!(bounded.starts_with('\u{e9}'));
- assert!(bounded.ends_with('\u{e9}'));
- }
-}
diff --git a/fluxer_media_proxy/src/server/response/error.rs b/fluxer_media_proxy/src/server/response/error.rs
index 812c8a7e4..2be83a6ee 100644
--- a/fluxer_media_proxy/src/server/response/error.rs
+++ b/fluxer_media_proxy/src/server/response/error.rs
@@ -99,101 +99,3 @@ pub(in crate::server) fn json_response(status: StatusCode, body: String) -> Resp
}
response
}
-
-#[cfg(test)]
-use crate::test_fixtures::CapturedLog;
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use crate::{
- metrics::Metrics,
- request_log::{RequestId, trace_public_request},
- };
- use axum::http::{HeaderMap, Method};
-
- #[test]
- fn text_responses_set_nosniff_header() {
- let response = text(StatusCode::NOT_FOUND, "Not Found");
- assert_eq!(
- "nosniff",
- response
- .headers()
- .get(header::X_CONTENT_TYPE_OPTIONS)
- .unwrap()
- .to_str()
- .unwrap()
- );
- assert_eq!(
- http_headers::STRICT_TRANSPORT_SECURITY,
- response
- .headers()
- .get("strict-transport-security")
- .unwrap()
- .to_str()
- .unwrap()
- );
- assert!(
- response
- .headers()
- .contains_key(header::CONTENT_SECURITY_POLICY)
- );
- assert!(response.headers().contains_key("permissions-policy"));
- }
-
- #[test]
- fn error_responses_declare_an_explicit_no_store_policy() {
- for status in [
- StatusCode::NOT_FOUND,
- StatusCode::BAD_GATEWAY,
- StatusCode::INTERNAL_SERVER_ERROR,
- ] {
- for response in [text(status, "nope"), json_response(status, "{}".to_owned())] {
- assert_eq!(
- ERROR_CACHE_CONTROL,
- response
- .headers()
- .get(header::CACHE_CONTROL)
- .expect("an error response declares a cache policy"),
- "status {status} must not be cacheable"
- );
- }
- }
- }
-
- #[tokio::test]
- async fn a_very_long_storage_key_cannot_bloat_the_logged_source() {
- let key = format!("attachments/1/2/{}.png", "k".repeat(4096));
- let captured = CapturedLog::default();
- let subscriber = tracing_subscriber::fmt()
- .with_writer(captured.clone())
- .with_ansi(false)
- .with_max_level(tracing::Level::TRACE)
- .finish();
- let response = {
- let _guard = tracing::subscriber::set_default(subscriber);
- let metrics = Metrics::new();
- trace_public_request(
- metrics.request().as_ref(),
- RequestId::generate(),
- Method::GET,
- "/attachments/1/2/missing.png",
- &HeaderMap::new(),
- async { storage_error_response(&key, StorageError::NotFound) },
- )
- .await
- };
- assert_eq!(StatusCode::NOT_FOUND, response.status());
- let line = captured.text();
- assert!(line.contains("reason=\"storage_error\""), "{line}");
- assert!(!line.contains(&key), "{line}");
- let source = line
- .split("source=")
- .nth(1)
- .expect("a failure log line has a source field")
- .trim_end();
- assert_eq!(513, source.len(), "{source}");
- assert!(source.ends_with('~'), "{source}");
- assert!(source.contains("key=attachments/1/2/kkk"), "{source}");
- }
-}
diff --git a/fluxer_media_proxy/src/server/response/mod.rs b/fluxer_media_proxy/src/server/response/mod.rs
index 5f69ac57d..fb57c1478 100644
--- a/fluxer_media_proxy/src/server/response/mod.rs
+++ b/fluxer_media_proxy/src/server/response/mod.rs
@@ -166,65 +166,3 @@ fn download_filename_for_content_type<'a>(
Cow::Owned(format!("{stem}.{ext}"))
}
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use crate::server::params::{filename_from_storage_key, url_filename};
-
- fn disposition_string(
- content_type: &str,
- requested_download: bool,
- filename: Option<&str>,
- ) -> String {
- content_disposition_header(content_type, requested_download, filename)
- .to_str()
- .expect("content disposition is ascii")
- .to_owned()
- }
-
- #[test]
- fn explicit_download_disposition_uses_response_image_extension() {
- assert_eq!(
- "attachment; filename=\"welcome.gif\"",
- disposition_string("image/gif", true, Some("welcome.png"))
- );
- assert_eq!(
- "attachment; filename=\"welcome.gif\"",
- disposition_string("image/gif", true, Some("welcome"))
- );
- assert_eq!(
- "attachment; filename=\"photo.jpg\"",
- disposition_string("image/jpeg", true, Some("photo.jpg"))
- );
- assert_eq!(
- "attachment; filename=\"photo.jpg\"",
- disposition_string(
- "image/jpeg",
- true,
- Some(filename_from_storage_key("attachments/123/456/photo.jpg"))
- )
- );
- assert_eq!(
- "inline; filename=\"welcome.png\"",
- disposition_string("image/gif", false, Some("welcome.png"))
- );
- }
-
- #[test]
- fn a_long_external_filename_keeps_its_leading_bytes_in_the_header() {
- let url = format!(
- "https://example.test/lead{}tail.png",
- "a".repeat(disposition::CONTENT_DISPOSITION_FILENAME_BYTES_MAX)
- );
- let filename = url_filename(&url);
- let value = disposition_string("image/png", false, Some(&filename));
- assert!(
- value.starts_with("inline; filename=\"lead"),
- "{}",
- &value[..32]
- );
- assert!(!value.contains("tail.png"));
- assert!(value.len() <= disposition::PUBLIC_MEDIA_MAX_HEADER_VALUE_BYTES);
- }
-}
diff --git a/fluxer_media_proxy/src/server/routes/internal.rs b/fluxer_media_proxy/src/server/routes/internal.rs
index 926302619..8400b2a7d 100644
--- a/fluxer_media_proxy/src/server/routes/internal.rs
+++ b/fluxer_media_proxy/src/server/routes/internal.rs
@@ -369,21 +369,10 @@ async fn read_limited_body(request: Request<Body>) -> Result<Bytes, StatusCode>
#[cfg(test)]
mod tests {
use super::*;
- use crate::{
- config::Config,
- storage::tests::{FakeObject, fake_s3},
- test_fixtures::synthetic_png,
- };
+ use crate::config::Config;
use axum::http::HeaderValue;
use http_body_util::BodyExt as _;
use std::path::Path;
- use tokio::sync::mpsc;
-
- fn test_app_state() -> Arc<AppState> {
- let cfg = Config::load_from_iter([("FLUXER_MEDIA_PROXY_SECRET_KEY", "secret")])
- .expect("test config");
- Arc::new(AppState::for_tests(cfg))
- }
fn local_storage_config(storage_root: &Path) -> Config {
Config::load_from_iter([
@@ -413,16 +402,6 @@ mod tests {
.expect("stored upload");
}
- async fn sniff_upload(app: &Arc<AppState>, key: &str) -> Response {
- let body = serde_json::json!({ "type": "upload", "upload_filename": key }).to_string();
- sniff_handler(
- State(Arc::clone(app)),
- authorized_headers(),
- json_request(body),
- )
- .await
- }
-
fn mpeg_ts_segment() -> Vec<u8> {
let mut segment = vec![0u8; 564];
for offset in [0, 188, 376] {
@@ -431,12 +410,6 @@ mod tests {
segment
}
- fn png_followed_by_zeros() -> Vec<u8> {
- let mut upload = synthetic_png(8, 8);
- upload.extend(std::iter::repeat_n(0u8, 64 * 1024));
- upload
- }
-
fn authorized_headers() -> HeaderMap {
let mut headers = HeaderMap::new();
headers.insert(
@@ -453,24 +426,6 @@ mod tests {
.expect("json request")
}
- fn oversized_request() -> Request<Body> {
- Request::builder()
- .method(Method::POST)
- .body(Body::from(Bytes::from(vec![
- 0u8;
- constants::MAX_INTERNAL_REQUEST_BODY_BYTES
- + 1
- ])))
- .expect("oversized request")
- }
-
- fn saturated_transform_app(storage_root: &Path) -> Arc<AppState> {
- let mut cfg = local_storage_config(storage_root);
- cfg.media.max_native_transforms = 1;
- cfg.media.worker_queue_capacity = 0;
- Arc::new(AppState::for_tests(cfg))
- }
-
async fn response_body(response: Response) -> String {
let bytes = response
.into_body()
@@ -488,229 +443,6 @@ mod tests {
assert!(!check_internal_auth(&headers, "other"));
}
- #[tokio::test]
- async fn frames_returns_empty_frames_when_extraction_fails() {
- let body = format!(
- r#"{{"version":2,"type":"base64","base64":"{}"}}"#,
- general_purpose::STANDARD.encode(b"definitely not a video")
- );
- let response = frames_handler(
- State(test_app_state()),
- authorized_headers(),
- json_request(body),
- )
- .await;
-
- assert_eq!(StatusCode::OK, response.status());
- assert_eq!(
- "application/json",
- response
- .headers()
- .get(header::CONTENT_TYPE)
- .expect("content type")
- .to_str()
- .expect("ascii content type")
- );
- assert_eq!("{\"frames\":[]}", response_body(response).await);
- }
-
- #[tokio::test]
- async fn metadata_reads_an_own_attachment_url_whatever_its_signature() {
- use fluxer_common::attachment_url_signature::{
- ATTACHMENT_URL_TTL_SECS, UrlKind, sign, with_signature,
- };
- use std::time::{SystemTime, UNIX_EPOCH};
-
- const SECRET: [u8; 32] = [5u8; 32];
- const ENDPOINT: &str = "https://media.test";
- let tmp = tempfile::tempdir().expect("temp storage root");
- let storage_root = tmp.path().canonicalize().expect("canonical storage root");
- let app = Arc::new(AppState::for_tests(
- Config::load_from_iter([
- (
- "FLUXER_MEDIA_PROXY_SECRET_KEY".to_owned(),
- "secret".to_owned(),
- ),
- (
- "FLUXER_MEDIA_PROXY_STORAGE_ROOT".to_owned(),
- storage_root.display().to_string(),
- ),
- (
- "FLUXER_MEDIA_PROXY_PUBLIC_ENDPOINT".to_owned(),
- ENDPOINT.to_owned(),
- ),
- (
- "FLUXER_MEDIA_PROXY_ATTACHMENT_SIGNATURE_MODE".to_owned(),
- "enforce".to_owned(),
- ),
- (
- "FLUXER_MEDIA_PROXY_ATTACHMENT_URL_SECRETS_BASE64".to_owned(),
- general_purpose::STANDARD.encode(SECRET),
- ),
- ])
- .expect("metadata signature config"),
- ));
- let key = "attachments/1/2/cat.png";
- app.store
- .write_object(
- &app.cfg.storage.bucket_cdn,
- key,
- &synthetic_png(4, 4),
- "image/png",
- )
- .await
- .expect("stored attachment");
- let unsigned = format!("{ENDPOINT}/{key}");
- let now = SystemTime::now()
- .duration_since(UNIX_EPOCH)
- .expect("the clock is after the unix epoch")
- .as_secs();
- let expires = now - 1;
- let issued = expires - ATTACHMENT_URL_TTL_SECS;
- let expired = format!(
- "{unsigned}?ex={expires:08x}&is={issued:08x}&hm={}",
- sign(key, expires, issued, UrlKind::Ordinary, &SECRET)
- );
-
- for spelling in [
- unsigned.clone(),
- expired,
- with_signature(&unsigned, key, now, now, &SECRET),
- ] {
- let body = serde_json::json!({
- "version": 2,
- "type": "external",
- "nsfw": "allow",
- "with_base64": true,
- "url": spelling,
- })
- .to_string();
- let response = metadata_handler(
- State(Arc::clone(&app)),
- authorized_headers(),
- json_request(body),
- )
- .await;
- assert_eq!(StatusCode::OK, response.status(), "{spelling}");
- assert!(response_body(response).await.contains("\"width\":4"));
- }
- }
-
- #[tokio::test]
- async fn metadata_requires_version_two_exactly() {
- let encoded = general_purpose::STANDARD.encode(synthetic_png(4, 4));
- let app = test_app_state();
-
- for version in ["1", "3", "null"] {
- let body = format!(
- r#"{{"version":{version},"type":"base64","nsfw":"allow","base64":"{encoded}"}}"#
- );
- let response = metadata_handler(
- State(Arc::clone(&app)),
- authorized_headers(),
- json_request(body),
- )
- .await;
- assert_eq!(
- StatusCode::BAD_REQUEST,
- response.status(),
- "version {version} must be rejected"
- );
- }
-
- let missing_version = format!(r#"{{"type":"base64","nsfw":"allow","base64":"{encoded}"}}"#);
- let response = metadata_handler(
- State(Arc::clone(&app)),
- authorized_headers(),
- json_request(missing_version),
- )
- .await;
- assert_eq!(StatusCode::BAD_REQUEST, response.status());
-
- let body =
- format!(r#"{{"version":2,"type":"base64","nsfw":"allow","base64":"{encoded}"}}"#);
- let response = metadata_handler(State(app), authorized_headers(), json_request(body)).await;
- assert_eq!(StatusCode::OK, response.status());
- assert!(response_body(response).await.contains("\"width\":4"));
- }
-
- #[tokio::test]
- async fn thumbnail_answers_an_oversized_body_with_413_payload_too_large() {
- let response = thumbnail_handler(
- State(test_app_state()),
- authorized_headers(),
- oversized_request(),
- )
- .await;
-
- assert_eq!(StatusCode::PAYLOAD_TOO_LARGE, response.status());
- assert_eq!("Payload Too Large", response_body(response).await);
- }
-
- #[tokio::test]
- async fn frames_answers_an_oversized_body_with_413_payload_too_large() {
- let response = frames_handler(
- State(test_app_state()),
- authorized_headers(),
- oversized_request(),
- )
- .await;
-
- assert_eq!(StatusCode::PAYLOAD_TOO_LARGE, response.status());
- assert_eq!("Payload Too Large", response_body(response).await);
- }
-
- #[tokio::test]
- async fn thumbnail_answers_a_full_admission_pool_with_504_gateway_timeout() {
- let tmp = tempfile::tempdir().expect("temp storage root");
- let storage_root = tmp.path().canonicalize().expect("canonical storage root");
- let app = saturated_transform_app(&storage_root);
- app.store
- .write_object(
- &app.cfg.storage.bucket_uploads,
- "thumb.png",
- &synthetic_png(8, 8),
- "image/png",
- )
- .await
- .expect("stored upload");
- let (release, mut released) = mpsc::channel::<()>(1);
- let (started, mut has_started) = mpsc::channel::<()>(1);
- let holder = Arc::clone(&app);
- let held = tokio::spawn(async move {
- holder
- .media
- .transforms()
- .tasks()
- .run_native(
- None,
- || Ok(media_process::NativeCost::Bytes(0)),
- move || {
- let _ = started.blocking_send(());
- let _ = released.blocking_recv();
- Ok(())
- },
- )
- .await
- });
- has_started
- .recv()
- .await
- .expect("the held transform started");
-
- let response = thumbnail_handler(
- State(Arc::clone(&app)),
- authorized_headers(),
- json_request(r#"{"upload_filename":"thumb.png"}"#.to_owned()),
- )
- .await;
-
- assert_eq!(StatusCode::GATEWAY_TIMEOUT, response.status());
- assert_eq!("Gateway Timeout", response_body(response).await);
- drop(release);
- held.await.expect("held task").expect("held work");
- }
-
#[tokio::test]
async fn sniff_requires_the_internal_bearer_secret() {
let (_tmp, app) = local_storage_app();
@@ -734,186 +466,4 @@ mod tests {
let response = sniff_handler(State(app), wrong_secret, json_request(body.to_owned())).await;
assert_eq!(StatusCode::UNAUTHORIZED, response.status());
}
-
- #[tokio::test]
- async fn sniff_reports_mpeg_ts_packets_stored_under_a_javascript_name_as_video_mp2t() {
- let (_tmp, app) = local_storage_app();
- store_upload(&app, "x.js", &mpeg_ts_segment(), "text/javascript").await;
-
- let response = sniff_upload(&app, "x.js").await;
-
- assert_eq!(StatusCode::OK, response.status());
- assert_eq!(
- Some("application/json"),
- response
- .headers()
- .get(header::CONTENT_TYPE)
- .and_then(|value| value.to_str().ok())
- );
- assert_eq!(
- r#"{"content_type":"video/mp2t"}"#,
- response_body(response).await
- );
- }
-
- #[tokio::test]
- async fn sniff_reports_null_for_javascript_svg_markup_and_pdf() {
- let (_tmp, app) = local_storage_app();
- let svg = br#"<svg xmlns="http://www.w3.org/2000/svg"></svg>"#.as_slice();
- assert_eq!("image/svg+xml", mime::sniff(svg).mime);
-
- for (key, data) in [
- ("a.js", b"export const a = 1;\n".as_slice()),
- ("b.tsx", svg),
- ("c.pdf", b"%PDF-1.7".as_slice()),
- ] {
- store_upload(&app, key, data, "application/octet-stream").await;
- let response = sniff_upload(&app, key).await;
- assert_eq!(StatusCode::OK, response.status(), "{key}");
- assert_eq!(
- r#"{"content_type":null}"#,
- response_body(response).await,
- "{key}"
- );
- }
- }
-
- #[tokio::test]
- async fn sniff_reports_null_for_an_empty_upload() {
- let (_tmp, app) = local_storage_app();
- store_upload(&app, "empty.js", b"", "text/javascript").await;
-
- let response = sniff_upload(&app, "empty.js").await;
-
- assert_eq!(StatusCode::OK, response.status());
- assert_eq!(r#"{"content_type":null}"#, response_body(response).await);
- }
-
- #[tokio::test]
- async fn sniff_reports_null_when_object_storage_answers_the_prefix_range_with_416() {
- let fake = fake_s3().await;
- fake.put_object(
- "uploads/empty.js",
- FakeObject {
- content_type: Some("text/javascript".to_owned()),
- read_status: Some(416),
- ..FakeObject::default()
- },
- );
- let tmp = tempfile::tempdir().expect("temp storage root");
- let app = Arc::new(AppState::for_tests(fake.config(tmp.path())));
-
- let response = sniff_upload(&app, "empty.js").await;
-
- assert_eq!(StatusCode::OK, response.status());
- assert_eq!(r#"{"content_type":null}"#, response_body(response).await);
- }
-
- #[tokio::test]
- async fn sniff_reports_the_prefix_verdict_for_an_upload_larger_than_the_prefix() {
- let (_tmp, app) = local_storage_app();
- store_upload(&app, "large.bin", &png_followed_by_zeros(), "text/plain").await;
-
- let response = sniff_upload(&app, "large.bin").await;
-
- assert_eq!(StatusCode::OK, response.status());
- assert_eq!(
- r#"{"content_type":"image/png"}"#,
- response_body(response).await
- );
- }
-
- #[tokio::test]
- async fn sniff_asks_object_storage_for_only_the_prefix_range() {
- let fake = fake_s3().await;
- fake.put_object(
- "uploads/large.bin",
- FakeObject {
- body: png_followed_by_zeros(),
- content_type: Some("text/plain".to_owned()),
- ..FakeObject::default()
- },
- );
- let tmp = tempfile::tempdir().expect("temp storage root");
- let app = Arc::new(AppState::for_tests(fake.config(tmp.path())));
-
- let response = sniff_upload(&app, "large.bin").await;
-
- assert_eq!(StatusCode::OK, response.status());
- assert_eq!(
- r#"{"content_type":"image/png"}"#,
- response_body(response).await
- );
- let reads: Vec<_> = fake
- .requests()
- .into_iter()
- .filter(|(method, _, _, _)| *method == Method::GET)
- .collect();
- assert_eq!(1, reads.len());
- let (_, uri, headers, _) = &reads[0];
- assert_eq!("/uploads/large.bin", uri.path());
- assert_eq!(
- Some("bytes=0-8191"),
- headers
- .get(header::RANGE)
- .and_then(|value| value.to_str().ok())
- );
- }
-
- #[tokio::test]
- async fn sniff_prefix_stops_at_the_prefix_when_storage_ignores_the_range() {
- let upload = png_followed_by_zeros();
- let mut yielded = 0;
- let stream_source = upload.clone();
- let body = Body::from_stream(futures_util::stream::poll_fn(move |_| {
- assert!(
- yielded < STORED_SNIFF_PREFIX_BYTES,
- "the body was polled again after {yielded} bytes were yielded"
- );
- let end = (yielded + 1000).min(stream_source.len());
- let chunk = Bytes::copy_from_slice(&stream_source[yielded..end]);
- yielded = end;
- std::task::Poll::Ready(Some(Ok::<_, std::io::Error>(chunk)))
- }));
-
- let prefix = read_sniff_prefix(body).await.expect("prefix read");
-
- assert_eq!(&upload[..STORED_SNIFF_PREFIX_BYTES], prefix.as_slice());
- }
-
- #[tokio::test]
- async fn sniff_answers_a_missing_upload_with_404() {
- let (_tmp, app) = local_storage_app();
-
- let response = sniff_upload(&app, "missing.js").await;
-
- assert_eq!(StatusCode::NOT_FOUND, response.status());
- assert_eq!("Not Found", response_body(response).await);
- }
-
- #[tokio::test]
- async fn sniff_answers_a_body_without_an_upload_filename_with_400() {
- let response = sniff_handler(
- State(test_app_state()),
- authorized_headers(),
- json_request(r#"{"type":"upload"}"#.to_owned()),
- )
- .await;
-
- assert_eq!(StatusCode::BAD_REQUEST, response.status());
- assert_eq!("Bad Request", response_body(response).await);
- }
-
- #[tokio::test]
- async fn sniff_answers_an_oversized_body_with_413_payload_too_large() {
- let response = sniff_handler(
- State(test_app_state()),
- authorized_headers(),
- oversized_request(),
- )
- .await;
-
- assert_eq!(StatusCode::PAYLOAD_TOO_LARGE, response.status());
- assert_eq!("Payload Too Large", response_body(response).await);
- }
}
diff --git a/fluxer_media_proxy/src/server/runtime.rs b/fluxer_media_proxy/src/server/runtime.rs
index 35b29cda8..fcdd4da90 100644
--- a/fluxer_media_proxy/src/server/runtime.rs
+++ b/fluxer_media_proxy/src/server/runtime.rs
@@ -169,580 +169,3 @@ async fn drain_and_shutdown(state: &Arc<AppState>, drain: &HttpRequestDrain) ->
.map_err(|_| anyhow::anyhow!("media proxy native task shutdown exceeded {grace_ms} ms"));
aggregate_results("media proxy shutdown", [requests, coalescer, native_tasks])
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use crate::{
- http_headers,
- storage::tests::{FakeObject, FakeS3, fake_s3},
- };
- use axum::{
- body::Body,
- extract::ConnectInfo,
- http::{Method, Request, StatusCode, header},
- response::Response,
- };
- use base64::{Engine as _, engine::general_purpose};
-
- const ENFORCE: &[(&str, &str)] = &[
- ("FLUXER_MEDIA_PROXY_CORS_MODE", "enforce"),
- (
- "FLUXER_MEDIA_PROXY_CORS_ALLOWED_ORIGINS",
- "https://web.fluxer.app",
- ),
- ];
-
- const DECLARED_ROUTES: &[(&str, &str, &str)] = &[
- ("/_health", "/_health", "GET,HEAD"),
- ("/_metrics", "/_metrics", "GET,HEAD"),
- ("/_metadata", "/_metadata", "POST"),
- ("/_sniff", "/_sniff", "POST"),
- ("/_thumbnail", "/_thumbnail", "POST"),
- ("/_frames", "/_frames", "POST"),
- (
- "/v1/relay/{*key}",
- "/v1/relay/uploads/file.png",
- "PUT,OPTIONS",
- ),
- ];
-
- fn test_config(mode: &str, extra: &[(&str, &str)]) -> Config {
- let mut env = vec![
- (
- "FLUXER_MEDIA_PROXY_SECRET_KEY".to_owned(),
- "secret".to_owned(),
- ),
- ("FLUXER_MEDIA_PROXY_MODE".to_owned(), mode.to_owned()),
- (
- "FLUXER_MEDIA_PROXY_UPLOAD_RELAY_SECRET_BASE64".to_owned(),
- general_purpose::STANDARD.encode([7u8; 32]),
- ),
- ];
- env.extend(
- extra
- .iter()
- .map(|(key, value)| ((*key).to_owned(), (*value).to_owned())),
- );
- Config::load_from_iter(env).expect("test config")
- }
-
- fn test_router_for(cfg: Config) -> Router {
- build_router(Arc::new(AppState::for_tests(cfg)), HttpRequestDrain::new())
- }
-
- async fn send(router: Router, request: Request<Body>) -> Response {
- tower::ServiceExt::oneshot(router, request)
- .await
- .expect("router response")
- }
-
- async fn probe(router: Router, path: &str, method: Method) -> Response {
- send(
- router,
- Request::builder()
- .method(method)
- .uri(path)
- .body(Body::empty())
- .expect("probe request"),
- )
- .await
- }
-
- fn header_text(response: &Response, name: impl header::AsHeaderName) -> Option<&str> {
- response
- .headers()
- .get(name)
- .map(|value| value.to_str().expect("header is ascii"))
- }
-
- const SIGNATURE_SECRET: [u8; 32] = [11u8; 32];
- const SIGNATURE_ENFORCE: &[(&str, &str)] = &[
- ("FLUXER_MEDIA_PROXY_ATTACHMENT_SIGNATURE_MODE", "enforce"),
- (
- "FLUXER_MEDIA_PROXY_ATTACHMENT_URL_SECRETS_BASE64",
- "CwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCws=",
- ),
- ];
- const ATTACHMENT_PATH: &str = "/attachments/1/2/file.bin";
- const ATTACHMENT_KEY: &str = "attachments/1/2/file.bin";
-
- #[tokio::test]
- async fn an_enforced_refusal_is_countable_apart_from_a_genuine_missing_object() {
- let tmp = tempfile::tempdir().expect("storage root");
- let root = tmp.path().canonicalize().expect("canonical storage root");
- let mut env: Vec<(String, String)> = SIGNATURE_ENFORCE
- .iter()
- .map(|(key, value)| ((*key).to_owned(), (*value).to_owned()))
- .collect();
- env.push((
- "FLUXER_MEDIA_PROXY_SECRET_KEY".to_owned(),
- "secret".to_owned(),
- ));
- env.push((
- "FLUXER_MEDIA_PROXY_STORAGE_BACKEND".to_owned(),
- "local".to_owned(),
- ));
- env.push((
- "FLUXER_MEDIA_PROXY_STORAGE_ROOT".to_owned(),
- root.display().to_string(),
- ));
- let cfg = Config::load_from_iter(env).expect("signature router config");
- let state = Arc::new(AppState::for_tests(cfg));
- let router = build_router(Arc::clone(&state), HttpRequestDrain::new());
-
- let refused = probe(router.clone(), ATTACHMENT_PATH, Method::GET).await;
- assert_eq!(StatusCode::NOT_FOUND, refused.status());
-
- let now = std::time::SystemTime::now()
- .duration_since(std::time::UNIX_EPOCH)
- .expect("the clock is after the unix epoch")
- .as_secs();
- let signed = fluxer_common::attachment_url_signature::with_signature(
- ATTACHMENT_PATH,
- ATTACHMENT_KEY,
- now,
- now,
- &SIGNATURE_SECRET,
- );
- let missing = probe(router, &signed, Method::GET).await;
- assert_eq!(StatusCode::NOT_FOUND, missing.status());
-
- let text = state.metrics.render();
- assert!(
- text.contains("fluxer_media_proxy_requests_4xx_total{kind=\"attachment\"} 2\n"),
- "{text}"
- );
- assert!(
- text.contains(
- "fluxer_media_proxy_attachment_signature_verdicts_total{verdict=\"missing\"} 1\n"
- ),
- "{text}"
- );
- assert!(
- text.contains(
- "fluxer_media_proxy_attachment_signature_verdicts_total{verdict=\"valid\"} 1\n"
- ),
- "{text}"
- );
- }
-
- async fn static_router_serving(objects: &[(&str, &str, &[u8])]) -> (FakeS3, Router) {
- let fake = fake_s3().await;
- let tmp = tempfile::tempdir().expect("config root");
- let mut cfg = fake.config(tmp.path());
- cfg.mode = DeploymentMode::Static;
- for (key, content_type, body) in objects {
- fake.put_object(
- &format!("{}/{key}", cfg.storage.bucket_static),
- FakeObject {
- body: body.to_vec(),
- content_type: Some((*content_type).to_owned()),
- ..FakeObject::default()
- },
- );
- }
- (fake, test_router_for(cfg))
- }
-
- async fn fetch(
- router: Router,
- path: &str,
- accept_encoding: Option<&str>,
- range: Option<&str>,
- ) -> (Response, Vec<u8>) {
- let mut builder = Request::builder().uri(path);
- if let Some(value) = accept_encoding {
- builder = builder.header(header::ACCEPT_ENCODING, value);
- }
- if let Some(value) = range {
- builder = builder.header(header::RANGE, value);
- }
- let response = send(router, builder.body(Body::empty()).expect("request")).await;
- let (parts, body) = response.into_parts();
- let bytes = axum::body::to_bytes(body, usize::MAX)
- .await
- .expect("response body")
- .to_vec();
- (Response::from_parts(parts, Body::empty()), bytes)
- }
-
- fn decoded(encoding: Option<&str>, body: &[u8]) -> Vec<u8> {
- let mut out = Vec::new();
- match encoding {
- Some("gzip") => {
- std::io::Read::read_to_end(&mut flate2::read::GzDecoder::new(body), &mut out)
- .expect("gzip body");
- }
- None => out.extend_from_slice(body),
- Some(other) => panic!("unexpected content-encoding {other}"),
- }
- out
- }
-
- fn compressible_bytes(len: usize) -> Vec<u8> {
- (0..len).map(|index| b"asm_module_"[index % 11]).collect()
- }
-
- #[tokio::test]
- async fn static_mode_gzips_scripts_styles_and_wasm_for_clients_that_accept_it() {
- let body = compressible_bytes(64 * 1024);
- let objects = [
- ("assets/0123456789abcdef.wasm", "application/wasm"),
- (
- "assets/0123456789abcdef.js",
- "application/javascript; charset=utf-8",
- ),
- ("assets/0123456789abcdef.css", "text/css; charset=utf-8"),
- (
- "assets/0123456789abcdef.js.map",
- "application/json; charset=utf-8",
- ),
- ("emoji/1f600.svg", "image/svg+xml"),
- ];
- let stored: Vec<(&str, &str, &[u8])> = objects
- .iter()
- .map(|(key, content_type)| (*key, *content_type, &body[..]))
- .collect();
- let (_fake, router) = static_router_serving(&stored).await;
- for (key, _) in objects {
- let path = format!("/{key}");
- for (accept, expected) in [
- ("br, gzip, zstd", Some("gzip")),
- ("gzip, deflate", Some("gzip")),
- ("gzip;q=0.5, br;q=1", Some("gzip")),
- ("br", None),
- ("deflate", None),
- ("identity", None),
- ("zstd", None),
- ] {
- let (response, bytes) = fetch(router.clone(), &path, Some(accept), None).await;
- assert_eq!(StatusCode::OK, response.status(), "{key} {accept}");
- let encoding = header_text(&response, header::CONTENT_ENCODING);
- assert_eq!(expected, encoding, "{key} {accept}");
- assert_eq!(body, decoded(encoding, &bytes), "{key} {accept}");
- assert_eq!(
- vec!["Accept-Encoding"],
- response
- .headers()
- .get_all(header::VARY)
- .iter()
- .map(|value| value.to_str().expect("vary is ascii"))
- .collect::<Vec<_>>(),
- "{key} {accept}"
- );
- if expected.is_some() {
- assert!(bytes.len() < body.len() / 4, "{key} {accept}");
- assert_eq!(None, header_text(&response, header::CONTENT_LENGTH));
- assert_eq!(None, header_text(&response, header::ACCEPT_RANGES));
- } else {
- assert_eq!(
- Some(body.len().to_string().as_str()),
- header_text(&response, header::CONTENT_LENGTH),
- "{key} {accept}"
- );
- }
- }
- let (response, bytes) = fetch(router.clone(), &path, None, None).await;
- assert_eq!(
- None,
- header_text(&response, header::CONTENT_ENCODING),
- "{key}"
- );
- assert_eq!(body, bytes, "{key}");
- }
- }
-
- #[tokio::test]
- async fn static_mode_serves_archives_images_ranges_and_small_files_as_stored() {
- let body = compressible_bytes(64 * 1024);
- let small = compressible_bytes(512);
- let (_fake, router) = static_router_serving(&[
- ("assets/0123456789abcdef.tar.gz", "application/gzip", &body),
- ("avatars/0.png", "image/png", &body),
- (
- "desktop/fluxer-setup.exe",
- "application/octet-stream",
- &body,
- ),
- (
- "assets/fedcba9876543210.js",
- "application/javascript; charset=utf-8",
- &small,
- ),
- ("assets/0123456789abcdef.wasm", "application/wasm", &body),
- ])
- .await;
- for (path, stored) in [
- ("/assets/0123456789abcdef.tar.gz", &body),
- ("/avatars/0.png", &body),
- ("/desktop/fluxer-setup.exe", &body),
- ("/assets/fedcba9876543210.js", &small),
- ] {
- let (response, bytes) = fetch(router.clone(), path, Some("br, gzip"), None).await;
- assert_eq!(StatusCode::OK, response.status(), "{path}");
- assert_eq!(
- None,
- header_text(&response, header::CONTENT_ENCODING),
- "{path}"
- );
- assert_eq!(stored, &bytes, "{path}");
- }
- let (response, bytes) = fetch(
- router,
- "/assets/0123456789abcdef.wasm",
- Some("br, gzip"),
- Some("bytes=100-199"),
- )
- .await;
- assert_eq!(StatusCode::PARTIAL_CONTENT, response.status());
- assert_eq!(None, header_text(&response, header::CONTENT_ENCODING));
- assert_eq!(&body[100..200], &bytes[..]);
- }
-
- #[tokio::test]
- async fn static_head_matches_get_representation_headers_without_a_body() {
- let body = compressible_bytes(1024);
- let small = compressible_bytes(1023);
- let (_fake, router) = static_router_serving(&[
- ("assets/0123456789abcdef.wasm", "application/wasm", &body),
- (
- "assets/fedcba9876543210.js",
- "application/javascript",
- &small,
- ),
- ("assets/0123456789abcdef.tar.gz", "application/gzip", &body),
- ])
- .await;
- let listener = TcpListener::bind("127.0.0.1:0")
- .await
- .expect("local listener");
- let address = listener.local_addr().expect("local address");
- let server = tokio::spawn(async move {
- axum::serve(
- listener,
- router.into_make_service_with_connect_info::<SocketAddr>(),
- )
- .await
- .expect("local server");
- });
- let client = reqwest::Client::builder()
- .no_proxy()
- .no_gzip()
- .no_brotli()
- .no_deflate()
- .no_zstd()
- .build()
- .expect("http client");
- for (path, accept_encoding, range, expected_encoding, expected_body) in [
- (
- "/assets/0123456789abcdef.wasm",
- "gzip",
- None,
- Some("gzip"),
- &body[..],
- ),
- (
- "/assets/0123456789abcdef.wasm",
- "identity",
- None,
- None,
- &body[..],
- ),
- (
- "/assets/fedcba9876543210.js",
- "gzip",
- None,
- None,
- &small[..],
- ),
- (
- "/assets/0123456789abcdef.tar.gz",
- "gzip",
- None,
- None,
- &body[..],
- ),
- (
- "/assets/0123456789abcdef.wasm",
- "gzip",
- Some("bytes=100-199"),
- None,
- &body[100..200],
- ),
- ] {
- let url = format!("http://{address}{path}");
- let mut get = client
- .get(&url)
- .header(header::ACCEPT_ENCODING, accept_encoding);
- let mut head = client
- .head(&url)
- .header(header::ACCEPT_ENCODING, accept_encoding);
- if let Some(range) = range {
- get = get.header(header::RANGE, range);
- head = head.header(header::RANGE, range);
- }
- let get = get.send().await.expect("get response");
- let status = get.status();
- let get_headers = get.headers().clone();
- let get_body = get.bytes().await.expect("get body");
- let encoding = get_headers
- .get(header::CONTENT_ENCODING)
- .map(|value| value.to_str().expect("encoding is ascii"));
- assert_eq!(expected_encoding, encoding, "{path} {accept_encoding}");
- assert_eq!(expected_body, decoded(encoding, &get_body));
- let head = head.send().await.expect("head response");
- assert_eq!(status, head.status(), "{path} {accept_encoding}");
- for name in [
- header::CONTENT_ENCODING,
- header::CONTENT_LENGTH,
- header::CONTENT_TYPE,
- header::CONTENT_RANGE,
- header::ACCEPT_RANGES,
- header::VARY,
- ] {
- assert_eq!(
- get_headers.get(&name),
- head.headers().get(&name),
- "{path} {accept_encoding} {name}"
- );
- }
- assert!(head.bytes().await.expect("head body").is_empty());
- }
- server.abort();
- let _ = server.await;
- }
-
- #[tokio::test]
- async fn router_exposes_exactly_the_declared_route_and_method_table() {
- for mode in ["mp", "static", "upload", "relay"] {
- let router = test_router_for(test_config(mode, &[]));
- for (declared, path, allowed) in DECLARED_ROUTES {
- let response = probe(router.clone(), path, Method::DELETE).await;
- assert_eq!(
- StatusCode::METHOD_NOT_ALLOWED,
- response.status(),
- "{mode} {declared} must reject an undeclared method"
- );
- assert_eq!(
- Some(*allowed),
- header_text(&response, header::ALLOW),
- "{mode} {declared} method table"
- );
- }
- }
- }
-
- #[tokio::test]
- async fn unknown_paths_reach_the_catch_all_fallback() {
- let router = test_router_for(test_config("mp", &[]));
- let response = probe(router.clone(), "/avatars/1/hash.png", Method::DELETE).await;
- assert_eq!(StatusCode::METHOD_NOT_ALLOWED, response.status());
- assert!(response.headers().get("allow").is_none());
- let response = probe(router, "/nope", Method::GET).await;
- assert_eq!(StatusCode::NOT_FOUND, response.status());
- }
-
- #[tokio::test]
- async fn relay_mode_router_serves_health_relay_and_internal_but_not_reads() {
- let router = test_router_for(test_config("relay", &[]));
-
- let health = probe(router.clone(), "/_health", Method::GET).await;
- assert_eq!(StatusCode::OK, health.status());
-
- let options = probe(
- router.clone(),
- "/v1/relay/uploads/file.png",
- Method::OPTIONS,
- )
- .await;
- assert_eq!(StatusCode::NO_CONTENT, options.status());
- assert_eq!(
- Some("*"),
- header_text(&options, header::ACCESS_CONTROL_ALLOW_ORIGIN)
- );
- assert_eq!(
- Some("PUT, OPTIONS"),
- header_text(&options, header::ACCESS_CONTROL_ALLOW_METHODS)
- );
-
- let put = probe(router.clone(), "/v1/relay/uploads/file.png", Method::PUT).await;
- assert_eq!(StatusCode::UNAUTHORIZED, put.status());
-
- let metadata = probe(router.clone(), "/_metadata", Method::POST).await;
- assert_eq!(StatusCode::UNAUTHORIZED, metadata.status());
-
- let sniff = probe(router.clone(), "/_sniff", Method::POST).await;
- assert_eq!(StatusCode::UNAUTHORIZED, sniff.status());
-
- let read = probe(router, "/attachments/1/2/stream.js", Method::GET).await;
- assert_eq!(StatusCode::NOT_FOUND, read.status());
- assert_eq!(
- None,
- header_text(&read, header::ACCESS_CONTROL_ALLOW_ORIGIN)
- );
- }
-
- #[tokio::test]
- async fn internal_routes_and_the_relay_ignore_the_cors_policy() {
- let off = test_router_for(test_config("upload", &[]));
- let enforce = test_router_for(test_config("upload", ENFORCE));
- let request = |method: Method, path: &str| {
- Request::builder()
- .method(method)
- .uri(path)
- .header(header::ORIGIN, "https://evil.example")
- .extension(ConnectInfo(SocketAddr::from(([127, 0, 0, 1], 40000))))
- .body(Body::empty())
- .expect("cors probe request")
- };
- for (method, path) in [
- (Method::GET, "/_health"),
- (Method::POST, "/_metadata"),
- (Method::POST, "/_sniff"),
- (Method::OPTIONS, "/v1/relay/uploads/file.png"),
- (Method::PUT, "/v1/relay/uploads/file.png"),
- (Method::GET, "/_metrics"),
- ] {
- let label = format!("{method} {path}");
- let expected = send(off.clone(), request(method.clone(), path)).await;
- let actual = send(enforce.clone(), request(method, path)).await;
- assert_eq!(expected.status(), actual.status(), "{label}");
- assert_eq!(expected.headers(), actual.headers(), "{label}");
- for vary in actual.headers().get_all(header::VARY) {
- assert!(
- !vary.to_str().expect("ascii vary").contains("Origin"),
- "{label}"
- );
- }
- }
- }
-
- #[tokio::test]
- async fn a_refused_read_through_the_router_has_security_and_version_headers() {
- let state = Arc::new(AppState::for_tests(test_config("mp", ENFORCE)));
- let router = build_router(Arc::clone(&state), HttpRequestDrain::new());
- let response = send(
- router,
- Request::builder()
- .method(Method::GET)
- .uri("/avatars/1/hash.png")
- .header(header::ORIGIN, "null".to_owned())
- .body(Body::empty())
- .expect("refused read request"),
- )
- .await;
- assert_eq!(StatusCode::FORBIDDEN, response.status());
- assert!(response.headers().contains_key("x-fluxer-version"));
- assert_eq!(
- Some(http_headers::STRICT_TRANSPORT_SECURITY),
- header_text(&response, header::STRICT_TRANSPORT_SECURITY)
- );
- assert!(
- state
- .metrics
- .render()
- .contains("fluxer_media_proxy_requests_4xx_total{kind=\"asset_image\"} 1\n")
- );
- }
-}
diff --git a/fluxer_media_proxy/src/server/self_origin.rs b/fluxer_media_proxy/src/server/self_origin.rs
index 64ac7e32f..9066f3096 100644
--- a/fluxer_media_proxy/src/server/self_origin.rs
+++ b/fluxer_media_proxy/src/server/self_origin.rs
@@ -113,163 +113,6 @@ fn effective_port(scheme: &str, port: Option<u16>) -> u16 {
#[cfg(test)]
mod tests {
use super::*;
- use crate::config::Config;
-
- const ENDPOINT: &str = "https://chat.example.com/media";
-
- fn app() -> AppState {
- AppState::for_tests(
- Config::load_from_iter([
- (
- "FLUXER_MEDIA_PROXY_SECRET_KEY".to_owned(),
- "secret".to_owned(),
- ),
- (
- "FLUXER_MEDIA_PROXY_PUBLIC_ENDPOINT".to_owned(),
- format!("{ENDPOINT}/"),
- ),
- (
- "FLUXER_MEDIA_PROXY_STORAGE_BACKEND".to_owned(),
- "local".to_owned(),
- ),
- ])
- .expect("self origin test config"),
- )
- }
-
- #[test]
- fn own_attachments_read_from_the_cdn_bucket() {
- let app = app();
- let resolved = resolve(
- &app,
- &format!("{ENDPOINT}/attachments/1544725486800732163/1544971349200470016/cat.gif"),
- );
- match resolved {
- Some(SelfOrigin::Stored { bucket, key, .. }) => {
- assert_eq!(app.cfg.storage.bucket_cdn, bucket);
- assert_eq!(
- "attachments/1544725486800732163/1544971349200470016/cat.gif",
- key
- );
- }
- _ => panic!("own attachment paths resolve to stored objects"),
- }
- }
-
- #[test]
- fn own_avatars_get_the_extension_fallback() {
- let app = app();
- match resolve(
- &app,
- &format!("{ENDPOINT}/avatars/1216100949629702144/a1b2c3d4e5f6.png?size=64"),
- ) {
- Some(SelfOrigin::Stored {
- key, fallback_ext, ..
- }) => {
- assert_eq!("avatars/1216100949629702144/a1b2c3d4e5f6", key);
- assert_eq!(Some(AssetExtension::Png), fallback_ext);
- }
- _ => panic!("own asset paths resolve to stored objects"),
- }
- }
-
- #[test]
- fn own_external_paths_unwrap_to_the_origin_url() {
- let app = app();
- let target = "https://static.klipy.com/ii/c8/28/HkAKKCzZ.webp";
- let proxied = fluxer_common::external_media_path::build_external_media_proxy_url(
- ENDPOINT,
- target,
- app.cfg.secret_key.as_bytes(),
- )
- .expect("proxy url");
- match resolve(&app, &proxied) {
- Some(SelfOrigin::External { url }) => assert_eq!(target, url),
- _ => panic!("own external paths unwrap to the origin url"),
- }
- let tampered = proxied.replace("/external/", "/external/x");
- assert!(resolve(&app, &tampered).is_none());
- }
-
- #[test]
- fn foreign_and_unknown_paths_stay_external_fetches() {
- let app = app();
- assert!(resolve(&app, "https://static.klipy.com/ii/c8/28/HkAKKCzZ.webp").is_none());
- assert!(resolve(&app, &format!("{ENDPOINT}/channels/1/2")).is_none());
- }
-
- #[test]
- fn a_non_default_public_port_reaches_the_signed_self_origin() {
- let app = AppState::for_tests(
- Config::load_from_iter([
- (
- "FLUXER_MEDIA_PROXY_SECRET_KEY".to_owned(),
- "secret".to_owned(),
- ),
- (
- "FLUXER_MEDIA_PROXY_PUBLIC_ENDPOINT".to_owned(),
- format!("{ENDPOINT}/"),
- ),
- (
- "FLUXER_MEDIA_PROXY_STORAGE_BACKEND".to_owned(),
- "local".to_owned(),
- ),
- (
- "FLUXER_BASE_DOMAIN".to_owned(),
- "chat.example.com".to_owned(),
- ),
- ("FLUXER_PUBLIC_PORT".to_owned(), "29080".to_owned()),
- ])
- .expect("self origin test config"),
- );
- let endpoint = app
- .cfg
- .public_endpoint
- .clone()
- .expect("a public endpoint is configured");
- assert_eq!("https://chat.example.com:29080/media", endpoint);
- let target = "https://static.klipy.com/ii/c8/28/HkAKKCzZ.webp";
- let proxied = fluxer_common::external_media_path::build_external_media_proxy_url(
- &endpoint,
- target,
- app.cfg.secret_key.as_bytes(),
- )
- .expect("proxy url");
- match resolve(&app, &proxied) {
- Some(SelfOrigin::External { url }) => assert_eq!(target, url),
- _ => panic!("its own signed url resolves to the origin url"),
- }
- match resolve(
- &app,
- &format!("{endpoint}/attachments/1544725486800732163/1544971349200470016/cat.gif"),
- ) {
- Some(SelfOrigin::Stored { key, .. }) => assert_eq!(
- "attachments/1544725486800732163/1544971349200470016/cat.gif",
- key
- ),
- _ => panic!("its own attachment paths resolve to stored objects"),
- }
- assert!(resolve(&app, "https://cdn.other.example/avatars/1/abc.png").is_none());
- assert!(resolve(&app, &format!("{ENDPOINT}/avatars/1/abc.png")).is_none());
- }
-
- #[test]
- fn own_path_matches_the_endpoint_prefix() {
- assert_eq!(
- own_path(
- "https://chat.example.com/media",
- "https://chat.example.com/media/attachments/1/2/cat.gif"
- ),
- Some("/attachments/1/2/cat.gif")
- );
- assert_eq!(
- own_path(
- "https://cdn.example.com",
- "https://cdn.example.com/avatars/1/abc.png?size=64"
- ),
- Some("/avatars/1/abc.png")
- );
- }
#[test]
fn own_path_rejects_other_origins_and_sibling_prefixes() {
@@ -302,67 +145,4 @@ mod tests {
None
);
}
-
- #[test]
- fn own_path_keeps_explicit_default_ports() {
- assert_eq!(
- own_path(
- "https://chat.example.com:443",
- "https://chat.example.com/a.png"
- ),
- Some("/a.png")
- );
- }
-
- #[test]
- fn an_enforcing_instance_still_resolves_its_own_attachment_for_internal_reads() {
- use base64::Engine as _;
- use fluxer_common::attachment_url_signature::with_signature;
- use std::time::{SystemTime, UNIX_EPOCH};
-
- const SECRET: [u8; 32] = [3u8; 32];
- let app = AppState::for_tests(
- Config::load_from_iter([
- (
- "FLUXER_MEDIA_PROXY_SECRET_KEY".to_owned(),
- "secret".to_owned(),
- ),
- (
- "FLUXER_MEDIA_PROXY_PUBLIC_ENDPOINT".to_owned(),
- format!("{ENDPOINT}/"),
- ),
- (
- "FLUXER_MEDIA_PROXY_STORAGE_BACKEND".to_owned(),
- "local".to_owned(),
- ),
- (
- "FLUXER_MEDIA_PROXY_ATTACHMENT_SIGNATURE_MODE".to_owned(),
- "enforce".to_owned(),
- ),
- (
- "FLUXER_MEDIA_PROXY_ATTACHMENT_URL_SECRETS_BASE64".to_owned(),
- base64::engine::general_purpose::STANDARD.encode(SECRET),
- ),
- ])
- .expect("self origin signature config"),
- );
- let key = "attachments/1544725486800732163/1544971349200470016/cat.gif";
- let unsigned = format!("{ENDPOINT}/{key}");
- let now = SystemTime::now()
- .duration_since(UNIX_EPOCH)
- .expect("the clock is after the unix epoch")
- .as_secs();
- let signed = with_signature(&unsigned, key, now, now, &SECRET);
-
- for spelling in [
- unsigned.clone(),
- signed.clone(),
- format!("{signed}&width=64#frame"),
- ] {
- match resolve(&app, &spelling) {
- Some(SelfOrigin::Stored { key: resolved, .. }) => assert_eq!(key, resolved),
- _ => panic!("an own attachment resolves to a stored object: {spelling}"),
- }
- }
- }
}
diff --git a/fluxer_media_proxy/src/server/stored/passthrough.rs b/fluxer_media_proxy/src/server/stored/passthrough.rs
index 7454c9b47..605972a41 100644
--- a/fluxer_media_proxy/src/server/stored/passthrough.rs
+++ b/fluxer_media_proxy/src/server/stored/passthrough.rs
@@ -317,32 +317,3 @@ async fn serve_stored_svg_rasterized(
disposition: passthrough_disposition_header(disposition, format.mime()),
})
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn passthrough_content_type_preserves_non_media_metadata() {
- assert_eq!(
- "application/zip",
- passthrough_content_type("application/zip", "downloads/app.zip")
- );
- }
-
- #[test]
- fn passthrough_content_type_prefers_known_extension_over_bad_metadata() {
- assert_eq!(
- "image/png",
- passthrough_content_type("text/plain", "image.png")
- );
- }
-
- #[test]
- fn passthrough_content_type_prefers_m4a_extension_over_mp4_metadata() {
- assert_eq!(
- "audio/mp4",
- passthrough_content_type("video/mp4", "track.m4a")
- );
- }
-}
diff --git a/fluxer_media_proxy/src/server/stored/response.rs b/fluxer_media_proxy/src/server/stored/response.rs
index 3b040f3df..ca43425ce 100644
--- a/fluxer_media_proxy/src/server/stored/response.rs
+++ b/fluxer_media_proxy/src/server/stored/response.rs
@@ -90,80 +90,3 @@ pub(super) fn streaming_media_response(response: StreamingMediaResponse<'_>) ->
}
use crate::server::response::media_body_response;
-#[cfg(test)]
-use axum::http::header;
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use bytes::Bytes;
- use http_body_util::BodyExt as _;
- use std::io;
-
- fn stored_stream(chunks: Vec<&'static [u8]>, content_length: u64) -> StreamObject {
- StreamObject {
- body: Body::from_stream(futures_util::stream::iter(
- chunks
- .into_iter()
- .map(|chunk| Ok::<_, io::Error>(Bytes::from_static(chunk))),
- )),
- status: StatusCode::OK,
- content_length: Some(content_length),
- content_type: "text/plain".to_owned(),
- byte_range: None,
- total_length: Some(content_length),
- }
- }
-
- fn stored_response(object: StreamObject) -> Response {
- streaming_media_response(StreamingMediaResponse {
- method: Method::GET,
- object,
- total_len: 5,
- content_type: "text/plain",
- disposition: None,
- stream_policy: DownloadStreamPolicy::for_passthrough(30_000),
- })
- }
-
- async fn body_error_kind(response: Response) -> io::ErrorKind {
- let mut error = response
- .into_body()
- .collect()
- .await
- .map(|collected| collected.to_bytes())
- .expect_err("the stored stream fails")
- .into_inner();
- loop {
- error = match error.downcast::<io::Error>() {
- Ok(error) => return error.kind(),
- Err(error) => error
- .downcast::<axum::Error>()
- .expect("the stored stream fails with an io error")
- .into_inner(),
- };
- }
- }
-
- #[tokio::test]
- async fn a_stored_stream_that_ends_short_of_its_content_length_fails_with_unexpected_eof() {
- let response = stored_response(stored_stream(vec![b"hel"], 5));
- assert_eq!(
- Some("5"),
- response
- .headers()
- .get(header::CONTENT_LENGTH)
- .and_then(|value| value.to_str().ok())
- );
- assert_eq!(
- io::ErrorKind::UnexpectedEof,
- body_error_kind(response).await
- );
- }
-
- #[tokio::test]
- async fn a_stored_stream_that_overruns_its_content_length_fails_with_invalid_data() {
- let response = stored_response(stored_stream(vec![b"hello", b"world"], 5));
- assert_eq!(io::ErrorKind::InvalidData, body_error_kind(response).await);
- }
-}
diff --git a/fluxer_media_proxy/src/server/transform/execution.rs b/fluxer_media_proxy/src/server/transform/execution.rs
index a47e7ea5f..aa0e268e3 100644
--- a/fluxer_media_proxy/src/server/transform/execution.rs
+++ b/fluxer_media_proxy/src/server/transform/execution.rs
@@ -153,75 +153,3 @@ pub(in crate::server) fn deadline_instant(deadline_ms: Option<i64>) -> Option<In
let remaining_ms = deadline_ms.saturating_sub(metrics::now_ms()).max(0) as u64;
Some(Instant::now() + Duration::from_millis(remaining_ms))
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn native_admission_failures_are_retyped_for_the_coalescer() {
- let shed = coalesced_work_result::<()>(Err(TimedSemaphoreError::QueueFull.into()))
- .expect_err("a full queue sheds the request");
- assert_eq!(
- Some(&CoalescerError::RequestTimeout),
- shed.downcast_ref::<CoalescerError>()
- );
- let unavailable = coalesced_work_result::<()>(Err(TimedSemaphoreError::Closed.into()))
- .expect_err("a closed pool is unavailable");
- assert_eq!(
- Some(&CoalescerError::Unavailable),
- unavailable.downcast_ref::<CoalescerError>()
- );
- let timed_out =
- coalesced_work_result::<()>(Err(TimedSemaphoreError::RequestTimeout.into()))
- .expect_err("a deadline is a timeout");
- assert_eq!(
- Some(&CoalescerError::RequestTimeout),
- timed_out.downcast_ref::<CoalescerError>()
- );
- let failed = coalesced_work_result::<()>(Err(MediaError::MediaDecodeFailed.into()))
- .expect_err("a decode failure passes through");
- assert_eq!(
- Some(&MediaError::MediaDecodeFailed),
- failed.downcast_ref::<MediaError>()
- );
- let ran_out = coalesced_work_result::<()>(Err(MediaError::RequestTimeout.into()))
- .expect_err("work that ran out its deadline passes through");
- assert_eq!(
- Some(&MediaError::RequestTimeout),
- ran_out.downcast_ref::<MediaError>()
- );
- }
-
- #[test]
- fn a_full_native_queue_answers_the_gateway_timeout_the_old_era_answered() {
- let queue_full: anyhow::Error = TimedSemaphoreError::QueueFull.into();
- assert!(
- transform_error_is_timeout(&queue_full),
- "a shed admission is a timeout for every caller, not just the coalescer"
- );
- let shed = coalesced_work_result::<()>(Err(TimedSemaphoreError::QueueFull.into()))
- .expect_err("a full queue sheds the request");
- let error = shed
- .downcast_ref::<CoalescerError>()
- .copied()
- .expect("the coalescer sees a typed failure");
- let response = crate::server::transform::cache::coalescer_failure_response(
- error,
- "coalescer_timeout_image",
- )
- .expect("a shed transform has a response");
- assert_eq!(
- axum::http::StatusCode::GATEWAY_TIMEOUT,
- response.status(),
- "the old era answered 504 when the admission queue was full"
- );
- assert_eq!(
- Some("coalescer_timeout_image"),
- response
- .extensions()
- .get::<request_log::ErrorReason>()
- .map(|reason| reason.code)
- );
- }
-}
diff --git a/fluxer_media_proxy/src/server/transform/mod.rs b/fluxer_media_proxy/src/server/transform/mod.rs
index 9b8a0abd4..27db9aaa4 100644
--- a/fluxer_media_proxy/src/server/transform/mod.rs
+++ b/fluxer_media_proxy/src/server/transform/mod.rs
@@ -4,8 +4,6 @@ pub(in crate::server) mod cache;
pub mod cache_key;
pub(in crate::server) mod execution;
pub mod parameters;
-#[cfg(test)]
-mod tests;
use crate::{
byte_budget::ByteBudget,
diff --git a/fluxer_media_proxy/src/server/transform/tests.rs b/fluxer_media_proxy/src/server/transform/tests.rs
deleted file mode 100644
index fff49fe95..000000000
--- a/fluxer_media_proxy/src/server/transform/tests.rs
+++ /dev/null
@@ -1,462 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-or-later
-
-use super::*;
-use crate::{
- constants::AssetExtension,
- server::format_policy::{default_transform_quality, transform_static_quality_default},
- test_fixtures::{
- JXL_64_LOSSY_ALPHA, JXL_4100_LOSSY_ALPHA, JXL_7000X4000_LOSSY_RGB, fixture_h264_mp4,
- fixture_jpeg, synthetic_png,
- },
-};
-use axum::body::to_bytes;
-
-const TRANSFORM_WIDTH: u32 = 32;
-
-fn test_runtime() -> TransformRuntime {
- let cfg = Config::load_from_iter([(
- "FLUXER_MEDIA_PROXY_SECRET_KEY".to_owned(),
- "secret".to_owned(),
- )])
- .expect("config loads");
- TransformRuntime::new(&cfg, &Arc::new(metrics::Metrics::new()))
- .expect("test transform runtime limits are valid")
-}
-
-fn attachment_cache_key(
- filename: &str,
- quality: ImageQuality,
- resize_mode: Option<ResizeMode>,
-) -> String {
- transform_cache_key(TransformCacheKeyInput {
- route: TransformRoute::Attachment,
- asset_kind: None,
- cache_identity: filename,
- width: Some(TRANSFORM_WIDTH),
- height: None,
- format: OutputFormat::WebP,
- quality: Some(quality),
- animated: false,
- effort: None,
- resize_mode,
- })
-}
-
-async fn transform_qualities_reaching_the_encoder(
- fixture: Vec<u8>,
- content_type: &str,
- filename: &str,
-) -> Vec<ImageQuality> {
- let runtime = test_runtime();
- let params = HashMap::from([
- ("format".to_owned(), "webp".to_owned()),
- ("width".to_owned(), TRANSFORM_WIDTH.to_string()),
- ]);
- let headers = HeaderMap::new();
- let response = serve_bytes_or_transform(
- &runtime,
- ServeBytesRequest {
- method: Method::GET,
- data: Bytes::from(fixture),
- content_type: content_type.to_owned(),
- cache_identity: filename,
- filename,
- route: TransformRoute::Attachment,
- params: &params,
- headers: &headers,
- },
- )
- .await;
- assert_eq!(
- StatusCode::OK,
- response.status(),
- "{filename} did not reach the transform path"
- );
- [
- ImageQuality::Lossless,
- ImageQuality::High,
- ImageQuality::Auto,
- ImageQuality::Low,
- ]
- .into_iter()
- .filter(|quality| {
- [None, Some(ResizeMode::Fit)]
- .into_iter()
- .any(|resize_mode| {
- runtime
- .cache()
- .get(&attachment_cache_key(filename, *quality, resize_mode))
- .is_some()
- })
- })
- .collect()
-}
-
-#[test]
-fn lossy_source_transform_defaults_to_lossy_output() {
- for lossy in [
- AssetExtension::Jpeg,
- AssetExtension::Heic,
- AssetExtension::Heif,
- AssetExtension::Jxl,
- ] {
- assert_eq!(
- ImageQuality::High,
- default_transform_quality(
- OutputFormat::WebP,
- false,
- transform_static_quality_default(Some(lossy))
- ),
- "{} source must not be re-encoded losslessly",
- lossy.name()
- );
- }
- for lossless in [
- AssetExtension::Png,
- AssetExtension::Apng,
- AssetExtension::Gif,
- AssetExtension::Avif,
- AssetExtension::Webp,
- ] {
- assert_eq!(
- ImageQuality::Lossless,
- default_transform_quality(
- OutputFormat::WebP,
- false,
- transform_static_quality_default(Some(lossless))
- ),
- "{} source must keep the lossless default",
- lossless.name()
- );
- }
- assert_eq!(
- ImageQuality::Lossless,
- default_transform_quality(
- OutputFormat::WebP,
- false,
- transform_static_quality_default(None)
- )
- );
- assert_eq!(
- ImageQuality::Auto,
- default_transform_quality(
- OutputFormat::WebP,
- true,
- transform_static_quality_default(Some(AssetExtension::Jpeg))
- )
- );
-}
-
-#[tokio::test]
-async fn image_transform_route_sends_lossy_sources_to_a_lossy_encode() {
- assert_eq!(
- vec![ImageQuality::High],
- transform_qualities_reaching_the_encoder(fixture_jpeg(), "image/jpeg", "photo.jpg").await,
- "a jpeg attachment must not be re-encoded losslessly"
- );
- assert_eq!(
- vec![ImageQuality::High],
- transform_qualities_reaching_the_encoder(
- JXL_64_LOSSY_ALPHA.to_vec(),
- "image/jxl",
- "photo.jxl"
- )
- .await,
- "a jxl attachment must not be re-encoded losslessly"
- );
- assert_eq!(
- vec![ImageQuality::Lossless],
- transform_qualities_reaching_the_encoder(synthetic_png(48, 48), "image/png", "art.png")
- .await,
- "a png attachment must keep the lossless encode"
- );
-}
-
-#[tokio::test]
-async fn video_poster_transform_route_sends_decoded_frames_to_a_lossy_encode() {
- assert_eq!(
- vec![ImageQuality::High],
- transform_qualities_reaching_the_encoder(fixture_h264_mp4(), "video/mp4", "clip.mp4").await,
- "a decoded video frame is already lossy and must not be re-encoded losslessly"
- );
-}
-
-#[tokio::test]
-async fn a_redundant_format_parameter_shares_the_implicit_transform_cache_entry() {
- const KEY: &str = "attachment:art.png|asset_kind=not-applicable|w=32|h=none|fmt=png|q=lossless|anim=false|effort=none|resize=fit";
- for params in [
- HashMap::from([("width".to_owned(), TRANSFORM_WIDTH.to_string())]),
- HashMap::from([
- ("width".to_owned(), TRANSFORM_WIDTH.to_string()),
- ("format".to_owned(), "png".to_owned()),
- ]),
- ] {
- let runtime = test_runtime();
- let headers = HeaderMap::new();
- let response = serve_bytes_or_transform(
- &runtime,
- ServeBytesRequest {
- method: Method::GET,
- data: Bytes::from(synthetic_png(48, 48)),
- content_type: "image/png".to_owned(),
- cache_identity: "art.png",
- filename: "art.png",
- route: TransformRoute::Attachment,
- params: &params,
- headers: &headers,
- },
- )
- .await;
- assert_eq!(StatusCode::OK, response.status());
- assert!(
- runtime.cache().get(KEY).is_some(),
- "naming the format the url already implies must not mint a second cache key"
- );
- }
-}
-
-async fn attachment_transform(data: &[u8], filename: &str, params: &[(&str, &str)]) -> Response {
- let params = params
- .iter()
- .map(|(name, value)| ((*name).to_owned(), (*value).to_owned()))
- .collect::<HashMap<_, _>>();
- serve_bytes_or_transform(
- &test_runtime(),
- ServeBytesRequest {
- method: Method::GET,
- data: Bytes::copy_from_slice(data),
- content_type: "application/octet-stream".to_owned(),
- cache_identity: filename,
- filename,
- route: TransformRoute::Attachment,
- params: &params,
- headers: &HeaderMap::new(),
- },
- )
- .await
-}
-
-#[tokio::test]
-async fn a_full_size_transform_too_large_to_process_serves_the_original() {
- let png = synthetic_png(6145, 4097);
- let response = attachment_transform(&png, "big.png", &[("format", "webp")]).await;
- assert_eq!(StatusCode::OK, response.status());
- assert_eq!(
- Some("image/png"),
- response
- .headers()
- .get(header::CONTENT_TYPE)
- .and_then(|value| value.to_str().ok())
- );
- let body = to_bytes(response.into_body(), usize::MAX)
- .await
- .expect("body");
- assert_eq!(png, body);
-}
-
-#[tokio::test]
-async fn a_waiter_releases_its_source_while_the_leader_runs() {
- const KEY: &str = "attachment:art.png|asset_kind=not-applicable|w=none|h=none|fmt=webp|q=lossless|anim=false|effort=none|resize=fit";
- let runtime = test_runtime();
- let params = HashMap::from([("format".to_owned(), "webp".to_owned())]);
- let headers = HeaderMap::new();
- let source = Bytes::from(synthetic_png(48, 48));
- let probe = source.clone();
- let (release, released) = tokio::sync::oneshot::channel::<()>();
- let leader =
- runtime
- .cache()
- .get_or_run(KEY.to_owned(), OutputFormat::WebP, None, || async move {
- let _ = released.await;
- Ok(MediaBytes::from(vec![0u8; 4]))
- });
- let waiter = async {
- let response = serve_bytes_or_transform(
- &runtime,
- ServeBytesRequest {
- method: Method::GET,
- data: source,
- content_type: "application/octet-stream".to_owned(),
- cache_identity: "art.png",
- filename: "art.png",
- route: TransformRoute::Attachment,
- params: &params,
- headers: &headers,
- },
- );
- tokio::pin!(response);
- assert!(
- tokio::time::timeout(std::time::Duration::from_millis(100), response.as_mut())
- .await
- .is_err(),
- "the request waits on the running leader"
- );
- assert!(
- probe.is_unique(),
- "a waiter must not pin its source while the leader runs"
- );
- let _ = release.send(());
- response.await
- };
- let (leader, response) = tokio::join!(leader, waiter);
- leader.expect("leader publishes");
- assert_eq!(StatusCode::OK, response.status());
-}
-
-#[tokio::test]
-async fn a_resized_transform_too_large_to_process_still_fails() {
- let response = attachment_transform(
- &synthetic_png(6145, 4097),
- "big.png",
- &[("format", "webp"), ("width", "6145")],
- )
- .await;
- assert_eq!(StatusCode::BAD_REQUEST, response.status());
-}
-
-#[tokio::test]
-async fn a_full_size_transform_too_large_to_process_never_serves_an_undisplayable_original() {
- let response =
- attachment_transform(JXL_4100_LOSSY_ALPHA, "big.jxl", &[("format", "webp")]).await;
- assert_eq!(StatusCode::BAD_REQUEST, response.status());
-}
-
-#[tokio::test]
-async fn a_full_size_jxl_above_the_static_output_cap_is_downscaled() {
- let response =
- attachment_transform(JXL_7000X4000_LOSSY_RGB, "big.jxl", &[("format", "webp")]).await;
- assert_eq!(StatusCode::OK, response.status());
- assert_eq!(
- Some("image/webp"),
- response
- .headers()
- .get(header::CONTENT_TYPE)
- .and_then(|value| value.to_str().ok())
- );
-}
-
-async fn jxl_attachment(
- runtime: &TransformRuntime,
- method: Method,
- params: &HashMap<String, String>,
- headers: &HeaderMap,
-) -> Response {
- serve_bytes_or_transform(
- runtime,
- ServeBytesRequest {
- method,
- data: Bytes::from_static(JXL_64_LOSSY_ALPHA),
- content_type: "image/jxl".to_owned(),
- cache_identity: "art.jxl",
- filename: "art.jxl",
- route: TransformRoute::Attachment,
- params,
- headers,
- },
- )
- .await
-}
-
-#[tokio::test]
-async fn a_ranged_request_for_a_transform_is_answered_with_the_whole_body() {
- let runtime = test_runtime();
- let transform = HashMap::from([("format".to_owned(), "webp".to_owned())]);
- let mut ranged = HeaderMap::new();
- ranged.insert(header::RANGE, "bytes=0-3".parse().unwrap());
- let mut bodies = Vec::new();
- for (method, headers) in [
- (Method::GET, &ranged),
- (Method::HEAD, &ranged),
- (Method::GET, &HeaderMap::new()),
- ] {
- let response = jxl_attachment(&runtime, method, &transform, headers).await;
- assert_eq!(StatusCode::OK, response.status());
- assert!(!response.headers().contains_key(header::ACCEPT_RANGES));
- assert!(!response.headers().contains_key(header::CONTENT_RANGE));
- let declared_len = response
- .headers()
- .get(header::CONTENT_LENGTH)
- .and_then(|value| value.to_str().ok())
- .and_then(|value| value.parse::<usize>().ok());
- let body = to_bytes(response.into_body(), usize::MAX)
- .await
- .expect("body");
- bodies.push((declared_len, body));
- }
- let [
- (ranged_len, ranged_body),
- (head_len, head_body),
- (whole_len, whole_body),
- ] = bodies.try_into().expect("three responses");
- assert!(whole_body.len() > 4);
- assert_eq!(Some(whole_body.len()), whole_len);
- assert_eq!(whole_len, ranged_len);
- assert_eq!(whole_len, head_len);
- assert_eq!(whole_body, ranged_body);
- assert!(head_body.is_empty());
-
- let original = jxl_attachment(&runtime, Method::GET, &HashMap::new(), &ranged).await;
- assert_eq!(StatusCode::PARTIAL_CONTENT, original.status());
- assert_eq!(
- Some("bytes"),
- original
- .headers()
- .get(header::ACCEPT_RANGES)
- .and_then(|value| value.to_str().ok())
- );
- let original_body = to_bytes(original.into_body(), usize::MAX)
- .await
- .expect("body");
- assert_eq!(&JXL_64_LOSSY_ALPHA[..4], &original_body[..]);
-}
-
-#[test]
-fn the_budget_leaves_room_for_the_cache_and_headroom() {
- assert_eq!(
- 2560 << 20,
- native_memory_budget_bytes(4096 << 20, 1024 << 20)
- );
-}
-
-#[test]
-fn a_cache_larger_than_the_pod_takes_at_most_half_of_it() {
- assert_eq!(192 << 20, native_memory_budget_bytes(512 << 20, 1 << 30));
- assert_eq!(384 << 20, native_memory_budget_bytes(1 << 30, 1 << 30));
-}
-
-#[test]
-fn the_budget_never_drops_below_its_floor() {
- assert_eq!(128 << 20, native_memory_budget_bytes(64 << 20, 0));
-}
-
-#[tokio::test]
-async fn an_animated_transform_over_the_work_budget_serves_the_original() {
- let webp = crate::test_fixtures::ANIMATED_WEBP_1024_17_FRAMES;
- let response = attachment_transform(
- webp,
- "anim.webp",
- &[("format", "webp"), ("animated", "true"), ("width", "1000")],
- )
- .await;
- assert_eq!(StatusCode::OK, response.status());
- let body = to_bytes(response.into_body(), usize::MAX)
- .await
- .expect("body");
- assert_eq!(webp, body.as_ref());
-}
-
-#[tokio::test]
-async fn an_animated_transform_within_the_work_budget_is_transformed() {
- let webp = crate::test_fixtures::ANIMATED_WEBP_1024_17_FRAMES;
- let response = attachment_transform(
- webp,
- "anim.webp",
- &[("format", "webp"), ("animated", "true"), ("width", "128")],
- )
- .await;
- assert_eq!(StatusCode::OK, response.status());
- let body = to_bytes(response.into_body(), usize::MAX)
- .await
- .expect("body");
- assert_ne!(webp, body.as_ref());
-}
diff --git a/fluxer_media_proxy/src/spool.rs b/fluxer_media_proxy/src/spool.rs
index 5c86af139..cbc604ded 100644
--- a/fluxer_media_proxy/src/spool.rs
+++ b/fluxer_media_proxy/src/spool.rs
@@ -49,14 +49,6 @@ impl SpooledBody {
let map = unsafe { Mmap::map(file)? };
Ok(MappedSpool { map, _spool: self })
}
-
- pub fn len(&self) -> u64 {
- self.length
- }
-
- pub fn is_empty(&self) -> bool {
- self.length == 0
- }
}
pub struct MappedSpool {
@@ -150,210 +142,3 @@ async fn spool_body_inner(
file.seek(SeekFrom::Start(0)).await?;
Ok((file, written))
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use bytes::Bytes;
- use http_body::Frame;
- use http_body_util::StreamBody;
- use tokio::io::AsyncReadExt;
-
- const SPOOL_RESERVATION_BYTES: u64 = 600 * 1024 * 1024;
- const SPOOL_CEILING_BYTES: u64 = 1000 * 1024 * 1024;
-
- async fn budgeted_spool(
- dir: &Path,
- declared_length: Option<u64>,
- ) -> Result<SpooledBody, SpoolError> {
- spool_to_temp(
- Body::from(vec![0u8; 16]),
- declared_length,
- SPOOL_RESERVATION_BYTES,
- dir,
- 64 * 1024,
- SPOOL_CEILING_BYTES,
- )
- .await
- }
-
- fn body_with_trailers(chunks: &[&[u8]]) -> Body {
- let mut frames: Vec<Result<Frame<Bytes>, std::io::Error>> = chunks
- .iter()
- .map(|chunk| Ok(Frame::data(Bytes::copy_from_slice(chunk))))
- .collect();
- let mut trailers = http::HeaderMap::new();
- trailers.insert("x-checksum", http::HeaderValue::from_static("deadbeef"));
- frames.push(Ok(Frame::trailers(trailers)));
- Body::new(StreamBody::new(futures_util::stream::iter(frames)))
- }
-
- #[tokio::test]
- async fn spools_full_body_to_disk() {
- let payload = vec![0xABu8; 4096];
- let body = Body::from(payload.clone());
- let dir = tempfile::tempdir().unwrap();
- let spooled = spool_to_temp(
- body,
- Some(payload.len() as u64),
- 1 << 20,
- dir.path(),
- 1024,
- 1 << 30,
- )
- .await
- .unwrap();
- let (mut file, len) = spooled.into_parts();
- assert_eq!(len, payload.len() as u64);
- let mut read_back = Vec::with_capacity(payload.len());
- file.read_to_end(&mut read_back).await.unwrap();
- assert_eq!(read_back, payload);
- }
-
- #[tokio::test]
- async fn rejects_payload_longer_than_declared() {
- let body = Body::from(vec![0u8; 2048]);
- let dir = tempfile::tempdir().unwrap();
- let err = spool_to_temp(body, Some(1024), 1 << 20, dir.path(), 1024, 1 << 30)
- .await
- .unwrap_err();
- assert!(matches!(err, SpoolError::PayloadTooLarge));
- }
-
- #[tokio::test]
- async fn rejects_payload_shorter_than_declared() {
- let body = Body::from(vec![0u8; 1024]);
- let dir = tempfile::tempdir().unwrap();
- let err = spool_to_temp(body, Some(2048), 1 << 20, dir.path(), 1024, 1 << 30)
- .await
- .unwrap_err();
- assert!(matches!(err, SpoolError::PayloadShortRead));
- }
-
- #[tokio::test]
- async fn rejects_when_budget_exhausted() {
- let body = Body::from(vec![0u8; 1024]);
- let dir = tempfile::tempdir().unwrap();
- let err = spool_to_temp(body, Some(1024), 1 << 20, dir.path(), 1024, 512)
- .await
- .unwrap_err();
- assert!(matches!(err, SpoolError::BudgetExhausted));
- }
-
- #[tokio::test]
- async fn spools_unknown_length_body_up_to_limit() {
- let payload = vec![0xCDu8; 2048];
- let body = Body::from(payload.clone());
- let dir = tempfile::tempdir().unwrap();
- let spooled = spool_to_temp(body, None, 4096, dir.path(), 1024, 1 << 30)
- .await
- .unwrap();
- let (mut file, len) = spooled.into_parts();
- assert_eq!(len, payload.len() as u64);
- let mut read_back = Vec::with_capacity(payload.len());
- file.read_to_end(&mut read_back).await.unwrap();
- assert_eq!(read_back, payload);
- }
-
- #[tokio::test]
- async fn rejects_unknown_length_body_over_limit() {
- let body = Body::from(vec![0u8; 2048]);
- let dir = tempfile::tempdir().unwrap();
- let err = spool_to_temp(body, None, 1024, dir.path(), 1024, 1 << 30)
- .await
- .unwrap_err();
- assert!(matches!(err, SpoolError::PayloadTooLarge));
- }
-
- #[tokio::test]
- async fn concurrent_spools_share_and_release_one_total_budget() {
- let dir = tempfile::tempdir().unwrap();
- let held = budgeted_spool(dir.path(), None).await.unwrap();
- assert_eq!(held.len(), 16);
- let contended = budgeted_spool(dir.path(), None).await.unwrap_err();
- assert!(matches!(contended, SpoolError::BudgetExhausted));
-
- drop(held);
- let failed = budgeted_spool(dir.path(), Some(SPOOL_RESERVATION_BYTES))
- .await
- .unwrap_err();
- assert!(matches!(failed, SpoolError::PayloadShortRead));
-
- let after_release = budgeted_spool(dir.path(), None).await.unwrap();
- assert_eq!(after_release.len(), 16);
- }
-
- #[tokio::test]
- async fn a_trailer_frame_adds_no_payload_towards_the_declared_length() {
- let dir = tempfile::tempdir().unwrap();
- let short = spool_to_temp(
- body_with_trailers(&[b"hello".as_slice()]),
- Some(8),
- 1 << 20,
- dir.path(),
- 1024,
- 1 << 30,
- )
- .await
- .unwrap_err();
- assert!(matches!(short, SpoolError::PayloadShortRead));
-
- let exact = spool_to_temp(
- body_with_trailers(&[b"hel".as_slice(), b"".as_slice(), b"lo".as_slice()]),
- Some(5),
- 1 << 20,
- dir.path(),
- 1024,
- 1 << 30,
- )
- .await
- .unwrap();
- let (mut file, len) = exact.into_parts();
- assert_eq!(len, 5);
- let mut read_back = Vec::new();
- file.read_to_end(&mut read_back).await.unwrap();
- assert_eq!(read_back, b"hello".as_slice());
- }
-
- #[tokio::test]
- async fn no_spool_file_is_ever_left_behind_in_the_spool_directory() {
- let dir = tempfile::tempdir().unwrap();
- for (declared, max_body_bytes, payload) in [
- (Some(1024u64), 1 << 20, 2048usize),
- (Some(2048), 1 << 20, 1024),
- (None, 1024, 2048),
- ] {
- assert!(
- spool_to_temp(
- Body::from(vec![0u8; payload]),
- declared,
- max_body_bytes,
- dir.path(),
- 1024,
- 1 << 30,
- )
- .await
- .is_err()
- );
- assert!(spool_dir_is_empty(dir.path()).await);
- }
- let spooled = spool_to_temp(
- Body::from(vec![0u8; 1024]),
- Some(1024),
- 1 << 20,
- dir.path(),
- 1024,
- 1 << 30,
- )
- .await
- .unwrap();
- assert!(spool_dir_is_empty(dir.path()).await);
- drop(spooled);
- assert!(spool_dir_is_empty(dir.path()).await);
- }
-
- async fn spool_dir_is_empty(dir: &Path) -> bool {
- let mut entries = tokio::fs::read_dir(dir).await.unwrap();
- entries.next_entry().await.unwrap().is_none()
- }
-}
diff --git a/fluxer_media_proxy/src/storage/tests/local.rs b/fluxer_media_proxy/src/storage/tests/local.rs
deleted file mode 100644
index 9c206b5cb..000000000
--- a/fluxer_media_proxy/src/storage/tests/local.rs
+++ /dev/null
@@ -1,100 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-or-later
-
-use super::{rendered_counter, store, store_with_storage_metrics, test_config};
-use crate::{metrics::Metrics, storage::StorageError};
-use http::StatusCode;
-
-#[tokio::test]
-async fn local_write_read_head_roundtrip() {
- let tmp = tempfile::tempdir().unwrap();
- let store = store(test_config(&tmp.path().canonicalize().unwrap()));
- store
- .write_object("cdn", "a/b.txt", b"hello", "text/plain")
- .await
- .unwrap();
- let head = store.head_object("cdn", "a/b.txt").await.unwrap();
- assert_eq!(5, head.content_length);
- let object = store.read_object("cdn", "a/b.txt").await.unwrap();
- assert_eq!(b"hello", &object.data[..]);
-}
-
-#[tokio::test]
-async fn local_stream_honors_range_without_buffered_read() {
- let tmp = tempfile::tempdir().unwrap();
- let store = store(test_config(&tmp.path().canonicalize().unwrap()));
- store
- .write_object("cdn", "a/b.txt", b"hello world", "text/plain")
- .await
- .unwrap();
- let object = store
- .stream_object("cdn", "a/b.txt", Some("bytes=6-10"))
- .await
- .unwrap();
- assert_eq!(StatusCode::PARTIAL_CONTENT, object.status);
- assert_eq!(Some(5), object.content_length);
- let body = axum::body::to_bytes(object.body, 16).await.unwrap();
- assert_eq!(b"world", &body[..]);
-}
-
-#[tokio::test]
-async fn local_reads_refuse_to_follow_a_symlinked_key() {
- let tmp = tempfile::tempdir().unwrap();
- let root = tmp.path().canonicalize().unwrap();
- let store = store(test_config(&root));
- store
- .write_object("cdn", "a/b.txt", b"hello", "text/plain")
- .await
- .unwrap();
- std::os::unix::fs::symlink(root.join("cdn/a/b.txt"), root.join("cdn/a/link.txt")).unwrap();
- assert!(matches!(
- store.read_object("cdn", "a/link.txt").await,
- Err(StorageError::InvalidKey)
- ));
-}
-
-#[tokio::test]
-async fn head_object_leaves_the_storage_counters_untouched() {
- let tmp = tempfile::tempdir().unwrap();
- let metrics = Metrics::new();
- let store = store_with_storage_metrics(
- test_config(&tmp.path().canonicalize().unwrap()),
- metrics.storage(),
- );
- store
- .write_object("cdn", "a/b.txt", b"hello", "text/plain")
- .await
- .unwrap();
-
- store.head_object("cdn", "a/b.txt").await.unwrap();
- assert!(store.head_object("cdn", "missing.txt").await.is_err());
- assert_eq!(
- 0,
- rendered_counter(&metrics, "fluxer_media_proxy_storage_hits_total")
- );
- assert_eq!(
- 0,
- rendered_counter(&metrics, "fluxer_media_proxy_storage_misses_total")
- );
- assert_eq!(
- 0,
- rendered_counter(&metrics, "fluxer_media_proxy_storage_errors_total")
- );
-
- store.read_object("cdn", "a/b.txt").await.unwrap();
- assert_eq!(
- 1,
- rendered_counter(&metrics, "fluxer_media_proxy_storage_hits_total")
- );
- assert!(matches!(
- store.read_object("cdn", "missing.txt").await,
- Err(StorageError::NotFound)
- ));
- assert_eq!(
- 1,
- rendered_counter(&metrics, "fluxer_media_proxy_storage_misses_total")
- );
- assert_eq!(
- 0,
- rendered_counter(&metrics, "fluxer_media_proxy_storage_errors_total")
- );
-}
diff --git a/fluxer_media_proxy/src/storage/tests/relay.rs b/fluxer_media_proxy/src/storage/tests/relay.rs
deleted file mode 100644
index a8fe870b9..000000000
--- a/fluxer_media_proxy/src/storage/tests/relay.rs
+++ /dev/null
@@ -1,82 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-or-later
-
-use super::{fake_s3, store};
-use crate::storage::{RelayBody, RelayBodyChunks, RelayPutOptions, s3::UNSIGNED_PAYLOAD};
-use bytes::Bytes;
-use http::header;
-
-fn chunks(frames: &'static [&'static [u8]]) -> RelayBodyChunks {
- Box::pin(futures_util::stream::iter(
- frames
- .iter()
- .map(|frame| Ok(Bytes::from_static(frame)))
- .collect::<Vec<_>>(),
- ))
-}
-
-#[tokio::test]
-async fn relay_put_s3_streams_body_with_unsigned_payload() {
- let fake = fake_s3().await;
- fake.set_put_etag("\"etag-123\"");
- let tmp = tempfile::tempdir().unwrap();
- let store = store(fake.config(tmp.path()));
-
- let etag = store
- .relay_put_object(
- "uploads",
- "guild/streamed.bin",
- RelayPutOptions {
- body: RelayBody::Streamed(chunks(&[b"hello ", b"world"])),
- content_length: 11,
- content_type: Some("application/octet-stream".to_owned()),
- upload_id: Some("upload-1".to_owned()),
- part_number: Some(2),
- timeout_ms: 5_000,
- },
- )
- .await
- .unwrap();
-
- assert_eq!(Some("\"etag-123\"".to_owned()), etag);
- let (_, uri, headers, body) = fake.last_request();
- assert_eq!("/uploads/guild/streamed.bin", uri.path());
- assert_eq!(Some("partNumber=2&uploadId=upload-1"), uri.query());
- assert_eq!(
- UNSIGNED_PAYLOAD,
- headers.get("x-amz-content-sha256").unwrap()
- );
- assert_eq!("11", headers.get(header::CONTENT_LENGTH).unwrap());
- assert!(headers.get(header::TRANSFER_ENCODING).is_none());
- assert!(
- headers
- .get(header::AUTHORIZATION)
- .unwrap()
- .to_str()
- .unwrap()
- .starts_with("AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/")
- );
- assert_eq!(b"hello world", body.as_ref());
-}
-
-#[tokio::test]
-async fn relay_put_s3_fails_when_stream_ends_short() {
- let fake = fake_s3().await;
- let tmp = tempfile::tempdir().unwrap();
- let store = store(fake.config(tmp.path()));
-
- let result = store
- .relay_put_object(
- "uploads",
- "guild/short.bin",
- RelayPutOptions {
- body: RelayBody::Streamed(chunks(&[b"only"])),
- content_length: 32,
- content_type: None,
- upload_id: None,
- part_number: None,
- timeout_ms: 5_000,
- },
- )
- .await;
- assert!(result.is_err());
-}
diff --git a/fluxer_media_proxy/src/storage/tests/response_body.rs b/fluxer_media_proxy/src/storage/tests/response_body.rs
deleted file mode 100644
index 190dfddcc..000000000
--- a/fluxer_media_proxy/src/storage/tests/response_body.rs
+++ /dev/null
@@ -1,267 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-or-later
-
-use crate::{
- byte_budget::ByteBudget,
- storage::{
- StorageError,
- response_body::{
- ByteStream, LocalStreamBufferPool, exact_byte_stream, exact_response_stream,
- local_reader_stream, read_exact_bytes, read_response_bytes,
- },
- },
-};
-use bytes::Bytes;
-use futures_util::{TryStreamExt as _, stream};
-
-fn byte_stream(chunks: Vec<Result<Bytes, std::io::Error>>) -> ByteStream {
- Box::pin(stream::iter(chunks))
-}
-
-fn provider_response(body: Vec<u8>) -> reqwest::Response {
- reqwest::Response::from(http::Response::new(body))
-}
-
-#[tokio::test]
-async fn exact_reader_accepts_exact_length_and_rejects_short_or_long_sources() {
- let budget = ByteBudget::new(64);
- let exact = read_exact_bytes(&b"abcdef"[..], 6, &budget)
- .await
- .expect("exact reader");
- assert_eq!(exact.as_ref(), b"abcdef");
-
- assert!(matches!(
- read_exact_bytes(&b"abc"[..], 4, &budget).await,
- Err(StorageError::ObjectChanged)
- ));
- assert!(matches!(
- read_exact_bytes(&b"abcde"[..], 4, &budget).await,
- Err(StorageError::ObjectChanged)
- ));
- assert!(matches!(
- read_exact_bytes(&b"a"[..], 1, &ByteBudget::new(0)).await,
- Err(StorageError::BufferBudgetExhausted)
- ));
-}
-
-#[tokio::test]
-async fn local_reader_stream_replenishes_owned_buffers_and_terminates() {
- let pool = LocalStreamBufferPool::new(4, 1).expect("local stream buffer pool");
- let stream =
- local_reader_stream(&b"abcdefgh"[..], pool.clone(), 4).expect("local reader stream");
- let data = stream
- .try_fold(Vec::new(), |mut data, chunk| async move {
- data.extend_from_slice(&chunk);
- Ok(data)
- })
- .await
- .expect("stream bytes");
- assert_eq!(data, b"abcdefgh");
-
- let held =
- local_reader_stream(&b"abcd"[..], pool.clone(), 4).expect("held local reader stream");
- let saturated = local_reader_stream(&b"xyz"[..], pool, 4)
- .expect("a saturated pool still serves the stream from an unpooled buffer");
- let data = saturated
- .try_fold(Vec::new(), |mut data, chunk| async move {
- data.extend_from_slice(&chunk);
- Ok(data)
- })
- .await
- .expect("stream bytes");
- assert_eq!(data, b"xyz");
- drop(held);
-}
-
-#[tokio::test]
-async fn a_saturated_buffer_pool_serves_a_live_stream_from_a_small_fallback_buffer() {
- use futures_util::StreamExt as _;
-
- const CHUNK: usize = 16 * 1024;
- static SOURCE: [u8; CHUNK * 2] = [b'z'; CHUNK * 2];
-
- let pool = LocalStreamBufferPool::new(CHUNK, 1).expect("local stream buffer pool");
- let mut stream = local_reader_stream(&SOURCE[..], pool, CHUNK).expect("local reader stream");
- let pooled = stream
- .next()
- .await
- .expect("a first chunk")
- .expect("a readable first chunk");
- assert_eq!(CHUNK, pooled.len());
- let fallback = stream
- .next()
- .await
- .expect("a second chunk")
- .expect("a saturated pool must not fail a stream that already sent a chunk");
- assert!(
- fallback.len() < pooled.len(),
- "a fallback buffer must not hold a full pooled chunk: {}",
- fallback.len()
- );
- drop(pooled);
-}
-
-#[tokio::test]
-async fn a_saturated_pool_still_replenishes_mid_stream_instead_of_truncating() {
- let pool = LocalStreamBufferPool::new(8, 1).expect("local stream buffer pool");
- let held = local_reader_stream(&b"12345678"[..], pool.clone(), 8).expect("held local stream");
-
- let source = vec![b'z'; 64 * 1024];
- let stream = local_reader_stream(std::io::Cursor::new(source.clone()), pool, 8)
- .expect("a saturated pool still starts the stream");
- let data = stream
- .try_fold(Vec::new(), |mut data, chunk| async move {
- data.extend_from_slice(&chunk);
- Ok(data)
- })
- .await
- .expect("a saturated pool must stream the whole body, not fail partway");
- assert_eq!(data.len(), source.len());
- assert_eq!(data, source);
- drop(held);
-}
-
-#[tokio::test]
-async fn exact_stream_rejects_short_long_and_erroring_sources() {
- let exact = exact_byte_stream(
- byte_stream(vec![
- Ok(Bytes::new()),
- Ok(Bytes::from_static(b"ab")),
- Ok(Bytes::from_static(b"cd")),
- ]),
- 4,
- );
- let chunks = exact.try_collect::<Vec<_>>().await.expect("exact stream");
- assert_eq!(
- chunks,
- vec![Bytes::from_static(b"ab"), Bytes::from_static(b"cd")]
- );
-
- let short = exact_byte_stream(byte_stream(vec![Ok(Bytes::from_static(b"abc"))]), 4);
- let error = short
- .try_collect::<Vec<_>>()
- .await
- .expect_err("short stream");
- assert_eq!(error.kind(), std::io::ErrorKind::UnexpectedEof);
-
- let long = exact_byte_stream(byte_stream(vec![Ok(Bytes::from_static(b"abcde"))]), 4);
- let error = long.try_collect::<Vec<_>>().await.expect_err("long stream");
- assert_eq!(error.kind(), std::io::ErrorKind::InvalidData);
-
- let source_error = exact_byte_stream(
- byte_stream(vec![Err(std::io::Error::other("source failed"))]),
- 1,
- );
- let error = source_error
- .try_collect::<Vec<_>>()
- .await
- .expect_err("source error");
- assert_eq!(error.kind(), std::io::ErrorKind::Other);
-}
-
-#[tokio::test]
-async fn provider_response_readers_hold_the_declared_length_and_the_byte_budget() {
- let budget = ByteBudget::new(1 << 20);
- let exact = read_response_bytes(provider_response(b"abcdef".to_vec()), 6, &budget)
- .await
- .expect("response bytes");
- assert_eq!(exact.as_ref(), b"abcdef");
-
- assert!(matches!(
- read_response_bytes(provider_response(b"abc".to_vec()), 4, &budget).await,
- Err(StorageError::ObjectChanged)
- ));
- assert!(matches!(
- read_response_bytes(provider_response(b"abcde".to_vec()), 4, &budget).await,
- Err(StorageError::ObjectChanged)
- ));
- assert!(matches!(
- read_response_bytes(provider_response(b"a".to_vec()), 1, &ByteBudget::new(0)).await,
- Err(StorageError::BufferBudgetExhausted)
- ));
-
- let streamed = exact_response_stream(provider_response(b"abcdef".to_vec()), 6)
- .try_fold(Vec::new(), |mut data, chunk| async move {
- data.extend_from_slice(&chunk);
- Ok(data)
- })
- .await
- .expect("response stream bytes");
- assert_eq!(streamed, b"abcdef");
-
- let error = exact_response_stream(provider_response(b"abc".to_vec()), 6)
- .try_collect::<Vec<_>>()
- .await
- .expect_err("truncated response stream");
- assert_eq!(error.kind(), std::io::ErrorKind::UnexpectedEof);
-}
-
-#[tokio::test]
-async fn exact_stream_accepts_small_transport_chunks_and_bounds_empty_ones() {
- const CHUNK_BYTES: usize = 64;
- const BODY_BYTES: usize = 8 * 1024;
- let chunks: Vec<Result<Bytes, std::io::Error>> = (0..BODY_BYTES / CHUNK_BYTES)
- .map(|_| Ok(Bytes::from(vec![7u8; CHUNK_BYTES])))
- .collect();
- let streamed = exact_byte_stream(byte_stream(chunks), BODY_BYTES as u64)
- .try_fold(Vec::new(), |mut data, chunk| async move {
- data.extend_from_slice(&chunk);
- Ok(data)
- })
- .await
- .expect("small transport chunks");
- assert_eq!(streamed.len(), BODY_BYTES);
-
- let empty: Vec<Result<Bytes, std::io::Error>> = (0..4096).map(|_| Ok(Bytes::new())).collect();
- let error = exact_byte_stream(byte_stream(empty), 4)
- .try_collect::<Vec<_>>()
- .await
- .expect_err("empty chunk flood");
- assert_eq!(error.kind(), std::io::ErrorKind::InvalidData);
-}
-
-fn chunked_provider_response(chunks: Vec<Bytes>) -> reqwest::Response {
- let body = reqwest::Body::wrap_stream(stream::iter(
- chunks.into_iter().map(Ok::<Bytes, std::io::Error>),
- ));
- reqwest::Response::from(http::Response::new(body))
-}
-
-#[tokio::test]
-async fn response_reader_accepts_transport_chunks_of_any_size_within_the_length() {
- const LARGE_CHUNK_BYTES: usize =
- crate::response_body_limit::RESPONSE_BODY_TRANSPORT_CHUNK_BYTES_MAX + 155_648;
- let budget = ByteBudget::new(4 * LARGE_CHUNK_BYTES);
- let large = read_response_bytes(
- provider_response(vec![7u8; LARGE_CHUNK_BYTES]),
- LARGE_CHUNK_BYTES,
- &budget,
- )
- .await
- .expect("a transport chunk larger than the reserved allowance");
- assert_eq!(large.as_ref().len(), LARGE_CHUNK_BYTES);
-
- const SMALL_CHUNK_BYTES: usize = 1448;
- const SMALL_CHUNKS: usize = 512;
- let small = read_response_bytes(
- chunked_provider_response(
- (0..SMALL_CHUNKS)
- .map(|_| Bytes::from(vec![9u8; SMALL_CHUNK_BYTES]))
- .collect(),
- ),
- SMALL_CHUNK_BYTES * SMALL_CHUNKS,
- &budget,
- )
- .await
- .expect("packet-sized transport chunks");
- assert_eq!(small.as_ref().len(), SMALL_CHUNK_BYTES * SMALL_CHUNKS);
-
- assert!(matches!(
- read_response_bytes(
- chunked_provider_response((0..4096).map(|_| Bytes::new()).collect()),
- 4,
- &budget,
- )
- .await,
- Err(StorageError::ObjectStorage(_))
- ));
-}
diff --git a/fluxer_media_proxy/src/storage/tests/s3.rs b/fluxer_media_proxy/src/storage/tests/s3.rs
deleted file mode 100644
index 189361f98..000000000
--- a/fluxer_media_proxy/src/storage/tests/s3.rs
+++ /dev/null
@@ -1,194 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-or-later
-
-use super::{FakeObject, FakeS3, connection_dropping_front, fake_s3, store};
-use crate::storage::StorageError;
-use http::{Method, StatusCode, header};
-use std::sync::atomic::Ordering;
-
-const LAST_MODIFIED: &str = "Wed, 21 Oct 2015 07:28:00 GMT";
-
-fn stored_object() -> FakeObject {
- FakeObject {
- body: b"hello world".to_vec(),
- etag: Some("\"v1\"".to_owned()),
- content_type: Some("text/plain".to_owned()),
- last_modified: Some(LAST_MODIFIED.to_owned()),
- ..FakeObject::default()
- }
-}
-
-#[tokio::test]
-async fn head_and_ranged_get_match_the_stored_object() {
- let fake = fake_s3().await;
- fake.put_object("cdn/a/b.txt", stored_object());
- let tmp = tempfile::tempdir().unwrap();
- let store = store(fake.config(tmp.path()));
-
- let head = store.head_object("cdn", "a/b.txt").await.unwrap();
- assert_eq!(11, head.content_length);
- assert_eq!("text/plain", head.content_type);
-
- let ranged = store
- .stream_object("cdn", "a/b.txt", Some("bytes=6-10"))
- .await
- .unwrap();
- assert_eq!(StatusCode::PARTIAL_CONTENT, ranged.status);
- assert_eq!(Some(5), ranged.content_length);
- let body = axum::body::to_bytes(ranged.body, 16).await.unwrap();
- assert_eq!(b"world", &body[..]);
-}
-
-#[tokio::test]
-async fn missing_and_failing_objects_map_to_storage_errors() {
- let fake = fake_s3().await;
- fake.put_object(
- "cdn/broken.txt",
- FakeObject {
- status: Some(500),
- ..FakeObject::default()
- },
- );
- let tmp = tempfile::tempdir().unwrap();
- let store = store(fake.config(tmp.path()));
-
- assert!(matches!(
- store.read_object("cdn", "gone.txt").await,
- Err(StorageError::NotFound)
- ));
- assert!(matches!(
- store.head_object("cdn", "gone.txt").await,
- Err(StorageError::NotFound)
- ));
- let failure = store.read_object("cdn", "broken.txt").await;
- assert!(matches!(failure, Err(StorageError::S3(_))));
-}
-
-#[tokio::test]
-async fn a_transient_origin_status_is_retried_while_a_missing_object_is_not() {
- let fake = fake_s3().await;
- fake.put_object(
- "cdn/broken.txt",
- FakeObject {
- status: Some(500),
- ..FakeObject::default()
- },
- );
- let tmp = tempfile::tempdir().unwrap();
- let store = store(fake.config(tmp.path()));
-
- assert!(matches!(
- store.read_object("cdn", "broken.txt").await,
- Err(StorageError::S3(_))
- ));
- assert_eq!(3, fake_gets(&fake, "/cdn/broken.txt"));
-
- assert!(matches!(
- store.read_object("cdn", "gone.txt").await,
- Err(StorageError::NotFound)
- ));
- assert_eq!(1, fake_gets(&fake, "/cdn/gone.txt"));
-
- assert!(matches!(
- store.head_object("cdn", "broken.txt").await,
- Err(StorageError::S3(_))
- ));
- assert_eq!(
- 3,
- fake.requests()
- .iter()
- .filter(|(method, uri, ..)| *method == Method::HEAD && uri.path() == "/cdn/broken.txt")
- .count()
- );
-}
-
-#[tokio::test]
-async fn a_ranged_read_asks_the_origin_for_exactly_the_requested_bytes() {
- let fake = fake_s3().await;
- fake.put_object("cdn/a/b.txt", stored_object());
- let tmp = tempfile::tempdir().unwrap();
- let store = store(fake.config(tmp.path()));
-
- let suffix = store
- .stream_object("cdn", "a/b.txt", Some("bytes=6-10"))
- .await
- .unwrap();
- assert_eq!(Some(5), suffix.content_length);
- assert_eq!(
- "bytes=6-10",
- fake.last_request().2.get(header::RANGE).unwrap()
- );
-}
-
-#[tokio::test]
-async fn stream_s3_surfaces_an_upstream_416_instead_of_a_storage_error() {
- // A range past the end of the object is a routine thing for a video player to ask for while
- // seeking. Mapping the upstream 416 onto a storage error would answer the client 502.
- let fake = fake_s3().await;
- fake.put_object(
- "cdn/video.mp4",
- FakeObject {
- read_status: Some(416),
- ..stored_object()
- },
- );
- let tmp = tempfile::tempdir().unwrap();
- let store = store(fake.config(tmp.path()));
-
- let object = store
- .stream_object("cdn", "video.mp4", Some("bytes=900-999"))
- .await
- .expect("an upstream 416 is an answer about the range, not a storage failure");
-
- assert_eq!(StatusCode::RANGE_NOT_SATISFIABLE, object.status);
- assert_eq!(None, object.byte_range);
- assert_eq!(Some(0), object.content_length);
-}
-
-#[tokio::test]
-async fn a_read_on_a_dropped_connection_is_retried_on_a_new_one() {
- let fake = fake_s3().await;
- fake.put_object("cdn/a/b.txt", stored_object());
- let (endpoint, accepted) = connection_dropping_front(fake.endpoint(), 1).await;
- let tmp = tempfile::tempdir().unwrap();
- let mut cfg = fake.config(tmp.path());
- cfg.storage.s3_endpoint = endpoint;
- let store = store(cfg);
-
- let object = store.read_object("cdn", "a/b.txt").await.unwrap();
-
- assert_eq!(b"hello world", &object.data[..]);
- assert_eq!(2, accepted.load(Ordering::SeqCst));
- assert_eq!(1, fake_gets(&fake, "/cdn/a/b.txt"));
-}
-
-#[tokio::test]
-async fn a_read_that_keeps_losing_its_connection_reports_the_cause() {
- let fake = fake_s3().await;
- fake.put_object("cdn/a/b.txt", stored_object());
- let (endpoint, accepted) = connection_dropping_front(fake.endpoint(), usize::MAX).await;
- let tmp = tempfile::tempdir().unwrap();
- let mut cfg = fake.config(tmp.path());
- cfg.storage.s3_endpoint = endpoint;
- let store = store(cfg);
-
- let error = store
- .head_object("cdn", "a/b.txt")
- .await
- .expect_err("every connection to the origin is dropped");
-
- assert!(
- error
- .to_string()
- .contains("connection closed before message completed"),
- "{error}"
- );
- assert_eq!(3, accepted.load(Ordering::SeqCst));
- assert!(fake.requests().is_empty());
-}
-
-fn fake_gets(fake: &FakeS3, path: &str) -> usize {
- fake.requests()
- .iter()
- .filter(|(method, uri, ..)| *method == Method::GET && uri.path() == path)
- .count()
-}
diff --git a/fluxer_media_proxy/src/storage/tests/s3_read_endpoint.rs b/fluxer_media_proxy/src/storage/tests/s3_read_endpoint.rs
deleted file mode 100644
index 5b593dbf7..000000000
--- a/fluxer_media_proxy/src/storage/tests/s3_read_endpoint.rs
+++ /dev/null
@@ -1,419 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-or-later
-
-use super::{FakeObject, FakeS3, fake_s3, store};
-use crate::{
- config::{BucketStyle, Config},
- storage::{RelayBody, RelayBodyChunks, RelayPutOptions, StorageError, Store},
-};
-use bytes::Bytes;
-use http::{Method, header};
-use std::path::Path;
-
-const PAYLOAD: &[u8] = b"hello world, this is a payload.";
-
-fn payload_object() -> FakeObject {
- FakeObject {
- body: PAYLOAD.to_vec(),
- content_type: Some("image/png".to_owned()),
- ..FakeObject::default()
- }
-}
-
-fn denied_object(status: u16) -> FakeObject {
- FakeObject {
- status: Some(status),
- ..FakeObject::default()
- }
-}
-
-fn fronted_config(origin: &FakeS3, cdn: &FakeS3, root: &Path) -> Config {
- let mut cfg = origin.config(root);
- cfg.storage.s3_read_endpoint = Some(cdn.endpoint().to_owned());
- cfg
-}
-
-fn only_request(fake: &FakeS3) -> super::CapturedRequest {
- let mut requests = fake.requests();
- assert_eq!(1, requests.len(), "expected exactly one captured request");
- requests.remove(0)
-}
-
-fn assert_unsigned(headers: &http::HeaderMap) {
- assert!(headers.get(header::AUTHORIZATION).is_none());
- assert!(headers.get("x-amz-date").is_none());
- assert!(headers.get("x-amz-content-sha256").is_none());
-}
-
-fn assert_signed(headers: &http::HeaderMap) {
- assert!(
- headers
- .get(header::AUTHORIZATION)
- .unwrap()
- .to_str()
- .unwrap()
- .starts_with("AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/")
- );
- assert!(headers.get("x-amz-date").is_some());
- assert!(headers.get("x-amz-content-sha256").is_some());
-}
-
-fn url_store(root: &Path, read_endpoint: &str, style: BucketStyle) -> Store {
- let mut cfg = super::test_config(root);
- cfg.storage.backend = crate::config::StorageBackend::S3;
- cfg.storage.s3_endpoint = "https://s3.example.test".to_owned();
- cfg.storage.s3_read_endpoint = Some(read_endpoint.to_owned());
- cfg.storage.s3_read_bucket_style = style;
- store(cfg)
-}
-
-#[tokio::test]
-async fn read_without_read_endpoint_hits_s3_endpoint_signed() {
- let origin = fake_s3().await;
- origin.put_object("cdn/attachments/1/2/a.png", payload_object());
- let tmp = tempfile::tempdir().unwrap();
- let store = store(origin.config(tmp.path()));
-
- let object = store
- .read_object("cdn", "attachments/1/2/a.png")
- .await
- .unwrap();
-
- assert_eq!(PAYLOAD, object.data.as_ref());
- let (method, uri, headers, _) = only_request(&origin);
- assert_eq!(Method::GET, method);
- assert_eq!("/cdn/attachments/1/2/a.png", uri.path());
- assert_signed(&headers);
-}
-
-#[tokio::test]
-async fn read_endpoint_routes_fronted_bucket_to_cdn_unsigned() {
- let origin = fake_s3().await;
- let cdn = fake_s3().await;
- cdn.put_object("cdn/attachments/1/2/a.png", payload_object());
- let tmp = tempfile::tempdir().unwrap();
- let store = store(fronted_config(&origin, &cdn, tmp.path()));
-
- let object = store
- .read_object("cdn", "attachments/1/2/a.png")
- .await
- .unwrap();
-
- assert_eq!(PAYLOAD, object.data.as_ref());
- assert!(
- origin.requests().is_empty(),
- "S3 endpoint must not be touched"
- );
- let (method, uri, headers, _) = only_request(&cdn);
- assert_eq!(Method::GET, method);
- assert_eq!("/cdn/attachments/1/2/a.png", uri.path());
- assert_unsigned(&headers);
-}
-
-#[tokio::test]
-async fn read_endpoint_rooted_style_omits_bucket_segment() {
- let origin = fake_s3().await;
- let cdn = fake_s3().await;
- cdn.put_object("attachments/1/2/a.png", payload_object());
- let tmp = tempfile::tempdir().unwrap();
- let mut cfg = fronted_config(&origin, &cdn, tmp.path());
- cfg.storage.s3_read_bucket_style = BucketStyle::Rooted;
- let store = store(cfg);
-
- store
- .read_object("cdn", "attachments/1/2/a.png")
- .await
- .unwrap();
-
- let (_, uri, ..) = only_request(&cdn);
- assert_eq!("/attachments/1/2/a.png", uri.path());
-}
-
-#[test]
-fn read_endpoint_virtual_style_uses_bucket_subdomain() {
- let tmp = tempfile::tempdir().unwrap();
- let store = url_store(
- tmp.path(),
- "https://cdn.example.net",
- BucketStyle::VirtualHosted,
- );
-
- assert_eq!(
- "https://cdn.cdn.example.net/attachments/1/2/a.png",
- store.s3_read_url("cdn", "attachments/1/2/a.png").unwrap()
- );
-}
-
-#[tokio::test]
-async fn read_endpoint_never_redirects_other_buckets() {
- let origin = fake_s3().await;
- let cdn = fake_s3().await;
- origin.put_object("uploads/fresh-upload-key", payload_object());
- let tmp = tempfile::tempdir().unwrap();
- let mut cfg = fronted_config(&origin, &cdn, tmp.path());
- cfg.storage.s3_read_bucket_style = BucketStyle::Rooted;
- let store = store(cfg);
-
- store
- .read_object("uploads", "fresh-upload-key")
- .await
- .unwrap();
-
- assert!(cdn.requests().is_empty(), "uploads must not hit the CDN");
- let (_, uri, headers, _) = only_request(&origin);
- assert_eq!("/uploads/fresh-upload-key", uri.path());
- assert_signed(&headers);
-}
-
-#[tokio::test]
-async fn read_endpoint_never_affects_writes() {
- let origin = fake_s3().await;
- let cdn = fake_s3().await;
- let tmp = tempfile::tempdir().unwrap();
- let mut cfg = fronted_config(&origin, &cdn, tmp.path());
- cfg.storage.s3_read_bucket = "uploads".to_owned();
- let store = store(cfg);
-
- let body: RelayBodyChunks = Box::pin(futures_util::stream::iter(vec![Ok(Bytes::from_static(
- b"body",
- ))]));
- store
- .relay_put_object(
- "uploads",
- "guild/x.bin",
- RelayPutOptions {
- body: RelayBody::Streamed(body),
- content_length: 4,
- content_type: Some("application/octet-stream".to_owned()),
- upload_id: None,
- part_number: None,
- timeout_ms: 5_000,
- },
- )
- .await
- .unwrap();
-
- assert!(cdn.requests().is_empty(), "writes must not hit the CDN");
- let (method, uri, headers, _) = only_request(&origin);
- assert_eq!(Method::PUT, method);
- assert_eq!("/uploads/guild/x.bin", uri.path());
- assert_signed(&headers);
-}
-
-#[tokio::test]
-async fn read_endpoint_signs_when_read_signed_enabled() {
- let origin = fake_s3().await;
- let cdn = fake_s3().await;
- cdn.put_object("cdn/a.png", payload_object());
- let cdn_host = cdn.endpoint().trim_start_matches("http://").to_owned();
- let tmp = tempfile::tempdir().unwrap();
- let mut cfg = fronted_config(&origin, &cdn, tmp.path());
- cfg.storage.s3_read_signed = true;
- let store = store(cfg);
-
- store.read_object("cdn", "a.png").await.unwrap();
-
- let (_, _, headers, _) = only_request(&cdn);
- assert_signed(&headers);
- assert_eq!(
- cdn_host,
- headers.get(header::HOST).unwrap().to_str().unwrap()
- );
-}
-
-#[tokio::test]
-async fn unsigned_read_still_sends_range_header() {
- let origin = fake_s3().await;
- let cdn = fake_s3().await;
- cdn.put_object("cdn/video.mp4", payload_object());
- let tmp = tempfile::tempdir().unwrap();
- let store = store(fronted_config(&origin, &cdn, tmp.path()));
-
- store
- .stream_object("cdn", "video.mp4", Some("bytes=10-19"))
- .await
- .unwrap();
-
- let (_, _, headers, _) = only_request(&cdn);
- assert_eq!("bytes=10-19", headers.get(header::RANGE).unwrap());
- assert_unsigned(&headers);
-}
-
-#[tokio::test]
-async fn head_object_uses_the_read_endpoint_like_every_other_read() {
- let origin = fake_s3().await;
- let cdn = fake_s3().await;
- cdn.put_object("a.png", payload_object());
- let tmp = tempfile::tempdir().unwrap();
- let mut cfg = fronted_config(&origin, &cdn, tmp.path());
- cfg.storage.s3_read_bucket_style = BucketStyle::Rooted;
- let store = store(cfg);
-
- store.head_object("cdn", "a.png").await.unwrap();
-
- // A HEAD is a read: routing it to the write origin would bypass the configured read
- // endpoint and attach credentials to a read the operator configured as unsigned.
- assert!(
- origin.requests().is_empty(),
- "HEAD must not hit the write origin"
- );
- let (method, uri, headers, _) = only_request(&cdn);
- assert_eq!(Method::HEAD, method);
- assert_eq!("/a.png", uri.path());
- assert_unsigned(&headers);
-}
-
-#[tokio::test]
-async fn head_and_body_reads_both_use_the_read_endpoint() {
- let origin = fake_s3().await;
- let cdn = fake_s3().await;
- cdn.put_object("video.mp4", payload_object());
- let tmp = tempfile::tempdir().unwrap();
- let mut cfg = fronted_config(&origin, &cdn, tmp.path());
- cfg.storage.s3_read_bucket_style = BucketStyle::Rooted;
- let store = store(cfg);
-
- store.head_object("cdn", "video.mp4").await.unwrap();
- store
- .stream_object("cdn", "video.mp4", Some("bytes=0-3"))
- .await
- .unwrap();
-
- assert!(
- origin.requests().is_empty(),
- "neither read may reach the write origin"
- );
- let cdn_requests = cdn.requests();
- assert_eq!(2, cdn_requests.len());
- assert_eq!(Method::HEAD, cdn_requests[0].0);
- assert_eq!(Method::GET, cdn_requests[1].0);
- assert_eq!("bytes=0-3", cdn_requests[1].2.get(header::RANGE).unwrap());
-}
-
-#[tokio::test]
-async fn unsigned_read_works_without_credentials() {
- let origin = fake_s3().await;
- let cdn = fake_s3().await;
- cdn.put_object("cdn/a.png", payload_object());
- let tmp = tempfile::tempdir().unwrap();
- let mut cfg = fronted_config(&origin, &cdn, tmp.path());
- cfg.storage.s3_access_key_id = String::new();
- cfg.storage.s3_secret_access_key = String::new();
- let store = store(cfg);
-
- store.read_object("cdn", "a.png").await.unwrap();
-
- let (_, _, headers, _) = only_request(&cdn);
- assert_unsigned(&headers);
-}
-
-#[test]
-fn read_url_matches_write_url_shape_for_encoding_and_trailing_slash() {
- let tmp = tempfile::tempdir().unwrap();
- let mut cfg = super::test_config(tmp.path());
- cfg.storage.backend = crate::config::StorageBackend::S3;
- cfg.storage.s3_endpoint = "https://s3.example.test/".to_owned();
- cfg.storage.s3_read_endpoint = Some("https://cdn.example.net/".to_owned());
- let store = store(cfg);
- let key = "attachments/1/2/na me+ü.png";
-
- assert_eq!(
- "https://s3.example.test/uploads/attachments/1/2/na%20me%2B%C3%BC.png",
- store.s3_url("uploads", key).unwrap()
- );
- assert_eq!(
- "https://cdn.example.net/cdn/attachments/1/2/na%20me%2B%C3%BC.png",
- store.s3_read_url("cdn", key).unwrap()
- );
-}
-
-#[test]
-fn read_url_rejects_unsafe_keys_and_buckets() {
- let tmp = tempfile::tempdir().unwrap();
- let store = url_store(tmp.path(), "https://cdn.example.net", BucketStyle::Rooted);
-
- assert!(store.s3_read_url("cdn", "../escape").is_err());
- assert!(store.s3_read_url("cdn", "/leading").is_err());
-}
-
-#[test]
-fn read_url_validates_bucket_inside_the_read_endpoint_branch() {
- let tmp = tempfile::tempdir().unwrap();
- let mut cfg = super::test_config(tmp.path());
- cfg.storage.backend = crate::config::StorageBackend::S3;
- cfg.storage.s3_endpoint = "https://s3.example.test".to_owned();
- cfg.storage.s3_read_endpoint = Some("https://cdn.example.net".to_owned());
- cfg.storage.s3_read_bucket = "..".to_owned();
- cfg.storage.s3_read_bucket_style = BucketStyle::Rooted;
- let store = store(cfg);
-
- assert!(store.read_endpoint_for("..").is_some());
- assert!(store.s3_read_url("..", "a.png").is_err());
-}
-
-#[tokio::test]
-async fn unsigned_cdn_read_treats_403_as_not_found() {
- let origin = fake_s3().await;
- let cdn = fake_s3().await;
- cdn.put_object("avatars/1/hash", denied_object(403));
- let tmp = tempfile::tempdir().unwrap();
- let mut cfg = fronted_config(&origin, &cdn, tmp.path());
- cfg.storage.s3_read_bucket_style = BucketStyle::Rooted;
- let store = store(cfg);
-
- let err = store
- .read_object("cdn", "avatars/1/hash")
- .await
- .unwrap_err();
- assert!(matches!(err, StorageError::NotFound), "got {err:?}");
- assert_eq!(1, cdn.requests().len());
-
- let stream = store.stream_object("cdn", "avatars/1/hash", None).await;
- assert!(
- matches!(stream, Err(StorageError::NotFound)),
- "stream_object should map 403 to NotFound too"
- );
-}
-
-#[tokio::test]
-async fn signed_cdn_read_keeps_403_as_an_error() {
- let origin = fake_s3().await;
- let cdn = fake_s3().await;
- cdn.put_object("cdn/avatars/1/hash", denied_object(403));
- let tmp = tempfile::tempdir().unwrap();
- let mut cfg = fronted_config(&origin, &cdn, tmp.path());
- cfg.storage.s3_read_signed = true;
- let store = store(cfg);
-
- let err = store
- .read_object("cdn", "avatars/1/hash")
- .await
- .unwrap_err();
- assert!(!matches!(err, StorageError::NotFound), "got {err:?}");
-}
-
-#[tokio::test]
-async fn origin_read_keeps_403_as_an_error() {
- let origin = fake_s3().await;
- origin.put_object("cdn/avatars/1/hash", denied_object(403));
- let tmp = tempfile::tempdir().unwrap();
- let store = store(origin.config(tmp.path()));
-
- let err = store
- .read_object("cdn", "avatars/1/hash")
- .await
- .unwrap_err();
- assert!(!matches!(err, StorageError::NotFound), "got {err:?}");
-}
-
-#[tokio::test]
-async fn unfronted_bucket_keeps_403_as_an_error() {
- let origin = fake_s3().await;
- let cdn = fake_s3().await;
- origin.put_object("uploads/fresh", denied_object(403));
- let tmp = tempfile::tempdir().unwrap();
- let store = store(fronted_config(&origin, &cdn, tmp.path()));
-
- let err = store.read_object("uploads", "fresh").await.unwrap_err();
- assert!(!matches!(err, StorageError::NotFound), "got {err:?}");
- assert!(cdn.requests().is_empty());
-}
diff --git a/fluxer_media_proxy/src/timed_semaphore.rs b/fluxer_media_proxy/src/timed_semaphore.rs
index 8436e08fb..cd2523932 100644
--- a/fluxer_media_proxy/src/timed_semaphore.rs
+++ b/fluxer_media_proxy/src/timed_semaphore.rs
@@ -83,72 +83,3 @@ impl TimedSemaphore {
}
}
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use std::time::Duration;
-
- #[tokio::test]
- async fn queue_capacity_rejects_the_waiter_past_the_bound() {
- let sem = TimedSemaphore::with_queue_capacity(1, 2);
- let held_admission = sem.try_admit().unwrap();
- let _held = sem
- .wait_until_admitted(&held_admission, None)
- .await
- .unwrap();
- let first = sem.try_admit().unwrap();
- let _second = sem.try_admit().unwrap();
- assert_eq!(TimedSemaphoreError::QueueFull, sem.try_admit().unwrap_err());
- drop(first);
- let _third = sem.try_admit().unwrap();
- }
-
- #[tokio::test]
- async fn dropping_a_waiting_future_restores_the_permit_and_the_queue_slot() {
- let sem = TimedSemaphore::with_queue_capacity(1, 1);
- let held_admission = sem.try_admit().unwrap();
- let held = sem
- .wait_until_admitted(&held_admission, None)
- .await
- .unwrap();
- let mut waiting = Box::pin(async {
- let admission = sem.try_admit().unwrap();
- sem.wait_until_admitted(&admission, None).await
- });
- assert!(
- tokio::time::timeout(Duration::from_millis(20), &mut waiting)
- .await
- .is_err()
- );
- assert_eq!(TimedSemaphoreError::QueueFull, sem.try_admit().unwrap_err());
- drop(waiting);
- drop(held);
- drop(held_admission);
- let reacquired_admission = sem.try_admit().unwrap();
- let _reacquired = sem
- .wait_until_admitted(&reacquired_admission, None)
- .await
- .unwrap();
- let _queued = sem.try_admit().unwrap();
- assert_eq!(TimedSemaphoreError::QueueFull, sem.try_admit().unwrap_err());
- }
-
- #[tokio::test]
- async fn wait_until_admitted_reuses_the_admission_the_caller_holds() {
- let sem = TimedSemaphore::with_queue_capacity(1, 1);
- let admission = sem.try_admit().unwrap();
- let permit = sem
- .wait_until_admitted(
- &admission,
- Some(Instant::now() + Duration::from_millis(100)),
- )
- .await
- .unwrap();
- let _queued = sem.try_admit().unwrap();
- assert_eq!(TimedSemaphoreError::QueueFull, sem.try_admit().unwrap_err());
- drop(permit);
- drop(admission);
- let _readmitted = sem.try_admit().unwrap();
- }
-}
diff --git a/fluxer_media_proxy/src/transform_cache.rs b/fluxer_media_proxy/src/transform_cache.rs
index 6e4d776ca..f32d0ad87 100644
--- a/fluxer_media_proxy/src/transform_cache.rs
+++ b/fluxer_media_proxy/src/transform_cache.rs
@@ -104,137 +104,4 @@ impl TransformCache {
})
.await
}
-
- #[cfg(test)]
- fn settle(&self) {
- self.cached.settle();
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use crate::metrics::Metrics;
- use std::sync::atomic::{AtomicU32, Ordering};
-
- fn counter(rendered: &str, name: &str) -> u64 {
- rendered
- .lines()
- .find_map(|line| line.strip_prefix(name)?.trim().parse().ok())
- .expect("counter series is rendered")
- }
-
- fn transform_cache(
- metrics: &Metrics,
- capacity_bytes: usize,
- max_entry_bytes: usize,
- ) -> TransformCache {
- TransformCache::new(TransformCacheSettings {
- cache: ByteCacheSettings::clamped(capacity_bytes, max_entry_bytes, 60_000),
- budget: ByteBudget::new(1 << 20),
- max_in_flight: 8,
- max_waiters: 8,
- cache_metrics: metrics.transform_cache(),
- coalescer_metrics: metrics.coalescer(),
- })
- }
-
- #[tokio::test]
- async fn the_second_request_for_a_key_is_served_from_the_cache() {
- let metrics = Metrics::new();
- let cache = transform_cache(&metrics, 65_536, 65_536);
- let runs = AtomicU32::new(0);
- for _ in 0..2 {
- let bytes = cache
- .get_or_run(
- "stored:abc|fmt=webp".to_owned(),
- OutputFormat::WebP,
- None,
- || async {
- runs.fetch_add(1, Ordering::SeqCst);
- Ok(MediaBytes::from(vec![1, 2, 3, 4]))
- },
- )
- .await
- .expect("transform result");
- assert_eq!(&[1, 2, 3, 4], bytes.as_ref());
- }
- assert_eq!(1, runs.load(Ordering::SeqCst));
- let rendered = metrics.render();
- assert!(rendered.contains("fluxer_media_proxy_transform_cache_hits_total 1\n"));
- assert!(rendered.contains("fluxer_media_proxy_transform_cache_misses_total 1\n"));
- }
-
- #[tokio::test]
- async fn an_output_over_the_entry_ceiling_is_returned_but_not_cached() {
- let metrics = Metrics::new();
- let cache = transform_cache(&metrics, 65_536, 1_024);
- let bytes = cache
- .get_or_run(
- "stored:big|fmt=png".to_owned(),
- OutputFormat::PNG,
- None,
- || async { Ok(MediaBytes::from(vec![0u8; 4_096])) },
- )
- .await
- .expect("transform result");
- assert_eq!(4_096, bytes.len());
- assert!(cache.get("stored:big|fmt=png").is_none());
- assert!(
- metrics
- .render()
- .contains("fluxer_media_proxy_transform_cache_insert_rejected_total 1\n")
- );
- }
-
- #[tokio::test]
- async fn entries_evicted_by_the_capacity_bound_are_counted() {
- let metrics = Metrics::new();
- let cache = transform_cache(&metrics, 8_192, 8_192);
- for index in 0..64 {
- cache
- .get_or_run(
- format!("stored:{index}|fmt=gif"),
- OutputFormat::GIF,
- None,
- || async { Ok(MediaBytes::from(vec![0u8; 512])) },
- )
- .await
- .expect("transform result");
- }
- cache.settle();
- let rendered = metrics.render();
- assert!(
- counter(
- &rendered,
- "fluxer_media_proxy_transform_cache_evictions_total"
- ) > 0
- );
- assert_eq!(
- 0,
- counter(
- &rendered,
- "fluxer_media_proxy_transform_cache_insert_rejected_total"
- )
- );
- }
-
- #[tokio::test]
- async fn a_disabled_cache_still_runs_and_returns_the_transform() {
- let metrics = Metrics::new();
- let cache = transform_cache(&metrics, 0, 0);
- let bytes = cache
- .get_or_run(
- "stored:off|fmt=jpeg".to_owned(),
- OutputFormat::JPEG,
- None,
- || async { Ok(MediaBytes::from(vec![5, 6])) },
- )
- .await
- .expect("transform result");
- assert_eq!(&[5, 6], bytes.as_ref());
- assert!(cache.get("stored:off|fmt=jpeg").is_none());
- cache.begin_shutdown();
- cache.wait_for_shutdown().await;
- }
}
diff --git a/fluxer_media_proxy/src/upload_relay/tests.rs b/fluxer_media_proxy/src/upload_relay/tests.rs
index d0bf6f0b6..79d3da689 100644
--- a/fluxer_media_proxy/src/upload_relay/tests.rs
+++ b/fluxer_media_proxy/src/upload_relay/tests.rs
@@ -2,12 +2,10 @@
use crate::upload_relay::{
RelayError,
- target::{RelayRequest, query_part_number, valid_content_type, validate_relay_request},
- token::{TokenError, TokenMethod, TokenPayload, decode_token, encode_token, token_from_query},
- try_reserve,
+ target::{RelayRequest, valid_content_type, validate_relay_request},
+ token::{TokenError, TokenMethod, TokenPayload, decode_token, encode_token},
};
use http::Method;
-use std::sync::atomic::{AtomicU64, Ordering};
const BASE64_URL_ALPHABET: &[u8] =
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
@@ -59,23 +57,6 @@ fn expired_token_rejected() {
);
}
-#[test]
-fn validates_matching_relay_request() {
- validate_relay_request(
- &token(),
- RelayRequest {
- uploads_bucket: "uploads",
- request_key: "guild/file.bin",
- request_method: &Method::PUT,
- query_upload_id: Some("upload-id"),
- query_part_number: Some(7),
- content_length: Some(99),
- max_body_bytes: 100,
- },
- )
- .unwrap();
-}
-
#[test]
fn validates_relay_mismatches() {
assert_eq!(
@@ -123,16 +104,6 @@ fn validates_relay_mismatches() {
.unwrap();
}
-#[test]
-fn buffer_budget_is_bounded() {
- let in_flight = AtomicU64::new(0);
- assert!(try_reserve(&in_flight, 4, 8));
- assert!(!try_reserve(&in_flight, 5, 8));
- in_flight.fetch_sub(4, Ordering::AcqRel);
- assert!(try_reserve(&in_flight, 8, 8));
- assert!(!try_reserve(&AtomicU64::new(0), 1, 0));
-}
-
#[test]
fn token_decoder_rejects_noncanonical_wrong_secret_and_oversized_tokens() {
let secret = [3u8; 32];
@@ -155,17 +126,6 @@ fn token_decoder_rejects_noncanonical_wrong_secret_and_oversized_tokens() {
);
}
-#[test]
-fn token_query_requires_a_present_and_bounded_token() {
- assert_eq!(Err(RelayError::MissingToken), token_from_query(None));
- assert_eq!(Err(RelayError::InvalidToken), token_from_query(Some("")));
- assert_eq!(
- Err(RelayError::InvalidToken),
- token_from_query(Some(&"x".repeat(16 * 1024 + 1)))
- );
- assert_eq!(Ok("value"), token_from_query(Some("value")));
-}
-
#[test]
fn relay_request_requires_the_exact_signed_key() {
for request_key in [
@@ -192,14 +152,6 @@ fn relay_request_requires_the_exact_signed_key() {
}
}
-#[test]
-fn part_number_query_is_optional_but_never_empty() {
- assert_eq!(Ok(None), query_part_number(None));
- assert_eq!(Ok(Some(3)), query_part_number(Some("3")));
- assert_eq!(Err(RelayError::BadQuery), query_part_number(Some("")));
- assert_eq!(Err(RelayError::BadQuery), query_part_number(Some("two")));
-}
-
#[test]
fn content_type_gate_rejects_header_injection_and_malformed_media_types() {
for accepted in [