mirror of
https://github.com/golang/go
synced 2024-11-12 07:40:23 -07:00
net: concatenate multiple TXT strings in single TXT record
When go resolver was changed to use dnsmessage.Parser, LookupTXT returned two strings in one record as two different records. This change reverts back to concatenating multiple strings in a single TXT record. Fixes #27763 Change-Id: Ice226fcb2be4be58853de34ed35b4627acb429ea Reviewed-on: https://go-review.googlesource.com/136955 Reviewed-by: Ian Gudger <igudger@google.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Ian Gudger <igudger@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
31d19c0ba3
commit
a6df1cece7
@ -1568,3 +1568,56 @@ func TestDNSDialTCP(t *testing.T) {
|
||||
t.Fatal("exhange failed:", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Issue 27763: verify that two strings in one TXT record are concatenated.
|
||||
func TestTXTRecordTwoStrings(t *testing.T) {
|
||||
fake := fakeDNSServer{
|
||||
rh: func(n, _ string, q dnsmessage.Message, _ time.Time) (dnsmessage.Message, error) {
|
||||
r := dnsmessage.Message{
|
||||
Header: dnsmessage.Header{
|
||||
ID: q.Header.ID,
|
||||
Response: true,
|
||||
RCode: dnsmessage.RCodeSuccess,
|
||||
},
|
||||
Questions: q.Questions,
|
||||
Answers: []dnsmessage.Resource{
|
||||
{
|
||||
Header: dnsmessage.ResourceHeader{
|
||||
Name: q.Questions[0].Name,
|
||||
Type: dnsmessage.TypeA,
|
||||
Class: dnsmessage.ClassINET,
|
||||
},
|
||||
Body: &dnsmessage.TXTResource{
|
||||
TXT: []string{"string1 ", "string2"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Header: dnsmessage.ResourceHeader{
|
||||
Name: q.Questions[0].Name,
|
||||
Type: dnsmessage.TypeA,
|
||||
Class: dnsmessage.ClassINET,
|
||||
},
|
||||
Body: &dnsmessage.TXTResource{
|
||||
TXT: []string{"onestring"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
return r, nil
|
||||
},
|
||||
}
|
||||
r := Resolver{PreferGo: true, Dial: fake.DialContext}
|
||||
txt, err := r.lookupTXT(context.Background(), "golang.org")
|
||||
if err != nil {
|
||||
t.Fatal("LookupTXT failed:", err)
|
||||
}
|
||||
if want := 2; len(txt) != want {
|
||||
t.Fatalf("len(txt), got %d, want %d", len(txt), want)
|
||||
}
|
||||
if want := "string1 string2"; txt[0] != want {
|
||||
t.Errorf("txt[0], got %q, want %q", txt[0], want)
|
||||
}
|
||||
if want := "onestring"; txt[1] != want {
|
||||
t.Errorf("txt[1], got %q, want %q", txt[1], want)
|
||||
}
|
||||
}
|
||||
|
@ -300,11 +300,21 @@ func (r *Resolver) lookupTXT(ctx context.Context, name string) ([]string, error)
|
||||
Server: server,
|
||||
}
|
||||
}
|
||||
if len(txts) == 0 {
|
||||
txts = txt.TXT
|
||||
} else {
|
||||
txts = append(txts, txt.TXT...)
|
||||
// Multiple strings in one TXT record need to be
|
||||
// concatenated without separator to be consistent
|
||||
// with previous Go resolver.
|
||||
n := 0
|
||||
for _, s := range txt.TXT {
|
||||
n += len(s)
|
||||
}
|
||||
txtJoin := make([]byte, 0, n)
|
||||
for _, s := range txt.TXT {
|
||||
txtJoin = append(txtJoin, s...)
|
||||
}
|
||||
if len(txts) == 0 {
|
||||
txts = make([]string, 0, 1)
|
||||
}
|
||||
txts = append(txts, string(txtJoin))
|
||||
}
|
||||
return txts, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user