portable-network-archive 0.32.2

Portable-Network-Archive cli
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
use crate::utils::{
    EmbedExt, TestResources, archive::for_each_entry_with_password, diff::assert_dirs_equal, setup,
};
use clap::Parser;
use portable_network_archive::cli;
use std::{fs, io::prelude::*, time};

const DURATION_24_HOURS: time::Duration = time::Duration::from_secs(24 * 60 * 60);

/// Precondition: An encrypted archive exists with AES-CTR encryption and Argon2 key derivation.
/// Action: Modify a file to have newer mtime, run `pna experimental update` with same password.
/// Expectation: Archive is updated successfully and can be extracted with the original password.
#[test]
fn update_encrypted_archive() {
    setup();
    // Clean up any leftover files from previous test runs
    let _ = fs::remove_dir_all("update_encrypted");
    TestResources::extract_in("raw/", "update_encrypted/in/").unwrap();

    // Create encrypted archive
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "c",
        "update_encrypted/archive.pna",
        "--overwrite",
        "update_encrypted/in/",
        "--password",
        "testpass",
        "--aes",
        "ctr",
    ])
    .unwrap()
    .execute()
    .unwrap();

    // Modify a file with newer mtime
    let mut file = fs::File::options()
        .write(true)
        .truncate(true)
        .open("update_encrypted/in/raw/text.txt")
        .unwrap();
    file.write_all(b"updated content for encryption test")
        .unwrap();
    file.set_modified(time::SystemTime::now() + DURATION_24_HOURS)
        .unwrap();

    // Update with same password
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "experimental",
        "update",
        "-f",
        "update_encrypted/archive.pna",
        "update_encrypted/in/",
        "--password",
        "testpass",
        "--keep-timestamp",
    ])
    .unwrap()
    .execute()
    .unwrap();

    // Extract and verify
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "x",
        "update_encrypted/archive.pna",
        "--overwrite",
        "--out-dir",
        "update_encrypted/out/",
        "--password",
        "testpass",
        "--strip-components",
        "2",
    ])
    .unwrap()
    .execute()
    .unwrap();

    assert_dirs_equal("update_encrypted/in/", "update_encrypted/out/");
}

/// Precondition: An encrypted archive contains initial files.
/// Action: Add a new file to the source directory, run `pna experimental update` with password.
/// Expectation: Both existing and new entries are accessible with the password.
#[test]
fn update_encrypted_add_entry() {
    setup();
    // Clean up any leftover files from previous test runs
    let _ = fs::remove_dir_all("update_encrypted_add");
    TestResources::extract_in("raw/", "update_encrypted_add/in/").unwrap();

    // Create encrypted archive
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "c",
        "update_encrypted_add/archive.pna",
        "--overwrite",
        "update_encrypted_add/in/",
        "--password",
        "testpass",
        "--aes",
        "ctr",
    ])
    .unwrap()
    .execute()
    .unwrap();

    // Add new file
    fs::write(
        "update_encrypted_add/in/raw/new_file.txt",
        "new file content",
    )
    .unwrap();

    // Update with password
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "experimental",
        "update",
        "-f",
        "update_encrypted_add/archive.pna",
        "update_encrypted_add/in/",
        "--password",
        "testpass",
    ])
    .unwrap()
    .execute()
    .unwrap();

    // Verify new entry exists and is accessible
    let mut found_new_file = false;
    for_each_entry_with_password("update_encrypted_add/archive.pna", "testpass", |entry| {
        if entry.header().path().as_str().ends_with("new_file.txt") {
            found_new_file = true;
        }
    })
    .unwrap();
    assert!(found_new_file, "new_file.txt should be in the archive");

    // Extract and verify all content
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "x",
        "update_encrypted_add/archive.pna",
        "--overwrite",
        "--out-dir",
        "update_encrypted_add/out/",
        "--password",
        "testpass",
        "--strip-components",
        "2",
    ])
    .unwrap()
    .execute()
    .unwrap();

    assert_dirs_equal("update_encrypted_add/in/", "update_encrypted_add/out/");
}

/// Precondition: An encrypted archive with multiple files exists.
/// Action: Modify only one file, run update with password.
/// Expectation: Modified file is updated, unchanged files retain original content.
#[test]
fn update_encrypted_keep_unchanged() {
    setup();
    // Clean up any leftover files from previous test runs
    let _ = fs::remove_dir_all("update_encrypted_keep");
    TestResources::extract_in("raw/", "update_encrypted_keep/in/").unwrap();

    // Create encrypted archive
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "c",
        "update_encrypted_keep/archive.pna",
        "--overwrite",
        "update_encrypted_keep/in/",
        "--password",
        "testpass",
        "--aes",
        "ctr",
        "--keep-timestamp",
    ])
    .unwrap()
    .execute()
    .unwrap();

    // Modify only text.txt, leave empty.txt unchanged
    let mut file = fs::File::options()
        .write(true)
        .truncate(true)
        .open("update_encrypted_keep/in/raw/text.txt")
        .unwrap();
    file.write_all(b"modified text content").unwrap();
    file.set_modified(time::SystemTime::now() + DURATION_24_HOURS)
        .unwrap();

    // Update
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "experimental",
        "update",
        "-f",
        "update_encrypted_keep/archive.pna",
        "update_encrypted_keep/in/",
        "--password",
        "testpass",
        "--keep-timestamp",
    ])
    .unwrap()
    .execute()
    .unwrap();

    // Extract and verify
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "x",
        "update_encrypted_keep/archive.pna",
        "--overwrite",
        "--out-dir",
        "update_encrypted_keep/out/",
        "--password",
        "testpass",
        "--strip-components",
        "2",
    ])
    .unwrap()
    .execute()
    .unwrap();

    assert_dirs_equal("update_encrypted_keep/in/", "update_encrypted_keep/out/");
}

/// Precondition: An encrypted archive created with AES-CBC mode.
/// Action: Run `pna experimental update` with the same password.
/// Expectation: Archive remains functional with AES-CBC encryption.
#[test]
fn update_encrypted_aes_cbc() {
    setup();
    // Clean up any leftover files from previous test runs
    let _ = fs::remove_dir_all("update_encrypted_aes_cbc");
    TestResources::extract_in("raw/", "update_encrypted_aes_cbc/in/").unwrap();

    // Create encrypted archive with AES-CBC
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "c",
        "update_encrypted_aes_cbc/archive.pna",
        "--overwrite",
        "update_encrypted_aes_cbc/in/",
        "--password",
        "testpass",
        "--aes",
        "cbc",
    ])
    .unwrap()
    .execute()
    .unwrap();

    // Modify a file
    let mut file = fs::File::options()
        .write(true)
        .truncate(true)
        .open("update_encrypted_aes_cbc/in/raw/text.txt")
        .unwrap();
    file.write_all(b"updated with aes-cbc").unwrap();
    file.set_modified(time::SystemTime::now() + DURATION_24_HOURS)
        .unwrap();

    // Update with same password
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "experimental",
        "update",
        "-f",
        "update_encrypted_aes_cbc/archive.pna",
        "update_encrypted_aes_cbc/in/",
        "--password",
        "testpass",
        "--aes",
        "cbc",
        "--keep-timestamp",
    ])
    .unwrap()
    .execute()
    .unwrap();

    // Extract and verify
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "x",
        "update_encrypted_aes_cbc/archive.pna",
        "--overwrite",
        "--out-dir",
        "update_encrypted_aes_cbc/out/",
        "--password",
        "testpass",
        "--strip-components",
        "2",
    ])
    .unwrap()
    .execute()
    .unwrap();

    assert_dirs_equal(
        "update_encrypted_aes_cbc/in/",
        "update_encrypted_aes_cbc/out/",
    );
}

/// Precondition: An encrypted archive created with Camellia-CTR.
/// Action: Run `pna experimental update` with the same password.
/// Expectation: Archive remains functional with Camellia encryption.
#[test]
fn update_encrypted_camellia_ctr() {
    setup();
    // Clean up any leftover files from previous test runs
    let _ = fs::remove_dir_all("update_encrypted_camellia");
    TestResources::extract_in("raw/", "update_encrypted_camellia/in/").unwrap();

    // Create encrypted archive with Camellia-CTR
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "c",
        "update_encrypted_camellia/archive.pna",
        "--overwrite",
        "update_encrypted_camellia/in/",
        "--password",
        "testpass",
        "--camellia",
        "ctr",
    ])
    .unwrap()
    .execute()
    .unwrap();

    // Modify a file
    let mut file = fs::File::options()
        .write(true)
        .truncate(true)
        .open("update_encrypted_camellia/in/raw/text.txt")
        .unwrap();
    file.write_all(b"updated with camellia-ctr").unwrap();
    file.set_modified(time::SystemTime::now() + DURATION_24_HOURS)
        .unwrap();

    // Update with same password
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "experimental",
        "update",
        "-f",
        "update_encrypted_camellia/archive.pna",
        "update_encrypted_camellia/in/",
        "--password",
        "testpass",
        "--camellia",
        "ctr",
        "--keep-timestamp",
    ])
    .unwrap()
    .execute()
    .unwrap();

    // Extract and verify
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "x",
        "update_encrypted_camellia/archive.pna",
        "--overwrite",
        "--out-dir",
        "update_encrypted_camellia/out/",
        "--password",
        "testpass",
        "--strip-components",
        "2",
    ])
    .unwrap()
    .execute()
    .unwrap();

    assert_dirs_equal(
        "update_encrypted_camellia/in/",
        "update_encrypted_camellia/out/",
    );
}

/// Precondition: An encrypted archive created with PBKDF2 key derivation.
/// Action: Run `pna experimental update` with the same password.
/// Expectation: Archive remains functional with PBKDF2 derived key.
#[test]
fn update_encrypted_pbkdf2() {
    setup();
    // Clean up any leftover files from previous test runs
    let _ = fs::remove_dir_all("update_encrypted_pbkdf2");
    TestResources::extract_in("raw/", "update_encrypted_pbkdf2/in/").unwrap();

    // Create encrypted archive with PBKDF2
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "c",
        "update_encrypted_pbkdf2/archive.pna",
        "--overwrite",
        "update_encrypted_pbkdf2/in/",
        "--password",
        "testpass",
        "--aes",
        "ctr",
        "--pbkdf2",
    ])
    .unwrap()
    .execute()
    .unwrap();

    // Modify a file
    let mut file = fs::File::options()
        .write(true)
        .truncate(true)
        .open("update_encrypted_pbkdf2/in/raw/text.txt")
        .unwrap();
    file.write_all(b"updated with pbkdf2").unwrap();
    file.set_modified(time::SystemTime::now() + DURATION_24_HOURS)
        .unwrap();

    // Update with same password and PBKDF2
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "experimental",
        "update",
        "-f",
        "update_encrypted_pbkdf2/archive.pna",
        "update_encrypted_pbkdf2/in/",
        "--password",
        "testpass",
        "--aes",
        "ctr",
        "--pbkdf2",
        "--keep-timestamp",
    ])
    .unwrap()
    .execute()
    .unwrap();

    // Extract and verify
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "x",
        "update_encrypted_pbkdf2/archive.pna",
        "--overwrite",
        "--out-dir",
        "update_encrypted_pbkdf2/out/",
        "--password",
        "testpass",
        "--strip-components",
        "2",
    ])
    .unwrap()
    .execute()
    .unwrap();

    assert_dirs_equal(
        "update_encrypted_pbkdf2/in/",
        "update_encrypted_pbkdf2/out/",
    );
}

/// Precondition: An encrypted archive with known file content.
/// Action: Modify file content to specific value, run update, extract.
/// Expectation: Extracted content matches the modified source file exactly.
#[test]
fn update_encrypted_content_verify() {
    setup();
    // Clean up any leftover files from previous test runs
    let _ = fs::remove_dir_all("update_encrypted_content");
    TestResources::extract_in("raw/", "update_encrypted_content/in/").unwrap();

    // Create encrypted archive
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "c",
        "update_encrypted_content/archive.pna",
        "--overwrite",
        "update_encrypted_content/in/",
        "--password",
        "testpass",
        "--aes",
        "ctr",
    ])
    .unwrap()
    .execute()
    .unwrap();

    // Modify file with specific content
    let expected_content = b"SPECIFIC_CONTENT_FOR_VERIFICATION_12345";
    let mut file = fs::File::options()
        .write(true)
        .truncate(true)
        .open("update_encrypted_content/in/raw/text.txt")
        .unwrap();
    file.write_all(expected_content).unwrap();
    file.set_modified(time::SystemTime::now() + DURATION_24_HOURS)
        .unwrap();

    // Update
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "experimental",
        "update",
        "-f",
        "update_encrypted_content/archive.pna",
        "update_encrypted_content/in/",
        "--password",
        "testpass",
    ])
    .unwrap()
    .execute()
    .unwrap();

    // Extract
    cli::Cli::try_parse_from([
        "pna",
        "--quiet",
        "x",
        "update_encrypted_content/archive.pna",
        "--overwrite",
        "--out-dir",
        "update_encrypted_content/out/",
        "--password",
        "testpass",
        "--strip-components",
        "2",
    ])
    .unwrap()
    .execute()
    .unwrap();

    // Verify exact content
    let extracted_content = fs::read("update_encrypted_content/out/raw/text.txt").unwrap();
    assert_eq!(
        extracted_content.as_slice(),
        expected_content,
        "Extracted content should match the modified source"
    );
}