//! `XMPFile` in a script: reading and writing a sidecar packet through the script's file places, //! scanning another file for its packet, and the paths and packets it refuses. use std::path::{Path, PathBuf}; use serde_json::{Value, json}; use crate::*; const LOAD: &str = "new ExternalObject('lib:AdobeXMPScript');\n"; const PACKET: &str = "\ \ \ image/svg+xmlab\ "; #[test] fn a_large_supported_packet_crosses_the_sidecar_write_transport() { let base = folder("large-write-transport"); let payload = "x".repeat(6 << 20); let packet = PACKET.replace("image/svg+xml", &payload); let mut session = with_data(&base); let setup = logged(&mut session, &base, "vc.log(Folder.userData.fsName + '/large.xmp');"); let path = PathBuf::from(setup[0].as_str().unwrap()); write(&path, packet.as_bytes()); let log = logged( &mut session, &base, "var p = Folder.userData.fsName + '/large.xmp'; var x = new XMPFile(p, XMPConst.FILE_XML, XMPConst.OPEN_FOR_UPDATE); var m = x.getXMP(); m.setProperty(XMPConst.NS_DC, 'title', 'changed-for-write'); x.putXMP(m); x.closeFile(); vc.log(true);", ); assert_eq!(log, json!([true])); let written = std::fs::read_to_string(path).unwrap(); assert!(written.contains(&payload)); assert!(written.contains("changed-for-write")); } struct Folder { _guard: vectorcraft_testkit::TempDir, path: PathBuf, } impl std::ops::Deref for Folder { type Target = PathBuf; fn deref(&self) -> &Self::Target { &self.path } } impl AsRef for Folder { fn as_ref(&self) -> &Path { &self.path } } /// A fresh folder for test `name`, with a `main` folder inside it for the script, by its real path. fn folder(name: &str) -> Folder { let guard = vectorcraft_testkit::TempDir::new(&format!("script-xmp-{name}")); let base = crate::cmd::findfiles::plain(std::fs::canonicalize(&*guard).unwrap()); std::fs::create_dir_all(base.join("main")).unwrap(); Folder { _guard: guard, path: base } } fn write(path: &Path, bytes: &[u8]) { if let Some(dir) = path.parent() { std::fs::create_dir_all(dir).unwrap(); } std::fs::write(path, bytes).unwrap(); } /// A session whose scripts keep their data folders in `base/settings/Script Data`. fn with_data(base: &Path) -> Session { let mut s = Session::new(); s.attach_script_data(base.join("settings/Script Data")); s } /// Runs `main/main.jsx` in `base` with the XMP library loaded and the text `text` in `s`. fn run_in(s: &mut Session, base: &Path, text: &str) -> Result { let main = base.join("main/main.jsx"); write(&main, format!("{LOAD}{text}").as_bytes()); s.execute("script.run", &json!({"path": main.to_string_lossy()})) } /// The values the run of `text` logged. fn logged(s: &mut Session, base: &Path, text: &str) -> Value { match run_in(s, base, text) { Ok(r) => r["log"].clone(), Err(e) => panic!("{text}\n{e}"), } } /// A sidecar in the script's data folder reads, takes a changed packet and holds it after /// `closeFile`; a new `XMPFile` reads it back. #[test] fn a_sidecar_packet_reads_and_writes() { let base = folder("sidecar"); let mut s = with_data(&base); let log = logged( &mut s, &base, &format!( "var p = Folder.userData.fsName + '/a.xmp'; var f = new File(p); f.encoding = 'UTF-8'; f.open('w'); f.write({PACKET:?}); f.close();\n\ var x = new XMPFile(p, XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_UPDATE);\n\ var m = x.getXMP(); var info = x.getFileInfo();\n\ var out = [m.getProperty(XMPConst.NS_DC, 'format').value, m.countArrayItems(XMPConst.NS_DC, 'subject'), info.format == XMPConst.FILE_XML, x.canPutXMP(m)];\n\ m.setProperty(XMPConst.NS_DC, 'format', 'text/plain'); m.appendArrayItem(XMPConst.NS_DC, 'subject', 'c');\n\ x.putXMP(m); x.closeFile();\n\ var y = new XMPFile(p, XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_READ); var n = y.getXMP();\n\ out.push(n.getProperty(XMPConst.NS_DC, 'format').value, n.getArrayItem(XMPConst.NS_DC, 'subject', 3).value, y.getPacketInfo().offset, y.canPutXMP(n));\n\ y.closeFile(); vc.log(out); vc.log(p);" ), ); assert_eq!(log[0], json!(["image/svg+xml", 2, true, true, "text/plain", "c", 0, false])); let path = PathBuf::from(log[1].as_str().unwrap()); let text = std::fs::read_to_string(&path).unwrap(); assert!(text.contains("text/plain"), "{text}"); let leftovers: Vec<_> = std::fs::read_dir(path.parent().unwrap()).unwrap().filter_map(|e| e.ok()).map(|e| e.file_name()).collect(); assert_eq!(leftovers.len(), 1, "no temporary file stays: {leftovers:?}"); let _ = std::fs::remove_dir_all(base); } /// A file that is not a sidecar is scanned for its packet, read only, and a file with none gives /// `null`. #[test] fn another_file_is_scanned_for_its_packet() { let base = folder("scan"); let mut bytes = b"\x89binary\x00head".to_vec(); bytes.extend_from_slice(PACKET.as_bytes()); bytes.extend_from_slice(b"\x00tail"); write(&base.join("main/art.bin"), &bytes); write(&base.join("main/none.bin"), b"no packet here"); let mut s = Session::new(); let log = logged( &mut s, &base, "var x = new XMPFile(File('art.bin').fsName, XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_READ); var i = x.getPacketInfo();\n\ var out = [x.getXMP().getProperty(XMPConst.NS_DC, 'format').value, i.offset, i.length, i.writeable, x.canPutXMP(x.getXMP())];\n\ try { x.putXMP(x.getXMP()); out.push('put'); } catch (e) { out.push(e.message); }\n\ out.push(new XMPFile(File('none.bin').fsName, XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_READ).getXMP());\n\ x.closeFile(); try { x.getXMP(); } catch (e) { out.push(e.message); } vc.log(out);", ); assert_eq!( log[0], json!([ "image/svg+xml", 12, PACKET.len(), true, false, "XMPFile.putXMP: the file cannot be updated", null, "XMPFile.getXMP: the file is closed" ]) ); let _ = std::fs::remove_dir_all(base); } /// `getPacketInfo().padSize` gives the bytes of whitespace before the packet's trailer: twice the /// characters in a UTF-16 packet, and 0 in a packet with none. #[test] fn packet_info_gives_the_padding() { let base = folder("pad"); let padded = PACKET.replace("');\n\ function text() {{ var g = new File(p); g.open('r'); var t = g.read(); g.close(); return t; }}\n\ var out = []; try {{ x.closeFile(); }} catch (e) {{ out.push(e.message); }} f.close(); out.push(text().indexOf('kept') > 0);\n\ x.closeFile(); out.push(text().indexOf('changed') > 0); vc.log(out);" ), ); assert_eq!(log[0], json!(["Script file could not be written", true, true]), "the File's changes are written, then the packet"); let _ = std::fs::remove_dir_all(base); } /// A file with the name of the temporary file is left alone: the packet goes through another name /// and replaces only the sidecar. #[test] fn a_file_in_the_temporary_name_is_left_alone() { let base = folder("taken"); let mut s = with_data(&base); let path = logged( &mut s, &base, &format!("var f = new File(Folder.userData.fsName + '/t.xmp'); f.open('w'); f.write({PACKET:?}); f.close(); vc.log(f.fsName);"), ); let path = PathBuf::from(path[0].as_str().unwrap()); let taken = path.with_file_name(format!(".t.xmp.{}.0.tmp", std::process::id())); write(&taken, b"someone else's"); let log = logged( &mut s, &base, "var x = new XMPFile(Folder.userData.fsName + '/t.xmp', XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_UPDATE);\n\ var m = x.getXMP(); m.setProperty(XMPConst.NS_DC, 'format', 'changed'); x.putXMP(m); x.closeFile(); vc.log('closed');", ); assert_eq!(log, json!(["closed"])); assert_eq!(std::fs::read(&taken).unwrap(), b"someone else's"); assert!(std::fs::read_to_string(&path).unwrap().contains("changed")); let names: Vec = std::fs::read_dir(path.parent().unwrap()).unwrap().map(|e| e.unwrap().file_name().to_string_lossy().into_owned()).collect(); assert_eq!(names.iter().filter(|n| n.ends_with(".tmp")).count(), 1, "no temporary file is left behind: {names:?}"); let _ = std::fs::remove_dir_all(base); } /// Pumps the run until it waits for a file dialog (`None`) or ends. fn pump_to_picker(s: &mut Session) -> Option> { for _ in 0..10_000 { if s.script_picker().is_some() { return None; } if let Some(r) = s.script_pump() { return Some(r); } } panic!("the run neither ended nor waited"); } /// Starts `main/main.jsx` in `base` with the XMP library loaded and the text `text`, in the app. fn start_framed(s: &mut Session, base: &Path, text: &str) { s.script_frames = true; let main = base.join("main/main.jsx"); write(&main, format!("{LOAD}{text}").as_bytes()); assert_eq!(s.execute("script.run", &json!({"path": main.to_string_lossy()})).unwrap()["running"], true); } /// With only the sidecar and the first temporary name granted, a taken first name does not make /// the packet go to another name outside the grants: `closeFile()` throws and no file is made. #[test] fn temporary_names_outside_the_grants_are_not_made() { let base = folder("tempgrant"); let (sidecar, taken) = (base.join("picked/t.xmp"), base.join(format!("picked/.t.xmp.{}.0.tmp", std::process::id()))); write(&sidecar, PACKET.as_bytes()); write(&taken, b"someone else's"); let mut s = with_data(&base); start_framed( &mut s, &base, "var a = File.saveDialog('a'), b = File.saveDialog('b');\n\ var x = new XMPFile(a.fsName, XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_UPDATE);\n\ var m = x.getXMP(); m.setProperty(XMPConst.NS_DC, 'format', 'changed'); x.putXMP(m);\n\ try { x.closeFile(); vc.log('closed'); } catch (e) { vc.log(e.message); }", ); for path in [&sidecar, &taken] { assert!(pump_to_picker(&mut s).is_none()); assert!(s.script_pick(crate::script::files::PickAnswer::Allow { paths: vec![path.to_string_lossy().into_owned()], always: false })); } let r = pump_to_picker(&mut s).unwrap().unwrap(); assert_eq!(r["log"], json!(["Script file could not be written"])); assert_eq!(std::fs::read_to_string(&sidecar).unwrap(), PACKET); assert_eq!(std::fs::read(&taken).unwrap(), b"someone else's"); let names: Vec = std::fs::read_dir(base.join("picked")).unwrap().map(|e| e.unwrap().file_name().to_string_lossy().into_owned()).collect(); assert_eq!(names.len(), 2, "no other file is made: {names:?}"); let _ = std::fs::remove_dir_all(base); } /// A `.xmp` link to another kind of file is scanned and takes no packet, and a link that leads to /// another kind of file by the time of `closeFile()` is not written through. #[cfg(unix)] #[test] fn a_link_to_another_kind_of_file_is_not_written() { let base = folder("link"); let mut s = with_data(&base); let log = logged( &mut s, &base, &format!( "var d = Folder.userData.fsName, l = d + '/l.xmp';\n\ function put(n) {{ var f = new File(d + '/' + n); f.open('w'); f.write({PACKET:?}); f.close(); }}\n\ function link(n) {{ var f = new File(l); if (f.exists) f.remove(); return !!new File(l).createAlias(d + '/' + n); }}\n\ put('other.txt'); put('real.xmp'); var out = [link('other.txt')];\n\ var a = new XMPFile(l, XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_UPDATE); out.push(a.canPutXMP(a.getXMP())); a.closeFile();\n\ out.push(link('real.xmp'));\n\ var x = new XMPFile(l, XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_UPDATE); var m = x.getXMP();\n\ m.setProperty(XMPConst.NS_DC, 'format', 'changed'); out.push(x.canPutXMP(m)); x.putXMP(m);\n\ out.push(link('other.txt'));\n\ try {{ x.closeFile(); out.push('closed'); }} catch (e) {{ out.push(e.message); }}\n\ vc.log(out); vc.log(d);" ), ); assert_eq!(log[0], json!([true, false, true, true, true, "Script file access denied"])); let d = PathBuf::from(log[1].as_str().unwrap()); assert_eq!(std::fs::read_to_string(d.join("other.txt")).unwrap(), PACKET); assert_eq!(std::fs::read_to_string(d.join("real.xmp")).unwrap(), PACKET); let _ = std::fs::remove_dir_all(base); } /// A sidecar replaced through a temporary file keeps its permission bits. #[cfg(unix)] #[test] fn a_replaced_sidecar_keeps_its_permissions() { use std::os::unix::fs::PermissionsExt; let base = folder("mode"); let mut s = with_data(&base); let log = logged( &mut s, &base, &format!("var p = Folder.userData.fsName + '/a.xmp'; var f = new File(p); f.open('w'); f.write({PACKET:?}); f.close(); vc.log(p);"), ); let path = PathBuf::from(log[0].as_str().unwrap()); std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap(); logged( &mut s, &base, "var x = new XMPFile(Folder.userData.fsName + '/a.xmp', XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_UPDATE);\n\ var m = x.getXMP(); m.setProperty(XMPConst.NS_DC, 'format', 'changed'); x.putXMP(m); x.closeFile();", ); assert!(std::fs::read_to_string(&path).unwrap().contains("changed")); assert_eq!(std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, 0o600); let _ = std::fs::remove_dir_all(base); } /// A sidecar in UTF-16 is read by its byte order mark, and one that is not text is refused. #[test] fn a_utf16_sidecar_reads_by_its_byte_order_mark() { let base = folder("utf16"); let mut le = vec![0xFF, 0xFE]; le.extend(PACKET.encode_utf16().flat_map(u16::to_le_bytes)); write(&base.join("main/le.xmp"), &le); write(&base.join("main/bad.xmp"), &[0xFF, 0xFE, 0x00]); let mut s = Session::new(); let log = logged( &mut s, &base, "var x = new XMPFile(File('le.xmp').fsName, XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_READ);\n\ var out = [x.getXMP().getProperty(XMPConst.NS_DC, 'format').value, x.getPacketInfo().charForm];\n\ try { new XMPFile(File('bad.xmp').fsName, XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_READ); } catch (e) { out.push(e.message); } vc.log(out);", ); assert_eq!(log[0], json!(["image/svg+xml", 3, "Script file cannot be decoded"])); let _ = std::fs::remove_dir_all(base); } /// A `File` open on a UTF-16 sidecar reads the UTF-8 packet `closeFile()` wrote, from the start. #[test] fn an_open_utf16_file_reads_the_written_packet() { let base = folder("utf16open"); let mut s = with_data(&base); let d = logged(&mut s, &base, "vc.log(Folder.userData.fsName);"); let d = PathBuf::from(d[0].as_str().unwrap()); let mut le = vec![0xFF, 0xFE]; le.extend(PACKET.encode_utf16().flat_map(u16::to_le_bytes)); write(&d.join("u.xmp"), &le); let log = logged( &mut s, &base, "var p = Folder.userData.fsName + '/u.xmp'; var f = new File(p); f.open('r'); var out = [f.encoding, f.read(5)];\n\ var x = new XMPFile(p, XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_UPDATE); var m = x.getXMP();\n\ m.setProperty(XMPConst.NS_DC, 'format', 'changed'); x.putXMP(m); x.closeFile();\n\ var t = f.read(), e = f.encoding; f.close(); out.push(e, t.slice(0, 9), t.indexOf('changed') > 0); vc.log(out);", ); assert_eq!(log[0], json!(["UTF-16LE", "\n{PACKET}\n"); let packet = format!("{body}"); let mut bytes = vec![0xFF, 0xFE]; bytes.extend(packet.encode_utf16().flat_map(u16::to_le_bytes)); write(&data.join("declared.xmp"), &bytes); logged( &mut session, &base, "var x = new XMPFile(Folder.userData.fsName + '/declared.xmp', XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_UPDATE); var packet = x.getPacketInfo().packet; x.putXMP(packet); x.closeFile();", ); let written = std::fs::read_to_string(data.join("declared.xmp")).unwrap(); assert_eq!(written, body); let _ = std::fs::remove_dir_all(base); } #[test] fn an_xmp_update_resets_bom_readers_and_keeps_unmarked_reader_positions() { for bom in [true, false] { let base = folder(if bom { "utf8bomopen" } else { "utf8open" }); let mut session = with_data(&base); let data = logged(&mut session, &base, "vc.log(Folder.userData.fsName);"); let data = PathBuf::from(data[0].as_str().unwrap()); let mut bytes = if bom { vec![0xEF, 0xBB, 0xBF] } else { Vec::new() }; bytes.extend_from_slice(PACKET.as_bytes()); write(&data.join("u.xmp"), &bytes); let log = logged( &mut session, &base, &format!( "var p = Folder.userData.fsName + '/u.xmp', f = new File(p); f.open('r');\n\ if (!{bom}) f.read(3); var before = f.tell();\n\ var x = new XMPFile(p, XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_UPDATE), m = x.getXMP();\n\ m.setProperty(XMPConst.NS_DC, 'format', 'changed'); x.putXMP(m); x.closeFile();\n\ var after = f.tell(), text = f.read(); f.close(); vc.log([before, after, text.slice(0, 9)]);" ), ); assert_eq!(log[0], if bom { json!([3, 0, " Option { for entry in std::fs::read_dir(dir).ok()?.filter_map(|e| e.ok()) { let path = entry.path(); if path.is_dir() { if let Some(found) = find_file(&path, name) { return Some(found); } } else if entry.file_name() == name { return Some(path); } } None } /// A write that fails leaves the sidecar as it was and the `XMPFile` open with its packet, so a /// later `closeFile` of the same object writes it. #[test] fn a_failed_write_keeps_the_packet_for_a_later_close() { let base = folder("fails"); let mut s = with_data(&base); crate::script::files::XMP_WRITE_FAILS.with(|f| f.set(true)); start_framed( &mut s, &base, &format!( "var p = Folder.userData.fsName + '/k.xmp'; var f = new File(p); f.open('w'); f.write({PACKET:?}); f.close();\n\ var kept = new XMPFile(p, XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_UPDATE);\n\ var m = kept.getXMP(); m.setProperty(XMPConst.NS_DC, 'format', 'changed'); kept.putXMP(m);\n\ var out = []; try {{ kept.closeFile(); out.push('closed'); }} catch (e) {{ out.push(e.message, kept.getXMP().getProperty(XMPConst.NS_DC, 'format').value); }}\n\ File.saveDialog('pause'); kept.closeFile(); out.push('closed'); vc.log(out); vc.log(p);" ), ); let paused = pump_to_picker(&mut s); crate::script::files::XMP_WRITE_FAILS.with(|f| f.set(false)); assert!(paused.is_none(), "the run waits at the dialog: {paused:?}"); let path = find_file(&base.join("settings"), "k.xmp").unwrap(); assert_eq!(std::fs::read_to_string(&path).unwrap(), PACKET, "the failed write leaves the file as it was"); assert!(s.script_pick(crate::script::files::PickAnswer::Deny)); let r = pump_to_picker(&mut s).unwrap().unwrap(); assert_eq!(r["log"][0], json!(["Script file could not be written", "changed", "closed"])); assert!(std::fs::read_to_string(&path).unwrap().contains("changed"), "the same XMPFile writes on its next close"); let _ = std::fs::remove_dir_all(base); } #[cfg(unix)] #[test] fn a_directory_flush_failure_keeps_the_written_packet_and_allows_retry() { struct Reset; impl Drop for Reset { fn drop(&mut self) { crate::script::files::XMP_DIRECTORY_SYNC_FAILS.with(|flag| flag.set(false)); } } let _reset = Reset; let base = folder("directory-flush"); let mut session = with_data(&base); let dir = logged(&mut session, &base, "vc.log(Folder.userData.fsName);"); let path = PathBuf::from(dir[0].as_str().unwrap()).join("packet.xmp"); write(&path, format!("\u{FEFF}{PACKET}").as_bytes()); crate::script::files::XMP_DIRECTORY_SYNC_FAILS.with(|flag| flag.set(true)); start_framed( &mut session, &base, &format!( "Folder.userData; var p = {:?}, reader = new File(p); reader.open('r'); reader.read(7);\n\ var kept = new XMPFile(p, XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_UPDATE);\n\ var meta = kept.getXMP(); meta.setProperty(XMPConst.NS_DC, 'format', 'changed'); kept.putXMP(meta);\n\ var out = []; try {{ kept.closeFile(); out.push('closed'); }} catch (e) {{ out.push(e.message, kept.getXMP().getProperty(XMPConst.NS_DC, 'format').value); }}\n\ out.push(reader.tell() === 0, reader.read().indexOf('changed') >= 0); reader.close();\n\ File.saveDialog('pause'); kept.closeFile(); out.push('closed'); vc.log(out);", path.to_string_lossy() ), ); let paused = pump_to_picker(&mut session); crate::script::files::XMP_DIRECTORY_SYNC_FAILS.with(|flag| flag.set(false)); assert!(paused.is_none(), "the run waits before retrying: {paused:?}"); assert!(std::fs::read_to_string(&path).unwrap().contains("changed"), "the replacement already wrote the packet"); assert!(session.script_pick(crate::script::files::PickAnswer::Deny)); let report = pump_to_picker(&mut session).unwrap().unwrap(); assert_eq!(report["log"][0], json!(["Script file could not be written", "changed", true, true, "closed"])); assert_eq!(report["written"], json!([path.to_string_lossy()]), "the written replacement is reported once"); } /// An `XMPFile` from an earlier run of a named engine is closed. #[test] fn an_xmp_file_from_an_earlier_run_is_closed() { let base = folder("earlier"); let mut s = with_data(&base); let engine = |text: &str| format!("#targetengine xmp\n{LOAD}{text}"); let main = base.join("main/main.jsx"); write(&main, engine(&format!("var f = new File(Folder.userData.fsName + '/k2.xmp'); f.open('w'); f.write({PACKET:?}); f.close();")).as_bytes()); s.execute("script.run", &json!({"path": main.to_string_lossy()})).unwrap(); write( &main, engine("$.global.kept = new XMPFile(Folder.userData.fsName + '/k2.xmp', XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_UPDATE);").as_bytes(), ); s.execute("script.run", &json!({"path": main.to_string_lossy()})).unwrap(); write(&main, engine("try { kept.getXMP(); vc.log('open'); } catch (e) { vc.log(e.message); }").as_bytes()); let r = s.execute("script.run", &json!({"path": main.to_string_lossy()})).unwrap(); assert_eq!(r["log"], json!(["XMPFile.getXMP: the file is closed"]), "an XMPFile from an earlier run is closed"); let _ = std::fs::remove_dir_all(base); } /// The temporary file a sidecar is replaced through is readable by its owner alone while the /// packet is written to it, and the sidecar keeps its own permission bits. #[cfg(unix)] #[test] fn a_temporary_file_starts_private() { use std::os::unix::fs::PermissionsExt; let base = folder("private"); let mut s = with_data(&base); let log = logged( &mut s, &base, &format!("var p = Folder.userData.fsName + '/a.xmp'; var f = new File(p); f.open('w'); f.write({PACKET:?}); f.close(); vc.log(p);"), ); let path = PathBuf::from(log[0].as_str().unwrap()); std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap(); crate::script::files::XMP_TEMP_MODE.with(|m| m.set(None)); logged( &mut s, &base, "var x = new XMPFile(Folder.userData.fsName + '/a.xmp', XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_UPDATE);\n\ var m = x.getXMP(); m.setProperty(XMPConst.NS_DC, 'format', 'changed'); x.putXMP(m); x.closeFile();", ); assert_eq!(crate::script::files::XMP_TEMP_MODE.with(std::cell::Cell::get).map(|m| m & 0o777), Some(0o600)); assert_eq!(std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, 0o644); let _ = std::fs::remove_dir_all(base); } /// A replaced sidecar that had no access ACL takes none from its folder's default ACL, whose named /// users the sidecar's group bits would let in. The default ACL is set through its extended /// attribute, so the test runs where the `acl` tools are not installed. #[cfg(target_os = "linux")] #[test] fn a_replaced_sidecar_takes_no_inherited_acl() { let base = folder("acl"); let mut s = with_data(&base); let log = logged( &mut s, &base, &format!("var p = Folder.userData.fsName + '/a.xmp'; var f = new File(p); f.open('w'); f.write({PACKET:?}); f.close(); vc.log(p);"), ); let path = PathBuf::from(log[0].as_str().unwrap()); let parent = path.parent().unwrap(); // `u::rw-,u:nobody:rw-,g::r--,m::rw-,o::r--` as the kernel's attribute: version 2, then tag, // permissions and id per entry. let mut acl = 2u32.to_le_bytes().to_vec(); for (tag, perm, id) in [(0x01u16, 6u16, u32::MAX), (0x02, 6, 65534), (0x04, 4, u32::MAX), (0x10, 6, u32::MAX), (0x20, 4, u32::MAX)] { acl.extend(tag.to_le_bytes()); acl.extend(perm.to_le_bytes()); acl.extend(id.to_le_bytes()); } rustix::fs::setxattr(parent, "system.posix_acl_default", &acl, rustix::fs::XattrFlags::empty()) .unwrap_or_else(|e| panic!("setting the default ACL (the temporary folder's file system needs ACLs): {e}")); let mut buf = vec![0u8; 4096]; let probe = parent.join("probe"); std::fs::write(&probe, b"").unwrap(); assert!(rustix::fs::getxattr(&probe, "system.posix_acl_access", &mut buf[..]).is_ok(), "a new file takes the default ACL"); std::fs::remove_file(&probe).unwrap(); logged( &mut s, &base, "var x = new XMPFile(Folder.userData.fsName + '/a.xmp', XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_UPDATE);\n\ var m = x.getXMP(); m.setProperty(XMPConst.NS_DC, 'format', 'changed'); x.putXMP(m); x.closeFile();", ); assert!(std::fs::read_to_string(&path).unwrap().contains("changed")); assert_eq!( rustix::fs::getxattr(&path, "system.posix_acl_access", &mut buf[..]), Err(rustix::io::Errno::NODATA), "the replacement takes no ACL entry the sidecar lacked" ); let _ = std::fs::remove_dir_all(base); } /// A packet argument whose conversion closes the `XMPFile` leaves it closed: `canPutXMP` and /// `putXMP` throw that the file is closed, and nothing is written. #[test] fn converting_the_packet_cannot_close_the_file() { let base = folder("reentrant"); let mut s = with_data(&base); let log = logged( &mut s, &base, &format!( "var p = Folder.userData.fsName + '/r.xmp'; var f = new File(p); f.open('w'); f.write({PACKET:?}); f.close();\n\ function closing(x) {{ var m = x.getXMP(); m.setProperty(XMPConst.NS_DC, 'format', 'changed'); var s = m.serialize();\n\ return {{ toString: function () {{ x.closeFile(); return s; }} }}; }}\n\ var out = [], a = new XMPFile(p, XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_UPDATE);\n\ try {{ out.push(a.canPutXMP(closing(a))); }} catch (e) {{ out.push(e.message); }}\n\ var b = new XMPFile(p, XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_UPDATE);\n\ try {{ b.putXMP(closing(b)); out.push('put'); }} catch (e) {{ out.push(e.message); }}\n\ b.closeFile(); vc.log(out); vc.log(p);" ), ); assert_eq!(log[0], json!(["XMPFile.canPutXMP: the file is closed", "XMPFile.putXMP: the file is closed"])); assert_eq!(std::fs::read_to_string(log[1].as_str().unwrap()).unwrap(), PACKET); let _ = std::fs::remove_dir_all(base); } /// With `CLOSE_UPDATE_SAFELY`, a sidecar whose folder takes no temporary file is left as it is and /// the `XMPFile` keeps its packet; a later `closeFile()` without the flag writes the file itself. #[test] fn a_safe_close_writes_only_through_a_temporary_file() { let base = folder("safely"); let sidecar = base.join("picked/s.xmp"); write(&sidecar, PACKET.as_bytes()); let mut s = with_data(&base); start_framed( &mut s, &base, "var a = File.saveDialog('a');\n\ var x = new XMPFile(a.fsName, XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_UPDATE);\n\ var m = x.getXMP(); m.setProperty(XMPConst.NS_DC, 'format', 'changed'); x.putXMP(m);\n\ var out = []; try { x.closeFile(XMPConst.CLOSE_UPDATE_SAFELY); out.push('closed'); } catch (e) { out.push(e.message, x.getXMP().getProperty(XMPConst.NS_DC, 'format').value); }\n\ File.saveDialog('pause'); x.closeFile(); out.push('written'); vc.log(out);", ); assert!(pump_to_picker(&mut s).is_none()); assert!(s.script_pick(crate::script::files::PickAnswer::Allow { paths: vec![sidecar.to_string_lossy().into_owned()], always: false })); assert!(pump_to_picker(&mut s).is_none()); assert_eq!(std::fs::read_to_string(&sidecar).unwrap(), PACKET, "the safe close leaves the file as it was"); assert!(s.script_pick(crate::script::files::PickAnswer::Deny)); let r = pump_to_picker(&mut s).unwrap().unwrap(); assert_eq!(r["log"], json!([["Script file could not be written", "changed", "written"]])); assert!(std::fs::read_to_string(&sidecar).unwrap().contains("changed")); let names: Vec = std::fs::read_dir(base.join("picked")).unwrap().map(|e| e.unwrap().file_name().to_string_lossy().into_owned()).collect(); assert_eq!(names, ["s.xmp"], "no other file is made"); let _ = std::fs::remove_dir_all(base); }