Skip to content

Commit

Permalink
Add unit test to verify OPT record is skipped and rest are read
Browse files Browse the repository at this point in the history
  • Loading branch information
jtt committed Feb 17, 2022
1 parent 408f0a9 commit c877315
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions dnsutils/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit c877315

Please sign in to comment.