From 02ac38e2528ff4adea90f184d71a3faa04b4b1b0 Mon Sep 17 00:00:00 2001 From: Ian Gudger Date: Tue, 18 Jul 2017 10:12:47 -0700 Subject: [PATCH] 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 Run-TryBot: Mikio Hara --- dns/dnsmessage/message_test.go | 107 +++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/dns/dnsmessage/message_test.go b/dns/dnsmessage/message_test.go index 0f98daa7..9295d36c 100644 --- a/dns/dnsmessage/message_test.go +++ b/dns/dnsmessage/message_test.go @@ -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{