From d27f0d3482b2cf0f8c610e47bada76eec058fee4 Mon Sep 17 00:00:00 2001 From: Tom Thorogood Date: Thu, 29 Nov 2018 09:53:00 +1030 Subject: [PATCH] Add a test case to cover escaping in the compression map --- length_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/length_test.go b/length_test.go index 17bca197..825af2a3 100644 --- a/length_test.go +++ b/length_test.go @@ -382,3 +382,26 @@ func TestMsgCompressLengthLargeRecordsAllValues(t *testing.T) { } } } + +func TestMsgCompressLengthEscapingMatch(t *testing.T) { + // Although slightly non-optimal, "example.org." and "ex\\097mple.org." + // are not considered equal in the compression map, even though \097 is + // a valid escaping of a. This test ensures that the Len code and the + // Pack don't disagree on this. + + msg := new(Msg) + msg.Compress = true + msg.SetQuestion("www.example.org.", TypeA) + msg.Answer = append(msg.Answer, &NS{Hdr: RR_Header{Name: "ex\\097mple.org.", Rrtype: TypeNS, Class: ClassINET}, Ns: "ns.example.org."}) + + predicted := msg.Len() + buf, err := msg.Pack() + if err != nil { + t.Error(err) + } + // Len doesn't account for escaping when calculating the length *yet* so + // we're off by three here. This will be fixed in a follow up change. + if predicted != len(buf)+3 { + t.Fatalf("predicted compressed length is wrong: predicted %d, actual %d", predicted, len(buf)) + } +}