|
| 1 | +// Regression test for the async PackedWrite run-length overflow bug. |
| 2 | +// |
| 3 | +// When a message contains more than 255 consecutive non-zero (or zero) words, |
| 4 | +// the run count byte must be capped at 255 to avoid silent truncation via |
| 5 | +// `as u8`. This test verifies that the async packed writer produces output |
| 6 | +// that can be correctly read back by both the async and synchronous packed |
| 7 | +// readers, even for large messages that exceed the 255-word run threshold. |
| 8 | + |
| 9 | +capnp::generated_code!(pub mod addressbook_capnp); |
| 10 | + |
| 11 | +#[cfg(test)] |
| 12 | +mod tests { |
| 13 | + use crate::addressbook_capnp::address_book; |
| 14 | + use capnp::message; |
| 15 | + use capnp::message::HeapAllocator; |
| 16 | + use capnp::serialize::OwnedSegments; |
| 17 | + |
| 18 | + fn populate_large_address_book(address_book: address_book::Builder) { |
| 19 | + let mut people = address_book.init_people(1); |
| 20 | + let mut entry = people.reborrow().get(0); |
| 21 | + |
| 22 | + // A long name ensures a big contiguous non-zero region. |
| 23 | + let long_name: String = "A".repeat(100_000); |
| 24 | + entry.set_name(&long_name); |
| 25 | + } |
| 26 | + |
| 27 | + fn verify_large_address_book(reader: address_book::Reader) { |
| 28 | + let people = reader.get_people().unwrap(); |
| 29 | + assert_eq!(people.len(), 1); |
| 30 | + let entry = people.get(0); |
| 31 | + |
| 32 | + let name = entry.get_name().unwrap().to_str().unwrap(); |
| 33 | + assert_eq!(name.len(), 100_000); |
| 34 | + assert!(name.chars().all(|c| c == 'A')); |
| 35 | + } |
| 36 | + |
| 37 | + fn write_sync(builder: &message::Builder<HeapAllocator>) -> Vec<u8> { |
| 38 | + let mut buf: Vec<u8> = Vec::new(); |
| 39 | + capnp::serialize_packed::write_message(&mut buf, builder).unwrap(); |
| 40 | + buf |
| 41 | + } |
| 42 | + |
| 43 | + fn write_async(builder: &message::Builder<HeapAllocator>) -> Vec<u8> { |
| 44 | + futures::executor::block_on(async { |
| 45 | + let mut buf: Vec<u8> = Vec::new(); |
| 46 | + capnp_futures::serialize_packed::write_message(&mut buf, builder) |
| 47 | + .await |
| 48 | + .unwrap(); |
| 49 | + buf |
| 50 | + }) |
| 51 | + } |
| 52 | + |
| 53 | + fn read_sync(buf: &[u8]) -> message::Reader<OwnedSegments> { |
| 54 | + capnp::serialize_packed::read_message(buf, message::DEFAULT_READER_OPTIONS).unwrap() |
| 55 | + } |
| 56 | + |
| 57 | + fn read_async(buf: &[u8]) -> message::Reader<OwnedSegments> { |
| 58 | + futures::executor::block_on(async { |
| 59 | + capnp_futures::serialize_packed::read_message(buf, message::DEFAULT_READER_OPTIONS) |
| 60 | + .await |
| 61 | + .unwrap() |
| 62 | + }) |
| 63 | + } |
| 64 | + |
| 65 | + #[test] |
| 66 | + fn test_write_sync_write_async_equivalence() { |
| 67 | + let mut builder = message::Builder::new(HeapAllocator::new()); |
| 68 | + populate_large_address_book(builder.init_root()); |
| 69 | + |
| 70 | + let sync_buf = write_sync(&builder); |
| 71 | + let async_buf = write_async(&builder); |
| 72 | + |
| 73 | + assert_eq!(sync_buf, async_buf); |
| 74 | + |
| 75 | + verify_large_address_book(read_sync(&sync_buf).get_root().unwrap()); |
| 76 | + verify_large_address_book(read_sync(&async_buf).get_root().unwrap()); |
| 77 | + |
| 78 | + verify_large_address_book(read_async(&sync_buf).get_root().unwrap()); |
| 79 | + verify_large_address_book(read_async(&async_buf).get_root().unwrap()); |
| 80 | + } |
| 81 | +} |
0 commit comments