Skip to content

Commit

Permalink
adds a test case of compressed positive response
Browse files Browse the repository at this point in the history
Add a test case where owner name in the A record is compressed.
  • Loading branch information
hunts committed May 29, 2024
1 parent 4469d5e commit 44f4218
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/base/message_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Name<Vec<u8>>>().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");
}
}

0 comments on commit 44f4218

Please sign in to comment.