juicewrld-desktopsrc-tauritestsscan.rs
1.4.2Released Nov 21, 2026
scan.rs
437 lines22.3 KBRust//! The gentle scan end to end, with the real scan child (juicewrld-plugins --scan) and the test plugin (host/testplug)//! copied under names that make it misbehave as it loads: one that opens an activation window and never finishes//! (what a copy-protected plugin with an expired licence does), one that crashes. Only modules this test makes are//! loaded, from a folder of its own; every load is logged (JW_PLUGINS_LOAD_LOG) and the log is checked.mod proto;mod pe;mod scan;use std::collections::HashSet;use std::path::{Path, PathBuf};use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};use std::sync::Arc;use std::time::{Duration, Instant};const HOST: &str = env!("CARGO_BIN_EXE_juicewrld-plugins");const DLL: &str = env!("JW_TEST_PLUGIN");/// The test plugin's activation window (host/testplug/jwtestplug.cpp, activationWindow).const ACTIVATION: &str = "JW Test Activation: all visible licenses expired";fn folder(name: &str) -> PathBuf { let base = std::env::var_os("JW_TEST_DIR").map(PathBuf::from).unwrap_or_else(std::env::temp_dir); let d = base.join(format!("jw-scan-{name}-{}", std::process::id())); let _ = std::fs::remove_dir_all(&d); std::fs::create_dir_all(&d).unwrap(); d}fn bundle(dir: &Path, name: &str) -> PathBuf { let b = dir.join(format!("{name}.vst3")); let bin = b.join("Contents").join("x86_64-win"); std::fs::create_dir_all(&bin).unwrap(); std::fs::copy(DLL, bin.join(format!("{name}.vst3"))).unwrap(); b}/// Whether a window with this title is open now, in any process.fn window_open(title: &str) -> bool { use windows::core::HSTRING; use windows::Win32::UI::WindowsAndMessaging::FindWindowW; unsafe { FindWindowW(None, &HSTRING::from(title)).is_ok() }}/// The modules the load log names, in order.fn loaded(log: &Path) -> Vec<String> { std::fs::read_to_string(log) .unwrap_or_default() .lines() .filter_map(|l| l.splitn(5, ' ').nth(4).map(str::to_string)) .collect()}/// The tests that set the scan children's environment (process-wide) run one at a time.static ENV: std::sync::Mutex<()> = std::sync::Mutex::new(());fn the_scan_is_gentle_one_at_a_time_and_remembers_what_needs_activation() { let _env = ENV.lock().unwrap_or_else(|e| e.into_inner()); let d = folder("gentle"); let log = d.join("loads.log"); // The scan children inherit these: they log every module they load, and refuse any outside this folder. std::env::set_var("JW_PLUGINS_LOAD_LOG", &log); std::env::set_var("JW_PLUGINS_ONLY_UNDER", &d); let plugins = d.join("plugins"); let good = bundle(&plugins, "JW Test"); let locked = bundle(&plugins, "JW Activation scan-hang"); let crashing = bundle(&plugins, "JW scan-crash"); bundle(&plugins, "JW noclasses"); let key = |p: &Path| p.to_string_lossy().to_string(); // A watcher notes when the activation window is up. let seen = Arc::new(AtomicBool::new(false)); let done = Arc::new(AtomicBool::new(false)); let watcher = { let (seen, done) = (seen.clone(), done.clone()); std::thread::spawn(move || { while !done.load(Ordering::SeqCst) { if window_open(ACTIVATION) { seen.store(true, Ordering::SeqCst); } std::thread::sleep(Duration::from_millis(20)); } }) }; let exe = Path::new(HOST); let stop = AtomicBool::new(false); let at_once = AtomicU32::new(0); let most = AtomicU32::new(0); let limit = Duration::from_secs(4); let timed: std::cell::RefCell<Vec<(String, Duration)>> = Default::default(); let mut load = |m: &Path| { let n = at_once.fetch_add(1, Ordering::SeqCst) + 1; most.fetch_max(n, Ordering::SeqCst); let t0 = Instant::now(); let r = scan::load_in_child(exe, m, limit, &stop); timed.borrow_mut().push((m.file_name().unwrap().to_string_lossy().to_string(), t0.elapsed())); at_once.fetch_sub(1, Ordering::SeqCst); r }; let mut steps = Vec::new(); let t0 = Instant::now(); let (cache, stats) = scan::scan(&[plugins.clone()], &scan::Cache::default(), &HashSet::new(), &stop, &mut load, &mut |p| steps.push(p.clone())); let first = t0.elapsed(); eprintln!("first scan: {stats:?} in {first:?}; each load: {:?}", timed.borrow()); assert_eq!(most.load(Ordering::SeqCst), 1, "modules load one at a time"); assert_eq!((stats.modules, stats.read, stats.loaded, stats.failed), (4, 4, 4, 2)); assert_eq!(stats.plugins, 3, "Gain, Crash and Synth from JW Test"); assert_eq!(stats.instruments, 1); // The locked module showed its window, was ended at its limit, and its window went with it. assert!(seen.load(Ordering::SeqCst), "the activation window never showed"); let hang_time = timed.borrow().iter().find(|(n, _)| n.contains("scan-hang")).unwrap().1; assert!(hang_time >= limit && hang_time < limit + Duration::from_secs(2), "{hang_time:?}"); std::thread::sleep(Duration::from_millis(300)); assert!(!window_open(ACTIVATION), "the activation window outlived its scan child"); let m = &cache.modules[&key(&locked)]; assert_eq!(m.problem, Some(scan::Problem::Timeout)); assert!(m.error.as_deref().unwrap().contains("activation"), "{:?}", m.error); assert_eq!(cache.modules[&key(&crashing)].problem, Some(scan::Problem::Crashed)); assert!(cache.modules[&key(&good)].error.is_none()); let unread: Vec<_> = scan::unread(&cache).into_iter().map(|u| u.name).collect(); assert_eq!(unread, ["JW Activation scan-hang.vst3", "JW scan-crash.vst3"]); assert_eq!(steps.iter().filter(|p| p.done < p.total).count(), 4, "progress for each module"); // Every module loaded is one of ours, from this folder, and each once. let loads = loaded(&log); assert_eq!(loads.len(), 4, "{loads:?}"); assert!(loads.iter().all(|p| proto::module_allowed(p, &[key(&plugins)])), "{loads:?}"); // Scanned again: nothing loads, the locked module least of all, and the list is the same. seen.store(false, Ordering::SeqCst); let t1 = Instant::now(); let (again, stats) = scan::scan(&[plugins.clone()], &cache, &HashSet::new(), &stop, &mut load, &mut |_| {}); eprintln!("second scan: {stats:?} in {:?}", t1.elapsed()); assert_eq!((stats.read, stats.loaded, stats.failed), (0, 0, 2)); assert_eq!(again.modules, cache.modules); assert_eq!(loaded(&log).len(), 4, "nothing loaded on the second scan"); assert!(!seen.load(Ordering::SeqCst), "no activation window on the second scan"); assert!(t1.elapsed() < Duration::from_secs(1)); // Try again, on the locked module alone: it loads once more (and is ended again). let retry: HashSet<String> = [key(&locked)].into(); let (again, stats) = scan::scan(&[plugins.clone()], &again, &retry, &stop, &mut load, &mut |_| {}); assert_eq!((stats.read, stats.loaded, stats.failed), (1, 1, 2)); assert!(seen.load(Ordering::SeqCst)); assert_eq!(loaded(&log).len(), 5); assert!(loaded(&log)[4].ends_with("JW Activation scan-hang.vst3")); // Its file changed (an update, say): it's loaded on the next scan without asking. Here the update fixed it. let bin = locked.join("Contents").join("x86_64-win").join("JW Activation scan-hang.vst3"); std::fs::remove_dir_all(&locked).unwrap(); let fixed = bundle(&plugins, "JW Activation fixed"); assert!(!bin.exists()); let (after, stats) = scan::scan(&[plugins.clone()], &again, &HashSet::new(), &stop, &mut load, &mut |_| {}); assert_eq!((stats.read, stats.loaded, stats.failed), (1, 1, 1)); assert!(after.modules[&key(&fixed)].error.is_none()); assert!(!after.modules.contains_key(&key(&locked))); // Stop: the module loading is ended at once, not at its limit, and isn't judged. let slow = bundle(&plugins, "JW Slow scan-hang"); let stop_now = AtomicBool::new(false); let t2 = Instant::now(); let mut load2 = |m: &Path| { // The user presses Stop scanning 800 ms into the load. let r = std::thread::scope(|s| { s.spawn(|| { std::thread::sleep(Duration::from_millis(800)); stop_now.store(true, Ordering::SeqCst); }); scan::load_in_child(exe, m, Duration::from_secs(20), &stop_now) }); r }; let (stopped, stats) = scan::scan(&[plugins.clone()], &after, &HashSet::new(), &stop_now, &mut load2, &mut |_| {}); assert!(stats.stopped); assert!(t2.elapsed() < Duration::from_secs(4), "{:?}", t2.elapsed()); assert!(!stopped.modules.contains_key(&key(&slow)), "a module the user stopped isn't marked"); std::thread::sleep(Duration::from_millis(300)); assert!(!window_open(ACTIVATION)); done.store(true, Ordering::SeqCst); watcher.join().unwrap(); let all = loaded(&log); eprintln!("modules loaded by this test's scan children ({}):", all.len()); for p in &all { eprintln!(" {p}"); } assert!(all.iter().all(|p| proto::module_allowed(p, &[key(&plugins)]))); let _ = std::fs::remove_dir_all(&d);}/// A module outside the folders a test run allows is refused by the scan child before it loads (debug builds).fn a_test_run_refuses_modules_outside_its_folder() { let d = folder("refuse"); let elsewhere = folder("refuse-elsewhere"); let outside = bundle(&elsewhere, "JW Test"); let out = std::process::Command::new(HOST) .arg("--scan") .arg(&outside) .env("JW_PLUGINS_ONLY_UNDER", &d) .env("JW_PLUGINS_LOAD_LOG", d.join("loads.log")) .output() .unwrap(); let text = String::from_utf8_lossy(&out.stdout); assert!(text.contains("outside the folders this test run allows"), "{text}"); assert!(String::from_utf8_lossy(&out.stderr).contains("refused")); // Nothing was loaded, so the load log has nothing. assert!(loaded(&d.join("loads.log")).is_empty()); let _ = std::fs::remove_dir_all(&d); let _ = std::fs::remove_dir_all(&elsewhere);}/// Plugins that can't run here, told apart by the headers of real DLLs a compiler built for this test/// (host/testplug/jwtestdll.c): an old-format plugin exporting VSTPluginMain, an older one exporting main, a 32-bit/// old-format one, a 32-bit VST 3 module (single file and bundle) and a helper library. Listed greyed and never loaded:/// no scan child starts at all (the load function would fail the test), and the one VST 3 version is read from its/// moduleinfo.json.fn old_format_and_32_bit_plugins_are_greyed_from_their_files_and_never_loaded() { let dlls = Path::new(env!("JW_TEST_DLLS")); let d = folder("greyed"); let plugins = d.join("plugins"); let x86 = dlls.join("vst2-x86.dll").is_file() && dlls.join("vst3-x86.dll").is_file(); let put = |from: &str, to: &Path| { std::fs::create_dir_all(to.parent().unwrap()).unwrap(); std::fs::copy(dlls.join(from), to).unwrap(); }; put("vst2-x64.dll", &plugins.join("JW Old Delay.dll")); put("vst2main-x64.dll", &plugins.join("Older").join("JW Older (Mono).dll")); put("helper-x64.dll", &plugins.join("JW Helper.dll")); if x86 { put("vst2-x86.dll", &plugins.join("x86").join("JW Crusher.dll")); put("vst3-x86.dll", &plugins.join("JW Tiny32.vst3")); put("vst3-x86.dll", &plugins.join("JW Bundle32.vst3").join("Contents").join("x86-win").join("JW Bundle32.vst3")); } else { eprintln!("no 32-bit test DLLs on this PC (no 32-bit compiler): the 32-bit cases are skipped"); } // The old-format plugin's VST 3 version, read from its moduleinfo.json (so nothing at all is loaded here). let vst3 = bundle(&plugins, "JW Old Delay"); std::fs::create_dir_all(vst3.join("Contents").join("Resources")).unwrap(); std::fs::write( scan::moduleinfo_path(&vst3), r#"{"Factory Info": {"Vendor": "juicewrld.io tests"}, "Classes": [{"CID": "4A57544741494E50524F433100000001", "Category": "Audio Module Class", "Name": "JW Old Delay", "Version": "2.0", "Sub Categories": ["Fx", "Delay"]}]}"#, ) .unwrap(); // What the compiler made, as the scan reads it. let info = pe::read(&plugins.join("JW Old Delay.dll")).unwrap(); assert_eq!((info.machine, info.exports.as_slice()), (pe::X64, &["VSTPluginMain".to_string()][..])); assert_eq!(pe::read(&plugins.join("Older").join("JW Older (Mono).dll")).unwrap().exports, ["main"]); assert!(!pe::read(&plugins.join("JW Helper.dll")).unwrap().exports_any(pe::VST2_ENTRIES)); if x86 { let i = pe::read(&plugins.join("x86").join("JW Crusher.dll")).unwrap(); assert_eq!((i.machine, i.exports.as_slice()), (pe::X86, &["VSTPluginMain".to_string()][..])); assert_eq!(pe::read(&plugins.join("JW Tiny32.vst3")).unwrap().machine, pe::X86); } let stop = AtomicBool::new(false); let mut load = |m: &Path| -> scan::Loaded { panic!("nothing here should load, but {} did", m.display()) }; let (cache, stats) = scan::scan(&[plugins.clone()], &scan::Cache::default(), &HashSet::new(), &stop, &mut load, &mut |_| {}); assert_eq!((stats.modules, stats.plugins, stats.loaded), (1, 1, 0)); let got: Vec<(String, String, bool)> = scan::unsupported(&cache).into_iter().map(|u| (u.file, u.why, u.replacement)).collect(); let mut want = vec![ ("JW Old Delay.dll", "VST 2, not supported: use its VST 3 version (VST 3 version installed)", true), ("JW Older (Mono).dll", "VST 2, not supported: use its VST 3 version", false), ]; if x86 { want.insert(0, ("JW Bundle32.vst3", "32-bit, not supported", false)); want.insert(1, ("JW Crusher.dll", "32-bit VST 2, not supported", false)); want.push(("JW Tiny32.vst3", "32-bit, not supported", false)); } let want: Vec<(String, String, bool)> = want.into_iter().map(|(a, b, c)| (a.to_string(), b.to_string(), c)).collect(); assert_eq!(got, want); eprintln!("greyed: {got:#?}"); let _ = std::fs::remove_dir_all(&d);}/// Whether the process is still running.fn alive(pid: u32) -> bool { use windows::Win32::Foundation::{CloseHandle, STILL_ACTIVE}; use windows::Win32::System::Threading::{GetExitCodeProcess, OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION}; unsafe { let Ok(h) = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, pid) else { return false }; let mut code = 0u32; let ok = GetExitCodeProcess(h, &mut code).is_ok(); let _ = CloseHandle(h); ok && code == STILL_ACTIVE.0 as u32 }}/// A module that opens a window as it loads is ended a moment after the window shows (the window rule the app's own/// scans use), not at the time limit, and its window goes; one whose licence checker runs in a process of its own has/// that process ended with it (the scan child's own job object); a module that opens no window loads as ever.fn a_window_ends_the_load_early_and_whatever_the_child_started_goes_with_it() { let _env = ENV.lock().unwrap_or_else(|e| e.into_inner()); let d = folder("window"); std::env::set_var("JW_PLUGINS_LOAD_LOG", d.join("loads.log")); std::env::set_var("JW_PLUGINS_ONLY_UNDER", &d); let pid_file = d.join("helper.pid"); std::env::set_var("JW_TEST_HELPER_PID", &pid_file); let exe = Path::new(HOST); let stop = AtomicBool::new(false); let grace = Duration::from_millis(800); // The window rule: ended at its window, long before the limit. let locked = bundle(&d, "JW Window scan-hang"); let t0 = Instant::now(); let e = scan::load_in_child_with(exe, &locked, Duration::from_secs(20), Some(grace), &stop).unwrap_err(); let took = t0.elapsed(); eprintln!("window rule: ended after {took:?}: {}", e.message); assert_eq!(e.problem, scan::Problem::Window); assert!(e.message.contains(ACTIVATION), "{}", e.message); assert!(took >= grace && took < Duration::from_secs(6), "{took:?}"); std::thread::sleep(Duration::from_millis(300)); assert!(!window_open(ACTIVATION), "the window outlived its scan child"); // A module with no window loads as ever under the rule. let good = bundle(&d, "JW Test"); assert_eq!(scan::load_in_child_with(exe, &good, Duration::from_secs(20), Some(grace), &stop).unwrap().1.len(), 4); // A helper process the plugin started (no window: the time limit ends it) goes with the child. let spawner = bundle(&d, "JW Helper scan-spawn"); let e = scan::load_in_child_with(exe, &spawner, Duration::from_secs(3), Some(grace), &stop).unwrap_err(); assert_eq!(e.problem, scan::Problem::Timeout); let helper: u32 = std::fs::read_to_string(&pid_file).expect("the helper's id").trim().parse().unwrap(); std::thread::sleep(Duration::from_millis(300)); assert!(!alive(helper), "the plugin's helper process {helper} outlived its scan child"); eprintln!("helper process {helper} ended with its scan child"); let loads = loaded(&d.join("loads.log")); assert_eq!(loads.len(), 3, "{loads:?}"); assert!(loads.iter().all(|p| proto::module_allowed(p, &[d.to_string_lossy().to_string()])), "{loads:?}"); std::env::remove_var("JW_TEST_HELPER_PID"); let _ = std::fs::remove_dir_all(&d);}/// A shell: one module whose factory lists hundreds of plugins, as Waves' WaveShell does (718 in WaveShell1-VST3 17.1),/// loaded the way the app loads modules (a scan child in a job of its own, the window rule on). Every class its factory/// describes is a plugin of its own under its own class ID; the one class it can't describe is skipped, not fatal; a/// name with a character outside the BMP comes through whole; and each ID reads as the ASCII it was made from, as Waves'/// IDs do, which is what the studio's .fst reader matches FL Studio's Waves slots by.fn a_shell_lists_every_plugin_in_it_under_its_own_id() { let _env = ENV.lock().unwrap_or_else(|e| e.into_inner()); let d = folder("shell"); let log = d.join("loads.log"); std::env::set_var("JW_PLUGINS_LOAD_LOG", &log); std::env::set_var("JW_PLUGINS_ONLY_UNDER", &d); let plugins = d.join("plugins"); let shell = bundle(&plugins, "JW WaveShell-like shell"); let exe = Path::new(HOST); let stop = AtomicBool::new(false); let grace = Some(Duration::from_secs(2)); let t0 = Instant::now(); let (vendor, classes) = scan::load_in_child_with(exe, &shell, Duration::from_secs(30), grace, &stop).unwrap(); eprintln!("the shell: {} classes in {:?}", classes.len(), t0.elapsed()); assert_eq!(vendor, "juicewrld.io tests"); assert_eq!(classes.len(), 719, "the first class can't be read and is skipped; the other 719 are listed"); // "VST" + a 4-digit code + M or S + "jwshell ", as hex: what the factory made each ID from. let ascii_id = |n: usize| -> String { format!("VST{n:04}{}jwshell ", if n % 2 == 1 { 'S' } else { 'M' }).bytes().map(|b| format!("{b:02X}")).collect() }; for (i, c) in classes.iter().enumerate() { let n = i + 1; assert_eq!(c.cid, ascii_id(n), "class {n}"); assert_eq!((c.category.as_str(), c.version.as_str()), ("Audio Module Class", "17.1.0"), "class {n}"); assert_eq!(c.sub_categories, ["Fx", "Dynamics"], "class {n}"); } assert_eq!(classes[0].name, "JW Shell 0001 Stereo"); assert_eq!(classes[7].name, "JW Shell \u{1F39B} 0008 Mono", "a character outside the BMP, whole"); assert_eq!(classes[718].name, "JW Shell 0719 Stereo"); // Scanned as the app scans: 719 plugins, each its own, effects all. let mut load = |m: &Path| scan::load_in_child_with(exe, m, Duration::from_secs(30), grace, &stop); let (cache, stats) = scan::scan(&[plugins.clone()], &scan::Cache::default(), &HashSet::new(), &stop, &mut load, &mut |_| {}); assert_eq!((stats.modules, stats.loaded, stats.failed, stats.plugins, stats.effects), (1, 1, 0, 719, 719)); let found = scan::plugins(&cache); let ids: HashSet<&str> = found.iter().map(|f| f.plugin.id.as_str()).collect(); assert_eq!((found.len(), ids.len()), (719, 719)); assert!(found.iter().all(|f| f.module == key(&shell) && f.plugin.kind == "effect")); let last = found.iter().find(|f| f.plugin.id == ascii_id(719)).expect("the last plugin by its ID"); assert_eq!(last.plugin.name, "JW Shell 0719 Stereo"); let loads = loaded(&log); assert_eq!(loads.len(), 2, "{loads:?}"); assert!(loads.iter().all(|p| proto::module_allowed(p, &[d.to_string_lossy().to_string()])), "{loads:?}"); let _ = std::fs::remove_dir_all(&d);}fn key(p: &Path) -> String { p.to_string_lossy().to_string()}/// What the owner's Waves and Antares modules looked like to the scan in the app: the process ended with exit code -1/// part way through, no crash, no answer. The scan now says it ended itself, which module called ExitProcess, and the/// step it was at; and a plugin never sees a pipe for a standard handle (one that can't stand that loads now).fn a_module_that_ends_the_process_is_named_with_the_step_it_did_it_at() { let _env = ENV.lock().unwrap_or_else(|e| e.into_inner()); let d = folder("quits"); std::env::set_var("JW_PLUGINS_LOAD_LOG", d.join("loads.log")); std::env::set_var("JW_PLUGINS_ONLY_UNDER", &d); let exe = Path::new(HOST); let stop = AtomicBool::new(false); let grace = Some(Duration::from_secs(2)); let quits = bundle(&d, "JW scan-exit"); let e = scan::load_in_child_with(exe, &quits, Duration::from_secs(20), grace, &stop).unwrap_err(); eprintln!("ended itself: {e:?}"); assert_eq!(e.problem, scan::Problem::Failed); for part in ["ended its own process", "exit code 0xFFFFFFFF", "by JW scan-exit.vst3", "while listing its plugins, at 3 of 4"] { assert!(e.message.contains(part), "{part:?} in {:?}", e.message); } let picky = bundle(&d, "JW scan-stdio"); let (_, classes) = scan::load_in_child_with(exe, &picky, Duration::from_secs(20), grace, &stop).unwrap(); assert_eq!(classes.len(), 4, "a plugin that ends the process at the sight of a pipe loads: it sees none"); // A crash says where it was too. let crashing = bundle(&d, "JW scan-crash"); let e = scan::load_in_child_with(exe, &crashing, Duration::from_secs(20), grace, &stop).unwrap_err(); eprintln!("crashed: {e:?}"); assert_eq!(e.problem, scan::Problem::Crashed); for part in ["crashed as it loaded", "0xC0000005", "in JW scan-crash.vst3", "while its DLL started"] { assert!(e.message.contains(part), "{part:?} in {:?}", e.message); } let _ = std::fs::remove_dir_all(&d);}