Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return ErrBuf when records processed < actual records send by upstream #1493

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add shorter test case
  • Loading branch information
SriHarsha001 committed Nov 7, 2023
commit cee8881c99b3ec38431a636fa4dc0fa4f2a32f99
87 changes: 20 additions & 67 deletions msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,52 +326,8 @@ func TestLenDynamicA(t *testing.T) {
}

func TestUnPackForErrBuf(t *testing.T) {
SriHarsha001 marked this conversation as resolved.
Show resolved Hide resolved
var testCases = []struct {
domainName string
noOfARecords int
udpSize uint16
expectErrBuf bool
}{
// Testing with 15 characters in the domain name.
// ----------------------------------------------------------------------------------------------------------------------
// Length of response for 15 A records will be 482 bytes and UDPSize is 512. Response is not truncated.
{domainName: "example123.org.", noOfARecords: 15, udpSize: 512, expectErrBuf: false},

// Length of response for 16 A records will be 512 bytes and UDPSize is 512. Response is not truncated.
{domainName: "example123.org.", noOfARecords: 16, udpSize: 512, expectErrBuf: false},

// Length of response for 17 A records will be 542 bytes and UDPSize is 512. Response is truncated, so expect ErrBuf.
{domainName: "example123.org.", noOfARecords: 17, udpSize: 512, expectErrBuf: true},

// Length of response for 17 A records will be 542 bytes and UDPSize is 542. Response is not truncated.
{domainName: "example123.org.", noOfARecords: 17, udpSize: 542, expectErrBuf: false},

// Testing with 19 characters in the domain name.
// ----------------------------------------------------------------------------------------------------------------------
// Length of response for 13 A records will be 478 bytes and UDPSize is 512. Response is not truncated.
{domainName: "testingexample.org.", noOfARecords: 13, udpSize: 512, expectErrBuf: false},

// Length of response for 14 A records will be 512 bytes and UDPSize is 512. Response is not truncated.
{domainName: "testingexample.org.", noOfARecords: 14, udpSize: 512, expectErrBuf: false},

// Length of response for 15 A records will be 546 bytes and UDPSize is 512. Response is truncated, so expect ErrBuf.
{domainName: "testingexample.org.", noOfARecords: 15, udpSize: 512, expectErrBuf: true},

// Length of response for 15 A records will be 546 bytes and UDPSize is 546. Response is not truncated.
{domainName: "testingexample.org.", noOfARecords: 15, udpSize: 546, expectErrBuf: false},
}

for _, tc := range testCases {
testName := tc.domainName + " with " + strconv.Itoa(tc.noOfARecords) + " A-records"
t.Run(testName, func(t *testing.T) {
testLongResponseDNSServer(t, tc.domainName, tc.noOfARecords, tc.udpSize, tc.expectErrBuf)
})
}
}

func testLongResponseDNSServer(t *testing.T, domainName string, noOfARecords int, UDPSize uint16, expectErrBuf bool) {
HandleFunc(domainName, HelloServerLargeResponse)
defer HandleRemove(domainName)
HandleFunc("example123.org.", HelloServerLargeResponse)
defer HandleRemove("example123.org.")

srv, address, _, err := RunLocalUDPServer(":0")
if err != nil {
Expand All @@ -380,33 +336,30 @@ func testLongResponseDNSServer(t *testing.T, domainName string, noOfARecords int
defer srv.Shutdown()

reqm := new(Msg)
reqm.SetQuestion(domainName, TypeA)

reqm.SetQuestion("example123.org.", TypeA)
c := new(Client)
c.Net = "udp"
c.UDPSize = UDPSize

M.Lock()
M.max = noOfARecords
M.max = 17 // Response is truncated, so expect ErrBuf.
M.Unlock()

response := new(Msg)
response, _, err = c.Exchange(reqm, address)

if expectErrBuf {
if err != ErrBuf {
t.Error("Expected ErrBuf due to truncation")
}
} else {
_, _, err = c.Exchange(reqm, address)
if err != ErrBuf {
t.Error("Expected ErrBuf due to truncation")
}

if err != nil {
t.Errorf("failed to exchange: %v", err)
}
if len(response.Answer) != M.max {
t.Errorf("Expected no of A records %v, but got %v", len(response.Answer), M.max)
}
if response.Rcode != RcodeSuccess {
t.Errorf("Expected No error, but got %v", response.Rcode)
}
M.Lock()
M.max = 16 // Response is not truncated. so expect no error.
M.Unlock()
response, _, err = c.Exchange(reqm, address)
if err != nil {
t.Errorf("failed to exchange: %v", err)
}
if len(response.Answer) != M.max {
t.Errorf("Expected no of A records %v, but got %v", M.max, len(response.Answer))
}
if response.Rcode != RcodeSuccess {
t.Errorf("Expected No error, but got %v", response.Rcode)
}
}