//! The rules of the sheet-asset upload (R5.5): manifest, destination, bytes, plan. The tool //! around them is exercised by `upload_tool.rs` against a stub of the local stack. #![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)] use faunosteo_core::curation::ids::sha256_hex; use faunosteo_core::curation::upload::{ AssetEntry, ObjectState, Plan, RowState, UploadKind, check_destination, parse_manifest, plan_entry, verify_asset, }; const SEED: &str = "../../supabase/seed/anatomical-sheets/equine/draft"; const UUID: &str = "7f767f74-3b36-51a7-8465-52dd7e2ead4a"; const SVG: &[u8] = b""; fn entry_for(bytes: &[u8]) -> AssetEntry { AssetEntry { file: "a.svg".into(), sha256: sha256_hex(bytes), byte_size: bytes.len(), svg_asset_id: UUID.into(), } } fn manifest(entries: &[(&str, &str, usize, &str)]) -> String { let items: Vec = entries .iter() .map(|(file, sha, size, id)| { format!("{{\"file\":\"{file}\",\"sha256\":\"{sha}\",\"byte_size\":{size},\"svg_asset_id\":\"{id}\"}}") }) .collect(); format!("[{}]", items.join(",")) } fn kind( result: Result, ) -> UploadKind { result.unwrap_err().kind } #[test] fn the_committed_manifest_and_drawing_pass() { let text = std::fs::read_to_string(format!("{SEED}/assets.json")).unwrap(); let entries = parse_manifest(&text).unwrap(); assert_eq!(entries.len(), 1); let bytes = std::fs::read(format!("{SEED}/assets/{}", entries[0].file)).unwrap(); verify_asset(&entries[0], &bytes).unwrap(); assert_eq!( entries[0].object_key(), format!("{}.svg", entries[0].sha256) ); } #[test] fn a_bad_manifest_is_refused_whole() { let sha = "a".repeat(64); let good = (&"a.svg", sha.as_str(), 5usize, UUID); let cases = [ manifest(&[]), "{}".into(), "[{\"file\":\"a.svg\"}]".into(), manifest(&[("../a.svg", &sha, 5, UUID)]), manifest(&[(".a.svg", &sha, 5, UUID)]), manifest(&[("a.png", &sha, 5, UUID)]), manifest(&[("a.svg", &sha.to_uppercase(), 5, UUID)]), manifest(&[("a.svg", "abc", 5, UUID)]), manifest(&[("a.svg", &sha, 5, "not-a-uuid")]), manifest(&[("a.svg", &sha, 0, UUID)]), manifest(&[("a.svg", &sha, 17 * 1024 * 1024, UUID)]), manifest(&[ (good.0, good.1, good.2, good.3), ( "a.svg", &"b".repeat(64), 5, "11111111-1111-1111-1111-111111111111", ), ]), manifest(&[ (good.0, good.1, good.2, good.3), ("b.svg", &sha, 5, "11111111-1111-1111-1111-111111111111"), ]), manifest(&[ (good.0, good.1, good.2, good.3), ("b.svg", &"b".repeat(64), 5, UUID), ]), "x".repeat(2 * 1024 * 1024), ]; for text in cases { assert_eq!( kind(parse_manifest(&text)), UploadKind::Manifest, "{}", &text[..text.len().min(80)] ); } } #[test] fn a_manifest_with_an_unknown_key_is_refused() { let text = manifest(&[("a.svg", &"a".repeat(64), 5, UUID)]).replace("}", ",\"x\":1}"); assert_eq!(kind(parse_manifest(&text)), UploadKind::Manifest); } #[test] fn only_bytes_that_match_and_that_the_svg_reader_keeps_whole_are_uploaded() { let entry = entry_for(SVG); verify_asset(&entry, SVG).unwrap(); let mut other = SVG.to_vec(); other[5] = b'X'; assert_eq!(kind(verify_asset(&entry, &other)), UploadKind::Content); let scripted: &[u8] = b""; assert_eq!( kind(verify_asset(&entry_for(scripted), scripted)), UploadKind::Content ); let not_svg: &[u8] = b""; assert_eq!( kind(verify_asset(&entry_for(not_svg), not_svg)), UploadKind::Content ); let binary: &[u8] = &[0xff, 0xfe, 0x00]; assert_eq!( kind(verify_asset(&entry_for(binary), binary)), UploadKind::Content ); } #[test] fn only_a_local_stack_is_a_destination() { for ok in [ "http://127.0.0.1:54321", "http://localhost:54321", "http://[::1]:54321", "http://127.0.0.2:9", ] { let destination = check_destination(ok, None).unwrap(); assert!(destination.origin().starts_with("http://"), "{ok}"); assert!(!destination.origin().ends_with('/')); } assert_eq!( check_destination("http://LOCALHOST:1", None) .unwrap() .origin(), "http://localhost:1" ); for refused in [ "https://127.0.0.1:54321", "http://127.0.0.1", "http://127.0.0.1:80", "http://user@127.0.0.1:54321", "http://127.0.0.1:54321/x", "http://127.0.0.1:54321/?a=1", "http://127.0.0.1:54321/#f", "http://10.1.2.3:54321", "http://example.test:54321", "http://abcd.supabase.co:54321", "http://faunosteo.com:54321", "http://0.0.0.0:54321", "http://[2001:db8::1]:54321", "127.0.0.1:54321", "", ] { assert_eq!( kind(check_destination(refused, None)), UploadKind::Destination, "{refused}" ); } } #[test] fn the_dev_address_is_allowed_only_when_configured_and_private() { let url = "http://192.168.1.100:54321"; assert!(check_destination(url, None).is_err()); assert!(check_destination(url, Some("192.168.1.101")).is_err()); assert!(check_destination(url, Some("192.168.1.100")).is_ok()); // A public address is never a DEV address, even when named as one. assert!(check_destination("http://8.8.8.8:54321", Some("8.8.8.8")).is_err()); // A name is not resolved: it cannot be proven local. assert!(check_destination("http://mybox:54321", Some("mybox")).is_err()); } #[test] fn the_plan_follows_what_the_destination_holds_and_never_overwrites() { let entry = entry_for(SVG); let plan = |object, row| plan_entry(&entry, &object, &row); let go = |upload, confirm| Ok(Plan { upload, confirm }); assert_eq!(plan(ObjectState::Absent, RowState::Pending), go(true, true)); assert_eq!( plan(ObjectState::Verified, RowState::Pending), go(false, true) ); assert_eq!( plan(ObjectState::Verified, RowState::Confirmed), go(false, false) ); assert_eq!( plan(ObjectState::Absent, RowState::Confirmed), go(true, false) ); for (object, row) in [ (ObjectState::Different("f".repeat(64)), RowState::Pending), (ObjectState::Absent, RowState::Unknown), (ObjectState::Verified, RowState::OtherHash("f".repeat(64))), ] { assert_eq!(kind(plan(object, row)), UploadKind::State); } }