libnetwork: assert DNS replies are well-formed

The well-formedness of a DNS message is only checked when it is
serialized, through the (*dns.Msg).Pack() method. Add a call to Pack()
to our tstwriter mock to mirror the behaviour of the real
dns.ResponseWriter implementation. And fix tests which generated
ill-formed DNS query messages.

Signed-off-by: Cory Snider <csnider@mirantis.com>
This commit is contained in:
Cory Snider 2023-12-19 11:13:35 -05:00
parent 0751141003
commit 1da85f7bdc
2 changed files with 7 additions and 3 deletions

View file

@ -38,6 +38,10 @@ type tstwriter struct {
}
func (w *tstwriter) WriteMsg(m *dns.Msg) (err error) {
// Assert that the message is serializable.
if _, err := m.Pack(); err != nil {
return err
}
w.msg = m
return nil
}

View file

@ -61,7 +61,7 @@ func TestDNSIPQuery(t *testing.T) {
// test name1's IP is resolved correctly with the default A type query
// Also make sure DNS lookups are case insensitive
names := []string{"name1", "NaMe1"}
names := []string{"name1.", "NaMe1."}
for _, name := range names {
q := new(dns.Msg)
q.SetQuestion(name, dns.TypeA)
@ -84,7 +84,7 @@ func TestDNSIPQuery(t *testing.T) {
// test MX query with name1 results in Success response with 0 answer records
q := new(dns.Msg)
q.SetQuestion("name1", dns.TypeMX)
q.SetQuestion("name1.", dns.TypeMX)
r.serveDNS(w, q)
resp := w.GetResponse()
checkNonNullResponse(t, resp)
@ -96,7 +96,7 @@ func TestDNSIPQuery(t *testing.T) {
// test MX query with non existent name results in ServFail response with 0 answer records
// since this is a unit test env, we disable proxying DNS above which results in ServFail rather than NXDOMAIN
q = new(dns.Msg)
q.SetQuestion("nonexistent", dns.TypeMX)
q.SetQuestion("nonexistent.", dns.TypeMX)
r.serveDNS(w, q)
resp = w.GetResponse()
checkNonNullResponse(t, resp)