mod common;
use common::TempDir;
use fmm_core::{cache::Cache, description_images::*};
use image::{Delay, DynamicImage, Frame, ImageFormat, Rgba, RgbaImage, codecs::gif::GifEncoder};
use std::{
collections::HashMap,
io::{Cursor, Read},
sync::Mutex,
time::Duration,
};
fn picture(format: ImageFormat) -> Vec<u8> {
let mut bytes = Cursor::new(Vec::new());
DynamicImage::ImageRgba8(RgbaImage::from_pixel(4, 3, Rgba([20, 60, 90, 255])))
.write_to(&mut bytes, format)
.unwrap();
bytes.into_inner()
}
#[test]
fn external_url_policy_does_not_change_portal_allowlist() {
for url in [
"https://bin.smwcentral.net/u/1055/AriaIdle.gif&key=h5giCYb2",
"https://i.imgur.com/x.png",
"https://raw.githubusercontent.com/a/b/c.gif",
"https://example.com:443/a",
"https://EXAMPLE.com/a",
] {
assert!(validate_url(url).is_ok(), "{url}");
assert!(fmm_core::net::validate_url(url).is_err());
}
for url in [
"http://example.com/x",
"data:image/png,abc",
"javascript:alert(1)",
"file:///x",
"https://user@host/",
"https://127.0.0.1/",
"https://[::1]/",
"https://0x7f.1/",
"https://2130706433/",
"https://0177.0.0.1/",
"https://localhost/",
"https://a.localhost/",
"https://printer.local/",
"https://printer.local./",
"https://a.internal/",
"https://intranet/",
"https://host:8443/",
"https://example.com:8443/",
"https://example.com/?token=x",
"https://example.com/?%74oken=x",
"https://example.com/?PASSWORD=x",
"https://example.com/a\\b",
"https://example.com/a b",
"https://example.com/\nx",
"https://",
"https:///a",
] {
assert!(validate_url(url).is_err(), "{url}");
}
}
struct Fake {
redirects: HashMap<String, String>,
bytes: Vec<u8>,
calls: Mutex<Vec<String>>,
}
impl Fake {
fn new(bytes: Vec<u8>) -> Self {
Self {
redirects: HashMap::new(),
bytes,
calls: Mutex::default(),
}
}
}
impl Transport for Fake {
fn get(&self, url: &str, remaining: Duration) -> fmm_core::Result<Response> {
assert!(remaining <= FETCH_TIMEOUT);
self.calls.lock().unwrap().push(url.into());
Ok(Response {
status: if self.redirects.contains_key(url) {
302
} else {
200
},
length: Some(self.bytes.len() as u64),
location: self.redirects.get(url).cloned(),
reader: Box::new(Cursor::new(self.bytes.clone())),
})
}
}
#[test]
fn redirects_revalidate_every_hop_and_resolve_relative_locations() {
for hops in [3, 4] {
let temp = TempDir::new();
let cache = Cache::new(temp.0.clone());
let mut fake = Fake::new(picture(ImageFormat::Png));
for n in 0..hops {
fake.redirects
.insert(format!("https://example.com/{n}"), format!("./{}", n + 1));
}
assert_eq!(
fetch(&fake, &cache, "https://example.com/0").is_ok(),
hops == 3
);
assert_eq!(fake.calls.lock().unwrap().len(), 4);
}
for location in [
"http://example.com/a",
"https://127.0.0.1/a",
"//[::1]/a",
"data:bad",
"https://example.com/?token=a",
] {
let temp = TempDir::new();
let cache = Cache::new(temp.0.clone());
let mut fake = Fake::new(picture(ImageFormat::Png));
fake.redirects
.insert("https://example.com/a".into(), location.into());
assert!(fetch(&fake, &cache, "https://example.com/a").is_err());
assert_eq!(fake.calls.lock().unwrap().len(), 1);
assert!(!cache.path("description-images").unwrap().exists());
}
}
#[test]
fn sniff_decode_and_cache_four_formats_and_clear_only_disposable_entries() {
let temp = TempDir::new();
let cache = Cache::new(temp.0.join("cache"));
for (format, ext) in [
(ImageFormat::Png, "png"),
(ImageFormat::Jpeg, "jpg"),
(ImageFormat::Gif, "gif"),
(ImageFormat::WebP, "webp"),
] {
let fake = Fake::new(picture(format));
let url = format!("https://example.com/{ext}");
let path = fetch(&fake, &cache, &url).unwrap();
assert_eq!(path.extension().unwrap(), ext);
assert_eq!(fetch(&fake, &cache, &url).unwrap(), path);
assert_eq!(fake.calls.lock().unwrap().len(), 1);
assert_eq!(
decode(&std::fs::read(path).unwrap()).unwrap().frames[0].dimensions(),
(4, 3)
);
}
let fake = Fake::new(picture(ImageFormat::Png));
assert!(
fetch(&fake, &cache, "https://assets-mod.factorio.com/a.png")
.unwrap()
.starts_with(cache.path("portal-thumbs").unwrap())
);
cache.write("settings.json", b"keep").unwrap();
cache.write("metadata/a.json", b"keep").unwrap();
cache.clear().unwrap();
assert!(!cache.path("description-images").unwrap().exists());
assert!(!cache.path("portal-thumbs").unwrap().exists());
assert_eq!(cache.read("settings.json").unwrap(), b"keep");
assert_eq!(cache.read("metadata/a.json").unwrap(), b"keep");
let fake = Fake::new(b"<html>pretending to be image/png</html>".to_vec());
assert!(fetch(&fake, &cache, "https://example.com/html.png").is_err());
assert!(!cache.path("description-images").unwrap().exists());
}
#[test]
fn bodies_over_eight_mib_load_with_and_without_content_length() {
let mut seed = 0x8b37c56eu32;
let image = RgbaImage::from_fn(1600, 1600, |_, _| {
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
Rgba(seed.to_le_bytes())
});
let mut encoded = Cursor::new(Vec::new());
DynamicImage::ImageRgba8(image)
.write_to(&mut encoded, ImageFormat::Png)
.unwrap();
let bytes = encoded.into_inner();
assert!(bytes.len() > 8 * 1024 * 1024);
for length in [true, false] {
struct Large(Fake, bool);
impl Transport for Large {
fn get(&self, url: &str, remaining: Duration) -> fmm_core::Result<Response> {
let mut r = self.0.get(url, remaining)?;
if !self.1 {
r.length = None;
}
Ok(r)
}
}
let temp = TempDir::new();
let cache = Cache::new(temp.0.clone());
let path = fetch(
&Large(Fake::new(bytes.clone()), length),
&cache,
"https://example.com/large.png",
)
.unwrap();
assert!(std::fs::metadata(path).unwrap().len() > 8 * 1024 * 1024);
}
}
#[test]
fn total_deadline_includes_redirects_and_reads_and_never_caches_failure() {
use std::sync::{
Arc,
atomic::{AtomicU64, Ordering},
};
struct Slow {
clock: Arc<AtomicU64>,
}
struct Reader {
clock: Arc<AtomicU64>,
bytes: Cursor<Vec<u8>>,
}
impl Read for Reader {
fn read(&mut self, out: &mut [u8]) -> std::io::Result<usize> {
self.clock.fetch_add(16, Ordering::Relaxed);
self.bytes.read(out)
}
}
impl Transport for Slow {
fn get(&self, _: &str, _: Duration) -> fmm_core::Result<Response> {
Ok(Response {
status: 200,
length: None,
location: None,
reader: Box::new(Reader {
clock: self.clock.clone(),
bytes: Cursor::new(picture(ImageFormat::Png)),
}),
})
}
}
let temp = TempDir::new();
let cache = Cache::new(temp.0.clone());
let clock = Arc::new(AtomicU64::new(0));
assert!(
fetch_with_clock(
&Slow {
clock: clock.clone()
},
&cache,
"https://example.com/slow",
|| Duration::from_secs(clock.load(Ordering::Relaxed))
)
.is_err()
);
assert!(!cache.path("description-images").unwrap().exists());
let fake = Fake::new(picture(ImageFormat::Png));
let time = AtomicU64::new(0);
assert!(
fetch_with_clock(&fake, &cache, "https://example.com/late", || {
Duration::from_secs(time.fetch_add(5, Ordering::Relaxed))
})
.is_err()
);
assert!(!cache.path("description-images").unwrap().exists());
}
#[test]
fn generated_gif_is_composited_with_browser_delays_and_dimension_limits() {
let mut bytes = Vec::new();
{
let mut encoder = GifEncoder::new(&mut bytes);
for delay in [0, 10, 40] {
encoder
.encode_frame(Frame::from_parts(
RgbaImage::from_pixel(2, 2, Rgba([delay as u8, 40, 90, 255])),
0,
0,
Delay::from_numer_denom_ms(delay, 1),
))
.unwrap();
}
}
let decoded = decode(&bytes).unwrap();
assert_eq!(decoded.frames.len(), 3);
assert_eq!(decoded.delays, [100, 100, 40]);
assert_eq!(decoded.frames[2].get_pixel(0, 0).0, [40, 40, 90, 255]);
let mut huge = Cursor::new(Vec::new());
DynamicImage::new_rgba8(4097, 1)
.write_to(&mut huge, ImageFormat::Png)
.unwrap();
assert!(decode(huge.get_ref()).is_err());
let mut huge = Cursor::new(Vec::new());
DynamicImage::new_rgba8(4001, 4000)
.write_to(&mut huge, ImageFormat::Png)
.unwrap();
assert!(decode(huge.get_ref()).is_err());
}
#[test]
fn inline_flow_wraps_four_two_one_and_never_enlarges() {
let sizes = [(48.0, 60.0); 4];
for (width, ys) in [
(204.0, vec![0.0, 0.0, 0.0, 0.0]),
(100.0, vec![0.0, 0.0, 64.0, 64.0]),
(48.0, vec![0.0, 64.0, 128.0, 192.0]),
] {
let (p, _) = layout(&sizes, width);
assert_eq!(p.iter().map(|p| p.y).collect::<Vec<_>>(), ys);
assert_eq!(p[0].width, 48.0);
assert_eq!(p[1].x, if width >= 100.0 { 52.0 } else { 0.0 });
}
let (p, h) = layout(&[(2000.0, 1000.0), (12.0, 8.0)], 500.0);
assert_eq!(
(p[0].width, p[0].height, p[1].width, p[1].height, h),
(500.0, 250.0, 12.0, 8.0, 262.0)
);
}