diff --git a/src/base/message_builder.rs b/src/base/message_builder.rs index a3ef26ce7..b6707c609 100644 --- a/src/base/message_builder.rs +++ b/src/base/message_builder.rs @@ -2449,4 +2449,50 @@ mod test { let msg = create_compressed(TreeCompressor::new(Vec::new())); assert_eq!(&expect[..], msg.as_ref()); } + + #[test] + fn compress_positive_response() { + // An example positive response to `A example.com.` that is compressed + // + // ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 0 + // ;; flags: qr rd ra ad; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0 + // + // ;; QUESTION SECTION: + // ;example.com. IN A + // + // ;; ANSWER SECTION: + // example.com. 3600 IN A 203.0.113.1 + // + // ;; MSG SIZE rcvd: 45 + let expect = &[ + 0x00, 0x00, 0x81, 0xa0, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03, 0x63, + 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x01, + 0x00, 0x01, 0x00, 0x00, 0x0e, 0x10, 0x00, 0x04, 0xcb, 0x00, 0x71, + 0x01, + ]; + + let name = "example.com.".parse::>>().unwrap(); + let mut msg = + MessageBuilder::from_target(StaticCompressor::new(Vec::new())) + .unwrap() + .question(); + msg.header_mut().set_rcode(Rcode::NOERROR); + msg.header_mut().set_rd(true); + msg.header_mut().set_ra(true); + msg.header_mut().set_qr(true); + msg.header_mut().set_ad(true); + + // Question + msg.push((name.clone(), Rtype::A)).unwrap(); + + // Answer + let mut msg = msg.answer(); + msg.push((name.clone(), 3600, A::from_octets(203, 0, 113, 1))) + .unwrap(); + + let actual = msg.finish().into_target(); + assert_eq!(45, actual.len(), "unexpected response size"); + assert_eq!(expect[..], actual, "unexpected response data"); + } }