//! Normative chat length buckets: the ladder, the plaintext encoding, and the //! receiver's bounds. The wire behaviour these prove (a `rat ` frame of a //! bucketed size, a tampered field failing closed) lives in `src/node.rs`'s //! unit tests, because `tests/*.rs` cannot build a `Node`. use onionwire::padding::{self, BUCKETS, MAX_BODY, PAD_HEADER_LEN}; #[test] fn the_ladder_is_the_documented_one() { assert_eq!(BUCKETS, [256, 512, 1024, 2048, 4096, 8192, 16384, 32768]); assert_eq!(PAD_HEADER_LEN, 2); assert_eq!(MAX_BODY, 32766); } #[test] fn buckets_follow_the_body_plus_the_two_byte_header() { let cases = [ (0, Some(256)), (1, Some(256)), (253, Some(256)), (254, Some(256)), (255, Some(512)), (510, Some(512)), (511, Some(1024)), (1022, Some(1024)), (1023, Some(2048)), (2046, Some(2048)), (2047, Some(4096)), (4094, Some(4096)), (4095, Some(8192)), (8190, Some(8192)), (8191, Some(16384)), (16382, Some(16384)), (16383, Some(32768)), (32766, Some(32768)), (32767, None), (65535, None), ]; for (body_len, want) in cases { assert_eq!( padding::bucket_for(body_len), want, "bucket for a {body_len}-byte body" ); } } #[test] fn a_one_byte_message_round_trips_in_the_smallest_bucket() { let padded = padding::encode(b"x").expect("encode"); assert_eq!(padded.len(), 256); assert_eq!(padding::decode(&padded).expect("decode"), b"x"); } #[test] fn an_empty_message_round_trips_in_the_smallest_bucket() { let padded = padding::encode(b"").expect("encode"); assert_eq!(padded.len(), 256); assert_eq!(padding::decode(&padded).expect("decode"), b""); } #[test] fn an_exactly_bucket_sized_message_round_trips_with_no_pad() { let body = vec![b'a'; 254]; let padded = padding::encode(&body).expect("encode"); assert_eq!(padded.len(), 256); assert_eq!(&padded[..2], &[0u8, 0u8], "pad_len is zero at an exact fit"); assert_eq!(padding::decode(&padded).expect("decode"), &body[..]); } #[test] fn an_oversize_message_fails_closed() { let err = padding::encode(&vec![b'a'; MAX_BODY + 1]).expect_err("must not fit a bucket"); assert!(err.contains("too large"), "{err}"); } #[test] fn a_body_ending_in_the_pad_fill_byte_round_trips() { // Hand-built: `pad_len` is authoritative, so trailing bytes are never // trimmed and a body that ends in the same value as the fill survives. // The total is deliberately not a ladder value: receivers accept any size. let mut padded = Vec::new(); padded.extend_from_slice(&3u16.to_be_bytes()); padded.extend_from_slice(b"body\xaa"); padded.extend_from_slice(&[0xaa; 3]); assert_eq!(padded.len(), 2 + 5 + 3); assert_eq!(padding::decode(&padded).expect("decode"), b"body\xaa"); } #[test] fn a_pad_len_that_overruns_the_plaintext_is_rejected() { // 16 bytes total, pad_len says 15: one byte of body plus header is already // 3, so it overruns by 1. let mut bad = Vec::new(); bad.extend_from_slice(&15u16.to_be_bytes()); bad.extend_from_slice(&[0u8; 14]); let err = padding::decode(&bad).expect_err("must fail closed"); assert!(err.contains("overrun"), "{err}"); } #[test] fn a_truncated_padding_header_is_rejected() { assert!(padding::decode(b"").is_err()); assert!(padding::decode(b"\x00").is_err()); } #[test] fn a_receiver_accepts_a_padded_plaintext_of_any_total_length() { // Interop: another implementation's ladder is not ours, and the receiver // must not require a bucket-sized total, only a consistent pad_len. let mut padded = Vec::new(); padded.extend_from_slice(&30u16.to_be_bytes()); padded.extend_from_slice(&[b'z'; 268]); padded.extend_from_slice(&[0u8; 30]); assert_eq!(padded.len(), 300); assert_eq!(padding::decode(&padded).expect("decode").len(), 268); }