shrike 0.1.0

AT Protocol library for Rust
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
use crate::cbor::Cid;

pub mod reader;
pub mod writer;

pub use reader::Reader;
pub use writer::Writer;

#[derive(Debug, thiserror::Error)]
pub enum CarError {
    #[error("invalid CAR header: {0}")]
    InvalidHeader(String),
    #[error("invalid CAR block: {0}")]
    InvalidBlock(String),
    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),
    #[error("CBOR error: {0}")]
    Cbor(#[from] crate::cbor::CborError),
}

/// A single block in a CAR file.
#[derive(Debug, Clone)]
pub struct Block {
    pub cid: Cid,
    pub data: Vec<u8>,
}

impl Default for Block {
    /// Creates a Block with a zeroed CID and empty data.
    ///
    /// The CID is not valid — this is intended for use with
    /// [`Reader::next_block_into`] which overwrites both fields.
    fn default() -> Self {
        Block {
            cid: Cid::zeroed(),
            data: Vec::new(),
        }
    }
}

/// Read all blocks from a CAR reader into memory.
pub fn read_all(mut reader: impl std::io::Read) -> Result<(Vec<Cid>, Vec<Block>), CarError> {
    let mut car = Reader::new(&mut reader)?;
    let roots = car.roots().to_vec();

    let mut blocks = Vec::new();
    while let Some(block) = car.next_block()? {
        blocks.push(block);
    }

    Ok((roots, blocks))
}

/// Write a complete CAR v1 file to bytes.
pub fn write_all(roots: &[Cid], blocks: &[Block]) -> Result<Vec<u8>, CarError> {
    // Pre-size: header (~100 bytes) + per-block (10 varint + 36 CID + data)
    let estimated = 128 + blocks.iter().map(|b| 10 + 36 + b.data.len()).sum::<usize>();
    let mut buf = Vec::with_capacity(estimated);
    let mut writer = Writer::new(&mut buf, roots)?;
    for block in blocks {
        writer.write_block(block)?;
    }
    Ok(buf)
}

/// Verify all blocks in a CAR file by recomputing each CID.
///
/// Uses the codec from the stored CID to compute a single SHA-256 hash per
/// block. Reuses a single buffer across all blocks to avoid per-block
/// heap allocation.
pub fn verify(mut reader: impl std::io::Read) -> Result<(), CarError> {
    let mut car = Reader::new(&mut reader)?;
    let mut block = Block::default();
    while car.next_block_into(&mut block)? {
        let computed = Cid::compute(block.cid.codec(), &block.data);
        if block.cid != computed {
            return Err(CarError::InvalidBlock(format!(
                "CID mismatch for block: stored {}, computed {}",
                block.cid, computed
            )));
        }
    }
    Ok(())
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::unreachable
)]
mod tests {
    use crate::car::*;
    use crate::cbor::Codec;

    // ---------------------------------------------------------------------------
    // Helpers
    // ---------------------------------------------------------------------------

    /// Build a valid CAR header buffer (varint-prefixed) from a raw CBOR map encoded
    /// by `build_fn`.  This lets individual tests inject malformed headers.
    fn build_car_with_header<F>(build_fn: F) -> Vec<u8>
    where
        F: FnOnce(&mut crate::cbor::Encoder<&mut Vec<u8>>),
    {
        let mut header_buf = Vec::new();
        {
            let mut enc = crate::cbor::Encoder::new(&mut header_buf);
            build_fn(&mut enc);
        }
        let mut car_buf = Vec::new();
        crate::cbor::varint::encode_varint(header_buf.len() as u64, &mut car_buf);
        car_buf.extend_from_slice(&header_buf);
        car_buf
    }

    // ---------------------------------------------------------------------------
    // Original tests (preserved)
    // ---------------------------------------------------------------------------

    #[test]
    fn write_and_read_roundtrip() {
        let blocks: Vec<Block> = (0..3)
            .map(|i| {
                let data = format!("block {i}").into_bytes();
                Block {
                    cid: Cid::compute(Codec::Raw, &data),
                    data,
                }
            })
            .collect();
        let written = write_all(&[blocks[0].cid], &blocks).unwrap();
        let (roots, read_blocks) = read_all(&written[..]).unwrap();
        assert_eq!(roots.len(), 1);
        assert_eq!(roots[0], blocks[0].cid);
        assert_eq!(read_blocks.len(), 3);
        for (orig, read) in blocks.iter().zip(read_blocks.iter()) {
            assert_eq!(orig.cid, read.cid);
            assert_eq!(orig.data, read.data);
        }
    }

    #[test]
    fn empty_car() {
        let root = Cid::compute(Codec::Drisl, b"root");
        let written = write_all(&[root], &[]).unwrap();
        let (roots, blocks) = read_all(&written[..]).unwrap();
        assert_eq!(roots, vec![root]);
        assert!(blocks.is_empty());
    }

    #[test]
    fn verify_valid_car() {
        let blocks: Vec<Block> = (0..3)
            .map(|i| {
                let data = format!("block {i}").into_bytes();
                Block {
                    cid: Cid::compute(Codec::Raw, &data),
                    data,
                }
            })
            .collect();
        let written = write_all(&[blocks[0].cid], &blocks).unwrap();
        verify(&written[..]).unwrap();
    }

    #[test]
    fn verify_corrupt_data_fails() {
        let data = b"test data".to_vec();
        let cid = Cid::compute(Codec::Raw, &data);
        let mut written = write_all(&[cid], &[Block { cid, data }]).unwrap();
        // Corrupt the last byte of block data
        let len = written.len();
        written[len - 1] ^= 0xff;
        assert!(verify(&written[..]).is_err());
    }

    #[test]
    fn reader_rejects_wrong_version() {
        // Manually construct a header with version 2
        let car_buf = build_car_with_header(|enc| {
            enc.encode_map_header(2).unwrap();
            enc.encode_text("roots").unwrap();
            enc.encode_array_header(0).unwrap();
            enc.encode_text("version").unwrap();
            enc.encode_u64(2).unwrap();
        });

        assert!(Reader::new(&car_buf[..]).is_err());
    }

    #[test]
    fn large_block_roundtrip() {
        let data = vec![0xABu8; 100_000];
        let cid = Cid::compute(Codec::Raw, &data);
        let block = Block { cid, data };
        let written = write_all(&[cid], std::slice::from_ref(&block)).unwrap();
        let (_, blocks) = read_all(&written[..]).unwrap();
        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].data.len(), 100_000);
    }

    #[test]
    fn multiple_roots() {
        let root1 = Cid::compute(Codec::Drisl, b"root1");
        let root2 = Cid::compute(Codec::Drisl, b"root2");
        let written = write_all(&[root1, root2], &[]).unwrap();
        let (roots, _) = read_all(&written[..]).unwrap();
        assert_eq!(roots.len(), 2);
        assert_eq!(roots[0], root1);
        assert_eq!(roots[1], root2);
    }

    // ---------------------------------------------------------------------------
    // Reader edge cases
    // ---------------------------------------------------------------------------

    #[test]
    fn reader_empty_input_errors() {
        assert!(Reader::new(&[][..]).is_err());
    }

    #[test]
    fn reader_header_with_no_roots() {
        // A valid CAR v1 header whose roots array is empty is legal.
        let car_buf = build_car_with_header(|enc| {
            enc.encode_map_header(2).unwrap();
            enc.encode_text("roots").unwrap();
            enc.encode_array_header(0).unwrap();
            enc.encode_text("version").unwrap();
            enc.encode_u64(1).unwrap();
        });
        let car = Reader::new(&car_buf[..]).unwrap();
        assert!(car.roots().is_empty());
    }

    #[test]
    fn reader_header_with_many_roots() {
        // Five roots should be accepted without error.
        let roots: Vec<Cid> = (0..5)
            .map(|i| Cid::compute(Codec::Drisl, format!("root{i}").as_bytes()))
            .collect();

        let car_buf = build_car_with_header(|enc| {
            enc.encode_map_header(2).unwrap();
            enc.encode_text("roots").unwrap();
            enc.encode_array_header(roots.len() as u64).unwrap();
            for r in &roots {
                enc.encode_cid(r).unwrap();
            }
            enc.encode_text("version").unwrap();
            enc.encode_u64(1).unwrap();
        });

        let car = Reader::new(&car_buf[..]).unwrap();
        assert_eq!(car.roots().len(), 5);
        for (i, r) in roots.iter().enumerate() {
            assert_eq!(car.roots()[i], *r);
        }
    }

    #[test]
    fn reader_truncated_block_errors() {
        // Write a valid header, then write a block-length varint that promises more
        // bytes than are actually present.
        let root = Cid::compute(Codec::Raw, b"root");
        let mut car = write_all(&[root], &[]).unwrap();

        // Claim a block of 200 bytes but write only 10.
        crate::cbor::varint::encode_varint(200, &mut car);
        car.extend_from_slice(&[0u8; 10]);

        assert!(read_all(&car[..]).is_err());
    }

    #[test]
    fn reader_block_with_wrong_cid_length_errors() {
        // Build a header, then a block whose announced length < 36 (too short for a CID).
        let root = Cid::compute(Codec::Raw, b"root");
        let mut car = write_all(&[root], &[]).unwrap();

        // 10-byte "block": not enough to hold a 36-byte CID.
        let fake_data = vec![0u8; 10];
        crate::cbor::varint::encode_varint(fake_data.len() as u64, &mut car);
        car.extend_from_slice(&fake_data);

        assert!(read_all(&car[..]).is_err());
    }

    #[test]
    fn reader_very_large_block() {
        // 1 MiB + 1 byte of data; verifies no length-overflow panics, etc.
        let data = vec![0x5Au8; 1_048_577];
        let cid = Cid::compute(Codec::Raw, &data);
        let block = Block {
            cid,
            data: data.clone(),
        };
        let written = write_all(&[cid], std::slice::from_ref(&block)).unwrap();
        let (_, blocks) = read_all(&written[..]).unwrap();
        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].data, data);
    }

    // ---------------------------------------------------------------------------
    // Writer edge cases
    // ---------------------------------------------------------------------------

    #[test]
    fn writer_zero_blocks_just_header() {
        let root = Cid::compute(Codec::Drisl, b"only-root");
        let written = write_all(&[root], &[]).unwrap();
        // Must be parseable; roots preserved; no blocks.
        let (roots, blocks) = read_all(&written[..]).unwrap();
        assert_eq!(roots, vec![root]);
        assert!(blocks.is_empty());
    }

    #[test]
    fn writer_single_block() {
        let data = b"singleton".to_vec();
        let cid = Cid::compute(Codec::Raw, &data);
        let block = Block {
            cid,
            data: data.clone(),
        };
        let written = write_all(&[cid], std::slice::from_ref(&block)).unwrap();
        let (roots, blocks) = read_all(&written[..]).unwrap();
        assert_eq!(roots, vec![cid]);
        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].data, data);
    }

    #[test]
    fn writer_many_blocks() {
        let blocks: Vec<Block> = (0..100)
            .map(|i| {
                let data = format!("block-{i:04}").into_bytes();
                Block {
                    cid: Cid::compute(Codec::Raw, &data),
                    data,
                }
            })
            .collect();
        let written = write_all(&[blocks[0].cid], &blocks).unwrap();
        let (_, read_blocks) = read_all(&written[..]).unwrap();
        assert_eq!(read_blocks.len(), 100);
        for (orig, read) in blocks.iter().zip(read_blocks.iter()) {
            assert_eq!(orig.cid, read.cid);
            assert_eq!(orig.data, read.data);
        }
    }

    // ---------------------------------------------------------------------------
    // Roundtrip tests
    // ---------------------------------------------------------------------------

    #[test]
    fn roundtrip_mixed_codecs() {
        // Mix Drisl and Raw blocks in the same CAR.
        let drisl_data = b"drisl content".to_vec();
        let raw_data = b"raw content".to_vec();
        let drisl_block = Block {
            cid: Cid::compute(Codec::Drisl, &drisl_data),
            data: drisl_data.clone(),
        };
        let raw_block = Block {
            cid: Cid::compute(Codec::Raw, &raw_data),
            data: raw_data.clone(),
        };
        let blocks = vec![drisl_block.clone(), raw_block.clone()];
        let written = write_all(&[drisl_block.cid, raw_block.cid], &blocks).unwrap();
        let (roots, read_blocks) = read_all(&written[..]).unwrap();

        assert_eq!(roots.len(), 2);
        assert_eq!(roots[0], drisl_block.cid);
        assert_eq!(roots[1], raw_block.cid);
        assert_eq!(read_blocks.len(), 2);
        assert_eq!(read_blocks[0].cid, drisl_block.cid);
        assert_eq!(read_blocks[0].data, drisl_data);
        assert_eq!(read_blocks[1].cid, raw_block.cid);
        assert_eq!(read_blocks[1].data, raw_data);
    }

    #[test]
    fn roundtrip_preserves_exact_bytes() {
        // Ensure the raw bytes coming back from read are bit-for-bit identical.
        let data: Vec<u8> = (0u8..=255).cycle().take(512).collect();
        let cid = Cid::compute(Codec::Raw, &data);
        let block = Block {
            cid,
            data: data.clone(),
        };
        let written = write_all(&[cid], std::slice::from_ref(&block)).unwrap();
        let (_, blocks) = read_all(&written[..]).unwrap();
        assert_eq!(blocks[0].data, data);
    }

    #[test]
    fn roundtrip_multiple_roots() {
        // Five roots, five blocks – each root maps 1-to-1 with its block.
        let pairs: Vec<(Cid, Block)> = (0..5)
            .map(|i| {
                let data = format!("content-{i}").into_bytes();
                let cid = Cid::compute(Codec::Drisl, &data);
                (cid, Block { cid, data })
            })
            .collect();
        let roots: Vec<Cid> = pairs.iter().map(|(c, _)| *c).collect();
        let blocks: Vec<Block> = pairs.into_iter().map(|(_, b)| b).collect();

        let written = write_all(&roots, &blocks).unwrap();
        let (read_roots, read_blocks) = read_all(&written[..]).unwrap();

        assert_eq!(read_roots, roots);
        assert_eq!(read_blocks.len(), blocks.len());
        for (orig, read) in blocks.iter().zip(read_blocks.iter()) {
            assert_eq!(orig.cid, read.cid);
            assert_eq!(orig.data, read.data);
        }
    }

    #[test]
    fn roundtrip_empty_data_blocks() {
        // Blocks with zero-length data payloads.
        let data: Vec<u8> = vec![];
        let cid = Cid::compute(Codec::Raw, &data);
        let block = Block {
            cid,
            data: data.clone(),
        };
        let written = write_all(&[cid], std::slice::from_ref(&block)).unwrap();
        let (_, blocks) = read_all(&written[..]).unwrap();
        assert_eq!(blocks.len(), 1);
        assert!(blocks[0].data.is_empty());
        assert_eq!(blocks[0].cid, cid);
    }

    // ---------------------------------------------------------------------------
    // Verify tests
    // ---------------------------------------------------------------------------

    #[test]
    fn verify_passes_on_valid_data() {
        let blocks: Vec<Block> = (0..5)
            .map(|i| {
                let data = format!("valid-{i}").into_bytes();
                Block {
                    cid: Cid::compute(if i % 2 == 0 { Codec::Drisl } else { Codec::Raw }, &data),
                    data,
                }
            })
            .collect();
        let roots = vec![blocks[0].cid];
        let written = write_all(&roots, &blocks).unwrap();
        verify(&written[..]).unwrap();
    }

    #[test]
    fn verify_fails_on_corrupted_cid() {
        // Write a valid block, then manually corrupt the CID bytes in the output
        // (the CID lives right after the block-length varint).
        let data = b"cid-corruption-test".to_vec();
        let cid = Cid::compute(Codec::Raw, &data);
        let mut written = write_all(&[cid], &[Block { cid, data }]).unwrap();

        // The CID bytes begin right after the header section.  Find the varint
        // that encodes the block length: it follows immediately after the header.
        // Rather than re-parsing, flip a byte in the hash portion of the first
        // CID (bytes 4..36 within the block payload, which starts after the
        // block-length varint).  We scan from the end of the header by writing
        // a second CAR with zero blocks to find the split point.
        let header_only = {
            let mut h = Vec::new();
            let w = Writer::new(&mut h, &[cid]).unwrap();
            // write nothing – just get header length
            let _ = w.finish();
            h
        };
        // block-length varint starts right after header_only
        let varint_start = header_only.len();
        // skip the varint (single byte if block_len < 128, else multi-byte)
        // block_len = 36 (cid) + data.len() < 128, so the varint is 1 byte.
        let cid_start = varint_start + 1;
        // flip a byte in the middle of the SHA-256 hash (offset 20 within CID)
        written[cid_start + 20] ^= 0xff;

        assert!(verify(&written[..]).is_err());
    }

    #[test]
    fn verify_fails_on_corrupted_block_data() {
        let data = b"block-data-corruption".to_vec();
        let cid = Cid::compute(Codec::Raw, &data);
        let mut written = write_all(&[cid], &[Block { cid, data }]).unwrap();
        // The block data lives at the very end – flip the last byte.
        let n = written.len();
        written[n - 1] ^= 0xAA;
        assert!(verify(&written[..]).is_err());
    }

    #[test]
    fn verify_fails_when_block_data_swapped() {
        // Two blocks whose CIDs are computed from their own data.
        // Swap the data payloads so each block's CID no longer matches its data.
        let data_a = b"payload-alpha".to_vec();
        let data_b = b"payload-beta".to_vec();
        let cid_a = Cid::compute(Codec::Raw, &data_a);
        let cid_b = Cid::compute(Codec::Raw, &data_b);

        // Write block_a with data_b and block_b with data_a (swapped).
        let blocks = vec![
            Block {
                cid: cid_a,
                data: data_b,
            },
            Block {
                cid: cid_b,
                data: data_a,
            },
        ];
        let written = write_all(&[cid_a, cid_b], &blocks).unwrap();
        assert!(verify(&written[..]).is_err());
    }

    // ---------------------------------------------------------------------------
    // Streaming reader test
    // ---------------------------------------------------------------------------

    #[test]
    fn streaming_reader_matches_read_all() {
        // Build a CAR with several blocks, then compare next_block() iteration
        // results with those from read_all().
        let blocks: Vec<Block> = (0..7)
            .map(|i| {
                let data = format!("stream-block-{i}").into_bytes();
                let codec = if i % 2 == 0 { Codec::Drisl } else { Codec::Raw };
                Block {
                    cid: Cid::compute(codec, &data),
                    data,
                }
            })
            .collect();
        let roots = vec![blocks[0].cid, blocks[1].cid];
        let written = write_all(&roots, &blocks).unwrap();

        // read_all path
        let (ra_roots, ra_blocks) = read_all(&written[..]).unwrap();

        // streaming path
        let mut car = Reader::new(&written[..]).unwrap();
        assert_eq!(car.roots(), ra_roots.as_slice());

        let mut stream_blocks = Vec::new();
        while let Some(block) = car.next_block().unwrap() {
            stream_blocks.push(block);
        }
        // Subsequent calls after EOF should return None, not an error.
        assert!(car.next_block().unwrap().is_none());

        assert_eq!(stream_blocks.len(), ra_blocks.len());
        for (s, r) in stream_blocks.iter().zip(ra_blocks.iter()) {
            assert_eq!(s.cid, r.cid);
            assert_eq!(s.data, r.data);
        }
    }

    // --- Security: header and block validation ---

    #[test]
    fn reader_rejects_version_zero() {
        let car_buf = build_car_with_header(|enc| {
            enc.encode_map_header(2).unwrap();
            enc.encode_text("roots").unwrap();
            enc.encode_array_header(0).unwrap();
            enc.encode_text("version").unwrap();
            enc.encode_u64(0).unwrap();
        });
        assert!(Reader::new(&car_buf[..]).is_err());
    }

    #[test]
    fn reader_rejects_version_three() {
        let car_buf = build_car_with_header(|enc| {
            enc.encode_map_header(2).unwrap();
            enc.encode_text("roots").unwrap();
            enc.encode_array_header(0).unwrap();
            enc.encode_text("version").unwrap();
            enc.encode_u64(3).unwrap();
        });
        assert!(Reader::new(&car_buf[..]).is_err());
    }

    #[test]
    fn reader_rejects_missing_roots() {
        // Header with version but no roots field
        let car_buf = build_car_with_header(|enc| {
            enc.encode_map_header(1).unwrap();
            enc.encode_text("version").unwrap();
            enc.encode_u64(1).unwrap();
        });
        assert!(Reader::new(&car_buf[..]).is_err());
    }

    #[test]
    fn reader_rejects_missing_version() {
        // Header with roots but no version field
        let car_buf = build_car_with_header(|enc| {
            enc.encode_map_header(1).unwrap();
            enc.encode_text("roots").unwrap();
            enc.encode_array_header(0).unwrap();
        });
        assert!(Reader::new(&car_buf[..]).is_err());
    }

    #[test]
    fn reader_rejects_incomplete_varint() {
        // Single byte 0x80 — continuation bit set but no follow-up byte
        let buf = [0x80];
        assert!(Reader::new(&buf[..]).is_err());
    }

    #[test]
    fn deterministic_write() {
        let blocks: Vec<Block> = (0..10)
            .map(|i| {
                let data = format!("block-{i}").into_bytes();
                Block {
                    cid: Cid::compute(Codec::Raw, &data),
                    data,
                }
            })
            .collect();
        let first = write_all(&[blocks[0].cid], &blocks).unwrap();
        for _ in 0..10 {
            let again = write_all(&[blocks[0].cid], &blocks).unwrap();
            assert_eq!(first, again, "write_all must be deterministic");
        }
    }

    #[test]
    fn next_block_into_reuses_buffer() {
        // Verify that next_block_into doesn't allocate new buffers
        // by checking that the Vec capacity grows to the largest block
        // and stays there.
        let blocks: Vec<Block> = (0..5)
            .map(|i| {
                let data = vec![i as u8; 100 * (i + 1)];
                Block {
                    cid: Cid::compute(Codec::Raw, &data),
                    data,
                }
            })
            .collect();
        let written = write_all(&[blocks[0].cid], &blocks).unwrap();

        let mut reader = Reader::new(&written[..]).unwrap();
        let mut block = Block::default();
        let mut max_cap = 0;
        while reader.next_block_into(&mut block).unwrap() {
            if block.data.capacity() > max_cap {
                max_cap = block.data.capacity();
            }
        }
        // After reading all blocks, capacity should be >= largest block data
        assert!(max_cap >= 500); // largest block is 500 bytes
    }
}