libmagic-rs 0.6.0

A pure-Rust implementation of libmagic for file type identification
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright (c) 2025-2026 the libmagic-rs contributors
// SPDX-License-Identifier: Apache-2.0

//! Compatibility tests for libmagic-rs
//!
//! These tests ensure that our implementation produces identical results to the original libmagic.
//! Test files are downloaded from the file/file repository and compared against expected results.

use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

use libmagic_rs::MagicDatabase;
use libmagic_rs::parser::{MagicFileFormat, detect_format};

/// Test result for a single compatibility test
#[derive(Debug, Clone)]
struct TestResult {
    test_file: PathBuf,
    status: TestStatus,
    #[allow(dead_code)]
    expected_output: String,
    #[allow(dead_code)]
    actual_output: String,
    error: Option<String>,
    errors: Vec<String>,
}

#[derive(Debug, Clone, PartialEq)]
enum TestStatus {
    Pass,
    Fail,
    Error,
}

/// Compatibility test runner
struct CompatibilityTestRunner {
    test_dir: PathBuf,
    rmagic_binary: PathBuf,
}

impl CompatibilityTestRunner {
    fn new() -> Result<Self, Box<dyn std::error::Error>> {
        let test_dir = PathBuf::from("third_party/tests");
        let rmagic_binary = find_rmagic_binary()?;

        if !test_dir.exists() {
            return Err(
                "Compatibility test files not found. Ensure third_party/tests directory exists."
                    .into(),
            );
        }

        Ok(Self {
            test_dir,
            rmagic_binary,
        })
    }

    /// Find all test files and their corresponding result files
    fn find_test_files(&self) -> Vec<(PathBuf, PathBuf)> {
        let mut test_files = Vec::new();

        if let Ok(entries) = fs::read_dir(&self.test_dir) {
            for entry in entries.flatten() {
                let path = entry.path();
                if path.extension().and_then(|s| s.to_str()) == Some("testfile") {
                    let result_file = path.with_extension("result");
                    if result_file.exists() {
                        test_files.push((path, result_file));
                    }
                }
            }
        }

        // Sort by input file path to ensure deterministic test execution
        test_files.sort_unstable_by_key(|(input_path, _)| input_path.clone());
        test_files
    }

    /// Run rmagic against a test file
    fn run_rmagic(&self, test_file: &Path) -> Result<String, Box<dyn std::error::Error>> {
        let output = Command::new(&self.rmagic_binary)
            .arg("--use-builtin")
            .arg(test_file)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .output()?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(format!("rmagic failed: {}", stderr).into());
        }

        let full_output = String::from_utf8_lossy(&output.stdout).trim().to_string();

        // Extract just the description part (after the colon)
        // Expected format: "filename: description" - split at first colon only
        // Fallback: return full output if no colon is present
        if let Some((_filename, description)) = full_output.split_once(':') {
            Ok(description.trim().to_string())
        } else {
            Ok(full_output)
        }
    }

    /// Normalize output for comparison
    fn normalize_output(&self, output: &str) -> String {
        output
            .lines()
            .map(|line| line.trim())
            .filter(|line| !line.is_empty())
            .collect::<Vec<_>>()
            .join("\n")
    }

    /// Run a single test with assertion
    fn run_single_test(&self, test_file: PathBuf, result_file: PathBuf) -> TestResult {
        let expected_output = match fs::read_to_string(&result_file) {
            Ok(content) => content.trim().to_string(),
            Err(e) => {
                return TestResult {
                    test_file: test_file.clone(),
                    status: TestStatus::Error,
                    expected_output: String::new(),
                    actual_output: String::new(),
                    error: Some(format!("Failed to read result file: {}", e)),
                    errors: vec![],
                };
            }
        };

        let actual_output = match self.run_rmagic(&test_file) {
            Ok(output) => output,
            Err(e) => {
                return TestResult {
                    test_file: test_file.clone(),
                    status: TestStatus::Error,
                    expected_output,
                    actual_output: String::new(),
                    error: Some(format!("rmagic failed: {}", e)),
                    errors: vec![],
                };
            }
        };

        // Compare normalized outputs and record failures instead of panicking
        let normalized_expected = self.normalize_output(&expected_output);
        let normalized_actual = self.normalize_output(&actual_output);

        let (status, errors) = if normalized_expected == normalized_actual {
            (TestStatus::Pass, vec![])
        } else {
            let error_message = format!(
                "Test failed for {}:\nExpected: {}\nActual: {}",
                test_file.display(),
                expected_output,
                actual_output
            );
            (TestStatus::Fail, vec![error_message])
        };

        TestResult {
            test_file,
            status,
            expected_output,
            actual_output,
            error: None,
            errors,
        }
    }

    /// Run all compatibility tests
    fn run_all_tests(&self) -> Vec<TestResult> {
        let test_files = self.find_test_files();
        let mut results = Vec::new();

        println!("Found {} test files", test_files.len());

        for (test_file, result_file) in test_files {
            let result = self.run_single_test(test_file, result_file);
            results.push(result);
        }

        results
    }

    /// Generate a summary report
    fn generate_report(&self, results: &[TestResult]) -> HashMap<String, usize> {
        let mut summary = HashMap::new();
        summary.insert("total".to_string(), results.len());
        summary.insert("passed".to_string(), 0);
        summary.insert("failed".to_string(), 0);
        summary.insert("errors".to_string(), 0);

        for result in results {
            match result.status {
                TestStatus::Pass => {
                    *summary.get_mut("passed").unwrap() += 1;
                }
                TestStatus::Fail => {
                    *summary.get_mut("failed").unwrap() += 1;
                }
                TestStatus::Error => {
                    *summary.get_mut("errors").unwrap() += 1;
                }
            }
        }

        summary
    }
}

/// Find the rmagic binary
fn find_rmagic_binary() -> Result<PathBuf, Box<dyn std::error::Error>> {
    // Use the cargo_bin! macro when available (works under cargo test, cargo llvm-cov, etc.)
    let cargo_bin_path = assert_cmd::cargo::cargo_bin!("rmagic");
    if cargo_bin_path.exists() {
        return Ok(cargo_bin_path.to_path_buf());
    }

    // Fallback to manual search for release/debug binaries
    let candidates = [
        "target/release/rmagic",
        "target/release/rmagic.exe",
        "target/debug/rmagic",
        "target/debug/rmagic.exe",
    ];

    candidates
        .iter()
        .find(|c| Path::new(c).exists())
        .map(PathBuf::from)
        .ok_or_else(|| "rmagic binary not found. Please build the project first.".into())
}

/// Test that downloads and runs compatibility tests
#[test]
#[ignore] // Ignore by default since it requires downloading test files
fn test_compatibility_with_original_libmagic() {
    let runner = match CompatibilityTestRunner::new() {
        Ok(runner) => runner,
        Err(e) => {
            println!("Skipping compatibility tests: {}", e);
            return;
        }
    };

    let results = runner.run_all_tests();
    let summary = runner.generate_report(&results);

    println!("\n=== COMPATIBILITY TEST SUMMARY ===");
    println!("Total tests: {}", summary["total"]);
    println!("Passed: {}", summary["passed"]);
    println!("Failed: {}", summary["failed"]);
    println!("Errors: {}", summary["errors"]);

    // Print failed tests
    let failed_tests: Vec<_> = results
        .iter()
        .filter(|r| r.status == TestStatus::Fail)
        .collect();

    if !failed_tests.is_empty() {
        println!("\n=== FAILED TESTS ===");
        for result in failed_tests {
            println!("FAIL {}", result.test_file.display());
            for error in &result.errors {
                println!("   {}", error);
            }
            println!();
        }
    }

    // Print error tests
    let error_tests: Vec<_> = results
        .iter()
        .filter(|r| r.status == TestStatus::Error)
        .collect();

    if !error_tests.is_empty() {
        println!("\n=== ERROR TESTS ===");
        for result in error_tests {
            println!("ERROR {}", result.test_file.display());
            if let Some(error) = &result.error {
                println!("   Error: {}", error);
            }
            println!();
        }
    }

    // Assert that we have some tests
    assert!(summary["total"] > 0, "No compatibility tests found");

    // Fail if we have errors (these are different from assertion failures)
    if summary["errors"] > 0 {
        panic!("{} tests had errors", summary["errors"]);
    }

    // Note: Individual test failures are now handled by assertions in run_single_test
    // If we reach here, all tests passed
    println!("\nCompatibility tests completed successfully!");
}

/// Test that verifies we can load the magic database
#[test]
fn test_magic_database_loading() {
    let magic_file = Path::new("third_party/magic.mgc");
    if !magic_file.exists() {
        println!("Skipping magic database test: third_party/magic.mgc not found");
        return;
    }

    match detect_format(magic_file) {
        Ok(MagicFileFormat::Binary) => {
            println!("Skipping magic database test: binary .mgc not supported");
            return;
        }
        Ok(MagicFileFormat::Text | MagicFileFormat::Directory) => {}
        Err(e) => {
            println!("Skipping magic database test: failed to detect format: {e}");
            return;
        }
    }

    let db = MagicDatabase::load_from_file(magic_file);
    assert!(db.is_ok(), "Failed to load magic database");
}

/// Test that verifies rmagic binary exists and works
#[test]
fn test_rmagic_binary() {
    let binary = find_rmagic_binary();
    assert!(binary.is_ok(), "rmagic binary not found");

    let binary_path = binary.unwrap();
    assert!(binary_path.exists(), "rmagic binary does not exist");

    // Test that the binary runs (even if it fails due to missing args)
    let output = Command::new(&binary_path)
        .output()
        .expect("Failed to run rmagic binary");

    // Should fail with usage message, not crash
    assert!(
        !output.status.success(),
        "rmagic should fail with missing arguments"
    );
}

/// Test that verifies test files are available
#[test]
fn test_compatibility_files_available() {
    let test_dir = Path::new("third_party/tests");
    if !test_dir.exists() {
        println!("Skipping compatibility files test: third_party/tests not found");
        return;
    }

    let runner = CompatibilityTestRunner::new().expect("Failed to create test runner");
    let test_files = runner.find_test_files();

    assert!(!test_files.is_empty(), "No compatibility test files found");
    println!("Found {} compatibility test files", test_files.len());
}

/// Partial-match regression for the canonical GNU `file` `searchbug` fixture.
///
/// Loose partial-match sanity check that runs even when consumers strip or
/// alter printf substitution (e.g., rendering helpers that output raw
/// template fragments). Full byte-for-byte parity against
/// `searchbug.result` is verified by `test_searchbug_matches_full_result_string`
/// in `tests/meta_types_integration.rs` -- both the `offset` pseudo-type
/// and printf-style format substitution are fully implemented. This test
/// asserts the recognizable message fragments survive the evaluator's
/// name/use + search dispatch; it's kept as a weaker regression guard
/// for future consumers that might intercept the substitution layer.
#[test]
fn test_searchbug_partial_match() {
    let magic_path = Path::new("third_party/tests/searchbug.magic");
    let testfile_path = Path::new("third_party/tests/searchbug.testfile");
    if !magic_path.exists() || !testfile_path.exists() {
        println!("Skipping searchbug partial-match test: fixtures not found");
        return;
    }

    let db =
        MagicDatabase::load_from_file(magic_path).expect("searchbug.magic must load end-to-end");
    let bytes = fs::read(testfile_path).expect("searchbug.testfile fixture must be readable");

    let result = db
        .evaluate_buffer(&bytes)
        .expect("evaluate_buffer on searchbug.testfile");

    assert!(
        result.description.starts_with("Testfmt"),
        "description should start with \"Testfmt\", got: {}",
        result.description
    );
    assert!(
        result.description.contains("found_ABC"),
        "description should contain \"found_ABC\" (subroutine match), got: {}",
        result.description
    );
    assert!(
        result.description.contains("followed_by"),
        "description should contain \"followed_by\" (subroutine child rule), got: {}",
        result.description
    );
}