Skip to content

Commit

Permalink
Add test to ensure query times out when server returns bad ID
Browse files Browse the repository at this point in the history
  • Loading branch information
AGWA committed Oct 12, 2020
1 parent ddbee29 commit aad691a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
25 changes: 25 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,31 @@ func TestClientSyncBadID(t *testing.T) {
m := new(Msg)
m.SetQuestion("miek.nl.", TypeSOA)

c := &Client{
Timeout: 1 * time.Second,
}
if _, _, err := c.Exchange(m, addrstr); err == nil || !strings.Contains(err.Error(), "timeout") {
t.Errorf("query did not time out")
}
// And now with plain Exchange().
if _, err = Exchange(m, addrstr); err == nil || !strings.Contains(err.Error(), "timeout") {
t.Errorf("query did not time out")
}
}

func TestClientSyncBadThenGoodID(t *testing.T) {
HandleFunc("miek.nl.", HelloServerBadThenGoodID)
defer HandleRemove("miek.nl.")

s, addrstr, err := RunLocalUDPServer(":0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer s.Shutdown()

m := new(Msg)
m.SetQuestion("miek.nl.", TypeSOA)

c := new(Client)
r, _, err := c.Exchange(m, addrstr)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ func HelloServerBadID(w ResponseWriter, req *Msg) {
m.Extra = make([]RR, 1)
m.Extra[0] = &TXT{Hdr: RR_Header{Name: m.Question[0].Name, Rrtype: TypeTXT, Class: ClassINET, Ttl: 0}, Txt: []string{"Hello world"}}
w.WriteMsg(m)
}

func HelloServerBadThenGoodID(w ResponseWriter, req *Msg) {
m := new(Msg)
m.SetReply(req)
m.Id++

m.Extra = make([]RR, 1)
m.Extra[0] = &TXT{Hdr: RR_Header{Name: m.Question[0].Name, Rrtype: TypeTXT, Class: ClassINET, Ttl: 0}, Txt: []string{"Hello world"}}
w.WriteMsg(m)

m.Id--
w.WriteMsg(m)
Expand Down

0 comments on commit aad691a

Please sign in to comment.