dns/dnsmessage: improve test coverage of skipping

Updates golang/go#16218

Change-Id: I613cf0a65d40a9823c83e0a547fbd48b2236d16e
Reviewed-on: https://go-review.googlesource.com/49630
Reviewed-by: Mikio Hara <mikioh.mikioh@gmail.com>
Run-TryBot: Mikio Hara <mikioh.mikioh@gmail.com>
This commit is contained in:
Ian Gudger 2017-07-18 10:12:47 -07:00 коммит произвёл Mikio Hara
Родитель aa7a5b9517
Коммит 02ac38e252
1 изменённых файлов: 107 добавлений и 0 удалений

Просмотреть файл

@ -277,6 +277,69 @@ func TestSkipAll(t *testing.T) {
}
}
func TestSkipEach(t *testing.T) {
msg := smallTestMsg()
buf, err := msg.Pack()
if err != nil {
t.Fatal("Packing test message:", err)
}
var p Parser
if _, err := p.Start(buf); err != nil {
t.Fatal(err)
}
tests := []struct {
name string
f func() error
}{
{"SkipQuestion", p.SkipQuestion},
{"SkipAnswer", p.SkipAnswer},
{"SkipAuthority", p.SkipAuthority},
{"SkipAdditional", p.SkipAdditional},
}
for _, test := range tests {
if err := test.f(); err != nil {
t.Errorf("First call: got %s() = %v, want = %v", test.name, err, nil)
}
if err := test.f(); err != ErrSectionDone {
t.Errorf("Second call: got %s() = %v, want = %v", test.name, err, ErrSectionDone)
}
}
}
func TestSkipAfterRead(t *testing.T) {
msg := smallTestMsg()
buf, err := msg.Pack()
if err != nil {
t.Fatal("Packing test message:", err)
}
var p Parser
if _, err := p.Start(buf); err != nil {
t.Fatal(err)
}
tests := []struct {
name string
skip func() error
read func() error
}{
{"Question", p.SkipQuestion, func() error { _, err := p.Question(); return err }},
{"Answer", p.SkipAnswer, func() error { _, err := p.Answer(); return err }},
{"Authority", p.SkipAuthority, func() error { _, err := p.Authority(); return err }},
{"Additional", p.SkipAdditional, func() error { _, err := p.Additional(); return err }},
}
for _, test := range tests {
if err := test.read(); err != nil {
t.Errorf("Got %s() = _, %v, want = _, %v", test.name, err, nil)
}
if err := test.skip(); err != ErrSectionDone {
t.Errorf("Got Skip%s() = %v, want = %v", test.name, err, ErrSectionDone)
}
}
}
func TestSkipNotStarted(t *testing.T) {
var p Parser
@ -745,6 +808,50 @@ func BenchmarkBuilding(b *testing.B) {
}
}
func smallTestMsg() Message {
name := mustNewName("example.com.")
return Message{
Header: Header{Response: true, Authoritative: true},
Questions: []Question{
{
Name: name,
Type: TypeA,
Class: ClassINET,
},
},
Answers: []Resource{
{
ResourceHeader{
Name: name,
Type: TypeA,
Class: ClassINET,
},
&AResource{[4]byte{127, 0, 0, 1}},
},
},
Authorities: []Resource{
{
ResourceHeader{
Name: name,
Type: TypeA,
Class: ClassINET,
},
&AResource{[4]byte{127, 0, 0, 1}},
},
},
Additionals: []Resource{
{
ResourceHeader{
Name: name,
Type: TypeA,
Class: ClassINET,
},
&AResource{[4]byte{127, 0, 0, 1}},
},
},
}
}
func largeTestMsg() Message {
name := mustNewName("foo.bar.example.com.")
return Message{