revault_lockbox_api 0.0.42

reVault lockbox API to create and manage lockboxes
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
#![cfg(feature = "external-source")]
//! API tests: external sources have no CLI equivalent. Fixtures use public APIs;
//! byte corruption and source failure injection exercise conditions no CLI can create.
use revault_lockbox_api::external_source::*;
use revault_lockbox_api::*;
use std::sync::{
    atomic::{AtomicUsize, Ordering},
    Arc, Mutex,
};

fn path(value: &str) -> LockboxPath {
    LockboxPath::new(value).unwrap()
}
fn fixture(compressed: bool, signed: bool, encrypted: bool) -> (Vec<u8>, Vec<u8>) {
    let signer = OwnerSigningKeyPair::generate().unwrap();
    let encryption = if encrypted {
        Encryption::Encrypted(LockboxProtection::ContentKey(
            SecretVec::try_from_slice(&[7; 32]).unwrap(),
        ))
    } else {
        Encryption::None
    };
    let mut options = LockboxCreateOptions::new(
        encryption,
        if signed {
            Signing::Owner(&signer)
        } else {
            Signing::None
        },
    );
    if !compressed {
        options.compression = Compression::None;
    }
    let mut archive = Lockbox::create_in_memory_with_options(options).unwrap();
    let payload: Vec<u8> = (0..300_137).map(|i| ((i * 73) % 251) as u8).collect();
    archive
        .add_file(&path("/data.bin"), &payload, false)
        .unwrap();
    archive.add_file(&path("/empty"), &[], false).unwrap();
    archive
        .add_symlink(&path("/link"), &path("/data.bin"), false)
        .unwrap();
    archive
        .set_variable(
            &VariableName::new("TEST_VALUE").unwrap(),
            "retained variable",
        )
        .unwrap();
    archive
        .define_form(
            "note",
            "Note",
            vec![
                FormFieldDefinition {
                    id: "body".into(),
                    label: "Body".into(),
                    kind: FormFieldKind::Text,
                    required: false,
                },
                FormFieldDefinition {
                    id: "secret".into(),
                    label: "Secret".into(),
                    kind: FormFieldKind::Secret,
                    required: false,
                },
            ],
        )
        .unwrap();
    archive
        .create_form_record(&path("/form"), "note", "record")
        .unwrap();
    archive
        .set_form_field_normal(&path("/form"), "body", "form contents")
        .unwrap();
    archive
        .set_form_field_secret(
            &path("/form"),
            "secret",
            &SecretString::try_from_slice(b"fixture form secret").unwrap(),
        )
        .unwrap();
    archive
        .set_secret_variable(
            &VariableName::new("SECRET_VALUE").unwrap(),
            &SecretString::try_from_slice(b"fixture variable secret").unwrap(),
        )
        .unwrap();
    archive.commit().unwrap();
    (archive.try_to_bytes().unwrap(), payload)
}
#[derive(Clone)]
struct Bytes(Arc<Vec<u8>>);
impl ReadAtSource for Bytes {
    fn len(&self) -> u64 {
        self.0.len() as u64
    }
    fn read_at(&self, offset: u64, output: &mut [u8]) -> std::result::Result<usize, SourceError> {
        output.copy_from_slice(&self.0[offset as usize..offset as usize + output.len()]);
        Ok(output.len())
    }
}
fn reader(source: Arc<dyn ReadAtSource>, encrypted: bool) -> ExternalReader {
    if encrypted {
        ExternalReader::with_content_key(
            source,
            SecretVec::try_from_slice(&[7; 32]).unwrap(),
            ExternalReaderOptions::default(),
        )
        .unwrap()
    } else {
        ExternalReader::new(source, ExternalReaderOptions::default()).unwrap()
    }
}
fn retry<T>(
    reader: &mut ExternalReader,
    sparse: &SparseSource,
    bytes: &[u8],
    mut operation: impl FnMut(&Lockbox<ReadOnly>) -> revault_lockbox_api::Result<T>,
) -> (T, usize) {
    for count in 0..4096 {
        match reader.try_read(&mut operation) {
            Ok(value) => return (value, count),
            Err(ExternalReadError::Source(SourceError::MissingRange { offset, length })) => sparse
                .supply(
                    "v1",
                    offset,
                    bytes[offset as usize..offset as usize + length].to_vec(),
                )
                .unwrap(),
            Err(error) => panic!("unexpected external failure: {error}"),
        }
    }
    panic!("retry loop did not converge")
}
#[test]
fn incremental_modes_preserve_files_and_other_record_types() {
    for compressed in [false, true] {
        for signed in [false, true] {
            for encrypted in [false, true] {
                let (bytes, payload) = fixture(compressed, signed, encrypted);
                let sparse = Arc::new(
                    SparseSource::new(bytes.len() as u64, "v1".into(), 4096, bytes.len() + 4096)
                        .unwrap(),
                );
                let mut reader = reader(sparse.clone(), encrypted);
                let ((actual, variable, form, link, empty), misses) =
                    retry(&mut reader, &sparse, &bytes, |archive| {
                        Ok((
                            archive.get_file(&path("/data.bin"))?,
                            archive.get_variable(&VariableName::new("TEST_VALUE")?)?,
                            archive.get_form_record(&path("/form"))?,
                            archive.get_symlink_target(&path("/link"))?,
                            archive.get_file(&path("/empty"))?,
                        ))
                    });
                assert_eq!(actual, payload);
                assert_eq!(variable.as_deref(), Some("retained variable"));
                let form = form.unwrap();
                assert_eq!(form.name, "record");
                assert_eq!(
                    form.values
                        .iter()
                        .find(|v| v.field_id == "body")
                        .unwrap()
                        .value,
                    FormValue::normal("form contents")
                );
                match &form
                    .values
                    .iter()
                    .find(|v| v.field_id == "secret")
                    .unwrap()
                    .value
                {
                    FormValue::Secret(value) => {
                        assert!(value.with_str(|s| s == "fixture form secret").unwrap())
                    }
                    _ => panic!("secret field lost sensitivity"),
                }
                assert_eq!(
                    retry(&mut reader, &sparse, &bytes, |archive| archive
                        .with_secret_variable(
                            &VariableName::new("SECRET_VALUE")?,
                            |value| value.with_str(|s| s == "fixture variable secret").unwrap()
                        ))
                    .0,
                    Some(true)
                );
                assert_eq!(link, path("/data.bin"));
                assert!(empty.is_empty());
                assert!(misses > 0 && misses <= bytes.len().div_ceil(4096));
                let (_, repeats) = retry(&mut reader, &sparse, &bytes, |archive| {
                    archive.get_file(&path("/data.bin"))
                });
                assert_eq!(repeats, 0);
                assert!(matches!(
                    reader.try_read(|archive| archive.get_file(&path("/absent"))),
                    Err(ExternalReadError::Archive(Error::NotFound(_)))
                ));
            }
        }
    }
}
#[test]
fn swallowed_errors_cannot_be_returned_as_success() {
    let (bytes, payload) = fixture(false, false, false);
    let sparse = Arc::new(
        SparseSource::new(bytes.len() as u64, "v1".into(), 4096, bytes.len() + 4096).unwrap(),
    );
    let mut reader = reader(sparse.clone(), false);
    retry(&mut reader, &sparse, &bytes, |_| Ok(()));
    assert!(sparse.cached_bytes().unwrap() < bytes.len());
    // Deliberately swallow every file-read error, as a fallback might do.
    assert!(matches!(
        reader.try_read(|archive| Ok(archive.get_file(&path("/data.bin")).is_ok())),
        Err(ExternalReadError::Source(SourceError::MissingRange { .. }))
    ));
    assert_eq!(
        retry(&mut reader, &sparse, &bytes, |archive| archive
            .get_file(&path("/data.bin")))
        .0,
        payload
    );
}
struct FaultSource {
    bytes: Bytes,
    call: AtomicUsize,
    fail_at: usize,
    trace: Mutex<Vec<(u64, usize)>>,
}
impl ReadAtSource for FaultSource {
    fn len(&self) -> u64 {
        self.bytes.len()
    }
    fn read_at(&self, offset: u64, output: &mut [u8]) -> std::result::Result<usize, SourceError> {
        self.trace.lock().unwrap().push((offset, output.len()));
        if self.call.fetch_add(1, Ordering::SeqCst) == self.fail_at {
            return Err(SourceError::MissingRange {
                offset,
                length: output.len(),
            });
        }
        self.bytes.read_at(offset, output)
    }
}
#[test]
fn every_open_and_lazy_record_read_failure_is_retryable_and_cannot_invoke_partial_open() {
    let (bytes, payload) = fixture(true, true, true);
    let bytes = Bytes(Arc::new(bytes));
    let baseline = Arc::new(FaultSource {
        bytes: bytes.clone(),
        call: AtomicUsize::new(0),
        fail_at: usize::MAX,
        trace: Mutex::new(vec![]),
    });
    let operation = |archive: &Lockbox<ReadOnly>| {
        archive.get_variable(&VariableName::new("TEST_VALUE")?)?;
        archive.get_form_record(&path("/form"))?;
        archive.get_file(&path("/data.bin"))
    };
    assert_eq!(
        reader(baseline.clone(), true).try_read(operation).unwrap(),
        payload
    );
    let calls = baseline.call.load(Ordering::SeqCst);
    assert!(calls > 4);
    for fail_at in 0..calls {
        let source = Arc::new(FaultSource {
            bytes: bytes.clone(),
            call: AtomicUsize::new(0),
            fail_at,
            trace: Mutex::new(vec![]),
        });
        let mut reader = reader(source, true);
        assert!(
            matches!(
                reader.try_read(operation),
                Err(ExternalReadError::Source(SourceError::MissingRange { .. }))
            ),
            "read {fail_at}"
        );
        assert_eq!(
            reader.try_read(operation).unwrap(),
            payload,
            "read {fail_at}"
        );
    }
    // Failure during open must not call a closure, even if open fallback succeeds.
    let open_calls = Arc::new(FaultSource {
        bytes: bytes.clone(),
        call: AtomicUsize::new(0),
        fail_at: usize::MAX,
        trace: Mutex::new(vec![]),
    });
    reader(open_calls.clone(), true)
        .try_read(|_| Ok(()))
        .unwrap();
    for fail_at in 0..open_calls.call.load(Ordering::SeqCst) {
        let source = Arc::new(FaultSource {
            bytes: bytes.clone(),
            call: AtomicUsize::new(0),
            fail_at,
            trace: Mutex::new(vec![]),
        });
        assert!(reader(source, true)
            .try_read::<()>(|_| panic!("closure called after incomplete open {fail_at}"))
            .is_err());
    }
}
#[test]
fn cache_bounds_eviction_overlap_identity_and_cancellation() {
    let cache = SparseSource::new(1600, "v1".into(), 512, 1024).unwrap();
    cache.supply("v1", 0, vec![1; 512]).unwrap();
    cache.supply("v1", 512, vec![2; 512]).unwrap();
    let mut out = [0; 8];
    cache.read_at(0, &mut out).unwrap(); // first block becomes most recently used
    cache.supply("v1", 1024, vec![3; 512]).unwrap();
    assert_eq!(cache.cached_bytes().unwrap(), 1024);
    assert!(matches!(
        cache.read_at(512, &mut out),
        Err(SourceError::MissingRange {
            offset: 512,
            length: 512
        })
    ));
    cache.supply("v1", 512, vec![2; 512]).unwrap();
    cache.read_at(1020, &mut out).unwrap();
    assert_eq!(out, [2, 2, 2, 2, 3, 3, 3, 3]);
    cache.supply("v1", 1536, vec![4; 64]).unwrap();
    assert!(cache.cached_bytes().unwrap() <= 1024);
    assert!(cache.supply("v1", 1536, vec![4; 63]).is_err());
    assert!(cache.supply("v1", u64::MAX, vec![4; 64]).is_err());
    assert!(cache.supply("v1", 1, vec![1; 512]).is_err());
    assert!(cache.read_at(u64::MAX, &mut out).is_err());
    assert_eq!(
        cache.supply("v2", 0, vec![1; 512]),
        Err(SourceError::VersionChanged)
    );
    assert_eq!(cache.cached_bytes().unwrap(), 0);
    assert_eq!(cache.validate(), Err(SourceError::VersionChanged));
    let cache = SparseSource::new(512, "v1".into(), 512, 512).unwrap();
    cache.supply("v1", 0, vec![1; 512]).unwrap();
    cache.supply("v1", 0, vec![1; 512]).unwrap();
    assert_eq!(
        cache.supply("v1", 0, vec![2; 512]),
        Err(SourceError::VersionChanged)
    );
}
#[test]
fn insufficient_cache_and_no_progress_exhaust_retry_budget() {
    let (bytes, _) = fixture(false, false, false);
    let sparse = Arc::new(SparseSource::new(bytes.len() as u64, "v1".into(), 512, 512).unwrap());
    let options = ExternalReaderOptions {
        max_missing_attempts: 8,
        ..Default::default()
    };
    let mut reader = ExternalReader::new(sparse.clone(), options).unwrap();
    for _ in 0..7 {
        match reader.try_read(|_| Ok(())) {
            Err(ExternalReadError::Source(SourceError::MissingRange { offset, length })) => sparse
                .supply(
                    "v1",
                    offset,
                    bytes[offset as usize..offset as usize + length].to_vec(),
                )
                .unwrap(),
            other => panic!("unexpected {other:?}"),
        }
    }
    assert_eq!(
        reader.try_read(|_| Ok(())),
        Err(ExternalReadError::RetryLimitExceeded)
    );
    assert_eq!(
        reader.try_read(|_| Ok(())),
        Err(ExternalReadError::RetryLimitExceeded)
    );
    assert!(sparse.cached_bytes().unwrap() <= 512);
}
#[test]
fn cancellation_and_version_change_are_checked_even_on_cache_hits() {
    let (bytes, _) = fixture(true, false, false);
    for cancel in [false, true] {
        let sparse = Arc::new(
            SparseSource::new(bytes.len() as u64, "v1".into(), 4096, bytes.len() + 4096).unwrap(),
        );
        let mut reader = reader(sparse.clone(), false);
        retry(&mut reader, &sparse, &bytes, |archive| {
            archive.get_file(&path("/data.bin"))
        });
        let expected = if cancel {
            sparse.cancel().unwrap();
            SourceError::Cancelled
        } else {
            assert_eq!(
                sparse.supply("v2", 0, vec![]),
                Err(SourceError::VersionChanged)
            );
            SourceError::VersionChanged
        };
        assert_eq!(
            reader.try_read(|archive| archive.get_file(&path("/data.bin"))),
            Err(ExternalReadError::Source(expected))
        );
    }
    let mut reader = reader(Arc::new(Bytes(Arc::new(bytes))), false);
    reader.cancel();
    assert_eq!(
        reader.try_read(|_| Ok(())),
        Err(ExternalReadError::Source(SourceError::Cancelled))
    );
}
struct InvalidSource {
    failure: Option<SourceError>,
}
impl ReadAtSource for InvalidSource {
    fn len(&self) -> u64 {
        4096
    }
    fn read_at(&self, _: u64, _: &mut [u8]) -> std::result::Result<usize, SourceError> {
        match &self.failure {
            Some(error) => Err(error.clone()),
            None => Ok(0),
        }
    }
}
#[test]
fn short_reads_invalid_missing_ranges_and_limits_fail_closed() {
    for failure in [
        None,
        Some(SourceError::MissingRange {
            offset: u64::MAX,
            length: 32,
        }),
        Some(SourceError::MissingRange {
            offset: 0,
            length: 0,
        }),
        Some(SourceError::Unavailable(
            "transport rejected response".into(),
        )),
    ] {
        let mut reader = reader(Arc::new(InvalidSource { failure }), false);
        assert!(matches!(
            reader.try_read(|_| Ok(())),
            Err(ExternalReadError::Source(SourceError::Unavailable(_)))
        ));
        assert!(reader
            .try_read::<()>(|_| panic!("terminal reader called closure"))
            .is_err());
    }
    let (bytes, _) = fixture(false, false, false);
    let mut reader = ExternalReader::new(
        Arc::new(Bytes(Arc::new(bytes))),
        ExternalReaderOptions {
            max_read_bytes: 1,
            ..Default::default()
        },
    )
    .unwrap();
    assert!(matches!(
        reader.try_read(|_| Ok(())),
        Err(ExternalReadError::Source(SourceError::Unavailable(_)))
    ));
}
#[test]
fn signed_content_corruption_truncation_and_wrong_key_are_rejected() {
    let (mut bytes, payload) = fixture(false, true, false);
    let offset = bytes
        .windows(64)
        .position(|value| value == &payload[..64])
        .unwrap();
    // Format 3 authenticates live content, not the physical tail extent.
    // Cut inside known live data: removing only unused tail padding is valid.
    for end in [offset + 1, offset + payload.len() / 2] {
        assert!(
            reader(Arc::new(Bytes(Arc::new(bytes[..end].to_vec()))), false)
                .try_read(|archive| archive.get_file(&path("/data.bin")))
                .is_err()
        );
    }
    bytes[offset + 12] ^= 1;
    assert!(reader(Arc::new(Bytes(Arc::new(bytes))), false)
        .try_read(|archive| archive.get_file(&path("/data.bin")))
        .is_err());
    let (bytes, _) = fixture(true, true, true);
    let mut wrong = ExternalReader::with_content_key(
        Arc::new(Bytes(Arc::new(bytes.clone()))),
        SecretVec::try_from_slice(&[8; 32]).unwrap(),
        ExternalReaderOptions::default(),
    )
    .unwrap();
    assert!(wrong.try_read(|_| Ok(())).is_err());
    assert!(reader(Arc::new(Bytes(Arc::new(bytes.clone()))), false)
        .try_read(|_| Ok(()))
        .is_err());
    for end in [1, 160, 319] {
        assert!(
            reader(Arc::new(Bytes(Arc::new(bytes[..end].to_vec()))), true)
                .try_read(|archive| archive.get_file(&path("/data.bin")))
                .is_err()
        );
    }
}
#[test]
fn independent_readers_share_cache_without_sharing_failure_state() {
    let (bytes, payload) = fixture(true, false, false);
    let bytes = Arc::new(bytes);
    let sparse = Arc::new(
        SparseSource::new(bytes.len() as u64, "v1".into(), 4096, bytes.len() + 4096).unwrap(),
    );
    let threads: Vec<_> = (0..4)
        .map(|_| {
            let sparse = sparse.clone();
            let bytes = bytes.clone();
            std::thread::spawn(move || {
                retry(
                    &mut reader(sparse.clone(), false),
                    &sparse,
                    &bytes,
                    |archive| archive.get_file(&path("/data.bin")),
                )
                .0
            })
        })
        .collect();
    for thread in threads {
        assert_eq!(thread.join().unwrap(), payload);
    }
    assert!(sparse.cached_bytes().unwrap() <= bytes.len());
}

#[test]
fn cumulative_read_budget_bounds_retry_work_without_network_progress() {
    let mut reader = ExternalReader::new(
        Arc::new(InvalidSource {
            failure: Some(SourceError::MissingRange {
                offset: 0,
                length: 512,
            }),
        }),
        ExternalReaderOptions {
            max_source_bytes_per_operation: 4096,
            max_missing_attempts: 1000,
            ..Default::default()
        },
    )
    .unwrap();
    let mut exhausted = false;
    for _ in 0..64 {
        match reader.try_read(|_| Ok(())) {
            Err(ExternalReadError::Source(SourceError::MissingRange { .. })) => {}
            Err(ExternalReadError::Source(SourceError::Unavailable(message))) => {
                assert!(message.contains("cumulative read budget"));
                exhausted = true;
                break;
            }
            other => panic!("unexpected result {other:?}"),
        }
    }
    assert!(
        exhausted,
        "read budget must terminate well before retry count"
    );
    assert!(matches!(
        reader.try_read(|_| Ok(())),
        Err(ExternalReadError::Source(SourceError::Unavailable(_)))
    ));
}

#[test]
fn source_invalidation_inside_an_operation_overrides_cached_success() {
    let (bytes, _) = fixture(true, false, false);
    let sparse = Arc::new(
        SparseSource::new(bytes.len() as u64, "v1".into(), 4096, bytes.len() + 4096).unwrap(),
    );
    let mut reader = reader(sparse.clone(), false);
    retry(&mut reader, &sparse, &bytes, |archive| {
        archive.get_file(&path("/data.bin"))
    });
    let result = reader.try_read(|archive| {
        let bytes = archive.get_file(&path("/data.bin"))?;
        sparse.cancel().unwrap();
        Ok(bytes)
    });
    assert_eq!(
        result,
        Err(ExternalReadError::Source(SourceError::Cancelled))
    );
}

struct MutableLength {
    bytes: Bytes,
    length: AtomicUsize,
}
impl ReadAtSource for MutableLength {
    fn len(&self) -> u64 {
        self.length.load(Ordering::SeqCst) as u64
    }
    fn read_at(&self, offset: u64, out: &mut [u8]) -> std::result::Result<usize, SourceError> {
        self.bytes.read_at(offset, out)
    }
}
#[test]
fn source_length_changes_are_rejected_before_and_after_cached_operations() {
    let (bytes, _) = fixture(true, false, false);
    for during in [false, true] {
        let source = Arc::new(MutableLength {
            length: AtomicUsize::new(bytes.len()),
            bytes: Bytes(Arc::new(bytes.clone())),
        });
        let mut reader = reader(source.clone(), false);
        reader
            .try_read(|archive| archive.get_file(&path("/data.bin")))
            .unwrap();
        if !during {
            source.length.fetch_add(1, Ordering::SeqCst);
        }
        let result = reader.try_read(|archive| {
            let value = archive.get_file(&path("/data.bin"))?;
            if during {
                source.length.fetch_add(1, Ordering::SeqCst);
            }
            Ok(value)
        });
        assert_eq!(
            result,
            Err(ExternalReadError::Source(SourceError::VersionChanged))
        );
    }
}

#[test]
fn sparse_construction_never_allocates_the_logical_archive_length() {
    let source = SparseSource::new(u64::MAX, "large immutable object".into(), 4096, 8192).unwrap();
    assert_eq!(source.len(), u64::MAX);
    assert_eq!(source.cached_bytes().unwrap(), 0);
    assert!(SparseSource::new(0, "v1".into(), 512, 512).is_err());
    assert!(SparseSource::new(512, "".into(), 512, 512).is_err());
    assert!(SparseSource::new(512, "v1".into(), 1, 512).is_err());
    assert!(SparseSource::new(512, "v1".into(), 512, 511).is_err());
}

#[test]
fn a_panicking_callback_cannot_leave_partially_observed_state_cached() {
    let (bytes, payload) = fixture(true, false, false);
    let source = Arc::new(FaultSource {
        bytes: Bytes(Arc::new(bytes)),
        call: AtomicUsize::new(0),
        fail_at: usize::MAX,
        trace: Mutex::new(vec![]),
    });
    let mut reader = reader(source.clone(), false);
    let panic = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        reader.try_read::<()>(|archive| {
            archive.get_variable(&VariableName::new("TEST_VALUE")?)?;
            panic!("application callback failed")
        })
    }));
    assert!(panic.is_err());
    let reads = source.call.load(Ordering::SeqCst);
    reader.try_read(|_| Ok(())).unwrap();
    assert!(
        source.call.load(Ordering::SeqCst) > reads,
        "must reopen after callback unwinds"
    );
    assert_eq!(
        reader
            .try_read(|archive| archive.get_file(&path("/data.bin")))
            .unwrap(),
        payload
    );
}

struct InvalidValidation;
impl ReadAtSource for InvalidValidation {
    fn len(&self) -> u64 {
        4096
    }
    fn read_at(&self, _: u64, _: &mut [u8]) -> std::result::Result<usize, SourceError> {
        panic!("validation should fail before reads")
    }
    fn validate(&self) -> std::result::Result<(), SourceError> {
        Err(SourceError::MissingRange {
            offset: u64::MAX,
            length: 1,
        })
    }
}
#[test]
fn invalid_missing_ranges_from_validation_cannot_reach_the_transport() {
    assert!(matches!(
        reader(Arc::new(InvalidValidation), false).try_read(|_| Ok(())),
        Err(ExternalReadError::Source(SourceError::Unavailable(_)))
    ));
}