froe 0.12.0

Reader and offline maintenance toolkit for Apache Jackrabbit Oak segment-tar (TarMK) repositories: parse archives and records, extract node data, compact, back up, and recover.
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
//! Walking a long binary's blocks as an `io::Read`, so a value larger
//! than memory is consumed a block at a time.

use super::{
    BLOCK_SIZE, Error, MAXIMUM_LIST_SIZE, MEDIUM_VALUE_LIMIT, RecordIdentifier, Result,
    SMALL_VALUE_LIMIT, SegmentProvider, io, uncounted_list_entry_with_provider,
};

#[derive(Clone, Copy, Debug)]
pub(crate) enum BinaryStreamSource {
    Direct {
        record_identifier: RecordIdentifier,
        content_offset: usize,
    },
    Blocks {
        list_identifier: RecordIdentifier,
        block_count: u64,
    },
}

/// A bounded-memory reader over an inline binary value.
///
/// The stream borrows its [`SegmentProvider`] and keeps no segment view or
/// content buffer alive between reads. Small and medium values are copied
/// directly from their value record. For a long value, each read resolves
/// only the list entry for the current 4 KiB block; the complete block list
/// and binary are never materialized by the stream itself.
///
/// This type implements [`io::Read`] and [`io::Seek`]. Call
/// [`Self::read_chunk`] instead when
/// the caller needs `froe`'s precise [`Error`] variants: `io::Read` preserves
/// non-I/O errors as the inner value of an [`io::Error`], where they remain
/// available through [`io::Error::get_ref`] and downcasting. The stream is
/// `Send` whenever its concrete provider type is `Sync`, and its lifetime
/// prevents it from outliving that provider.
pub struct BinaryStream<
    'provider,
    Provider: SegmentProvider + ?Sized = dyn SegmentProvider + 'provider,
> {
    pub(crate) provider: &'provider Provider,
    pub(crate) source: BinaryStreamSource,
    pub(crate) length: u64,
    pub(crate) position: u64,
    pub(crate) resolved_block: Option<(u64, RecordIdentifier)>,
}

impl<Provider: SegmentProvider + ?Sized> BinaryStream<'_, Provider> {
    fn resolve_block_identifier(
        &mut self,
        list_identifier: RecordIdentifier,
        block_count: u64,
        block_index: u64,
    ) -> Result<RecordIdentifier> {
        if let Some((resolved_index, identifier)) = self.resolved_block
            && resolved_index == block_index
        {
            return Ok(identifier);
        }
        let identifier = uncounted_list_entry_with_provider(
            self.provider,
            list_identifier,
            block_count,
            block_index,
        )?;
        self.resolved_block = Some((block_index, identifier));
        Ok(identifier)
    }

    pub(in crate::content) fn current_block_identifier(
        &mut self,
    ) -> Result<Option<RecordIdentifier>> {
        if self.position == self.length {
            return Ok(None);
        }
        match self.source {
            BinaryStreamSource::Direct { .. } => Ok(None),
            BinaryStreamSource::Blocks {
                list_identifier,
                block_count,
            } => self
                .resolve_block_identifier(list_identifier, block_count, self.position / BLOCK_SIZE)
                .map(Some),
        }
    }

    /// Returns the total binary length declared by the value record.
    #[must_use]
    pub const fn len(&self) -> u64 {
        self.length
    }

    /// Returns whether the binary is empty.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.length == 0
    }

    /// Returns the number of bytes already read from the stream.
    #[must_use]
    pub const fn position(&self) -> u64 {
        self.position
    }

    /// Reads content into `buffer`, preserving `froe`'s typed errors.
    ///
    /// A long-value read stops at the current 4 KiB block boundary even when
    /// `buffer` has more room. Repeated calls continue with the next block,
    /// as permitted by [`io::Read`]. An empty buffer or an exhausted stream
    /// returns zero.
    pub fn read_chunk(&mut self, buffer: &mut [u8]) -> Result<usize> {
        if buffer.is_empty() || self.position == self.length {
            return Ok(0);
        }

        let buffer_capacity = u64::try_from(buffer.len()).unwrap_or(u64::MAX);
        let remaining = self.length - self.position;
        let maximum_read = remaining.min(buffer_capacity);

        let read_length = match self.source {
            BinaryStreamSource::Direct {
                record_identifier,
                content_offset,
            } => {
                let read_length =
                    usize::try_from(maximum_read).map_err(|_| Error::InvalidFormat {
                        details: format!(
                            "binary read length does not fit this platform in record \
                             {record_identifier}"
                        ),
                    })?;
                let position =
                    usize::try_from(self.position).map_err(|_| Error::InvalidFormat {
                        details: format!(
                            "binary position does not fit this platform in record \
                             {record_identifier}"
                        ),
                    })?;
                let offset =
                    content_offset
                        .checked_add(position)
                        .ok_or_else(|| Error::InvalidFormat {
                            details: format!(
                                "binary content offset overflows in record {record_identifier}"
                            ),
                        })?;
                let view = self.provider.segment(record_identifier.segment)?;
                buffer[..read_length].copy_from_slice(view.read_bytes(
                    record_identifier.record_number,
                    offset,
                    read_length,
                )?);
                read_length
            }
            BinaryStreamSource::Blocks {
                list_identifier,
                block_count,
            } => {
                let block_index = self.position / BLOCK_SIZE;
                let block_offset = self.position % BLOCK_SIZE;
                let block_remaining = BLOCK_SIZE - block_offset;
                let read_length =
                    usize::try_from(maximum_read.min(block_remaining)).map_err(|_| {
                        Error::InvalidFormat {
                            details: format!(
                                "binary block read length does not fit this platform for list \
                             {list_identifier}"
                            ),
                        }
                    })?;
                let block_identifier =
                    self.resolve_block_identifier(list_identifier, block_count, block_index)?;
                let view = self.provider.segment(block_identifier.segment)?;
                buffer[..read_length].copy_from_slice(view.read_bytes(
                    block_identifier.record_number,
                    usize::try_from(block_offset).map_err(|_| Error::InvalidFormat {
                        details: format!(
                            "binary block offset does not fit this platform in record \
                             {block_identifier}"
                        ),
                    })?,
                    read_length,
                )?);
                read_length
            }
        };
        let read_length_u64 = u64::try_from(read_length).map_err(|_| Error::InvalidFormat {
            details: "binary read length does not fit the 64-bit value format".to_owned(),
        })?;
        self.position += read_length_u64;
        Ok(read_length)
    }
}

/// Positioning within one binary value's blocks.
///
/// The stream already tracks its position and its declared length, and a
/// block is resolved from the position on every read, so a seek is a
/// position change and nothing else: no block is fetched until the next read
/// asks for one.
///
/// The bounds are the ones a reader of a *stored* value can honour. A seek
/// before the start is [`io::ErrorKind::InvalidInput`], as it is for every
/// `Seek` implementation. A seek **past the end is also refused**, which
/// `io::Seek` permits but does not require: a file's blocks exist only up to
/// its length, so a position beyond it could not be read from and letting one
/// be set would only move the failure to a later call with less context.
///
/// Non-I/O failures — a malformed record reached while resolving a
/// length — cannot occur here, because seeking resolves nothing. A caller
/// that needs froe's typed errors gets them from the next
/// [`BinaryStream::read_chunk`].
impl<Provider: SegmentProvider + ?Sized> io::Seek for BinaryStream<'_, Provider> {
    fn seek(&mut self, position: io::SeekFrom) -> io::Result<u64> {
        let (origin, offset) = match position {
            io::SeekFrom::Start(offset) => {
                self.position = self.refuse_past_end(offset)?;
                return Ok(self.position);
            }
            io::SeekFrom::End(offset) => (self.length, offset),
            io::SeekFrom::Current(offset) => (self.position, offset),
        };
        let absolute = origin.checked_add_signed(offset).ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::InvalidInput,
                "seeking to a negative position in a binary value",
            )
        })?;
        self.position = self.refuse_past_end(absolute)?;
        Ok(self.position)
    }

    fn stream_position(&mut self) -> io::Result<u64> {
        Ok(self.position)
    }
}

impl<Provider: SegmentProvider + ?Sized> BinaryStream<'_, Provider> {
    fn refuse_past_end(&self, position: u64) -> io::Result<u64> {
        if position > self.length {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                format!(
                    "seeking to {position} in a binary value of {} bytes",
                    self.length
                ),
            ));
        }
        Ok(position)
    }
}

impl<Provider: SegmentProvider + ?Sized> io::Read for BinaryStream<'_, Provider> {
    fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
        self.read_chunk(buffer).map_err(|error| match error {
            Error::InputOutput(source) => source,
            other => io::Error::other(other),
        })
    }
}

/// Opens a bounded-memory stream over an inline binary value.
///
/// The returned stream borrows `provider` and implements [`io::Read`]. Its
/// own memory use is constant regardless of the binary's declared length;
/// caller-provided read buffers determine the amount copied at a time.
/// Requesting a short-identifier external binary fails with
/// [`Error::ExternalBinaryContentUnavailable`]. A long-identifier external
/// binary fails with [`Error::ExternalBinaryContentUnavailableByRecord`]
/// without following or materializing its identifier string record. This
/// keeps opening a stream bounded even when that string record is hostile or
/// unavailable.
pub fn read_binary_stream<Provider: SegmentProvider + ?Sized>(
    provider: &Provider,
    identifier: RecordIdentifier,
) -> Result<BinaryStream<'_, Provider>> {
    let view = provider.segment(identifier.segment)?;
    let head = view.read_u8(identifier.record_number, 0)?;
    let (length, source) = if head & 0x80 == 0 {
        (
            u64::from(head),
            BinaryStreamSource::Direct {
                record_identifier: identifier,
                content_offset: 1,
            },
        )
    } else if head & 0x40 == 0 {
        let stored = view.read_u16(identifier.record_number, 0)?;
        (
            u64::from(stored & 0x3FFF) + SMALL_VALUE_LIMIT,
            BinaryStreamSource::Direct {
                record_identifier: identifier,
                content_offset: 2,
            },
        )
    } else if head & 0x20 == 0 {
        let stored = view.read_u64(identifier.record_number, 0)?;
        let length = (stored & 0x1FFF_FFFF_FFFF_FFFF) + MEDIUM_VALUE_LIMIT;
        let block_count = length.div_ceil(BLOCK_SIZE);
        if block_count > MAXIMUM_LIST_SIZE {
            return Err(Error::InvalidFormat {
                details: format!(
                    "binary of {length} bytes in record {identifier} needs {block_count} blocks, \
                     exceeding the list maximum of {MAXIMUM_LIST_SIZE}"
                ),
            });
        }
        let list_identifier = view.read_record_identifier(identifier.record_number, 8, 0)?;
        (
            length,
            BinaryStreamSource::Blocks {
                list_identifier,
                block_count,
            },
        )
    } else if head & 0x10 == 0 {
        let stored = view.read_u16(identifier.record_number, 0)?;
        let length = usize::from(stored & 0x0FFF);
        let blob_identifier =
            String::from_utf8_lossy(view.read_bytes(identifier.record_number, 2, length)?)
                .into_owned();
        return Err(Error::ExternalBinaryContentUnavailable { blob_identifier });
    } else if head & 0x08 == 0 {
        let string_identifier = view.read_record_identifier(identifier.record_number, 1, 0)?;
        return Err(Error::ExternalBinaryContentUnavailableByRecord {
            value_identifier: identifier,
            blob_identifier_record: string_identifier,
        });
    } else {
        return Err(Error::InvalidFormat {
            details: format!("unexpected value record marker {head:#04x} in record {identifier}"),
        });
    };

    Ok(BinaryStream {
        provider,
        source,
        length,
        position: 0,
        resolved_block: None,
    })
}

#[cfg(test)]
mod tests {
    use super::{BinaryStream, read_binary_stream};
    use crate::content::provider::tests::MemorySegmentProvider;
    use crate::content::value::binary::{read_binary_content, verify_binary_content};
    use crate::content::value::test_support::{
        CountingProvider, direct_binary_record, local_record_identifier, long_binary_record,
        referenced_record_identifier, small_string_record,
    };
    use crate::content::value::{BLOCK_SIZE, MEDIUM_VALUE_LIMIT};
    use crate::error::Error;
    use crate::segment::parsed_segment::{
        MAXIMUM_SEGMENT_SIZE,
        tests::{bulk_segment_identifier, data_segment_identifier, synthetic_data_segment},
    };
    use crate::segment::record::RecordIdentifier;
    use std::io::Read;

    #[test]
    fn long_external_stream_does_not_follow_the_identifier_record() {
        let value_segment = data_segment_identifier(11);
        let identifier_segment = data_segment_identifier(12);
        let mut long_external = vec![0xF0u8];
        long_external.extend_from_slice(&referenced_record_identifier(1, 7));

        let mut inner = MemorySegmentProvider::default();
        inner.insert(
            value_segment,
            synthetic_data_segment(&[identifier_segment], &[(3, 8, long_external)]),
        );
        inner.insert(
            identifier_segment,
            synthetic_data_segment(
                &[],
                &[(7, 4, small_string_record("identifier-must-not-be-read"))],
            ),
        );
        let provider = CountingProvider::new(&inner);
        let value_identifier = RecordIdentifier::new(value_segment, 3);

        match read_binary_stream(&provider, value_identifier) {
            Err(Error::ExternalBinaryContentUnavailableByRecord {
                value_identifier: reported_value,
                blob_identifier_record,
            }) => {
                assert_eq!(reported_value, value_identifier);
                assert_eq!(
                    blob_identifier_record,
                    RecordIdentifier::new(identifier_segment, 7)
                );
            }
            _ => panic!("expected bounded long-external error"),
        }
        assert_eq!(
            provider.segment_reads(),
            1,
            "opening the stream reads only the value record's segment"
        );
        provider.reset_segment_reads();
        verify_binary_content(&provider, value_identifier)
            .expect("consistency verification validates the identifier record");
        assert!(
            provider.segment_reads() > 1,
            "verification follows the identifier while the stream opener stays bounded"
        );
    }

    #[test]
    fn streams_small_and_medium_binaries_through_io_read() {
        let segment = data_segment_identifier(1);
        let empty = Vec::new();
        let boundary_small: Vec<u8> = (0..127).map(|index| index as u8).collect();
        let first_medium: Vec<u8> = (0..128).map(|index| index as u8).collect();
        let boundary_medium: Vec<u8> = (0..16_511).map(|index| (index % 251) as u8).collect();
        let mut provider = MemorySegmentProvider::default();
        provider.insert(
            segment,
            synthetic_data_segment(
                &[],
                &[
                    (0, 4, direct_binary_record(&empty)),
                    (1, 4, direct_binary_record(&boundary_small)),
                    (2, 4, direct_binary_record(&first_medium)),
                    (3, 4, direct_binary_record(&boundary_medium)),
                ],
            ),
        );

        for (record_number, expected) in [
            (0, empty.as_slice()),
            (1, boundary_small.as_slice()),
            (2, first_medium.as_slice()),
            (3, boundary_medium.as_slice()),
        ] {
            let mut stream =
                read_binary_stream(&provider, RecordIdentifier::new(segment, record_number))
                    .expect("stream");
            assert_eq!(stream.len(), expected.len() as u64);
            assert_eq!(stream.is_empty(), expected.is_empty());
            assert_eq!(stream.position(), 0);

            let mut actual = Vec::new();
            let mut buffer = [0u8; 37];
            loop {
                let read_length = stream.read(&mut buffer).expect("read");
                if read_length == 0 {
                    break;
                }
                actual.extend_from_slice(&buffer[..read_length]);
            }
            assert_eq!(actual, expected);
            assert_eq!(stream.position(), expected.len() as u64);
            assert_eq!(
                read_binary_content(&provider, RecordIdentifier::new(segment, record_number))
                    .expect("compatibility helper"),
                expected
            );
        }
    }

    #[test]
    fn streams_long_binary_from_partial_bulk_segment_across_block_boundaries() {
        let data_segment = data_segment_identifier(1);
        let bulk_segment = bulk_segment_identifier(2);
        let content: Vec<u8> = (0..20_000).map(|index| (index % 251) as u8).collect();
        let first_virtual_offset = (MAXIMUM_SEGMENT_SIZE - content.len()) as u32;

        let mut block_list = Vec::new();
        for block_offset in (0..content.len()).step_by(BLOCK_SIZE as usize) {
            block_list.extend_from_slice(&referenced_record_identifier(
                1,
                first_virtual_offset + block_offset as u32,
            ));
        }
        let value_record = long_binary_record(content.len() as u64, 10);

        let mut provider = MemorySegmentProvider::default();
        provider.insert(bulk_segment, content.clone());
        provider.insert(
            data_segment,
            synthetic_data_segment(
                &[bulk_segment],
                &[(10, 2, block_list), (11, 4, value_record)],
            ),
        );

        let identifier = RecordIdentifier::new(data_segment, 11);
        let mut stream = read_binary_stream(&provider, identifier).expect("stream");
        let mut first = [0u8; 17];
        assert_eq!(stream.read(&mut first).expect("first bytes"), first.len());
        assert_eq!(&first, &content[..17]);

        let mut through_boundary = [0u8; 5000];
        assert_eq!(
            stream.read(&mut through_boundary).expect("rest of block"),
            BLOCK_SIZE as usize - first.len(),
            "a read stops at the current block boundary"
        );
        assert_eq!(stream.position(), BLOCK_SIZE);

        let mut actual = content[..BLOCK_SIZE as usize].to_vec();
        stream.read_to_end(&mut actual).expect("remaining blocks");
        assert_eq!(actual, content);
        assert_eq!(
            read_binary_content(&provider, identifier).expect("compatibility helper"),
            content
        );
    }

    #[test]
    fn stream_preserves_corrupt_and_missing_record_errors() {
        let segment = data_segment_identifier(1);
        let truncated_direct = direct_binary_record(&[1, 2, 3, 4]);
        let mut provider = MemorySegmentProvider::default();
        provider.insert(
            segment,
            synthetic_data_segment(&[], &[(0, 4, truncated_direct[..3].to_vec())]),
        );
        let mut stream =
            read_binary_stream(&provider, RecordIdentifier::new(segment, 0)).expect("stream");
        let mut content = [0u8; 4];
        let error = stream
            .read_chunk(&mut content)
            .expect_err("truncated value");
        assert!(matches!(error, Error::InvalidFormat { .. }));
        assert_eq!(stream.position(), 0, "failed reads do not advance");
        assert!(matches!(
            read_binary_content(&provider, RecordIdentifier::new(segment, 0)),
            Err(Error::InvalidFormat { .. })
        ));

        let mut stream =
            read_binary_stream(&provider, RecordIdentifier::new(segment, 0)).expect("stream");
        let error = stream.read(&mut content).expect_err("io error");
        assert!(matches!(
            error
                .get_ref()
                .and_then(|source| source.downcast_ref::<Error>()),
            Some(Error::InvalidFormat { .. })
        ));
    }

    #[test]
    fn stream_reports_missing_long_block_after_completed_prefix() {
        let segment = data_segment_identifier(1);
        let length = MEDIUM_VALUE_LIMIT;
        let mut block_list = Vec::new();
        for record_number in [1u32, 2, 3, 4, 99] {
            block_list.extend_from_slice(&local_record_identifier(record_number));
        }
        let records = [
            (1, 5, vec![1; BLOCK_SIZE as usize]),
            (2, 5, vec![2; BLOCK_SIZE as usize]),
            (3, 5, vec![3; BLOCK_SIZE as usize]),
            (4, 5, vec![4; BLOCK_SIZE as usize]),
            (10, 2, block_list),
            (11, 4, long_binary_record(length, 10)),
        ];
        let mut provider = MemorySegmentProvider::default();
        provider.insert(segment, synthetic_data_segment(&[], &records));
        let mut stream =
            read_binary_stream(&provider, RecordIdentifier::new(segment, 11)).expect("stream");
        let mut block = [0u8; BLOCK_SIZE as usize];
        for expected in 1..=4 {
            assert_eq!(
                stream.read_chunk(&mut block).expect("present block"),
                block.len()
            );
            assert!(block.iter().all(|&byte| byte == expected));
        }
        let error = stream
            .read_chunk(&mut block)
            .expect_err("missing fifth block");
        assert!(matches!(error, Error::InvalidFormat { .. }));
        assert_eq!(stream.position(), 4 * BLOCK_SIZE);
    }

    #[test]
    fn stream_rejects_a_truncated_block_list_bucket() {
        let segment = data_segment_identifier(1);
        let length = MEDIUM_VALUE_LIMIT;
        let mut truncated_block_list = Vec::new();
        for record_number in 1..=4u32 {
            truncated_block_list.extend_from_slice(&local_record_identifier(record_number));
        }
        // Record 20 is last in physical record order, so resolving its
        // absent fifth identifier cannot accidentally consume a following
        // record's bytes.
        let records = [
            (1, 5, vec![1; BLOCK_SIZE as usize]),
            (2, 5, vec![2; BLOCK_SIZE as usize]),
            (3, 5, vec![3; BLOCK_SIZE as usize]),
            (4, 5, vec![4; BLOCK_SIZE as usize]),
            (10, 4, long_binary_record(length, 20)),
            (20, 2, truncated_block_list),
        ];
        let mut provider = MemorySegmentProvider::default();
        provider.insert(segment, synthetic_data_segment(&[], &records));
        let mut stream =
            read_binary_stream(&provider, RecordIdentifier::new(segment, 10)).expect("stream");
        let mut block = [0u8; BLOCK_SIZE as usize];
        for _ in 0..4 {
            assert_eq!(
                stream.read_chunk(&mut block).expect("present block"),
                block.len()
            );
        }
        match stream.read_chunk(&mut block) {
            Err(Error::InvalidFormat { details }) => {
                assert!(details.contains("record 20"), "unexpected error: {details}");
            }
            _ => panic!("expected truncated list bucket error"),
        }
        assert_eq!(stream.position(), 4 * BLOCK_SIZE);
    }

    #[test]
    fn stream_preserves_missing_segment_identity() {
        let data_segment = data_segment_identifier(1);
        let missing_bulk_segment = bulk_segment_identifier(2);
        let length = MEDIUM_VALUE_LIMIT;
        let missing_block =
            referenced_record_identifier(1, (MAXIMUM_SEGMENT_SIZE - BLOCK_SIZE as usize) as u32);
        let mut block_list = Vec::new();
        for _ in 0..length.div_ceil(BLOCK_SIZE) {
            block_list.extend_from_slice(&missing_block);
        }
        let mut provider = MemorySegmentProvider::default();
        provider.insert(
            data_segment,
            synthetic_data_segment(
                &[missing_bulk_segment],
                &[(10, 2, block_list), (11, 4, long_binary_record(length, 10))],
            ),
        );

        let mut stream =
            read_binary_stream(&provider, RecordIdentifier::new(data_segment, 11)).expect("stream");
        let mut byte = [0u8; 1];
        match stream.read_chunk(&mut byte) {
            Err(Error::SegmentNotFound { segment_identifier }) => {
                assert_eq!(segment_identifier, missing_bulk_segment);
            }
            _ => panic!("expected missing bulk segment error"),
        }
        assert_eq!(stream.position(), 0);
    }

    #[test]
    fn stream_rejects_truncated_heads_and_oversized_block_lists() {
        let segment = data_segment_identifier(1);
        let truncated = ((MEDIUM_VALUE_LIMIT - MEDIUM_VALUE_LIMIT) | (0x3 << 62))
            .to_be_bytes()
            .to_vec();
        let oversized_length = (crate::content::list::MAXIMUM_LIST_SIZE + 1) * BLOCK_SIZE;
        let oversized = long_binary_record(oversized_length, 99);
        let mut provider = MemorySegmentProvider::default();
        provider.insert(
            segment,
            synthetic_data_segment(&[], &[(0, 4, truncated), (1, 4, oversized)]),
        );

        assert!(matches!(
            read_binary_stream(&provider, RecordIdentifier::new(segment, 0)),
            Err(Error::InvalidFormat { .. })
        ));
        match read_binary_stream(&provider, RecordIdentifier::new(segment, 1)) {
            Err(Error::InvalidFormat { details }) => {
                assert!(details.contains("exceeding the list maximum"));
            }
            _ => panic!("expected oversized list error"),
        }
    }

    #[test]
    fn binary_stream_is_send_when_its_provider_is_sync() {
        fn assert_send<Type: Send>() {}
        assert_send::<BinaryStream<'static, MemorySegmentProvider>>();
    }
}