|
| 1 | +use serde_json::json; |
| 2 | +use std::fs; |
| 3 | +use std::process::Command; |
| 4 | +use tempfile::tempdir; |
| 5 | + |
| 6 | +fn run_scan(root: &std::path::Path, rules: Option<&std::path::Path>) -> std::process::Output { |
| 7 | + let mut command = Command::new(env!("CARGO_BIN_EXE_diskern")); |
| 8 | + command.arg("scan").arg(root).arg("--top").arg("0"); |
| 9 | + if let Some(rules) = rules { |
| 10 | + command.arg("--rules").arg(rules); |
| 11 | + } |
| 12 | + command.output().expect("diskern should start") |
| 13 | +} |
| 14 | + |
| 15 | +fn write_rules(path: &std::path::Path, pattern: &str) { |
| 16 | + let rules = json!({ |
| 17 | + "version": 1, |
| 18 | + "rules": [{ |
| 19 | + "id": "test-rule", |
| 20 | + "patterns": [pattern], |
| 21 | + "category": "browser_cache", |
| 22 | + "verdict": "safe", |
| 23 | + "description": "Rule loaded from the test file." |
| 24 | + }] |
| 25 | + }); |
| 26 | + fs::write(path, serde_json::to_vec(&rules).unwrap()).unwrap(); |
| 27 | +} |
| 28 | + |
| 29 | +#[test] |
| 30 | +fn scan_without_rules_uses_embedded_database() { |
| 31 | + let root = tempdir().unwrap(); |
| 32 | + let cache = root.path().join(".cache/google-chrome/Default/Cache"); |
| 33 | + fs::create_dir_all(&cache).unwrap(); |
| 34 | + fs::write(cache.join("entry"), b"cached").unwrap(); |
| 35 | + |
| 36 | + let output = run_scan(root.path(), None); |
| 37 | + assert!( |
| 38 | + output.status.success(), |
| 39 | + "{}", |
| 40 | + String::from_utf8_lossy(&output.stderr) |
| 41 | + ); |
| 42 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 43 | + assert!(stdout.contains("matched rule chrome-cache"), "{stdout}"); |
| 44 | +} |
| 45 | + |
| 46 | +#[test] |
| 47 | +fn scan_accepts_and_uses_external_rules_file() { |
| 48 | + let root = tempdir().unwrap(); |
| 49 | + fs::write(root.path().join("sample.custom"), b"custom").unwrap(); |
| 50 | + let rules = root.path().join("rules.json"); |
| 51 | + write_rules(&rules, "**/*.custom"); |
| 52 | + |
| 53 | + let output = run_scan(root.path(), Some(&rules)); |
| 54 | + assert!( |
| 55 | + output.status.success(), |
| 56 | + "{}", |
| 57 | + String::from_utf8_lossy(&output.stderr) |
| 58 | + ); |
| 59 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 60 | + assert!(stdout.contains("Rules: external database"), "{stdout}"); |
| 61 | + assert!(stdout.contains("matched rule test-rule"), "{stdout}"); |
| 62 | + assert!( |
| 63 | + stdout.contains("Rule loaded from the test file."), |
| 64 | + "{stdout}" |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +#[test] |
| 69 | +fn missing_rules_file_fails_clearly() { |
| 70 | + let root = tempdir().unwrap(); |
| 71 | + let missing = root.path().join("missing.json"); |
| 72 | + let output = run_scan(root.path(), Some(&missing)); |
| 73 | + |
| 74 | + assert!(!output.status.success()); |
| 75 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 76 | + assert!(stderr.contains("could not read rules file"), "{stderr}"); |
| 77 | + assert!(stderr.contains("missing.json"), "{stderr}"); |
| 78 | +} |
| 79 | + |
| 80 | +#[test] |
| 81 | +fn malformed_rules_file_fails_clearly() { |
| 82 | + let root = tempdir().unwrap(); |
| 83 | + let rules = root.path().join("malformed.json"); |
| 84 | + fs::write(&rules, b"{ not json }").unwrap(); |
| 85 | + let output = run_scan(root.path(), Some(&rules)); |
| 86 | + |
| 87 | + assert!(!output.status.success()); |
| 88 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 89 | + assert!(stderr.contains("could not parse rules file"), "{stderr}"); |
| 90 | + assert!(stderr.contains("malformed.json"), "{stderr}"); |
| 91 | +} |
| 92 | + |
| 93 | +#[test] |
| 94 | +fn invalid_glob_in_rules_file_fails_before_scan() { |
| 95 | + let root = tempdir().unwrap(); |
| 96 | + let rules = root.path().join("invalid-glob.json"); |
| 97 | + write_rules(&rules, "["); |
| 98 | + |
| 99 | + let output = run_scan(root.path(), Some(&rules)); |
| 100 | + |
| 101 | + assert!(!output.status.success()); |
| 102 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 103 | + assert!(stderr.contains("could not validate rules file"), "{stderr}"); |
| 104 | + assert!(stderr.contains("invalid glob pattern"), "{stderr}"); |
| 105 | + assert!(stderr.contains("test-rule"), "{stderr}"); |
| 106 | +} |
| 107 | + |
| 108 | +#[test] |
| 109 | +fn external_rules_cannot_shadow_embedded_protected_rules() { |
| 110 | + let root = tempdir().unwrap(); |
| 111 | + let protected_path = root.path().join("windows/installer/setup.msi"); |
| 112 | + fs::create_dir_all(protected_path.parent().unwrap()).unwrap(); |
| 113 | + fs::write(&protected_path, b"installer").unwrap(); |
| 114 | + let rules = root.path().join("rules.json"); |
| 115 | + write_rules(&rules, "**/*.msi"); |
| 116 | + |
| 117 | + let output = run_scan(root.path(), Some(&rules)); |
| 118 | + |
| 119 | + assert!( |
| 120 | + output.status.success(), |
| 121 | + "{}", |
| 122 | + String::from_utf8_lossy(&output.stderr) |
| 123 | + ); |
| 124 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 125 | + assert!( |
| 126 | + stdout.contains("matched rule windows-installer-cache"), |
| 127 | + "{stdout}" |
| 128 | + ); |
| 129 | + assert!(!stdout.contains("matched rule test-rule"), "{stdout}"); |
| 130 | + assert!(stdout.contains("Protected — do not touch"), "{stdout}"); |
| 131 | +} |
0 commit comments