zesven 1.1.0

A pure Rust implementation of the 7z archive format
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Integration tests for archive editing operations.
//!
//! These tests verify that the editing API correctly:
//! - Removes entries from archives
//! - Renames entries within archives
//! - Updates entry content
//! - Adds new entries to existing archives
//! - Returns correct error types for invalid operations

#![cfg(feature = "lzma2")]

mod common;

use std::io::Cursor;
use zesven::edit::ArchiveEditor;
use zesven::read::Archive;
use zesven::{ArchivePath, Error};

use common::create_archive;

/// Reads an archive and returns a map of path -> content.
fn read_archive_contents(archive_bytes: &[u8]) -> zesven::Result<Vec<(String, Vec<u8>)>> {
    let mut archive = Archive::open(Cursor::new(archive_bytes))?;
    let mut contents = Vec::new();

    // Collect entry info first to avoid borrow issues
    let entry_infos: Vec<_> = archive
        .entries()
        .iter()
        .filter(|e| !e.is_directory)
        .map(|e| e.path.as_str().to_string())
        .collect();

    for path in entry_infos {
        let data = archive.extract_to_vec(&path)?;
        contents.push((path, data));
    }

    Ok(contents)
}

// ============================================================================
// Delete operation tests
// ============================================================================

#[test]
fn test_editor_delete_entry() {
    let entries = [
        ("keep.txt", b"Keep this" as &[u8]),
        ("delete.txt", b"Delete this"),
        ("also_keep.txt", b"Also keep"),
    ];

    let archive_bytes = create_archive(&entries).unwrap();
    let archive = Archive::open(Cursor::new(archive_bytes)).unwrap();

    let mut editor = ArchiveEditor::new(archive);
    editor.delete("delete.txt").unwrap();

    assert_eq!(editor.pending_operations(), 1);

    let mut output = Cursor::new(Vec::new());
    let result = editor.apply(&mut output).unwrap();

    assert_eq!(result.entries_kept, 2);
    assert_eq!(result.entries_deleted, 1);
    assert_eq!(result.total_entries(), 2);

    // Verify the archive contents
    let contents = read_archive_contents(output.get_ref()).unwrap();
    assert_eq!(contents.len(), 2);

    let paths: Vec<_> = contents.iter().map(|(p, _)| p.as_str()).collect();
    assert!(paths.contains(&"keep.txt"));
    assert!(paths.contains(&"also_keep.txt"));
    assert!(!paths.contains(&"delete.txt"));
}

#[test]
fn test_editor_delete_nonexistent_returns_error() {
    let entries = [("existing.txt", b"content" as &[u8])];

    let archive_bytes = create_archive(&entries).unwrap();
    let archive = Archive::open(Cursor::new(archive_bytes)).unwrap();

    let mut editor = ArchiveEditor::new(archive);
    let result = editor.delete("nonexistent.txt");

    assert!(result.is_err());
    match result.unwrap_err() {
        Error::EntryNotFound { path } => {
            assert_eq!(path, "nonexistent.txt");
        }
        e => panic!("Expected EntryNotFound, got: {:?}", e),
    }
}

// ============================================================================
// Rename operation tests
// ============================================================================

#[test]
fn test_editor_rename_entry() {
    let entries = [
        ("old_name.txt", b"Content to rename" as &[u8]),
        ("other.txt", b"Other content"),
    ];

    let archive_bytes = create_archive(&entries).unwrap();
    let archive = Archive::open(Cursor::new(archive_bytes)).unwrap();

    let mut editor = ArchiveEditor::new(archive);
    editor.rename("old_name.txt", "new_name.txt").unwrap();

    let mut output = Cursor::new(Vec::new());
    let result = editor.apply(&mut output).unwrap();

    assert_eq!(result.entries_renamed, 1);
    assert_eq!(result.entries_kept, 1);
    assert_eq!(result.total_entries(), 2);

    // Verify the archive contents
    let contents = read_archive_contents(output.get_ref()).unwrap();
    let paths: Vec<_> = contents.iter().map(|(p, _)| p.as_str()).collect();

    assert!(paths.contains(&"new_name.txt"));
    assert!(paths.contains(&"other.txt"));
    assert!(!paths.contains(&"old_name.txt"));

    // Verify content is preserved
    let renamed_content = contents.iter().find(|(p, _)| p == "new_name.txt").unwrap();
    assert_eq!(renamed_content.1, b"Content to rename");
}

#[test]
fn test_editor_rename_nonexistent_returns_error() {
    let entries = [("existing.txt", b"content" as &[u8])];

    let archive_bytes = create_archive(&entries).unwrap();
    let archive = Archive::open(Cursor::new(archive_bytes)).unwrap();

    let mut editor = ArchiveEditor::new(archive);
    let result = editor.rename("nonexistent.txt", "new.txt");

    assert!(result.is_err());
    match result.unwrap_err() {
        Error::EntryNotFound { path } => {
            assert_eq!(path, "nonexistent.txt");
        }
        e => panic!("Expected EntryNotFound, got: {:?}", e),
    }
}

#[test]
fn test_editor_rename_to_existing_returns_error() {
    let entries = [
        ("source.txt", b"Source content" as &[u8]),
        ("target.txt", b"Target content"),
    ];

    let archive_bytes = create_archive(&entries).unwrap();
    let archive = Archive::open(Cursor::new(archive_bytes)).unwrap();

    let mut editor = ArchiveEditor::new(archive);
    let result = editor.rename("source.txt", "target.txt");

    assert!(result.is_err());
    match result.unwrap_err() {
        Error::EntryExists { path } => {
            assert_eq!(path, "target.txt");
        }
        e => panic!("Expected EntryExists, got: {:?}", e),
    }
}

// ============================================================================
// Update operation tests
// ============================================================================

#[test]
fn test_editor_update_entry() {
    let entries = [
        ("update_me.txt", b"Original content" as &[u8]),
        ("untouched.txt", b"Untouched content"),
    ];

    let archive_bytes = create_archive(&entries).unwrap();
    let archive = Archive::open(Cursor::new(archive_bytes)).unwrap();

    let mut editor = ArchiveEditor::new(archive);
    editor
        .update("update_me.txt", b"New content here!".to_vec())
        .unwrap();

    let mut output = Cursor::new(Vec::new());
    let result = editor.apply(&mut output).unwrap();

    assert_eq!(result.entries_updated, 1);
    assert_eq!(result.entries_kept, 1);
    assert_eq!(result.total_entries(), 2);

    // Verify the archive contents
    let contents = read_archive_contents(output.get_ref()).unwrap();

    let updated = contents.iter().find(|(p, _)| p == "update_me.txt").unwrap();
    assert_eq!(updated.1, b"New content here!");

    let untouched = contents.iter().find(|(p, _)| p == "untouched.txt").unwrap();
    assert_eq!(untouched.1, b"Untouched content");
}

#[test]
fn test_editor_update_nonexistent_returns_error() {
    let entries = [("existing.txt", b"content" as &[u8])];

    let archive_bytes = create_archive(&entries).unwrap();
    let archive = Archive::open(Cursor::new(archive_bytes)).unwrap();

    let mut editor = ArchiveEditor::new(archive);
    let result = editor.update("nonexistent.txt", b"new data".to_vec());

    assert!(result.is_err());
    match result.unwrap_err() {
        Error::EntryNotFound { path } => {
            assert_eq!(path, "nonexistent.txt");
        }
        e => panic!("Expected EntryNotFound, got: {:?}", e),
    }
}

// ============================================================================
// Add operation tests
// ============================================================================

#[test]
fn test_editor_add_entry() {
    let entries = [("existing.txt", b"Existing content" as &[u8])];

    let archive_bytes = create_archive(&entries).unwrap();
    let archive = Archive::open(Cursor::new(archive_bytes)).unwrap();

    let mut editor = ArchiveEditor::new(archive);
    let new_path = ArchivePath::new("new_file.txt").unwrap();
    editor.add(new_path, b"Brand new content".to_vec()).unwrap();

    let mut output = Cursor::new(Vec::new());
    let result = editor.apply(&mut output).unwrap();

    assert_eq!(result.entries_kept, 1);
    assert_eq!(result.entries_added, 1);
    assert_eq!(result.total_entries(), 2);

    // Verify the archive contents
    let contents = read_archive_contents(output.get_ref()).unwrap();
    assert_eq!(contents.len(), 2);

    let new_content = contents.iter().find(|(p, _)| p == "new_file.txt").unwrap();
    assert_eq!(new_content.1, b"Brand new content");
}

#[test]
fn test_editor_add_duplicate_returns_error() {
    let entries = [("existing.txt", b"content" as &[u8])];

    let archive_bytes = create_archive(&entries).unwrap();
    let archive = Archive::open(Cursor::new(archive_bytes)).unwrap();

    let mut editor = ArchiveEditor::new(archive);
    let path = ArchivePath::new("existing.txt").unwrap();
    let result = editor.add(path, b"duplicate".to_vec());

    assert!(result.is_err());
    match result.unwrap_err() {
        Error::EntryExists { path } => {
            assert_eq!(path, "existing.txt");
        }
        e => panic!("Expected EntryExists, got: {:?}", e),
    }
}

// ============================================================================
// Combined operations tests
// ============================================================================

#[test]
fn test_editor_multiple_operations() {
    let entries = [
        ("keep.txt", b"Keep this" as &[u8]),
        ("delete.txt", b"Delete this"),
        ("rename.txt", b"Rename this"),
        ("update.txt", b"Update this"),
    ];

    let archive_bytes = create_archive(&entries).unwrap();
    let archive = Archive::open(Cursor::new(archive_bytes)).unwrap();

    let mut editor = ArchiveEditor::new(archive);

    editor.delete("delete.txt").unwrap();
    editor.rename("rename.txt", "renamed.txt").unwrap();
    editor
        .update("update.txt", b"Updated content".to_vec())
        .unwrap();
    let new_path = ArchivePath::new("added.txt").unwrap();
    editor.add(new_path, b"Added content".to_vec()).unwrap();

    assert_eq!(editor.pending_operations(), 4);

    let mut output = Cursor::new(Vec::new());
    let result = editor.apply(&mut output).unwrap();

    assert_eq!(result.entries_kept, 1);
    assert_eq!(result.entries_deleted, 1);
    assert_eq!(result.entries_renamed, 1);
    assert_eq!(result.entries_updated, 1);
    assert_eq!(result.entries_added, 1);
    assert_eq!(result.total_entries(), 4);

    // Verify final archive state
    let contents = read_archive_contents(output.get_ref()).unwrap();
    let paths: Vec<_> = contents.iter().map(|(p, _)| p.as_str()).collect();

    assert!(paths.contains(&"keep.txt"));
    assert!(paths.contains(&"renamed.txt"));
    assert!(paths.contains(&"update.txt"));
    assert!(paths.contains(&"added.txt"));
    assert!(!paths.contains(&"delete.txt"));
    assert!(!paths.contains(&"rename.txt"));
}

// ============================================================================
// Clear operations test
// ============================================================================

#[test]
fn test_editor_clear_operations() {
    let entries = [("file.txt", b"content" as &[u8])];

    let archive_bytes = create_archive(&entries).unwrap();
    let archive = Archive::open(Cursor::new(archive_bytes)).unwrap();

    let mut editor = ArchiveEditor::new(archive);

    editor.delete("file.txt").unwrap();
    assert!(editor.has_pending_operations());
    assert_eq!(editor.pending_operations(), 1);

    editor.clear_operations();
    assert!(!editor.has_pending_operations());
    assert_eq!(editor.pending_operations(), 0);
}

// ============================================================================
// Encrypted Archive Editing Tests
// ============================================================================

/// Tests editing operations on header-encrypted archives.
///
/// This module verifies that the ArchiveEditor correctly handles encrypted
/// archives opened with a password. The expected behavior is:
/// - Archive can be opened with correct password
/// - Edit operations can be performed
/// - Output archive preserves encryption (requires password option on writer)
#[cfg(feature = "aes")]
mod encrypted_editing {
    use super::*;
    use zesven::WriteOptions;

    /// Helper to create a header-encrypted archive.
    fn create_encrypted_archive(password: &str, entries: &[(&str, &[u8])]) -> Vec<u8> {
        let mut archive_bytes = Vec::new();
        {
            use zesven::Writer;
            let cursor = Cursor::new(&mut archive_bytes);
            let mut writer = Writer::create(cursor)
                .expect("Failed to create writer")
                .options(WriteOptions::new().password(password).encrypt_header(true));

            for (name, data) in entries {
                let path = ArchivePath::new(name).expect("Invalid path");
                writer.add_bytes(path, data).expect("Failed to add entry");
            }
            let _ = writer.finish().expect("Failed to finish archive");
        }
        archive_bytes
    }

    #[test]
    fn test_editor_delete_from_encrypted_archive() {
        let password = "test_password";
        let entries = [
            ("keep.txt", b"Keep this content" as &[u8]),
            ("delete.txt", b"Delete this content"),
        ];

        let archive_bytes = create_encrypted_archive(password, &entries);

        // Open with password and create editor
        let archive = Archive::open_with_password(Cursor::new(archive_bytes), password)
            .expect("Should open with correct password");

        let mut editor = ArchiveEditor::new(archive);
        editor.delete("delete.txt").expect("Delete should queue");

        // Apply changes
        let mut output = Cursor::new(Vec::new());
        let result = editor.apply(&mut output).expect("Apply should succeed");

        assert_eq!(result.entries_deleted, 1);
        assert_eq!(result.entries_kept, 1);

        // Verify the output archive can be read (note: output is NOT encrypted
        // unless writer options specify encryption)
        let contents = read_archive_contents(output.get_ref()).expect("Should read edited archive");
        assert_eq!(contents.len(), 1);
        assert_eq!(contents[0].0, "keep.txt");
    }

    #[test]
    fn test_editor_rename_in_encrypted_archive() {
        let password = "rename_test";
        let entries = [("original.txt", b"Content to rename" as &[u8])];

        let archive_bytes = create_encrypted_archive(password, &entries);

        let archive = Archive::open_with_password(Cursor::new(archive_bytes), password)
            .expect("Should open with correct password");

        let mut editor = ArchiveEditor::new(archive);
        editor
            .rename("original.txt", "renamed.txt")
            .expect("Rename should queue");

        let mut output = Cursor::new(Vec::new());
        let result = editor.apply(&mut output).expect("Apply should succeed");

        assert_eq!(result.entries_renamed, 1);

        let contents = read_archive_contents(output.get_ref()).expect("Should read edited archive");
        assert_eq!(contents.len(), 1);
        assert_eq!(contents[0].0, "renamed.txt");
        assert_eq!(contents[0].1, b"Content to rename");
    }

    #[test]
    fn test_editor_update_in_encrypted_archive() {
        let password = "update_test";
        let entries = [("file.txt", b"Original content" as &[u8])];

        let archive_bytes = create_encrypted_archive(password, &entries);

        let archive = Archive::open_with_password(Cursor::new(archive_bytes), password)
            .expect("Should open with correct password");

        let mut editor = ArchiveEditor::new(archive);
        editor
            .update("file.txt", b"Updated content".to_vec())
            .expect("Update should queue");

        let mut output = Cursor::new(Vec::new());
        let result = editor.apply(&mut output).expect("Apply should succeed");

        assert_eq!(result.entries_updated, 1);

        let contents = read_archive_contents(output.get_ref()).expect("Should read edited archive");
        assert_eq!(contents.len(), 1);
        assert_eq!(contents[0].1, b"Updated content");
    }
}

// ============================================================================
// Writer Error Handling Tests
// ============================================================================

/// Tests that ArchiveEditor::apply() propagates writer errors correctly.
///
/// When the output writer fails mid-operation, apply() should return an error
/// rather than silently producing corrupt output. This tests I/O error handling
/// during the archive rewriting process.
#[test]
fn test_editor_apply_propagates_writer_error() {
    use std::io::{self, Write};

    /// A writer that fails after writing a specified number of bytes.
    struct FailingWriter {
        bytes_until_failure: usize,
        bytes_written: usize,
    }

    impl FailingWriter {
        fn new(bytes_until_failure: usize) -> Self {
            Self {
                bytes_until_failure,
                bytes_written: 0,
            }
        }
    }

    impl Write for FailingWriter {
        fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
            let remaining = self.bytes_until_failure.saturating_sub(self.bytes_written);
            if remaining == 0 {
                return Err(io::Error::other("simulated write failure"));
            }
            let to_write = buf.len().min(remaining);
            self.bytes_written += to_write;
            Ok(to_write)
        }

        fn flush(&mut self) -> io::Result<()> {
            if self.bytes_written >= self.bytes_until_failure {
                Err(io::Error::other("simulated flush failure"))
            } else {
                Ok(())
            }
        }
    }

    impl io::Seek for FailingWriter {
        fn seek(&mut self, _pos: io::SeekFrom) -> io::Result<u64> {
            // Minimal seek implementation - return current position
            Ok(self.bytes_written as u64)
        }
    }

    // Create a valid archive to edit
    let entries = [
        ("file1.txt", b"Content for file 1" as &[u8]),
        ("file2.txt", b"Content for file 2"),
    ];
    let archive_bytes = create_archive(&entries).unwrap();
    let archive = Archive::open(Cursor::new(archive_bytes)).unwrap();

    let editor = ArchiveEditor::new(archive);

    // Try to apply with a writer that fails after 50 bytes
    // This should be enough to start writing but fail mid-stream
    let mut failing_writer = FailingWriter::new(50);
    let result = editor.apply(&mut failing_writer);

    // The apply operation should return an error, not silently fail
    assert!(
        result.is_err(),
        "apply() should return an error when the writer fails"
    );

    // Verify it's an I/O error
    match result.unwrap_err() {
        Error::Io(io_err) => {
            assert!(
                io_err.to_string().contains("simulated"),
                "Error should contain our simulated error message: {}",
                io_err
            );
        }
        other => panic!("Expected Io error, got: {:?}", other),
    }
}

// ============================================================================
// EditResult tests
// ============================================================================

#[test]
fn test_edit_result_compression_ratio() {
    let entries = [(
        "file.txt",
        b"This is some compressible content that should compress well. " as &[u8],
    )];

    let archive_bytes = create_archive(&entries).unwrap();
    let archive = Archive::open(Cursor::new(archive_bytes)).unwrap();

    let editor = ArchiveEditor::new(archive);

    let mut output = Cursor::new(Vec::new());
    let result = editor.apply(&mut output).unwrap();

    // Compression ratio should be between 0 and 1 (compressed/total)
    let ratio = result.compression_ratio();
    assert!(ratio > 0.0);
    assert!(ratio <= 1.0);
}