From 933cd8a0689d92a8a45e70abaf40e7685662129e Mon Sep 17 00:00:00 2001 From: Shady Khalifa Date: Mon, 24 Apr 2023 22:33:46 +0200 Subject: [PATCH] add failing test --- src/service/media/mod.rs | 84 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/src/service/media/mod.rs b/src/service/media/mod.rs index 93937533..f5f499e0 100644 --- a/src/service/media/mod.rs +++ b/src/service/media/mod.rs @@ -224,3 +224,87 @@ impl Service { } } } + +#[cfg(test)] +mod tests { + use std::path::PathBuf; + + use super::*; + + struct MockedKVDatabase; + + impl Data for MockedKVDatabase { + fn create_file_metadata( + &self, + mxc: String, + width: u32, + height: u32, + content_disposition: Option<&str>, + content_type: Option<&str>, + ) -> Result> { + // copied from src/database/key_value/media.rs + let mut key = mxc.as_bytes().to_vec(); + key.push(0xff); + key.extend_from_slice(&width.to_be_bytes()); + key.extend_from_slice(&height.to_be_bytes()); + key.push(0xff); + key.extend_from_slice( + content_disposition + .as_ref() + .map(|f| f.as_bytes()) + .unwrap_or_default(), + ); + key.push(0xff); + key.extend_from_slice( + content_type + .as_ref() + .map(|c| c.as_bytes()) + .unwrap_or_default(), + ); + + Ok(key) + } + + fn search_file_metadata( + &self, + mxc: String, + width: u32, + height: u32, + ) -> Result<(Option, Option, Vec)> { + todo!() + } + } + + #[tokio::test] + async fn long_file_names_works() { + static DB: MockedKVDatabase = MockedKVDatabase; + let media = Service { db: &DB }; + + let mxc = "mxc://example.com/ascERGshawAWawugaAcauga".to_owned(); + let width = 100; + let height = 100; + let content_disposition = "attachment; filename=\"this is a very long file name with spaces and special characters like äöüß and even emoji like 🦀.png\""; + let content_type = "image/png"; + let key = media + .db + .create_file_metadata( + mxc, + width, + height, + Some(content_disposition), + Some(content_type), + ) + .unwrap(); + let mut r = PathBuf::new(); + r.push("/tmp"); + r.push("media"); + r.push(base64::encode_config(key, base64::URL_SAFE_NO_PAD)); + // Check that the file path is not longer than 255 characters + // (255 is the maximum length of a file path on most file systems) + assert!( + r.to_str().unwrap().len() <= 255, + "File path is too long: {}", + r.to_str().unwrap().len() + ); + } +}