Merge pull request #42 from yalp/FixTxtLen

Fix length computation for TXT and a testcase.
This commit is contained in:
Miek Gieben 2013-06-06 11:36:11 -07:00
commit 3b95788578
2 changed files with 51 additions and 1 deletions

47
pack_test.go Normal file
View File

@ -0,0 +1,47 @@
package dns
import (
"testing"
)
func TestTxtPack(t *testing.T) {
// Test single entry TXT record
rr, err := NewRR(`_raop._tcp.local. 60 IN TXT "single value"`)
if err != nil {
t.Error("Failed to parse single value TXT record", err)
} else if rr, ok := rr.(*TXT); !ok {
t.Error("Wrong type, record should be of type TXT")
} else {
if len(rr.Txt) != 1 {
t.Error("Bad size of TXT value:", len(rr.Txt))
} else if rr.Txt[0] != "single value" {
t.Error("Bad single value")
}
if rr.String() != `_raop._tcp.local. 60 IN TXT "single value"` {
t.Error("Bad representation of TXT record:", rr.String())
}
if rr.len() == 10 {
t.Error("Bad size of serialized record:", rr.len())
}
}
// Test multi entries TXT record
rr, err = NewRR(`_raop._tcp.local. 60 IN TXT "a=1" "b=2" "c=3" "d=4"`)
if err != nil {
t.Error("Failed to parse multi-values TXT record", err)
} else if rr, ok := rr.(*TXT); !ok {
t.Error("Wrong type, record should be of type TXT")
} else {
if len(rr.Txt) != 4 {
t.Error("Bad size of TXT multi-value:", len(rr.Txt))
} else if rr.Txt[0] != "a=1" || rr.Txt[1] != "b=2" || rr.Txt[2] != "c=3" || rr.Txt[3] != "d=4" {
t.Error("Bad values in TXT records")
}
if rr.String() != `_raop._tcp.local. 60 IN TXT "a=1" "b=2" "c=3" "d=4"` {
t.Error("Bad representation of TXT multi value record:", rr.String())
}
if rr.len() != 43 {
t.Error("Bad size of serialized multi value record:", rr.len())
}
}
}

View File

@ -487,7 +487,10 @@ func (rr *TXT) String() string {
func (rr *TXT) len() int {
l := rr.Hdr.len()
for _, t := range rr.Txt {
for i, t := range rr.Txt {
if i > 0 {
l += 1
}
l += len(t)
}
return l