faucet-core 1.17.0

Shared types, traits, and utilities for the faucet-stream ecosystem
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
//! Bounds what one Avro datum may decode to before the Avro reader sees it.
//!
//! [`Bounded`] sits between the input and `apache_avro::Reader`. It reads the
//! container header and each block itself, decompresses the block, and walks
//! every datum against the writer schema, counting the values it would
//! produce. A datum whose values would pass `max_decoded_bytes` is refused
//! before it is decoded, which covers arrays of zero-width items (`null`, an
//! empty record) that take no input bytes. The reader then gets the same
//! file with each block already decompressed (the header's codec set to
//! `null`), so it decodes exactly as it would have.
//!
//! Input this module cannot make sense of is handed on unchanged, and the
//! reader reports it as before.

use super::budget;
use crate::error::FaucetError;
use apache_avro::schema::{
    InnerDecimalSchema, Name, NamespaceRef, RecordSchema, ResolvedSchema, UuidSchema,
};
use apache_avro::{Codec, Schema};
use std::cell::RefCell;
use std::collections::{HashMap, VecDeque};
use std::io::{self, Read};
use std::rc::Rc;
use std::str::FromStr;

/// Charged per decoded value: the Avro value and the JSON value it becomes.
const NODE: u64 = (std::mem::size_of::<apache_avro::types::Value>()
    + std::mem::size_of::<serde_json::Value>()) as u64;

/// Deepest nesting a datum may reach.
pub(crate) const MAX_DEPTH: usize = 1024;

/// The largest header or block handled; the Avro reader refuses more.
const MAX_SECTION: u64 = apache_avro::util::DEFAULT_MAX_ALLOCATION_BYTES as u64;

const MAGIC: [u8; 4] = [b'O', b'b', b'j', 1];

/// Where a refusal is left for the caller, since the Avro reader wraps the
/// I/O error it surfaces as.
pub(crate) type Failure = Rc<RefCell<Option<FaucetError>>>;

enum State {
    Header,
    Blocks,
    Passthrough,
    Done,
}

pub(crate) struct Bounded<R> {
    inner: R,
    limit: u64,
    failure: Failure,
    state: State,
    out: VecDeque<Vec<u8>>,
    pos: usize,
    recorded: Vec<u8>,
    sync: [u8; 16],
    codec: Codec,
    schema: Option<Schema>,
    names: HashMap<Name, Schema>,
    reader_schema: Option<Schema>,
}

impl<R: Read> Bounded<R> {
    pub(crate) fn new(
        inner: R,
        limit: u64,
        reader_schema: Option<&Schema>,
        failure: Failure,
    ) -> Self {
        Self {
            inner,
            limit,
            failure,
            state: State::Header,
            out: VecDeque::new(),
            pos: 0,
            recorded: Vec::new(),
            sync: [0; 16],
            codec: Codec::Null,
            schema: None,
            names: HashMap::new(),
            reader_schema: reader_schema.cloned(),
        }
    }

    fn fail(&self, e: FaucetError) -> io::Error {
        let msg = e.to_string();
        *self.failure.borrow_mut() = Some(e);
        io::Error::new(io::ErrorKind::InvalidData, msg)
    }

    /// Hand on everything read for the current section, then the rest.
    fn pass_through(&mut self) {
        self.out = VecDeque::from([std::mem::take(&mut self.recorded)]);
        self.pos = 0;
        self.state = State::Passthrough;
    }

    /// Read `n` bytes onto `recorded`; where they sit there, if all arrived.
    fn take_range(&mut self, n: u64) -> io::Result<Option<std::ops::Range<usize>>> {
        if n > MAX_SECTION {
            return Ok(None);
        }
        let start = self.recorded.len();
        (&mut self.inner).take(n).read_to_end(&mut self.recorded)?;
        let end = self.recorded.len();
        Ok(((end - start) as u64 == n).then_some(start..end))
    }

    fn take_exact(&mut self, n: u64) -> io::Result<Option<Vec<u8>>> {
        Ok(self.take_range(n)?.map(|r| self.recorded[r].to_vec()))
    }

    /// A zig-zag long; `Ok(None)` at a clean end of input or a malformed one.
    fn long(&mut self, eof_ok: &mut bool) -> io::Result<Option<i64>> {
        let mut value: u64 = 0;
        let mut shift = 0;
        loop {
            let mut b = [0u8; 1];
            if self.inner.read(&mut b)? == 0 {
                *eof_ok = shift == 0;
                return Ok(None);
            }
            self.recorded.push(b[0]);
            if shift >= 64 {
                return Ok(None);
            }
            value |= u64::from(b[0] & 0x7f) << shift;
            if b[0] & 0x80 == 0 {
                return Ok(Some(((value >> 1) as i64) ^ -((value & 1) as i64)));
            }
            shift += 7;
        }
    }

    fn header(&mut self) -> io::Result<()> {
        self.recorded.clear();
        let Some(magic) = self.take_exact(4)? else {
            self.pass_through();
            return Ok(());
        };
        if magic != MAGIC {
            self.pass_through();
            return Ok(());
        }
        let mut meta: Vec<(String, Vec<u8>)> = Vec::new();
        let mut eof = false;
        loop {
            let Some(count) = self.long(&mut eof)? else {
                self.pass_through();
                return Ok(());
            };
            if count == 0 {
                break;
            }
            if count < 0 && self.long(&mut eof)?.is_none() {
                self.pass_through();
                return Ok(());
            }
            for _ in 0..count.unsigned_abs() {
                let key = match self.long(&mut eof)? {
                    Some(n) if n >= 0 => self.take_exact(n as u64)?,
                    _ => None,
                };
                let value = match self.long(&mut eof)? {
                    Some(n) if n >= 0 => self.take_exact(n as u64)?,
                    _ => None,
                };
                let (Some(key), Some(value)) = (key, value) else {
                    self.pass_through();
                    return Ok(());
                };
                let Ok(key) = String::from_utf8(key) else {
                    self.pass_through();
                    return Ok(());
                };
                meta.push((key, value));
            }
        }
        let Some(sync) = self.take_exact(16)? else {
            self.pass_through();
            return Ok(());
        };
        let codec = match meta.iter().find(|(k, _)| k == "avro.codec") {
            None => Some(Codec::Null),
            Some((_, v)) => std::str::from_utf8(v)
                .ok()
                .and_then(|s| Codec::from_str(s).ok()),
        };
        let schema = meta
            .iter()
            .find(|(k, _)| k == "avro.schema")
            .and_then(|(_, v)| std::str::from_utf8(v).ok())
            .and_then(|json| self.writer_schema(json));
        if schema.is_none() && self.reader_schema.is_some() {
            return Err(self.fail(FaucetError::Source(
                "avro header: the writer schema does not resolve against the reader schema".into(),
            )));
        }
        let (Some(codec), Some((schema, names))) = (codec, schema) else {
            self.pass_through();
            return Ok(());
        };
        self.codec = codec;
        self.schema = Some(schema);
        self.names = names;
        self.sync.copy_from_slice(&sync);
        let kept: Vec<&(String, Vec<u8>)> = meta
            .iter()
            .filter(|(k, _)| k != "avro.codec" && k != "avro.codec.compression_level")
            .collect();
        let mut out = MAGIC.to_vec();
        put_long(&mut out, kept.len() as i64 + 1);
        for (k, v) in kept {
            put_bytes(&mut out, k.as_bytes());
            put_bytes(&mut out, v);
        }
        put_bytes(&mut out, b"avro.codec");
        put_bytes(&mut out, b"null");
        put_long(&mut out, 0);
        out.extend_from_slice(&self.sync);
        self.out = VecDeque::from([out]);
        self.pos = 0;
        self.state = State::Blocks;
        Ok(())
    }

    /// The writer schema and every type it names, resolved the way the Avro
    /// reader resolves it (against the reader schema's names when needed).
    fn writer_schema(&self, json: &str) -> Option<(Schema, HashMap<Name, Schema>)> {
        let own = |schema: Schema| {
            let names = ResolvedSchema::try_from(&schema)
                .ok()?
                .get_names()
                .iter()
                .map(|(n, s)| (n.clone(), (*s).clone()))
                .collect();
            Some((schema, names))
        };
        if let Some(found) = Schema::parse_str(json).ok().and_then(own) {
            return Some(found);
        }
        let reader = self.reader_schema.as_ref()?;
        let reader_json = serde_json::to_string(reader).ok()?;
        let (schema, list) = Schema::parse_str_with_list(json, [reader_json]).ok()?;
        let mut schemata = vec![&schema];
        schemata.extend(list.iter());
        let names = ResolvedSchema::new_with_schemata(schemata)
            .ok()?
            .get_names()
            .iter()
            .map(|(n, s)| (n.clone(), (*s).clone()))
            .collect();
        Some((schema, names))
    }

    fn block(&mut self) -> io::Result<()> {
        self.recorded.clear();
        let mut eof = false;
        let count = match self.long(&mut eof)? {
            Some(n) if n >= 0 => n,
            None if eof => {
                self.state = State::Done;
                return Ok(());
            }
            _ => {
                self.pass_through();
                return Ok(());
            }
        };
        let data = match self.long(&mut eof)? {
            Some(n) if n >= 0 => self.take_range(n as u64)?,
            _ => None,
        };
        let sync = self.take_range(16)?;
        let (Some(range), Some(sync)) = (data, sync) else {
            self.pass_through();
            return Ok(());
        };
        if self.recorded[sync] != self.sync {
            self.pass_through();
            return Ok(());
        }
        let mut data = std::mem::take(&mut self.recorded);
        data.truncate(range.end);
        data.drain(..range.start);
        if let Err(e) = self.codec.decompress(&mut data) {
            return Err(self.fail(FaucetError::Source(format!("avro block: {e}"))));
        }
        let schema = self.schema.as_ref().expect("set with the header");
        let mut census = Census {
            names: &self.names,
            limit: self.limit,
            used: 0,
        };
        let mut input: &[u8] = &data;
        for _ in 0..count {
            let before = input.len();
            census.used = 0;
            match census.walk(schema, None, &mut input, 0) {
                Ok(()) => {}
                Err(Stop::Malformed) => break,
                Err(Stop::Over) => {
                    return Err(self.fail(budget::exceeded("an avro record", self.limit)));
                }
                Err(Stop::TooDeep) => {
                    return Err(self.fail(FaucetError::Source(format!(
                        "an avro record nests deeper than {MAX_DEPTH} levels"
                    ))));
                }
            }
            if input.len() == before {
                break;
            }
        }
        let mut prefix = Vec::with_capacity(20);
        put_long(&mut prefix, count);
        put_long(&mut prefix, data.len() as i64);
        self.out = VecDeque::from([prefix, data, self.sync.to_vec()]);
        self.pos = 0;
        Ok(())
    }
}

impl<R: Read> Read for Bounded<R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        loop {
            if let Some(front) = self.out.front() {
                if self.pos < front.len() {
                    let n = buf.len().min(front.len() - self.pos);
                    buf[..n].copy_from_slice(&front[self.pos..self.pos + n]);
                    self.pos += n;
                    return Ok(n);
                }
                self.out.pop_front();
                self.pos = 0;
                continue;
            }
            match self.state {
                State::Header => self.header()?,
                State::Blocks => self.block()?,
                State::Passthrough => return self.inner.read(buf),
                State::Done => return Ok(0),
            }
        }
    }
}

fn put_long(out: &mut Vec<u8>, v: i64) {
    let mut z = ((v << 1) ^ (v >> 63)) as u64;
    loop {
        if z < 0x80 {
            out.push(z as u8);
            return;
        }
        out.push((z as u8 & 0x7f) | 0x80);
        z >>= 7;
    }
}

fn put_bytes(out: &mut Vec<u8>, b: &[u8]) {
    put_long(out, b.len() as i64);
    out.extend_from_slice(b);
}

enum Stop {
    Over,
    TooDeep,
    Malformed,
}

/// Counts the values one datum decodes to.
struct Census<'a> {
    names: &'a HashMap<Name, Schema>,
    limit: u64,
    used: u64,
}

impl Census<'_> {
    fn charge(&mut self, n: u64) -> Result<(), Stop> {
        self.used = self.used.saturating_add(n);
        if self.used > self.limit {
            return Err(Stop::Over);
        }
        Ok(())
    }

    fn walk(
        &mut self,
        s: &Schema,
        ns: NamespaceRef,
        input: &mut &[u8],
        depth: usize,
    ) -> Result<(), Stop> {
        if depth > MAX_DEPTH {
            return Err(Stop::TooDeep);
        }
        match s {
            Schema::Null => self.charge(NODE),
            Schema::Boolean => {
                skip(input, 1)?;
                self.charge(NODE)
            }
            Schema::Int
            | Schema::Long
            | Schema::Date
            | Schema::TimeMillis
            | Schema::TimeMicros
            | Schema::TimestampMillis
            | Schema::TimestampMicros
            | Schema::TimestampNanos
            | Schema::LocalTimestampMillis
            | Schema::LocalTimestampMicros
            | Schema::LocalTimestampNanos
            | Schema::Enum(_) => {
                long(input)?;
                self.charge(NODE)
            }
            Schema::Float => {
                skip(input, 4)?;
                self.charge(NODE)
            }
            Schema::Double => {
                skip(input, 8)?;
                self.charge(NODE)
            }
            Schema::Duration(_) => {
                skip(input, 12)?;
                self.charge(NODE)
            }
            Schema::Bytes
            | Schema::String
            | Schema::BigDecimal
            | Schema::Uuid(UuidSchema::Bytes | UuidSchema::String)
            | Schema::Decimal(apache_avro::schema::DecimalSchema {
                inner: InnerDecimalSchema::Bytes,
                ..
            }) => {
                let n = long(input)?;
                let n = u64::try_from(n).map_err(|_| Stop::Malformed)?;
                skip(input, n)?;
                self.charge(NODE.saturating_add(n.saturating_mul(2)))
            }
            Schema::Fixed(f)
            | Schema::Uuid(UuidSchema::Fixed(f))
            | Schema::Decimal(apache_avro::schema::DecimalSchema {
                inner: InnerDecimalSchema::Fixed(f),
                ..
            }) => {
                skip(input, f.size as u64)?;
                self.charge(NODE.saturating_add((f.size as u64).saturating_mul(2)))
            }
            Schema::Array(a) => {
                self.charge(NODE)?;
                self.items(input, ns, |c, input, ns| {
                    c.walk(&a.items, ns, input, depth + 1)
                })
            }
            Schema::Map(m) => {
                self.charge(NODE)?;
                self.items(input, ns, |c, input, ns| {
                    c.walk(&Schema::String, ns, input, depth + 1)?;
                    c.walk(&m.types, ns, input, depth + 1)
                })
            }
            Schema::Union(u) => {
                let i = long(input)?;
                let variant = usize::try_from(i)
                    .ok()
                    .and_then(|i| u.variants().get(i))
                    .ok_or(Stop::Malformed)?;
                self.charge(NODE)?;
                self.walk(variant, ns, input, depth + 1)
            }
            Schema::Record(RecordSchema { name, fields, .. }) => {
                self.charge(NODE)?;
                let full = name.fully_qualified_name(ns);
                for field in fields {
                    self.charge(field.name.len() as u64)?;
                    self.walk(&field.schema, full.namespace(), input, depth + 1)?;
                }
                Ok(())
            }
            Schema::Ref { name } => {
                let full = name.fully_qualified_name(ns);
                let resolved = self.names.get(full.as_ref()).ok_or(Stop::Malformed)?;
                self.walk(resolved, full.namespace(), input, depth + 1)
            }
        }
    }

    /// The blocks of an array or map. A run of items that take no input
    /// bytes is charged in one step rather than walked item by item.
    fn items(
        &mut self,
        input: &mut &[u8],
        ns: NamespaceRef,
        mut item: impl FnMut(&mut Self, &mut &[u8], NamespaceRef) -> Result<(), Stop>,
    ) -> Result<(), Stop> {
        loop {
            let mut count = long(input)?;
            if count == 0 {
                return Ok(());
            }
            if count < 0 {
                count = count.checked_neg().ok_or(Stop::Malformed)?;
                long(input)?;
            }
            let count = count as u64;
            if self.used.saturating_add(count.saturating_mul(NODE)) > self.limit {
                return Err(Stop::Over);
            }
            let mut i = 0;
            while i < count {
                let (bytes, used) = (input.len(), self.used);
                item(self, input, ns)?;
                i += 1;
                if input.len() == bytes {
                    let each = self.used - used;
                    self.charge(each.saturating_mul(count - i))?;
                    break;
                }
            }
        }
    }
}

fn skip(input: &mut &[u8], n: u64) -> Result<(), Stop> {
    let n = usize::try_from(n).map_err(|_| Stop::Malformed)?;
    if input.len() < n {
        return Err(Stop::Malformed);
    }
    *input = &input[n..];
    Ok(())
}

fn long(input: &mut &[u8]) -> Result<i64, Stop> {
    let mut value: u64 = 0;
    for (i, b) in input.iter().enumerate().take(10) {
        value |= u64::from(b & 0x7f) << (7 * i);
        if b & 0x80 == 0 {
            *input = &input[i + 1..];
            return Ok(((value >> 1) as i64) ^ -((value & 1) as i64));
        }
    }
    Err(Stop::Malformed)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::file_format::avro::read_records;
    use crate::file_format::{ContainerDecoder, FileFormat, FileInput, FormatOptions};
    use serde_json::{Value, json};

    const SYNC: [u8; 16] = [7; 16];

    fn ocf(schema: &str, blocks: &[(i64, Vec<u8>)]) -> Vec<u8> {
        let mut out = MAGIC.to_vec();
        put_long(&mut out, 2);
        put_bytes(&mut out, b"avro.schema");
        put_bytes(&mut out, schema.as_bytes());
        put_bytes(&mut out, b"avro.codec");
        put_bytes(&mut out, b"null");
        put_long(&mut out, 0);
        out.extend_from_slice(&SYNC);
        for (count, data) in blocks {
            put_long(&mut out, *count);
            put_long(&mut out, data.len() as i64);
            out.extend_from_slice(data);
            out.extend_from_slice(&SYNC);
        }
        out
    }

    fn longs(values: &[i64]) -> Vec<u8> {
        let mut out = Vec::new();
        for v in values {
            put_long(&mut out, *v);
        }
        out
    }

    fn read(bytes: &[u8], limit: u64) -> Result<Vec<Value>, FaucetError> {
        let mut out = Vec::new();
        let mut d = ContainerDecoder::new(FileFormat::Avro, &FormatOptions::default())?
            .with_max_decoded_bytes(limit);
        d.records("f.avro", FileInput::Bytes(bytes.to_vec()), 0, &mut |c| {
            out.extend(c);
            Ok(())
        })?;
        Ok(out)
    }

    const NULLS: &str = r#"{"type":"record","name":"r","fields":[{"name":"a","type":{"type":"array","items":"null"}}]}"#;

    #[test]
    fn a_huge_array_of_nulls_is_refused_before_it_is_decoded() {
        let bomb = ocf(NULLS, &[(1, longs(&[1 << 40, 0]))]);
        assert!(bomb.len() < 200, "{}", bomb.len());
        let err = read(&bomb, budget::MAX_DEFAULT_DECODED_BYTES).unwrap_err();
        assert!(err.to_string().contains("max_decoded_bytes"), "{err}");
        let err = read_records(&bomb[..], None, 10, &mut |_| Ok(())).unwrap_err();
        assert!(err.to_string().contains("max_decoded_bytes"), "{err}");
    }

    #[test]
    fn nested_arrays_of_nulls_are_refused_before_they_are_decoded() {
        let schema = r#"{"type":"record","name":"r","fields":[{"name":"a","type":{"type":"array","items":{"type":"array","items":"null"}}}]}"#;
        let mut datum = longs(&[1000]);
        for _ in 0..1000 {
            datum.extend(longs(&[4_000_000, 0]));
        }
        datum.extend(longs(&[0]));
        let bomb = ocf(schema, &[(1, datum)]);
        let err = read(&bomb, budget::MAX_DEFAULT_DECODED_BYTES).unwrap_err();
        assert!(err.to_string().contains("max_decoded_bytes"), "{err}");
    }

    #[test]
    fn arrays_of_empty_records_count_like_nulls() {
        let schema = r#"{"type":"record","name":"r","fields":[{"name":"a","type":{"type":"array","items":{"type":"record","name":"e","fields":[]}}}]}"#;
        let bomb = ocf(schema, &[(1, longs(&[-(1 << 40), 0, 0]))]);
        let err = read(&bomb, 1 << 20).unwrap_err();
        assert!(err.to_string().contains("max_decoded_bytes"), "{err}");
        let small = ocf(schema, &[(1, longs(&[3, 0]))]);
        assert_eq!(
            read(&small, 1 << 20).unwrap(),
            vec![json!({"a": [{}, {}, {}]})]
        );
    }

    #[test]
    fn zero_width_runs_are_charged_once_per_item() {
        let ok = ocf(NULLS, &[(1, longs(&[100, 0]))]);
        assert_eq!(
            read(&ok, 1 << 20).unwrap()[0]["a"]
                .as_array()
                .unwrap()
                .len(),
            100
        );
        let over = ocf(NULLS, &[(1, longs(&[10_000, 0]))]);
        let err = read(&over, 100 * NODE).unwrap_err();
        assert!(err.to_string().contains("max_decoded_bytes"), "{err}");
    }

    #[test]
    fn records_within_the_limit_decode_and_a_batch_over_it_is_refused() {
        let schema = r#"{"type":"record","name":"r","fields":[{"name":"s","type":"string"},{"name":"m","type":{"type":"map","values":"long"}},{"name":"u","type":["null","double"]},{"name":"f","type":{"type":"fixed","name":"x","size":2}}]}"#;
        let mut datum = longs(&[3]);
        datum.extend_from_slice(b"abc");
        datum.extend(longs(&[1, 1]));
        datum.push(b'k');
        datum.extend(longs(&[5, 0, 1]));
        datum.extend_from_slice(&1.5f64.to_le_bytes());
        datum.extend_from_slice(&[0, 1]);
        let file = ocf(schema, &[(2, [datum.clone(), datum].concat())]);
        let rows = read(&file, 1 << 20).unwrap();
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0]["s"], json!("abc"));
        assert_eq!(rows[0]["m"], json!({"k": 5}));
        let err = read(&file, 2 * NODE).unwrap_err();
        assert!(err.to_string().contains("max_decoded_bytes"), "{err}");
        assert!(err.to_string().contains("f.avro"), "{err}");
    }

    #[test]
    fn records_nested_past_the_depth_limit_are_refused() {
        let schema =
            r#"{"type":"record","name":"n","fields":[{"name":"next","type":["null","n"]}]}"#;
        let mut datum = longs(&vec![1; MAX_DEPTH + 10]);
        datum.extend(longs(&[0]));
        let err = read(&ocf(schema, &[(1, datum)]), 1 << 30).unwrap_err();
        assert!(err.to_string().contains("nests deeper"), "{err}");
    }

    #[test]
    fn compressed_blocks_are_bounded_too() {
        let schema = apache_avro::Schema::parse_str(NULLS).unwrap();
        let mut w = apache_avro::Writer::with_codec(
            &schema,
            Vec::new(),
            Codec::Deflate(Default::default()),
        )
        .unwrap();
        let mut rec = apache_avro::types::Record::new(&schema).unwrap();
        rec.put(
            "a",
            apache_avro::types::Value::Array(vec![apache_avro::types::Value::Null; 5000]),
        );
        w.append_value(rec).unwrap();
        let file = w.into_inner().unwrap();
        assert_eq!(
            read(&file, 1 << 30).unwrap()[0]["a"]
                .as_array()
                .unwrap()
                .len(),
            5000
        );
        let err = read(&file, 1000 * NODE).unwrap_err();
        assert!(err.to_string().contains("max_decoded_bytes"), "{err}");
    }

    #[test]
    fn input_it_cannot_read_reaches_the_avro_reader_unchanged() {
        let err = read(b"not avro at all", 1 << 20).unwrap_err();
        assert!(err.to_string().contains("avro header"), "{err}");
        let mut truncated = ocf(NULLS, &[(1, longs(&[2, 0]))]);
        truncated.truncate(truncated.len() - 4);
        assert!(read(&truncated, 1 << 20).is_err());
        let mut bad_sync = ocf(NULLS, &[(1, longs(&[2, 0]))]);
        let n = bad_sync.len();
        bad_sync[n - 1] = 0;
        assert!(read(&bad_sync, 1 << 20).is_err());
        let unknown = ocf(
            r#"{"type":"record","name":"r","fields":[{"name":"a","type":"missing"}]}"#,
            &[],
        );
        assert!(read(&unknown, 1 << 20).is_err());
        assert!(read(&ocf(NULLS, &[(-1, vec![])]), 1 << 20).is_err());
        let reader = apache_avro::Schema::parse_str(NULLS).unwrap();
        let err = read_records(&unknown[..], Some(&reader), 10, &mut |_| Ok(())).unwrap_err();
        assert!(err.to_string().contains("does not resolve"), "{err}");
        assert!(read(&ocf(NULLS, &[]), 1 << 20).unwrap().is_empty());
    }

    #[test]
    fn uncompressed_blocks_pass_through_byte_for_byte() {
        let file = ocf(
            NULLS,
            &[(1, longs(&[2, 0])), (2, longs(&[1, 0, 0])), (0, vec![])],
        );
        let mut bounded = Bounded::new(&file[..], 1 << 20, None, Failure::default());
        let mut seen = Vec::new();
        let mut byte = [0u8; 1];
        while bounded.read(&mut byte).unwrap() == 1 {
            seen.push(byte[0]);
        }
        assert_eq!(seen, file);
    }

    #[test]
    fn varints_round_trip() {
        for v in [0i64, 1, -1, 63, -64, 64, 1 << 40, i64::MAX, i64::MIN] {
            let bytes = longs(&[v]);
            let mut input: &[u8] = &bytes;
            assert_eq!(long(&mut input).ok(), Some(v));
            assert!(input.is_empty());
        }
        let mut input: &[u8] = &[0x80; 11];
        assert!(matches!(long(&mut input), Err(Stop::Malformed)));
    }
}