zzz-arc 0.4.2

A fast compression multitool supporting zst, tgz, txz, zip, and 7z formats with optional encryption
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//! Error handling and edge case tests for zzz

use std::fs;
use std::path::Path;
use tempfile::TempDir;
use zzz_arc::filter::FileFilter;
use zzz_arc::formats::zstd::ZstdFormat;
use zzz_arc::formats::{CompressionFormat, CompressionOptions, ExtractionOptions};

type Result<T> = anyhow::Result<T>;

#[test]
fn test_compress_nonexistent_file() {
    let temp_dir = TempDir::new().unwrap();
    let nonexistent = temp_dir.path().join("does_not_exist.txt");
    let output = temp_dir.path().join("output.zst");

    let options = CompressionOptions::default();
    let filter = FileFilter::new(true, &[]).unwrap();

    let result = ZstdFormat::compress(&nonexistent, &output, &options, &filter, None);
    assert!(result.is_err());

    let error_msg = result.unwrap_err().to_string();
    assert!(error_msg.contains("does_not_exist.txt") || error_msg.contains("No such file"));
}

#[test]
fn test_extract_nonexistent_archive() {
    let temp_dir = TempDir::new().unwrap();
    let nonexistent_archive = temp_dir.path().join("does_not_exist.zst");
    let extract_dir = temp_dir.path().join("extract");

    fs::create_dir(&extract_dir).unwrap();
    let options = ExtractionOptions::default();

    let result = ZstdFormat::extract(&nonexistent_archive, &extract_dir, &options, None);
    assert!(result.is_err());

    let error_msg = result.unwrap_err().to_string();
    assert!(error_msg.contains("failed to open archive file"));
}

#[test]
fn test_list_nonexistent_archive() {
    let temp_dir = TempDir::new().unwrap();
    let nonexistent_archive = temp_dir.path().join("does_not_exist.zst");

    let result = ZstdFormat::list(&nonexistent_archive);
    assert!(result.is_err());

    let error_msg = result.unwrap_err().to_string();
    assert!(error_msg.contains("failed to open archive file"));
}

#[test]
fn test_extract_corrupted_archive() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let corrupted_archive = temp_dir.path().join("corrupted.zst");
    let extract_dir = temp_dir.path().join("extract");

    // Create a corrupted "archive" (just random bytes)
    fs::write(&corrupted_archive, b"This is not a valid zstd archive")?;
    fs::create_dir(&extract_dir)?;

    let options = ExtractionOptions::default();
    let result = ZstdFormat::extract(&corrupted_archive, &extract_dir, &options, None);

    assert!(result.is_err());
    let error_msg = result.unwrap_err().to_string();
    // The exact error message may vary, but it should indicate a zstd/decoding problem
    assert!(
        error_msg.contains("zstd")
            || error_msg.contains("decoder")
            || error_msg.contains("invalid")
            || error_msg.contains("frame descriptor"),
        "Error message: {error_msg}"
    );

    Ok(())
}

#[test]
fn test_list_corrupted_archive() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let corrupted_archive = temp_dir.path().join("corrupted.zst");

    // Create corrupted archive
    fs::write(&corrupted_archive, b"Not a zstd file")?;

    let result = ZstdFormat::list(&corrupted_archive);
    assert!(result.is_err());

    Ok(())
}

#[test]
fn test_compress_invalid_output_directory() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let source_file = temp_dir.path().join("test.txt");
    let invalid_output = temp_dir.path().join("nonexistent_dir").join("output.zst");

    fs::write(&source_file, "test content")?;

    let options = CompressionOptions::default();
    let filter = FileFilter::new(true, &[])?;

    let result = ZstdFormat::compress(&source_file, &invalid_output, &options, &filter, None);
    assert!(result.is_err());

    let error_msg = result.unwrap_err().to_string();
    assert!(error_msg.contains("failed to create output file"));

    Ok(())
}

#[test]
fn test_extract_to_readonly_directory() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let source_file = temp_dir.path().join("test.txt");
    let archive_path = temp_dir.path().join("test.zst");
    let readonly_dir = temp_dir.path().join("readonly");

    // Create and compress file
    fs::write(&source_file, "test content")?;
    let options = CompressionOptions::default();
    let filter = FileFilter::new(true, &[])?;
    ZstdFormat::compress(&source_file, &archive_path, &options, &filter, None)?;

    // Create readonly directory (platform-dependent behavior)
    fs::create_dir(&readonly_dir)?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(&readonly_dir)?.permissions();
        perms.set_mode(0o444); // Read-only
        fs::set_permissions(&readonly_dir, perms)?;
    }

    let extract_options = ExtractionOptions::default();
    let result = ZstdFormat::extract(&archive_path, &readonly_dir, &extract_options, None);

    // Restore permissions for cleanup
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(&readonly_dir)?.permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&readonly_dir, perms)?;
    }

    // On Unix systems, this should fail due to permissions
    #[cfg(unix)]
    assert!(result.is_err());

    // On other systems, behavior may vary
    #[cfg(not(unix))]
    {
        // Don't assert on Windows as behavior is different
        let _ = result;
    }

    Ok(())
}

#[test]
fn test_overwrite_protection() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let source_file = temp_dir.path().join("test.txt");
    let archive_path = temp_dir.path().join("test.zst");
    let extract_dir = temp_dir.path().join("extract");
    let target_file = extract_dir.join("test.txt");

    // Create and compress file
    fs::write(&source_file, "original")?;
    let options = CompressionOptions::default();
    let filter = FileFilter::new(true, &[])?;
    ZstdFormat::compress(&source_file, &archive_path, &options, &filter, None)?;

    // Extract once
    fs::create_dir(&extract_dir)?;
    let extract_options = ExtractionOptions {
        overwrite: false,
        ..Default::default()
    };
    ZstdFormat::extract(&archive_path, &extract_dir, &extract_options, None)?;

    // Modify the extracted file
    fs::write(&target_file, "modified")?;

    // Try to extract again without overwrite flag - should fail
    let result = ZstdFormat::extract(&archive_path, &extract_dir, &extract_options, None);
    assert!(result.is_err());

    let error_msg = result.unwrap_err().to_string();
    assert!(error_msg.contains("already exists"));
    assert!(error_msg.contains("overwrite"));

    // File should still contain modified content
    assert_eq!(fs::read_to_string(&target_file)?, "modified");

    Ok(())
}

#[test]
fn test_directory_traversal_protection() -> Result<()> {
    // This test would require creating a malicious archive with "../" paths
    // For now, we'll test that our extraction logic would catch such cases

    // Note: This is a simplified test. In practice, you'd need to create
    // an actual malicious tar archive to fully test this protection.

    let temp_dir = TempDir::new()?;
    let extract_dir = temp_dir.path().join("safe_extract");
    fs::create_dir(&extract_dir)?;

    // Test that our path validation would catch dangerous paths
    let dangerous_paths = vec![
        "../etc/passwd",
        "../../sensitive_file",
        "/etc/passwd",
        "subdir/../../../outside",
    ];

    for dangerous_path in dangerous_paths {
        let path = Path::new(dangerous_path);

        // Check if path contains parent directory components
        let has_parent_dir = path
            .components()
            .any(|comp| matches!(comp, std::path::Component::ParentDir));

        if has_parent_dir {
            // Our extraction code should reject such paths
            // Our extraction code should reject such paths
            println!("Path {dangerous_path} contains dangerous parent directory references");
        }
    }

    Ok(())
}

#[test]
fn test_extract_rejects_unsafe_paths() -> Result<()> {
    use std::io::Write;

    let temp_dir = TempDir::new()?;
    let archive_path = temp_dir.path().join("unsafe.zip");
    let extract_dir = temp_dir.path().join("extract");
    fs::create_dir(&extract_dir)?;

    let output_file = fs::File::create(&archive_path)?;
    let mut zip = zip::ZipWriter::new(output_file);
    zip.start_file("../evil.txt", zip::write::FileOptions::<()>::default())?;
    zip.write_all(b"unsafe content")?;
    zip.finish()?;

    let options = ExtractionOptions::default();
    let result =
        zzz_arc::formats::zip::ZipFormat::extract(&archive_path, &extract_dir, &options, None);

    assert!(result.is_err());
    let error_msg = result.unwrap_err().to_string();
    assert!(error_msg.contains("unsafe archive path"));

    Ok(())
}

#[test]
fn test_zip_integrity_detects_corruption() -> Result<()> {
    use std::io::Write;
    use zip::write::FileOptions;

    let temp_dir = TempDir::new()?;
    let archive_path = temp_dir.path().join("corrupt.zip");

    let file = fs::File::create(&archive_path)?;
    let mut zip = zip::ZipWriter::new(file);
    zip.start_file("file.txt", FileOptions::<()>::default())?;
    zip.write_all(b"zip content")?;
    zip.finish()?;

    let mut data = fs::read(&archive_path)?;
    if let Some(last) = data.last_mut() {
        *last ^= 0xFF;
    }
    fs::write(&archive_path, data)?;

    let result = zzz_arc::formats::zip::ZipFormat::test_integrity(&archive_path);
    assert!(result.is_err());

    Ok(())
}

#[cfg(unix)]
#[test]
fn test_extract_rejects_symlink_ancestor() -> Result<()> {
    use std::io::Write;
    use std::os::unix::fs::symlink;
    use zip::write::FileOptions;

    let temp_dir = TempDir::new()?;
    let archive_path = temp_dir.path().join("symlink.zip");
    let extract_dir = temp_dir.path().join("extract");
    let outside_dir = temp_dir.path().join("outside");

    fs::create_dir(&extract_dir)?;
    fs::create_dir(&outside_dir)?;
    symlink(&outside_dir, extract_dir.join("link"))?;

    let file = fs::File::create(&archive_path)?;
    let mut zip = zip::ZipWriter::new(file);
    zip.start_file("link/evil.txt", FileOptions::<()>::default())?;
    zip.write_all(b"evil")?;
    zip.finish()?;

    let options = ExtractionOptions::default();
    let result =
        zzz_arc::formats::zip::ZipFormat::extract(&archive_path, &extract_dir, &options, None);

    assert!(result.is_err());
    let error_msg = result.unwrap_err().to_string();
    assert!(error_msg.contains("symlink ancestor"));

    Ok(())
}

#[cfg(unix)]
#[test]
fn test_compress_rejects_symlink() -> Result<()> {
    use std::os::unix::fs::symlink;

    let temp_dir = TempDir::new()?;
    let source_dir = temp_dir.path().join("source");
    let archive_path = temp_dir.path().join("archive.zst");

    fs::create_dir(&source_dir)?;
    fs::write(source_dir.join("file.txt"), "content")?;
    symlink(source_dir.join("file.txt"), source_dir.join("link.txt"))?;

    let options = CompressionOptions::default();
    let filter = FileFilter::new(true, &[])?;
    let result = ZstdFormat::compress(&source_dir, &archive_path, &options, &filter, None);

    assert!(result.is_err());
    let error_msg = result.unwrap_err().to_string();
    assert!(error_msg.contains("symlink"));

    Ok(())
}

#[cfg(unix)]
#[test]
fn test_compress_follow_symlink() -> Result<()> {
    use std::os::unix::fs::symlink;

    let temp_dir = TempDir::new()?;
    let source_dir = temp_dir.path().join("source");
    let archive_path = temp_dir.path().join("archive.zst");
    let extract_dir = temp_dir.path().join("extract");

    fs::create_dir(&source_dir)?;
    let target = source_dir.join("target.txt");
    fs::write(&target, "symlink content")?;
    symlink(&target, source_dir.join("link.txt"))?;

    let options = CompressionOptions {
        follow_symlinks: true,
        ..Default::default()
    };
    let filter = FileFilter::new(true, &[])?;
    let stats = ZstdFormat::compress(&source_dir, &archive_path, &options, &filter, None)?;
    assert!(stats.output_size > 0);

    fs::create_dir(&extract_dir)?;
    let extract_options = ExtractionOptions::default();
    ZstdFormat::extract(&archive_path, &extract_dir, &extract_options, None)?;

    let linked_path = extract_dir.join("source/link.txt");
    assert!(linked_path.exists());
    assert_eq!(fs::read_to_string(&linked_path)?, "symlink content");

    Ok(())
}

#[cfg(unix)]
#[test]
fn test_compress_follow_symlink_escape_rejected() -> Result<()> {
    use std::os::unix::fs::symlink;

    let temp_dir = TempDir::new()?;
    let source_dir = temp_dir.path().join("source");
    let outside_dir = temp_dir.path().join("outside");
    let archive_path = temp_dir.path().join("archive.zst");

    fs::create_dir(&source_dir)?;
    fs::create_dir(&outside_dir)?;
    let outside_file = outside_dir.join("outside.txt");
    fs::write(&outside_file, "outside content")?;
    symlink(&outside_file, source_dir.join("link.txt"))?;

    let options = CompressionOptions {
        follow_symlinks: true,
        ..Default::default()
    };
    let filter = FileFilter::new(true, &[])?;
    let result = ZstdFormat::compress(&source_dir, &archive_path, &options, &filter, None);

    assert!(result.is_err());
    let error_msg = result.unwrap_err().to_string();
    assert!(error_msg.contains("escapes input root"));

    Ok(())
}

#[cfg(unix)]
#[test]
fn test_compress_follow_symlink_escape_allowed() -> Result<()> {
    use std::os::unix::fs::symlink;

    let temp_dir = TempDir::new()?;
    let source_dir = temp_dir.path().join("source");
    let outside_dir = temp_dir.path().join("outside");
    let archive_path = temp_dir.path().join("archive.zst");
    let extract_dir = temp_dir.path().join("extract");

    fs::create_dir(&source_dir)?;
    fs::create_dir(&outside_dir)?;
    let outside_file = outside_dir.join("outside.txt");
    fs::write(&outside_file, "outside content")?;
    symlink(&outside_file, source_dir.join("link.txt"))?;

    let options = CompressionOptions {
        follow_symlinks: true,
        allow_symlink_escape: true,
        ..Default::default()
    };
    let filter = FileFilter::new(true, &[])?;
    ZstdFormat::compress(&source_dir, &archive_path, &options, &filter, None)?;

    fs::create_dir(&extract_dir)?;
    let extract_options = ExtractionOptions::default();
    ZstdFormat::extract(&archive_path, &extract_dir, &extract_options, None)?;

    let linked_path = extract_dir.join("source/link.txt");
    assert!(linked_path.exists());
    assert_eq!(fs::read_to_string(&linked_path)?, "outside content");

    Ok(())
}

#[test]
fn test_empty_directory_handling() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let empty_dir = temp_dir.path().join("empty");
    let archive_path = temp_dir.path().join("empty.zst");
    let extract_dir = temp_dir.path().join("extract");

    // Create empty directory
    fs::create_dir(&empty_dir)?;

    // Compress empty directory
    let options = CompressionOptions::default();
    let filter = FileFilter::new(true, &[])?;
    let stats = ZstdFormat::compress(&empty_dir, &archive_path, &options, &filter, None)?;

    // Should succeed and create archive
    assert!(archive_path.exists());
    assert_eq!(stats.input_size, 0); // No files to compress

    // Extract and verify
    fs::create_dir(&extract_dir)?;
    let extract_options = ExtractionOptions::default();
    ZstdFormat::extract(&archive_path, &extract_dir, &extract_options, None)?;

    // Empty directory should be recreated
    assert!(extract_dir.join("empty").exists());
    assert!(extract_dir.join("empty").is_dir());

    Ok(())
}

#[test]
fn test_very_long_filename() -> Result<()> {
    let temp_dir = TempDir::new()?;

    // Create file with very long name (close to filesystem limits)
    let long_name = "a".repeat(200); // 200 character filename
    let long_file = temp_dir.path().join(&long_name);
    let archive_path = temp_dir.path().join("long.zst");

    fs::write(&long_file, "content")?;

    // Should handle long filenames gracefully
    let options = CompressionOptions::default();
    let filter = FileFilter::new(true, &[])?;
    let result = ZstdFormat::compress(&long_file, &archive_path, &options, &filter, None);

    // Should either succeed or fail gracefully
    match result {
        Ok(_) => {
            // If compression succeeded, extraction should also work
            let extract_dir = temp_dir.path().join("extract");
            fs::create_dir(&extract_dir)?;
            let extract_options = ExtractionOptions::default();
            ZstdFormat::extract(&archive_path, &extract_dir, &extract_options, None)?;

            let extracted_file = extract_dir.join(&long_name);
            assert!(extracted_file.exists());
            assert_eq!(fs::read_to_string(extracted_file)?, "content");
        }
        Err(_) => {
            // If it fails, that's also acceptable for extremely long names
            println!("Long filename handling failed gracefully");
        }
    }

    Ok(())
}

#[test]
fn test_invalid_glob_pattern_in_filter() {
    // Test that FileFilter handles invalid glob patterns gracefully
    let invalid_patterns = vec![
        "[".to_string(),     // Unclosed bracket
        "[z-a]".to_string(), // Invalid range (may or may not fail depending on glob implementation)
    ];

    for pattern in invalid_patterns {
        let result = FileFilter::new(true, std::slice::from_ref(&pattern));
        // Some patterns may be accepted by the glob crate, so we just ensure
        // the function doesn't panic and handles them gracefully
        match result {
            Ok(_) => println!("Pattern '{pattern}' was accepted"),
            Err(_) => println!("Pattern '{pattern}' was rejected"),
        }
    }
}

#[test]
fn test_zero_byte_file() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let empty_file = temp_dir.path().join("empty.txt");
    let archive_path = temp_dir.path().join("empty.zst");
    let extract_dir = temp_dir.path().join("extract");

    // Create zero-byte file
    fs::write(&empty_file, b"")?;

    // Compress
    let options = CompressionOptions::default();
    let filter = FileFilter::new(true, &[])?;
    let stats = ZstdFormat::compress(&empty_file, &archive_path, &options, &filter, None)?;

    assert!(archive_path.exists());
    assert_eq!(stats.input_size, 0);

    // Extract
    fs::create_dir(&extract_dir)?;
    let extract_options = ExtractionOptions::default();
    ZstdFormat::extract(&archive_path, &extract_dir, &extract_options, None)?;

    // Verify empty file was extracted
    let extracted_file = extract_dir.join("empty.txt");
    assert!(extracted_file.exists());
    assert_eq!(fs::read(extracted_file)?, b"");

    Ok(())
}