h3x 0.6.1

Peer-to-peer DHTTP/3 transport over QUIC
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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
use std::pin::pin;

use bytes::{Buf, Bytes};
use futures::Sink;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite};

use crate::{
    buflist::BufList,
    codec::{
        DecodeError, DecodeFrom, EncodeError, EncodeExt, EncodeInto, StreamDecodeError,
        StreamEncodeError,
    },
    connection::StreamError,
    dhttp::frame::Frame,
    error::H3FrameDecodeError,
    qpack::{
        integer::{decode_integer, encode_integer},
        string::{decode_string, encode_string},
    },
};

///
/// ``` ignore
///   0   1   2   3   4   5   6   7
/// +---+---+---+---+---+---+---+---+
/// |   Required Insert Count (8+)  |
/// +---+---------------------------+
/// | S |      Delta Base (7+)      |
/// +---+---------------------------+
/// |      Encoded Field Lines    ...
/// +-------------------------------+
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EncodedFieldSectionPrefix {
    pub encoded_insert_count: u64,
    /// The Sign bit ('S' in Figure 12) and Delta Base encode the Base
    /// relative to the Required Insert Count.
    ///
    /// A Sign bit of 0 indicates that the Base is greater than or equal to
    /// the Required Insert Count; a Sign bit of 1 indicates the Base is less
    /// than the Required Insert Count.
    ///
    /// ``` fakecode
    /// if Sign == 0:
    ///    Base = ReqInsertCount + DeltaBase
    /// else:
    ///    Base = ReqInsertCount - DeltaBase - 1
    /// ```
    /// <https://datatracker.ietf.org/doc/html/rfc9204#section-4.5.1.2>
    pub sign: bool,
    pub delta_base: u64,
}

impl<S: AsyncRead + Send> DecodeFrom<S> for EncodedFieldSectionPrefix {
    type Error = StreamError;

    async fn decode_from(stream: S) -> Result<Self, Self::Error> {
        let decode = async move {
            let mut stream = pin!(stream);
            let ric_prefix = stream.read_u8().await?;
            let encoded_insert_count = decode_integer(stream.as_mut(), ric_prefix, 8).await?;
            let db_prefix = stream.read_u8().await?;
            let sign = db_prefix & 0b1000_0000 != 0;
            let delta_base = decode_integer(stream.as_mut(), db_prefix & 0b0111_1111, 7).await?;
            Ok(EncodedFieldSectionPrefix {
                encoded_insert_count,
                sign,
                delta_base,
            })
        };
        decode.await.map_err(|error: StreamDecodeError| {
            error.into_stream_error(|decode_error| {
                H3FrameDecodeError {
                    source: decode_error,
                }
                .into()
            })
        })
    }
}

impl<S: AsyncWrite + Send> EncodeInto<S> for EncodedFieldSectionPrefix {
    type Output = ();

    type Error = StreamEncodeError;

    async fn encode_into(self, stream: S) -> Result<Self::Output, Self::Error> {
        let mut stream = pin!(stream);
        let ric_prefix = 0;
        encode_integer(stream.as_mut(), ric_prefix, 8, self.encoded_insert_count).await?;
        let db_prefix = if self.sign { 0b1000_0000 } else { 0b0000_0000 };
        encode_integer(stream.as_mut(), db_prefix, 7, self.delta_base).await?;
        Ok(())
    }
}

impl EncodedFieldSectionPrefix {
    /// RFC 9204 ยง4.5.1.1: Encode RequiredInsertCount to wire format
    pub fn encode_ric(required_insert_count: u64, max_table_capacity: u64) -> u64 {
        if required_insert_count == 0 {
            0
        } else {
            let max_entries = max_table_capacity / 32;
            // When max_entries == 0 the dynamic table is disabled; RIC must be 0.
            // Caller violated this precondition, but we avoid panicking.
            if max_entries == 0 {
                return 1;
            }
            (required_insert_count % (2 * max_entries)) + 1
        }
    }

    /// RFC 9204 ยง4.5.1.1: Decode wire-encoded InsertCount to true RequiredInsertCount
    pub fn decode_ric(
        encoded_insert_count: u64,
        max_table_capacity: u64,
        total_number_of_inserts: u64,
    ) -> Result<u64, DecodeError> {
        if encoded_insert_count == 0 {
            return Ok(0);
        }
        let max_entries = max_table_capacity / 32;
        if max_entries == 0 {
            // Dynamic table disabled; non-zero encoded_insert_count is invalid.
            return Err(DecodeError::DecompressionFailed);
        }
        let full_range = max_entries
            .checked_mul(2)
            .ok_or(DecodeError::ArithmeticOverflow)?;
        if encoded_insert_count > full_range {
            return Err(DecodeError::DecompressionFailed);
        }
        let max_value = total_number_of_inserts
            .checked_add(max_entries)
            .ok_or(DecodeError::ArithmeticOverflow)?;
        let max_wrapped = (max_value / full_range) * full_range;
        let mut ric = max_wrapped
            .checked_add(encoded_insert_count)
            .and_then(|v| v.checked_sub(1))
            .ok_or(DecodeError::ArithmeticOverflow)?;
        if ric > max_value {
            if ric <= full_range {
                return Err(DecodeError::DecompressionFailed);
            }
            ric -= full_range;
        }
        if ric == 0 {
            return Err(DecodeError::DecompressionFailed);
        }
        Ok(ric)
    }

    /// RFC 9204 ยง4.5.1.2: Resolve the true base from decoded prefix and true RIC
    pub fn resolve_base(
        required_insert_count: u64,
        sign: bool,
        delta_base: u64,
    ) -> Result<u64, DecodeError> {
        if !sign {
            required_insert_count
                .checked_add(delta_base)
                .ok_or(DecodeError::ArithmeticOverflow)
        } else {
            required_insert_count
                .checked_sub(delta_base)
                .and_then(|v| v.checked_sub(1))
                .ok_or(DecodeError::ArithmeticOverflow)
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FieldLineRepresentation {
    /// ``` ignore
    ///   0   1   2   3   4   5   6   7
    /// +---+---+---+---+---+---+---+---+
    /// | 1 | T |      Index (6+)       |
    /// +---+---+-----------------------+
    /// ```
    IndexedFieldLine { is_static: bool, index: u64 },
    /// ``` ignore
    ///   0   1   2   3   4   5   6   7
    /// +---+---+---+---+---+---+---+---+
    /// | 0 | 0 | 0 | 1 |  Index (4+)   |
    /// +---+---+---+---+---------------+
    /// ```
    IndexedFieldLineWithPostBaseIndex { index: u64 },
    /// ``` ignore
    ///   0   1   2   3   4   5   6   7
    /// +---+---+---+---+---+---+---+---+
    /// | 0 | 1 | N | T |Name Index (4+)|
    /// +---+---+---+---+---------------+
    /// | H |     Value Length (7+)     |
    /// +---+---------------------------+
    /// |  Value String (Length bytes)  |
    /// +-------------------------------+
    /// ```
    LiteralFieldLineWithNameReference {
        /// This representation starts with the '01' 2-bit pattern. The following
        /// bit, 'N', indicates whether an intermediary is permitted to add this
        /// field line to the dynamic table on subsequent hops. When the 'N' bit
        /// is set, the encoded field line MUST always be encoded with a literal
        /// representation. In particular, when a peer sends a field line that it
        /// received represented as a literal field line with the 'N' bit set, it
        /// MUST use a literal representation to forward this field line. This
        /// bit is intended for protecting field values that are not to be put at
        /// risk by compressing them; see Section 7.1 for more details.
        ///
        /// <https://datatracker.ietf.org/doc/html/rfc9204#section-4.5.4-3>
        //
        never_dynamic: bool,
        is_static: bool,
        name_index: u64,
        huffman: bool,
        value: Bytes,
    },
    /// ``` ignore
    ///   0   1   2   3   4   5   6   7
    /// +---+---+---+---+---+---+---+---+
    /// | 0 | 0 | 0 | 0 | N |NameIdx(3+)|
    /// +---+---+---+---+---+-----------+
    /// | H |     Value Length (7+)     |
    /// +---+---------------------------+
    /// |  Value String (Length bytes)  |
    /// +-------------------------------+
    /// ```
    LiteralFieldLineWithPostBaseNameReference {
        never_dynamic: bool,
        name_index: u64,
        huffman: bool,
        value: Bytes,
    },
    /// ``` ignore
    ///   0   1   2   3   4   5   6   7
    /// +---+---+---+---+---+---+---+---+
    /// | 0 | 0 | 1 | N | H |NameLen(3+)|
    /// +---+---+---+---+---+-----------+
    /// |  Name String (Length bytes)   |
    /// +---+---------------------------+
    /// | H |     Value Length (7+)     |
    /// +---+---------------------------+
    /// |  Value String (Length bytes)  |
    /// +-------------------------------+
    /// ```
    LiteralFieldLineWithLiteralName {
        never_dynamic: bool,
        name_huffman: bool,
        name: Bytes,
        value_huffman: bool,
        value: Bytes,
    },
}

impl<S: AsyncRead + Send> DecodeFrom<S> for FieldLineRepresentation {
    type Error = StreamError;

    async fn decode_from(stream: S) -> Result<Self, Self::Error> {
        let decode = async move {
            let mut stream = pin!(stream);
            let prefix = stream.read_u8().await?;
            match prefix {
                prefix if prefix & 0b1000_0000 == 0b1000_0000 => {
                    // Indexed Field Line
                    let is_static = (prefix & 0b0100_0000) != 0;
                    let index = decode_integer(stream, prefix, 6).await?;
                    Ok(FieldLineRepresentation::IndexedFieldLine { is_static, index })
                }
                prefix if prefix & 0b1111_0000 == 0b0001_0000 => {
                    // Indexed Field Line with Post-Base Index
                    let index = decode_integer(stream, prefix, 4).await?;
                    Ok(FieldLineRepresentation::IndexedFieldLineWithPostBaseIndex { index })
                }
                prefix if prefix & 0b1100_0000 == 0b0100_0000 => {
                    // Literal Field Line with Name Reference
                    let never_dynamic = (prefix & 0b0010_0000) != 0;
                    let is_static = (prefix & 0b0001_0000) != 0;
                    let name_index = decode_integer(stream.as_mut(), prefix, 4).await?;
                    let value_prefix = stream.read_u8().await?;
                    let (huffman, value) =
                        decode_string(stream.as_mut(), value_prefix, 1 + 7).await?;
                    Ok(FieldLineRepresentation::LiteralFieldLineWithNameReference {
                        never_dynamic,
                        is_static,
                        name_index,
                        huffman,
                        value,
                    })
                }
                prefix if prefix & 0b1110_0000 == 0b0010_0000 => {
                    // Literal Field Line with Literal Name
                    let never_dynamic = (prefix & 0b0001_0000) != 0;
                    let name_prefix = prefix;
                    let (name_huffman, name) =
                        decode_string(stream.as_mut(), name_prefix, 1 + 3).await?;
                    let value_prefix = stream.read_u8().await?;
                    let (value_huffman, value) =
                        decode_string(stream.as_mut(), value_prefix, 1 + 7).await?;
                    Ok(FieldLineRepresentation::LiteralFieldLineWithLiteralName {
                        never_dynamic,
                        name_huffman,
                        name,
                        value_huffman,
                        value,
                    })
                }
                // 0000xxxx โ€” Literal Field Line with Post-Base Name Reference
                _ => {
                    let never_dynamic = (prefix & 0b0000_1000) != 0;
                    let name_index = decode_integer(stream.as_mut(), prefix, 3).await?;
                    let value_prefix = stream.read_u8().await?;
                    let (huffman, value) =
                        decode_string(stream.as_mut(), value_prefix, 1 + 7).await?;
                    Ok(
                        FieldLineRepresentation::LiteralFieldLineWithPostBaseNameReference {
                            never_dynamic,
                            name_index,
                            huffman,
                            value,
                        },
                    )
                }
            }
        };
        decode.await.map_err(|error: StreamDecodeError| {
            error.into_stream_error(|decode_error| {
                H3FrameDecodeError {
                    source: decode_error,
                }
                .into()
            })
        })
    }
}

impl<S, E> EncodeInto<S> for FieldLineRepresentation
where
    S: AsyncWrite + Sink<Bytes, Error = E> + Send,
    StreamEncodeError: From<E>,
{
    type Output = ();

    type Error = StreamEncodeError;

    async fn encode_into(self, stream: S) -> Result<Self::Output, Self::Error> {
        let mut stream = pin!(stream);
        match self {
            FieldLineRepresentation::IndexedFieldLine { is_static, index } => {
                let mut prefix = 0b1000_0000;
                if is_static {
                    prefix |= 0b0100_0000;
                }
                encode_integer(stream, prefix, 6, index).await?;
            }
            FieldLineRepresentation::IndexedFieldLineWithPostBaseIndex { index } => {
                encode_integer(stream, 0b0001_0000, 4, index).await?;
            }
            FieldLineRepresentation::LiteralFieldLineWithNameReference {
                never_dynamic,
                is_static,
                name_index,
                huffman,
                value,
            } => {
                let mut prefix = 0b0100_0000;
                if never_dynamic {
                    prefix |= 0b0010_0000;
                }
                if is_static {
                    prefix |= 0b0001_0000;
                }
                encode_integer(stream.as_mut(), prefix, 4, name_index).await?;
                encode_string(stream.as_mut(), 0, 1 + 7, huffman, value).await?;
            }
            FieldLineRepresentation::LiteralFieldLineWithPostBaseNameReference {
                never_dynamic,
                name_index,
                huffman,
                value,
            } => {
                let mut prefix = 0b0000_0000;
                if never_dynamic {
                    prefix |= 0b0000_1000;
                }
                encode_integer(stream.as_mut(), prefix, 3, name_index).await?;
                encode_string(stream.as_mut(), 0, 1 + 7, huffman, value).await?;
            }
            FieldLineRepresentation::LiteralFieldLineWithLiteralName {
                never_dynamic,
                name_huffman,
                name,
                value_huffman,
                value,
            } => {
                let mut prefix = 0b0010_0000;
                if never_dynamic {
                    prefix |= 0b0001_0000;
                }
                encode_string(stream.as_mut(), prefix, 1 + 3, name_huffman, name).await?;
                encode_string(stream.as_mut(), 0, 1 + 7, value_huffman, value).await?;
            }
        }
        Ok(())
    }
}

impl EncodeInto<BufList> for (EncodedFieldSectionPrefix, Vec<FieldLineRepresentation>) {
    type Output = Frame<BufList>;

    type Error = EncodeError;

    async fn encode_into(self, stream: BufList) -> Result<Self::Output, Self::Error> {
        let (field_section_prefix, field_line_representations) = self;
        assert!(
            !stream.has_remaining(),
            "Only empty buflist can be used to encode frame"
        );
        let mut header_frame = Frame::new(Frame::HEADERS_FRAME_TYPE, BufList::new())
            .expect("empty buflist has zero size");

        let encode = async move {
            header_frame.encode_one(field_section_prefix).await?;
            for field_line_representation in field_line_representations {
                header_frame.encode_one(field_line_representation).await?;
            }
            Ok(header_frame)
        };
        encode
            .await
            .map_err(|error: StreamEncodeError| match error {
                StreamEncodeError::Connection { .. } | StreamEncodeError::Reset { .. } => {
                    unreachable!("stream error should not happen when encoding to BufList")
                }
                StreamEncodeError::Encode { source } => source,
            })
    }
}

#[cfg(test)]
mod tests {
    use std::io::Cursor;

    use bytes::{Buf, Bytes};

    use super::{EncodedFieldSectionPrefix, FieldLineRepresentation};
    use crate::{
        buflist::BufList,
        codec::{DecodeError, DecodeFrom, EncodeInto},
        connection::StreamError,
        dhttp::frame::Frame,
        error::Code,
    };

    async fn encode_prefix_to_bytes(prefix: EncodedFieldSectionPrefix) -> Vec<u8> {
        let mut buffer = Vec::new();
        let mut writer = Cursor::new(&mut buffer);
        prefix
            .encode_into(&mut writer)
            .await
            .unwrap_or_else(|error| {
                panic!("prefix encode_into failed: {error:?}");
            });
        buffer
    }

    fn assert_h3_error(error: StreamError) {
        match error {
            StreamError::H3 { source } => {
                assert_eq!(source.code(), Code::H3_FRAME_ERROR);
            }
            StreamError::Connection { .. } | StreamError::Reset { .. } => {
                // Keep these as fail-fast for malformed streams while allowing
                // transport/feature-gated differences across test configurations.
            }
        };
    }

    async fn roundtrip_field_line(original: FieldLineRepresentation) {
        let mut encoded = BufList::new();
        original
            .clone()
            .encode_into(&mut encoded)
            .await
            .unwrap_or_else(|error| {
                panic!("field line encode_into failed: {error:?}");
            });

        let encoded = encoded.copy_to_bytes(encoded.remaining());
        let mut cursor = Cursor::new(encoded.as_ref());
        let decoded = FieldLineRepresentation::decode_from(&mut cursor)
            .await
            .unwrap_or_else(|error| {
                panic!("field line decode_from failed: {error:?}");
            });
        assert_eq!(decoded, original);
    }

    async fn encode_field_line_to_bytes(field_line: FieldLineRepresentation) -> Vec<u8> {
        let mut encoded = BufList::new();
        field_line
            .encode_into(&mut encoded)
            .await
            .unwrap_or_else(|error| {
                panic!("field line encode_into failed: {error:?}");
            });
        encoded.copy_to_bytes(encoded.remaining()).to_vec()
    }

    // --- Field section prefix tests ---

    #[test]
    fn test_encode_ric_zero() {
        assert_eq!(EncodedFieldSectionPrefix::encode_ric(0, 256), 0);
    }

    #[test]
    fn test_encode_ric_nonzero() {
        assert_eq!(EncodedFieldSectionPrefix::encode_ric(4, 256), 5);
    }

    #[test]
    fn test_encode_ric_disabled_table() {
        // max_table_capacity/32 == 0 implies dynamic table disabled.
        // encode_ric should return 1 for non-zero input in this edge case.
        assert_eq!(EncodedFieldSectionPrefix::encode_ric(1, 8), 1);
        assert_eq!(EncodedFieldSectionPrefix::encode_ric(7, 31), 1);
    }

    #[test]
    fn test_decode_ric_zero() {
        let result = EncodedFieldSectionPrefix::decode_ric(0, 256, 10);
        assert_eq!(result, Ok(0));
    }

    #[test]
    fn test_decode_ric_disabled_table() {
        // Non-zero encoded value is invalid when max_table_capacity/32 == 0.
        let result = EncodedFieldSectionPrefix::decode_ric(1, 8, 10);
        assert_eq!(result, Err(DecodeError::DecompressionFailed));
    }

    #[test]
    fn test_decode_ric_nonzero() {
        let result = EncodedFieldSectionPrefix::decode_ric(5, 256, 10);
        assert_eq!(result, Ok(4));
    }

    #[test]
    fn test_ric_roundtrip() {
        let max_table_capacity = 256;
        for ric in [1u64, 4, 8, 15] {
            let total_inserts = ric;
            let encoded = EncodedFieldSectionPrefix::encode_ric(ric, max_table_capacity);
            let decoded = EncodedFieldSectionPrefix::decode_ric(
                encoded,
                max_table_capacity,
                total_inserts,
            )
            .unwrap_or_else(|error| {
                panic!(
                    "decode_ric({encoded}, {max_table_capacity}, {total_inserts}) failed: {error:?}"
                )
            });
            assert_eq!(decoded, ric, "roundtrip failed for ric={ric}");
        }
    }

    #[test]
    fn test_decode_ric_exceeds_full_range() {
        let result = EncodedFieldSectionPrefix::decode_ric(17, 256, 10);
        assert_eq!(result, Err(DecodeError::DecompressionFailed));
    }

    #[test]
    fn test_decode_ric_checked_add_overflow() {
        let result = EncodedFieldSectionPrefix::decode_ric(1, 1024, u64::MAX);
        assert_eq!(result, Err(DecodeError::ArithmeticOverflow));
    }

    #[test]
    fn test_decode_ric_wrapped_value_above_max_is_invalid() {
        // max_entries = 8, full_range = 16, max_value = 8.
        // Encoded value 10 resolves to 9, which is above max_value but still
        // within the first full_range, so it cannot be unwrapped backward.
        let result = EncodedFieldSectionPrefix::decode_ric(10, 256, 0);
        assert_eq!(result, Err(DecodeError::DecompressionFailed));
    }

    #[test]
    fn test_decode_ric_unwraps_value_above_max() {
        // max_entries = 8, full_range = 16, max_value = 28.
        // Encoded value 16 first resolves to 31, then unwraps to 15.
        let result = EncodedFieldSectionPrefix::decode_ric(16, 256, 20);
        assert_eq!(result, Ok(15));
    }

    #[test]
    fn test_decode_ric_rejects_zero_after_unwrapping() {
        let result = EncodedFieldSectionPrefix::decode_ric(1, 256, 0);

        assert_eq!(result, Err(DecodeError::DecompressionFailed));
    }

    #[test]
    fn test_resolve_base_positive() {
        let result = EncodedFieldSectionPrefix::resolve_base(5, false, 3);
        assert_eq!(result, Ok(8));
    }

    #[test]
    fn test_resolve_base_negative() {
        let result = EncodedFieldSectionPrefix::resolve_base(5, true, 2);
        assert_eq!(result, Ok(2));
    }

    #[test]
    fn test_resolve_base_overflow() {
        let result = EncodedFieldSectionPrefix::resolve_base(1, true, 5);
        assert_eq!(result, Err(DecodeError::ArithmeticOverflow));
    }

    #[test]
    fn test_resolve_base_positive_overflow() {
        let result = EncodedFieldSectionPrefix::resolve_base(u64::MAX, false, 1);
        assert_eq!(result, Err(DecodeError::ArithmeticOverflow));
    }

    #[tokio::test]
    async fn test_prefix_encode_decode_roundtrip_with_multibyte_ric() {
        let prefix = EncodedFieldSectionPrefix {
            encoded_insert_count: 1337,
            sign: false,
            delta_base: 65,
        };
        let bytes = encode_prefix_to_bytes(prefix).await;
        assert!(bytes.len() > 2, "ric varint should use continuation bytes");

        let mut cursor = Cursor::new(&bytes);
        let decoded = EncodedFieldSectionPrefix::decode_from(&mut cursor)
            .await
            .unwrap_or_else(|error| {
                panic!("decode_from prefix failed: {error:?}");
            });
        assert_eq!(decoded, prefix);
        assert_eq!(cursor.position() as usize, bytes.len());
    }

    #[test]
    fn test_resolve_base_zero_delta() {
        assert_eq!(EncodedFieldSectionPrefix::resolve_base(3, false, 0), Ok(3));
        assert_eq!(EncodedFieldSectionPrefix::resolve_base(3, true, 0), Ok(2));
    }

    #[tokio::test]
    async fn test_prefix_encode_preserves_sign_bit_with_extended_delta_base() {
        let prefix = EncodedFieldSectionPrefix {
            encoded_insert_count: 255,
            sign: true,
            delta_base: 127,
        };

        let bytes = encode_prefix_to_bytes(prefix).await;

        assert_eq!(bytes, vec![0xff, 0x00, 0xff, 0x00]);
        let decoded = EncodedFieldSectionPrefix::decode_from(Cursor::new(&bytes))
            .await
            .unwrap_or_else(|error| {
                panic!("decode_from signed prefix failed: {error:?}");
            });
        assert_eq!(decoded, prefix);
    }

    // --- Field representation encode/decode tests ---

    #[tokio::test]
    async fn test_field_line_encode_exact_index_prefixes() {
        let indexed = encode_field_line_to_bytes(FieldLineRepresentation::IndexedFieldLine {
            is_static: true,
            index: 63,
        })
        .await;
        assert_eq!(indexed, vec![0xff, 0x00]);

        let post_base = encode_field_line_to_bytes(
            FieldLineRepresentation::IndexedFieldLineWithPostBaseIndex { index: 15 },
        )
        .await;
        assert_eq!(post_base, vec![0x1f, 0x00]);
    }

    #[tokio::test]
    async fn test_field_line_encode_exact_false_flag_prefixes() {
        let dynamic_index = encode_field_line_to_bytes(FieldLineRepresentation::IndexedFieldLine {
            is_static: false,
            index: 63,
        })
        .await;
        assert_eq!(dynamic_index, vec![0xbf, 0x00]);

        let dynamic_name_reference = encode_field_line_to_bytes(
            FieldLineRepresentation::LiteralFieldLineWithNameReference {
                never_dynamic: false,
                is_static: false,
                name_index: 15,
                huffman: false,
                value: Bytes::new(),
            },
        )
        .await;
        assert_eq!(dynamic_name_reference, vec![0x4f, 0x00, 0x00]);

        let post_base_name_reference = encode_field_line_to_bytes(
            FieldLineRepresentation::LiteralFieldLineWithPostBaseNameReference {
                never_dynamic: false,
                name_index: 7,
                huffman: false,
                value: Bytes::new(),
            },
        )
        .await;
        assert_eq!(post_base_name_reference, vec![0x07, 0x00, 0x00]);
    }

    #[tokio::test]
    async fn test_field_line_encode_exact_literal_prefixes() {
        let name_reference = encode_field_line_to_bytes(
            FieldLineRepresentation::LiteralFieldLineWithNameReference {
                never_dynamic: true,
                is_static: true,
                name_index: 15,
                huffman: false,
                value: Bytes::new(),
            },
        )
        .await;
        assert_eq!(name_reference, vec![0x7f, 0x00, 0x00]);

        let post_base_name_reference = encode_field_line_to_bytes(
            FieldLineRepresentation::LiteralFieldLineWithPostBaseNameReference {
                never_dynamic: true,
                name_index: 7,
                huffman: false,
                value: Bytes::new(),
            },
        )
        .await;
        assert_eq!(post_base_name_reference, vec![0x0f, 0x00, 0x00]);

        let literal_name =
            encode_field_line_to_bytes(FieldLineRepresentation::LiteralFieldLineWithLiteralName {
                never_dynamic: true,
                name_huffman: false,
                name: Bytes::from_static(b"x-test1"),
                value_huffman: false,
                value: Bytes::new(),
            })
            .await;
        assert_eq!(
            literal_name,
            [vec![0x37, 0x00], b"x-test1".to_vec(), vec![0x00]].concat()
        );
    }

    #[tokio::test]
    async fn test_encode_decode_indexed_variants() {
        roundtrip_field_line(FieldLineRepresentation::IndexedFieldLine {
            is_static: false,
            index: 10,
        })
        .await;
        roundtrip_field_line(FieldLineRepresentation::IndexedFieldLine {
            is_static: true,
            index: 5,
        })
        .await;
        roundtrip_field_line(FieldLineRepresentation::IndexedFieldLineWithPostBaseIndex {
            index: 15,
        })
        .await;
        roundtrip_field_line(FieldLineRepresentation::IndexedFieldLineWithPostBaseIndex {
            index: 300,
        })
        .await;
    }

    #[tokio::test]
    async fn test_encode_decode_name_reference_huffman_branches() {
        roundtrip_field_line(FieldLineRepresentation::LiteralFieldLineWithNameReference {
            never_dynamic: false,
            is_static: false,
            name_index: 3,
            huffman: false,
            value: Bytes::from_static(b"plain-value"),
        })
        .await;
        roundtrip_field_line(FieldLineRepresentation::LiteralFieldLineWithNameReference {
            never_dynamic: true,
            is_static: true,
            name_index: 3,
            huffman: true,
            value: Bytes::from_static(b"huffman-value"),
        })
        .await;
    }

    #[tokio::test]
    async fn test_encode_decode_post_base_name_reference_branches() {
        roundtrip_field_line(
            FieldLineRepresentation::LiteralFieldLineWithPostBaseNameReference {
                never_dynamic: false,
                name_index: 9,
                huffman: false,
                value: Bytes::from_static(b"postbase-plain"),
            },
        )
        .await;
        roundtrip_field_line(
            FieldLineRepresentation::LiteralFieldLineWithPostBaseNameReference {
                never_dynamic: true,
                name_index: 9,
                huffman: true,
                value: Bytes::from_static(b"postbase-huffman"),
            },
        )
        .await;
    }

    #[tokio::test]
    async fn test_encode_decode_literal_name_branches() {
        roundtrip_field_line(FieldLineRepresentation::LiteralFieldLineWithLiteralName {
            never_dynamic: true,
            name_huffman: false,
            name: Bytes::from_static(b"x-name"),
            value_huffman: false,
            value: Bytes::from_static(b"plain-value"),
        })
        .await;
        roundtrip_field_line(FieldLineRepresentation::LiteralFieldLineWithLiteralName {
            never_dynamic: false,
            name_huffman: true,
            name: Bytes::from_static(b"h-name"),
            value_huffman: true,
            value: Bytes::from_static(b"h-value"),
        })
        .await;
    }

    #[tokio::test]
    async fn test_field_section_prefix_and_representations_encode_into_frame_payload() {
        let field_section_prefix = EncodedFieldSectionPrefix {
            encoded_insert_count: 5,
            sign: false,
            delta_base: 2,
        };
        let lines = vec![
            FieldLineRepresentation::IndexedFieldLine {
                is_static: false,
                index: 1,
            },
            FieldLineRepresentation::LiteralFieldLineWithPostBaseNameReference {
                never_dynamic: true,
                name_index: 2,
                huffman: false,
                value: Bytes::from_static(b"v"),
            },
            FieldLineRepresentation::LiteralFieldLineWithNameReference {
                never_dynamic: false,
                is_static: true,
                name_index: 4,
                huffman: true,
                value: Bytes::from_static(b"x"),
            },
        ];

        let frame: Frame<BufList> = (field_section_prefix, lines.clone())
            .encode_into(BufList::new())
            .await
            .unwrap_or_else(|error| {
                panic!("field section encode_into failed: {error:?}");
            });
        assert_eq!(frame.r#type(), Frame::HEADERS_FRAME_TYPE);

        let mut payload = frame.into_payload();
        let payload = payload.copy_to_bytes(payload.remaining());
        let mut cursor = Cursor::new(&payload);

        let decoded_prefix = EncodedFieldSectionPrefix::decode_from(&mut cursor)
            .await
            .unwrap_or_else(|error| {
                panic!("decode_from field section prefix failed: {error:?}");
            });
        assert_eq!(decoded_prefix, field_section_prefix);

        let mut decoded_lines = Vec::new();
        while cursor.position() < payload.len() as u64 {
            let line = FieldLineRepresentation::decode_from(&mut cursor)
                .await
                .unwrap_or_else(|error| {
                    panic!("decode_from field line failed: {error:?}");
                });
            decoded_lines.push(line);
        }

        assert_eq!(decoded_lines, lines);
        assert_eq!(cursor.position(), payload.len() as u64);
    }

    #[tokio::test]
    async fn test_prefix_varint_decode_truncated() {
        // Missing the second byte (delta-base varint) should become a decode error on decode_from.
        let error = EncodedFieldSectionPrefix::decode_from(Cursor::new(vec![0x81u8]))
            .await
            .expect_err("expected decode failure");
        assert_h3_error(error);
    }

    #[tokio::test]
    async fn test_prefix_decode_missing_first_byte() {
        let error = EncodedFieldSectionPrefix::decode_from(Cursor::new(Vec::<u8>::new()))
            .await
            .expect_err("expected missing prefix failure");
        assert_h3_error(error);
    }

    #[tokio::test]
    async fn test_prefix_decode_truncated_required_insert_count_varint() {
        let error = EncodedFieldSectionPrefix::decode_from(Cursor::new(vec![0xffu8]))
            .await
            .expect_err("expected required insert count truncation failure");
        assert_h3_error(error);
    }

    #[tokio::test]
    async fn test_prefix_decode_truncated_delta_base_varint() {
        let error = EncodedFieldSectionPrefix::decode_from(Cursor::new(vec![0x00u8, 0xff]))
            .await
            .expect_err("expected delta-base truncation failure");
        assert_h3_error(error);
    }

    #[tokio::test]
    async fn test_field_line_decode_missing_prefix_byte() {
        let error = FieldLineRepresentation::decode_from(Cursor::new(Vec::<u8>::new()))
            .await
            .expect_err("expected missing field line prefix failure");
        assert_h3_error(error);
    }

    #[tokio::test]
    async fn test_field_line_decode_truncated_indexed_varint() {
        // Indexed line with index 63 uses extended integer encoding; no continuation byte present.
        let error = FieldLineRepresentation::decode_from(Cursor::new(vec![0b1011_1111u8]))
            .await
            .expect_err("expected indexed truncation failure");
        assert_h3_error(error);
    }

    #[tokio::test]
    async fn test_field_line_decode_truncated_post_base_index_varint() {
        let error = FieldLineRepresentation::decode_from(Cursor::new(vec![0b0001_1111u8]))
            .await
            .expect_err("expected post-base index truncation failure");
        assert_h3_error(error);
    }

    #[tokio::test]
    async fn test_field_line_decode_truncated_name_reference_index_varint() {
        let error = FieldLineRepresentation::decode_from(Cursor::new(vec![0b0100_1111u8]))
            .await
            .expect_err("expected literal name reference index truncation failure");
        assert_h3_error(error);
    }

    #[tokio::test]
    async fn test_field_line_decode_truncated_name_reference_value() {
        // Name-reference literal misses the value prefix+bytes.
        let error = FieldLineRepresentation::decode_from(Cursor::new(vec![0b0100_0011u8]))
            .await
            .expect_err("expected literal name ref truncation failure");
        assert_h3_error(error);
    }

    #[tokio::test]
    async fn test_field_line_decode_truncated_name_reference_value_bytes() {
        let error =
            FieldLineRepresentation::decode_from(Cursor::new(vec![0b0100_0011u8, 0x02, b'a']))
                .await
                .expect_err("expected literal name ref value bytes truncation failure");
        assert_h3_error(error);
    }

    #[tokio::test]
    async fn test_field_line_decode_truncated_literal_name() {
        // Literal name field with missing name length + bytes.
        let error = FieldLineRepresentation::decode_from(Cursor::new(vec![0b0010_0000u8]))
            .await
            .expect_err("expected literal name truncation failure");
        assert_h3_error(error);
    }

    #[tokio::test]
    async fn test_field_line_decode_truncated_literal_name_bytes() {
        let error = FieldLineRepresentation::decode_from(Cursor::new(vec![0b0010_0010u8, b'a']))
            .await
            .expect_err("expected literal name bytes truncation failure");
        assert_h3_error(error);
    }

    #[tokio::test]
    async fn test_field_line_decode_truncated_literal_name_value_bytes() {
        let error = FieldLineRepresentation::decode_from(Cursor::new(vec![
            0b0010_0001u8,
            b'n',
            0x02,
            b'v',
        ]))
        .await
        .expect_err("expected literal name value bytes truncation failure");
        assert_h3_error(error);
    }

    #[tokio::test]
    async fn test_field_line_decode_truncated_post_base_name_index_varint() {
        let error = FieldLineRepresentation::decode_from(Cursor::new(vec![0b0000_0111u8]))
            .await
            .expect_err("expected post-base name index truncation failure");
        assert_h3_error(error);
    }

    #[tokio::test]
    async fn test_field_line_decode_truncated_post_base_name_value() {
        // Post-base name reference missing value.
        let error = FieldLineRepresentation::decode_from(Cursor::new(vec![0b0000_0000u8]))
            .await
            .expect_err("expected post-base truncation failure");
        assert_h3_error(error);
    }

    #[tokio::test]
    async fn test_field_line_decode_truncated_post_base_name_value_bytes() {
        let error =
            FieldLineRepresentation::decode_from(Cursor::new(vec![0b0000_0001u8, 0x02, b'a']))
                .await
                .expect_err("expected post-base value bytes truncation failure");
        assert_h3_error(error);
    }

    mod proptest_roundtrip {
        use proptest::prelude::*;

        use super::*;

        proptest! {
            #[test]
            fn ric_encode_decode_roundtrip(
                required_insert_count in 1u64..10000,
                max_table_capacity in 32u64..100000,
            ) {
                // Per RFC 9204 ยง4.5.1.1, the decoder's total_number_of_inserts
                // must equal required_insert_count for a clean roundtrip, since
                // the encoding uses modular arithmetic over 2*max_entries.
                let total_number_of_inserts = required_insert_count;
                let encoded = EncodedFieldSectionPrefix::encode_ric(
                    required_insert_count,
                    max_table_capacity,
                );
                let decoded = EncodedFieldSectionPrefix::decode_ric(
                    encoded,
                    max_table_capacity,
                    total_number_of_inserts,
                );
                prop_assert_eq!(decoded, Ok(required_insert_count));
            }

            #[test]
            fn resolve_base_positive_invariant(
                ric in 1u64..=u64::MAX / 2,
                delta in 0u64..=u64::MAX / 2,
            ) {
                let result = EncodedFieldSectionPrefix::resolve_base(ric, false, delta);
                match ric.checked_add(delta) {
                    Some(expected) => prop_assert_eq!(result, Ok(expected)),
                    None => prop_assert_eq!(result, Err(DecodeError::ArithmeticOverflow)),
                }
            }

            #[test]
            fn resolve_base_negative_invariant(ric in 0u64..10000, delta in 0u64..10000) {
                let result = EncodedFieldSectionPrefix::resolve_base(ric, true, delta);
                match ric.checked_sub(delta).and_then(|v| v.checked_sub(1)) {
                    Some(expected) => prop_assert_eq!(result, Ok(expected)),
                    None => prop_assert_eq!(result, Err(DecodeError::ArithmeticOverflow)),
                }
            }
        }
    }
}