From c8773151c85620688029d66536ed8d47b42ef2c6 Mon Sep 17 00:00:00 2001 From: Jukka Taimisto Date: Thu, 17 Feb 2022 14:30:16 +0200 Subject: [PATCH] Add unit test to verify OPT record is skipped and rest are read --- dnsutils/dns_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/dnsutils/dns_test.go b/dnsutils/dns_test.go index aa02df07..3c9f38bc 100644 --- a/dnsutils/dns_test.go +++ b/dnsutils/dns_test.go @@ -721,6 +721,58 @@ func TestDecodeRdataSOA_Minimization(t *testing.T) { t.Errorf(" error returned: %v", err) } } +func TestDecodeQuestion_SkipOpt(t *testing.T) { + payload := []byte{ + 0x43, 0xac, 0x01, 0x00, 0x00, 0x01, 0x00, 0x02, + 0x00, 0x00, 0x00, 0x00, + // Query section + 0x0f, 0x64, 0x6e, 0x73, + 0x74, 0x61, 0x70, 0x63, 0x6f, 0x6c, 0x6c, 0x65, + 0x63, 0x74, 0x6f, 0x72, 0x04, 0x74, 0x65, 0x73, + 0x74, 0x00, 0x00, 0x01, 0x00, 0x01, + // Answer Resource Records + 0x0f, 0x64, + 0x6e, 0x73, 0x74, 0x61, 0x70, 0x63, 0x6f, 0x6c, + 0x6c, 0x65, 0x63, 0x74, 0x6f, 0x72, 0x04, 0x74, + 0x65, 0x73, 0x74, 0x00, + // type OPT, class IN + 0x00, 0x29, 0x00, 0x01, + // TTL + 0x00, 0x00, 0x0e, 0x10, + // RDLENGTH + 0x00, 0x01, + //RDATA + 0x01, + // 2nd resource record + 0x0f, 0x64, + 0x6e, 0x73, 0x74, 0x61, 0x70, 0x63, 0x6f, 0x6c, + 0x6c, 0x65, 0x63, 0x74, 0x6f, 0x72, 0x04, 0x74, + 0x65, 0x73, 0x74, 0x00, + // type A, class IN + 0x00, 0x01, 0x00, 0x01, + // TTL + 0x00, 0x00, 0x0e, 0x10, + // RDLENGTH + 0x00, 0x04, + // RDATA + 0x7f, 0x00, 0x00, 0x01, + } + _, _, offsetrr, err := DecodeQuestion(payload) + if err != nil { + t.Errorf("Unexpected error decoding question: %v", err) + } + + answer, _, erra := DecodeAnswer(2, offsetrr, payload) + if erra != nil { + t.Errorf("Unexpected error decoding answer: %v", erra) + } + if len(answer) != 1 { + t.Fatalf("Expected answer to contain one resource record, got %d", len(answer)) + } + if answer[0].Rdatatype != RdatatypeToString(0x01) || answer[0].Rdata != "127.0.0.1" { + t.Errorf("unexpected answer %s %s, expected A 127.0.0.1", answer[0].Rdatatype, answer[0].Rdata) + } +} func TestDecodeDns_HeaderTooShort(t *testing.T) { decoded := []byte{183, 59}