fn write_partial_fin_test()

in quic/s2n-quic-core/src/buffer/reassembler/tests.rs [872:964]


fn write_partial_fin_test() {
    for partial_size in INTERESTING_CHUNK_SIZES.iter().copied() {
        for fin_size in INTERESTING_CHUNK_SIZES.iter().copied() {
            for reverse in [false, true] {
                dbg!(partial_size, fin_size, reverse);

                let partial_bytes: Vec<u8> = Iterator::map(0..partial_size, |v| v as u8).collect();
                let fin_bytes: Vec<u8> = Iterator::map(0..fin_size, |v| v as u8).collect();

                let mut buffer = Reassembler::new();
                assert!(!buffer.is_writing_complete());
                assert!(!buffer.is_reading_complete());

                let mut oracle = Reassembler::new();

                let mut requests = vec![
                    (0u32, &partial_bytes, false),
                    (partial_size, &fin_bytes, true),
                ];

                if reverse {
                    requests.reverse();
                }

                for (offset, data, is_fin) in requests {
                    oracle.write_at(offset.into(), data).unwrap();
                    if is_fin {
                        buffer.write_at_fin(offset.into(), data).unwrap()
                    } else {
                        buffer.write_at(offset.into(), data).unwrap()
                    }
                }

                assert!(buffer.is_writing_complete());

                let mut results = vec![];

                for buf in [&mut buffer, &mut oracle] {
                    let mut chunks = vec![];
                    let mut actual_len = 0;

                    // look at how many bytes we actually allocated
                    let allocated_len: u64 = buf
                        .slots
                        .iter()
                        .map(|slot| slot.end_allocated() - slot.start())
                        .sum();

                    while let Some(chunk) = buf.pop() {
                        actual_len += chunk.len();
                        chunks.push(chunk);
                    }

                    assert_eq!(
                        (partial_size + fin_size) as usize,
                        actual_len,
                        "the number of received bytes should match"
                    );

                    results.push((chunks, actual_len, allocated_len));
                }

                assert!(buffer.is_reading_complete());

                let mut oracle_results = results.pop().unwrap();
                let actual_results = results.pop().unwrap();

                assert_eq!(
                    oracle_results.1, actual_results.1,
                    "the lengths should match"
                );

                // make sure the buffers match
                crate::slice::zip(&actual_results.0, &mut oracle_results.0, |a, b| {
                    assert_eq!(a, b, "all chunk bytes should match");
                });

                assert!(
                    oracle_results.2 >= actual_results.2,
                    "the actual allocations should be no worse than the oracle"
                );

                if reverse {
                    let ideal_allocation = (partial_size + fin_size) as u64;
                    assert_eq!(
                        actual_results.2, ideal_allocation,
                        "if the chunks were reversed, the allocation should be ideal"
                    );
                }
            }
        }
    }
}