|
| 1 | +//! Exercise the default macOS policy through the real signed carrier fixture. |
| 2 | +
|
| 3 | +use std::{ |
| 4 | + ffi::OsStr, |
| 5 | + fs, |
| 6 | + os::unix::fs::{MetadataExt, PermissionsExt}, |
| 7 | + path::Path, |
| 8 | + process::Command, |
| 9 | +}; |
| 10 | + |
| 11 | +use super::fixture_source; |
| 12 | + |
| 13 | +#[derive(Clone, Copy, Debug)] |
| 14 | +enum Installation { |
| 15 | + Default, |
| 16 | + Empty, |
| 17 | + Custom, |
| 18 | + Fallback, |
| 19 | + Temporary, |
| 20 | + Hardlinked, |
| 21 | + ReadOnly, |
| 22 | +} |
| 23 | + |
| 24 | +pub fn check_cache_policy(node: &OsStr, source: &Path, raw: &[u8], key: &str) { |
| 25 | + for installation in [ |
| 26 | + Installation::Default, |
| 27 | + Installation::Empty, |
| 28 | + Installation::Custom, |
| 29 | + Installation::Fallback, |
| 30 | + Installation::Temporary, |
| 31 | + Installation::Hardlinked, |
| 32 | + Installation::ReadOnly, |
| 33 | + ] { |
| 34 | + let temporary = tempfile::tempdir().unwrap(); |
| 35 | + // Darwin's /var alias is intentionally not accepted by cache validation. |
| 36 | + let root = temporary.path().canonicalize().unwrap(); |
| 37 | + let installed = root.join("installed"); |
| 38 | + fs::create_dir(&installed).unwrap(); |
| 39 | + let carrier = installed.join("carrier.node"); |
| 40 | + fs::copy(source, &carrier).unwrap(); |
| 41 | + let original = fs::read(&carrier).unwrap(); |
| 42 | + let default = root.join("default"); |
| 43 | + let custom = root.join("custom"); |
| 44 | + let blocked = root.join("blocked"); |
| 45 | + fs::write(&blocked, b"unusable cache root").unwrap(); |
| 46 | + if matches!(installation, Installation::Hardlinked) { |
| 47 | + fs::hard_link(&carrier, installed.join("carrier-link.node")).unwrap(); |
| 48 | + } |
| 49 | + if matches!(installation, Installation::ReadOnly) { |
| 50 | + fs::set_permissions(&carrier, fs::Permissions::from_mode(0o444)).unwrap(); |
| 51 | + fs::set_permissions(&installed, fs::Permissions::from_mode(0o555)).unwrap(); |
| 52 | + } |
| 53 | + let before = fs::metadata(&carrier).unwrap(); |
| 54 | + let mut command = Command::new(node); |
| 55 | + command |
| 56 | + .arg(fixture_source("smoke.cjs")) |
| 57 | + .arg(&carrier) |
| 58 | + .arg("success") |
| 59 | + .env("XDG_CACHE_HOME", &default) |
| 60 | + .env_remove("SWC_NATIVE_BINDING_CACHE"); |
| 61 | + let cache_root = match installation { |
| 62 | + Installation::Default => &default, |
| 63 | + Installation::Empty => { |
| 64 | + command.env("SWC_NATIVE_BINDING_CACHE", ""); |
| 65 | + &default |
| 66 | + } |
| 67 | + Installation::Fallback => { |
| 68 | + command.env("SWC_NATIVE_BINDING_CACHE", &blocked); |
| 69 | + &default |
| 70 | + } |
| 71 | + Installation::Temporary => { |
| 72 | + command.env("SWC_NATIVE_BINDING_CACHE", "0"); |
| 73 | + &default |
| 74 | + } |
| 75 | + _ => { |
| 76 | + command.env("SWC_NATIVE_BINDING_CACHE", &custom); |
| 77 | + &custom |
| 78 | + } |
| 79 | + }; |
| 80 | + let mut image_identity = None; |
| 81 | + for load in 0..3 { |
| 82 | + // Each invocation is a fresh Node process: cold, warm, then repair. |
| 83 | + let output = command.output().unwrap(); |
| 84 | + if !output.status.success() { |
| 85 | + fs::set_permissions(&installed, fs::Permissions::from_mode(0o755)).unwrap(); |
| 86 | + panic!( |
| 87 | + "{installation:?}: {}", |
| 88 | + String::from_utf8_lossy(&output.stderr) |
| 89 | + ); |
| 90 | + } |
| 91 | + let after = fs::metadata(&carrier).unwrap(); |
| 92 | + assert_eq!(after.ino(), before.ino(), "{installation:?}"); |
| 93 | + assert_eq!(after.mode(), before.mode(), "{installation:?}"); |
| 94 | + assert_eq!(after.nlink(), before.nlink(), "{installation:?}"); |
| 95 | + assert_eq!(fs::read(&carrier).unwrap(), original, "{installation:?}"); |
| 96 | + let directory = swc_native_addon::cache::cache_directory(cache_root).unwrap(); |
| 97 | + let image = directory.join(format!("{key}.node")); |
| 98 | + if matches!(installation, Installation::Temporary) { |
| 99 | + assert!(!image.exists()); |
| 100 | + assert!(fs::read_dir(&directory).unwrap().all(|entry| { |
| 101 | + entry |
| 102 | + .unwrap() |
| 103 | + .path() |
| 104 | + .extension() |
| 105 | + .map_or(true, |ext| ext != "node") |
| 106 | + })); |
| 107 | + } else { |
| 108 | + assert_eq!(fs::read(&image).unwrap(), raw, "{installation:?}"); |
| 109 | + let metadata = fs::metadata(&image).unwrap(); |
| 110 | + if load == 0 { |
| 111 | + image_identity = Some((metadata.ino(), metadata.modified().unwrap())); |
| 112 | + } else if load == 1 { |
| 113 | + assert_eq!( |
| 114 | + Some((metadata.ino(), metadata.modified().unwrap())), |
| 115 | + image_identity, |
| 116 | + "verified cache hit must reuse the existing image" |
| 117 | + ); |
| 118 | + // Same-length damage must be detected by the full-byte digest. |
| 119 | + let mut damaged = raw.to_vec(); |
| 120 | + let last = damaged.last_mut().unwrap(); |
| 121 | + *last ^= 1; |
| 122 | + fs::write(&image, damaged).unwrap(); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + assert_eq!(fs::read(&blocked).unwrap(), b"unusable cache root"); |
| 127 | + fs::set_permissions(&installed, fs::Permissions::from_mode(0o755)).unwrap(); |
| 128 | + } |
| 129 | +} |
0 commit comments